blob: d570b0d5bc9cedbd5cb27bda8e8fd146e2731a74 [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
Chris Craik62d307c2014-07-29 10:35:13 -070048#if DEBUG_DETAILED_EVENTS
49 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
50#else
51 #define EVENT_LOGD(...)
52#endif
53
Chris Craika8bea8e2014-09-24 11:29:43 -070054static void atraceFormatBegin(const char* fmt, ...) {
55 const int BUFFER_SIZE = 256;
56 va_list ap;
57 char buf[BUFFER_SIZE];
58
59 va_start(ap, fmt);
60 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
61 va_end(ap);
62
63 ATRACE_BEGIN(buf);
64}
65
66#define ATRACE_FORMAT_BEGIN(fmt, ...) \
67 if (CC_UNLIKELY(ATRACE_ENABLED())) atraceFormatBegin(fmt, ##__VA_ARGS__)
68
Romain Guye4d01122010-06-16 18:44:05 -070069namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070070namespace uirenderer {
71
Chris Craik678625242014-02-28 12:26:34 -080072static GLenum getFilter(const SkPaint* paint) {
73 if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) {
74 return GL_LINEAR;
75 }
76 return GL_NEAREST;
77}
Romain Guyd21b6e12011-11-30 20:21:23 -080078
Romain Guy9d5316e2010-06-24 19:30:36 -070079///////////////////////////////////////////////////////////////////////////////
80// Globals
81///////////////////////////////////////////////////////////////////////////////
82
Romain Guy889f8d12010-07-29 14:37:42 -070083/**
84 * Structure mapping Skia xfermodes to OpenGL blending factors.
85 */
86struct Blender {
87 SkXfermode::Mode mode;
88 GLenum src;
89 GLenum dst;
90}; // struct Blender
91
Romain Guy026c5e162010-06-28 17:12:22 -070092// In this array, the index of each Blender equals the value of the first
93// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
94static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070095 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
96 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
97 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
98 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
100 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
101 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
102 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
103 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
104 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
105 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
106 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
107 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500108 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -0700109 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -0700110};
Romain Guye4d01122010-06-16 18:44:05 -0700111
Romain Guy87a76572010-09-13 18:11:21 -0700112// This array contains the swapped version of each SkXfermode. For instance
113// this array's SrcOver blending mode is actually DstOver. You can refer to
114// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -0700115static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -0700116 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
117 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
118 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
119 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
120 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
121 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
122 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
123 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
124 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
125 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
126 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
127 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
128 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500129 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700130 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700131};
132
Romain Guyf6a11b82010-06-23 17:47:49 -0700133///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700134// Functions
135///////////////////////////////////////////////////////////////////////////////
136
137template<typename T>
138static inline T min(T a, T b) {
139 return a < b ? a : b;
140}
141
142///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700143// Constructors/destructor
144///////////////////////////////////////////////////////////////////////////////
145
John Reck3b202512014-06-23 13:13:08 -0700146OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Chris Craik058fc642014-07-23 18:19:28 -0700147 : mFrameStarted(false)
148 , mCaches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -0700149 , mExtensions(Extensions::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -0700150 , mRenderState(renderState)
151 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -0700152 , mSuppressTiling(false)
153 , mFirstFrameAfterResize(true)
John Reck1aa5d2d2014-07-24 13:38:28 -0700154 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -0700155 , mLightRadius(FLT_MIN)
156 , mAmbientShadowAlpha(0)
157 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800158 // *set* draw modifiers to be 0
159 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700160 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700161
Romain Guyac670c02010-07-27 17:39:27 -0700162 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700163}
164
Romain Guy85bf02f2010-06-22 13:11:24 -0700165OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700166 // The context has already been destroyed at this point, do not call
167 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700168}
169
Romain Guy87e2f7572012-09-24 11:37:12 -0700170void OpenGLRenderer::initProperties() {
171 char property[PROPERTY_VALUE_MAX];
172 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
173 mScissorOptimizationDisabled = !strcasecmp(property, "true");
174 INIT_LOGD(" Scissor optimization %s",
175 mScissorOptimizationDisabled ? "disabled" : "enabled");
176 } else {
177 INIT_LOGD(" Scissor optimization enabled");
178 }
Romain Guy13631f32012-01-30 17:41:55 -0800179}
180
Chris Craik058fc642014-07-23 18:19:28 -0700181void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
182 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
183 mLightCenter = lightCenter;
184 mLightRadius = lightRadius;
185 mAmbientShadowAlpha = ambientShadowAlpha;
186 mSpotShadowAlpha = spotShadowAlpha;
187}
188
Romain Guy13631f32012-01-30 17:41:55 -0800189///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700190// Setup
191///////////////////////////////////////////////////////////////////////////////
192
Chris Craik797b95b2014-05-20 18:10:25 -0700193void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700194 glDisable(GL_DITHER);
195 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
196
197 glEnableVertexAttribArray(Program::kBindingPosition);
Chris Craik284b2432014-09-18 16:05:35 -0700198 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700199}
200
Romain Guy96885eb2013-03-26 15:05:58 -0700201void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800202 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800203 mCaches.clearGarbage();
Chris Craik69e5adf2014-08-14 13:34:01 -0700204 initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700205 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700206 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700207}
208
209status_t OpenGLRenderer::startFrame() {
210 if (mFrameStarted) return DrawGlInfo::kStatusDone;
211 mFrameStarted = true;
212
Romain Guy41308e22012-10-22 20:02:43 -0700213 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700214
Romain Guy96885eb2013-03-26 15:05:58 -0700215 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700216
John Reck3b202512014-06-23 13:13:08 -0700217 mRenderState.setViewport(getWidth(), getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800218
Romain Guy54c1a642012-09-27 17:55:46 -0700219 // Functors break the tiling extension in pretty spectacular ways
220 // This ensures we don't use tiling when a functor is going to be
221 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700222 mSuppressTiling = mCaches.hasRegisteredFunctors()
223 || mFirstFrameAfterResize;
224 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700225
Chris Craikd6b65f62014-01-01 14:45:21 -0800226 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700227
Romain Guy7c450aa2012-09-21 19:15:00 -0700228 debugOverdraw(true, true);
229
Romain Guy96885eb2013-03-26 15:05:58 -0700230 return clear(mTilingClip.left, mTilingClip.top,
231 mTilingClip.right, mTilingClip.bottom, mOpaque);
232}
233
Romain Guy96885eb2013-03-26 15:05:58 -0700234status_t OpenGLRenderer::prepareDirty(float left, float top,
235 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700236
Romain Guy96885eb2013-03-26 15:05:58 -0700237 setupFrameState(left, top, right, bottom, opaque);
238
239 // Layer renderers will start the frame immediately
240 // The framebuffer renderer will first defer the display list
241 // for each layer and wait until the first drawing command
242 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800243 if (currentSnapshot()->fbo == 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700244 syncState();
245 updateLayers();
246 } else {
247 return startFrame();
248 }
249
250 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700251}
252
Romain Guydcfc8362013-01-03 13:08:57 -0800253void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
254 // If we know that we are going to redraw the entire framebuffer,
255 // perform a discard to let the driver know we don't need to preserve
256 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800257 if (mExtensions.hasDiscardFramebuffer() &&
Chris Craik14e51302013-12-30 15:32:54 -0800258 left <= 0.0f && top <= 0.0f && right >= getWidth() && bottom >= getHeight()) {
Romain Guyf1581982013-01-31 17:20:30 -0800259 const bool isFbo = getTargetFbo() == 0;
260 const GLenum attachments[] = {
261 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
262 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800263 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
264 }
265}
266
Romain Guy7c25aab2012-10-18 15:05:02 -0700267status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700268 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700269 mCaches.enableScissor();
Chris Craika64a2be2014-05-14 14:17:01 -0700270 mCaches.setScissor(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700271 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700272 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700273 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700274
Romain Guy7c25aab2012-10-18 15:05:02 -0700275 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700276 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700277}
278
279void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700280 if (mCaches.blend) {
281 glEnable(GL_BLEND);
282 } else {
283 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700284 }
Romain Guybb9524b2010-06-22 18:56:38 -0700285}
286
henry.uh_chen33f5a592014-07-02 19:36:56 +0800287void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700288 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800289 const Snapshot* snapshot = currentSnapshot();
290
Chris Craik14e51302013-12-30 15:32:54 -0800291 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800292 if (snapshot->flags & Snapshot::kFlagFboTarget) {
293 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700294 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700295
henry.uh_chen33f5a592014-07-02 19:36:56 +0800296 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800297 }
298}
299
henry.uh_chen33f5a592014-07-02 19:36:56 +0800300void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800301 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800302 if(expand) {
303 // Expand the startTiling region by 1
304 int leftNotZero = (clip.left > 0) ? 1 : 0;
305 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
306
307 mCaches.startTiling(
308 clip.left - leftNotZero,
309 windowHeight - clip.bottom - topNotZero,
310 clip.right - clip.left + leftNotZero + 1,
311 clip.bottom - clip.top + topNotZero + 1,
312 opaque);
313 } else {
314 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800315 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800316 }
Romain Guy54c1a642012-09-27 17:55:46 -0700317 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700318}
319
320void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700321 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700322}
323
Romain Guyb025b9c2010-09-16 14:16:48 -0700324void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700325 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700326 endTiling();
327
Romain Guyca89e2a2013-03-08 17:44:20 -0800328 // When finish() is invoked on FBO 0 we've reached the end
329 // of the current frame
330 if (getTargetFbo() == 0) {
331 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700332 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800333 }
334
Romain Guy11cb6422012-09-21 00:39:43 -0700335 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700336#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700337 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700338#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700339
Romain Guyc15008e2010-11-10 11:59:15 -0800340#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800341 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700342#else
343 if (mCaches.getDebugLevel() & kDebugMemory) {
344 mCaches.dumpMemoryUsage();
345 }
Romain Guyc15008e2010-11-10 11:59:15 -0800346#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700347 }
Romain Guy96885eb2013-03-26 15:05:58 -0700348
349 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700350}
351
Romain Guy35643dd2012-09-18 15:40:58 -0700352void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700353 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
354 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700355 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700356
357 mCaches.resetScissor();
358 dirtyClip();
359}
360
Romain Guy8f3b8e32012-03-27 16:33:45 -0700361status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800362 if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
Chris Craik408eb122013-03-26 18:55:15 -0700363
Chris Craikd6b65f62014-01-01 14:45:21 -0800364 Rect clip(*currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700365 clip.snapToPixelBoundaries();
366
Romain Guyd643bb52011-03-01 14:55:21 -0800367 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800368 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800369 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800370 dirtyLayerUnchecked(clip, getRegion());
371 }
Romain Guyd643bb52011-03-01 14:55:21 -0800372
Romain Guy08aa2cb2011-03-17 11:06:57 -0700373 DrawGlInfo info;
374 info.clipLeft = clip.left;
375 info.clipTop = clip.top;
376 info.clipRight = clip.right;
377 info.clipBottom = clip.bottom;
378 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700379 info.width = getViewportWidth();
380 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800381 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700382
John Reck3b202512014-06-23 13:13:08 -0700383 bool prevDirtyClip = mDirtyClip;
Chris Craik54f574a2013-08-26 11:23:46 -0700384 // setup GL state for functor
385 if (mDirtyClip) {
Chris Craik54f574a2013-08-26 11:23:46 -0700386 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
387 }
John Reck3b202512014-06-23 13:13:08 -0700388 if (mCaches.enableScissor() || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700389 setScissorFromClip();
390 }
Chris Craik54f574a2013-08-26 11:23:46 -0700391
John Reck3b202512014-06-23 13:13:08 -0700392 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
393 // Scissor may have been modified, reset dirty clip
394 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800395
John Reck750ca6d2014-03-28 16:33:18 -0700396 return DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800397}
398
Romain Guyf6a11b82010-06-23 17:47:49 -0700399///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700400// Debug
401///////////////////////////////////////////////////////////////////////////////
402
Chris Craik62d307c2014-07-29 10:35:13 -0700403void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
404#if DEBUG_DETAILED_EVENTS
405 const int BUFFER_SIZE = 256;
406 va_list ap;
407 char buf[BUFFER_SIZE];
408
409 va_start(ap, fmt);
410 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
411 va_end(ap);
412
413 eventMark(buf);
414#endif
415}
416
417
Romain Guy0f6675332013-03-01 14:31:04 -0800418void OpenGLRenderer::eventMark(const char* name) const {
419 mCaches.eventMark(0, name);
420}
421
Romain Guy87e2f7572012-09-24 11:37:12 -0700422void OpenGLRenderer::startMark(const char* name) const {
423 mCaches.startMark(0, name);
424}
425
426void OpenGLRenderer::endMark() const {
427 mCaches.endMark();
428}
429
430void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700431 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700432}
433
434void OpenGLRenderer::renderOverdraw() {
435 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700436 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700437
438 mCaches.enableScissor();
Chris Craika64a2be2014-05-14 14:17:01 -0700439 mCaches.setScissor(clip->left, firstSnapshot()->getViewportHeight() - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700440 clip->right - clip->left, clip->bottom - clip->top);
441
Romain Guy627c6fd2013-08-21 11:53:18 -0700442 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700443 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700444 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
445
446 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700447 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700448 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
449
450 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700451 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700452 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
453
454 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700455 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700456 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
457
Romain Guy87e2f7572012-09-24 11:37:12 -0700458 mCaches.stencil.disable();
459 }
460}
461
462///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700463// Layers
464///////////////////////////////////////////////////////////////////////////////
465
466bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700467 if (layer->deferredUpdateScheduled && layer->renderer
468 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700469 ATRACE_CALL();
470
Romain Guy11cb6422012-09-21 00:39:43 -0700471 Rect& dirty = layer->dirtyRect;
472
Romain Guy7c450aa2012-09-21 19:15:00 -0700473 if (inFrame) {
474 endTiling();
475 debugOverdraw(false, false);
476 }
Romain Guy11cb6422012-09-21 00:39:43 -0700477
Romain Guy96885eb2013-03-26 15:05:58 -0700478 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700479 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700480 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700481 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700482 }
Romain Guy11cb6422012-09-21 00:39:43 -0700483
484 if (inFrame) {
485 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800486 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700487 }
488
Romain Guy5bb3c732012-11-29 17:52:58 -0800489 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700490 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700491
492 return true;
493 }
494
495 return false;
496}
497
498void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700499 // If draw deferring is enabled this method will simply defer
500 // the display list of each individual layer. The layers remain
501 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700502 int count = mLayerUpdates.size();
503 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700504 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
505 startMark("Layer Updates");
506 } else {
507 startMark("Defer Layer Updates");
508 }
Romain Guy11cb6422012-09-21 00:39:43 -0700509
Chris Craik1206b9b2013-04-04 14:46:24 -0700510 // Note: it is very important to update the layers in order
511 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700512 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700513 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700514 }
Romain Guy11cb6422012-09-21 00:39:43 -0700515
Romain Guy96885eb2013-03-26 15:05:58 -0700516 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
517 mLayerUpdates.clear();
John Reck3b202512014-06-23 13:13:08 -0700518 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700519 }
520 endMark();
521 }
522}
523
524void OpenGLRenderer::flushLayers() {
525 int count = mLayerUpdates.size();
526 if (count > 0) {
527 startMark("Apply Layer Updates");
528 char layerName[12];
529
Chris Craik1206b9b2013-04-04 14:46:24 -0700530 // Note: it is very important to update the layers in order
531 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700532 Layer* layer = mLayerUpdates.itemAt(i).get();
Chris Craika8bea8e2014-09-24 11:29:43 -0700533
Romain Guy96885eb2013-03-26 15:05:58 -0700534 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700535 startMark(layerName);
Chris Craika8bea8e2014-09-24 11:29:43 -0700536 ATRACE_FORMAT_BEGIN("flushLayer %ux%u", layer->getWidth(), layer->getHeight());
Romain Guy02b49b72013-03-29 12:37:16 -0700537
Romain Guy02b49b72013-03-29 12:37:16 -0700538 layer->flush();
Chris Craika8bea8e2014-09-24 11:29:43 -0700539
Romain Guy40543602013-06-12 15:31:28 -0700540 ATRACE_END();
Chris Craika8bea8e2014-09-24 11:29:43 -0700541 endMark();
Romain Guy96885eb2013-03-26 15:05:58 -0700542 }
543
544 mLayerUpdates.clear();
John Reck3b202512014-06-23 13:13:08 -0700545 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700546
Romain Guy11cb6422012-09-21 00:39:43 -0700547 endMark();
548 }
549}
550
551void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
552 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700553 // Make sure we don't introduce duplicates.
554 // SortedVector would do this automatically but we need to respect
555 // the insertion order. The linear search is not an issue since
556 // this list is usually very short (typically one item, at most a few)
557 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
558 if (mLayerUpdates.itemAt(i) == layer) {
559 return;
560 }
561 }
Romain Guy11cb6422012-09-21 00:39:43 -0700562 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700563 }
564}
565
Romain Guye93482f2013-06-17 13:14:51 -0700566void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
567 if (layer) {
568 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
569 if (mLayerUpdates.itemAt(i) == layer) {
570 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700571 break;
572 }
573 }
574 }
575}
576
Romain Guy40543602013-06-12 15:31:28 -0700577void OpenGLRenderer::flushLayerUpdates() {
John Recka7c2ea22014-08-08 13:21:00 -0700578 ATRACE_CALL();
Romain Guy40543602013-06-12 15:31:28 -0700579 syncState();
580 updateLayers();
581 flushLayers();
582 // Wait for all the layer updates to be executed
583 AutoFence fence;
584}
585
John Reck443a7142014-09-04 17:40:05 -0700586void OpenGLRenderer::markLayersAsBuildLayers() {
587 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
588 mLayerUpdates[i]->wasBuildLayered = true;
589 }
590}
591
Romain Guy11cb6422012-09-21 00:39:43 -0700592///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700593// State management
594///////////////////////////////////////////////////////////////////////////////
595
Chris Craik14e51302013-12-30 15:32:54 -0800596void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700597 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800598 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
599 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700600
Chris Craika64a2be2014-05-14 14:17:01 -0700601 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700602 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700603 }
604
Romain Guy2542d192010-08-18 11:47:12 -0700605 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700606 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700607 }
Romain Guy2542d192010-08-18 11:47:12 -0700608
Romain Guy5ec99242010-11-03 16:19:08 -0700609 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700610 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700611 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700612 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800613 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700614 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700615 }
Romain Guybb9524b2010-06-22 18:56:38 -0700616}
617
Romain Guyf6a11b82010-06-23 17:47:49 -0700618///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700619// Layers
620///////////////////////////////////////////////////////////////////////////////
621
622int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700623 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700624 // force matrix/clip isolation for layer
625 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
626
Romain Guyeb993562010-10-05 18:14:38 -0700627 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700628
Chris Craikd6b65f62014-01-01 14:45:21 -0800629 if (!currentSnapshot()->isIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700630 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700631 }
Romain Guyd55a8612010-06-28 17:42:46 -0700632
633 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700634}
635
Chris Craikd90144d2013-03-19 15:03:48 -0700636void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
637 const Rect untransformedBounds(bounds);
638
Chris Craikd6b65f62014-01-01 14:45:21 -0800639 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700640
641 // Layers only make sense if they are in the framebuffer's bounds
Chris Craikd6b65f62014-01-01 14:45:21 -0800642 if (bounds.intersect(*currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700643 // We cannot work with sub-pixels in this case
644 bounds.snapToPixelBoundaries();
645
646 // When the layer is not an FBO, we may use glCopyTexImage so we
647 // need to make sure the layer does not extend outside the bounds
648 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700649 const Snapshot& previous = *(currentSnapshot()->previous);
650 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
651 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700652 bounds.setEmpty();
653 } else if (fboLayer) {
654 clip.set(bounds);
655 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800656 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700657 inverse.mapRect(clip);
658 clip.snapToPixelBoundaries();
659 if (clip.intersect(untransformedBounds)) {
660 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
661 bounds.set(untransformedBounds);
662 } else {
663 clip.setEmpty();
664 }
665 }
666 } else {
667 bounds.setEmpty();
668 }
669}
670
Chris Craik408eb122013-03-26 18:55:15 -0700671void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
672 bool fboLayer, int alpha) {
673 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
674 bounds.getHeight() > mCaches.maxTextureSize ||
675 (fboLayer && clip.isEmpty())) {
676 mSnapshot->empty = fboLayer;
677 } else {
Chris Craik74cf7e62014-08-07 14:34:46 -0700678 mSnapshot->invisible = mSnapshot->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700679 }
680}
681
Chris Craikd90144d2013-03-19 15:03:48 -0700682int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500683 const SkPaint* paint, int flags) {
Chris Craikd90144d2013-03-19 15:03:48 -0700684 const int count = saveSnapshot(flags);
685
Chris Craikd6b65f62014-01-01 14:45:21 -0800686 if (!currentSnapshot()->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700687 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
688 // operations will be able to store and restore the current clip and transform info, and
689 // quick rejection will be correct (for display lists)
690
691 Rect bounds(left, top, right, bottom);
692 Rect clip;
693 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500694 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700695
Chris Craikd6b65f62014-01-01 14:45:21 -0800696 if (!currentSnapshot()->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700697 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
698 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craika64a2be2014-05-14 14:17:01 -0700699 mSnapshot->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craikdeeda3d2014-05-05 19:09:33 -0700700 mSnapshot->roundRectClipState = NULL;
Chris Craikd90144d2013-03-19 15:03:48 -0700701 }
702 }
703
704 return count;
705}
706
Romain Guy1c740bc2010-09-13 18:00:09 -0700707/**
708 * Layers are viewed by Skia are slightly different than layers in image editing
709 * programs (for instance.) When a layer is created, previously created layers
710 * and the frame buffer still receive every drawing command. For instance, if a
711 * layer is created and a shape intersecting the bounds of the layers and the
712 * framebuffer is draw, the shape will be drawn on both (unless the layer was
713 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
714 *
715 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
716 * texture. Unfortunately, this is inefficient as it requires every primitive to
717 * be drawn n + 1 times, where n is the number of active layers. In practice this
718 * means, for every primitive:
719 * - Switch active frame buffer
720 * - Change viewport, clip and projection matrix
721 * - Issue the drawing
722 *
723 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700724 * To avoid this, layers are implemented in a different way here, at least in the
725 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
726 * is set. When this flag is set we can redirect all drawing operations into a
727 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700728 *
729 * This implementation relies on the frame buffer being at least RGBA 8888. When
730 * a layer is created, only a texture is created, not an FBO. The content of the
731 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700732 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700733 * buffer and drawing continues as normal. This technique therefore treats the
734 * frame buffer as a scratch buffer for the layers.
735 *
736 * To compose the layers back onto the frame buffer, each layer texture
737 * (containing the original frame buffer data) is drawn as a simple quad over
738 * the frame buffer. The trick is that the quad is set as the composition
739 * destination in the blending equation, and the frame buffer becomes the source
740 * of the composition.
741 *
742 * Drawing layers with an alpha value requires an extra step before composition.
743 * An empty quad is drawn over the layer's region in the frame buffer. This quad
744 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
745 * quad is used to multiply the colors in the frame buffer. This is achieved by
746 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
747 * GL_ZERO, GL_SRC_ALPHA.
748 *
749 * Because glCopyTexImage2D() can be slow, an alternative implementation might
750 * be use to draw a single clipped layer. The implementation described above
751 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700752 *
753 * (1) The frame buffer is actually not cleared right away. To allow the GPU
754 * to potentially optimize series of calls to glCopyTexImage2D, the frame
755 * buffer is left untouched until the first drawing operation. Only when
756 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700757 */
Chet Haased48885a2012-08-28 17:43:28 -0700758bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700759 const SkPaint* paint, int flags, const SkPath* convexMask) {
Romain Guyeb993562010-10-05 18:14:38 -0700760 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700761 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700762
Romain Guyeb993562010-10-05 18:14:38 -0700763 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
764
Romain Guyf607bdc2010-09-10 19:20:06 -0700765 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700766 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700767 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700768 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000769 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700770
771 // Bail out if we won't draw in this snapshot
Chris Craikd6b65f62014-01-01 14:45:21 -0800772 if (currentSnapshot()->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700773 return false;
774 }
Romain Guyf18fd992010-07-08 11:45:51 -0700775
Romain Guya1d3c912011-12-13 14:55:06 -0800776 mCaches.activeTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700777 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700778 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700779 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700780 }
781
Derek Sollenberger674554f2014-02-19 16:47:32 +0000782 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700783 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700784 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
785 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500786
Chet Haasea23eed82012-04-12 15:19:04 -0700787 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700788 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700789 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700790
Romain Guy8fb95422010-08-17 18:38:51 -0700791 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700792 mSnapshot->flags |= Snapshot::kFlagIsLayer;
793 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700794
Chris Craika8bea8e2014-09-24 11:29:43 -0700795 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
796 fboLayer ? "" : "unclipped ",
797 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700798 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700799 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700800 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700801 } else {
802 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700803 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800804 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700805 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700806 // Workaround for some GL drivers. When reading pixels lying outside
807 // of the window we should get undefined values for those pixels.
808 // Unfortunately some drivers will turn the entire target texture black
809 // when reading outside of the window.
810 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
811 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700812 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800813 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700814
Chris Craika64a2be2014-05-14 14:17:01 -0700815 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
816 bounds.left, getViewportHeight() - bounds.bottom,
817 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700818
Romain Guy54be1cd2011-06-13 19:04:27 -0700819 // Enqueue the buffer coordinates to clear the corresponding region later
820 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700821 }
Romain Guyeb993562010-10-05 18:14:38 -0700822 }
Romain Guyf86ef572010-07-01 11:05:42 -0700823
Romain Guyd55a8612010-06-28 17:42:46 -0700824 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700825}
826
Chris Craike63f7c622013-10-17 10:30:55 -0700827bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800828 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700829 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700830
Chet Haased48885a2012-08-28 17:43:28 -0700831 mSnapshot->region = &mSnapshot->layer->region;
Chris Craika64a2be2014-05-14 14:17:01 -0700832 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
Chet Haased48885a2012-08-28 17:43:28 -0700833 mSnapshot->fbo = layer->getFbo();
834 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
835 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craika64a2be2014-05-14 14:17:01 -0700836 mSnapshot->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craikdeeda3d2014-05-05 19:09:33 -0700837 mSnapshot->roundRectClipState = NULL;
Romain Guy5b3b3522010-10-27 18:57:51 -0700838
Romain Guy2b7028e2012-09-19 17:25:38 -0700839 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700840 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700841 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700842 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700843 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700844
845 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700846 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700847 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700848 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700849 }
850
851 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700852 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700853
henry.uh_chen33f5a592014-07-02 19:36:56 +0800854 // Expand the startTiling region by 1
855 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700856
857 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700858 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800859 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700860 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700861 glClear(GL_COLOR_BUFFER_BIT);
862
863 dirtyClip();
864
865 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700866 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700867 return true;
868}
869
Romain Guy1c740bc2010-09-13 18:00:09 -0700870/**
871 * Read the documentation of createLayer() before doing anything in this method.
872 */
Chris Craik14e51302013-12-30 15:32:54 -0800873void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
874 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000875 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700876 return;
877 }
878
Chris Craik14e51302013-12-30 15:32:54 -0800879 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800880 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800881 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700882
Chris Craik39a908c2013-06-13 14:39:01 -0700883 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -0800884 calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700885 &clipRequired, NULL, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -0700886 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
887
Romain Guyeb993562010-10-05 18:14:38 -0700888 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700889 endTiling();
890
Romain Guye0aa84b2012-04-03 19:30:26 -0700891 // Detach the texture from the FBO
892 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800893
894 layer->removeFbo(false);
895
Romain Guyeb993562010-10-05 18:14:38 -0700896 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700897 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700898 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700899
Chris Craikd6b65f62014-01-01 14:45:21 -0800900 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700901 }
902
Romain Guy9ace8f52011-07-07 20:50:11 -0700903 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500904 SkPaint layerPaint;
905 layerPaint.setAlpha(layer->getAlpha());
906 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
907 layerPaint.setColorFilter(layer->getColorFilter());
908
909 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700910 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700911 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700912 }
913
Romain Guy03750a02010-10-18 14:06:08 -0700914 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700915
Romain Guya1d3c912011-12-13 14:55:06 -0800916 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700917
Romain Guy5b3b3522010-10-27 18:57:51 -0700918 // When the layer is stored in an FBO, we can save a bit of fillrate by
919 // drawing only the dirty region
920 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800921 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700922 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700923 } else if (!rect.isEmpty()) {
924 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530925
926 save(0);
927 // the layer contains screen buffer content that shouldn't be alpha modulated
928 // (and any necessary alpha modulation was handled drawing into the layer)
929 mSnapshot->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700930 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530931 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700932 }
Romain Guy8b55f372010-08-18 17:10:07 -0700933
Romain Guy746b7402010-10-26 16:27:31 -0700934 dirtyClip();
935
Romain Guyeb993562010-10-05 18:14:38 -0700936 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craik3f0854292014-04-15 16:18:08 -0700937 layer->setConvexMask(NULL);
Romain Guy8550c4c2010-10-08 15:49:53 -0700938 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700939 LAYER_LOGD("Deleting layer");
John Reck0e89e2b2014-10-31 14:49:06 -0700940 layer->decStrong(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700941 }
942}
943
Romain Guyaa6c24c2011-04-28 18:40:04 -0700944void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700945 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700946
947 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700948 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700949 setupDrawWithTexture();
950 } else {
951 setupDrawWithExternalTexture();
952 }
953 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700954 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500955 setupDrawColorFilter(layer->getColorFilter());
956 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700957 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700958 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500959 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700960 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
961 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700962 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700963 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700964 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800965 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800966 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700967 layer->getWidth() == (uint32_t) rect.getWidth() &&
968 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800969 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
970 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700971
Romain Guyd21b6e12011-11-30 20:21:23 -0800972 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800973 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
974 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700975 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800976 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800977 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
978 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700979 }
980 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700981 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700982
983 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700984}
985
Romain Guy5b3b3522010-10-27 18:57:51 -0700986void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700987 if (layer->isTextureLayer()) {
988 EVENT_LOGD("composeTextureLayerRect");
989 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
990 drawTextureLayer(layer, rect);
991 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
992 } else {
993 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700994 const Rect& texCoords = layer->texCoords;
995 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
996 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700997
Romain Guy9ace8f52011-07-07 20:50:11 -0700998 float x = rect.left;
999 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -08001000 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001001 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001002 layer->getHeight() == (uint32_t) rect.getHeight();
1003
1004 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001005 // When we're swapping, the layer is already in screen coordinates
1006 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001007 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1008 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001009 }
1010
Romain Guyd21b6e12011-11-30 20:21:23 -08001011 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001012 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001013 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001014 }
1015
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001016 SkPaint layerPaint;
1017 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
1018 layerPaint.setXfermodeMode(layer->getMode());
1019 layerPaint.setColorFilter(layer->getColorFilter());
1020
1021 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001022 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001023 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001024 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001025 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001026
Romain Guyaa6c24c2011-04-28 18:40:04 -07001027 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001028 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001029}
1030
Chris Craik34416ea2013-04-15 16:08:28 -07001031/**
1032 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1033 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1034 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1035 * by saveLayer's restore
1036 */
1037#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1038 DRAW_COMMAND; \
1039 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1040 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1041 DRAW_COMMAND; \
1042 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1043 } \
1044 }
1045
1046#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1047
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001048// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
1049// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
1050class LayerShader : public SkShader {
1051public:
1052 LayerShader(Layer* layer, const SkMatrix* localMatrix)
1053 : INHERITED(localMatrix)
1054 , mLayer(layer) {
1055 }
1056
1057 virtual bool asACustomShader(void** data) const {
1058 if (data) {
1059 *data = static_cast<void*>(mLayer);
1060 }
1061 return true;
1062 }
1063
1064 virtual bool isOpaque() const {
1065 return !mLayer->isBlend();
1066 }
1067
1068protected:
1069 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
1070 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
1071 }
1072
1073 virtual void flatten(SkWriteBuffer&) const {
1074 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1075 }
1076
1077 virtual Factory getFactory() const {
1078 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
1079 return NULL;
1080 }
1081private:
1082 // Unowned.
1083 Layer* mLayer;
1084 typedef SkShader INHERITED;
1085};
1086
Romain Guy5b3b3522010-10-27 18:57:51 -07001087void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001088 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1089
1090 if (layer->getConvexMask()) {
1091 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1092
1093 // clip to the area of the layer the mask can be larger
1094 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1095
1096 SkPaint paint;
1097 paint.setAntiAlias(true);
1098 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1099
Chris Craik3f0854292014-04-15 16:18:08 -07001100 // create LayerShader to map SaveLayer content into subsequent draw
1101 SkMatrix shaderMatrix;
1102 shaderMatrix.setTranslate(rect.left, rect.bottom);
1103 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001104 LayerShader layerShader(layer, &shaderMatrix);
1105 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001106
1107 // Since the drawing primitive is defined in local drawing space,
1108 // we don't need to modify the draw matrix
1109 const SkPath* maskPath = layer->getConvexMask();
1110 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1111
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001112 paint.setShader(NULL);
Chris Craik3f0854292014-04-15 16:18:08 -07001113 restore();
1114
1115 return;
1116 }
1117
Romain Guy5b3b3522010-10-27 18:57:51 -07001118 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001119 layer->setRegionAsRect();
1120
Chris Craik34416ea2013-04-15 16:08:28 -07001121 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001122
Romain Guy5b3b3522010-10-27 18:57:51 -07001123 layer->region.clear();
1124 return;
1125 }
1126
Chris Craik62d307c2014-07-29 10:35:13 -07001127 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001128 // standard Region based draw
1129 size_t count;
1130 const android::Rect* rects;
1131 Region safeRegion;
1132 if (CC_LIKELY(hasRectToRectTransform())) {
1133 rects = layer->region.getArray(&count);
1134 } else {
1135 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1136 rects = safeRegion.getArray(&count);
1137 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001138
Chris Craik3f0854292014-04-15 16:18:08 -07001139 const float alpha = getLayerAlpha(layer);
1140 const float texX = 1.0f / float(layer->getWidth());
1141 const float texY = 1.0f / float(layer->getHeight());
1142 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001143
Chris Craik3f0854292014-04-15 16:18:08 -07001144 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001145
Chris Craik3f0854292014-04-15 16:18:08 -07001146 // We must get (and therefore bind) the region mesh buffer
1147 // after we setup drawing in case we need to mess with the
1148 // stencil buffer in setupDraw()
1149 TextureVertex* mesh = mCaches.getRegionMesh();
1150 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001151
Chris Craik3f0854292014-04-15 16:18:08 -07001152 setupDrawWithTexture();
1153 setupDrawColor(alpha, alpha, alpha, alpha);
1154 setupDrawColorFilter(layer->getColorFilter());
1155 setupDrawBlending(layer);
1156 setupDrawProgram();
1157 setupDrawDirtyRegionsDisabled();
1158 setupDrawPureColorUniforms();
1159 setupDrawColorFilterUniforms(layer->getColorFilter());
1160 setupDrawTexture(layer->getTexture());
1161 if (currentTransform()->isPureTranslate()) {
1162 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1163 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001164
Chris Craik3f0854292014-04-15 16:18:08 -07001165 layer->setFilter(GL_NEAREST);
1166 setupDrawModelView(kModelViewMode_Translate, false,
1167 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1168 } else {
1169 layer->setFilter(GL_LINEAR);
1170 setupDrawModelView(kModelViewMode_Translate, false,
1171 rect.left, rect.top, rect.right, rect.bottom);
1172 }
1173 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001174
Chris Craik3f0854292014-04-15 16:18:08 -07001175 for (size_t i = 0; i < count; i++) {
1176 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001177
Chris Craik3f0854292014-04-15 16:18:08 -07001178 const float u1 = r->left * texX;
1179 const float v1 = (height - r->top) * texY;
1180 const float u2 = r->right * texX;
1181 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001182
Chris Craik3f0854292014-04-15 16:18:08 -07001183 // TODO: Reject quads outside of the clip
1184 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1185 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1186 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1187 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001188
Chris Craik3f0854292014-04-15 16:18:08 -07001189 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001190
Chris Craik3f0854292014-04-15 16:18:08 -07001191 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001192 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1193 GL_UNSIGNED_SHORT, NULL));
Chris Craik3f0854292014-04-15 16:18:08 -07001194 numQuads = 0;
1195 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001196 }
Chris Craik3f0854292014-04-15 16:18:08 -07001197 }
1198
1199 if (numQuads > 0) {
1200 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1201 GL_UNSIGNED_SHORT, NULL));
1202 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001203
Romain Guy5b3b3522010-10-27 18:57:51 -07001204#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001205 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001206#endif
1207
Chris Craik3f0854292014-04-15 16:18:08 -07001208 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001209}
1210
Romain Guy3a3133d2011-02-01 22:59:58 -08001211#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001212void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001213 size_t count;
1214 const android::Rect* rects = region.getArray(&count);
1215
1216 uint32_t colors[] = {
1217 0x7fff0000, 0x7f00ff00,
1218 0x7f0000ff, 0x7fff00ff,
1219 };
1220
1221 int offset = 0;
1222 int32_t top = rects[0].top;
1223
1224 for (size_t i = 0; i < count; i++) {
1225 if (top != rects[i].top) {
1226 offset ^= 0x2;
1227 top = rects[i].top;
1228 }
1229
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001230 SkPaint paint;
1231 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001232 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001233 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001234 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001235}
Chris Craike63f7c622013-10-17 10:30:55 -07001236#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001237
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001238void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001239 Vector<float> rects;
1240
1241 SkRegion::Iterator it(region);
1242 while (!it.done()) {
1243 const SkIRect& r = it.rect();
1244 rects.push(r.fLeft);
1245 rects.push(r.fTop);
1246 rects.push(r.fRight);
1247 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001248 it.next();
1249 }
1250
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001251 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001252}
1253
Romain Guy5b3b3522010-10-27 18:57:51 -07001254void OpenGLRenderer::dirtyLayer(const float left, const float top,
1255 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001256 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001257 Rect bounds(left, top, right, bottom);
1258 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001259 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001260 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001261}
1262
1263void OpenGLRenderer::dirtyLayer(const float left, const float top,
1264 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001265 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001266 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001267 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001268 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001269}
1270
1271void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001272 if (bounds.intersect(*currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001273 bounds.snapToPixelBoundaries();
1274 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1275 if (!dirty.isEmpty()) {
1276 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001277 }
1278 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001279}
1280
Chris Craik4063a0e2013-11-15 16:06:56 -08001281void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001282 GLsizei elementsCount = quadsCount * 6;
1283 while (elementsCount > 0) {
1284 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1285
Romain Guy3380cfd2013-08-15 16:57:57 -07001286 setupDrawIndexedVertices(&mesh[0].x);
Romain Guy448455f2013-07-22 13:57:50 -07001287 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1288
1289 elementsCount -= drawCount;
1290 // Though there are 4 vertices in a quad, we use 6 indices per
1291 // quad to draw with GL_TRIANGLES
1292 mesh += (drawCount / 6) * 4;
1293 }
1294}
1295
Romain Guy54be1cd2011-06-13 19:04:27 -07001296void OpenGLRenderer::clearLayerRegions() {
1297 const size_t count = mLayers.size();
1298 if (count == 0) return;
1299
Chris Craikd6b65f62014-01-01 14:45:21 -08001300 if (!currentSnapshot()->isIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001301 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001302 // Doing several glScissor/glClear here can negatively impact
1303 // GPUs with a tiler architecture, instead we draw quads with
1304 // the Clear blending mode
1305
1306 // The list contains bounds that have already been clipped
1307 // against their initial clip rect, and the current clip
1308 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001309 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001310
Romain Guy448455f2013-07-22 13:57:50 -07001311 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001312 Vertex* vertex = mesh;
1313
1314 for (uint32_t i = 0; i < count; i++) {
1315 Rect* bounds = mLayers.itemAt(i);
1316
Romain Guy54be1cd2011-06-13 19:04:27 -07001317 Vertex::set(vertex++, bounds->left, bounds->top);
1318 Vertex::set(vertex++, bounds->right, bounds->top);
1319 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001320 Vertex::set(vertex++, bounds->right, bounds->bottom);
1321
1322 delete bounds;
1323 }
Romain Guye67307c2013-02-11 18:01:20 -08001324 // We must clear the list of dirty rects before we
1325 // call setupDraw() to prevent stencil setup to do
1326 // the same thing again
1327 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001328
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001329 SkPaint clearPaint;
1330 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1331
Romain Guy54be1cd2011-06-13 19:04:27 -07001332 setupDraw(false);
1333 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001334 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001335 setupDrawProgram();
1336 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001337 setupDrawModelView(kModelViewMode_Translate, false,
1338 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001339
Chris Craik4063a0e2013-11-15 16:06:56 -08001340 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001341
1342 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001343 } else {
1344 for (uint32_t i = 0; i < count; i++) {
1345 delete mLayers.itemAt(i);
1346 }
Romain Guye67307c2013-02-11 18:01:20 -08001347 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001348 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001349}
1350
Romain Guybd6b79b2010-06-26 00:13:53 -07001351///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001352// State Deferral
1353///////////////////////////////////////////////////////////////////////////////
1354
Chris Craikff785832013-03-08 13:12:16 -08001355bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001356 const Rect* currentClip = currentClipRect();
1357 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001358
Chris Craikff785832013-03-08 13:12:16 -08001359 if (stateDeferFlags & kStateDeferFlag_Draw) {
1360 // state has bounds initialized in local coordinates
1361 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001362 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001363 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001364 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1365 // is used, it should more closely duplicate the quickReject logic (in how it uses
1366 // snapToPixelBoundaries)
1367
Chris Craikd6b65f62014-01-01 14:45:21 -08001368 if(!clippedBounds.intersect(*currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001369 // quick rejected
1370 return true;
1371 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001372
Chris Craika02c4ed2013-06-14 13:43:58 -07001373 state.mClipSideFlags = kClipSide_None;
Chris Craikd6b65f62014-01-01 14:45:21 -08001374 if (!currentClip->contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001375 int& flags = state.mClipSideFlags;
1376 // op partially clipped, so record which sides are clipped for clip-aware merging
Chris Craikd6b65f62014-01-01 14:45:21 -08001377 if (currentClip->left > state.mBounds.left) flags |= kClipSide_Left;
1378 if (currentClip->top > state.mBounds.top) flags |= kClipSide_Top;
1379 if (currentClip->right < state.mBounds.right) flags |= kClipSide_Right;
1380 if (currentClip->bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001381 }
1382 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001383 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001384 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1385 // overdraw avoidance (since we don't know what it overlaps)
1386 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikd6b65f62014-01-01 14:45:21 -08001387 state.mBounds.set(*currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001388 }
1389 }
1390
Chris Craik527a3aa2013-03-04 10:19:31 -08001391 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1392 if (state.mClipValid) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001393 state.mClip.set(*currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001394 }
1395
Chris Craik7273daa2013-03-28 11:25:24 -07001396 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1397 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001398 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001399 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001400 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001401
1402 // always store/restore, since it's just a pointer
1403 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001404 return false;
1405}
1406
Chris Craik527a3aa2013-03-04 10:19:31 -08001407void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001408 setMatrix(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001409 mSnapshot->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001410 mDrawModifiers = state.mDrawModifiers;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001411 mSnapshot->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001412
Chris Craik527a3aa2013-03-04 10:19:31 -08001413 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001414 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1415 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001416 dirtyClip();
1417 }
Chris Craikc3566d02013-02-04 16:16:33 -08001418}
1419
Chris Craik28ce94a2013-05-31 11:38:03 -07001420/**
1421 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1422 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1423 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1424 *
1425 * This method should be called when restoreDisplayState() won't be restoring the clip
1426 */
1427void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1428 if (clipRect != NULL) {
1429 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1430 } else {
Chris Craik14e51302013-12-30 15:32:54 -08001431 mSnapshot->setClip(0, 0, getWidth(), getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001432 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001433 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001434 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001435}
1436
Chris Craikc3566d02013-02-04 16:16:33 -08001437///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001438// Clipping
1439///////////////////////////////////////////////////////////////////////////////
1440
Romain Guybb9524b2010-06-22 18:56:38 -07001441void OpenGLRenderer::setScissorFromClip() {
Chris Craikd6b65f62014-01-01 14:45:21 -08001442 Rect clip(*currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001443 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001444
Chris Craika64a2be2014-05-14 14:17:01 -07001445 if (mCaches.setScissor(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001446 clip.getWidth(), clip.getHeight())) {
1447 mDirtyClip = false;
1448 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001449}
1450
Romain Guy8ce00302013-01-15 18:51:42 -08001451void OpenGLRenderer::ensureStencilBuffer() {
1452 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1453 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1454 // just hope we have one when hasLayer() returns false.
1455 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001456 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001457 }
1458}
1459
1460void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1461 // The layer's FBO is already bound when we reach this stage
1462 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001463 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1464 // is attached after we initiated tiling. We must turn it off,
1465 // attach the new render buffer then turn tiling back on
1466 endTiling();
1467
Romain Guy8d4aeb72013-02-12 16:08:55 -08001468 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001469 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001470 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001471
Romain Guyf735c8e2013-01-31 17:45:55 -08001472 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001473 }
1474}
1475
1476void OpenGLRenderer::setStencilFromClip() {
1477 if (!mCaches.debugOverdraw) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001478 if (!currentSnapshot()->clipRegion->isEmpty()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001479 EVENT_LOGD("setStencilFromClip - enabling");
1480
Romain Guy8ce00302013-01-15 18:51:42 -08001481 // NOTE: The order here is important, we must set dirtyClip to false
1482 // before any draw call to avoid calling back into this method
1483 mDirtyClip = false;
1484
1485 ensureStencilBuffer();
1486
1487 mCaches.stencil.enableWrite();
1488
Chris Craikdeeda3d2014-05-05 19:09:33 -07001489 // Clear and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001490 // to the region's bounds
1491 bool resetScissor = mCaches.enableScissor();
1492 if (resetScissor) {
1493 // The scissor was not set so we now need to update it
1494 setScissorFromClip();
1495 }
1496 mCaches.stencil.clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001497
1498 // stash and disable the outline clip state, since stencil doesn't account for outline
1499 bool storedSkipOutlineClip = mSkipOutlineClip;
1500 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001501
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001502 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001503 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001504 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1505
Romain Guy8ce00302013-01-15 18:51:42 -08001506 // NOTE: We could use the region contour path to generate a smaller mesh
1507 // Since we are using the stencil we could use the red book path
1508 // drawing technique. It might increase bandwidth usage though.
1509
1510 // The last parameter is important: we are not drawing in the color buffer
1511 // so we don't want to dirty the current layer, if any
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001512 drawRegionRects(*(currentSnapshot()->clipRegion), paint, false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001513 if (resetScissor) mCaches.disableScissor();
1514 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001515
1516 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001517
1518 // Draw the region used to generate the stencil if the appropriate debug
1519 // mode is enabled
1520 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001521 paint.setColor(0x7f0000ff);
1522 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
1523 drawRegionRects(*(currentSnapshot()->clipRegion), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001524 }
Romain Guy8ce00302013-01-15 18:51:42 -08001525 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001526 EVENT_LOGD("setStencilFromClip - disabling");
Romain Guy8ce00302013-01-15 18:51:42 -08001527 mCaches.stencil.disable();
1528 }
1529 }
1530}
1531
Chris Craikf0a59072013-11-19 18:00:46 -08001532/**
1533 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1534 *
1535 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1536 * style, and tessellated AA ramp
1537 */
1538bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001539 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001540 bool snapOut = paint && paint->isAntiAlias();
1541
1542 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1543 float outset = paint->getStrokeWidth() * 0.5f;
1544 left -= outset;
1545 top -= outset;
1546 right += outset;
1547 bottom += outset;
1548 }
1549
Chris Craikdeeda3d2014-05-05 19:09:33 -07001550 bool clipRequired = false;
1551 bool roundRectClipRequired = false;
1552 if (calculateQuickRejectForScissor(left, top, right, bottom,
1553 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001554 return true;
1555 }
1556
Chris Craikf23b25a2014-06-26 15:46:20 -07001557 // not quick rejected, so enable the scissor if clipRequired
1558 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1559 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001560 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001561}
1562
Romain Guy8ce00302013-01-15 18:51:42 -08001563void OpenGLRenderer::debugClip() {
1564#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001565 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001566 SkPaint paint;
1567 paint.setColor(0x7f00ff00);
1568 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1569
Romain Guy8ce00302013-01-15 18:51:42 -08001570 }
1571#endif
1572}
1573
Romain Guyf6a11b82010-06-23 17:47:49 -07001574///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001575// Drawing commands
1576///////////////////////////////////////////////////////////////////////////////
1577
Chris Craik62d307c2014-07-29 10:35:13 -07001578void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001579 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001580 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001581 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001582 // Make sure setScissor & setStencil happen at the beginning of
1583 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001584 if (mDirtyClip) {
1585 if (mCaches.scissorEnabled) {
1586 setScissorFromClip();
1587 }
Chris Craik62d307c2014-07-29 10:35:13 -07001588
1589 if (clearLayer) {
1590 setStencilFromClip();
1591 } else {
1592 // While clearing layer, force disable stencil buffer, since
1593 // it's invalid to stencil-clip *during* the layer clear
1594 mCaches.stencil.disable();
1595 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001596 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001597
Romain Guy70ca14e2010-12-13 18:24:33 -08001598 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001599
Romain Guy70ca14e2010-12-13 18:24:33 -08001600 mSetShaderColor = false;
1601 mColorSet = false;
1602 mColorA = mColorR = mColorG = mColorB = 0.0f;
1603 mTextureUnit = 0;
1604 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001605
1606 // Enable debug highlight when what we're about to draw is tested against
1607 // the stencil buffer and if stencil highlight debugging is on
1608 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1609 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1610 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001611}
1612
1613void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1614 mDescription.hasTexture = true;
1615 mDescription.hasAlpha8Texture = isAlpha8;
1616}
1617
Romain Guyff316ec2013-02-13 18:39:43 -08001618void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1619 mDescription.hasTexture = true;
1620 mDescription.hasColors = true;
1621 mDescription.hasAlpha8Texture = isAlpha8;
1622}
1623
Romain Guyaa6c24c2011-04-28 18:40:04 -07001624void OpenGLRenderer::setupDrawWithExternalTexture() {
1625 mDescription.hasExternalTexture = true;
1626}
1627
Romain Guy15bc6432011-12-13 13:11:32 -08001628void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001629 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001630}
1631
Chris Craik91a8c7c2014-08-12 14:31:35 -07001632void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1633 mDescription.hasVertexAlpha = true;
1634 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001635}
1636
Romain Guy8d0d4782010-12-14 20:13:35 -08001637void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1638 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001639 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1640 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1641 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001642 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001643 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001644}
1645
Romain Guy86568192010-12-14 15:55:39 -08001646void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1647 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001648 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1649 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1650 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001651 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001652 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001653}
1654
Romain Guy41210632012-07-16 17:04:24 -07001655void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1656 mCaches.fontRenderer->describe(mDescription, paint);
1657}
1658
Romain Guy70ca14e2010-12-13 18:24:33 -08001659void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1660 mColorA = a;
1661 mColorR = r;
1662 mColorG = g;
1663 mColorB = b;
1664 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001665 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001666}
1667
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001668void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
1669 if (shader != NULL) {
1670 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001671 }
1672}
1673
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001674void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
1675 if (filter == NULL) {
1676 return;
1677 }
1678
1679 SkXfermode::Mode mode;
1680 if (filter->asColorMode(NULL, &mode)) {
1681 mDescription.colorOp = ProgramDescription::kColorBlend;
1682 mDescription.colorMode = mode;
1683 } else if (filter->asColorMatrix(NULL)) {
1684 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001685 }
1686}
1687
Romain Guyf09ef512011-05-27 11:43:46 -07001688void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1689 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1690 mColorA = 1.0f;
1691 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001692 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001693 }
1694}
1695
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001696void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1697 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001698 // When the blending mode is kClear_Mode, we need to use a modulate color
1699 // argb=1,0,0,0
1700 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001701 // TODO: check shader blending, once we have shader drawing support for layers.
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001702 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001703 (mColorSet && mColorA < 1.0f) || isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001704 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001705}
1706
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001707void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1708 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001709 // When the blending mode is kClear_Mode, we need to use a modulate color
1710 // argb=1,0,0,0
1711 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001712 blend |= (mColorSet && mColorA < 1.0f) ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001713 (getShader(paint) && !getShader(paint)->isOpaque()) ||
1714 isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001715 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001716}
1717
1718void OpenGLRenderer::setupDrawProgram() {
1719 useProgram(mCaches.programCache.get(mDescription));
Chris Craikdeeda3d2014-05-05 19:09:33 -07001720 if (mDescription.hasRoundRectClip) {
1721 // TODO: avoid doing this repeatedly, stashing state pointer in program
1722 const RoundRectClipState* state = mSnapshot->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001723 const Rect& innerRect = state->innerRect;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001724 glUniform4f(mCaches.currentProgram->getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001725 innerRect.left, innerRect.top,
1726 innerRect.right, innerRect.bottom);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001727 glUniformMatrix4fv(mCaches.currentProgram->getUniform("roundRectInvTransform"),
1728 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001729
1730 // add half pixel to round out integer rect space to cover pixel centers
1731 float roundedOutRadius = state->radius + 0.5f;
1732 glUniform1f(mCaches.currentProgram->getUniform("roundRectRadius"),
1733 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001734 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001735}
1736
1737void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1738 mTrackDirtyRegions = false;
1739}
1740
Chris Craik4063a0e2013-11-15 16:06:56 -08001741void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1742 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001743 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001744 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001745 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001746 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001747
Romain Guy86568192010-12-14 15:55:39 -08001748 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001749 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
1750 mCaches.currentProgram->set(mSnapshot->getOrthoMatrix(), mModelViewMatrix, transformMatrix, offset);
1751 if (dirty && mTrackDirtyRegions) {
1752 if (!ignoreTransform) {
1753 dirtyLayer(left, top, right, bottom, *currentTransform());
1754 } else {
1755 dirtyLayer(left, top, right, bottom);
1756 }
Romain Guy86568192010-12-14 15:55:39 -08001757 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001758}
1759
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001760void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1761 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001762 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1763 }
1764}
1765
Romain Guy86568192010-12-14 15:55:39 -08001766void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001767 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001768 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001769 }
1770}
1771
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001772void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
1773 if (shader == NULL) {
1774 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001775 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001776
1777 if (ignoreTransform) {
1778 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1779 // because it was built into modelView / the geometry, and the description needs to
1780 // compensate.
1781 mat4 modelViewWithoutTransform;
1782 modelViewWithoutTransform.loadInverse(*currentTransform());
1783 modelViewWithoutTransform.multiply(mModelViewMatrix);
1784 mModelViewMatrix.load(modelViewWithoutTransform);
1785 }
1786
1787 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001788}
1789
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001790void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
1791 if (NULL == filter) {
1792 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001793 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001794
1795 SkColor color;
1796 SkXfermode::Mode mode;
1797 if (filter->asColorMode(&color, &mode)) {
1798 const int alpha = SkColorGetA(color);
1799 const GLfloat a = alpha / 255.0f;
1800 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1801 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1802 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1803 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1804 return;
1805 }
1806
1807 SkScalar srcColorMatrix[20];
1808 if (filter->asColorMatrix(srcColorMatrix)) {
1809
1810 float colorMatrix[16];
1811 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1812 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1813 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1814 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1815
1816 // Skia uses the range [0..255] for the addition vector, but we need
1817 // the [0..1] range to apply the vector in GLSL
1818 float colorVector[4];
1819 colorVector[0] = srcColorMatrix[4] / 255.0f;
1820 colorVector[1] = srcColorMatrix[9] / 255.0f;
1821 colorVector[2] = srcColorMatrix[14] / 255.0f;
1822 colorVector[3] = srcColorMatrix[19] / 255.0f;
1823
1824 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1825 GL_FALSE, colorMatrix);
1826 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1827 return;
1828 }
1829
1830 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001831}
1832
Romain Guy41210632012-07-16 17:04:24 -07001833void OpenGLRenderer::setupDrawTextGammaUniforms() {
1834 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1835}
1836
Romain Guy70ca14e2010-12-13 18:24:33 -08001837void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001838 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001839 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001840 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001841}
1842
1843void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001844 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001845 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001846 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001847}
1848
Romain Guyaa6c24c2011-04-28 18:40:04 -07001849void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1850 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001851 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001852 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001853}
1854
Romain Guy8f0095c2011-05-02 17:24:22 -07001855void OpenGLRenderer::setupDrawTextureTransform() {
1856 mDescription.hasTextureTransform = true;
1857}
1858
1859void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001860 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1861 GL_FALSE, &transform.data[0]);
1862}
1863
Chris Craik564acf72014-01-02 16:46:18 -08001864void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1865 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001866 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001867 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001868 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001869 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001870 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001871 }
Romain Guyd71dd362011-12-12 19:03:35 -08001872
Chris Craikcb4d6002012-09-25 12:00:29 -07001873 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001874 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001875 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001876 }
1877
1878 mCaches.unbindIndicesBuffer();
1879}
1880
Chris Craik564acf72014-01-02 16:46:18 -08001881void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1882 const GLvoid* texCoords, const GLvoid* colors) {
Romain Guyff316ec2013-02-13 18:39:43 -08001883 bool force = mCaches.unbindMeshBuffer();
1884 GLsizei stride = sizeof(ColorTextureVertex);
1885
1886 mCaches.bindPositionVertexPointer(force, vertices, stride);
1887 if (mCaches.currentProgram->texCoords >= 0) {
1888 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1889 }
1890 int slot = mCaches.currentProgram->getAttrib("colors");
1891 if (slot >= 0) {
1892 glEnableVertexAttribArray(slot);
1893 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1894 }
1895
1896 mCaches.unbindIndicesBuffer();
1897}
1898
Chris Craik564acf72014-01-02 16:46:18 -08001899void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1900 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001901 bool force = false;
1902 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1903 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1904 // use the default VBO found in Caches
1905 if (!vertices || vbo) {
1906 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1907 } else {
1908 force = mCaches.unbindMeshBuffer();
1909 }
ztenghui63d41ab2014-02-14 13:13:41 -08001910 mCaches.bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001911
Chris Craikcb4d6002012-09-25 12:00:29 -07001912 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001913 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001914 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001915 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001916}
1917
Romain Guy448455f2013-07-22 13:57:50 -07001918void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001919 bool force = mCaches.unbindMeshBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08001920 mCaches.bindQuadIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001921 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001922}
1923
Romain Guy70ca14e2010-12-13 18:24:33 -08001924///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001925// Drawing
1926///////////////////////////////////////////////////////////////////////////////
1927
Chris Craika7090e02014-06-20 16:01:00 -07001928status_t OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07001929 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08001930 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1931 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001932 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001933 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001934 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001935 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07001936 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001937 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001938 renderNode->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07001939 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08001940 }
1941
John Reck23d307c2014-10-27 12:38:48 -07001942 DeferredDisplayList deferredList(*currentClipRect());
Chris Craikff785832013-03-08 13:12:16 -08001943 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001944 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001945
1946 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07001947 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001948
Chris Craikf57776b2013-10-25 18:30:17 -07001949 return deferredList.flush(*this, dirty) | status;
Romain Guy0fe478e2010-11-08 12:08:41 -08001950 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001951
henry.uh_chen3a1bffa2014-07-03 18:01:37 +08001952 // Even if there is no drawing command(Ex: invisible),
1953 // it still needs startFrame to clear buffer and start tiling.
1954 return startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001955}
1956
Chris Craikd218a922014-01-02 17:13:34 -08001957void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, const SkPaint* paint) {
Romain Guy886b2752013-01-04 12:26:18 -08001958 int color = paint != NULL ? paint->getColor() : 0;
1959
Romain Guya168d732011-03-18 16:50:13 -07001960 float x = left;
1961 float y = top;
1962
Romain Guy886b2752013-01-04 12:26:18 -08001963 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1964
Romain Guya168d732011-03-18 16:50:13 -07001965 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001966 if (currentTransform()->isPureTranslate()) {
1967 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1968 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001969 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001970
1971 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001972 } else {
Chris Craik678625242014-02-28 12:26:34 -08001973 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001974 }
1975
Romain Guy3b748a42013-04-17 18:54:38 -07001976 // No need to check for a UV mapper on the texture object, only ARGB_8888
1977 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001978 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001979 paint, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy3b748a42013-04-17 18:54:38 -07001980 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001981}
1982
Romain Guy03c00b52013-06-20 18:30:28 -07001983/**
1984 * Important note: this method is intended to draw batches of bitmaps and
1985 * will not set the scissor enable or dirty the current layer, if any.
1986 * The caller is responsible for properly dirtying the current layer.
1987 */
Chris Craikd218a922014-01-02 17:13:34 -08001988status_t OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
1989 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1990 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001991 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001992 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08001993 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07001994
Chris Craik527a3aa2013-03-04 10:19:31 -08001995 const AutoTexture autoCleanup(texture);
1996
Chris Craik527a3aa2013-03-04 10:19:31 -08001997 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08001998 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08001999
2000 const float x = (int) floorf(bounds.left + 0.5f);
2001 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04002002 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002003 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002004 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002005 GL_TRIANGLES, bitmapCount * 6, true,
2006 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002007 } else {
2008 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002009 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002010 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2011 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002012 }
2013
2014 return DrawGlInfo::kStatusDrew;
2015}
2016
Chris Craik79647502014-08-06 13:42:24 -07002017status_t OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
2018 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Chet Haase48659092012-05-31 15:21:51 -07002019 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002020 }
2021
Romain Guya1d3c912011-12-13 14:55:06 -08002022 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002023 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002024 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002025 const AutoTexture autoCleanup(texture);
2026
Mike Reed1103b322014-07-08 12:36:44 -04002027 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002028 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002029 } else {
Chris Craik79647502014-08-06 13:42:24 -07002030 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002031 }
Chet Haase48659092012-05-31 15:21:51 -07002032
2033 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002034}
2035
Chris Craik79647502014-08-06 13:42:24 -07002036status_t OpenGLRenderer::drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) {
2037 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Chet Haase48659092012-05-31 15:21:51 -07002038 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002039 }
2040
2041 mCaches.activeTexture(0);
2042 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2043 const AutoTexture autoCleanup(texture);
2044
Mike Reed1103b322014-07-08 12:36:44 -04002045 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002046 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002047 } else {
Chris Craik79647502014-08-06 13:42:24 -07002048 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002049 }
Chet Haase48659092012-05-31 15:21:51 -07002050
2051 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002052}
2053
Chris Craikd218a922014-01-02 17:13:34 -08002054status_t OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
2055 const float* vertices, const int* colors, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002056 if (!vertices || currentSnapshot()->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002057 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002058 }
2059
Chris Craik39a908c2013-06-13 14:39:01 -07002060 // TODO: use quickReject on bounds from vertices
2061 mCaches.enableScissor();
2062
Romain Guyb18d2d02011-02-10 15:52:54 -08002063 float left = FLT_MAX;
2064 float top = FLT_MAX;
2065 float right = FLT_MIN;
2066 float bottom = FLT_MIN;
2067
Romain Guya92bb4d2012-10-16 11:08:44 -07002068 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002069
Chris Craik564acf72014-01-02 16:46:18 -08002070 Vector<ColorTextureVertex> mesh; // TODO: use C++11 unique_ptr
2071 mesh.setCapacity(count);
2072 ColorTextureVertex* vertex = mesh.editArray();
Romain Guyff316ec2013-02-13 18:39:43 -08002073
2074 bool cleanupColors = false;
2075 if (!colors) {
2076 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craikd218a922014-01-02 17:13:34 -08002077 int* newColors = new int[colorsCount];
2078 memset(newColors, 0xff, colorsCount * sizeof(int));
2079 colors = newColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002080 cleanupColors = true;
2081 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002082
Romain Guy3b748a42013-04-17 18:54:38 -07002083 mCaches.activeTexture(0);
2084 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2085 const UvMapper& mapper(getMapper(texture));
2086
Romain Guy5a7b4662011-01-20 19:09:30 -08002087 for (int32_t y = 0; y < meshHeight; y++) {
2088 for (int32_t x = 0; x < meshWidth; x++) {
2089 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2090
2091 float u1 = float(x) / meshWidth;
2092 float u2 = float(x + 1) / meshWidth;
2093 float v1 = float(y) / meshHeight;
2094 float v2 = float(y + 1) / meshHeight;
2095
Romain Guy3b748a42013-04-17 18:54:38 -07002096 mapper.map(u1, v1, u2, v2);
2097
Romain Guy5a7b4662011-01-20 19:09:30 -08002098 int ax = i + (meshWidth + 1) * 2;
2099 int ay = ax + 1;
2100 int bx = i;
2101 int by = bx + 1;
2102 int cx = i + 2;
2103 int cy = cx + 1;
2104 int dx = i + (meshWidth + 1) * 2 + 2;
2105 int dy = dx + 1;
2106
Romain Guyff316ec2013-02-13 18:39:43 -08002107 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2108 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2109 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002110
Romain Guyff316ec2013-02-13 18:39:43 -08002111 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2112 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2113 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002114
Romain Guya92bb4d2012-10-16 11:08:44 -07002115 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2116 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2117 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2118 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002119 }
2120 }
2121
Chris Craikf0a59072013-11-19 18:00:46 -08002122 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002123 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002124 return DrawGlInfo::kStatusDone;
2125 }
2126
Romain Guyff316ec2013-02-13 18:39:43 -08002127 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002128 texture = mCaches.textureCache.get(bitmap);
2129 if (!texture) {
2130 if (cleanupColors) delete[] colors;
2131 return DrawGlInfo::kStatusDone;
2132 }
Romain Guyff316ec2013-02-13 18:39:43 -08002133 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002134 const AutoTexture autoCleanup(texture);
2135
2136 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002137 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002138
2139 int alpha;
2140 SkXfermode::Mode mode;
2141 getAlphaAndMode(paint, &alpha, &mode);
2142
Romain Guyff316ec2013-02-13 18:39:43 -08002143 float a = alpha / 255.0f;
2144
Romain Guya92bb4d2012-10-16 11:08:44 -07002145 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002146 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002147 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002148
Romain Guyff316ec2013-02-13 18:39:43 -08002149 setupDraw();
2150 setupDrawWithTextureAndColor();
2151 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002152 setupDrawColorFilter(getColorFilter(paint));
2153 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002154 setupDrawProgram();
2155 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002156 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002157 setupDrawTexture(texture->id);
2158 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002159 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002160 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002161
2162 glDrawArrays(GL_TRIANGLES, 0, count);
2163
Romain Guyff316ec2013-02-13 18:39:43 -08002164 int slot = mCaches.currentProgram->getAttrib("colors");
2165 if (slot >= 0) {
2166 glDisableVertexAttribArray(slot);
2167 }
2168
2169 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002170
2171 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002172}
2173
Chris Craikd218a922014-01-02 17:13:34 -08002174status_t OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002175 float srcLeft, float srcTop, float srcRight, float srcBottom,
2176 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002177 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002178 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002179 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002180 }
2181
Romain Guya1d3c912011-12-13 14:55:06 -08002182 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002183 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002184 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002185 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002186
Romain Guy8ba548f2010-06-30 19:21:21 -07002187 const float width = texture->width;
2188 const float height = texture->height;
2189
Romain Guy3b748a42013-04-17 18:54:38 -07002190 float u1 = fmax(0.0f, srcLeft / width);
2191 float v1 = fmax(0.0f, srcTop / height);
2192 float u2 = fmin(1.0f, srcRight / width);
2193 float v2 = fmin(1.0f, srcBottom / height);
2194
2195 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002196
Romain Guy03750a02010-10-18 14:06:08 -07002197 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002198 resetDrawTextureTexCoords(u1, v1, u2, v2);
2199
Romain Guyd21b6e12011-11-30 20:21:23 -08002200 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2201
Romain Guy886b2752013-01-04 12:26:18 -08002202 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2203 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002204
Romain Guy886b2752013-01-04 12:26:18 -08002205 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2206 // Apply a scale transform on the canvas only when a shader is in use
2207 // Skia handles the ratio between the dst and src rects as a scale factor
2208 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002209 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002210 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002211
Chris Craikd6b65f62014-01-01 14:45:21 -08002212 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2213 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2214 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002215
2216 dstRight = x + (dstRight - dstLeft);
2217 dstBottom = y + (dstBottom - dstTop);
2218
2219 dstLeft = x;
2220 dstTop = y;
2221
Chris Craik678625242014-02-28 12:26:34 -08002222 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002223 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002224 } else {
Chris Craik678625242014-02-28 12:26:34 -08002225 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002226 }
2227
2228 if (CC_UNLIKELY(useScaleTransform)) {
2229 save(SkCanvas::kMatrix_SaveFlag);
2230 translate(dstLeft, dstTop);
2231 scale(scaleX, scaleY);
2232
2233 dstLeft = 0.0f;
2234 dstTop = 0.0f;
2235
2236 dstRight = srcRight - srcLeft;
2237 dstBottom = srcBottom - srcTop;
2238 }
2239
Mike Reed1103b322014-07-08 12:36:44 -04002240 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002241 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002242 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002243 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002244 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2245 } else {
2246 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002247 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002248 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002249 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2250 }
2251
2252 if (CC_UNLIKELY(useScaleTransform)) {
2253 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002254 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002255
2256 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002257
2258 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002259}
2260
Chris Craikd218a922014-01-02 17:13:34 -08002261status_t OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
2262 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002263 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002264 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002265 }
2266
Romain Guy4c2547f2013-06-11 16:19:24 -07002267 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002268 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2269 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002270
Romain Guy03c00b52013-06-20 18:30:28 -07002271 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002272}
2273
Chris Craikd218a922014-01-02 17:13:34 -08002274status_t OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
2275 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2276 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002277 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002278 return DrawGlInfo::kStatusDone;
2279 }
2280
Romain Guy211370f2012-02-01 16:10:55 -08002281 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002282 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002283 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002284 if (!texture) return DrawGlInfo::kStatusDone;
2285 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002286
Romain Guya4adcf02013-02-28 12:15:35 -08002287 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2288 texture->setFilter(GL_LINEAR, true);
2289
Chris Craikd6b65f62014-01-01 14:45:21 -08002290 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002291 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002292 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002293 const float offsetX = left + currentTransform()->getTranslateX();
2294 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002295 const size_t count = mesh->quads.size();
2296 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002297 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002298 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002299 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2300 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2301 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002302 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002303 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002304 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002305 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002306 }
2307 }
2308
Chris Craik4063a0e2013-11-15 16:06:56 -08002309 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002310 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002311 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2312 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002313
Romain Guy3b748a42013-04-17 18:54:38 -07002314 right = x + right - left;
2315 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002316 left = x;
2317 top = y;
2318 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002319 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002320 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2321 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002322 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2323 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002324 }
Chet Haase48659092012-05-31 15:21:51 -07002325
2326 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002327}
2328
Romain Guy03c00b52013-06-20 18:30:28 -07002329/**
2330 * Important note: this method is intended to draw batches of 9-patch objects and
2331 * will not set the scissor enable or dirty the current layer, if any.
2332 * The caller is responsible for properly dirtying the current layer.
2333 */
Chris Craikd218a922014-01-02 17:13:34 -08002334status_t OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
2335 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002336 mCaches.activeTexture(0);
2337 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2338 if (!texture) return DrawGlInfo::kStatusDone;
2339 const AutoTexture autoCleanup(texture);
2340
2341 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2342 texture->setFilter(GL_LINEAR, true);
2343
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002344 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2345 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002346 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002347
2348 return DrawGlInfo::kStatusDrew;
2349}
2350
Chris Craik05f3d6e2014-06-02 16:27:04 -07002351status_t OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002352 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002353 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002354 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002355 // no vertices to draw
2356 return DrawGlInfo::kStatusDone;
2357 }
2358
Chris Craik0d5ac952014-07-15 13:01:02 -07002359 Rect bounds(vertexBuffer.getBounds());
2360 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002361 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2362
Chris Craikcb4d6002012-09-25 12:00:29 -07002363 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002364 bool isAA = paint->isAntiAlias();
2365
Chris Craik710f46d2012-09-17 17:25:49 -07002366 setupDraw();
2367 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002368 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Chris Craik710f46d2012-09-17 17:25:49 -07002369 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002370 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002371 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002372 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002373 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002374 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2375 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002376 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002377 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002378 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002379
ztenghui55bfb4e2013-12-03 10:38:55 -08002380 const void* vertices = vertexBuffer.getBuffer();
Chris Craik710f46d2012-09-17 17:25:49 -07002381 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002382 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002383 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002384
Chris Craik710f46d2012-09-17 17:25:49 -07002385 int alphaSlot = -1;
2386 if (isAA) {
2387 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2388 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002389 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002390 glEnableVertexAttribArray(alphaSlot);
2391 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002392 }
Romain Guy04299382012-07-18 17:15:41 -07002393
Chris Craik05f3d6e2014-06-02 16:27:04 -07002394 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2395 if (mode == VertexBuffer::kStandard) {
ztenghui63d41ab2014-02-14 13:13:41 -08002396 mCaches.unbindIndicesBuffer();
2397 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002398 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002399 mCaches.bindShadowIndicesBuffer();
ztenghui50ecf842014-03-11 16:52:30 -07002400 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002401 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
ztenghui50ecf842014-03-11 16:52:30 -07002402 mCaches.bindShadowIndicesBuffer();
2403 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
ztenghuid5e8ade2014-08-13 15:48:02 -07002404 } else if (mode == VertexBuffer::kIndices) {
2405 mCaches.unbindIndicesBuffer();
2406 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(), GL_UNSIGNED_SHORT,
2407 vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002408 }
Romain Guy04299382012-07-18 17:15:41 -07002409
Chris Craik710f46d2012-09-17 17:25:49 -07002410 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002411 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002412 }
Chris Craik65cd6122012-12-10 17:56:27 -08002413
2414 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002415}
2416
2417/**
Chris Craik65cd6122012-12-10 17:56:27 -08002418 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2419 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2420 * screen space in all directions. However, instead of using a fragment shader to compute the
2421 * translucency of the color from its position, we simply use a varying parameter to define how far
2422 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2423 *
2424 * Doesn't yet support joins, caps, or path effects.
2425 */
Chris Craikd218a922014-01-02 17:13:34 -08002426status_t OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002427 VertexBuffer vertexBuffer;
2428 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002429 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002430 return drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002431}
2432
2433/**
2434 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2435 * and additional geometry for defining an alpha slope perimeter.
2436 *
2437 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2438 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2439 * in-shader alpha region, but found it to be taxing on some GPUs.
2440 *
2441 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2442 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002443 */
Chris Craikd218a922014-01-02 17:13:34 -08002444status_t OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002445 if (currentSnapshot()->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002446
Chris Craik65cd6122012-12-10 17:56:27 -08002447 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002448
Chris Craik65cd6122012-12-10 17:56:27 -08002449 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002450 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2451 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002452
Chris Craik05f3d6e2014-06-02 16:27:04 -07002453 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Romain Guyd71ff91d2013-02-08 13:46:40 -08002454 return DrawGlInfo::kStatusDone;
2455 }
2456
Chris Craikbf759452014-08-11 16:00:44 -07002457 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
2458 return drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002459}
2460
Chris Craikd218a922014-01-02 17:13:34 -08002461status_t OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002462 if (currentSnapshot()->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002463
Chris Craik6d29c8d2013-05-08 18:35:44 -07002464 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002465
Chris Craik6d29c8d2013-05-08 18:35:44 -07002466 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002467 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002468
Chris Craik05f3d6e2014-06-02 16:27:04 -07002469 const Rect& bounds = buffer.getBounds();
2470 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002471 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002472 }
2473
Chris Craikbf759452014-08-11 16:00:44 -07002474 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
2475 return drawVertexBuffer(buffer, paint, displayFlags);
Romain Guyed6fcb02011-03-21 13:11:28 -07002476}
2477
Chet Haase48659092012-05-31 15:21:51 -07002478status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002479 // No need to check against the clip, we fill the clip region
Chris Craikd6b65f62014-01-01 14:45:21 -08002480 if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002481
Chris Craikd6b65f62014-01-01 14:45:21 -08002482 Rect clip(*currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002483 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002484
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002485 SkPaint paint;
2486 paint.setColor(color);
2487 paint.setXfermodeMode(mode);
2488
2489 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002490
2491 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002492}
Romain Guy9d5316e2010-06-24 19:30:36 -07002493
Chet Haase48659092012-05-31 15:21:51 -07002494status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002495 const SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002496 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002497 const AutoTexture autoCleanup(texture);
2498
2499 const float x = left + texture->left - texture->offset;
2500 const float y = top + texture->top - texture->offset;
2501
2502 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002503
2504 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002505}
2506
Chet Haase48659092012-05-31 15:21:51 -07002507status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002508 float rx, float ry, const SkPaint* p) {
Chris Craik947eabf2014-08-19 10:21:12 -07002509 if (currentSnapshot()->isIgnored()
2510 || quickRejectSetupScissor(left, top, right, bottom, p)
2511 || paintWillNotDraw(*p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002512 return DrawGlInfo::kStatusDone;
2513 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002514
Chris Craikcb4d6002012-09-25 12:00:29 -07002515 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002516 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002517 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002518 right - left, bottom - top, rx, ry, p);
2519 return drawShape(left, top, texture, p);
2520 }
2521
Chris Craik6ac174b2014-06-17 13:47:05 -07002522 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2523 *currentTransform(), *p, right - left, bottom - top, rx, ry);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002524 return drawVertexBuffer(left, top, *vertexBuffer, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002525}
2526
Chris Craikd218a922014-01-02 17:13:34 -08002527status_t OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Chris Craik947eabf2014-08-19 10:21:12 -07002528 if (currentSnapshot()->isIgnored()
2529 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
2530 || paintWillNotDraw(*p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002531 return DrawGlInfo::kStatusDone;
2532 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002533 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002534 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002535 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002536 return drawShape(x - radius, y - radius, texture, p);
2537 }
2538
2539 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002540 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2541 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2542 } else {
2543 path.addCircle(x, y, radius);
2544 }
Chris Craik65cd6122012-12-10 17:56:27 -08002545 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002546}
Romain Guy01d58e42011-01-19 21:54:02 -08002547
Chet Haase48659092012-05-31 15:21:51 -07002548status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002549 const SkPaint* p) {
Chris Craik947eabf2014-08-19 10:21:12 -07002550 if (currentSnapshot()->isIgnored()
2551 || quickRejectSetupScissor(left, top, right, bottom, p)
2552 || paintWillNotDraw(*p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002553 return DrawGlInfo::kStatusDone;
2554 }
Romain Guy01d58e42011-01-19 21:54:02 -08002555
Chris Craikcb4d6002012-09-25 12:00:29 -07002556 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002557 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002558 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002559 return drawShape(left, top, texture, p);
2560 }
2561
2562 SkPath path;
2563 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002564 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2565 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2566 }
Chris Craik710f46d2012-09-17 17:25:49 -07002567 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002568 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002569}
2570
Chet Haase48659092012-05-31 15:21:51 -07002571status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002572 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Chris Craik947eabf2014-08-19 10:21:12 -07002573 if (currentSnapshot()->isIgnored()
2574 || quickRejectSetupScissor(left, top, right, bottom, p)
2575 || paintWillNotDraw(*p)) {
Chris Craik780c1282012-10-04 14:10:49 -07002576 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002577 }
2578
Chris Craik780c1282012-10-04 14:10:49 -07002579 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002580 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002581 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002582 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002583 startAngle, sweepAngle, useCenter, p);
2584 return drawShape(left, top, texture, p);
2585 }
2586
2587 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2588 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2589 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2590 }
2591
2592 SkPath path;
2593 if (useCenter) {
2594 path.moveTo(rect.centerX(), rect.centerY());
2595 }
2596 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2597 if (useCenter) {
2598 path.close();
2599 }
Chris Craik65cd6122012-12-10 17:56:27 -08002600 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002601}
2602
Romain Guycf8675e2012-10-02 12:32:25 -07002603// See SkPaintDefaults.h
2604#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2605
Chris Craikd218a922014-01-02 17:13:34 -08002606status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
2607 const SkPaint* p) {
Chris Craik947eabf2014-08-19 10:21:12 -07002608 if (currentSnapshot()->isIgnored()
2609 || quickRejectSetupScissor(left, top, right, bottom, p)
2610 || paintWillNotDraw(*p)) {
Chet Haase48659092012-05-31 15:21:51 -07002611 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002612 }
2613
Chris Craik710f46d2012-09-17 17:25:49 -07002614 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002615 // only fill style is supported by drawConvexPath, since others have to handle joins
2616 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2617 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2618 mCaches.activeTexture(0);
2619 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002620 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002621 return drawShape(left, top, texture, p);
2622 }
2623
2624 SkPath path;
2625 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2626 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2627 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2628 }
2629 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002630 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002631 }
2632
Chris Craikd6b65f62014-01-01 14:45:21 -08002633 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002634 SkPath path;
2635 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002636 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002637 } else {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002638 drawColorRect(left, top, right, bottom, p);
Chris Craik65cd6122012-12-10 17:56:27 -08002639 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002640 }
Romain Guyc7d53492010-06-25 13:41:57 -07002641}
Romain Guy9d5316e2010-06-24 19:30:36 -07002642
Chris Craikd218a922014-01-02 17:13:34 -08002643void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2644 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002645 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002646 mCaches.activeTexture(0);
2647
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002648 TextShadow textShadow;
2649 if (!getTextShadow(paint, &textShadow)) {
2650 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2651 }
2652
Raph Levien416a8472012-07-19 22:48:17 -07002653 // NOTE: The drop shadow will not perform gamma correction
2654 // if shader-based correction is enabled
2655 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2656 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002657 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002658 // If the drop shadow exceeds the max texture size or couldn't be
2659 // allocated, skip drawing
2660 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002661 const AutoTexture autoCleanup(shadow);
2662
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002663 const float sx = x - shadow->left + textShadow.dx;
2664 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002665
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002666 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * mSnapshot->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002667 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002668 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002669 }
2670
2671 setupDraw();
2672 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002673 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002674 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002675 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002676 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002677 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002678 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2679 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002680 setupDrawTexture(shadow->id);
2681 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002682 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002683 setupDrawShaderUniforms(getShader(paint));
Raph Levien416a8472012-07-19 22:48:17 -07002684 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2685
2686 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2687}
2688
Romain Guy768bffc2013-02-27 13:50:45 -08002689bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002690 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
Romain Guy768bffc2013-02-27 13:50:45 -08002691 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2692}
2693
Chet Haase48659092012-05-31 15:21:51 -07002694status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002695 const float* positions, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002696 if (text == NULL || count == 0 || currentSnapshot()->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002697 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002698 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002699
Romain Guy671d6cf2012-01-18 12:39:17 -08002700 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002701 if (!currentTransform()->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002702 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002703 }
2704
Chris Craik39a908c2013-06-13 14:39:01 -07002705 mCaches.enableScissor();
2706
Romain Guy671d6cf2012-01-18 12:39:17 -08002707 float x = 0.0f;
2708 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002709 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002710 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002711 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2712 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002713 }
2714
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002715 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002716 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002717
2718 int alpha;
2719 SkXfermode::Mode mode;
2720 getAlphaAndMode(paint, &alpha, &mode);
2721
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002722 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002723 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002724 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002725 }
2726
Romain Guy671d6cf2012-01-18 12:39:17 -08002727 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002728 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002729 if (pureTranslate && !linearFilter) {
2730 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2731 }
Romain Guy257ae352013-03-20 16:31:12 -07002732 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002733
2734 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2735 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2736
Romain Guy211370f2012-02-01 16:10:55 -08002737 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002738
Victoria Lease1e546812013-06-25 14:25:17 -07002739 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002740 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002741 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002742 if (hasActiveLayer) {
2743 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002744 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002745 }
2746 dirtyLayerUnchecked(bounds, getRegion());
2747 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002748 }
Chet Haase48659092012-05-31 15:21:51 -07002749
2750 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002751}
2752
Chris Craik59744b72014-07-01 17:56:52 -07002753bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002754 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002755 outMatrix->setIdentity();
2756 return false;
2757 } else if (CC_UNLIKELY(transform.isPerspective())) {
2758 outMatrix->setIdentity();
2759 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002760 }
Chris Craik59744b72014-07-01 17:56:52 -07002761
2762 /**
Chris Craika736cd92014-08-04 13:18:38 -07002763 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2764 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002765 */
2766 float sx, sy;
2767 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002768 outMatrix->setScale(
2769 roundf(fmaxf(1.0f, sx)),
2770 roundf(fmaxf(1.0f, sy)));
2771 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002772}
2773
Chris Craik41541822013-05-03 16:35:54 -07002774status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002775 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002776 DrawOpMode drawOpMode) {
2777
Chris Craik527a3aa2013-03-04 10:19:31 -08002778 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002779 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2780 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craikd6b65f62014-01-01 14:45:21 -08002781 if (text == NULL || count == 0 || currentSnapshot()->isIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002782 quickRejectSetupScissor(bounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002783 return DrawGlInfo::kStatusDone;
2784 }
Romain Guycac5fd32011-12-01 20:08:50 -08002785 }
2786
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002787 const float oldX = x;
2788 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002789
Chris Craikd6b65f62014-01-01 14:45:21 -08002790 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002791 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002792
Romain Guy211370f2012-02-01 16:10:55 -08002793 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002794 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2795 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002796 }
2797
Romain Guy86568192010-12-14 15:55:39 -08002798 int alpha;
2799 SkXfermode::Mode mode;
2800 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002801
Romain Guyc74f45a2013-02-26 19:10:14 -08002802 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2803
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002804 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002805 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002806 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002807 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002808 }
2809
Romain Guy19d4dd82013-03-04 11:14:26 -08002810 const bool hasActiveLayer = hasLayer();
2811
Romain Guy624234f2013-03-05 16:43:31 -08002812 // We only pass a partial transform to the font renderer. That partial
2813 // matrix defines how glyphs are rasterized. Typically we want glyphs
2814 // to be rasterized at their final size on screen, which means the partial
2815 // matrix needs to take the scale factor into account.
2816 // When a partial matrix is used to transform glyphs during rasterization,
2817 // the mesh is generated with the inverse transform (in the case of scale,
2818 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2819 // apply the full transform matrix at draw time in the vertex shader.
2820 // Applying the full matrix in the shader is the easiest way to handle
2821 // rotation and perspective and allows us to always generated quads in the
2822 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002823 SkMatrix fontTransform;
2824 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2825 || fabs(y - (int) y) > 0.0f
2826 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002827 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002828 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002829
Romain Guy624234f2013-03-05 16:43:31 -08002830 // TODO: Implement better clipping for scaled/rotated text
Chris Craikd6b65f62014-01-01 14:45:21 -08002831 const Rect* clip = !pureTranslate ? NULL : currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002832 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 -07002833
Raph Levien996e57c2012-07-23 15:22:52 -07002834 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002835 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002836
2837 // don't call issuedrawcommand, do it at end of batch
2838 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002839 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002840 SkPaint paintCopy(*paint);
2841 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2842 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002843 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002844 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002845 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002846 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002847 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002848
Chris Craik527a3aa2013-03-04 10:19:31 -08002849 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002850 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002851 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002852 }
Chris Craik41541822013-05-03 16:35:54 -07002853 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002854 }
Romain Guy694b5192010-07-21 21:33:20 -07002855
Chris Craike63f7c622013-10-17 10:30:55 -07002856 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002857
2858 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002859}
2860
Chris Craikd218a922014-01-02 17:13:34 -08002861status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
2862 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002863 if (text == NULL || count == 0 || currentSnapshot()->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002864 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002865 }
2866
Chris Craik39a908c2013-06-13 14:39:01 -07002867 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2868 mCaches.enableScissor();
2869
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002870 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002871 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002872 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002873
2874 int alpha;
2875 SkXfermode::Mode mode;
2876 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002877 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002878
Romain Guy97771732012-02-28 18:17:02 -08002879 const Rect* clip = &mSnapshot->getLocalClip();
2880 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 -08002881
Romain Guy97771732012-02-28 18:17:02 -08002882 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002883
2884 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07002885 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002886 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002887 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002888 dirtyLayerUnchecked(bounds, getRegion());
2889 }
Romain Guy97771732012-02-28 18:17:02 -08002890 }
Chet Haase48659092012-05-31 15:21:51 -07002891
2892 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002893}
2894
Chris Craikd218a922014-01-02 17:13:34 -08002895status_t OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002896 if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002897
Romain Guya1d3c912011-12-13 14:55:06 -08002898 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002899
Romain Guyfb8b7632010-08-23 21:05:08 -07002900 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002901 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002902 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002903
Romain Guy8b55f372010-08-18 17:10:07 -07002904 const float x = texture->left - texture->offset;
2905 const float y = texture->top - texture->offset;
2906
Romain Guy01d58e42011-01-19 21:54:02 -08002907 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002908
2909 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002910}
2911
Chris Craika08f95c2013-03-15 17:24:33 -07002912status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002913 if (!layer) {
2914 return DrawGlInfo::kStatusDone;
2915 }
2916
Romain Guyb2e2f242012-10-17 18:18:35 -07002917 mat4* transform = NULL;
2918 if (layer->isTextureLayer()) {
2919 transform = &layer->getTransform();
2920 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002921 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002922 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002923 }
2924 }
2925
Chris Craik39a908c2013-06-13 14:39:01 -07002926 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08002927 const bool rejected = calculateQuickRejectForScissor(x, y,
Chris Craikdeeda3d2014-05-05 19:09:33 -07002928 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, NULL, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002929
2930 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002931 if (transform && !transform->isIdentity()) {
2932 restore();
2933 }
Chet Haase48659092012-05-31 15:21:51 -07002934 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002935 }
2936
Chris Craik62d307c2014-07-29 10:35:13 -07002937 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2938 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2939
Romain Guy5bb3c732012-11-29 17:52:58 -08002940 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002941
Chris Craik39a908c2013-06-13 14:39:01 -07002942 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08002943 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002944
Romain Guy211370f2012-02-01 16:10:55 -08002945 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002946 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002947 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2948 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002949 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002950
Chris Craik16ecda52013-03-29 10:59:59 -07002951 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002952 setupDraw();
2953 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002954 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002955 setupDrawColorFilter(layer->getColorFilter());
2956 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002957 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002958 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002959 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07002960 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08002961 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
2962 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2963 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002964
Romain Guyd21b6e12011-11-30 20:21:23 -08002965 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08002966 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07002967 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002968 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002969 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08002970 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07002971 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2972 }
Romain Guyf219da52011-01-16 12:54:25 -08002973
Romain Guy448455f2013-07-22 13:57:50 -07002974 TextureVertex* mesh = &layer->mesh[0];
2975 GLsizei elementsCount = layer->meshElementCount;
2976
2977 while (elementsCount > 0) {
2978 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
2979
Romain Guy3380cfd2013-08-15 16:57:57 -07002980 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07002981 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2982 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
2983
2984 elementsCount -= drawCount;
2985 // Though there are 4 vertices in a quad, we use 6 indices per
2986 // quad to draw with GL_TRIANGLES
2987 mesh += (drawCount / 6) * 4;
2988 }
Romain Guyf219da52011-01-16 12:54:25 -08002989
Romain Guy3a3133d2011-02-01 22:59:58 -08002990#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07002991 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08002992#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002993 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002994
Romain Guy5bb3c732012-11-29 17:52:58 -08002995 if (layer->debugDrawUpdate) {
2996 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002997
2998 SkPaint paint;
2999 paint.setColor(0x7f00ff00);
3000 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003001 }
Romain Guyf219da52011-01-16 12:54:25 -08003002 }
Chris Craik34416ea2013-04-15 16:08:28 -07003003 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003004
Romain Guyb2e2f242012-10-17 18:18:35 -07003005 if (transform && !transform->isIdentity()) {
3006 restore();
3007 }
3008
Chet Haase48659092012-05-31 15:21:51 -07003009 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003010}
3011
Romain Guy6926c722010-07-12 20:20:03 -07003012///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003013// Draw filters
3014///////////////////////////////////////////////////////////////////////////////
3015
3016void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003017 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3018 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003019 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003020 mDrawModifiers.mPaintFilterClearBits = 0;
3021 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003022}
3023
3024void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craik591be6c2014-09-11 11:09:34 -07003025 // TODO: don't bother with boolean, it's redundant with clear/set bits
Chris Craikc3566d02013-02-04 16:16:33 -08003026 mDrawModifiers.mHasDrawFilter = true;
3027 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3028 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003029}
3030
Chris Craikd218a922014-01-02 17:13:34 -08003031const SkPaint* OpenGLRenderer::filterPaint(const SkPaint* paint) {
Chris Craik591be6c2014-09-11 11:09:34 -07003032 // TODO: use CompatFlagsDrawFilter here, and combine logic with android/graphics/DrawFilter.cpp
3033 // to avoid clobbering 0x02 paint flag
3034
3035 // Equivalent to the Java Paint's FILTER_BITMAP_FLAG.
3036 static const uint32_t sFilterBitmapFlag = 0x02;
3037
Romain Guy758724f2013-02-27 11:53:12 -08003038 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003039 return paint;
3040 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003041
Chris Craik591be6c2014-09-11 11:09:34 -07003042 const uint32_t clearBits = mDrawModifiers.mPaintFilterClearBits;
3043 const uint32_t setBits = mDrawModifiers.mPaintFilterSetBits;
Romain Guy5ff9df62012-01-23 17:09:05 -08003044
Chris Craik591be6c2014-09-11 11:09:34 -07003045 const uint32_t flags = (paint->getFlags() & ~clearBits) | setBits;
Romain Guy5ff9df62012-01-23 17:09:05 -08003046 mFilteredPaint = *paint;
Chris Craik591be6c2014-09-11 11:09:34 -07003047 mFilteredPaint.setFlags(flags);
3048
3049 // check if paint filter trying to override bitmap filter
3050 if ((clearBits | setBits) & sFilterBitmapFlag) {
3051 mFilteredPaint.setFilterLevel(flags & sFilterBitmapFlag
3052 ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
3053 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003054
3055 return &mFilteredPaint;
3056}
3057
3058///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003059// Drawing implementation
3060///////////////////////////////////////////////////////////////////////////////
3061
Chris Craikd218a922014-01-02 17:13:34 -08003062Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
Romain Guy3b748a42013-04-17 18:54:38 -07003063 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3064 if (!texture) {
3065 return mCaches.textureCache.get(bitmap);
3066 }
3067 return texture;
3068}
3069
Romain Guy01d58e42011-01-19 21:54:02 -08003070void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003071 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003072 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003073 return;
3074 }
3075
3076 int alpha;
3077 SkXfermode::Mode mode;
3078 getAlphaAndMode(paint, &alpha, &mode);
3079
3080 setupDraw();
3081 setupDrawWithTexture(true);
3082 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003083 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003084 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003085 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003086 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003087 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3088 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003089 setupDrawTexture(texture->id);
3090 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003091 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003092 setupDrawShaderUniforms(getShader(paint));
Romain Guy01d58e42011-01-19 21:54:02 -08003093 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3094
3095 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003096}
3097
Romain Guyf607bdc2010-09-10 19:20:06 -07003098// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003099#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3100#define kStdUnderline_Offset (1.0f / 9.0f)
3101#define kStdUnderline_Thickness (1.0f / 18.0f)
3102
Chris Craikd218a922014-01-02 17:13:34 -08003103void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3104 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003105 // Handle underline and strike-through
3106 uint32_t flags = paint->getFlags();
3107 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003108 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003109
Romain Guy211370f2012-02-01 16:10:55 -08003110 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003111 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003112 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003113
Raph Levien8b4072d2012-07-30 15:50:00 -07003114 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003115 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003116
Romain Guyf6834472011-01-23 13:32:12 -08003117 int linesCount = 0;
3118 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3119 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3120
3121 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003122 float points[pointsCount];
3123 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003124
3125 if (flags & SkPaint::kUnderlineText_Flag) {
3126 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003127 points[currentPoint++] = left;
3128 points[currentPoint++] = top;
3129 points[currentPoint++] = left + underlineWidth;
3130 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003131 }
3132
3133 if (flags & SkPaint::kStrikeThruText_Flag) {
3134 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003135 points[currentPoint++] = left;
3136 points[currentPoint++] = top;
3137 points[currentPoint++] = left + underlineWidth;
3138 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003139 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003140
Romain Guy726aeba2011-06-01 14:52:00 -07003141 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003142
Romain Guy726aeba2011-06-01 14:52:00 -07003143 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003144 }
3145 }
3146}
3147
Chris Craikd218a922014-01-02 17:13:34 -08003148status_t OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003149 if (currentSnapshot()->isIgnored()) {
Romain Guy672433d2013-01-04 19:05:13 -08003150 return DrawGlInfo::kStatusDone;
3151 }
3152
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003153 return drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003154}
3155
Chris Craikb79a3e32014-03-11 12:20:17 -07003156static void mapPointFakeZ(Vector3& point, const mat4& transformXY, const mat4& transformZ) {
3157 // map z coordinate with true 3d matrix
3158 point.z = transformZ.mapZ(point);
3159
3160 // map x,y coordinates with draw/Skia matrix
3161 transformXY.mapPoint(point.x, point.y);
3162}
3163
Chris Craik05f3d6e2014-06-02 16:27:04 -07003164status_t OpenGLRenderer::drawShadow(float casterAlpha,
3165 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003166 if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
Chris Craikf57776b2013-10-25 18:30:17 -07003167
Chris Craik15a07a22014-01-26 13:43:53 -08003168 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craikf57776b2013-10-25 18:30:17 -07003169 mCaches.enableScissor();
3170
3171 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003172 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003173
ztenghui14a4e352014-08-13 10:44:39 -07003174 // The caller has made sure casterAlpha > 0.
3175 float ambientShadowAlpha = mAmbientShadowAlpha;
3176 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3177 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3178 }
3179 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3180 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003181 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003182 }
ztenghui7b4516e2014-01-07 10:42:55 -08003183
ztenghui14a4e352014-08-13 10:44:39 -07003184 float spotShadowAlpha = mSpotShadowAlpha;
3185 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3186 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3187 }
3188 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3189 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003190 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003191 }
ztenghui7b4516e2014-01-07 10:42:55 -08003192
3193 return DrawGlInfo::kStatusDrew;
Chris Craikf57776b2013-10-25 18:30:17 -07003194}
3195
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003196status_t OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
3197 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003198 if (count == 0) {
3199 return DrawGlInfo::kStatusDone;
3200 }
Romain Guy735738c2012-12-03 12:34:51 -08003201
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003202 int color = paint->getColor();
3203 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003204 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003205 color |= 0x00ffffff;
3206 }
3207
Romain Guy672433d2013-01-04 19:05:13 -08003208 float left = FLT_MAX;
3209 float top = FLT_MAX;
3210 float right = FLT_MIN;
3211 float bottom = FLT_MIN;
3212
Romain Guy448455f2013-07-22 13:57:50 -07003213 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003214 Vertex* vertex = mesh;
3215
Chris Craik2af46352012-11-26 18:30:17 -08003216 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003217 float l = rects[index + 0];
3218 float t = rects[index + 1];
3219 float r = rects[index + 2];
3220 float b = rects[index + 3];
3221
Romain Guy3b753822013-03-05 10:27:35 -08003222 Vertex::set(vertex++, l, t);
3223 Vertex::set(vertex++, r, t);
3224 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003225 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003226
Romain Guy3b753822013-03-05 10:27:35 -08003227 left = fminf(left, l);
3228 top = fminf(top, t);
3229 right = fmaxf(right, r);
3230 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003231 }
3232
Chris Craikf0a59072013-11-19 18:00:46 -08003233 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003234 return DrawGlInfo::kStatusDone;
3235 }
Romain Guy672433d2013-01-04 19:05:13 -08003236
Romain Guy672433d2013-01-04 19:05:13 -08003237 setupDraw();
3238 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003239 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003240 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003241 setupDrawColorFilter(getColorFilter(paint));
3242 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003243 setupDrawProgram();
3244 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003245 setupDrawModelView(kModelViewMode_Translate, false,
3246 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003247 setupDrawColorUniforms(getShader(paint));
3248 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003249 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003250
Romain Guy8ce00302013-01-15 18:51:42 -08003251 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003252 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003253 }
3254
Chris Craik4063a0e2013-11-15 16:06:56 -08003255 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003256
3257 return DrawGlInfo::kStatusDrew;
3258}
3259
Romain Guy026c5e162010-06-28 17:12:22 -07003260void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003261 const SkPaint* paint, bool ignoreTransform) {
3262 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003263 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003264 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003265 color |= 0x00ffffff;
3266 }
3267
Romain Guy70ca14e2010-12-13 18:24:33 -08003268 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003269 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003270 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003271 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003272 setupDrawColorFilter(getColorFilter(paint));
3273 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003274 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003275 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3276 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003277 setupDrawColorUniforms(getShader(paint));
3278 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003279 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003280 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003281
Romain Guyc95c8d62010-09-17 15:31:32 -07003282 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3283}
3284
Romain Guy82ba8142010-07-09 13:25:56 -07003285void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003286 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003287 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003288
Romain Guy3b748a42013-04-17 18:54:38 -07003289 GLvoid* vertices = (GLvoid*) NULL;
3290 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3291
3292 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003293 vertices = &mMeshVertices[0].x;
3294 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003295
3296 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3297 texture->uvMapper->map(uvs);
3298
3299 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3300 }
3301
Chris Craikd6b65f62014-01-01 14:45:21 -08003302 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3303 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3304 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003305
Romain Guyd21b6e12011-11-30 20:21:23 -08003306 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003307 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003308 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003309 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003310 } else {
Chris Craik678625242014-02-28 12:26:34 -08003311 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003312 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003313 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3314 }
3315
3316 if (texture->uvMapper) {
3317 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003318 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003319}
3320
Romain Guyf7f93552010-07-08 19:17:03 -07003321void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003322 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003323 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003324 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3325 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003326
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003327 int a;
3328 SkXfermode::Mode mode;
3329 getAlphaAndMode(paint, &a, &mode);
3330 const float alpha = a / 255.0f;
3331
Romain Guy746b7402010-10-26 16:27:31 -07003332 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003333 setupDrawWithTexture();
3334 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003335 setupDrawColorFilter(getColorFilter(paint));
3336 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003337 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003338 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003339 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003340 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003341 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003342 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003343 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003344
Romain Guy6820ac82010-09-15 18:11:50 -07003345 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003346}
3347
Romain Guy3b748a42013-04-17 18:54:38 -07003348void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003349 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003350 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003351 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3352 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003353
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003354 int a;
3355 SkXfermode::Mode mode;
3356 getAlphaAndMode(paint, &a, &mode);
3357 const float alpha = a / 255.0f;
3358
Romain Guy3b748a42013-04-17 18:54:38 -07003359 setupDraw();
3360 setupDrawWithTexture();
3361 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003362 setupDrawColorFilter(getColorFilter(paint));
3363 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003364 setupDrawProgram();
3365 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003366 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003367 setupDrawTexture(texture);
3368 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003369 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003370 setupDrawMeshIndices(vertices, texCoords, vbo);
3371
3372 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003373}
3374
Romain Guy886b2752013-01-04 12:26:18 -08003375void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003376 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003377 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003378 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003379
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003380 int color = paint != NULL ? paint->getColor() : 0;
3381 int alpha;
3382 SkXfermode::Mode mode;
3383 getAlphaAndMode(paint, &alpha, &mode);
3384
Romain Guy886b2752013-01-04 12:26:18 -08003385 setupDraw();
3386 setupDrawWithTexture(true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003387 if (paint != NULL) {
Romain Guy886b2752013-01-04 12:26:18 -08003388 setupDrawAlpha8Color(color, alpha);
3389 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003390 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003391 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003392 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003393 setupDrawProgram();
3394 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003395 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003396 setupDrawTexture(texture);
3397 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003398 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003399 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003400 setupDrawMesh(vertices, texCoords);
3401
3402 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003403}
3404
Romain Guya5aed0d2010-09-09 14:42:43 -07003405void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003406 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003407
3408 if (mSnapshot->roundRectClipState != NULL /*&& !mSkipOutlineClip*/) {
3409 blend = true;
3410 mDescription.hasRoundRectClip = true;
3411 }
3412 mSkipOutlineClip = true;
3413
Romain Guy82ba8142010-07-09 13:25:56 -07003414 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003415
Romain Guy82ba8142010-07-09 13:25:56 -07003416 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003417 // These blend modes are not supported by OpenGL directly and have
3418 // to be implemented using shaders. Since the shader will perform
3419 // the blending, turn blending off here
3420 // If the blend mode cannot be implemented using shaders, fall
3421 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003422 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003423 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003424 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003425 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003426
Romain Guy82bc7a72012-01-03 14:13:39 -08003427 if (mCaches.blend) {
3428 glDisable(GL_BLEND);
3429 mCaches.blend = false;
3430 }
3431
3432 return;
3433 } else {
3434 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003435 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003436 }
3437
3438 if (!mCaches.blend) {
3439 glEnable(GL_BLEND);
3440 }
3441
3442 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3443 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3444
3445 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3446 glBlendFunc(sourceMode, destMode);
3447 mCaches.lastSrcMode = sourceMode;
3448 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003449 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003450 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003451 glDisable(GL_BLEND);
3452 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003453 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003454}
3455
Romain Guy889f8d12010-07-29 14:37:42 -07003456bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003457 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003458 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003459 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003460 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003461 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003462 }
Romain Guy6926c722010-07-12 20:20:03 -07003463 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003464}
3465
Romain Guy026c5e162010-06-28 17:12:22 -07003466void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003467 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003468 TextureVertex::setUV(v++, u1, v1);
3469 TextureVertex::setUV(v++, u2, v1);
3470 TextureVertex::setUV(v++, u1, v2);
3471 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003472}
3473
Chris Craikd218a922014-01-02 17:13:34 -08003474void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003475 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003476 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3477 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003478 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003479 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003480 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003481}
3482
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003483float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003484 float alpha;
3485 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3486 alpha = mDrawModifiers.mOverrideLayerAlpha;
3487 } else {
3488 alpha = layer->getAlpha() / 255.0f;
3489 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003490 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003491}
3492
Romain Guy9d5316e2010-06-24 19:30:36 -07003493}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003494}; // namespace android