blob: 5a96132217a68ba75bd0cd7b5be3ed1aa10a10e0 [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>
Chris Craik98d608d2014-07-17 12:25:11 -070024#include <SkColor.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040025#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070026#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070027
Romain Guye4d01122010-06-16 18:44:05 -070028#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070029#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070030
Romain Guy08aa2cb2011-03-17 11:06:57 -070031#include <private/hwui/DrawGlInfo.h>
32
Romain Guy5b3b3522010-10-27 18:57:51 -070033#include <ui/Rect.h>
34
Romain Guy85bf02f2010-06-22 13:11:24 -070035#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080036#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080037#include "DisplayListRenderer.h"
Romain Guy40543602013-06-12 15:31:28 -070038#include "Fence.h"
John Reck3b202512014-06-23 13:13:08 -070039#include "RenderState.h"
Chris Craik65cd6122012-12-10 17:56:27 -080040#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070041#include "Properties.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080042#include "ShadowTessellator.h"
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040043#include "SkiaShader.h"
Chris Craike4aa95e2014-05-08 13:57:05 -070044#include "utils/GLUtils.h"
Romain Guya957eea2010-12-08 18:34:42 -080045#include "Vector.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080046#include "VertexBuffer.h"
Romain Guye4d01122010-06-16 18:44:05 -070047
48namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070049namespace uirenderer {
50
51///////////////////////////////////////////////////////////////////////////////
52// Defines
53///////////////////////////////////////////////////////////////////////////////
54
Romain Guy759ea802010-09-16 20:49:46 -070055#define RAD_TO_DEG (180.0f / 3.14159265f)
56#define MIN_ANGLE 0.001f
57
Romain Guyf8773082012-07-12 18:01:00 -070058#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070059
Chris Craik678625242014-02-28 12:26:34 -080060static GLenum getFilter(const SkPaint* paint) {
61 if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) {
62 return GL_LINEAR;
63 }
64 return GL_NEAREST;
65}
Romain Guyd21b6e12011-11-30 20:21:23 -080066
Romain Guy9d5316e2010-06-24 19:30:36 -070067///////////////////////////////////////////////////////////////////////////////
68// Globals
69///////////////////////////////////////////////////////////////////////////////
70
Romain Guy889f8d12010-07-29 14:37:42 -070071/**
72 * Structure mapping Skia xfermodes to OpenGL blending factors.
73 */
74struct Blender {
75 SkXfermode::Mode mode;
76 GLenum src;
77 GLenum dst;
78}; // struct Blender
79
Romain Guy026c5e162010-06-28 17:12:22 -070080// In this array, the index of each Blender equals the value of the first
81// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
82static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070083 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
84 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
85 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
86 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
87 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
88 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
89 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
90 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
92 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
93 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
94 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050096 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070097 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070098};
Romain Guye4d01122010-06-16 18:44:05 -070099
Romain Guy87a76572010-09-13 18:11:21 -0700100// This array contains the swapped version of each SkXfermode. For instance
101// this array's SrcOver blending mode is actually DstOver. You can refer to
102// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -0700103static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -0700104 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
105 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
106 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
107 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
108 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
109 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
110 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
111 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
112 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
113 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
114 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
115 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
116 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500117 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700118 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700119};
120
Romain Guyf6a11b82010-06-23 17:47:49 -0700121///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700122// Functions
123///////////////////////////////////////////////////////////////////////////////
124
125template<typename T>
126static inline T min(T a, T b) {
127 return a < b ? a : b;
128}
129
130///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700131// Constructors/destructor
132///////////////////////////////////////////////////////////////////////////////
133
John Reck3b202512014-06-23 13:13:08 -0700134OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
135 : mCaches(Caches::getInstance())
136 , mExtensions(Extensions::getInstance())
137 , mRenderState(renderState) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800138 // *set* draw modifiers to be 0
139 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700140 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700141
Romain Guyac670c02010-07-27 17:39:27 -0700142 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
143
Romain Guy96885eb2013-03-26 15:05:58 -0700144 mFrameStarted = false;
Romain Guy78dd96d2013-05-03 14:24:16 -0700145 mCountOverdraw = false;
Romain Guy87e2f7572012-09-24 11:37:12 -0700146
147 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700148}
149
Romain Guy85bf02f2010-06-22 13:11:24 -0700150OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700151 // The context has already been destroyed at this point, do not call
152 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700153}
154
Romain Guy87e2f7572012-09-24 11:37:12 -0700155void OpenGLRenderer::initProperties() {
156 char property[PROPERTY_VALUE_MAX];
157 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
158 mScissorOptimizationDisabled = !strcasecmp(property, "true");
159 INIT_LOGD(" Scissor optimization %s",
160 mScissorOptimizationDisabled ? "disabled" : "enabled");
161 } else {
162 INIT_LOGD(" Scissor optimization enabled");
163 }
Romain Guy13631f32012-01-30 17:41:55 -0800164}
165
166///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700167// Setup
168///////////////////////////////////////////////////////////////////////////////
169
Chris Craik797b95b22014-05-20 18:10:25 -0700170void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700171 glDisable(GL_DITHER);
172 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
173
174 glEnableVertexAttribArray(Program::kBindingPosition);
175}
176
Romain Guy96885eb2013-03-26 15:05:58 -0700177void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800178 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800179 mCaches.clearGarbage();
180
Chris Craik14e51302013-12-30 15:32:54 -0800181 initializeSaveStack(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700182 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700183 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700184}
185
186status_t OpenGLRenderer::startFrame() {
187 if (mFrameStarted) return DrawGlInfo::kStatusDone;
188 mFrameStarted = true;
189
Romain Guy41308e22012-10-22 20:02:43 -0700190 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700191
Romain Guy96885eb2013-03-26 15:05:58 -0700192 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700193
John Reck3b202512014-06-23 13:13:08 -0700194 mRenderState.setViewport(getWidth(), getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800195
Romain Guy54c1a642012-09-27 17:55:46 -0700196 // Functors break the tiling extension in pretty spectacular ways
197 // This ensures we don't use tiling when a functor is going to be
198 // invoked during the frame
199 mSuppressTiling = mCaches.hasRegisteredFunctors();
200
Chris Craikd6b65f62014-01-01 14:45:21 -0800201 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700202
Romain Guy7c450aa2012-09-21 19:15:00 -0700203 debugOverdraw(true, true);
204
Romain Guy96885eb2013-03-26 15:05:58 -0700205 return clear(mTilingClip.left, mTilingClip.top,
206 mTilingClip.right, mTilingClip.bottom, mOpaque);
207}
208
Romain Guy96885eb2013-03-26 15:05:58 -0700209status_t OpenGLRenderer::prepareDirty(float left, float top,
210 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700211
Romain Guy96885eb2013-03-26 15:05:58 -0700212 setupFrameState(left, top, right, bottom, opaque);
213
214 // Layer renderers will start the frame immediately
215 // The framebuffer renderer will first defer the display list
216 // for each layer and wait until the first drawing command
217 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800218 if (currentSnapshot()->fbo == 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700219 syncState();
220 updateLayers();
221 } else {
222 return startFrame();
223 }
224
225 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700226}
227
Romain Guydcfc8362013-01-03 13:08:57 -0800228void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
229 // If we know that we are going to redraw the entire framebuffer,
230 // perform a discard to let the driver know we don't need to preserve
231 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800232 if (mExtensions.hasDiscardFramebuffer() &&
Chris Craik14e51302013-12-30 15:32:54 -0800233 left <= 0.0f && top <= 0.0f && right >= getWidth() && bottom >= getHeight()) {
Romain Guyf1581982013-01-31 17:20:30 -0800234 const bool isFbo = getTargetFbo() == 0;
235 const GLenum attachments[] = {
236 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
237 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800238 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
239 }
240}
241
Romain Guy7c25aab2012-10-18 15:05:02 -0700242status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700243 if (!opaque || mCountOverdraw) {
Romain Guy586cae32012-07-13 15:28:31 -0700244 mCaches.enableScissor();
Chris Craika64a2be2014-05-14 14:17:01 -0700245 mCaches.setScissor(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700246 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700247 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700248 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700249
Romain Guy7c25aab2012-10-18 15:05:02 -0700250 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700251 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700252}
253
254void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700255 if (mCaches.blend) {
256 glEnable(GL_BLEND);
257 } else {
258 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700259 }
Romain Guybb9524b2010-06-22 18:56:38 -0700260}
261
henry.uh_chen33f5a592014-07-02 19:36:56 +0800262void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700263 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800264 const Snapshot* snapshot = currentSnapshot();
265
Chris Craik14e51302013-12-30 15:32:54 -0800266 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800267 if (snapshot->flags & Snapshot::kFlagFboTarget) {
268 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700269 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700270
henry.uh_chen33f5a592014-07-02 19:36:56 +0800271 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800272 }
273}
274
henry.uh_chen33f5a592014-07-02 19:36:56 +0800275void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800276 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800277 if(expand) {
278 // Expand the startTiling region by 1
279 int leftNotZero = (clip.left > 0) ? 1 : 0;
280 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
281
282 mCaches.startTiling(
283 clip.left - leftNotZero,
284 windowHeight - clip.bottom - topNotZero,
285 clip.right - clip.left + leftNotZero + 1,
286 clip.bottom - clip.top + topNotZero + 1,
287 opaque);
288 } else {
289 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800290 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800291 }
Romain Guy54c1a642012-09-27 17:55:46 -0700292 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700293}
294
295void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700296 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700297}
298
Romain Guyb025b9c2010-09-16 14:16:48 -0700299void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700300 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700301 endTiling();
302
Romain Guyca89e2a2013-03-08 17:44:20 -0800303 // When finish() is invoked on FBO 0 we've reached the end
304 // of the current frame
305 if (getTargetFbo() == 0) {
306 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700307 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800308 }
309
Romain Guy11cb6422012-09-21 00:39:43 -0700310 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700311#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700312 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700313#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700314
Romain Guyc15008e2010-11-10 11:59:15 -0800315#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800316 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700317#else
318 if (mCaches.getDebugLevel() & kDebugMemory) {
319 mCaches.dumpMemoryUsage();
320 }
Romain Guyc15008e2010-11-10 11:59:15 -0800321#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700322 }
Romain Guy96885eb2013-03-26 15:05:58 -0700323
Romain Guy78dd96d2013-05-03 14:24:16 -0700324 if (mCountOverdraw) {
325 countOverdraw();
326 }
327
Romain Guy96885eb2013-03-26 15:05:58 -0700328 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700329}
330
Romain Guy35643dd2012-09-18 15:40:58 -0700331void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700332 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
333 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700334 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700335
336 mCaches.resetScissor();
337 dirtyClip();
338}
339
Romain Guy8f3b8e32012-03-27 16:33:45 -0700340status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800341 if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
Chris Craik408eb122013-03-26 18:55:15 -0700342
Chris Craikd6b65f62014-01-01 14:45:21 -0800343 Rect clip(*currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700344 clip.snapToPixelBoundaries();
345
Romain Guyd643bb52011-03-01 14:55:21 -0800346 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800347 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800348 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800349 dirtyLayerUnchecked(clip, getRegion());
350 }
Romain Guyd643bb52011-03-01 14:55:21 -0800351
Romain Guy08aa2cb2011-03-17 11:06:57 -0700352 DrawGlInfo info;
353 info.clipLeft = clip.left;
354 info.clipTop = clip.top;
355 info.clipRight = clip.right;
356 info.clipBottom = clip.bottom;
357 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700358 info.width = getViewportWidth();
359 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800360 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700361
John Reck3b202512014-06-23 13:13:08 -0700362 bool prevDirtyClip = mDirtyClip;
Chris Craik54f574a2013-08-26 11:23:46 -0700363 // setup GL state for functor
364 if (mDirtyClip) {
Chris Craik54f574a2013-08-26 11:23:46 -0700365 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
366 }
John Reck3b202512014-06-23 13:13:08 -0700367 if (mCaches.enableScissor() || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700368 setScissorFromClip();
369 }
Chris Craik54f574a2013-08-26 11:23:46 -0700370
John Reck3b202512014-06-23 13:13:08 -0700371 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
372 // Scissor may have been modified, reset dirty clip
373 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800374
John Reck750ca6d2014-03-28 16:33:18 -0700375 return DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800376}
377
Romain Guyf6a11b82010-06-23 17:47:49 -0700378///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700379// Debug
380///////////////////////////////////////////////////////////////////////////////
381
Romain Guy0f6675332013-03-01 14:31:04 -0800382void OpenGLRenderer::eventMark(const char* name) const {
383 mCaches.eventMark(0, name);
384}
385
Romain Guy87e2f7572012-09-24 11:37:12 -0700386void OpenGLRenderer::startMark(const char* name) const {
387 mCaches.startMark(0, name);
388}
389
390void OpenGLRenderer::endMark() const {
391 mCaches.endMark();
392}
393
394void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700395 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700396}
397
398void OpenGLRenderer::renderOverdraw() {
399 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700400 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700401
402 mCaches.enableScissor();
Chris Craika64a2be2014-05-14 14:17:01 -0700403 mCaches.setScissor(clip->left, firstSnapshot()->getViewportHeight() - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700404 clip->right - clip->left, clip->bottom - clip->top);
405
Romain Guy627c6fd2013-08-21 11:53:18 -0700406 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700407 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700408 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
409
410 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700411 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700412 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
413
414 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700415 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700416 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
417
418 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700419 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700420 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
421
Romain Guy87e2f7572012-09-24 11:37:12 -0700422 mCaches.stencil.disable();
423 }
424}
425
Romain Guy78dd96d2013-05-03 14:24:16 -0700426void OpenGLRenderer::countOverdraw() {
Chris Craik14e51302013-12-30 15:32:54 -0800427 size_t count = getWidth() * getHeight();
Romain Guy78dd96d2013-05-03 14:24:16 -0700428 uint32_t* buffer = new uint32_t[count];
Chris Craik14e51302013-12-30 15:32:54 -0800429 glReadPixels(0, 0, getWidth(), getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
Romain Guy78dd96d2013-05-03 14:24:16 -0700430
431 size_t total = 0;
432 for (size_t i = 0; i < count; i++) {
433 total += buffer[i] & 0xff;
434 }
435
436 mOverdraw = total / float(count);
437
438 delete[] buffer;
439}
440
Romain Guy87e2f7572012-09-24 11:37:12 -0700441///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700442// Layers
443///////////////////////////////////////////////////////////////////////////////
444
445bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700446 if (layer->deferredUpdateScheduled && layer->renderer
447 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700448 ATRACE_CALL();
449
Romain Guy11cb6422012-09-21 00:39:43 -0700450 Rect& dirty = layer->dirtyRect;
451
Romain Guy7c450aa2012-09-21 19:15:00 -0700452 if (inFrame) {
453 endTiling();
454 debugOverdraw(false, false);
455 }
Romain Guy11cb6422012-09-21 00:39:43 -0700456
Romain Guy96885eb2013-03-26 15:05:58 -0700457 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700458 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700459 } else {
460 layer->defer();
461 }
Romain Guy11cb6422012-09-21 00:39:43 -0700462
463 if (inFrame) {
464 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800465 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700466 }
467
Romain Guy5bb3c732012-11-29 17:52:58 -0800468 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700469 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700470
471 return true;
472 }
473
474 return false;
475}
476
477void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700478 // If draw deferring is enabled this method will simply defer
479 // the display list of each individual layer. The layers remain
480 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700481 int count = mLayerUpdates.size();
482 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700483 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
484 startMark("Layer Updates");
485 } else {
486 startMark("Defer Layer Updates");
487 }
Romain Guy11cb6422012-09-21 00:39:43 -0700488
Chris Craik1206b9b2013-04-04 14:46:24 -0700489 // Note: it is very important to update the layers in order
490 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700491 Layer* layer = mLayerUpdates.itemAt(i);
492 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700493 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
494 mCaches.resourceCache.decrementRefcount(layer);
495 }
Romain Guy11cb6422012-09-21 00:39:43 -0700496 }
Romain Guy11cb6422012-09-21 00:39:43 -0700497
Romain Guy96885eb2013-03-26 15:05:58 -0700498 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
499 mLayerUpdates.clear();
John Reck3b202512014-06-23 13:13:08 -0700500 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700501 }
502 endMark();
503 }
504}
505
506void OpenGLRenderer::flushLayers() {
507 int count = mLayerUpdates.size();
508 if (count > 0) {
509 startMark("Apply Layer Updates");
510 char layerName[12];
511
Chris Craik1206b9b2013-04-04 14:46:24 -0700512 // Note: it is very important to update the layers in order
513 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700514 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700515 startMark(layerName);
516
Romain Guy40543602013-06-12 15:31:28 -0700517 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700518 Layer* layer = mLayerUpdates.itemAt(i);
519 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700520 ATRACE_END();
521
Romain Guy02b49b72013-03-29 12:37:16 -0700522 mCaches.resourceCache.decrementRefcount(layer);
523
Romain Guy96885eb2013-03-26 15:05:58 -0700524 endMark();
525 }
526
527 mLayerUpdates.clear();
John Reck3b202512014-06-23 13:13:08 -0700528 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700529
Romain Guy11cb6422012-09-21 00:39:43 -0700530 endMark();
531 }
532}
533
534void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
535 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700536 // Make sure we don't introduce duplicates.
537 // SortedVector would do this automatically but we need to respect
538 // the insertion order. The linear search is not an issue since
539 // this list is usually very short (typically one item, at most a few)
540 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
541 if (mLayerUpdates.itemAt(i) == layer) {
542 return;
543 }
544 }
Romain Guy11cb6422012-09-21 00:39:43 -0700545 mLayerUpdates.push_back(layer);
546 mCaches.resourceCache.incrementRefcount(layer);
547 }
548}
549
Romain Guye93482f2013-06-17 13:14:51 -0700550void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
551 if (layer) {
552 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
553 if (mLayerUpdates.itemAt(i) == layer) {
554 mLayerUpdates.removeAt(i);
555 mCaches.resourceCache.decrementRefcount(layer);
556 break;
557 }
558 }
559 }
560}
561
Romain Guy11cb6422012-09-21 00:39:43 -0700562void OpenGLRenderer::clearLayerUpdates() {
563 size_t count = mLayerUpdates.size();
564 if (count > 0) {
565 mCaches.resourceCache.lock();
566 for (size_t i = 0; i < count; i++) {
567 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
568 }
569 mCaches.resourceCache.unlock();
570 mLayerUpdates.clear();
571 }
572}
573
Romain Guy40543602013-06-12 15:31:28 -0700574void OpenGLRenderer::flushLayerUpdates() {
575 syncState();
576 updateLayers();
577 flushLayers();
578 // Wait for all the layer updates to be executed
579 AutoFence fence;
580}
581
Romain Guy11cb6422012-09-21 00:39:43 -0700582///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700583// State management
584///////////////////////////////////////////////////////////////////////////////
585
Chris Craik14e51302013-12-30 15:32:54 -0800586void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700587 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800588 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
589 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700590
Chris Craika64a2be2014-05-14 14:17:01 -0700591 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700592 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700593 }
594
Romain Guy2542d192010-08-18 11:47:12 -0700595 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700596 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700597 }
Romain Guy2542d192010-08-18 11:47:12 -0700598
Romain Guy5ec99242010-11-03 16:19:08 -0700599 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700600 endMark(); // Savelayer
601 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800602 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700603 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700604 }
Romain Guybb9524b2010-06-22 18:56:38 -0700605}
606
Romain Guyf6a11b82010-06-23 17:47:49 -0700607///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700608// Layers
609///////////////////////////////////////////////////////////////////////////////
610
611int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700612 const SkPaint* paint, int flags, const SkPath* convexMask) {
Romain Guyeb993562010-10-05 18:14:38 -0700613 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700614
Chris Craikd6b65f62014-01-01 14:45:21 -0800615 if (!currentSnapshot()->isIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700616 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700617 }
Romain Guyd55a8612010-06-28 17:42:46 -0700618
619 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700620}
621
Chris Craikd90144d2013-03-19 15:03:48 -0700622void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
623 const Rect untransformedBounds(bounds);
624
Chris Craikd6b65f62014-01-01 14:45:21 -0800625 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700626
627 // Layers only make sense if they are in the framebuffer's bounds
Chris Craikd6b65f62014-01-01 14:45:21 -0800628 if (bounds.intersect(*currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700629 // We cannot work with sub-pixels in this case
630 bounds.snapToPixelBoundaries();
631
632 // When the layer is not an FBO, we may use glCopyTexImage so we
633 // need to make sure the layer does not extend outside the bounds
634 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700635 const Snapshot& previous = *(currentSnapshot()->previous);
636 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
637 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700638 bounds.setEmpty();
639 } else if (fboLayer) {
640 clip.set(bounds);
641 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800642 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700643 inverse.mapRect(clip);
644 clip.snapToPixelBoundaries();
645 if (clip.intersect(untransformedBounds)) {
646 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
647 bounds.set(untransformedBounds);
648 } else {
649 clip.setEmpty();
650 }
651 }
652 } else {
653 bounds.setEmpty();
654 }
655}
656
Chris Craik408eb122013-03-26 18:55:15 -0700657void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
658 bool fboLayer, int alpha) {
659 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
660 bounds.getHeight() > mCaches.maxTextureSize ||
661 (fboLayer && clip.isEmpty())) {
662 mSnapshot->empty = fboLayer;
663 } else {
664 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
665 }
666}
667
Chris Craikd90144d2013-03-19 15:03:48 -0700668int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500669 const SkPaint* paint, int flags) {
Chris Craikd90144d2013-03-19 15:03:48 -0700670 const int count = saveSnapshot(flags);
671
Chris Craikd6b65f62014-01-01 14:45:21 -0800672 if (!currentSnapshot()->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700673 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
674 // operations will be able to store and restore the current clip and transform info, and
675 // quick rejection will be correct (for display lists)
676
677 Rect bounds(left, top, right, bottom);
678 Rect clip;
679 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500680 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700681
Chris Craikd6b65f62014-01-01 14:45:21 -0800682 if (!currentSnapshot()->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700683 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
684 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craika64a2be2014-05-14 14:17:01 -0700685 mSnapshot->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craikdeeda3d2014-05-05 19:09:33 -0700686 mSnapshot->roundRectClipState = NULL;
Chris Craikd90144d2013-03-19 15:03:48 -0700687 }
688 }
689
690 return count;
691}
692
Romain Guy1c740bc2010-09-13 18:00:09 -0700693/**
694 * Layers are viewed by Skia are slightly different than layers in image editing
695 * programs (for instance.) When a layer is created, previously created layers
696 * and the frame buffer still receive every drawing command. For instance, if a
697 * layer is created and a shape intersecting the bounds of the layers and the
698 * framebuffer is draw, the shape will be drawn on both (unless the layer was
699 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
700 *
701 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
702 * texture. Unfortunately, this is inefficient as it requires every primitive to
703 * be drawn n + 1 times, where n is the number of active layers. In practice this
704 * means, for every primitive:
705 * - Switch active frame buffer
706 * - Change viewport, clip and projection matrix
707 * - Issue the drawing
708 *
709 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700710 * To avoid this, layers are implemented in a different way here, at least in the
711 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
712 * is set. When this flag is set we can redirect all drawing operations into a
713 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700714 *
715 * This implementation relies on the frame buffer being at least RGBA 8888. When
716 * a layer is created, only a texture is created, not an FBO. The content of the
717 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700718 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700719 * buffer and drawing continues as normal. This technique therefore treats the
720 * frame buffer as a scratch buffer for the layers.
721 *
722 * To compose the layers back onto the frame buffer, each layer texture
723 * (containing the original frame buffer data) is drawn as a simple quad over
724 * the frame buffer. The trick is that the quad is set as the composition
725 * destination in the blending equation, and the frame buffer becomes the source
726 * of the composition.
727 *
728 * Drawing layers with an alpha value requires an extra step before composition.
729 * An empty quad is drawn over the layer's region in the frame buffer. This quad
730 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
731 * quad is used to multiply the colors in the frame buffer. This is achieved by
732 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
733 * GL_ZERO, GL_SRC_ALPHA.
734 *
735 * Because glCopyTexImage2D() can be slow, an alternative implementation might
736 * be use to draw a single clipped layer. The implementation described above
737 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700738 *
739 * (1) The frame buffer is actually not cleared right away. To allow the GPU
740 * to potentially optimize series of calls to glCopyTexImage2D, the frame
741 * buffer is left untouched until the first drawing operation. Only when
742 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700743 */
Chet Haased48885a2012-08-28 17:43:28 -0700744bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700745 const SkPaint* paint, int flags, const SkPath* convexMask) {
Romain Guyeb993562010-10-05 18:14:38 -0700746 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700747 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700748
Romain Guyeb993562010-10-05 18:14:38 -0700749 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
750
Romain Guyf607bdc2010-09-10 19:20:06 -0700751 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700752 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700753 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700754 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000755 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700756
757 // Bail out if we won't draw in this snapshot
Chris Craikd6b65f62014-01-01 14:45:21 -0800758 if (currentSnapshot()->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700759 return false;
760 }
Romain Guyf18fd992010-07-08 11:45:51 -0700761
Romain Guya1d3c912011-12-13 14:55:06 -0800762 mCaches.activeTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700763 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700764 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700765 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700766 }
767
Derek Sollenberger674554f2014-02-19 16:47:32 +0000768 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700769 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700770 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
771 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500772
Chet Haasea23eed82012-04-12 15:19:04 -0700773 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700774 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700775 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700776
Romain Guy8fb95422010-08-17 18:38:51 -0700777 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700778 mSnapshot->flags |= Snapshot::kFlagIsLayer;
779 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700780
Chris Craik7273daa2013-03-28 11:25:24 -0700781 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700782 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700783 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700784 } else {
785 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700786 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800787 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700788 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700789 // Workaround for some GL drivers. When reading pixels lying outside
790 // of the window we should get undefined values for those pixels.
791 // Unfortunately some drivers will turn the entire target texture black
792 // when reading outside of the window.
793 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
794 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700795 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800796 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700797
Chris Craika64a2be2014-05-14 14:17:01 -0700798 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
799 bounds.left, getViewportHeight() - bounds.bottom,
800 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700801
Romain Guy54be1cd2011-06-13 19:04:27 -0700802 // Enqueue the buffer coordinates to clear the corresponding region later
803 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700804 }
Romain Guyeb993562010-10-05 18:14:38 -0700805 }
Romain Guyf86ef572010-07-01 11:05:42 -0700806
Romain Guyd55a8612010-06-28 17:42:46 -0700807 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700808}
809
Chris Craike63f7c622013-10-17 10:30:55 -0700810bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800811 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700812 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700813
Chet Haased48885a2012-08-28 17:43:28 -0700814 mSnapshot->region = &mSnapshot->layer->region;
Chris Craika64a2be2014-05-14 14:17:01 -0700815 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
Chet Haased48885a2012-08-28 17:43:28 -0700816 mSnapshot->fbo = layer->getFbo();
817 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
818 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craika64a2be2014-05-14 14:17:01 -0700819 mSnapshot->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craikdeeda3d2014-05-05 19:09:33 -0700820 mSnapshot->roundRectClipState = NULL;
Romain Guy5b3b3522010-10-27 18:57:51 -0700821
Romain Guy2b7028e2012-09-19 17:25:38 -0700822 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700823 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700824 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700825 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700826 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700827
828 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700829 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700830 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700831 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700832 }
833
834 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700835 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700836
henry.uh_chen33f5a592014-07-02 19:36:56 +0800837 // Expand the startTiling region by 1
838 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700839
840 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700841 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800842 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700843 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700844 glClear(GL_COLOR_BUFFER_BIT);
845
846 dirtyClip();
847
848 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700849 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700850 return true;
851}
852
Romain Guy1c740bc2010-09-13 18:00:09 -0700853/**
854 * Read the documentation of createLayer() before doing anything in this method.
855 */
Chris Craik14e51302013-12-30 15:32:54 -0800856void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
857 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000858 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700859 return;
860 }
861
Chris Craik14e51302013-12-30 15:32:54 -0800862 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800863 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800864 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700865
Chris Craik39a908c2013-06-13 14:39:01 -0700866 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -0800867 calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700868 &clipRequired, NULL, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -0700869 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
870
Romain Guyeb993562010-10-05 18:14:38 -0700871 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700872 endTiling();
873
Romain Guye0aa84b2012-04-03 19:30:26 -0700874 // Detach the texture from the FBO
875 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800876
877 layer->removeFbo(false);
878
Romain Guyeb993562010-10-05 18:14:38 -0700879 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700880 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700881 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700882
Chris Craikd6b65f62014-01-01 14:45:21 -0800883 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700884 }
885
Romain Guy9ace8f52011-07-07 20:50:11 -0700886 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500887 SkPaint layerPaint;
888 layerPaint.setAlpha(layer->getAlpha());
889 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
890 layerPaint.setColorFilter(layer->getColorFilter());
891
892 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700893 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700894 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700895 }
896
Romain Guy03750a02010-10-18 14:06:08 -0700897 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700898
Romain Guya1d3c912011-12-13 14:55:06 -0800899 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700900
Romain Guy5b3b3522010-10-27 18:57:51 -0700901 // When the layer is stored in an FBO, we can save a bit of fillrate by
902 // drawing only the dirty region
903 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800904 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700905 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700906 } else if (!rect.isEmpty()) {
907 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530908
909 save(0);
910 // the layer contains screen buffer content that shouldn't be alpha modulated
911 // (and any necessary alpha modulation was handled drawing into the layer)
912 mSnapshot->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700913 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530914 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700915 }
Romain Guy8b55f372010-08-18 17:10:07 -0700916
Romain Guy746b7402010-10-26 16:27:31 -0700917 dirtyClip();
918
Romain Guyeb993562010-10-05 18:14:38 -0700919 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craik3f0854292014-04-15 16:18:08 -0700920 layer->setConvexMask(NULL);
Romain Guy8550c4c2010-10-08 15:49:53 -0700921 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700922 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700923 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700924 }
925}
926
Romain Guyaa6c24c2011-04-28 18:40:04 -0700927void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700928 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700929
930 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700931 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700932 setupDrawWithTexture();
933 } else {
934 setupDrawWithExternalTexture();
935 }
936 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700937 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500938 setupDrawColorFilter(layer->getColorFilter());
939 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700940 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700941 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500942 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700943 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
944 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700945 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700946 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700947 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800948 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800949 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700950 layer->getWidth() == (uint32_t) rect.getWidth() &&
951 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800952 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
953 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700954
Romain Guyd21b6e12011-11-30 20:21:23 -0800955 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800956 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
957 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700958 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800959 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800960 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
961 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700962 }
963 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700964 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700965
966 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700967}
968
Romain Guy5b3b3522010-10-27 18:57:51 -0700969void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700970 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700971 const Rect& texCoords = layer->texCoords;
972 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
973 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700974
Romain Guy9ace8f52011-07-07 20:50:11 -0700975 float x = rect.left;
976 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -0800977 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700978 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700979 layer->getHeight() == (uint32_t) rect.getHeight();
980
981 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700982 // When we're swapping, the layer is already in screen coordinates
983 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800984 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
985 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700986 }
987
Romain Guyd21b6e12011-11-30 20:21:23 -0800988 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700989 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800990 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700991 }
992
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500993 SkPaint layerPaint;
994 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
995 layerPaint.setXfermodeMode(layer->getMode());
996 layerPaint.setColorFilter(layer->getColorFilter());
997
998 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700999 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001000 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001001 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001002 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001003
Romain Guyaa6c24c2011-04-28 18:40:04 -07001004 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1005 } else {
1006 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1007 drawTextureLayer(layer, rect);
1008 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1009 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001010}
1011
Chris Craik34416ea2013-04-15 16:08:28 -07001012/**
1013 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1014 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1015 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1016 * by saveLayer's restore
1017 */
1018#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1019 DRAW_COMMAND; \
1020 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1021 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1022 DRAW_COMMAND; \
1023 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1024 } \
1025 }
1026
1027#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1028
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001029// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
1030// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
1031class LayerShader : public SkShader {
1032public:
1033 LayerShader(Layer* layer, const SkMatrix* localMatrix)
1034 : INHERITED(localMatrix)
1035 , mLayer(layer) {
1036 }
1037
1038 virtual bool asACustomShader(void** data) const {
1039 if (data) {
1040 *data = static_cast<void*>(mLayer);
1041 }
1042 return true;
1043 }
1044
1045 virtual bool isOpaque() const {
1046 return !mLayer->isBlend();
1047 }
1048
1049protected:
1050 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
1051 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
1052 }
1053
1054 virtual void flatten(SkWriteBuffer&) const {
1055 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1056 }
1057
1058 virtual Factory getFactory() const {
1059 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
1060 return NULL;
1061 }
1062private:
1063 // Unowned.
1064 Layer* mLayer;
1065 typedef SkShader INHERITED;
1066};
1067
Romain Guy5b3b3522010-10-27 18:57:51 -07001068void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001069 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1070
1071 if (layer->getConvexMask()) {
1072 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1073
1074 // clip to the area of the layer the mask can be larger
1075 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1076
1077 SkPaint paint;
1078 paint.setAntiAlias(true);
1079 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1080
Chris Craik3f0854292014-04-15 16:18:08 -07001081 // create LayerShader to map SaveLayer content into subsequent draw
1082 SkMatrix shaderMatrix;
1083 shaderMatrix.setTranslate(rect.left, rect.bottom);
1084 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001085 LayerShader layerShader(layer, &shaderMatrix);
1086 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001087
1088 // Since the drawing primitive is defined in local drawing space,
1089 // we don't need to modify the draw matrix
1090 const SkPath* maskPath = layer->getConvexMask();
1091 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1092
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001093 paint.setShader(NULL);
Chris Craik3f0854292014-04-15 16:18:08 -07001094 restore();
1095
1096 return;
1097 }
1098
Romain Guy5b3b3522010-10-27 18:57:51 -07001099 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001100 layer->setRegionAsRect();
1101
Chris Craik34416ea2013-04-15 16:08:28 -07001102 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001103
Romain Guy5b3b3522010-10-27 18:57:51 -07001104 layer->region.clear();
1105 return;
1106 }
1107
Chris Craik3f0854292014-04-15 16:18:08 -07001108 // standard Region based draw
1109 size_t count;
1110 const android::Rect* rects;
1111 Region safeRegion;
1112 if (CC_LIKELY(hasRectToRectTransform())) {
1113 rects = layer->region.getArray(&count);
1114 } else {
1115 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1116 rects = safeRegion.getArray(&count);
1117 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001118
Chris Craik3f0854292014-04-15 16:18:08 -07001119 const float alpha = getLayerAlpha(layer);
1120 const float texX = 1.0f / float(layer->getWidth());
1121 const float texY = 1.0f / float(layer->getHeight());
1122 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001123
Chris Craik3f0854292014-04-15 16:18:08 -07001124 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001125
Chris Craik3f0854292014-04-15 16:18:08 -07001126 // We must get (and therefore bind) the region mesh buffer
1127 // after we setup drawing in case we need to mess with the
1128 // stencil buffer in setupDraw()
1129 TextureVertex* mesh = mCaches.getRegionMesh();
1130 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001131
Chris Craik3f0854292014-04-15 16:18:08 -07001132 setupDrawWithTexture();
1133 setupDrawColor(alpha, alpha, alpha, alpha);
1134 setupDrawColorFilter(layer->getColorFilter());
1135 setupDrawBlending(layer);
1136 setupDrawProgram();
1137 setupDrawDirtyRegionsDisabled();
1138 setupDrawPureColorUniforms();
1139 setupDrawColorFilterUniforms(layer->getColorFilter());
1140 setupDrawTexture(layer->getTexture());
1141 if (currentTransform()->isPureTranslate()) {
1142 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1143 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001144
Chris Craik3f0854292014-04-15 16:18:08 -07001145 layer->setFilter(GL_NEAREST);
1146 setupDrawModelView(kModelViewMode_Translate, false,
1147 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1148 } else {
1149 layer->setFilter(GL_LINEAR);
1150 setupDrawModelView(kModelViewMode_Translate, false,
1151 rect.left, rect.top, rect.right, rect.bottom);
1152 }
1153 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001154
Chris Craik3f0854292014-04-15 16:18:08 -07001155 for (size_t i = 0; i < count; i++) {
1156 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001157
Chris Craik3f0854292014-04-15 16:18:08 -07001158 const float u1 = r->left * texX;
1159 const float v1 = (height - r->top) * texY;
1160 const float u2 = r->right * texX;
1161 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001162
Chris Craik3f0854292014-04-15 16:18:08 -07001163 // TODO: Reject quads outside of the clip
1164 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1165 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1166 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1167 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001168
Chris Craik3f0854292014-04-15 16:18:08 -07001169 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001170
Chris Craik3f0854292014-04-15 16:18:08 -07001171 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001172 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1173 GL_UNSIGNED_SHORT, NULL));
Chris Craik3f0854292014-04-15 16:18:08 -07001174 numQuads = 0;
1175 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001176 }
Chris Craik3f0854292014-04-15 16:18:08 -07001177 }
1178
1179 if (numQuads > 0) {
1180 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1181 GL_UNSIGNED_SHORT, NULL));
1182 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001183
Romain Guy5b3b3522010-10-27 18:57:51 -07001184#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001185 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001186#endif
1187
Chris Craik3f0854292014-04-15 16:18:08 -07001188 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001189}
1190
Romain Guy3a3133d2011-02-01 22:59:58 -08001191#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001192void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001193 size_t count;
1194 const android::Rect* rects = region.getArray(&count);
1195
1196 uint32_t colors[] = {
1197 0x7fff0000, 0x7f00ff00,
1198 0x7f0000ff, 0x7fff00ff,
1199 };
1200
1201 int offset = 0;
1202 int32_t top = rects[0].top;
1203
1204 for (size_t i = 0; i < count; i++) {
1205 if (top != rects[i].top) {
1206 offset ^= 0x2;
1207 top = rects[i].top;
1208 }
1209
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001210 SkPaint paint;
1211 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001212 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001213 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001214 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001215}
Chris Craike63f7c622013-10-17 10:30:55 -07001216#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001217
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001218void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001219 Vector<float> rects;
1220
1221 SkRegion::Iterator it(region);
1222 while (!it.done()) {
1223 const SkIRect& r = it.rect();
1224 rects.push(r.fLeft);
1225 rects.push(r.fTop);
1226 rects.push(r.fRight);
1227 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001228 it.next();
1229 }
1230
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001231 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001232}
1233
Romain Guy5b3b3522010-10-27 18:57:51 -07001234void OpenGLRenderer::dirtyLayer(const float left, const float top,
1235 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001236 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001237 Rect bounds(left, top, right, bottom);
1238 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001239 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001240 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001241}
1242
1243void OpenGLRenderer::dirtyLayer(const float left, const float top,
1244 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001245 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001246 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001247 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001248 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001249}
1250
1251void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001252 if (bounds.intersect(*currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001253 bounds.snapToPixelBoundaries();
1254 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1255 if (!dirty.isEmpty()) {
1256 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001257 }
1258 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001259}
1260
Chris Craik4063a0e2013-11-15 16:06:56 -08001261void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001262 GLsizei elementsCount = quadsCount * 6;
1263 while (elementsCount > 0) {
1264 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1265
Romain Guy3380cfd2013-08-15 16:57:57 -07001266 setupDrawIndexedVertices(&mesh[0].x);
Romain Guy448455f2013-07-22 13:57:50 -07001267 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1268
1269 elementsCount -= drawCount;
1270 // Though there are 4 vertices in a quad, we use 6 indices per
1271 // quad to draw with GL_TRIANGLES
1272 mesh += (drawCount / 6) * 4;
1273 }
1274}
1275
Romain Guy54be1cd2011-06-13 19:04:27 -07001276void OpenGLRenderer::clearLayerRegions() {
1277 const size_t count = mLayers.size();
1278 if (count == 0) return;
1279
Chris Craikd6b65f62014-01-01 14:45:21 -08001280 if (!currentSnapshot()->isIgnored()) {
Romain Guy54be1cd2011-06-13 19:04:27 -07001281 // Doing several glScissor/glClear here can negatively impact
1282 // GPUs with a tiler architecture, instead we draw quads with
1283 // the Clear blending mode
1284
1285 // The list contains bounds that have already been clipped
1286 // against their initial clip rect, and the current clip
1287 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001288 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001289
Romain Guy448455f2013-07-22 13:57:50 -07001290 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001291 Vertex* vertex = mesh;
1292
1293 for (uint32_t i = 0; i < count; i++) {
1294 Rect* bounds = mLayers.itemAt(i);
1295
Romain Guy54be1cd2011-06-13 19:04:27 -07001296 Vertex::set(vertex++, bounds->left, bounds->top);
1297 Vertex::set(vertex++, bounds->right, bounds->top);
1298 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001299 Vertex::set(vertex++, bounds->right, bounds->bottom);
1300
1301 delete bounds;
1302 }
Romain Guye67307c2013-02-11 18:01:20 -08001303 // We must clear the list of dirty rects before we
1304 // call setupDraw() to prevent stencil setup to do
1305 // the same thing again
1306 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001307
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001308 SkPaint clearPaint;
1309 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1310
Romain Guy54be1cd2011-06-13 19:04:27 -07001311 setupDraw(false);
1312 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001313 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001314 setupDrawProgram();
1315 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001316 setupDrawModelView(kModelViewMode_Translate, false,
1317 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001318
Chris Craik4063a0e2013-11-15 16:06:56 -08001319 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001320
1321 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001322 } else {
1323 for (uint32_t i = 0; i < count; i++) {
1324 delete mLayers.itemAt(i);
1325 }
Romain Guye67307c2013-02-11 18:01:20 -08001326 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001327 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001328}
1329
Romain Guybd6b79b2010-06-26 00:13:53 -07001330///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001331// State Deferral
1332///////////////////////////////////////////////////////////////////////////////
1333
Chris Craikff785832013-03-08 13:12:16 -08001334bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001335 const Rect* currentClip = currentClipRect();
1336 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001337
Chris Craikff785832013-03-08 13:12:16 -08001338 if (stateDeferFlags & kStateDeferFlag_Draw) {
1339 // state has bounds initialized in local coordinates
1340 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001341 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001342 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001343 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1344 // is used, it should more closely duplicate the quickReject logic (in how it uses
1345 // snapToPixelBoundaries)
1346
Chris Craikd6b65f62014-01-01 14:45:21 -08001347 if(!clippedBounds.intersect(*currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001348 // quick rejected
1349 return true;
1350 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001351
Chris Craika02c4ed2013-06-14 13:43:58 -07001352 state.mClipSideFlags = kClipSide_None;
Chris Craikd6b65f62014-01-01 14:45:21 -08001353 if (!currentClip->contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001354 int& flags = state.mClipSideFlags;
1355 // op partially clipped, so record which sides are clipped for clip-aware merging
Chris Craikd6b65f62014-01-01 14:45:21 -08001356 if (currentClip->left > state.mBounds.left) flags |= kClipSide_Left;
1357 if (currentClip->top > state.mBounds.top) flags |= kClipSide_Top;
1358 if (currentClip->right < state.mBounds.right) flags |= kClipSide_Right;
1359 if (currentClip->bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001360 }
1361 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001362 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001363 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1364 // overdraw avoidance (since we don't know what it overlaps)
1365 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikd6b65f62014-01-01 14:45:21 -08001366 state.mBounds.set(*currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001367 }
1368 }
1369
Chris Craik527a3aa2013-03-04 10:19:31 -08001370 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1371 if (state.mClipValid) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001372 state.mClip.set(*currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001373 }
1374
Chris Craik7273daa2013-03-28 11:25:24 -07001375 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1376 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001377 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001378 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001379 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001380
1381 // always store/restore, since it's just a pointer
1382 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001383 return false;
1384}
1385
Chris Craik527a3aa2013-03-04 10:19:31 -08001386void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001387 setMatrix(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001388 mSnapshot->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001389 mDrawModifiers = state.mDrawModifiers;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001390 mSnapshot->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001391
Chris Craik527a3aa2013-03-04 10:19:31 -08001392 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001393 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1394 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001395 dirtyClip();
1396 }
Chris Craikc3566d02013-02-04 16:16:33 -08001397}
1398
Chris Craik28ce94a2013-05-31 11:38:03 -07001399/**
1400 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1401 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1402 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1403 *
1404 * This method should be called when restoreDisplayState() won't be restoring the clip
1405 */
1406void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1407 if (clipRect != NULL) {
1408 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1409 } else {
Chris Craik14e51302013-12-30 15:32:54 -08001410 mSnapshot->setClip(0, 0, getWidth(), getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001411 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001412 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001413 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001414}
1415
Chris Craikc3566d02013-02-04 16:16:33 -08001416///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001417// Clipping
1418///////////////////////////////////////////////////////////////////////////////
1419
Romain Guybb9524b2010-06-22 18:56:38 -07001420void OpenGLRenderer::setScissorFromClip() {
Chris Craikd6b65f62014-01-01 14:45:21 -08001421 Rect clip(*currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001422 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001423
Chris Craika64a2be2014-05-14 14:17:01 -07001424 if (mCaches.setScissor(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001425 clip.getWidth(), clip.getHeight())) {
1426 mDirtyClip = false;
1427 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001428}
1429
Romain Guy8ce00302013-01-15 18:51:42 -08001430void OpenGLRenderer::ensureStencilBuffer() {
1431 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1432 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1433 // just hope we have one when hasLayer() returns false.
1434 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001435 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001436 }
1437}
1438
1439void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1440 // The layer's FBO is already bound when we reach this stage
1441 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001442 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1443 // is attached after we initiated tiling. We must turn it off,
1444 // attach the new render buffer then turn tiling back on
1445 endTiling();
1446
Romain Guy8d4aeb72013-02-12 16:08:55 -08001447 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001448 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001449 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001450
Romain Guyf735c8e2013-01-31 17:45:55 -08001451 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001452 }
1453}
1454
1455void OpenGLRenderer::setStencilFromClip() {
1456 if (!mCaches.debugOverdraw) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001457 if (!currentSnapshot()->clipRegion->isEmpty()) {
Romain Guy8ce00302013-01-15 18:51:42 -08001458 // NOTE: The order here is important, we must set dirtyClip to false
1459 // before any draw call to avoid calling back into this method
1460 mDirtyClip = false;
1461
1462 ensureStencilBuffer();
1463
1464 mCaches.stencil.enableWrite();
1465
Chris Craikdeeda3d2014-05-05 19:09:33 -07001466 // Clear and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001467 // to the region's bounds
1468 bool resetScissor = mCaches.enableScissor();
1469 if (resetScissor) {
1470 // The scissor was not set so we now need to update it
1471 setScissorFromClip();
1472 }
1473 mCaches.stencil.clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001474
1475 // stash and disable the outline clip state, since stencil doesn't account for outline
1476 bool storedSkipOutlineClip = mSkipOutlineClip;
1477 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001478
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001479 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001480 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001481 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1482
Romain Guy8ce00302013-01-15 18:51:42 -08001483 // NOTE: We could use the region contour path to generate a smaller mesh
1484 // Since we are using the stencil we could use the red book path
1485 // drawing technique. It might increase bandwidth usage though.
1486
1487 // The last parameter is important: we are not drawing in the color buffer
1488 // so we don't want to dirty the current layer, if any
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001489 drawRegionRects(*(currentSnapshot()->clipRegion), paint, false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001490 if (resetScissor) mCaches.disableScissor();
1491 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001492
1493 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001494
1495 // Draw the region used to generate the stencil if the appropriate debug
1496 // mode is enabled
1497 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001498 paint.setColor(0x7f0000ff);
1499 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
1500 drawRegionRects(*(currentSnapshot()->clipRegion), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001501 }
Romain Guy8ce00302013-01-15 18:51:42 -08001502 } else {
1503 mCaches.stencil.disable();
1504 }
1505 }
1506}
1507
Chris Craikf0a59072013-11-19 18:00:46 -08001508/**
1509 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1510 *
1511 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1512 * style, and tessellated AA ramp
1513 */
1514bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001515 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001516 bool snapOut = paint && paint->isAntiAlias();
1517
1518 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1519 float outset = paint->getStrokeWidth() * 0.5f;
1520 left -= outset;
1521 top -= outset;
1522 right += outset;
1523 bottom += outset;
1524 }
1525
Chris Craikdeeda3d2014-05-05 19:09:33 -07001526 bool clipRequired = false;
1527 bool roundRectClipRequired = false;
1528 if (calculateQuickRejectForScissor(left, top, right, bottom,
1529 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001530 return true;
1531 }
1532
Chris Craikf23b25a2014-06-26 15:46:20 -07001533 // not quick rejected, so enable the scissor if clipRequired
1534 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1535 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001536 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001537}
1538
Romain Guy8ce00302013-01-15 18:51:42 -08001539void OpenGLRenderer::debugClip() {
1540#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001541 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001542 SkPaint paint;
1543 paint.setColor(0x7f00ff00);
1544 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1545
Romain Guy8ce00302013-01-15 18:51:42 -08001546 }
1547#endif
1548}
1549
Romain Guyf6a11b82010-06-23 17:47:49 -07001550///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001551// Drawing commands
1552///////////////////////////////////////////////////////////////////////////////
1553
Romain Guy54be1cd2011-06-13 19:04:27 -07001554void OpenGLRenderer::setupDraw(bool clear) {
Chris Craikf0a59072013-11-19 18:00:46 -08001555 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001556 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001557 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001558 // Make sure setScissor & setStencil happen at the beginning of
1559 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001560 if (mDirtyClip) {
1561 if (mCaches.scissorEnabled) {
1562 setScissorFromClip();
1563 }
Romain Guy8ce00302013-01-15 18:51:42 -08001564 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001565 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001566
Romain Guy70ca14e2010-12-13 18:24:33 -08001567 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001568
Romain Guy70ca14e2010-12-13 18:24:33 -08001569 mSetShaderColor = false;
1570 mColorSet = false;
1571 mColorA = mColorR = mColorG = mColorB = 0.0f;
1572 mTextureUnit = 0;
1573 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001574
1575 // Enable debug highlight when what we're about to draw is tested against
1576 // the stencil buffer and if stencil highlight debugging is on
1577 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1578 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1579 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001580
1581 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001582}
1583
1584void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1585 mDescription.hasTexture = true;
1586 mDescription.hasAlpha8Texture = isAlpha8;
1587}
1588
Romain Guyff316ec2013-02-13 18:39:43 -08001589void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1590 mDescription.hasTexture = true;
1591 mDescription.hasColors = true;
1592 mDescription.hasAlpha8Texture = isAlpha8;
1593}
1594
Romain Guyaa6c24c2011-04-28 18:40:04 -07001595void OpenGLRenderer::setupDrawWithExternalTexture() {
1596 mDescription.hasExternalTexture = true;
1597}
1598
Romain Guy15bc6432011-12-13 13:11:32 -08001599void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001600 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001601}
1602
Chris Craik710f46d2012-09-17 17:25:49 -07001603void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001604 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001605}
1606
Romain Guy8d0d4782010-12-14 20:13:35 -08001607void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1608 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001609 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1610 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1611 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001612 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001613 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001614}
1615
Romain Guy86568192010-12-14 15:55:39 -08001616void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1617 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001618 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1619 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1620 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001621 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001622 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001623}
1624
Romain Guy41210632012-07-16 17:04:24 -07001625void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1626 mCaches.fontRenderer->describe(mDescription, paint);
1627}
1628
Romain Guy70ca14e2010-12-13 18:24:33 -08001629void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1630 mColorA = a;
1631 mColorR = r;
1632 mColorG = g;
1633 mColorB = b;
1634 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001635 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001636}
1637
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001638void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
1639 if (shader != NULL) {
1640 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001641 }
1642}
1643
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001644void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
1645 if (filter == NULL) {
1646 return;
1647 }
1648
1649 SkXfermode::Mode mode;
1650 if (filter->asColorMode(NULL, &mode)) {
1651 mDescription.colorOp = ProgramDescription::kColorBlend;
1652 mDescription.colorMode = mode;
1653 } else if (filter->asColorMatrix(NULL)) {
1654 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001655 }
1656}
1657
Romain Guyf09ef512011-05-27 11:43:46 -07001658void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1659 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1660 mColorA = 1.0f;
1661 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001662 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001663 }
1664}
1665
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001666static bool isBlendedColorFilter(const SkColorFilter* filter) {
1667 if (filter == NULL) {
1668 return false;
1669 }
1670 return (filter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag) == 0;
1671}
1672
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001673void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1674 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001675 // When the blending mode is kClear_Mode, we need to use a modulate color
1676 // argb=1,0,0,0
1677 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001678 // TODO: check shader blending, once we have shader drawing support for layers.
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001679 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001680 (mColorSet && mColorA < 1.0f) || isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001681 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001682}
1683
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001684void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1685 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001686 // When the blending mode is kClear_Mode, we need to use a modulate color
1687 // argb=1,0,0,0
1688 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001689 blend |= (mColorSet && mColorA < 1.0f) ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001690 (getShader(paint) && !getShader(paint)->isOpaque()) ||
1691 isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001692 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001693}
1694
1695void OpenGLRenderer::setupDrawProgram() {
1696 useProgram(mCaches.programCache.get(mDescription));
Chris Craikdeeda3d2014-05-05 19:09:33 -07001697 if (mDescription.hasRoundRectClip) {
1698 // TODO: avoid doing this repeatedly, stashing state pointer in program
1699 const RoundRectClipState* state = mSnapshot->roundRectClipState;
1700 const Rect& innerRect = state->outlineInnerRect;
1701 glUniform4f(mCaches.currentProgram->getUniform("roundRectInnerRectLTRB"),
1702 innerRect.left, innerRect.top,
1703 innerRect.right, innerRect.bottom);
1704 glUniform1f(mCaches.currentProgram->getUniform("roundRectRadius"),
1705 state->outlineRadius);
1706 glUniformMatrix4fv(mCaches.currentProgram->getUniform("roundRectInvTransform"),
1707 1, GL_FALSE, &state->matrix.data[0]);
1708 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001709}
1710
1711void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1712 mTrackDirtyRegions = false;
1713}
1714
Chris Craik4063a0e2013-11-15 16:06:56 -08001715void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1716 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001717 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001718 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001719 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001720 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001721
Romain Guy86568192010-12-14 15:55:39 -08001722 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001723 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
1724 mCaches.currentProgram->set(mSnapshot->getOrthoMatrix(), mModelViewMatrix, transformMatrix, offset);
1725 if (dirty && mTrackDirtyRegions) {
1726 if (!ignoreTransform) {
1727 dirtyLayer(left, top, right, bottom, *currentTransform());
1728 } else {
1729 dirtyLayer(left, top, right, bottom);
1730 }
Romain Guy86568192010-12-14 15:55:39 -08001731 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001732}
1733
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001734void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1735 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001736 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1737 }
1738}
1739
Romain Guy86568192010-12-14 15:55:39 -08001740void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001741 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001742 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001743 }
1744}
1745
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001746void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
1747 if (shader == NULL) {
1748 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001749 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001750
1751 if (ignoreTransform) {
1752 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1753 // because it was built into modelView / the geometry, and the description needs to
1754 // compensate.
1755 mat4 modelViewWithoutTransform;
1756 modelViewWithoutTransform.loadInverse(*currentTransform());
1757 modelViewWithoutTransform.multiply(mModelViewMatrix);
1758 mModelViewMatrix.load(modelViewWithoutTransform);
1759 }
1760
1761 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001762}
1763
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001764void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
1765 if (NULL == filter) {
1766 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001767 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001768
1769 SkColor color;
1770 SkXfermode::Mode mode;
1771 if (filter->asColorMode(&color, &mode)) {
1772 const int alpha = SkColorGetA(color);
1773 const GLfloat a = alpha / 255.0f;
1774 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1775 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1776 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1777 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1778 return;
1779 }
1780
1781 SkScalar srcColorMatrix[20];
1782 if (filter->asColorMatrix(srcColorMatrix)) {
1783
1784 float colorMatrix[16];
1785 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1786 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1787 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1788 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1789
1790 // Skia uses the range [0..255] for the addition vector, but we need
1791 // the [0..1] range to apply the vector in GLSL
1792 float colorVector[4];
1793 colorVector[0] = srcColorMatrix[4] / 255.0f;
1794 colorVector[1] = srcColorMatrix[9] / 255.0f;
1795 colorVector[2] = srcColorMatrix[14] / 255.0f;
1796 colorVector[3] = srcColorMatrix[19] / 255.0f;
1797
1798 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1799 GL_FALSE, colorMatrix);
1800 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1801 return;
1802 }
1803
1804 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001805}
1806
Romain Guy41210632012-07-16 17:04:24 -07001807void OpenGLRenderer::setupDrawTextGammaUniforms() {
1808 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1809}
1810
Romain Guy70ca14e2010-12-13 18:24:33 -08001811void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001812 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001813 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001814 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001815}
1816
1817void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001818 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001819 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001820 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001821}
1822
Romain Guyaa6c24c2011-04-28 18:40:04 -07001823void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1824 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001825 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001826 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001827}
1828
Romain Guy8f0095c2011-05-02 17:24:22 -07001829void OpenGLRenderer::setupDrawTextureTransform() {
1830 mDescription.hasTextureTransform = true;
1831}
1832
1833void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001834 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1835 GL_FALSE, &transform.data[0]);
1836}
1837
Chris Craik564acf72014-01-02 16:46:18 -08001838void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1839 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001840 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001841 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001842 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001843 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001844 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001845 }
Romain Guyd71dd362011-12-12 19:03:35 -08001846
Chris Craikcb4d6002012-09-25 12:00:29 -07001847 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001848 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001849 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001850 }
1851
1852 mCaches.unbindIndicesBuffer();
1853}
1854
Chris Craik564acf72014-01-02 16:46:18 -08001855void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1856 const GLvoid* texCoords, const GLvoid* colors) {
Romain Guyff316ec2013-02-13 18:39:43 -08001857 bool force = mCaches.unbindMeshBuffer();
1858 GLsizei stride = sizeof(ColorTextureVertex);
1859
1860 mCaches.bindPositionVertexPointer(force, vertices, stride);
1861 if (mCaches.currentProgram->texCoords >= 0) {
1862 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1863 }
1864 int slot = mCaches.currentProgram->getAttrib("colors");
1865 if (slot >= 0) {
1866 glEnableVertexAttribArray(slot);
1867 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1868 }
1869
1870 mCaches.unbindIndicesBuffer();
1871}
1872
Chris Craik564acf72014-01-02 16:46:18 -08001873void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1874 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001875 bool force = false;
1876 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1877 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1878 // use the default VBO found in Caches
1879 if (!vertices || vbo) {
1880 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1881 } else {
1882 force = mCaches.unbindMeshBuffer();
1883 }
ztenghui63d41ab2014-02-14 13:13:41 -08001884 mCaches.bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001885
Chris Craikcb4d6002012-09-25 12:00:29 -07001886 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001887 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001888 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001889 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001890}
1891
Romain Guy448455f2013-07-22 13:57:50 -07001892void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001893 bool force = mCaches.unbindMeshBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08001894 mCaches.bindQuadIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001895 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001896}
1897
Romain Guy70ca14e2010-12-13 18:24:33 -08001898///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001899// Drawing
1900///////////////////////////////////////////////////////////////////////////////
1901
Chris Craika7090e02014-06-20 16:01:00 -07001902status_t OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07001903 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08001904 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1905 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001906 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001907 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001908 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001909 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07001910 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001911 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001912 renderNode->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07001913 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08001914 }
1915
Chris Craik28ce94a2013-05-31 11:38:03 -07001916 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
Chris Craikd6b65f62014-01-01 14:45:21 -08001917 DeferredDisplayList deferredList(*currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001918 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001919 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001920
1921 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07001922 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001923
Chris Craikf57776b2013-10-25 18:30:17 -07001924 return deferredList.flush(*this, dirty) | status;
Romain Guy0fe478e2010-11-08 12:08:41 -08001925 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001926
Romain Guy65549432012-03-26 16:45:05 -07001927 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001928}
1929
Chris Craikd218a922014-01-02 17:13:34 -08001930void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, const SkPaint* paint) {
Romain Guy886b2752013-01-04 12:26:18 -08001931 int color = paint != NULL ? paint->getColor() : 0;
1932
Romain Guya168d732011-03-18 16:50:13 -07001933 float x = left;
1934 float y = top;
1935
Romain Guy886b2752013-01-04 12:26:18 -08001936 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1937
Romain Guya168d732011-03-18 16:50:13 -07001938 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001939 if (currentTransform()->isPureTranslate()) {
1940 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1941 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001942 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001943
1944 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001945 } else {
Chris Craik678625242014-02-28 12:26:34 -08001946 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001947 }
1948
Romain Guy3b748a42013-04-17 18:54:38 -07001949 // No need to check for a UV mapper on the texture object, only ARGB_8888
1950 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001951 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001952 paint, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy3b748a42013-04-17 18:54:38 -07001953 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001954}
1955
Romain Guy03c00b52013-06-20 18:30:28 -07001956/**
1957 * Important note: this method is intended to draw batches of bitmaps and
1958 * will not set the scissor enable or dirty the current layer, if any.
1959 * The caller is responsible for properly dirtying the current layer.
1960 */
Chris Craikd218a922014-01-02 17:13:34 -08001961status_t OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
1962 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1963 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001964 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001965 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08001966 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07001967
Chris Craik527a3aa2013-03-04 10:19:31 -08001968 const AutoTexture autoCleanup(texture);
1969
Chris Craik527a3aa2013-03-04 10:19:31 -08001970 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08001971 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08001972
1973 const float x = (int) floorf(bounds.left + 0.5f);
1974 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04001975 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001976 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001977 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001978 GL_TRIANGLES, bitmapCount * 6, true,
1979 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001980 } else {
1981 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001982 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001983 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
1984 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001985 }
1986
1987 return DrawGlInfo::kStatusDrew;
1988}
1989
Chris Craikd218a922014-01-02 17:13:34 -08001990status_t OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, float left, float top,
1991 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001992 const float right = left + bitmap->width();
1993 const float bottom = top + bitmap->height();
1994
Chris Craikf0a59072013-11-19 18:00:46 -08001995 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001996 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001997 }
1998
Romain Guya1d3c912011-12-13 14:55:06 -08001999 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002000 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002001 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002002 const AutoTexture autoCleanup(texture);
2003
Mike Reed1103b322014-07-08 12:36:44 -04002004 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guya168d732011-03-18 16:50:13 -07002005 drawAlphaBitmap(texture, left, top, paint);
2006 } else {
2007 drawTextureRect(left, top, right, bottom, texture, paint);
2008 }
Chet Haase48659092012-05-31 15:21:51 -07002009
2010 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002011}
2012
Derek Sollenberger13908822013-12-10 12:28:58 -05002013status_t OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkMatrix& matrix,
Chris Craikd218a922014-01-02 17:13:34 -08002014 const SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002015 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
Derek Sollenberger13908822013-12-10 12:28:58 -05002016 const mat4 transform(matrix);
Romain Guyf86ef572010-07-01 11:05:42 -07002017 transform.mapRect(r);
2018
Chris Craikf0a59072013-11-19 18:00:46 -08002019 if (quickRejectSetupScissor(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002020 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002021 }
2022
Romain Guya1d3c912011-12-13 14:55:06 -08002023 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002024 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002025 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002026 const AutoTexture autoCleanup(texture);
2027
Romain Guy5b3b3522010-10-27 18:57:51 -07002028 // This could be done in a cheaper way, all we need is pass the matrix
2029 // to the vertex shader. The save/restore is a bit overkill.
2030 save(SkCanvas::kMatrix_SaveFlag);
2031 concatMatrix(matrix);
Mike Reed1103b322014-07-08 12:36:44 -04002032 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002033 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2034 } else {
2035 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2036 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002037 restore();
Chet Haase48659092012-05-31 15:21:51 -07002038
2039 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002040}
2041
Chris Craikd218a922014-01-02 17:13:34 -08002042status_t OpenGLRenderer::drawBitmapData(const SkBitmap* bitmap, float left, float top,
2043 const SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002044 const float right = left + bitmap->width();
2045 const float bottom = top + bitmap->height();
2046
Chris Craikf0a59072013-11-19 18:00:46 -08002047 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002048 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002049 }
2050
2051 mCaches.activeTexture(0);
2052 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2053 const AutoTexture autoCleanup(texture);
2054
Mike Reed1103b322014-07-08 12:36:44 -04002055 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002056 drawAlphaBitmap(texture, left, top, paint);
2057 } else {
2058 drawTextureRect(left, top, right, bottom, texture, paint);
2059 }
Chet Haase48659092012-05-31 15:21:51 -07002060
2061 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002062}
2063
Chris Craikd218a922014-01-02 17:13:34 -08002064status_t OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
2065 const float* vertices, const int* colors, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002066 if (!vertices || currentSnapshot()->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002067 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002068 }
2069
Chris Craik39a908c2013-06-13 14:39:01 -07002070 // TODO: use quickReject on bounds from vertices
2071 mCaches.enableScissor();
2072
Romain Guyb18d2d02011-02-10 15:52:54 -08002073 float left = FLT_MAX;
2074 float top = FLT_MAX;
2075 float right = FLT_MIN;
2076 float bottom = FLT_MIN;
2077
Romain Guya92bb4d2012-10-16 11:08:44 -07002078 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002079
Chris Craik564acf72014-01-02 16:46:18 -08002080 Vector<ColorTextureVertex> mesh; // TODO: use C++11 unique_ptr
2081 mesh.setCapacity(count);
2082 ColorTextureVertex* vertex = mesh.editArray();
Romain Guyff316ec2013-02-13 18:39:43 -08002083
2084 bool cleanupColors = false;
2085 if (!colors) {
2086 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craikd218a922014-01-02 17:13:34 -08002087 int* newColors = new int[colorsCount];
2088 memset(newColors, 0xff, colorsCount * sizeof(int));
2089 colors = newColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002090 cleanupColors = true;
2091 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002092
Romain Guy3b748a42013-04-17 18:54:38 -07002093 mCaches.activeTexture(0);
2094 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2095 const UvMapper& mapper(getMapper(texture));
2096
Romain Guy5a7b4662011-01-20 19:09:30 -08002097 for (int32_t y = 0; y < meshHeight; y++) {
2098 for (int32_t x = 0; x < meshWidth; x++) {
2099 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2100
2101 float u1 = float(x) / meshWidth;
2102 float u2 = float(x + 1) / meshWidth;
2103 float v1 = float(y) / meshHeight;
2104 float v2 = float(y + 1) / meshHeight;
2105
Romain Guy3b748a42013-04-17 18:54:38 -07002106 mapper.map(u1, v1, u2, v2);
2107
Romain Guy5a7b4662011-01-20 19:09:30 -08002108 int ax = i + (meshWidth + 1) * 2;
2109 int ay = ax + 1;
2110 int bx = i;
2111 int by = bx + 1;
2112 int cx = i + 2;
2113 int cy = cx + 1;
2114 int dx = i + (meshWidth + 1) * 2 + 2;
2115 int dy = dx + 1;
2116
Romain Guyff316ec2013-02-13 18:39:43 -08002117 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2118 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2119 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002120
Romain Guyff316ec2013-02-13 18:39:43 -08002121 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2122 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2123 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002124
Romain Guya92bb4d2012-10-16 11:08:44 -07002125 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2126 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2127 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2128 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002129 }
2130 }
2131
Chris Craikf0a59072013-11-19 18:00:46 -08002132 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002133 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002134 return DrawGlInfo::kStatusDone;
2135 }
2136
Romain Guyff316ec2013-02-13 18:39:43 -08002137 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002138 texture = mCaches.textureCache.get(bitmap);
2139 if (!texture) {
2140 if (cleanupColors) delete[] colors;
2141 return DrawGlInfo::kStatusDone;
2142 }
Romain Guyff316ec2013-02-13 18:39:43 -08002143 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002144 const AutoTexture autoCleanup(texture);
2145
2146 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002147 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002148
2149 int alpha;
2150 SkXfermode::Mode mode;
2151 getAlphaAndMode(paint, &alpha, &mode);
2152
Romain Guyff316ec2013-02-13 18:39:43 -08002153 float a = alpha / 255.0f;
2154
Romain Guya92bb4d2012-10-16 11:08:44 -07002155 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002156 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002157 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002158
Romain Guyff316ec2013-02-13 18:39:43 -08002159 setupDraw();
2160 setupDrawWithTextureAndColor();
2161 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002162 setupDrawColorFilter(getColorFilter(paint));
2163 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002164 setupDrawProgram();
2165 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002166 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002167 setupDrawTexture(texture->id);
2168 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002169 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002170 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002171
2172 glDrawArrays(GL_TRIANGLES, 0, count);
2173
Romain Guyff316ec2013-02-13 18:39:43 -08002174 int slot = mCaches.currentProgram->getAttrib("colors");
2175 if (slot >= 0) {
2176 glDisableVertexAttribArray(slot);
2177 }
2178
2179 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002180
2181 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002182}
2183
Chris Craikd218a922014-01-02 17:13:34 -08002184status_t OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002185 float srcLeft, float srcTop, float srcRight, float srcBottom,
2186 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002187 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002188 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002189 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002190 }
2191
Romain Guya1d3c912011-12-13 14:55:06 -08002192 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002193 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002194 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002195 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002196
Romain Guy8ba548f2010-06-30 19:21:21 -07002197 const float width = texture->width;
2198 const float height = texture->height;
2199
Romain Guy3b748a42013-04-17 18:54:38 -07002200 float u1 = fmax(0.0f, srcLeft / width);
2201 float v1 = fmax(0.0f, srcTop / height);
2202 float u2 = fmin(1.0f, srcRight / width);
2203 float v2 = fmin(1.0f, srcBottom / height);
2204
2205 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002206
Romain Guy03750a02010-10-18 14:06:08 -07002207 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002208 resetDrawTextureTexCoords(u1, v1, u2, v2);
2209
Romain Guyd21b6e12011-11-30 20:21:23 -08002210 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2211
Romain Guy886b2752013-01-04 12:26:18 -08002212 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2213 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002214
Romain Guy886b2752013-01-04 12:26:18 -08002215 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2216 // Apply a scale transform on the canvas only when a shader is in use
2217 // Skia handles the ratio between the dst and src rects as a scale factor
2218 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002219 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002220 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002221
Chris Craikd6b65f62014-01-01 14:45:21 -08002222 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2223 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2224 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002225
2226 dstRight = x + (dstRight - dstLeft);
2227 dstBottom = y + (dstBottom - dstTop);
2228
2229 dstLeft = x;
2230 dstTop = y;
2231
Chris Craik678625242014-02-28 12:26:34 -08002232 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002233 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002234 } else {
Chris Craik678625242014-02-28 12:26:34 -08002235 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002236 }
2237
2238 if (CC_UNLIKELY(useScaleTransform)) {
2239 save(SkCanvas::kMatrix_SaveFlag);
2240 translate(dstLeft, dstTop);
2241 scale(scaleX, scaleY);
2242
2243 dstLeft = 0.0f;
2244 dstTop = 0.0f;
2245
2246 dstRight = srcRight - srcLeft;
2247 dstBottom = srcBottom - srcTop;
2248 }
2249
Mike Reed1103b322014-07-08 12:36:44 -04002250 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002251 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002252 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002253 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002254 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2255 } else {
2256 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002257 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002258 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002259 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2260 }
2261
2262 if (CC_UNLIKELY(useScaleTransform)) {
2263 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002264 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002265
2266 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002267
2268 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002269}
2270
Chris Craikd218a922014-01-02 17:13:34 -08002271status_t OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
2272 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002273 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002274 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002275 }
2276
Romain Guy4c2547f2013-06-11 16:19:24 -07002277 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002278 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2279 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002280
Romain Guy03c00b52013-06-20 18:30:28 -07002281 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002282}
2283
Chris Craikd218a922014-01-02 17:13:34 -08002284status_t OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
2285 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2286 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002287 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002288 return DrawGlInfo::kStatusDone;
2289 }
2290
Romain Guy211370f2012-02-01 16:10:55 -08002291 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002292 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002293 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002294 if (!texture) return DrawGlInfo::kStatusDone;
2295 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002296
Romain Guya4adcf02013-02-28 12:15:35 -08002297 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2298 texture->setFilter(GL_LINEAR, true);
2299
Chris Craikd6b65f62014-01-01 14:45:21 -08002300 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002301 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002302 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002303 const float offsetX = left + currentTransform()->getTranslateX();
2304 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002305 const size_t count = mesh->quads.size();
2306 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002307 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002308 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002309 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2310 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2311 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002312 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002313 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002314 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002315 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002316 }
2317 }
2318
Chris Craik4063a0e2013-11-15 16:06:56 -08002319 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002320 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002321 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2322 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002323
Romain Guy3b748a42013-04-17 18:54:38 -07002324 right = x + right - left;
2325 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002326 left = x;
2327 top = y;
2328 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002329 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002330 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2331 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002332 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2333 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002334 }
Chet Haase48659092012-05-31 15:21:51 -07002335
2336 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002337}
2338
Romain Guy03c00b52013-06-20 18:30:28 -07002339/**
2340 * Important note: this method is intended to draw batches of 9-patch objects and
2341 * will not set the scissor enable or dirty the current layer, if any.
2342 * The caller is responsible for properly dirtying the current layer.
2343 */
Chris Craikd218a922014-01-02 17:13:34 -08002344status_t OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
2345 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002346 mCaches.activeTexture(0);
2347 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2348 if (!texture) return DrawGlInfo::kStatusDone;
2349 const AutoTexture autoCleanup(texture);
2350
2351 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2352 texture->setFilter(GL_LINEAR, true);
2353
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002354 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2355 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002356 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002357
2358 return DrawGlInfo::kStatusDrew;
2359}
2360
Chris Craik05f3d6e2014-06-02 16:27:04 -07002361status_t OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
ztenghui63d41ab2014-02-14 13:13:41 -08002362 const VertexBuffer& vertexBuffer, const SkPaint* paint, bool useOffset) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002363 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002364 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002365 // no vertices to draw
2366 return DrawGlInfo::kStatusDone;
2367 }
2368
Chris Craik0d5ac952014-07-15 13:01:02 -07002369 Rect bounds(vertexBuffer.getBounds());
2370 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002371 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2372
Chris Craikcb4d6002012-09-25 12:00:29 -07002373 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002374 bool isAA = paint->isAntiAlias();
2375
Chris Craik710f46d2012-09-17 17:25:49 -07002376 setupDraw();
2377 setupDrawNoTexture();
2378 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002379 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002380 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002381 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002382 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002383 setupDrawProgram();
Chris Craik05f3d6e2014-06-02 16:27:04 -07002384 setupDrawModelView(kModelViewMode_Translate, useOffset, translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002385 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002386 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002387 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002388
ztenghui55bfb4e2013-12-03 10:38:55 -08002389 const void* vertices = vertexBuffer.getBuffer();
Chris Craik710f46d2012-09-17 17:25:49 -07002390 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002391 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002392 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002393
Chet Haase858aa932011-05-12 09:06:00 -07002394
Chris Craik710f46d2012-09-17 17:25:49 -07002395 int alphaSlot = -1;
2396 if (isAA) {
2397 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2398 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002399 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002400 glEnableVertexAttribArray(alphaSlot);
2401 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002402 }
Romain Guy04299382012-07-18 17:15:41 -07002403
Chris Craik05f3d6e2014-06-02 16:27:04 -07002404 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2405 if (mode == VertexBuffer::kStandard) {
ztenghui63d41ab2014-02-14 13:13:41 -08002406 mCaches.unbindIndicesBuffer();
2407 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002408 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002409 mCaches.bindShadowIndicesBuffer();
ztenghui50ecf842014-03-11 16:52:30 -07002410 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002411 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
ztenghui50ecf842014-03-11 16:52:30 -07002412 mCaches.bindShadowIndicesBuffer();
2413 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
ztenghui63d41ab2014-02-14 13:13:41 -08002414 }
Romain Guy04299382012-07-18 17:15:41 -07002415
Chris Craik710f46d2012-09-17 17:25:49 -07002416 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002417 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002418 }
Chris Craik65cd6122012-12-10 17:56:27 -08002419
2420 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002421}
2422
2423/**
Chris Craik65cd6122012-12-10 17:56:27 -08002424 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2425 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2426 * screen space in all directions. However, instead of using a fragment shader to compute the
2427 * translucency of the color from its position, we simply use a varying parameter to define how far
2428 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2429 *
2430 * Doesn't yet support joins, caps, or path effects.
2431 */
Chris Craikd218a922014-01-02 17:13:34 -08002432status_t OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002433 VertexBuffer vertexBuffer;
2434 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002435 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002436 return drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002437}
2438
2439/**
2440 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2441 * and additional geometry for defining an alpha slope perimeter.
2442 *
2443 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2444 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2445 * in-shader alpha region, but found it to be taxing on some GPUs.
2446 *
2447 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2448 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002449 */
Chris Craikd218a922014-01-02 17:13:34 -08002450status_t OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002451 if (currentSnapshot()->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002452
Chris Craik65cd6122012-12-10 17:56:27 -08002453 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002454
Chris Craik65cd6122012-12-10 17:56:27 -08002455 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002456 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2457 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002458
Chris Craik05f3d6e2014-06-02 16:27:04 -07002459 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Romain Guyd71ff91d2013-02-08 13:46:40 -08002460 return DrawGlInfo::kStatusDone;
2461 }
2462
Chris Craik65cd6122012-12-10 17:56:27 -08002463 bool useOffset = !paint->isAntiAlias();
Chris Craik05f3d6e2014-06-02 16:27:04 -07002464 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002465}
2466
Chris Craikd218a922014-01-02 17:13:34 -08002467status_t OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002468 if (currentSnapshot()->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002469
Chris Craik6d29c8d2013-05-08 18:35:44 -07002470 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002471
Chris Craik6d29c8d2013-05-08 18:35:44 -07002472 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002473 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002474
Chris Craik05f3d6e2014-06-02 16:27:04 -07002475 const Rect& bounds = buffer.getBounds();
2476 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002477 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002478 }
2479
Chris Craik6d29c8d2013-05-08 18:35:44 -07002480 bool useOffset = !paint->isAntiAlias();
Chris Craik05f3d6e2014-06-02 16:27:04 -07002481 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002482}
2483
Chet Haase48659092012-05-31 15:21:51 -07002484status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002485 // No need to check against the clip, we fill the clip region
Chris Craikd6b65f62014-01-01 14:45:21 -08002486 if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002487
Chris Craikd6b65f62014-01-01 14:45:21 -08002488 Rect clip(*currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002489 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002490
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002491 SkPaint paint;
2492 paint.setColor(color);
2493 paint.setXfermodeMode(mode);
2494
2495 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002496
2497 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002498}
Romain Guy9d5316e2010-06-24 19:30:36 -07002499
Chet Haase48659092012-05-31 15:21:51 -07002500status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002501 const SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002502 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002503 const AutoTexture autoCleanup(texture);
2504
2505 const float x = left + texture->left - texture->offset;
2506 const float y = top + texture->top - texture->offset;
2507
2508 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002509
2510 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002511}
2512
Chet Haase48659092012-05-31 15:21:51 -07002513status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002514 float rx, float ry, const SkPaint* p) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002515 if (currentSnapshot()->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002516 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002517 return DrawGlInfo::kStatusDone;
2518 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002519
Chris Craikcb4d6002012-09-25 12:00:29 -07002520 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002521 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002522 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002523 right - left, bottom - top, rx, ry, p);
2524 return drawShape(left, top, texture, p);
2525 }
2526
Chris Craik6ac174b2014-06-17 13:47:05 -07002527 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2528 *currentTransform(), *p, right - left, bottom - top, rx, ry);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002529 return drawVertexBuffer(left, top, *vertexBuffer, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002530}
2531
Chris Craikd218a922014-01-02 17:13:34 -08002532status_t OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002533 if (currentSnapshot()->isIgnored() || quickRejectSetupScissor(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002534 x + radius, y + radius, p) ||
2535 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002536 return DrawGlInfo::kStatusDone;
2537 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002538 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002539 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002540 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002541 return drawShape(x - radius, y - radius, texture, p);
2542 }
2543
2544 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002545 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2546 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2547 } else {
2548 path.addCircle(x, y, radius);
2549 }
Chris Craik65cd6122012-12-10 17:56:27 -08002550 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002551}
Romain Guy01d58e42011-01-19 21:54:02 -08002552
Chet Haase48659092012-05-31 15:21:51 -07002553status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002554 const SkPaint* p) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002555 if (currentSnapshot()->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002556 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002557 return DrawGlInfo::kStatusDone;
2558 }
Romain Guy01d58e42011-01-19 21:54:02 -08002559
Chris Craikcb4d6002012-09-25 12:00:29 -07002560 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002561 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002562 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002563 return drawShape(left, top, texture, p);
2564 }
2565
2566 SkPath path;
2567 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002568 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2569 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2570 }
Chris Craik710f46d2012-09-17 17:25:49 -07002571 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002572 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002573}
2574
Chet Haase48659092012-05-31 15:21:51 -07002575status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002576 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002577 if (currentSnapshot()->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002578 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002579 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002580 }
2581
Chris Craik780c1282012-10-04 14:10:49 -07002582 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002583 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002584 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002585 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002586 startAngle, sweepAngle, useCenter, p);
2587 return drawShape(left, top, texture, p);
2588 }
2589
2590 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2591 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2592 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2593 }
2594
2595 SkPath path;
2596 if (useCenter) {
2597 path.moveTo(rect.centerX(), rect.centerY());
2598 }
2599 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2600 if (useCenter) {
2601 path.close();
2602 }
Chris Craik65cd6122012-12-10 17:56:27 -08002603 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002604}
2605
Romain Guycf8675e2012-10-02 12:32:25 -07002606// See SkPaintDefaults.h
2607#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2608
Chris Craikd218a922014-01-02 17:13:34 -08002609status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
2610 const SkPaint* p) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002611 if (currentSnapshot()->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002612 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002613 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002614 }
2615
Chris Craik710f46d2012-09-17 17:25:49 -07002616 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002617 // only fill style is supported by drawConvexPath, since others have to handle joins
2618 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2619 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2620 mCaches.activeTexture(0);
2621 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002622 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002623 return drawShape(left, top, texture, p);
2624 }
2625
2626 SkPath path;
2627 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2628 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2629 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2630 }
2631 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002632 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002633 }
2634
Chris Craikd6b65f62014-01-01 14:45:21 -08002635 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002636 SkPath path;
2637 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002638 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002639 } else {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002640 drawColorRect(left, top, right, bottom, p);
Chris Craik65cd6122012-12-10 17:56:27 -08002641 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002642 }
Romain Guyc7d53492010-06-25 13:41:57 -07002643}
Romain Guy9d5316e2010-06-24 19:30:36 -07002644
Chris Craikd218a922014-01-02 17:13:34 -08002645void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2646 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002647 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002648 mCaches.activeTexture(0);
2649
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002650 TextShadow textShadow;
2651 if (!getTextShadow(paint, &textShadow)) {
2652 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2653 }
2654
Raph Levien416a8472012-07-19 22:48:17 -07002655 // NOTE: The drop shadow will not perform gamma correction
2656 // if shader-based correction is enabled
2657 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2658 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002659 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002660 // If the drop shadow exceeds the max texture size or couldn't be
2661 // allocated, skip drawing
2662 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002663 const AutoTexture autoCleanup(shadow);
2664
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002665 const float sx = x - shadow->left + textShadow.dx;
2666 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002667
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002668 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * mSnapshot->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002669 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002670 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002671 }
2672
2673 setupDraw();
2674 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002675 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002676 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002677 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002678 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002679 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002680 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2681 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002682 setupDrawTexture(shadow->id);
2683 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002684 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002685 setupDrawShaderUniforms(getShader(paint));
Raph Levien416a8472012-07-19 22:48:17 -07002686 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2687
2688 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2689}
2690
Romain Guy768bffc2013-02-27 13:50:45 -08002691bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002692 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
Romain Guy768bffc2013-02-27 13:50:45 -08002693 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2694}
2695
Chet Haase48659092012-05-31 15:21:51 -07002696status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002697 const float* positions, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002698 if (text == NULL || count == 0 || currentSnapshot()->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002699 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002700 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002701
Romain Guy671d6cf2012-01-18 12:39:17 -08002702 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002703 if (!currentTransform()->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002704 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002705 }
2706
Chris Craik39a908c2013-06-13 14:39:01 -07002707 mCaches.enableScissor();
2708
Romain Guy671d6cf2012-01-18 12:39:17 -08002709 float x = 0.0f;
2710 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002711 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002712 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002713 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2714 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002715 }
2716
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002717 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002718 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002719
2720 int alpha;
2721 SkXfermode::Mode mode;
2722 getAlphaAndMode(paint, &alpha, &mode);
2723
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002724 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002725 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002726 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002727 }
2728
Romain Guy671d6cf2012-01-18 12:39:17 -08002729 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002730 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002731 if (pureTranslate && !linearFilter) {
2732 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2733 }
Romain Guy257ae352013-03-20 16:31:12 -07002734 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002735
2736 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2737 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2738
Romain Guy211370f2012-02-01 16:10:55 -08002739 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002740
Victoria Lease1e546812013-06-25 14:25:17 -07002741 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002742 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002743 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002744 if (hasActiveLayer) {
2745 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002746 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002747 }
2748 dirtyLayerUnchecked(bounds, getRegion());
2749 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002750 }
Chet Haase48659092012-05-31 15:21:51 -07002751
2752 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002753}
2754
Chris Craik59744b72014-07-01 17:56:52 -07002755bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002756 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002757 outMatrix->setIdentity();
2758 return false;
2759 } else if (CC_UNLIKELY(transform.isPerspective())) {
2760 outMatrix->setIdentity();
2761 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002762 }
Chris Craik59744b72014-07-01 17:56:52 -07002763
2764 /**
2765 * Input is a non-perspective, scaling transform. Generate a scale-only transform, based upon
2766 * bucketed scale values. Special case for 'extra raster buckets' - disable filtration in the
2767 * case of an exact match, and isSimple() transform
2768 */
2769 float sx, sy;
2770 transform.decomposeScale(sx, sy);
2771
2772 float bestSx = roundf(fmaxf(1.0f, sx));
2773 float bestSy = roundf(fmaxf(1.0f, sy));
2774 bool filter = true;
2775
2776 for (unsigned int i = 0; i < mCaches.propertyExtraRasterBuckets.size(); i++) {
2777 float bucket = mCaches.propertyExtraRasterBuckets[i];
2778 if (sx == bucket && sy == bucket) {
2779 bestSx = bestSy = bucket;
2780 filter = !transform.isSimple(); // disable filter, if simple
2781 break;
2782 }
2783
2784 if (fabs(bucket - sx) < fabs(bestSx - sx)) bestSx = sx;
2785 if (fabs(bucket - sy) < fabs(bestSy - sy)) bestSy = sy;
2786 }
2787
2788 outMatrix->setScale(bestSx, bestSy);
2789 return filter;
Romain Guy624234f2013-03-05 16:43:31 -08002790}
2791
Chris Craik41541822013-05-03 16:35:54 -07002792status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002793 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002794 DrawOpMode drawOpMode) {
2795
Chris Craik527a3aa2013-03-04 10:19:31 -08002796 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002797 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2798 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craikd6b65f62014-01-01 14:45:21 -08002799 if (text == NULL || count == 0 || currentSnapshot()->isIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002800 quickRejectSetupScissor(bounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002801 return DrawGlInfo::kStatusDone;
2802 }
Romain Guycac5fd32011-12-01 20:08:50 -08002803 }
2804
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002805 const float oldX = x;
2806 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002807
Chris Craikd6b65f62014-01-01 14:45:21 -08002808 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002809 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002810
Romain Guy211370f2012-02-01 16:10:55 -08002811 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002812 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2813 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002814 }
2815
Romain Guy86568192010-12-14 15:55:39 -08002816 int alpha;
2817 SkXfermode::Mode mode;
2818 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002819
Romain Guyc74f45a2013-02-26 19:10:14 -08002820 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2821
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002822 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002823 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002824 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002825 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002826 }
2827
Romain Guy19d4dd82013-03-04 11:14:26 -08002828 const bool hasActiveLayer = hasLayer();
2829
Romain Guy624234f2013-03-05 16:43:31 -08002830 // We only pass a partial transform to the font renderer. That partial
2831 // matrix defines how glyphs are rasterized. Typically we want glyphs
2832 // to be rasterized at their final size on screen, which means the partial
2833 // matrix needs to take the scale factor into account.
2834 // When a partial matrix is used to transform glyphs during rasterization,
2835 // the mesh is generated with the inverse transform (in the case of scale,
2836 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2837 // apply the full transform matrix at draw time in the vertex shader.
2838 // Applying the full matrix in the shader is the easiest way to handle
2839 // rotation and perspective and allows us to always generated quads in the
2840 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002841 SkMatrix fontTransform;
2842 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2843 || fabs(y - (int) y) > 0.0f
2844 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002845 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002846 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002847
Romain Guy624234f2013-03-05 16:43:31 -08002848 // TODO: Implement better clipping for scaled/rotated text
Chris Craikd6b65f62014-01-01 14:45:21 -08002849 const Rect* clip = !pureTranslate ? NULL : currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002850 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 -07002851
Raph Levien996e57c2012-07-23 15:22:52 -07002852 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002853 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002854
2855 // don't call issuedrawcommand, do it at end of batch
2856 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002857 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002858 SkPaint paintCopy(*paint);
2859 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2860 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002861 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002862 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002863 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002864 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002865 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002866
Chris Craik527a3aa2013-03-04 10:19:31 -08002867 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002868 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002869 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002870 }
Chris Craik41541822013-05-03 16:35:54 -07002871 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002872 }
Romain Guy694b5192010-07-21 21:33:20 -07002873
Chris Craike63f7c622013-10-17 10:30:55 -07002874 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002875
2876 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002877}
2878
Chris Craikd218a922014-01-02 17:13:34 -08002879status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
2880 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002881 if (text == NULL || count == 0 || currentSnapshot()->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002882 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002883 }
2884
Chris Craik39a908c2013-06-13 14:39:01 -07002885 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2886 mCaches.enableScissor();
2887
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002888 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002889 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002890 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002891
2892 int alpha;
2893 SkXfermode::Mode mode;
2894 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002895 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002896
Romain Guy97771732012-02-28 18:17:02 -08002897 const Rect* clip = &mSnapshot->getLocalClip();
2898 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 -08002899
Romain Guy97771732012-02-28 18:17:02 -08002900 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002901
2902 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07002903 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002904 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002905 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002906 dirtyLayerUnchecked(bounds, getRegion());
2907 }
Romain Guy97771732012-02-28 18:17:02 -08002908 }
Chet Haase48659092012-05-31 15:21:51 -07002909
2910 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002911}
2912
Chris Craikd218a922014-01-02 17:13:34 -08002913status_t OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002914 if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002915
Romain Guya1d3c912011-12-13 14:55:06 -08002916 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002917
Romain Guyfb8b7632010-08-23 21:05:08 -07002918 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002919 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002920 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002921
Romain Guy8b55f372010-08-18 17:10:07 -07002922 const float x = texture->left - texture->offset;
2923 const float y = texture->top - texture->offset;
2924
Romain Guy01d58e42011-01-19 21:54:02 -08002925 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002926
2927 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002928}
2929
Chris Craika08f95c2013-03-15 17:24:33 -07002930status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002931 if (!layer) {
2932 return DrawGlInfo::kStatusDone;
2933 }
2934
Romain Guyb2e2f242012-10-17 18:18:35 -07002935 mat4* transform = NULL;
2936 if (layer->isTextureLayer()) {
2937 transform = &layer->getTransform();
2938 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002939 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002940 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002941 }
2942 }
2943
Chris Craik39a908c2013-06-13 14:39:01 -07002944 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08002945 const bool rejected = calculateQuickRejectForScissor(x, y,
Chris Craikdeeda3d2014-05-05 19:09:33 -07002946 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, NULL, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002947
2948 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002949 if (transform && !transform->isIdentity()) {
2950 restore();
2951 }
Chet Haase48659092012-05-31 15:21:51 -07002952 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002953 }
2954
Romain Guy5bb3c732012-11-29 17:52:58 -08002955 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002956
Chris Craik39a908c2013-06-13 14:39:01 -07002957 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08002958 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002959
Romain Guy211370f2012-02-01 16:10:55 -08002960 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002961 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002962 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2963 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002964 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002965
Chris Craik16ecda52013-03-29 10:59:59 -07002966 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002967 setupDraw();
2968 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002969 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002970 setupDrawColorFilter(layer->getColorFilter());
2971 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002972 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002973 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002974 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07002975 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08002976 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
2977 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2978 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002979
Romain Guyd21b6e12011-11-30 20:21:23 -08002980 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08002981 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07002982 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002983 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002984 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08002985 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07002986 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2987 }
Romain Guyf219da52011-01-16 12:54:25 -08002988
Romain Guy448455f2013-07-22 13:57:50 -07002989 TextureVertex* mesh = &layer->mesh[0];
2990 GLsizei elementsCount = layer->meshElementCount;
2991
2992 while (elementsCount > 0) {
2993 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
2994
Romain Guy3380cfd2013-08-15 16:57:57 -07002995 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07002996 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2997 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
2998
2999 elementsCount -= drawCount;
3000 // Though there are 4 vertices in a quad, we use 6 indices per
3001 // quad to draw with GL_TRIANGLES
3002 mesh += (drawCount / 6) * 4;
3003 }
Romain Guyf219da52011-01-16 12:54:25 -08003004
Romain Guy3a3133d2011-02-01 22:59:58 -08003005#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003006 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003007#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003008 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003009
Romain Guy5bb3c732012-11-29 17:52:58 -08003010 if (layer->debugDrawUpdate) {
3011 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003012
3013 SkPaint paint;
3014 paint.setColor(0x7f00ff00);
3015 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003016 }
Romain Guyf219da52011-01-16 12:54:25 -08003017 }
Chris Craik34416ea2013-04-15 16:08:28 -07003018 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003019
Romain Guyb2e2f242012-10-17 18:18:35 -07003020 if (transform && !transform->isIdentity()) {
3021 restore();
3022 }
3023
Chet Haase48659092012-05-31 15:21:51 -07003024 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003025}
3026
Romain Guy6926c722010-07-12 20:20:03 -07003027///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003028// Draw filters
3029///////////////////////////////////////////////////////////////////////////////
3030
3031void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003032 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3033 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003034 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003035 mDrawModifiers.mPaintFilterClearBits = 0;
3036 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003037}
3038
3039void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003040 mDrawModifiers.mHasDrawFilter = true;
3041 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3042 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003043}
3044
Chris Craikd218a922014-01-02 17:13:34 -08003045const SkPaint* OpenGLRenderer::filterPaint(const SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003046 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003047 return paint;
3048 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003049
3050 uint32_t flags = paint->getFlags();
3051
3052 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003053 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3054 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003055
3056 return &mFilteredPaint;
3057}
3058
3059///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003060// Drawing implementation
3061///////////////////////////////////////////////////////////////////////////////
3062
Chris Craikd218a922014-01-02 17:13:34 -08003063Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
Romain Guy3b748a42013-04-17 18:54:38 -07003064 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3065 if (!texture) {
3066 return mCaches.textureCache.get(bitmap);
3067 }
3068 return texture;
3069}
3070
Romain Guy01d58e42011-01-19 21:54:02 -08003071void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003072 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003073 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003074 return;
3075 }
3076
3077 int alpha;
3078 SkXfermode::Mode mode;
3079 getAlphaAndMode(paint, &alpha, &mode);
3080
3081 setupDraw();
3082 setupDrawWithTexture(true);
3083 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003084 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003085 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003086 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003087 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003088 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3089 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003090 setupDrawTexture(texture->id);
3091 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003092 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003093 setupDrawShaderUniforms(getShader(paint));
Romain Guy01d58e42011-01-19 21:54:02 -08003094 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3095
3096 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003097}
3098
Romain Guyf607bdc2010-09-10 19:20:06 -07003099// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003100#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3101#define kStdUnderline_Offset (1.0f / 9.0f)
3102#define kStdUnderline_Thickness (1.0f / 18.0f)
3103
Chris Craikd218a922014-01-02 17:13:34 -08003104void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3105 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003106 // Handle underline and strike-through
3107 uint32_t flags = paint->getFlags();
3108 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003109 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003110
Romain Guy211370f2012-02-01 16:10:55 -08003111 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003112 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003113 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003114
Raph Levien8b4072d2012-07-30 15:50:00 -07003115 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003116 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003117
Romain Guyf6834472011-01-23 13:32:12 -08003118 int linesCount = 0;
3119 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3120 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3121
3122 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003123 float points[pointsCount];
3124 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003125
3126 if (flags & SkPaint::kUnderlineText_Flag) {
3127 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003128 points[currentPoint++] = left;
3129 points[currentPoint++] = top;
3130 points[currentPoint++] = left + underlineWidth;
3131 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003132 }
3133
3134 if (flags & SkPaint::kStrikeThruText_Flag) {
3135 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003136 points[currentPoint++] = left;
3137 points[currentPoint++] = top;
3138 points[currentPoint++] = left + underlineWidth;
3139 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003140 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003141
Romain Guy726aeba2011-06-01 14:52:00 -07003142 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003143
Romain Guy726aeba2011-06-01 14:52:00 -07003144 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003145 }
3146 }
3147}
3148
Chris Craikd218a922014-01-02 17:13:34 -08003149status_t OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003150 if (currentSnapshot()->isIgnored()) {
Romain Guy672433d2013-01-04 19:05:13 -08003151 return DrawGlInfo::kStatusDone;
3152 }
3153
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003154 return drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003155}
3156
Chris Craikb79a3e32014-03-11 12:20:17 -07003157static void mapPointFakeZ(Vector3& point, const mat4& transformXY, const mat4& transformZ) {
3158 // map z coordinate with true 3d matrix
3159 point.z = transformZ.mapZ(point);
3160
3161 // map x,y coordinates with draw/Skia matrix
3162 transformXY.mapPoint(point.x, point.y);
3163}
3164
Chris Craik05f3d6e2014-06-02 16:27:04 -07003165status_t OpenGLRenderer::drawShadow(float casterAlpha,
3166 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003167 if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
Chris Craikf57776b2013-10-25 18:30:17 -07003168
Chris Craik15a07a22014-01-26 13:43:53 -08003169 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craikf57776b2013-10-25 18:30:17 -07003170 mCaches.enableScissor();
3171
3172 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003173 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003174
Chris Craik05f3d6e2014-06-02 16:27:04 -07003175 if (ambientShadowVertexBuffer && mCaches.propertyAmbientShadowStrength > 0) {
Chris Craik919e95c2014-02-21 17:13:45 -08003176 paint.setARGB(casterAlpha * mCaches.propertyAmbientShadowStrength, 0, 0, 0);
Chris Craik05f3d6e2014-06-02 16:27:04 -07003177 drawVertexBuffer(*ambientShadowVertexBuffer, &paint);
ztenghuief94c6f2014-02-13 17:09:45 -08003178 }
ztenghui7b4516e2014-01-07 10:42:55 -08003179
Chris Craik05f3d6e2014-06-02 16:27:04 -07003180 if (spotShadowVertexBuffer && mCaches.propertySpotShadowStrength > 0) {
Chris Craik919e95c2014-02-21 17:13:45 -08003181 paint.setARGB(casterAlpha * mCaches.propertySpotShadowStrength, 0, 0, 0);
Chris Craik05f3d6e2014-06-02 16:27:04 -07003182 drawVertexBuffer(*spotShadowVertexBuffer, &paint);
ztenghuief94c6f2014-02-13 17:09:45 -08003183 }
ztenghui7b4516e2014-01-07 10:42:55 -08003184
3185 return DrawGlInfo::kStatusDrew;
Chris Craikf57776b2013-10-25 18:30:17 -07003186}
3187
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003188status_t OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
3189 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003190 if (count == 0) {
3191 return DrawGlInfo::kStatusDone;
3192 }
Romain Guy735738c2012-12-03 12:34:51 -08003193
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003194 int color = paint->getColor();
3195 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003196 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003197 color |= 0x00ffffff;
3198 }
3199
Romain Guy672433d2013-01-04 19:05:13 -08003200 float left = FLT_MAX;
3201 float top = FLT_MAX;
3202 float right = FLT_MIN;
3203 float bottom = FLT_MIN;
3204
Romain Guy448455f2013-07-22 13:57:50 -07003205 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003206 Vertex* vertex = mesh;
3207
Chris Craik2af46352012-11-26 18:30:17 -08003208 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003209 float l = rects[index + 0];
3210 float t = rects[index + 1];
3211 float r = rects[index + 2];
3212 float b = rects[index + 3];
3213
Romain Guy3b753822013-03-05 10:27:35 -08003214 Vertex::set(vertex++, l, t);
3215 Vertex::set(vertex++, r, t);
3216 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003217 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003218
Romain Guy3b753822013-03-05 10:27:35 -08003219 left = fminf(left, l);
3220 top = fminf(top, t);
3221 right = fmaxf(right, r);
3222 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003223 }
3224
Chris Craikf0a59072013-11-19 18:00:46 -08003225 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003226 return DrawGlInfo::kStatusDone;
3227 }
Romain Guy672433d2013-01-04 19:05:13 -08003228
Romain Guy672433d2013-01-04 19:05:13 -08003229 setupDraw();
3230 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003231 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003232 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003233 setupDrawColorFilter(getColorFilter(paint));
3234 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003235 setupDrawProgram();
3236 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003237 setupDrawModelView(kModelViewMode_Translate, false,
3238 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003239 setupDrawColorUniforms(getShader(paint));
3240 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003241 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003242
Romain Guy8ce00302013-01-15 18:51:42 -08003243 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003244 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003245 }
3246
Chris Craik4063a0e2013-11-15 16:06:56 -08003247 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003248
3249 return DrawGlInfo::kStatusDrew;
3250}
3251
Romain Guy026c5e162010-06-28 17:12:22 -07003252void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003253 const SkPaint* paint, bool ignoreTransform) {
3254 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003255 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003256 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003257 color |= 0x00ffffff;
3258 }
3259
Romain Guy70ca14e2010-12-13 18:24:33 -08003260 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003261 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003262 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003263 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003264 setupDrawColorFilter(getColorFilter(paint));
3265 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003266 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003267 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3268 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003269 setupDrawColorUniforms(getShader(paint));
3270 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003271 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003272 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003273
Romain Guyc95c8d62010-09-17 15:31:32 -07003274 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3275}
3276
Romain Guy82ba8142010-07-09 13:25:56 -07003277void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003278 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003279 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003280
Romain Guy3b748a42013-04-17 18:54:38 -07003281 GLvoid* vertices = (GLvoid*) NULL;
3282 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3283
3284 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003285 vertices = &mMeshVertices[0].x;
3286 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003287
3288 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3289 texture->uvMapper->map(uvs);
3290
3291 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3292 }
3293
Chris Craikd6b65f62014-01-01 14:45:21 -08003294 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3295 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3296 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003297
Romain Guyd21b6e12011-11-30 20:21:23 -08003298 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003299 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003300 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003301 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003302 } else {
Chris Craik678625242014-02-28 12:26:34 -08003303 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003304 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003305 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3306 }
3307
3308 if (texture->uvMapper) {
3309 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003310 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003311}
3312
Romain Guyf7f93552010-07-08 19:17:03 -07003313void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003314 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003315 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003316 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3317 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003318
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003319 int a;
3320 SkXfermode::Mode mode;
3321 getAlphaAndMode(paint, &a, &mode);
3322 const float alpha = a / 255.0f;
3323
Romain Guy746b7402010-10-26 16:27:31 -07003324 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003325 setupDrawWithTexture();
3326 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003327 setupDrawColorFilter(getColorFilter(paint));
3328 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003329 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003330 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003331 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003332 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003333 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003334 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003335 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003336
Romain Guy6820ac82010-09-15 18:11:50 -07003337 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003338}
3339
Romain Guy3b748a42013-04-17 18:54:38 -07003340void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003341 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003342 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003343 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3344 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003345
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003346 int a;
3347 SkXfermode::Mode mode;
3348 getAlphaAndMode(paint, &a, &mode);
3349 const float alpha = a / 255.0f;
3350
Romain Guy3b748a42013-04-17 18:54:38 -07003351 setupDraw();
3352 setupDrawWithTexture();
3353 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003354 setupDrawColorFilter(getColorFilter(paint));
3355 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003356 setupDrawProgram();
3357 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003358 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003359 setupDrawTexture(texture);
3360 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003361 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003362 setupDrawMeshIndices(vertices, texCoords, vbo);
3363
3364 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003365}
3366
Romain Guy886b2752013-01-04 12:26:18 -08003367void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003368 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003369 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003370 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003371
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003372 int color = paint != NULL ? paint->getColor() : 0;
3373 int alpha;
3374 SkXfermode::Mode mode;
3375 getAlphaAndMode(paint, &alpha, &mode);
3376
Romain Guy886b2752013-01-04 12:26:18 -08003377 setupDraw();
3378 setupDrawWithTexture(true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003379 if (paint != NULL) {
Romain Guy886b2752013-01-04 12:26:18 -08003380 setupDrawAlpha8Color(color, alpha);
3381 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003382 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003383 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003384 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003385 setupDrawProgram();
3386 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003387 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003388 setupDrawTexture(texture);
3389 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003390 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003391 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003392 setupDrawMesh(vertices, texCoords);
3393
3394 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003395}
3396
Romain Guya5aed0d2010-09-09 14:42:43 -07003397void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003398 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003399
3400 if (mSnapshot->roundRectClipState != NULL /*&& !mSkipOutlineClip*/) {
3401 blend = true;
3402 mDescription.hasRoundRectClip = true;
3403 }
3404 mSkipOutlineClip = true;
3405
Romain Guy78dd96d2013-05-03 14:24:16 -07003406 if (mCountOverdraw) {
3407 if (!mCaches.blend) glEnable(GL_BLEND);
3408 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3409 glBlendFunc(GL_ONE, GL_ONE);
3410 }
3411
3412 mCaches.blend = true;
3413 mCaches.lastSrcMode = GL_ONE;
3414 mCaches.lastDstMode = GL_ONE;
3415
3416 return;
3417 }
3418
Romain Guy82ba8142010-07-09 13:25:56 -07003419 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003420
Romain Guy82ba8142010-07-09 13:25:56 -07003421 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003422 // These blend modes are not supported by OpenGL directly and have
3423 // to be implemented using shaders. Since the shader will perform
3424 // the blending, turn blending off here
3425 // If the blend mode cannot be implemented using shaders, fall
3426 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003427 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003428 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003429 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003430 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003431
Romain Guy82bc7a72012-01-03 14:13:39 -08003432 if (mCaches.blend) {
3433 glDisable(GL_BLEND);
3434 mCaches.blend = false;
3435 }
3436
3437 return;
3438 } else {
3439 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003440 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003441 }
3442
3443 if (!mCaches.blend) {
3444 glEnable(GL_BLEND);
3445 }
3446
3447 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3448 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3449
3450 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3451 glBlendFunc(sourceMode, destMode);
3452 mCaches.lastSrcMode = sourceMode;
3453 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003454 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003455 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003456 glDisable(GL_BLEND);
3457 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003458 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003459}
3460
Romain Guy889f8d12010-07-29 14:37:42 -07003461bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003462 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003463 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003464 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003465 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003466 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003467 }
Romain Guy6926c722010-07-12 20:20:03 -07003468 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003469}
3470
Romain Guy026c5e162010-06-28 17:12:22 -07003471void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003472 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003473 TextureVertex::setUV(v++, u1, v1);
3474 TextureVertex::setUV(v++, u2, v1);
3475 TextureVertex::setUV(v++, u1, v2);
3476 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003477}
3478
Chris Craikd218a922014-01-02 17:13:34 -08003479void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003480 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003481 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3482 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003483 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003484 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003485 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003486}
3487
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003488float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003489 float alpha;
3490 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3491 alpha = mDrawModifiers.mOverrideLayerAlpha;
3492 } else {
3493 alpha = layer->getAlpha() / 255.0f;
3494 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003495 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003496}
3497
Romain Guy9d5316e2010-06-24 19:30:36 -07003498}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003499}; // namespace android