blob: 71b7c11ed9eb7825dbf1f894c66569d27da9f53c [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"
Tom Hudson2dc236b2014-10-15 15:46:42 -040039#include "GammaFontRenderer.h"
40#include "Patch.h"
Chris Craik65cd6122012-12-10 17:56:27 -080041#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070042#include "Properties.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040043#include "RenderNode.h"
44#include "RenderState.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080045#include "ShadowTessellator.h"
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040046#include "SkiaShader.h"
Romain Guya957eea2010-12-08 18:34:42 -080047#include "Vector.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080048#include "VertexBuffer.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040049#include "utils/GLUtils.h"
Romain Guye4d01122010-06-16 18:44:05 -070050
Chris Craik62d307c2014-07-29 10:35:13 -070051#if DEBUG_DETAILED_EVENTS
52 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
53#else
54 #define EVENT_LOGD(...)
55#endif
56
Chris Craika8bea8e2014-09-24 11:29:43 -070057static void atraceFormatBegin(const char* fmt, ...) {
58 const int BUFFER_SIZE = 256;
59 va_list ap;
60 char buf[BUFFER_SIZE];
61
62 va_start(ap, fmt);
63 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
64 va_end(ap);
65
66 ATRACE_BEGIN(buf);
67}
68
69#define ATRACE_FORMAT_BEGIN(fmt, ...) \
70 if (CC_UNLIKELY(ATRACE_ENABLED())) atraceFormatBegin(fmt, ##__VA_ARGS__)
71
Romain Guye4d01122010-06-16 18:44:05 -070072namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070073namespace uirenderer {
74
Chris Craik678625242014-02-28 12:26:34 -080075static GLenum getFilter(const SkPaint* paint) {
76 if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) {
77 return GL_LINEAR;
78 }
79 return GL_NEAREST;
80}
Romain Guyd21b6e12011-11-30 20:21:23 -080081
Romain Guy9d5316e2010-06-24 19:30:36 -070082///////////////////////////////////////////////////////////////////////////////
83// Globals
84///////////////////////////////////////////////////////////////////////////////
85
Romain Guy889f8d12010-07-29 14:37:42 -070086/**
87 * Structure mapping Skia xfermodes to OpenGL blending factors.
88 */
89struct Blender {
90 SkXfermode::Mode mode;
91 GLenum src;
92 GLenum dst;
93}; // struct Blender
94
Romain Guy026c5e162010-06-28 17:12:22 -070095// In this array, the index of each Blender equals the value of the first
96// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
97static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070098 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
100 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
101 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
103 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
104 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
105 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
106 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
107 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
108 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
109 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
110 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500111 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -0700112 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -0700113};
Romain Guye4d01122010-06-16 18:44:05 -0700114
Romain Guy87a76572010-09-13 18:11:21 -0700115// This array contains the swapped version of each SkXfermode. For instance
116// this array's SrcOver blending mode is actually DstOver. You can refer to
117// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -0700118static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -0700119 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
120 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
121 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
122 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
123 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
124 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
125 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
126 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
127 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
128 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
129 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
130 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
131 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500132 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700133 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700134};
135
Romain Guyf6a11b82010-06-23 17:47:49 -0700136///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700137// Functions
138///////////////////////////////////////////////////////////////////////////////
139
140template<typename T>
141static inline T min(T a, T b) {
142 return a < b ? a : b;
143}
144
145///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700146// Constructors/destructor
147///////////////////////////////////////////////////////////////////////////////
148
John Reck3b202512014-06-23 13:13:08 -0700149OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -0400150 : mState(*this)
151 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -0700152 , mCaches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -0700153 , mExtensions(Extensions::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -0700154 , mRenderState(renderState)
155 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -0700156 , mSuppressTiling(false)
157 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -0400158 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -0700159 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -0700160 , mLightRadius(FLT_MIN)
161 , mAmbientShadowAlpha(0)
162 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800163 // *set* draw modifiers to be 0
164 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700165 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700166
Romain Guyac670c02010-07-27 17:39:27 -0700167 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700168}
169
Romain Guy85bf02f2010-06-22 13:11:24 -0700170OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700171 // The context has already been destroyed at this point, do not call
172 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700173}
174
Romain Guy87e2f7572012-09-24 11:37:12 -0700175void OpenGLRenderer::initProperties() {
176 char property[PROPERTY_VALUE_MAX];
177 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
178 mScissorOptimizationDisabled = !strcasecmp(property, "true");
179 INIT_LOGD(" Scissor optimization %s",
180 mScissorOptimizationDisabled ? "disabled" : "enabled");
181 } else {
182 INIT_LOGD(" Scissor optimization enabled");
183 }
Romain Guy13631f32012-01-30 17:41:55 -0800184}
185
Chris Craik058fc642014-07-23 18:19:28 -0700186void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
187 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
188 mLightCenter = lightCenter;
189 mLightRadius = lightRadius;
190 mAmbientShadowAlpha = ambientShadowAlpha;
191 mSpotShadowAlpha = spotShadowAlpha;
192}
193
Romain Guy13631f32012-01-30 17:41:55 -0800194///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700195// Setup
196///////////////////////////////////////////////////////////////////////////////
197
Chris Craik797b95b22014-05-20 18:10:25 -0700198void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700199 glDisable(GL_DITHER);
200 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
201
202 glEnableVertexAttribArray(Program::kBindingPosition);
Chris Craik284b2432014-09-18 16:05:35 -0700203 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700204}
205
Romain Guy96885eb2013-03-26 15:05:58 -0700206void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800207 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800208 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400209 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700210 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700211 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700212}
213
Tom Hudson107843d2014-09-08 11:26:26 -0400214void OpenGLRenderer::startFrame() {
215 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700216 mFrameStarted = true;
217
Tom Hudson984162f2014-10-10 13:38:16 -0400218 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700219
Romain Guy96885eb2013-03-26 15:05:58 -0700220 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700221
Tom Hudson984162f2014-10-10 13:38:16 -0400222 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800223
Romain Guy54c1a642012-09-27 17:55:46 -0700224 // Functors break the tiling extension in pretty spectacular ways
225 // This ensures we don't use tiling when a functor is going to be
226 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700227 mSuppressTiling = mCaches.hasRegisteredFunctors()
228 || mFirstFrameAfterResize;
229 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700230
Chris Craikd6b65f62014-01-01 14:45:21 -0800231 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700232
Romain Guy7c450aa2012-09-21 19:15:00 -0700233 debugOverdraw(true, true);
234
Tom Hudson107843d2014-09-08 11:26:26 -0400235 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700236 mTilingClip.right, mTilingClip.bottom, mOpaque);
237}
238
Tom Hudson107843d2014-09-08 11:26:26 -0400239void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700240 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700241
Romain Guy96885eb2013-03-26 15:05:58 -0700242 setupFrameState(left, top, right, bottom, opaque);
243
244 // Layer renderers will start the frame immediately
245 // The framebuffer renderer will first defer the display list
246 // for each layer and wait until the first drawing command
247 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800248 if (currentSnapshot()->fbo == 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700249 syncState();
250 updateLayers();
251 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400252 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700253 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700254}
255
Romain Guydcfc8362013-01-03 13:08:57 -0800256void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
257 // If we know that we are going to redraw the entire framebuffer,
258 // perform a discard to let the driver know we don't need to preserve
259 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800260 if (mExtensions.hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400261 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
262 const bool isFbo = onGetTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800263 const GLenum attachments[] = {
264 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
265 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800266 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
267 }
268}
269
Tom Hudson107843d2014-09-08 11:26:26 -0400270void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700271 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700272 mCaches.enableScissor();
Chris Craika64a2be2014-05-14 14:17:01 -0700273 mCaches.setScissor(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700274 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400275 mDirty = true;
276 return;
Romain Guyddf74372012-05-22 14:07:07 -0700277 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700278
Romain Guy7c25aab2012-10-18 15:05:02 -0700279 mCaches.resetScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700280}
281
282void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700283 if (mCaches.blend) {
284 glEnable(GL_BLEND);
285 } else {
286 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700287 }
Romain Guybb9524b2010-06-22 18:56:38 -0700288}
289
henry.uh_chen33f5a592014-07-02 19:36:56 +0800290void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700291 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800292 const Snapshot* snapshot = currentSnapshot();
293
Chris Craik14e51302013-12-30 15:32:54 -0800294 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800295 if (snapshot->flags & Snapshot::kFlagFboTarget) {
296 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700297 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700298
henry.uh_chen33f5a592014-07-02 19:36:56 +0800299 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800300 }
301}
302
henry.uh_chen33f5a592014-07-02 19:36:56 +0800303void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800304 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800305 if(expand) {
306 // Expand the startTiling region by 1
307 int leftNotZero = (clip.left > 0) ? 1 : 0;
308 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
309
310 mCaches.startTiling(
311 clip.left - leftNotZero,
312 windowHeight - clip.bottom - topNotZero,
313 clip.right - clip.left + leftNotZero + 1,
314 clip.bottom - clip.top + topNotZero + 1,
315 opaque);
316 } else {
317 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800318 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800319 }
Romain Guy54c1a642012-09-27 17:55:46 -0700320 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700321}
322
323void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700324 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700325}
326
Tom Hudson107843d2014-09-08 11:26:26 -0400327bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700328 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700329 endTiling();
330
Romain Guyca89e2a2013-03-08 17:44:20 -0800331 // When finish() is invoked on FBO 0 we've reached the end
332 // of the current frame
Tom Hudson984162f2014-10-10 13:38:16 -0400333 if (onGetTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800334 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700335 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800336 }
337
Romain Guy11cb6422012-09-21 00:39:43 -0700338 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700339#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700340 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700341#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700342
Romain Guyc15008e2010-11-10 11:59:15 -0800343#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800344 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700345#else
346 if (mCaches.getDebugLevel() & kDebugMemory) {
347 mCaches.dumpMemoryUsage();
348 }
Romain Guyc15008e2010-11-10 11:59:15 -0800349#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700350 }
Romain Guy96885eb2013-03-26 15:05:58 -0700351
352 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400353
354 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700355}
356
Romain Guy35643dd2012-09-18 15:40:58 -0700357void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700358 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
359 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700360 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700361
362 mCaches.resetScissor();
363 dirtyClip();
364}
365
Tom Hudson107843d2014-09-08 11:26:26 -0400366void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400367 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700368
Tom Hudson984162f2014-10-10 13:38:16 -0400369 Rect clip(*mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700370 clip.snapToPixelBoundaries();
371
Romain Guyd643bb52011-03-01 14:55:21 -0800372 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800373 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800374 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800375 dirtyLayerUnchecked(clip, getRegion());
376 }
Romain Guyd643bb52011-03-01 14:55:21 -0800377
Romain Guy08aa2cb2011-03-17 11:06:57 -0700378 DrawGlInfo info;
379 info.clipLeft = clip.left;
380 info.clipTop = clip.top;
381 info.clipRight = clip.right;
382 info.clipBottom = clip.bottom;
383 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700384 info.width = getViewportWidth();
385 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800386 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700387
Tom Hudson984162f2014-10-10 13:38:16 -0400388 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700389 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400390 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700391 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
392 }
John Reck3b202512014-06-23 13:13:08 -0700393 if (mCaches.enableScissor() || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700394 setScissorFromClip();
395 }
Chris Craik54f574a2013-08-26 11:23:46 -0700396
John Reck3b202512014-06-23 13:13:08 -0700397 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
398 // Scissor may have been modified, reset dirty clip
399 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800400
Tom Hudson107843d2014-09-08 11:26:26 -0400401 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800402}
403
Romain Guyf6a11b82010-06-23 17:47:49 -0700404///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700405// Debug
406///////////////////////////////////////////////////////////////////////////////
407
Chris Craik62d307c2014-07-29 10:35:13 -0700408void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
409#if DEBUG_DETAILED_EVENTS
410 const int BUFFER_SIZE = 256;
411 va_list ap;
412 char buf[BUFFER_SIZE];
413
414 va_start(ap, fmt);
415 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
416 va_end(ap);
417
418 eventMark(buf);
419#endif
420}
421
422
Romain Guy0f6675332013-03-01 14:31:04 -0800423void OpenGLRenderer::eventMark(const char* name) const {
424 mCaches.eventMark(0, name);
425}
426
Romain Guy87e2f7572012-09-24 11:37:12 -0700427void OpenGLRenderer::startMark(const char* name) const {
428 mCaches.startMark(0, name);
429}
430
431void OpenGLRenderer::endMark() const {
432 mCaches.endMark();
433}
434
435void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700436 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700437}
438
439void OpenGLRenderer::renderOverdraw() {
Tom Hudson984162f2014-10-10 13:38:16 -0400440 if (mCaches.debugOverdraw && onGetTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700441 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700442
443 mCaches.enableScissor();
Tom Hudson984162f2014-10-10 13:38:16 -0400444 mCaches.setScissor(clip->left, mState.firstSnapshot()->getViewportHeight() - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700445 clip->right - clip->left, clip->bottom - clip->top);
446
Romain Guy627c6fd2013-08-21 11:53:18 -0700447 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700448 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700449 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
450
451 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700452 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700453 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
454
455 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700456 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700457 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
458
459 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700460 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700461 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
462
Romain Guy87e2f7572012-09-24 11:37:12 -0700463 mCaches.stencil.disable();
464 }
465}
466
467///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700468// Layers
469///////////////////////////////////////////////////////////////////////////////
470
471bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700472 if (layer->deferredUpdateScheduled && layer->renderer
473 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700474 ATRACE_CALL();
475
Romain Guy11cb6422012-09-21 00:39:43 -0700476 Rect& dirty = layer->dirtyRect;
477
Romain Guy7c450aa2012-09-21 19:15:00 -0700478 if (inFrame) {
479 endTiling();
480 debugOverdraw(false, false);
481 }
Romain Guy11cb6422012-09-21 00:39:43 -0700482
Romain Guy96885eb2013-03-26 15:05:58 -0700483 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700484 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700485 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700486 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700487 }
Romain Guy11cb6422012-09-21 00:39:43 -0700488
489 if (inFrame) {
490 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800491 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700492 }
493
Romain Guy5bb3c732012-11-29 17:52:58 -0800494 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700495 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700496
497 return true;
498 }
499
500 return false;
501}
502
503void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700504 // If draw deferring is enabled this method will simply defer
505 // the display list of each individual layer. The layers remain
506 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700507 int count = mLayerUpdates.size();
508 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700509 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
510 startMark("Layer Updates");
511 } else {
512 startMark("Defer Layer Updates");
513 }
Romain Guy11cb6422012-09-21 00:39:43 -0700514
Chris Craik1206b9b2013-04-04 14:46:24 -0700515 // Note: it is very important to update the layers in order
516 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700517 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700518 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700519 }
Romain Guy11cb6422012-09-21 00:39:43 -0700520
Romain Guy96885eb2013-03-26 15:05:58 -0700521 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
522 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400523 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700524 }
525 endMark();
526 }
527}
528
529void OpenGLRenderer::flushLayers() {
530 int count = mLayerUpdates.size();
531 if (count > 0) {
532 startMark("Apply Layer Updates");
533 char layerName[12];
534
Chris Craik1206b9b2013-04-04 14:46:24 -0700535 // Note: it is very important to update the layers in order
536 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700537 Layer* layer = mLayerUpdates.itemAt(i).get();
Chris Craika8bea8e2014-09-24 11:29:43 -0700538
Romain Guy96885eb2013-03-26 15:05:58 -0700539 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700540 startMark(layerName);
Chris Craika8bea8e2014-09-24 11:29:43 -0700541 ATRACE_FORMAT_BEGIN("flushLayer %ux%u", layer->getWidth(), layer->getHeight());
Romain Guy02b49b72013-03-29 12:37:16 -0700542
Romain Guy02b49b72013-03-29 12:37:16 -0700543 layer->flush();
Chris Craika8bea8e2014-09-24 11:29:43 -0700544
Romain Guy40543602013-06-12 15:31:28 -0700545 ATRACE_END();
Chris Craika8bea8e2014-09-24 11:29:43 -0700546 endMark();
Romain Guy96885eb2013-03-26 15:05:58 -0700547 }
548
549 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400550 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700551
Romain Guy11cb6422012-09-21 00:39:43 -0700552 endMark();
553 }
554}
555
556void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
557 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700558 // Make sure we don't introduce duplicates.
559 // SortedVector would do this automatically but we need to respect
560 // the insertion order. The linear search is not an issue since
561 // this list is usually very short (typically one item, at most a few)
562 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
563 if (mLayerUpdates.itemAt(i) == layer) {
564 return;
565 }
566 }
Romain Guy11cb6422012-09-21 00:39:43 -0700567 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700568 }
569}
570
Romain Guye93482f2013-06-17 13:14:51 -0700571void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
572 if (layer) {
573 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
574 if (mLayerUpdates.itemAt(i) == layer) {
575 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700576 break;
577 }
578 }
579 }
580}
581
Romain Guy40543602013-06-12 15:31:28 -0700582void OpenGLRenderer::flushLayerUpdates() {
John Recka7c2ea22014-08-08 13:21:00 -0700583 ATRACE_CALL();
Romain Guy40543602013-06-12 15:31:28 -0700584 syncState();
585 updateLayers();
586 flushLayers();
587 // Wait for all the layer updates to be executed
588 AutoFence fence;
589}
590
John Reck443a7142014-09-04 17:40:05 -0700591void OpenGLRenderer::markLayersAsBuildLayers() {
592 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
593 mLayerUpdates[i]->wasBuildLayered = true;
594 }
595}
596
Romain Guy11cb6422012-09-21 00:39:43 -0700597///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700598// State management
599///////////////////////////////////////////////////////////////////////////////
600
Chris Craik14e51302013-12-30 15:32:54 -0800601void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700602 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800603 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
604 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700605
Chris Craika64a2be2014-05-14 14:17:01 -0700606 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700607 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700608 }
609
Romain Guy2542d192010-08-18 11:47:12 -0700610 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700611 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700612 }
Romain Guy2542d192010-08-18 11:47:12 -0700613
Romain Guy5ec99242010-11-03 16:19:08 -0700614 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700615 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700616 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700617 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800618 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700619 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700620 }
Romain Guybb9524b2010-06-22 18:56:38 -0700621}
622
Romain Guyf6a11b82010-06-23 17:47:49 -0700623///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700624// Layers
625///////////////////////////////////////////////////////////////////////////////
626
627int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700628 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700629 // force matrix/clip isolation for layer
630 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
631
Tom Hudson984162f2014-10-10 13:38:16 -0400632 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700633
Tom Hudson984162f2014-10-10 13:38:16 -0400634 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700635 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700636 }
Romain Guyd55a8612010-06-28 17:42:46 -0700637
638 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700639}
640
Chris Craikd90144d2013-03-19 15:03:48 -0700641void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
642 const Rect untransformedBounds(bounds);
643
Chris Craikd6b65f62014-01-01 14:45:21 -0800644 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700645
646 // Layers only make sense if they are in the framebuffer's bounds
Tom Hudson984162f2014-10-10 13:38:16 -0400647 if (bounds.intersect(*mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700648 // We cannot work with sub-pixels in this case
649 bounds.snapToPixelBoundaries();
650
651 // When the layer is not an FBO, we may use glCopyTexImage so we
652 // need to make sure the layer does not extend outside the bounds
653 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700654 const Snapshot& previous = *(currentSnapshot()->previous);
655 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
656 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700657 bounds.setEmpty();
658 } else if (fboLayer) {
659 clip.set(bounds);
660 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800661 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700662 inverse.mapRect(clip);
663 clip.snapToPixelBoundaries();
664 if (clip.intersect(untransformedBounds)) {
665 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
666 bounds.set(untransformedBounds);
667 } else {
668 clip.setEmpty();
669 }
670 }
671 } else {
672 bounds.setEmpty();
673 }
674}
675
Chris Craik408eb122013-03-26 18:55:15 -0700676void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
677 bool fboLayer, int alpha) {
678 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
679 bounds.getHeight() > mCaches.maxTextureSize ||
680 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400681 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700682 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400683 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700684 }
685}
686
Chris Craikd90144d2013-03-19 15:03:48 -0700687int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500688 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400689 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700690
Tom Hudson984162f2014-10-10 13:38:16 -0400691 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700692 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
693 // operations will be able to store and restore the current clip and transform info, and
694 // quick rejection will be correct (for display lists)
695
696 Rect bounds(left, top, right, bottom);
697 Rect clip;
698 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500699 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700700
Tom Hudson984162f2014-10-10 13:38:16 -0400701 if (!mState.currentlyIgnored()) {
702 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
703 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
704 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
705 writableSnapshot()->roundRectClipState = NULL;
Chris Craikd90144d2013-03-19 15:03:48 -0700706 }
707 }
708
709 return count;
710}
711
Romain Guy1c740bc2010-09-13 18:00:09 -0700712/**
713 * Layers are viewed by Skia are slightly different than layers in image editing
714 * programs (for instance.) When a layer is created, previously created layers
715 * and the frame buffer still receive every drawing command. For instance, if a
716 * layer is created and a shape intersecting the bounds of the layers and the
717 * framebuffer is draw, the shape will be drawn on both (unless the layer was
718 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
719 *
720 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
721 * texture. Unfortunately, this is inefficient as it requires every primitive to
722 * be drawn n + 1 times, where n is the number of active layers. In practice this
723 * means, for every primitive:
724 * - Switch active frame buffer
725 * - Change viewport, clip and projection matrix
726 * - Issue the drawing
727 *
728 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700729 * To avoid this, layers are implemented in a different way here, at least in the
730 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
731 * is set. When this flag is set we can redirect all drawing operations into a
732 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700733 *
734 * This implementation relies on the frame buffer being at least RGBA 8888. When
735 * a layer is created, only a texture is created, not an FBO. The content of the
736 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700737 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700738 * buffer and drawing continues as normal. This technique therefore treats the
739 * frame buffer as a scratch buffer for the layers.
740 *
741 * To compose the layers back onto the frame buffer, each layer texture
742 * (containing the original frame buffer data) is drawn as a simple quad over
743 * the frame buffer. The trick is that the quad is set as the composition
744 * destination in the blending equation, and the frame buffer becomes the source
745 * of the composition.
746 *
747 * Drawing layers with an alpha value requires an extra step before composition.
748 * An empty quad is drawn over the layer's region in the frame buffer. This quad
749 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
750 * quad is used to multiply the colors in the frame buffer. This is achieved by
751 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
752 * GL_ZERO, GL_SRC_ALPHA.
753 *
754 * Because glCopyTexImage2D() can be slow, an alternative implementation might
755 * be use to draw a single clipped layer. The implementation described above
756 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700757 *
758 * (1) The frame buffer is actually not cleared right away. To allow the GPU
759 * to potentially optimize series of calls to glCopyTexImage2D, the frame
760 * buffer is left untouched until the first drawing operation. Only when
761 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700762 */
Chet Haased48885a2012-08-28 17:43:28 -0700763bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700764 const SkPaint* paint, int flags, const SkPath* convexMask) {
Romain Guyeb993562010-10-05 18:14:38 -0700765 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700766 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700767
Romain Guyeb993562010-10-05 18:14:38 -0700768 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
769
Romain Guyf607bdc2010-09-10 19:20:06 -0700770 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700771 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700772 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700773 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000774 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700775
776 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400777 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700778 return false;
779 }
Romain Guyf18fd992010-07-08 11:45:51 -0700780
Romain Guya1d3c912011-12-13 14:55:06 -0800781 mCaches.activeTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700782 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700783 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700784 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700785 }
786
Derek Sollenberger674554f2014-02-19 16:47:32 +0000787 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700788 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700789 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
790 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500791
Chet Haasea23eed82012-04-12 15:19:04 -0700792 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700793 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700794 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700795
Romain Guy8fb95422010-08-17 18:38:51 -0700796 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400797 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
798 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700799
Chris Craika8bea8e2014-09-24 11:29:43 -0700800 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
801 fboLayer ? "" : "unclipped ",
802 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700803 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700804 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700805 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700806 } else {
807 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700808 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800809 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700810 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700811 // Workaround for some GL drivers. When reading pixels lying outside
812 // of the window we should get undefined values for those pixels.
813 // Unfortunately some drivers will turn the entire target texture black
814 // when reading outside of the window.
815 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
816 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700817 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800818 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700819
Chris Craika64a2be2014-05-14 14:17:01 -0700820 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
821 bounds.left, getViewportHeight() - bounds.bottom,
822 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700823
Romain Guy54be1cd2011-06-13 19:04:27 -0700824 // Enqueue the buffer coordinates to clear the corresponding region later
825 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700826 }
Romain Guyeb993562010-10-05 18:14:38 -0700827 }
Romain Guyf86ef572010-07-01 11:05:42 -0700828
Romain Guyd55a8612010-06-28 17:42:46 -0700829 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700830}
831
Chris Craike63f7c622013-10-17 10:30:55 -0700832bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800833 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700834 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700835
Tom Hudson984162f2014-10-10 13:38:16 -0400836 writableSnapshot()->region = &writableSnapshot()->layer->region;
837 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
838 writableSnapshot()->fbo = layer->getFbo();
839 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
840 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
841 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
842 writableSnapshot()->roundRectClipState = NULL;
Romain Guy5b3b3522010-10-27 18:57:51 -0700843
Romain Guy2b7028e2012-09-19 17:25:38 -0700844 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700845 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700846 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700847 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700848 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700849
850 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700851 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700852 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700853 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700854 }
855
856 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700857 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700858
henry.uh_chen33f5a592014-07-02 19:36:56 +0800859 // Expand the startTiling region by 1
860 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700861
862 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700863 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800864 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700865 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700866 glClear(GL_COLOR_BUFFER_BIT);
867
868 dirtyClip();
869
870 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700871 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700872 return true;
873}
874
Romain Guy1c740bc2010-09-13 18:00:09 -0700875/**
876 * Read the documentation of createLayer() before doing anything in this method.
877 */
Chris Craik14e51302013-12-30 15:32:54 -0800878void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
879 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000880 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700881 return;
882 }
883
Chris Craik14e51302013-12-30 15:32:54 -0800884 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800885 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800886 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700887
Chris Craik39a908c2013-06-13 14:39:01 -0700888 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400889 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700890 &clipRequired, NULL, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -0700891 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
892
Romain Guyeb993562010-10-05 18:14:38 -0700893 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700894 endTiling();
895
Romain Guye0aa84b2012-04-03 19:30:26 -0700896 // Detach the texture from the FBO
897 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800898
899 layer->removeFbo(false);
900
Romain Guyeb993562010-10-05 18:14:38 -0700901 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700902 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700903 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700904
Chris Craikd6b65f62014-01-01 14:45:21 -0800905 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700906 }
907
Romain Guy9ace8f52011-07-07 20:50:11 -0700908 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500909 SkPaint layerPaint;
910 layerPaint.setAlpha(layer->getAlpha());
911 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
912 layerPaint.setColorFilter(layer->getColorFilter());
913
914 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700915 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700916 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700917 }
918
Romain Guy03750a02010-10-18 14:06:08 -0700919 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700920
Romain Guya1d3c912011-12-13 14:55:06 -0800921 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700922
Romain Guy5b3b3522010-10-27 18:57:51 -0700923 // When the layer is stored in an FBO, we can save a bit of fillrate by
924 // drawing only the dirty region
925 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800926 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700927 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700928 } else if (!rect.isEmpty()) {
929 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530930
931 save(0);
932 // the layer contains screen buffer content that shouldn't be alpha modulated
933 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400934 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700935 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530936 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700937 }
Romain Guy8b55f372010-08-18 17:10:07 -0700938
Romain Guy746b7402010-10-26 16:27:31 -0700939 dirtyClip();
940
Romain Guyeb993562010-10-05 18:14:38 -0700941 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craik3f0854292014-04-15 16:18:08 -0700942 layer->setConvexMask(NULL);
Romain Guy8550c4c2010-10-08 15:49:53 -0700943 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700944 LAYER_LOGD("Deleting layer");
John Reck0e89e2b2014-10-31 14:49:06 -0700945 layer->decStrong(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700946 }
947}
948
Romain Guyaa6c24c2011-04-28 18:40:04 -0700949void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700950 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700951
952 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700953 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700954 setupDrawWithTexture();
955 } else {
956 setupDrawWithExternalTexture();
957 }
958 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700959 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500960 setupDrawColorFilter(layer->getColorFilter());
961 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700962 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700963 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500964 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700965 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
966 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700967 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700968 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700969 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800970 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800971 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700972 layer->getWidth() == (uint32_t) rect.getWidth() &&
973 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800974 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
975 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700976
Romain Guyd21b6e12011-11-30 20:21:23 -0800977 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800978 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
979 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700980 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800981 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800982 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
983 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700984 }
985 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700986 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700987
988 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700989}
990
Romain Guy5b3b3522010-10-27 18:57:51 -0700991void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700992 if (layer->isTextureLayer()) {
993 EVENT_LOGD("composeTextureLayerRect");
994 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
995 drawTextureLayer(layer, rect);
996 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
997 } else {
998 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700999 const Rect& texCoords = layer->texCoords;
1000 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1001 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001002
Romain Guy9ace8f52011-07-07 20:50:11 -07001003 float x = rect.left;
1004 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -08001005 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001006 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001007 layer->getHeight() == (uint32_t) rect.getHeight();
1008
1009 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001010 // When we're swapping, the layer is already in screen coordinates
1011 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001012 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1013 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001014 }
1015
Romain Guyd21b6e12011-11-30 20:21:23 -08001016 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001017 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001018 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001019 }
1020
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001021 SkPaint layerPaint;
1022 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
1023 layerPaint.setXfermodeMode(layer->getMode());
1024 layerPaint.setColorFilter(layer->getColorFilter());
1025
1026 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001027 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001028 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001029 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001030 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001031
Romain Guyaa6c24c2011-04-28 18:40:04 -07001032 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001033 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001034}
1035
Chris Craik34416ea2013-04-15 16:08:28 -07001036/**
1037 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1038 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1039 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1040 * by saveLayer's restore
1041 */
Tom Hudson984162f2014-10-10 13:38:16 -04001042#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1043 DRAW_COMMAND; \
1044 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
1045 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1046 DRAW_COMMAND; \
1047 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1048 } \
Chris Craik34416ea2013-04-15 16:08:28 -07001049 }
1050
1051#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1052
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001053// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
1054// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
1055class LayerShader : public SkShader {
1056public:
1057 LayerShader(Layer* layer, const SkMatrix* localMatrix)
1058 : INHERITED(localMatrix)
1059 , mLayer(layer) {
1060 }
1061
1062 virtual bool asACustomShader(void** data) const {
1063 if (data) {
1064 *data = static_cast<void*>(mLayer);
1065 }
1066 return true;
1067 }
1068
1069 virtual bool isOpaque() const {
1070 return !mLayer->isBlend();
1071 }
1072
1073protected:
1074 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
1075 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
1076 }
1077
1078 virtual void flatten(SkWriteBuffer&) const {
1079 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1080 }
1081
1082 virtual Factory getFactory() const {
1083 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
1084 return NULL;
1085 }
1086private:
1087 // Unowned.
1088 Layer* mLayer;
1089 typedef SkShader INHERITED;
1090};
1091
Romain Guy5b3b3522010-10-27 18:57:51 -07001092void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001093 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1094
1095 if (layer->getConvexMask()) {
1096 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1097
1098 // clip to the area of the layer the mask can be larger
1099 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1100
1101 SkPaint paint;
1102 paint.setAntiAlias(true);
1103 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1104
Chris Craik3f0854292014-04-15 16:18:08 -07001105 // create LayerShader to map SaveLayer content into subsequent draw
1106 SkMatrix shaderMatrix;
1107 shaderMatrix.setTranslate(rect.left, rect.bottom);
1108 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001109 LayerShader layerShader(layer, &shaderMatrix);
1110 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001111
1112 // Since the drawing primitive is defined in local drawing space,
1113 // we don't need to modify the draw matrix
1114 const SkPath* maskPath = layer->getConvexMask();
1115 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1116
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001117 paint.setShader(NULL);
Chris Craik3f0854292014-04-15 16:18:08 -07001118 restore();
1119
1120 return;
1121 }
1122
Romain Guy5b3b3522010-10-27 18:57:51 -07001123 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001124 layer->setRegionAsRect();
1125
Chris Craik34416ea2013-04-15 16:08:28 -07001126 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001127
Romain Guy5b3b3522010-10-27 18:57:51 -07001128 layer->region.clear();
1129 return;
1130 }
1131
Chris Craik62d307c2014-07-29 10:35:13 -07001132 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001133 // standard Region based draw
1134 size_t count;
1135 const android::Rect* rects;
1136 Region safeRegion;
1137 if (CC_LIKELY(hasRectToRectTransform())) {
1138 rects = layer->region.getArray(&count);
1139 } else {
1140 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1141 rects = safeRegion.getArray(&count);
1142 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001143
Chris Craik3f0854292014-04-15 16:18:08 -07001144 const float alpha = getLayerAlpha(layer);
1145 const float texX = 1.0f / float(layer->getWidth());
1146 const float texY = 1.0f / float(layer->getHeight());
1147 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001148
Chris Craik3f0854292014-04-15 16:18:08 -07001149 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001150
Chris Craik3f0854292014-04-15 16:18:08 -07001151 // We must get (and therefore bind) the region mesh buffer
1152 // after we setup drawing in case we need to mess with the
1153 // stencil buffer in setupDraw()
1154 TextureVertex* mesh = mCaches.getRegionMesh();
1155 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001156
Chris Craik3f0854292014-04-15 16:18:08 -07001157 setupDrawWithTexture();
1158 setupDrawColor(alpha, alpha, alpha, alpha);
1159 setupDrawColorFilter(layer->getColorFilter());
1160 setupDrawBlending(layer);
1161 setupDrawProgram();
1162 setupDrawDirtyRegionsDisabled();
1163 setupDrawPureColorUniforms();
1164 setupDrawColorFilterUniforms(layer->getColorFilter());
1165 setupDrawTexture(layer->getTexture());
1166 if (currentTransform()->isPureTranslate()) {
1167 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1168 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001169
Chris Craik3f0854292014-04-15 16:18:08 -07001170 layer->setFilter(GL_NEAREST);
1171 setupDrawModelView(kModelViewMode_Translate, false,
1172 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1173 } else {
1174 layer->setFilter(GL_LINEAR);
1175 setupDrawModelView(kModelViewMode_Translate, false,
1176 rect.left, rect.top, rect.right, rect.bottom);
1177 }
1178 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001179
Chris Craik3f0854292014-04-15 16:18:08 -07001180 for (size_t i = 0; i < count; i++) {
1181 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001182
Chris Craik3f0854292014-04-15 16:18:08 -07001183 const float u1 = r->left * texX;
1184 const float v1 = (height - r->top) * texY;
1185 const float u2 = r->right * texX;
1186 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001187
Chris Craik3f0854292014-04-15 16:18:08 -07001188 // TODO: Reject quads outside of the clip
1189 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1190 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1191 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1192 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001193
Chris Craik3f0854292014-04-15 16:18:08 -07001194 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001195
Chris Craik3f0854292014-04-15 16:18:08 -07001196 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001197 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1198 GL_UNSIGNED_SHORT, NULL));
Chris Craik3f0854292014-04-15 16:18:08 -07001199 numQuads = 0;
1200 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001201 }
Chris Craik3f0854292014-04-15 16:18:08 -07001202 }
1203
1204 if (numQuads > 0) {
1205 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1206 GL_UNSIGNED_SHORT, NULL));
1207 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001208
Romain Guy5b3b3522010-10-27 18:57:51 -07001209#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001210 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001211#endif
1212
Chris Craik3f0854292014-04-15 16:18:08 -07001213 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001214}
1215
Romain Guy3a3133d2011-02-01 22:59:58 -08001216#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001217void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001218 size_t count;
1219 const android::Rect* rects = region.getArray(&count);
1220
1221 uint32_t colors[] = {
1222 0x7fff0000, 0x7f00ff00,
1223 0x7f0000ff, 0x7fff00ff,
1224 };
1225
1226 int offset = 0;
1227 int32_t top = rects[0].top;
1228
1229 for (size_t i = 0; i < count; i++) {
1230 if (top != rects[i].top) {
1231 offset ^= 0x2;
1232 top = rects[i].top;
1233 }
1234
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001235 SkPaint paint;
1236 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001237 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001238 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001239 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001240}
Chris Craike63f7c622013-10-17 10:30:55 -07001241#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001242
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001243void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001244 Vector<float> rects;
1245
1246 SkRegion::Iterator it(region);
1247 while (!it.done()) {
1248 const SkIRect& r = it.rect();
1249 rects.push(r.fLeft);
1250 rects.push(r.fTop);
1251 rects.push(r.fRight);
1252 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001253 it.next();
1254 }
1255
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001256 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001257}
1258
Romain Guy5b3b3522010-10-27 18:57:51 -07001259void OpenGLRenderer::dirtyLayer(const float left, const float top,
1260 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001261 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001262 Rect bounds(left, top, right, bottom);
1263 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001264 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001265 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001266}
1267
1268void OpenGLRenderer::dirtyLayer(const float left, const float top,
1269 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001270 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001271 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001272 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001273 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001274}
1275
1276void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Tom Hudson984162f2014-10-10 13:38:16 -04001277 if (bounds.intersect(*mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001278 bounds.snapToPixelBoundaries();
1279 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1280 if (!dirty.isEmpty()) {
1281 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001282 }
1283 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001284}
1285
Chris Craik4063a0e2013-11-15 16:06:56 -08001286void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001287 GLsizei elementsCount = quadsCount * 6;
1288 while (elementsCount > 0) {
1289 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1290
Romain Guy3380cfd2013-08-15 16:57:57 -07001291 setupDrawIndexedVertices(&mesh[0].x);
Romain Guy448455f2013-07-22 13:57:50 -07001292 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1293
1294 elementsCount -= drawCount;
1295 // Though there are 4 vertices in a quad, we use 6 indices per
1296 // quad to draw with GL_TRIANGLES
1297 mesh += (drawCount / 6) * 4;
1298 }
1299}
1300
Romain Guy54be1cd2011-06-13 19:04:27 -07001301void OpenGLRenderer::clearLayerRegions() {
1302 const size_t count = mLayers.size();
1303 if (count == 0) return;
1304
Tom Hudson984162f2014-10-10 13:38:16 -04001305 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001306 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001307 // Doing several glScissor/glClear here can negatively impact
1308 // GPUs with a tiler architecture, instead we draw quads with
1309 // the Clear blending mode
1310
1311 // The list contains bounds that have already been clipped
1312 // against their initial clip rect, and the current clip
1313 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001314 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001315
Romain Guy448455f2013-07-22 13:57:50 -07001316 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001317 Vertex* vertex = mesh;
1318
1319 for (uint32_t i = 0; i < count; i++) {
1320 Rect* bounds = mLayers.itemAt(i);
1321
Romain Guy54be1cd2011-06-13 19:04:27 -07001322 Vertex::set(vertex++, bounds->left, bounds->top);
1323 Vertex::set(vertex++, bounds->right, bounds->top);
1324 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001325 Vertex::set(vertex++, bounds->right, bounds->bottom);
1326
1327 delete bounds;
1328 }
Romain Guye67307c2013-02-11 18:01:20 -08001329 // We must clear the list of dirty rects before we
1330 // call setupDraw() to prevent stencil setup to do
1331 // the same thing again
1332 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001333
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001334 SkPaint clearPaint;
1335 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1336
Romain Guy54be1cd2011-06-13 19:04:27 -07001337 setupDraw(false);
1338 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001339 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001340 setupDrawProgram();
1341 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001342 setupDrawModelView(kModelViewMode_Translate, false,
1343 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001344
Chris Craik4063a0e2013-11-15 16:06:56 -08001345 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001346
1347 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001348 } else {
1349 for (uint32_t i = 0; i < count; i++) {
1350 delete mLayers.itemAt(i);
1351 }
Romain Guye67307c2013-02-11 18:01:20 -08001352 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001353 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001354}
1355
Romain Guybd6b79b2010-06-26 00:13:53 -07001356///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001357// State Deferral
1358///////////////////////////////////////////////////////////////////////////////
1359
Chris Craikff785832013-03-08 13:12:16 -08001360bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Tom Hudson984162f2014-10-10 13:38:16 -04001361 const Rect* currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001362 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001363
Chris Craikff785832013-03-08 13:12:16 -08001364 if (stateDeferFlags & kStateDeferFlag_Draw) {
1365 // state has bounds initialized in local coordinates
1366 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001367 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001368 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001369 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1370 // is used, it should more closely duplicate the quickReject logic (in how it uses
1371 // snapToPixelBoundaries)
1372
Chris Craikd6b65f62014-01-01 14:45:21 -08001373 if(!clippedBounds.intersect(*currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001374 // quick rejected
1375 return true;
1376 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001377
Chris Craika02c4ed2013-06-14 13:43:58 -07001378 state.mClipSideFlags = kClipSide_None;
Chris Craikd6b65f62014-01-01 14:45:21 -08001379 if (!currentClip->contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001380 int& flags = state.mClipSideFlags;
1381 // op partially clipped, so record which sides are clipped for clip-aware merging
Chris Craikd6b65f62014-01-01 14:45:21 -08001382 if (currentClip->left > state.mBounds.left) flags |= kClipSide_Left;
1383 if (currentClip->top > state.mBounds.top) flags |= kClipSide_Top;
1384 if (currentClip->right < state.mBounds.right) flags |= kClipSide_Right;
1385 if (currentClip->bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001386 }
1387 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001388 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001389 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1390 // overdraw avoidance (since we don't know what it overlaps)
1391 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikd6b65f62014-01-01 14:45:21 -08001392 state.mBounds.set(*currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001393 }
1394 }
1395
Chris Craik527a3aa2013-03-04 10:19:31 -08001396 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1397 if (state.mClipValid) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001398 state.mClip.set(*currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001399 }
1400
Chris Craik7273daa2013-03-28 11:25:24 -07001401 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1402 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001403 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001404 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001405 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001406
1407 // always store/restore, since it's just a pointer
1408 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001409 return false;
1410}
1411
Chris Craik527a3aa2013-03-04 10:19:31 -08001412void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001413 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001414 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001415 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001416 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001417
Chris Craik527a3aa2013-03-04 10:19:31 -08001418 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001419 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001420 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001421 dirtyClip();
1422 }
Chris Craikc3566d02013-02-04 16:16:33 -08001423}
1424
Chris Craik28ce94a2013-05-31 11:38:03 -07001425/**
1426 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1427 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1428 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1429 *
1430 * This method should be called when restoreDisplayState() won't be restoring the clip
1431 */
1432void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1433 if (clipRect != NULL) {
Tom Hudson984162f2014-10-10 13:38:16 -04001434 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001435 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001436 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001437 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001438 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001439 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001440}
1441
Chris Craikc3566d02013-02-04 16:16:33 -08001442///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001443// Clipping
1444///////////////////////////////////////////////////////////////////////////////
1445
Romain Guybb9524b2010-06-22 18:56:38 -07001446void OpenGLRenderer::setScissorFromClip() {
Tom Hudson984162f2014-10-10 13:38:16 -04001447 Rect clip(*mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001448 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001449
Chris Craika64a2be2014-05-14 14:17:01 -07001450 if (mCaches.setScissor(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001451 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001452 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001453 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001454}
1455
Romain Guy8ce00302013-01-15 18:51:42 -08001456void OpenGLRenderer::ensureStencilBuffer() {
1457 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1458 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1459 // just hope we have one when hasLayer() returns false.
1460 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001461 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001462 }
1463}
1464
1465void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1466 // The layer's FBO is already bound when we reach this stage
1467 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001468 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1469 // is attached after we initiated tiling. We must turn it off,
1470 // attach the new render buffer then turn tiling back on
1471 endTiling();
1472
Romain Guy8d4aeb72013-02-12 16:08:55 -08001473 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001474 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001475 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001476
Romain Guyf735c8e2013-01-31 17:45:55 -08001477 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001478 }
1479}
1480
1481void OpenGLRenderer::setStencilFromClip() {
1482 if (!mCaches.debugOverdraw) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001483 if (!currentSnapshot()->clipRegion->isEmpty()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001484 EVENT_LOGD("setStencilFromClip - enabling");
1485
Romain Guy8ce00302013-01-15 18:51:42 -08001486 // NOTE: The order here is important, we must set dirtyClip to false
1487 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001488 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001489
1490 ensureStencilBuffer();
1491
1492 mCaches.stencil.enableWrite();
1493
Chris Craikdeeda3d2014-05-05 19:09:33 -07001494 // Clear and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001495 // to the region's bounds
1496 bool resetScissor = mCaches.enableScissor();
1497 if (resetScissor) {
1498 // The scissor was not set so we now need to update it
1499 setScissorFromClip();
1500 }
1501 mCaches.stencil.clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001502
1503 // stash and disable the outline clip state, since stencil doesn't account for outline
1504 bool storedSkipOutlineClip = mSkipOutlineClip;
1505 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001506
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001507 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001508 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001509 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1510
Romain Guy8ce00302013-01-15 18:51:42 -08001511 // NOTE: We could use the region contour path to generate a smaller mesh
1512 // Since we are using the stencil we could use the red book path
1513 // drawing technique. It might increase bandwidth usage though.
1514
1515 // The last parameter is important: we are not drawing in the color buffer
1516 // so we don't want to dirty the current layer, if any
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001517 drawRegionRects(*(currentSnapshot()->clipRegion), paint, false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001518 if (resetScissor) mCaches.disableScissor();
1519 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001520
1521 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001522
1523 // Draw the region used to generate the stencil if the appropriate debug
1524 // mode is enabled
1525 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001526 paint.setColor(0x7f0000ff);
1527 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
1528 drawRegionRects(*(currentSnapshot()->clipRegion), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001529 }
Romain Guy8ce00302013-01-15 18:51:42 -08001530 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001531 EVENT_LOGD("setStencilFromClip - disabling");
Romain Guy8ce00302013-01-15 18:51:42 -08001532 mCaches.stencil.disable();
1533 }
1534 }
1535}
1536
Chris Craikf0a59072013-11-19 18:00:46 -08001537/**
1538 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1539 *
1540 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1541 * style, and tessellated AA ramp
1542 */
1543bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001544 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001545 bool snapOut = paint && paint->isAntiAlias();
1546
1547 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1548 float outset = paint->getStrokeWidth() * 0.5f;
1549 left -= outset;
1550 top -= outset;
1551 right += outset;
1552 bottom += outset;
1553 }
1554
Chris Craikdeeda3d2014-05-05 19:09:33 -07001555 bool clipRequired = false;
1556 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001557 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001558 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001559 return true;
1560 }
1561
Chris Craikf23b25a2014-06-26 15:46:20 -07001562 // not quick rejected, so enable the scissor if clipRequired
1563 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1564 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001565 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001566}
1567
Romain Guy8ce00302013-01-15 18:51:42 -08001568void OpenGLRenderer::debugClip() {
1569#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001570 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001571 SkPaint paint;
1572 paint.setColor(0x7f00ff00);
1573 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1574
Romain Guy8ce00302013-01-15 18:51:42 -08001575 }
1576#endif
1577}
1578
Romain Guyf6a11b82010-06-23 17:47:49 -07001579///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001580// Drawing commands
1581///////////////////////////////////////////////////////////////////////////////
1582
Chris Craik62d307c2014-07-29 10:35:13 -07001583void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001584 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001585 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001586 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001587 // Make sure setScissor & setStencil happen at the beginning of
1588 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001589 if (mState.getDirtyClip()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001590 if (mCaches.scissorEnabled) {
1591 setScissorFromClip();
1592 }
Chris Craik62d307c2014-07-29 10:35:13 -07001593
1594 if (clearLayer) {
1595 setStencilFromClip();
1596 } else {
1597 // While clearing layer, force disable stencil buffer, since
1598 // it's invalid to stencil-clip *during* the layer clear
1599 mCaches.stencil.disable();
1600 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001601 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001602
Romain Guy70ca14e2010-12-13 18:24:33 -08001603 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001604
Romain Guy70ca14e2010-12-13 18:24:33 -08001605 mSetShaderColor = false;
1606 mColorSet = false;
1607 mColorA = mColorR = mColorG = mColorB = 0.0f;
1608 mTextureUnit = 0;
1609 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001610
1611 // Enable debug highlight when what we're about to draw is tested against
1612 // the stencil buffer and if stencil highlight debugging is on
1613 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1614 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1615 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001616}
1617
1618void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1619 mDescription.hasTexture = true;
1620 mDescription.hasAlpha8Texture = isAlpha8;
1621}
1622
Romain Guyff316ec2013-02-13 18:39:43 -08001623void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1624 mDescription.hasTexture = true;
1625 mDescription.hasColors = true;
1626 mDescription.hasAlpha8Texture = isAlpha8;
1627}
1628
Romain Guyaa6c24c2011-04-28 18:40:04 -07001629void OpenGLRenderer::setupDrawWithExternalTexture() {
1630 mDescription.hasExternalTexture = true;
1631}
1632
Romain Guy15bc6432011-12-13 13:11:32 -08001633void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001634 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001635}
1636
Chris Craik91a8c7c2014-08-12 14:31:35 -07001637void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1638 mDescription.hasVertexAlpha = true;
1639 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001640}
1641
Romain Guy8d0d4782010-12-14 20:13:35 -08001642void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1643 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001644 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1645 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1646 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001647 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001648 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001649}
1650
Romain Guy86568192010-12-14 15:55:39 -08001651void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1652 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001653 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1654 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1655 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001656 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001657 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001658}
1659
Romain Guy41210632012-07-16 17:04:24 -07001660void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1661 mCaches.fontRenderer->describe(mDescription, paint);
1662}
1663
Romain Guy70ca14e2010-12-13 18:24:33 -08001664void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1665 mColorA = a;
1666 mColorR = r;
1667 mColorG = g;
1668 mColorB = b;
1669 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001670 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001671}
1672
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001673void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
1674 if (shader != NULL) {
1675 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001676 }
1677}
1678
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001679void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
1680 if (filter == NULL) {
1681 return;
1682 }
1683
1684 SkXfermode::Mode mode;
1685 if (filter->asColorMode(NULL, &mode)) {
1686 mDescription.colorOp = ProgramDescription::kColorBlend;
1687 mDescription.colorMode = mode;
1688 } else if (filter->asColorMatrix(NULL)) {
1689 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001690 }
1691}
1692
Romain Guyf09ef512011-05-27 11:43:46 -07001693void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1694 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1695 mColorA = 1.0f;
1696 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001697 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001698 }
1699}
1700
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001701void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1702 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001703 // When the blending mode is kClear_Mode, we need to use a modulate color
1704 // argb=1,0,0,0
1705 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001706 // TODO: check shader blending, once we have shader drawing support for layers.
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001707 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001708 (mColorSet && mColorA < 1.0f) || isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001709 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001710}
1711
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001712void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1713 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001714 // When the blending mode is kClear_Mode, we need to use a modulate color
1715 // argb=1,0,0,0
1716 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001717 blend |= (mColorSet && mColorA < 1.0f) ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001718 (getShader(paint) && !getShader(paint)->isOpaque()) ||
1719 isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001720 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001721}
1722
1723void OpenGLRenderer::setupDrawProgram() {
1724 useProgram(mCaches.programCache.get(mDescription));
Chris Craikdeeda3d2014-05-05 19:09:33 -07001725 if (mDescription.hasRoundRectClip) {
1726 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001727 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001728 const Rect& innerRect = state->innerRect;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001729 glUniform4f(mCaches.currentProgram->getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001730 innerRect.left, innerRect.top,
1731 innerRect.right, innerRect.bottom);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001732 glUniformMatrix4fv(mCaches.currentProgram->getUniform("roundRectInvTransform"),
1733 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001734
1735 // add half pixel to round out integer rect space to cover pixel centers
1736 float roundedOutRadius = state->radius + 0.5f;
1737 glUniform1f(mCaches.currentProgram->getUniform("roundRectRadius"),
1738 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001739 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001740}
1741
1742void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1743 mTrackDirtyRegions = false;
1744}
1745
Chris Craik4063a0e2013-11-15 16:06:56 -08001746void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1747 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001748 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001749 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001750 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001751 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001752
Romain Guy86568192010-12-14 15:55:39 -08001753 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001754 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Tom Hudson984162f2014-10-10 13:38:16 -04001755 mCaches.currentProgram->set(writableSnapshot()->getOrthoMatrix(), mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001756 if (dirty && mTrackDirtyRegions) {
1757 if (!ignoreTransform) {
1758 dirtyLayer(left, top, right, bottom, *currentTransform());
1759 } else {
1760 dirtyLayer(left, top, right, bottom);
1761 }
Romain Guy86568192010-12-14 15:55:39 -08001762 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001763}
1764
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001765void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1766 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001767 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1768 }
1769}
1770
Romain Guy86568192010-12-14 15:55:39 -08001771void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001772 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001773 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001774 }
1775}
1776
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001777void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
1778 if (shader == NULL) {
1779 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001780 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001781
1782 if (ignoreTransform) {
1783 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1784 // because it was built into modelView / the geometry, and the description needs to
1785 // compensate.
1786 mat4 modelViewWithoutTransform;
1787 modelViewWithoutTransform.loadInverse(*currentTransform());
1788 modelViewWithoutTransform.multiply(mModelViewMatrix);
1789 mModelViewMatrix.load(modelViewWithoutTransform);
1790 }
1791
1792 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001793}
1794
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001795void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
1796 if (NULL == filter) {
1797 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001798 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001799
1800 SkColor color;
1801 SkXfermode::Mode mode;
1802 if (filter->asColorMode(&color, &mode)) {
1803 const int alpha = SkColorGetA(color);
1804 const GLfloat a = alpha / 255.0f;
1805 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1806 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1807 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1808 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1809 return;
1810 }
1811
1812 SkScalar srcColorMatrix[20];
1813 if (filter->asColorMatrix(srcColorMatrix)) {
1814
1815 float colorMatrix[16];
1816 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1817 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1818 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1819 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1820
1821 // Skia uses the range [0..255] for the addition vector, but we need
1822 // the [0..1] range to apply the vector in GLSL
1823 float colorVector[4];
1824 colorVector[0] = srcColorMatrix[4] / 255.0f;
1825 colorVector[1] = srcColorMatrix[9] / 255.0f;
1826 colorVector[2] = srcColorMatrix[14] / 255.0f;
1827 colorVector[3] = srcColorMatrix[19] / 255.0f;
1828
1829 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1830 GL_FALSE, colorMatrix);
1831 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1832 return;
1833 }
1834
1835 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001836}
1837
Romain Guy41210632012-07-16 17:04:24 -07001838void OpenGLRenderer::setupDrawTextGammaUniforms() {
1839 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1840}
1841
Romain Guy70ca14e2010-12-13 18:24:33 -08001842void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001843 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001844 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001845 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001846}
1847
1848void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001849 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001850 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001851 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001852}
1853
Romain Guyaa6c24c2011-04-28 18:40:04 -07001854void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1855 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001856 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001857 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001858}
1859
Romain Guy8f0095c2011-05-02 17:24:22 -07001860void OpenGLRenderer::setupDrawTextureTransform() {
1861 mDescription.hasTextureTransform = true;
1862}
1863
1864void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001865 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1866 GL_FALSE, &transform.data[0]);
1867}
1868
Chris Craik564acf72014-01-02 16:46:18 -08001869void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1870 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001871 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001872 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001873 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001874 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001875 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001876 }
Romain Guyd71dd362011-12-12 19:03:35 -08001877
Chris Craikcb4d6002012-09-25 12:00:29 -07001878 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001879 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001880 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001881 }
1882
1883 mCaches.unbindIndicesBuffer();
1884}
1885
Chris Craik564acf72014-01-02 16:46:18 -08001886void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1887 const GLvoid* texCoords, const GLvoid* colors) {
Romain Guyff316ec2013-02-13 18:39:43 -08001888 bool force = mCaches.unbindMeshBuffer();
1889 GLsizei stride = sizeof(ColorTextureVertex);
1890
1891 mCaches.bindPositionVertexPointer(force, vertices, stride);
1892 if (mCaches.currentProgram->texCoords >= 0) {
1893 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1894 }
1895 int slot = mCaches.currentProgram->getAttrib("colors");
1896 if (slot >= 0) {
1897 glEnableVertexAttribArray(slot);
1898 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1899 }
1900
1901 mCaches.unbindIndicesBuffer();
1902}
1903
Chris Craik564acf72014-01-02 16:46:18 -08001904void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1905 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001906 bool force = false;
1907 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1908 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1909 // use the default VBO found in Caches
1910 if (!vertices || vbo) {
1911 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1912 } else {
1913 force = mCaches.unbindMeshBuffer();
1914 }
ztenghui63d41ab2014-02-14 13:13:41 -08001915 mCaches.bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001916
Chris Craikcb4d6002012-09-25 12:00:29 -07001917 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001918 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001919 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001920 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001921}
1922
Romain Guy448455f2013-07-22 13:57:50 -07001923void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001924 bool force = mCaches.unbindMeshBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08001925 mCaches.bindQuadIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001926 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001927}
1928
Romain Guy70ca14e2010-12-13 18:24:33 -08001929///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001930// Drawing
1931///////////////////////////////////////////////////////////////////////////////
1932
Tom Hudson107843d2014-09-08 11:26:26 -04001933void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001934 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1935 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001936 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001937 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001938 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001939 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001940 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001941 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001942 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001943 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001944 }
1945
Tom Hudson984162f2014-10-10 13:38:16 -04001946 DeferredDisplayList deferredList(*mState.currentClipRect());
Chris Craikff785832013-03-08 13:12:16 -08001947 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001948 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001949
1950 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001951 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001952
Tom Hudson107843d2014-09-08 11:26:26 -04001953 deferredList.flush(*this, dirty);
1954 } else {
1955 // Even if there is no drawing command(Ex: invisible),
1956 // it still needs startFrame to clear buffer and start tiling.
1957 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001958 }
1959}
1960
Chris Craikd218a922014-01-02 17:13:34 -08001961void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, const SkPaint* paint) {
Romain Guy886b2752013-01-04 12:26:18 -08001962 int color = paint != NULL ? paint->getColor() : 0;
1963
Romain Guya168d732011-03-18 16:50:13 -07001964 float x = left;
1965 float y = top;
1966
Romain Guy886b2752013-01-04 12:26:18 -08001967 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1968
Romain Guya168d732011-03-18 16:50:13 -07001969 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001970 if (currentTransform()->isPureTranslate()) {
1971 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1972 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001973 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001974
1975 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001976 } else {
Chris Craik678625242014-02-28 12:26:34 -08001977 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001978 }
1979
Romain Guy3b748a42013-04-17 18:54:38 -07001980 // No need to check for a UV mapper on the texture object, only ARGB_8888
1981 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001982 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001983 paint, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy3b748a42013-04-17 18:54:38 -07001984 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001985}
1986
Romain Guy03c00b52013-06-20 18:30:28 -07001987/**
1988 * Important note: this method is intended to draw batches of bitmaps and
1989 * will not set the scissor enable or dirty the current layer, if any.
1990 * The caller is responsible for properly dirtying the current layer.
1991 */
Tom Hudson107843d2014-09-08 11:26:26 -04001992void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001993 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1994 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001995 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001996 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001997 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001998
Chris Craik527a3aa2013-03-04 10:19:31 -08001999 const AutoTexture autoCleanup(texture);
2000
Chris Craik527a3aa2013-03-04 10:19:31 -08002001 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002002 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002003
2004 const float x = (int) floorf(bounds.left + 0.5f);
2005 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04002006 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002007 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002008 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002009 GL_TRIANGLES, bitmapCount * 6, true,
2010 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002011 } else {
2012 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002013 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002014 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2015 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002016 }
2017
Tom Hudson107843d2014-09-08 11:26:26 -04002018 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08002019}
2020
Tom Hudson107843d2014-09-08 11:26:26 -04002021void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002022 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002023 return;
Romain Guy6926c722010-07-12 20:20:03 -07002024 }
2025
Romain Guya1d3c912011-12-13 14:55:06 -08002026 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002027 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002028 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002029 const AutoTexture autoCleanup(texture);
2030
Mike Reed1103b322014-07-08 12:36:44 -04002031 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002032 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002033 } else {
Chris Craik79647502014-08-06 13:42:24 -07002034 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002035 }
Chet Haase48659092012-05-31 15:21:51 -07002036
Tom Hudson107843d2014-09-08 11:26:26 -04002037 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002038}
2039
Tom Hudson107843d2014-09-08 11:26:26 -04002040void OpenGLRenderer::drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002041 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002042 return;
Romain Guye651cc62012-05-14 19:44:40 -07002043 }
2044
2045 mCaches.activeTexture(0);
2046 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2047 const AutoTexture autoCleanup(texture);
2048
Mike Reed1103b322014-07-08 12:36:44 -04002049 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002050 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002051 } else {
Chris Craik79647502014-08-06 13:42:24 -07002052 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002053 }
Chet Haase48659092012-05-31 15:21:51 -07002054
Tom Hudson107843d2014-09-08 11:26:26 -04002055 mDirty = true;
Romain Guye651cc62012-05-14 19:44:40 -07002056}
2057
Tom Hudson107843d2014-09-08 11:26:26 -04002058void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002059 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002060 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002061 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002062 }
2063
Chris Craik39a908c2013-06-13 14:39:01 -07002064 // TODO: use quickReject on bounds from vertices
2065 mCaches.enableScissor();
2066
Romain Guyb18d2d02011-02-10 15:52:54 -08002067 float left = FLT_MAX;
2068 float top = FLT_MAX;
2069 float right = FLT_MIN;
2070 float bottom = FLT_MIN;
2071
Romain Guya92bb4d2012-10-16 11:08:44 -07002072 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002073
Chris Craik564acf72014-01-02 16:46:18 -08002074 Vector<ColorTextureVertex> mesh; // TODO: use C++11 unique_ptr
2075 mesh.setCapacity(count);
2076 ColorTextureVertex* vertex = mesh.editArray();
Romain Guyff316ec2013-02-13 18:39:43 -08002077
2078 bool cleanupColors = false;
2079 if (!colors) {
2080 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craikd218a922014-01-02 17:13:34 -08002081 int* newColors = new int[colorsCount];
2082 memset(newColors, 0xff, colorsCount * sizeof(int));
2083 colors = newColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002084 cleanupColors = true;
2085 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002086
Romain Guy3b748a42013-04-17 18:54:38 -07002087 mCaches.activeTexture(0);
2088 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2089 const UvMapper& mapper(getMapper(texture));
2090
Romain Guy5a7b4662011-01-20 19:09:30 -08002091 for (int32_t y = 0; y < meshHeight; y++) {
2092 for (int32_t x = 0; x < meshWidth; x++) {
2093 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2094
2095 float u1 = float(x) / meshWidth;
2096 float u2 = float(x + 1) / meshWidth;
2097 float v1 = float(y) / meshHeight;
2098 float v2 = float(y + 1) / meshHeight;
2099
Romain Guy3b748a42013-04-17 18:54:38 -07002100 mapper.map(u1, v1, u2, v2);
2101
Romain Guy5a7b4662011-01-20 19:09:30 -08002102 int ax = i + (meshWidth + 1) * 2;
2103 int ay = ax + 1;
2104 int bx = i;
2105 int by = bx + 1;
2106 int cx = i + 2;
2107 int cy = cx + 1;
2108 int dx = i + (meshWidth + 1) * 2 + 2;
2109 int dy = dx + 1;
2110
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[ax], vertices[ay], u1, v2, colors[ax / 2]);
2113 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002114
Romain Guyff316ec2013-02-13 18:39:43 -08002115 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2116 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2117 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002118
Romain Guya92bb4d2012-10-16 11:08:44 -07002119 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2120 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2121 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2122 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002123 }
2124 }
2125
Chris Craikf0a59072013-11-19 18:00:46 -08002126 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002127 if (cleanupColors) delete[] colors;
Tom Hudson107843d2014-09-08 11:26:26 -04002128 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002129 }
2130
Romain Guyff316ec2013-02-13 18:39:43 -08002131 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002132 texture = mCaches.textureCache.get(bitmap);
2133 if (!texture) {
2134 if (cleanupColors) delete[] colors;
Tom Hudson107843d2014-09-08 11:26:26 -04002135 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002136 }
Romain Guyff316ec2013-02-13 18:39:43 -08002137 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002138 const AutoTexture autoCleanup(texture);
2139
2140 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002141 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002142
2143 int alpha;
2144 SkXfermode::Mode mode;
2145 getAlphaAndMode(paint, &alpha, &mode);
2146
Romain Guyff316ec2013-02-13 18:39:43 -08002147 float a = alpha / 255.0f;
2148
Romain Guya92bb4d2012-10-16 11:08:44 -07002149 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002150 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002151 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002152
Romain Guyff316ec2013-02-13 18:39:43 -08002153 setupDraw();
2154 setupDrawWithTextureAndColor();
2155 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002156 setupDrawColorFilter(getColorFilter(paint));
2157 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002158 setupDrawProgram();
2159 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002160 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002161 setupDrawTexture(texture->id);
2162 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002163 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002164 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002165
2166 glDrawArrays(GL_TRIANGLES, 0, count);
2167
Romain Guyff316ec2013-02-13 18:39:43 -08002168 int slot = mCaches.currentProgram->getAttrib("colors");
2169 if (slot >= 0) {
2170 glDisableVertexAttribArray(slot);
2171 }
2172
2173 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002174
Tom Hudson107843d2014-09-08 11:26:26 -04002175 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002176}
2177
Tom Hudson107843d2014-09-08 11:26:26 -04002178void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002179 float srcLeft, float srcTop, float srcRight, float srcBottom,
2180 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002181 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002182 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002183 return;
Romain Guy6926c722010-07-12 20:20:03 -07002184 }
2185
Romain Guya1d3c912011-12-13 14:55:06 -08002186 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002187 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002188 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002189 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002190
Romain Guy8ba548f2010-06-30 19:21:21 -07002191 const float width = texture->width;
2192 const float height = texture->height;
2193
Romain Guy3b748a42013-04-17 18:54:38 -07002194 float u1 = fmax(0.0f, srcLeft / width);
2195 float v1 = fmax(0.0f, srcTop / height);
2196 float u2 = fmin(1.0f, srcRight / width);
2197 float v2 = fmin(1.0f, srcBottom / height);
2198
2199 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002200
Romain Guy03750a02010-10-18 14:06:08 -07002201 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002202 resetDrawTextureTexCoords(u1, v1, u2, v2);
2203
Romain Guyd21b6e12011-11-30 20:21:23 -08002204 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2205
Romain Guy886b2752013-01-04 12:26:18 -08002206 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2207 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002208
Romain Guy886b2752013-01-04 12:26:18 -08002209 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2210 // Apply a scale transform on the canvas only when a shader is in use
2211 // Skia handles the ratio between the dst and src rects as a scale factor
2212 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002213 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002214 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002215
Chris Craikd6b65f62014-01-01 14:45:21 -08002216 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2217 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2218 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002219
2220 dstRight = x + (dstRight - dstLeft);
2221 dstBottom = y + (dstBottom - dstTop);
2222
2223 dstLeft = x;
2224 dstTop = y;
2225
Chris Craik678625242014-02-28 12:26:34 -08002226 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002227 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002228 } else {
Chris Craik678625242014-02-28 12:26:34 -08002229 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002230 }
2231
2232 if (CC_UNLIKELY(useScaleTransform)) {
2233 save(SkCanvas::kMatrix_SaveFlag);
2234 translate(dstLeft, dstTop);
2235 scale(scaleX, scaleY);
2236
2237 dstLeft = 0.0f;
2238 dstTop = 0.0f;
2239
2240 dstRight = srcRight - srcLeft;
2241 dstBottom = srcBottom - srcTop;
2242 }
2243
Mike Reed1103b322014-07-08 12:36:44 -04002244 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002245 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002246 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002247 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002248 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2249 } else {
2250 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002251 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002252 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002253 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2254 }
2255
2256 if (CC_UNLIKELY(useScaleTransform)) {
2257 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002258 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002259
2260 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002261
Tom Hudson107843d2014-09-08 11:26:26 -04002262 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002263}
2264
Tom Hudson107843d2014-09-08 11:26:26 -04002265void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002266 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002267 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002268 return;
Romain Guy6926c722010-07-12 20:20:03 -07002269 }
2270
Romain Guy4c2547f2013-06-11 16:19:24 -07002271 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002272 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2273 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002274
Tom Hudson107843d2014-09-08 11:26:26 -04002275 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002276}
2277
Tom Hudson107843d2014-09-08 11:26:26 -04002278void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002279 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2280 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002281 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002282 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002283 }
2284
Romain Guy211370f2012-02-01 16:10:55 -08002285 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002286 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002287 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002288 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002289 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002290
Romain Guya4adcf02013-02-28 12:15:35 -08002291 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2292 texture->setFilter(GL_LINEAR, true);
2293
Chris Craikd6b65f62014-01-01 14:45:21 -08002294 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002295 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002296 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002297 const float offsetX = left + currentTransform()->getTranslateX();
2298 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002299 const size_t count = mesh->quads.size();
2300 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002301 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002302 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002303 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2304 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2305 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002306 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002307 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002308 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002309 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002310 }
2311 }
2312
Chris Craik4063a0e2013-11-15 16:06:56 -08002313 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002314 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002315 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2316 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002317
Romain Guy3b748a42013-04-17 18:54:38 -07002318 right = x + right - left;
2319 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002320 left = x;
2321 top = y;
2322 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002323 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002324 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2325 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002326 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2327 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002328 }
Chet Haase48659092012-05-31 15:21:51 -07002329
Tom Hudson107843d2014-09-08 11:26:26 -04002330 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002331}
2332
Romain Guy03c00b52013-06-20 18:30:28 -07002333/**
2334 * Important note: this method is intended to draw batches of 9-patch objects and
2335 * will not set the scissor enable or dirty the current layer, if any.
2336 * The caller is responsible for properly dirtying the current layer.
2337 */
Tom Hudson107843d2014-09-08 11:26:26 -04002338void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002339 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002340 mCaches.activeTexture(0);
2341 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002342 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002343 const AutoTexture autoCleanup(texture);
2344
2345 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2346 texture->setFilter(GL_LINEAR, true);
2347
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002348 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2349 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002350 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002351
Tom Hudson107843d2014-09-08 11:26:26 -04002352 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002353}
2354
Tom Hudson107843d2014-09-08 11:26:26 -04002355void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002356 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002357 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002358 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002359 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002360 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002361 }
2362
Chris Craik0d5ac952014-07-15 13:01:02 -07002363 Rect bounds(vertexBuffer.getBounds());
2364 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002365 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2366
Chris Craikcb4d6002012-09-25 12:00:29 -07002367 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002368 bool isAA = paint->isAntiAlias();
2369
Chris Craik710f46d2012-09-17 17:25:49 -07002370 setupDraw();
2371 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002372 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Tom Hudson984162f2014-10-10 13:38:16 -04002373 setupDrawColor(color, ((color >> 24) & 0xFF) * writableSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002374 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002375 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002376 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002377 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002378 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2379 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002380 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002381 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002382 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002383
ztenghui55bfb4e2013-12-03 10:38:55 -08002384 const void* vertices = vertexBuffer.getBuffer();
Chris Craik710f46d2012-09-17 17:25:49 -07002385 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002386 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002387 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002388
Chris Craik710f46d2012-09-17 17:25:49 -07002389 int alphaSlot = -1;
2390 if (isAA) {
2391 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2392 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002393 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002394 glEnableVertexAttribArray(alphaSlot);
2395 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002396 }
Romain Guy04299382012-07-18 17:15:41 -07002397
Chris Craik05f3d6e2014-06-02 16:27:04 -07002398 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2399 if (mode == VertexBuffer::kStandard) {
ztenghui63d41ab2014-02-14 13:13:41 -08002400 mCaches.unbindIndicesBuffer();
2401 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002402 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002403 mCaches.bindShadowIndicesBuffer();
ztenghui50ecf842014-03-11 16:52:30 -07002404 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002405 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
ztenghui50ecf842014-03-11 16:52:30 -07002406 mCaches.bindShadowIndicesBuffer();
2407 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
ztenghuid5e8ade2014-08-13 15:48:02 -07002408 } else if (mode == VertexBuffer::kIndices) {
2409 mCaches.unbindIndicesBuffer();
2410 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(), GL_UNSIGNED_SHORT,
2411 vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002412 }
Romain Guy04299382012-07-18 17:15:41 -07002413
Chris Craik710f46d2012-09-17 17:25:49 -07002414 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002415 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002416 }
Chris Craik65cd6122012-12-10 17:56:27 -08002417
Tom Hudson107843d2014-09-08 11:26:26 -04002418 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002419}
2420
2421/**
Chris Craik65cd6122012-12-10 17:56:27 -08002422 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2423 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2424 * screen space in all directions. However, instead of using a fragment shader to compute the
2425 * translucency of the color from its position, we simply use a varying parameter to define how far
2426 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2427 *
2428 * Doesn't yet support joins, caps, or path effects.
2429 */
Tom Hudson107843d2014-09-08 11:26:26 -04002430void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002431 VertexBuffer vertexBuffer;
2432 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002433 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002434 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002435}
2436
2437/**
2438 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2439 * and additional geometry for defining an alpha slope perimeter.
2440 *
2441 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2442 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2443 * in-shader alpha region, but found it to be taxing on some GPUs.
2444 *
2445 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2446 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002447 */
Tom Hudson107843d2014-09-08 11:26:26 -04002448void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002449 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002450
Chris Craik65cd6122012-12-10 17:56:27 -08002451 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002452
Chris Craik65cd6122012-12-10 17:56:27 -08002453 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002454 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2455 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002456
Chris Craik05f3d6e2014-06-02 16:27:04 -07002457 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002458 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002459 }
2460
Chris Craikbf759452014-08-11 16:00:44 -07002461 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002462 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002463}
2464
Tom Hudson107843d2014-09-08 11:26:26 -04002465void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002466 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002467
Chris Craik6d29c8d2013-05-08 18:35:44 -07002468 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002469
Chris Craik6d29c8d2013-05-08 18:35:44 -07002470 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002471 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002472
Chris Craik05f3d6e2014-06-02 16:27:04 -07002473 const Rect& bounds = buffer.getBounds();
2474 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002475 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002476 }
2477
Chris Craikbf759452014-08-11 16:00:44 -07002478 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002479 drawVertexBuffer(buffer, paint, displayFlags);
2480
2481 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002482}
2483
Tom Hudson107843d2014-09-08 11:26:26 -04002484void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002485 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002486 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002487
Tom Hudson984162f2014-10-10 13:38:16 -04002488 Rect clip(*mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002489 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002490
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002491 SkPaint paint;
2492 paint.setColor(color);
2493 paint.setXfermodeMode(mode);
2494
2495 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002496
Tom Hudson107843d2014-09-08 11:26:26 -04002497 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002498}
Romain Guy9d5316e2010-06-24 19:30:36 -07002499
Tom Hudson107843d2014-09-08 11:26:26 -04002500void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002501 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002502 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002503 const AutoTexture autoCleanup(texture);
2504
2505 const float x = left + texture->left - texture->offset;
2506 const float y = top + texture->top - texture->offset;
2507
2508 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002509
Tom Hudson107843d2014-09-08 11:26:26 -04002510 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002511}
2512
Tom Hudson107843d2014-09-08 11:26:26 -04002513void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002514 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002515 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002516 || quickRejectSetupScissor(left, top, right, bottom, p)
2517 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002518 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002519 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002520
Chris Craikcb4d6002012-09-25 12:00:29 -07002521 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002522 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002523 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002524 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002525 drawShape(left, top, texture, p);
2526 } else {
2527 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2528 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2529 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002530 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002531}
2532
Tom Hudson107843d2014-09-08 11:26:26 -04002533void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002534 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002535 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
2536 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002537 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002538 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002539 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002540 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002541 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002542 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002543 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002544 SkPath path;
2545 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2546 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2547 } else {
2548 path.addCircle(x, y, radius);
2549 }
2550 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002551 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002552}
Romain Guy01d58e42011-01-19 21:54:02 -08002553
Tom Hudson107843d2014-09-08 11:26:26 -04002554void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002555 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002556 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002557 || quickRejectSetupScissor(left, top, right, bottom, p)
2558 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002559 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002560 }
Romain Guy01d58e42011-01-19 21:54:02 -08002561
Chris Craikcb4d6002012-09-25 12:00:29 -07002562 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002563 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002564 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002565 drawShape(left, top, texture, p);
2566 } else {
2567 SkPath path;
2568 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2569 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2570 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2571 }
2572 path.addOval(rect);
2573 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002574 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002575}
2576
Tom Hudson107843d2014-09-08 11:26:26 -04002577void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002578 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002579 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002580 || quickRejectSetupScissor(left, top, right, bottom, p)
2581 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002582 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002583 }
2584
Chris Craik780c1282012-10-04 14:10:49 -07002585 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002586 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002587 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002588 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002589 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002590 drawShape(left, top, texture, p);
2591 return;
Chris Craik780c1282012-10-04 14:10:49 -07002592 }
Chris Craik780c1282012-10-04 14:10:49 -07002593 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2594 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2595 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2596 }
2597
2598 SkPath path;
2599 if (useCenter) {
2600 path.moveTo(rect.centerX(), rect.centerY());
2601 }
2602 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2603 if (useCenter) {
2604 path.close();
2605 }
Tom Hudson107843d2014-09-08 11:26:26 -04002606 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002607}
2608
Romain Guycf8675e2012-10-02 12:32:25 -07002609// See SkPaintDefaults.h
2610#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2611
Tom Hudson107843d2014-09-08 11:26:26 -04002612void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002613 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002614 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002615 || quickRejectSetupScissor(left, top, right, bottom, p)
2616 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002617 return;
Romain Guy6926c722010-07-12 20:20:03 -07002618 }
2619
Chris Craik710f46d2012-09-17 17:25:49 -07002620 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002621 // only fill style is supported by drawConvexPath, since others have to handle joins
2622 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2623 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2624 mCaches.activeTexture(0);
2625 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002626 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002627 drawShape(left, top, texture, p);
2628 } else {
2629 SkPath path;
2630 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2631 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2632 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2633 }
2634 path.addRect(rect);
2635 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002636 }
Chet Haase858aa932011-05-12 09:06:00 -07002637 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002638 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2639 SkPath path;
2640 path.addRect(left, top, right, bottom);
2641 drawConvexPath(path, p);
2642 } else {
2643 drawColorRect(left, top, right, bottom, p);
2644
2645 mDirty = true;
2646 }
Chet Haase858aa932011-05-12 09:06:00 -07002647 }
Romain Guyc7d53492010-06-25 13:41:57 -07002648}
Romain Guy9d5316e2010-06-24 19:30:36 -07002649
Chris Craikd218a922014-01-02 17:13:34 -08002650void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2651 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002652 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002653 mCaches.activeTexture(0);
2654
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002655 TextShadow textShadow;
2656 if (!getTextShadow(paint, &textShadow)) {
2657 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2658 }
2659
Raph Levien416a8472012-07-19 22:48:17 -07002660 // NOTE: The drop shadow will not perform gamma correction
2661 // if shader-based correction is enabled
2662 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2663 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002664 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002665 // If the drop shadow exceeds the max texture size or couldn't be
2666 // allocated, skip drawing
2667 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002668 const AutoTexture autoCleanup(shadow);
2669
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002670 const float sx = x - shadow->left + textShadow.dx;
2671 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002672
Tom Hudson984162f2014-10-10 13:38:16 -04002673 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002674 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002675 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002676 }
2677
2678 setupDraw();
2679 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002680 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002681 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002682 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002683 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002684 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002685 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2686 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002687 setupDrawTexture(shadow->id);
2688 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002689 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002690 setupDrawShaderUniforms(getShader(paint));
Raph Levien416a8472012-07-19 22:48:17 -07002691 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2692
2693 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2694}
2695
Romain Guy768bffc2013-02-27 13:50:45 -08002696bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002697 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Romain Guy768bffc2013-02-27 13:50:45 -08002698 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2699}
2700
Tom Hudson107843d2014-09-08 11:26:26 -04002701void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002702 const float* positions, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002703 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002704 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002705 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002706
Romain Guy671d6cf2012-01-18 12:39:17 -08002707 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002708 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002709 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002710 }
2711
Chris Craik39a908c2013-06-13 14:39:01 -07002712 mCaches.enableScissor();
2713
Romain Guy671d6cf2012-01-18 12:39:17 -08002714 float x = 0.0f;
2715 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002716 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002717 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002718 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2719 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002720 }
2721
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002722 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002723 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002724
2725 int alpha;
2726 SkXfermode::Mode mode;
2727 getAlphaAndMode(paint, &alpha, &mode);
2728
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002729 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002730 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002731 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002732 }
2733
Romain Guy671d6cf2012-01-18 12:39:17 -08002734 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002735 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002736 if (pureTranslate && !linearFilter) {
2737 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2738 }
Romain Guy257ae352013-03-20 16:31:12 -07002739 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002740
Tom Hudson984162f2014-10-10 13:38:16 -04002741 const Rect* clip = pureTranslate ? writableSnapshot()->clipRect : &writableSnapshot()->getLocalClip();
Romain Guy671d6cf2012-01-18 12:39:17 -08002742 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2743
Romain Guy211370f2012-02-01 16:10:55 -08002744 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002745
Victoria Lease1e546812013-06-25 14:25:17 -07002746 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002747 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002748 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002749 if (hasActiveLayer) {
2750 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002751 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002752 }
2753 dirtyLayerUnchecked(bounds, getRegion());
2754 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002755 }
Chet Haase48659092012-05-31 15:21:51 -07002756
Tom Hudson107843d2014-09-08 11:26:26 -04002757 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002758}
2759
Chris Craik59744b72014-07-01 17:56:52 -07002760bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002761 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002762 outMatrix->setIdentity();
2763 return false;
2764 } else if (CC_UNLIKELY(transform.isPerspective())) {
2765 outMatrix->setIdentity();
2766 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002767 }
Chris Craik59744b72014-07-01 17:56:52 -07002768
2769 /**
Chris Craika736cd92014-08-04 13:18:38 -07002770 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2771 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002772 */
2773 float sx, sy;
2774 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002775 outMatrix->setScale(
2776 roundf(fmaxf(1.0f, sx)),
2777 roundf(fmaxf(1.0f, sy)));
2778 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002779}
2780
Tom Hudson984162f2014-10-10 13:38:16 -04002781int OpenGLRenderer::getSaveCount() const {
2782 return mState.getSaveCount();
2783}
2784
2785int OpenGLRenderer::save(int flags) {
2786 return mState.save(flags);
2787}
2788
2789void OpenGLRenderer::restore() {
2790 return mState.restore();
2791}
2792
2793void OpenGLRenderer::restoreToCount(int saveCount) {
2794 return mState.restoreToCount(saveCount);
2795}
2796
2797void OpenGLRenderer::translate(float dx, float dy, float dz) {
2798 return mState.translate(dx, dy, dz);
2799}
2800
2801void OpenGLRenderer::rotate(float degrees) {
2802 return mState.rotate(degrees);
2803}
2804
2805void OpenGLRenderer::scale(float sx, float sy) {
2806 return mState.scale(sx, sy);
2807}
2808
2809void OpenGLRenderer::skew(float sx, float sy) {
2810 return mState.skew(sx, sy);
2811}
2812
2813void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2814 mState.setMatrix(matrix);
2815}
2816
2817void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2818 mState.concatMatrix(matrix);
2819}
2820
2821bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2822 return mState.clipRect(left, top, right, bottom, op);
2823}
2824
2825bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2826 return mState.clipPath(path, op);
2827}
2828
2829bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2830 return mState.clipRegion(region, op);
2831}
2832
2833void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2834 mState.setClippingOutline(allocator, outline);
2835}
2836
2837void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2838 const Rect& rect, float radius, bool highPriority) {
2839 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2840}
2841
2842
2843
Tom Hudson107843d2014-09-08 11:26:26 -04002844void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002845 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002846 DrawOpMode drawOpMode) {
2847
Chris Craik527a3aa2013-03-04 10:19:31 -08002848 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002849 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2850 // drawing as ops from DeferredDisplayList are already filtered for these
Tom Hudson984162f2014-10-10 13:38:16 -04002851 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002852 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002853 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002854 }
Romain Guycac5fd32011-12-01 20:08:50 -08002855 }
2856
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002857 const float oldX = x;
2858 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002859
Chris Craikd6b65f62014-01-01 14:45:21 -08002860 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002861 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002862
Romain Guy211370f2012-02-01 16:10:55 -08002863 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002864 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2865 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002866 }
2867
Romain Guy86568192010-12-14 15:55:39 -08002868 int alpha;
2869 SkXfermode::Mode mode;
2870 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002871
Romain Guyc74f45a2013-02-26 19:10:14 -08002872 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2873
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002874 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002875 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002876 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002877 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002878 }
2879
Romain Guy19d4dd82013-03-04 11:14:26 -08002880 const bool hasActiveLayer = hasLayer();
2881
Romain Guy624234f2013-03-05 16:43:31 -08002882 // We only pass a partial transform to the font renderer. That partial
2883 // matrix defines how glyphs are rasterized. Typically we want glyphs
2884 // to be rasterized at their final size on screen, which means the partial
2885 // matrix needs to take the scale factor into account.
2886 // When a partial matrix is used to transform glyphs during rasterization,
2887 // the mesh is generated with the inverse transform (in the case of scale,
2888 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2889 // apply the full transform matrix at draw time in the vertex shader.
2890 // Applying the full matrix in the shader is the easiest way to handle
2891 // rotation and perspective and allows us to always generated quads in the
2892 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002893 SkMatrix fontTransform;
2894 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2895 || fabs(y - (int) y) > 0.0f
2896 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002897 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002898 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002899
Romain Guy624234f2013-03-05 16:43:31 -08002900 // TODO: Implement better clipping for scaled/rotated text
Tom Hudson984162f2014-10-10 13:38:16 -04002901 const Rect* clip = !pureTranslate ? NULL : mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002902 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 -07002903
Raph Levien996e57c2012-07-23 15:22:52 -07002904 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002905 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002906
2907 // don't call issuedrawcommand, do it at end of batch
2908 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002909 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002910 SkPaint paintCopy(*paint);
2911 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2912 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002913 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002914 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002915 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002916 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002917 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002918
Chris Craik527a3aa2013-03-04 10:19:31 -08002919 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002920 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002921 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002922 }
Chris Craik41541822013-05-03 16:35:54 -07002923 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002924 }
Romain Guy694b5192010-07-21 21:33:20 -07002925
Chris Craike63f7c622013-10-17 10:30:55 -07002926 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002927
Tom Hudson107843d2014-09-08 11:26:26 -04002928 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002929}
2930
Tom Hudson107843d2014-09-08 11:26:26 -04002931void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002932 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002933 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002934 return;
Romain Guy03d58522012-02-24 17:54:07 -08002935 }
2936
Chris Craik39a908c2013-06-13 14:39:01 -07002937 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2938 mCaches.enableScissor();
2939
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002940 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002941 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002942 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002943
2944 int alpha;
2945 SkXfermode::Mode mode;
2946 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002947 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002948
Tom Hudson984162f2014-10-10 13:38:16 -04002949 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002950 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 -08002951
Romain Guy97771732012-02-28 18:17:02 -08002952 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002953
2954 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07002955 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002956 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002957 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002958 dirtyLayerUnchecked(bounds, getRegion());
2959 }
Romain Guy97771732012-02-28 18:17:02 -08002960 }
Chet Haase48659092012-05-31 15:21:51 -07002961
Tom Hudson107843d2014-09-08 11:26:26 -04002962 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002963}
2964
Tom Hudson107843d2014-09-08 11:26:26 -04002965void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002966 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002967
Romain Guya1d3c912011-12-13 14:55:06 -08002968 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002969
Romain Guyfb8b7632010-08-23 21:05:08 -07002970 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002971 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002972 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002973
Romain Guy8b55f372010-08-18 17:10:07 -07002974 const float x = texture->left - texture->offset;
2975 const float y = texture->top - texture->offset;
2976
Romain Guy01d58e42011-01-19 21:54:02 -08002977 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002978 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002979}
2980
Tom Hudson107843d2014-09-08 11:26:26 -04002981void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002982 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002983 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002984 }
2985
Romain Guyb2e2f242012-10-17 18:18:35 -07002986 mat4* transform = NULL;
2987 if (layer->isTextureLayer()) {
2988 transform = &layer->getTransform();
2989 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002990 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002991 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002992 }
2993 }
2994
Chris Craik39a908c2013-06-13 14:39:01 -07002995 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04002996 const bool rejected = mState.calculateQuickRejectForScissor(x, y,
Chris Craikdeeda3d2014-05-05 19:09:33 -07002997 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, NULL, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002998
2999 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003000 if (transform && !transform->isIdentity()) {
3001 restore();
3002 }
Tom Hudson107843d2014-09-08 11:26:26 -04003003 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08003004 }
3005
Chris Craik62d307c2014-07-29 10:35:13 -07003006 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
3007 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
3008
Romain Guy5bb3c732012-11-29 17:52:58 -08003009 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003010
Chris Craik39a908c2013-06-13 14:39:01 -07003011 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003012 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003013
Romain Guy211370f2012-02-01 16:10:55 -08003014 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08003015 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003016 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3017 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003018 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003019
Chris Craik16ecda52013-03-29 10:59:59 -07003020 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003021 setupDraw();
3022 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003023 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003024 setupDrawColorFilter(layer->getColorFilter());
3025 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003026 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003027 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003028 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07003029 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08003030 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3031 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3032 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003033
Romain Guyd21b6e12011-11-30 20:21:23 -08003034 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003035 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003036 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003037 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003038 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003039 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003040 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3041 }
Romain Guyf219da52011-01-16 12:54:25 -08003042
Romain Guy448455f2013-07-22 13:57:50 -07003043 TextureVertex* mesh = &layer->mesh[0];
3044 GLsizei elementsCount = layer->meshElementCount;
3045
3046 while (elementsCount > 0) {
3047 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3048
Romain Guy3380cfd2013-08-15 16:57:57 -07003049 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003050 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3051 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3052
3053 elementsCount -= drawCount;
3054 // Though there are 4 vertices in a quad, we use 6 indices per
3055 // quad to draw with GL_TRIANGLES
3056 mesh += (drawCount / 6) * 4;
3057 }
Romain Guyf219da52011-01-16 12:54:25 -08003058
Romain Guy3a3133d2011-02-01 22:59:58 -08003059#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003060 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003061#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003062 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003063
Romain Guy5bb3c732012-11-29 17:52:58 -08003064 if (layer->debugDrawUpdate) {
3065 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003066
3067 SkPaint paint;
3068 paint.setColor(0x7f00ff00);
3069 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003070 }
Romain Guyf219da52011-01-16 12:54:25 -08003071 }
Chris Craik34416ea2013-04-15 16:08:28 -07003072 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003073
Romain Guyb2e2f242012-10-17 18:18:35 -07003074 if (transform && !transform->isIdentity()) {
3075 restore();
3076 }
3077
Tom Hudson107843d2014-09-08 11:26:26 -04003078 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003079}
3080
Romain Guy6926c722010-07-12 20:20:03 -07003081///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003082// Draw filters
3083///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003084void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3085 // We should never get here since we apply the draw filter when stashing
3086 // the paints in the DisplayList.
3087 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003088}
3089
3090///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003091// Drawing implementation
3092///////////////////////////////////////////////////////////////////////////////
3093
Chris Craikd218a922014-01-02 17:13:34 -08003094Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
Romain Guy3b748a42013-04-17 18:54:38 -07003095 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3096 if (!texture) {
3097 return mCaches.textureCache.get(bitmap);
3098 }
3099 return texture;
3100}
3101
Romain Guy01d58e42011-01-19 21:54:02 -08003102void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003103 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003104 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003105 return;
3106 }
3107
3108 int alpha;
3109 SkXfermode::Mode mode;
3110 getAlphaAndMode(paint, &alpha, &mode);
3111
3112 setupDraw();
3113 setupDrawWithTexture(true);
3114 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003115 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003116 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003117 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003118 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003119 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3120 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003121 setupDrawTexture(texture->id);
3122 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003123 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003124 setupDrawShaderUniforms(getShader(paint));
Romain Guy01d58e42011-01-19 21:54:02 -08003125 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3126
3127 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003128}
3129
Romain Guyf607bdc2010-09-10 19:20:06 -07003130// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003131#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3132#define kStdUnderline_Offset (1.0f / 9.0f)
3133#define kStdUnderline_Thickness (1.0f / 18.0f)
3134
Chris Craikd218a922014-01-02 17:13:34 -08003135void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3136 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003137 // Handle underline and strike-through
3138 uint32_t flags = paint->getFlags();
3139 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003140 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003141
Romain Guy211370f2012-02-01 16:10:55 -08003142 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003143 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003144 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003145
Raph Levien8b4072d2012-07-30 15:50:00 -07003146 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003147 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003148
Romain Guyf6834472011-01-23 13:32:12 -08003149 int linesCount = 0;
3150 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3151 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3152
3153 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003154 float points[pointsCount];
3155 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003156
3157 if (flags & SkPaint::kUnderlineText_Flag) {
3158 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003159 points[currentPoint++] = left;
3160 points[currentPoint++] = top;
3161 points[currentPoint++] = left + underlineWidth;
3162 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003163 }
3164
3165 if (flags & SkPaint::kStrikeThruText_Flag) {
3166 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003167 points[currentPoint++] = left;
3168 points[currentPoint++] = top;
3169 points[currentPoint++] = left + underlineWidth;
3170 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003171 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003172
Romain Guy726aeba2011-06-01 14:52:00 -07003173 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003174
Romain Guy726aeba2011-06-01 14:52:00 -07003175 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003176 }
3177 }
3178}
3179
Tom Hudson107843d2014-09-08 11:26:26 -04003180void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003181 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003182 return;
Romain Guy672433d2013-01-04 19:05:13 -08003183 }
3184
Tom Hudson107843d2014-09-08 11:26:26 -04003185 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003186}
3187
Chris Craikb79a3e32014-03-11 12:20:17 -07003188static void mapPointFakeZ(Vector3& point, const mat4& transformXY, const mat4& transformZ) {
3189 // map z coordinate with true 3d matrix
3190 point.z = transformZ.mapZ(point);
3191
3192 // map x,y coordinates with draw/Skia matrix
3193 transformXY.mapPoint(point.x, point.y);
3194}
3195
Tom Hudson107843d2014-09-08 11:26:26 -04003196void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003197 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003198 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003199
Chris Craik15a07a22014-01-26 13:43:53 -08003200 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craikf57776b2013-10-25 18:30:17 -07003201 mCaches.enableScissor();
3202
3203 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003204 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003205
ztenghui14a4e352014-08-13 10:44:39 -07003206 // The caller has made sure casterAlpha > 0.
3207 float ambientShadowAlpha = mAmbientShadowAlpha;
3208 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3209 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3210 }
3211 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3212 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003213 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003214 }
ztenghui7b4516e2014-01-07 10:42:55 -08003215
ztenghui14a4e352014-08-13 10:44:39 -07003216 float spotShadowAlpha = mSpotShadowAlpha;
3217 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3218 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3219 }
3220 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3221 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003222 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003223 }
ztenghui7b4516e2014-01-07 10:42:55 -08003224
Tom Hudson107843d2014-09-08 11:26:26 -04003225 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003226}
3227
Tom Hudson107843d2014-09-08 11:26:26 -04003228void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003229 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003230 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003231 return;
Romain Guy3b753822013-03-05 10:27:35 -08003232 }
Romain Guy735738c2012-12-03 12:34:51 -08003233
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003234 int color = paint->getColor();
3235 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003236 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003237 color |= 0x00ffffff;
3238 }
3239
Romain Guy672433d2013-01-04 19:05:13 -08003240 float left = FLT_MAX;
3241 float top = FLT_MAX;
3242 float right = FLT_MIN;
3243 float bottom = FLT_MIN;
3244
Romain Guy448455f2013-07-22 13:57:50 -07003245 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003246 Vertex* vertex = mesh;
3247
Chris Craik2af46352012-11-26 18:30:17 -08003248 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003249 float l = rects[index + 0];
3250 float t = rects[index + 1];
3251 float r = rects[index + 2];
3252 float b = rects[index + 3];
3253
Romain Guy3b753822013-03-05 10:27:35 -08003254 Vertex::set(vertex++, l, t);
3255 Vertex::set(vertex++, r, t);
3256 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003257 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003258
Romain Guy3b753822013-03-05 10:27:35 -08003259 left = fminf(left, l);
3260 top = fminf(top, t);
3261 right = fmaxf(right, r);
3262 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003263 }
3264
Chris Craikf0a59072013-11-19 18:00:46 -08003265 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003266 return;
Romain Guya362c692013-02-04 13:50:16 -08003267 }
Romain Guy672433d2013-01-04 19:05:13 -08003268
Romain Guy672433d2013-01-04 19:05:13 -08003269 setupDraw();
3270 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003271 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003272 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003273 setupDrawColorFilter(getColorFilter(paint));
3274 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003275 setupDrawProgram();
3276 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003277 setupDrawModelView(kModelViewMode_Translate, false,
3278 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003279 setupDrawColorUniforms(getShader(paint));
3280 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003281 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003282
Romain Guy8ce00302013-01-15 18:51:42 -08003283 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003284 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003285 }
3286
Chris Craik4063a0e2013-11-15 16:06:56 -08003287 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003288
Tom Hudson107843d2014-09-08 11:26:26 -04003289 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003290}
3291
Romain Guy026c5e162010-06-28 17:12:22 -07003292void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003293 const SkPaint* paint, bool ignoreTransform) {
3294 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003295 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003296 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003297 color |= 0x00ffffff;
3298 }
3299
Romain Guy70ca14e2010-12-13 18:24:33 -08003300 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003301 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003302 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003303 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003304 setupDrawColorFilter(getColorFilter(paint));
3305 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003306 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003307 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3308 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003309 setupDrawColorUniforms(getShader(paint));
3310 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003311 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003312 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003313
Romain Guyc95c8d62010-09-17 15:31:32 -07003314 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3315}
3316
Romain Guy82ba8142010-07-09 13:25:56 -07003317void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003318 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003319 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003320
Romain Guy3b748a42013-04-17 18:54:38 -07003321 GLvoid* vertices = (GLvoid*) NULL;
3322 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3323
3324 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003325 vertices = &mMeshVertices[0].x;
3326 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003327
3328 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3329 texture->uvMapper->map(uvs);
3330
3331 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3332 }
3333
Chris Craikd6b65f62014-01-01 14:45:21 -08003334 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3335 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3336 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003337
Romain Guyd21b6e12011-11-30 20:21:23 -08003338 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003339 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003340 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003341 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003342 } else {
Chris Craik678625242014-02-28 12:26:34 -08003343 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003344 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003345 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3346 }
3347
3348 if (texture->uvMapper) {
3349 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003350 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003351}
3352
Romain Guyf7f93552010-07-08 19:17:03 -07003353void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003354 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003355 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003356 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3357 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003358
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003359 int a;
3360 SkXfermode::Mode mode;
3361 getAlphaAndMode(paint, &a, &mode);
3362 const float alpha = a / 255.0f;
3363
Romain Guy746b7402010-10-26 16:27:31 -07003364 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003365 setupDrawWithTexture();
3366 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003367 setupDrawColorFilter(getColorFilter(paint));
3368 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003369 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003370 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003371 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003372 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003373 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003374 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003375 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003376
Romain Guy6820ac82010-09-15 18:11:50 -07003377 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003378}
3379
Romain Guy3b748a42013-04-17 18:54:38 -07003380void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003381 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003382 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003383 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3384 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003385
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003386 int a;
3387 SkXfermode::Mode mode;
3388 getAlphaAndMode(paint, &a, &mode);
3389 const float alpha = a / 255.0f;
3390
Romain Guy3b748a42013-04-17 18:54:38 -07003391 setupDraw();
3392 setupDrawWithTexture();
3393 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003394 setupDrawColorFilter(getColorFilter(paint));
3395 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003396 setupDrawProgram();
3397 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003398 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003399 setupDrawTexture(texture);
3400 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003401 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003402 setupDrawMeshIndices(vertices, texCoords, vbo);
3403
3404 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003405}
3406
Romain Guy886b2752013-01-04 12:26:18 -08003407void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003408 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003409 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003410 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003411
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003412 int color = paint != NULL ? paint->getColor() : 0;
3413 int alpha;
3414 SkXfermode::Mode mode;
3415 getAlphaAndMode(paint, &alpha, &mode);
3416
Romain Guy886b2752013-01-04 12:26:18 -08003417 setupDraw();
3418 setupDrawWithTexture(true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003419 if (paint != NULL) {
Romain Guy886b2752013-01-04 12:26:18 -08003420 setupDrawAlpha8Color(color, alpha);
3421 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003422 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003423 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003424 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003425 setupDrawProgram();
3426 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003427 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003428 setupDrawTexture(texture);
3429 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003430 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003431 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003432 setupDrawMesh(vertices, texCoords);
3433
3434 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003435}
3436
Romain Guya5aed0d2010-09-09 14:42:43 -07003437void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003438 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003439
Tom Hudson984162f2014-10-10 13:38:16 -04003440 if (writableSnapshot()->roundRectClipState != NULL /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003441 blend = true;
3442 mDescription.hasRoundRectClip = true;
3443 }
3444 mSkipOutlineClip = true;
3445
Romain Guy82ba8142010-07-09 13:25:56 -07003446 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003447
Romain Guy82ba8142010-07-09 13:25:56 -07003448 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003449 // These blend modes are not supported by OpenGL directly and have
3450 // to be implemented using shaders. Since the shader will perform
3451 // the blending, turn blending off here
3452 // If the blend mode cannot be implemented using shaders, fall
3453 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003454 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003455 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003456 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003457 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003458
Romain Guy82bc7a72012-01-03 14:13:39 -08003459 if (mCaches.blend) {
3460 glDisable(GL_BLEND);
3461 mCaches.blend = false;
3462 }
3463
3464 return;
3465 } else {
3466 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003467 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003468 }
3469
3470 if (!mCaches.blend) {
3471 glEnable(GL_BLEND);
3472 }
3473
3474 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3475 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3476
3477 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3478 glBlendFunc(sourceMode, destMode);
3479 mCaches.lastSrcMode = sourceMode;
3480 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003481 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003482 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003483 glDisable(GL_BLEND);
3484 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003485 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003486}
3487
Romain Guy889f8d12010-07-29 14:37:42 -07003488bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003489 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003490 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003491 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003492 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003493 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003494 }
Romain Guy6926c722010-07-12 20:20:03 -07003495 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003496}
3497
Romain Guy026c5e162010-06-28 17:12:22 -07003498void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003499 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003500 TextureVertex::setUV(v++, u1, v1);
3501 TextureVertex::setUV(v++, u2, v1);
3502 TextureVertex::setUV(v++, u1, v2);
3503 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003504}
3505
Chris Craikd218a922014-01-02 17:13:34 -08003506void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003507 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003508 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3509 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003510 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003511 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003512 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003513}
3514
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003515float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003516 float alpha;
3517 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3518 alpha = mDrawModifiers.mOverrideLayerAlpha;
3519 } else {
3520 alpha = layer->getAlpha() / 255.0f;
3521 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003522 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003523}
3524
Romain Guy9d5316e2010-06-24 19:30:36 -07003525}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003526}; // namespace android