blob: 02fbd89dbc50107d40b05af95662cd94bb665ab4 [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
Chris Craik65fe5ee2015-01-26 18:06:29 -080017#include "OpenGLRenderer.h"
18
19#include "DeferredDisplayList.h"
20#include "DisplayListRenderer.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080021#include "GammaFontRenderer.h"
Chris Craik03188872015-02-02 18:39:33 -080022#include "Glop.h"
23#include "GlopBuilder.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080024#include "Patch.h"
25#include "PathTessellator.h"
26#include "Properties.h"
27#include "RenderNode.h"
Chris Craik03188872015-02-02 18:39:33 -080028#include "renderstate/MeshState.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080029#include "renderstate/RenderState.h"
30#include "ShadowTessellator.h"
31#include "SkiaShader.h"
32#include "Vector.h"
33#include "VertexBuffer.h"
34#include "utils/GLUtils.h"
35#include "utils/PaintUtils.h"
36#include "utils/TraceUtils.h"
37
Romain Guye4d01122010-06-16 18:44:05 -070038#include <stdlib.h>
39#include <stdint.h>
40#include <sys/types.h>
41
Romain Guy5cbbce52010-06-27 22:59:20 -070042#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070043#include <SkColor.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040044#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070045#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070046
Romain Guye4d01122010-06-16 18:44:05 -070047#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070048#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070049
Romain Guy08aa2cb2011-03-17 11:06:57 -070050#include <private/hwui/DrawGlInfo.h>
51
Romain Guy5b3b3522010-10-27 18:57:51 -070052#include <ui/Rect.h>
53
Chris Craik62d307c2014-07-29 10:35:13 -070054#if DEBUG_DETAILED_EVENTS
55 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
56#else
57 #define EVENT_LOGD(...)
58#endif
59
Chris Craik0519c8102015-02-11 13:17:06 -080060#define USE_GLOPS true
61
Romain Guye4d01122010-06-16 18:44:05 -070062namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070063namespace uirenderer {
64
Romain Guy9d5316e2010-06-24 19:30:36 -070065///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -070066// Constructors/destructor
67///////////////////////////////////////////////////////////////////////////////
68
John Reck3b202512014-06-23 13:13:08 -070069OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -040070 : mState(*this)
Chris Craik058fc642014-07-23 18:19:28 -070071 , mCaches(Caches::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -070072 , mRenderState(renderState)
Chris Craik65fe5ee2015-01-26 18:06:29 -080073 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -070074 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -070075 , mSuppressTiling(false)
76 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -040077 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -070078 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -070079 , mLightRadius(FLT_MIN)
80 , mAmbientShadowAlpha(0)
81 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -080082 // *set* draw modifiers to be 0
83 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -070084 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -070085
Chris Craik03188872015-02-02 18:39:33 -080086 memcpy(mMeshVertices, kUnitQuadVertices, sizeof(kUnitQuadVertices));
Romain Guye4d01122010-06-16 18:44:05 -070087}
88
Romain Guy85bf02f2010-06-22 13:11:24 -070089OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -070090 // The context has already been destroyed at this point, do not call
91 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -070092}
93
Romain Guy87e2f7572012-09-24 11:37:12 -070094void OpenGLRenderer::initProperties() {
95 char property[PROPERTY_VALUE_MAX];
96 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
97 mScissorOptimizationDisabled = !strcasecmp(property, "true");
98 INIT_LOGD(" Scissor optimization %s",
99 mScissorOptimizationDisabled ? "disabled" : "enabled");
100 } else {
101 INIT_LOGD(" Scissor optimization enabled");
102 }
Romain Guy13631f32012-01-30 17:41:55 -0800103}
104
Chris Craik058fc642014-07-23 18:19:28 -0700105void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
106 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
107 mLightCenter = lightCenter;
108 mLightRadius = lightRadius;
109 mAmbientShadowAlpha = ambientShadowAlpha;
110 mSpotShadowAlpha = spotShadowAlpha;
111}
112
Romain Guy13631f32012-01-30 17:41:55 -0800113///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700114// Setup
115///////////////////////////////////////////////////////////////////////////////
116
Chris Craik797b95b22014-05-20 18:10:25 -0700117void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700118 glDisable(GL_DITHER);
119 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Chris Craik284b2432014-09-18 16:05:35 -0700120 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700121}
122
Romain Guy96885eb2013-03-26 15:05:58 -0700123void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800124 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800125 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400126 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700127 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700128 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700129}
130
Tom Hudson107843d2014-09-08 11:26:26 -0400131void OpenGLRenderer::startFrame() {
132 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700133 mFrameStarted = true;
134
Tom Hudson984162f2014-10-10 13:38:16 -0400135 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700136
Romain Guy96885eb2013-03-26 15:05:58 -0700137 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700138
Tom Hudson984162f2014-10-10 13:38:16 -0400139 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800140
Romain Guy54c1a642012-09-27 17:55:46 -0700141 // Functors break the tiling extension in pretty spectacular ways
142 // This ensures we don't use tiling when a functor is going to be
143 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700144 mSuppressTiling = mCaches.hasRegisteredFunctors()
145 || mFirstFrameAfterResize;
146 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700147
Chris Craikd6b65f62014-01-01 14:45:21 -0800148 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700149
Romain Guy7c450aa2012-09-21 19:15:00 -0700150 debugOverdraw(true, true);
151
Tom Hudson107843d2014-09-08 11:26:26 -0400152 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700153 mTilingClip.right, mTilingClip.bottom, mOpaque);
154}
155
Tom Hudson107843d2014-09-08 11:26:26 -0400156void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700157 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700158
Romain Guy96885eb2013-03-26 15:05:58 -0700159 setupFrameState(left, top, right, bottom, opaque);
160
161 // Layer renderers will start the frame immediately
162 // The framebuffer renderer will first defer the display list
163 // for each layer and wait until the first drawing command
164 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800165 if (currentSnapshot()->fbo == 0) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800166 mRenderState.blend().syncEnabled();
Romain Guy96885eb2013-03-26 15:05:58 -0700167 updateLayers();
168 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400169 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700170 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700171}
172
Romain Guydcfc8362013-01-03 13:08:57 -0800173void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
174 // If we know that we are going to redraw the entire framebuffer,
175 // perform a discard to let the driver know we don't need to preserve
176 // the back buffer for this frame.
Chris Craik117bdbc2015-02-05 10:12:38 -0800177 if (mCaches.extensions().hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400178 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
Chris Craik6b109c72015-02-27 10:55:28 -0800179 const bool isFbo = getTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800180 const GLenum attachments[] = {
181 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
182 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800183 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
184 }
185}
186
Tom Hudson107843d2014-09-08 11:26:26 -0400187void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700188 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800189 mRenderState.scissor().setEnabled(true);
190 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700191 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400192 mDirty = true;
193 return;
Romain Guyddf74372012-05-22 14:07:07 -0700194 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700195
Chris Craik65fe5ee2015-01-26 18:06:29 -0800196 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700197}
198
henry.uh_chen33f5a592014-07-02 19:36:56 +0800199void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700200 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800201 const Snapshot* snapshot = currentSnapshot();
202
Chris Craik14e51302013-12-30 15:32:54 -0800203 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800204 if (snapshot->flags & Snapshot::kFlagFboTarget) {
205 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700206 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700207
henry.uh_chen33f5a592014-07-02 19:36:56 +0800208 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800209 }
210}
211
henry.uh_chen33f5a592014-07-02 19:36:56 +0800212void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800213 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800214 if(expand) {
215 // Expand the startTiling region by 1
216 int leftNotZero = (clip.left > 0) ? 1 : 0;
217 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
218
219 mCaches.startTiling(
220 clip.left - leftNotZero,
221 windowHeight - clip.bottom - topNotZero,
222 clip.right - clip.left + leftNotZero + 1,
223 clip.bottom - clip.top + topNotZero + 1,
224 opaque);
225 } else {
226 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800227 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800228 }
Romain Guy54c1a642012-09-27 17:55:46 -0700229 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700230}
231
232void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700233 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700234}
235
Tom Hudson107843d2014-09-08 11:26:26 -0400236bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700237 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700238 endTiling();
Chris Craik4ac36f82014-12-09 16:54:03 -0800239 mTempPaths.clear();
240
Romain Guyca89e2a2013-03-08 17:44:20 -0800241 // When finish() is invoked on FBO 0 we've reached the end
242 // of the current frame
Chris Craik6b109c72015-02-27 10:55:28 -0800243 if (getTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800244 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700245 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800246 }
247
Romain Guy11cb6422012-09-21 00:39:43 -0700248 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700249#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700250 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700251#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700252
Romain Guyc15008e2010-11-10 11:59:15 -0800253#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800254 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700255#else
256 if (mCaches.getDebugLevel() & kDebugMemory) {
257 mCaches.dumpMemoryUsage();
258 }
Romain Guyc15008e2010-11-10 11:59:15 -0800259#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700260 }
Romain Guy96885eb2013-03-26 15:05:58 -0700261
262 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400263
264 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700265}
266
Romain Guy35643dd2012-09-18 15:40:58 -0700267void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700268 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
269 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700270 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700271
Chris Craik65fe5ee2015-01-26 18:06:29 -0800272 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700273 dirtyClip();
274}
275
Andreas Gampe64bb4132014-11-22 00:35:09 +0000276void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400277 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700278
Rob Tsuk487a92c2015-01-06 13:22:54 -0800279 Rect clip(mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700280 clip.snapToPixelBoundaries();
281
Romain Guyd643bb52011-03-01 14:55:21 -0800282 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800283 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800284 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800285 dirtyLayerUnchecked(clip, getRegion());
286 }
Romain Guyd643bb52011-03-01 14:55:21 -0800287
Romain Guy08aa2cb2011-03-17 11:06:57 -0700288 DrawGlInfo info;
289 info.clipLeft = clip.left;
290 info.clipTop = clip.top;
291 info.clipRight = clip.right;
292 info.clipBottom = clip.bottom;
293 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700294 info.width = getViewportWidth();
295 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800296 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700297
Tom Hudson984162f2014-10-10 13:38:16 -0400298 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700299 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400300 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700301 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
302 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800303 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700304 setScissorFromClip();
305 }
Chris Craik54f574a2013-08-26 11:23:46 -0700306
John Reck3b202512014-06-23 13:13:08 -0700307 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
308 // Scissor may have been modified, reset dirty clip
309 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800310
Tom Hudson107843d2014-09-08 11:26:26 -0400311 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800312}
313
Romain Guyf6a11b82010-06-23 17:47:49 -0700314///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700315// Debug
316///////////////////////////////////////////////////////////////////////////////
317
Chris Craik62d307c2014-07-29 10:35:13 -0700318void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
319#if DEBUG_DETAILED_EVENTS
320 const int BUFFER_SIZE = 256;
321 va_list ap;
322 char buf[BUFFER_SIZE];
323
324 va_start(ap, fmt);
325 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
326 va_end(ap);
327
328 eventMark(buf);
329#endif
330}
331
332
Romain Guy0f6675332013-03-01 14:31:04 -0800333void OpenGLRenderer::eventMark(const char* name) const {
334 mCaches.eventMark(0, name);
335}
336
Romain Guy87e2f7572012-09-24 11:37:12 -0700337void OpenGLRenderer::startMark(const char* name) const {
338 mCaches.startMark(0, name);
339}
340
341void OpenGLRenderer::endMark() const {
342 mCaches.endMark();
343}
344
345void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700346 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700347}
348
349void OpenGLRenderer::renderOverdraw() {
Chris Craik6b109c72015-02-27 10:55:28 -0800350 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700351 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700352
Chris Craik65fe5ee2015-01-26 18:06:29 -0800353 mRenderState.scissor().setEnabled(true);
354 mRenderState.scissor().set(clip->left,
355 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
356 clip->right - clip->left,
357 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700358
Romain Guy627c6fd2013-08-21 11:53:18 -0700359 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800360 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700361 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
362
363 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800364 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700365 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
366
367 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800368 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700369 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
370
371 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800372 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700373 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
374
Chris Craik96a5c4c2015-01-27 15:46:35 -0800375 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700376 }
377}
378
379///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700380// Layers
381///////////////////////////////////////////////////////////////////////////////
382
383bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700384 if (layer->deferredUpdateScheduled && layer->renderer
385 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700386
Romain Guy7c450aa2012-09-21 19:15:00 -0700387 if (inFrame) {
388 endTiling();
389 debugOverdraw(false, false);
390 }
Romain Guy11cb6422012-09-21 00:39:43 -0700391
Romain Guy96885eb2013-03-26 15:05:58 -0700392 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700393 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700394 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700395 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700396 }
Romain Guy11cb6422012-09-21 00:39:43 -0700397
398 if (inFrame) {
399 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800400 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700401 }
402
Romain Guy5bb3c732012-11-29 17:52:58 -0800403 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700404 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700405
406 return true;
407 }
408
409 return false;
410}
411
412void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700413 // If draw deferring is enabled this method will simply defer
414 // the display list of each individual layer. The layers remain
415 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700416 int count = mLayerUpdates.size();
417 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700418 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
419 startMark("Layer Updates");
420 } else {
421 startMark("Defer Layer Updates");
422 }
Romain Guy11cb6422012-09-21 00:39:43 -0700423
Chris Craik1206b9b2013-04-04 14:46:24 -0700424 // Note: it is very important to update the layers in order
425 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700426 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700427 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700428 }
Romain Guy11cb6422012-09-21 00:39:43 -0700429
Romain Guy96885eb2013-03-26 15:05:58 -0700430 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
431 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800432 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700433 }
434 endMark();
435 }
436}
437
438void OpenGLRenderer::flushLayers() {
439 int count = mLayerUpdates.size();
440 if (count > 0) {
441 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700442
Chris Craik1206b9b2013-04-04 14:46:24 -0700443 // Note: it is very important to update the layers in order
444 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800445 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700446 }
447
448 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800449 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700450
Romain Guy11cb6422012-09-21 00:39:43 -0700451 endMark();
452 }
453}
454
455void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
456 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700457 // Make sure we don't introduce duplicates.
458 // SortedVector would do this automatically but we need to respect
459 // the insertion order. The linear search is not an issue since
460 // this list is usually very short (typically one item, at most a few)
461 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
462 if (mLayerUpdates.itemAt(i) == layer) {
463 return;
464 }
465 }
Romain Guy11cb6422012-09-21 00:39:43 -0700466 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700467 }
468}
469
Romain Guye93482f2013-06-17 13:14:51 -0700470void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
471 if (layer) {
472 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
473 if (mLayerUpdates.itemAt(i) == layer) {
474 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700475 break;
476 }
477 }
478 }
479}
480
Romain Guy40543602013-06-12 15:31:28 -0700481void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800482 ATRACE_NAME("Update HW Layers");
Chris Craik44eb2c02015-01-29 09:45:09 -0800483 mRenderState.blend().syncEnabled();
Romain Guy40543602013-06-12 15:31:28 -0700484 updateLayers();
485 flushLayers();
486 // Wait for all the layer updates to be executed
John Reck55156372015-01-21 07:46:37 -0800487 glFinish();
Romain Guy40543602013-06-12 15:31:28 -0700488}
489
John Reck443a7142014-09-04 17:40:05 -0700490void OpenGLRenderer::markLayersAsBuildLayers() {
491 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
492 mLayerUpdates[i]->wasBuildLayered = true;
493 }
494}
495
Romain Guy11cb6422012-09-21 00:39:43 -0700496///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700497// State management
498///////////////////////////////////////////////////////////////////////////////
499
Chris Craik14e51302013-12-30 15:32:54 -0800500void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700501 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800502 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
503 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700504
Chris Craika64a2be2014-05-14 14:17:01 -0700505 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700506 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700507 }
508
Romain Guy2542d192010-08-18 11:47:12 -0700509 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700510 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700511 }
Romain Guy2542d192010-08-18 11:47:12 -0700512
Romain Guy5ec99242010-11-03 16:19:08 -0700513 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700514 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700515 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700516 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800517 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700518 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700519 }
Romain Guybb9524b2010-06-22 18:56:38 -0700520}
521
Romain Guyf6a11b82010-06-23 17:47:49 -0700522///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700523// Layers
524///////////////////////////////////////////////////////////////////////////////
525
526int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700527 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700528 // force matrix/clip isolation for layer
529 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
530
Tom Hudson984162f2014-10-10 13:38:16 -0400531 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700532
Tom Hudson984162f2014-10-10 13:38:16 -0400533 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700534 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700535 }
Romain Guyd55a8612010-06-28 17:42:46 -0700536
537 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700538}
539
Chris Craikd90144d2013-03-19 15:03:48 -0700540void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
541 const Rect untransformedBounds(bounds);
542
Chris Craikd6b65f62014-01-01 14:45:21 -0800543 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700544
545 // Layers only make sense if they are in the framebuffer's bounds
Rob Tsuk487a92c2015-01-06 13:22:54 -0800546 if (bounds.intersect(mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700547 // We cannot work with sub-pixels in this case
548 bounds.snapToPixelBoundaries();
549
550 // When the layer is not an FBO, we may use glCopyTexImage so we
551 // need to make sure the layer does not extend outside the bounds
552 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700553 const Snapshot& previous = *(currentSnapshot()->previous);
554 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
555 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700556 bounds.setEmpty();
557 } else if (fboLayer) {
558 clip.set(bounds);
559 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800560 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700561 inverse.mapRect(clip);
562 clip.snapToPixelBoundaries();
563 if (clip.intersect(untransformedBounds)) {
564 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
565 bounds.set(untransformedBounds);
566 } else {
567 clip.setEmpty();
568 }
569 }
570 } else {
571 bounds.setEmpty();
572 }
573}
574
Chris Craik408eb122013-03-26 18:55:15 -0700575void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
576 bool fboLayer, int alpha) {
577 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
578 bounds.getHeight() > mCaches.maxTextureSize ||
579 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400580 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700581 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400582 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700583 }
584}
585
Chris Craikd90144d2013-03-19 15:03:48 -0700586int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500587 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400588 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700589
Tom Hudson984162f2014-10-10 13:38:16 -0400590 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700591 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
592 // operations will be able to store and restore the current clip and transform info, and
593 // quick rejection will be correct (for display lists)
594
595 Rect bounds(left, top, right, bottom);
596 Rect clip;
597 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500598 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700599
Tom Hudson984162f2014-10-10 13:38:16 -0400600 if (!mState.currentlyIgnored()) {
601 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
602 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
603 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800604 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700605 }
606 }
607
608 return count;
609}
610
Romain Guy1c740bc2010-09-13 18:00:09 -0700611/**
612 * Layers are viewed by Skia are slightly different than layers in image editing
613 * programs (for instance.) When a layer is created, previously created layers
614 * and the frame buffer still receive every drawing command. For instance, if a
615 * layer is created and a shape intersecting the bounds of the layers and the
616 * framebuffer is draw, the shape will be drawn on both (unless the layer was
617 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
618 *
619 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
620 * texture. Unfortunately, this is inefficient as it requires every primitive to
621 * be drawn n + 1 times, where n is the number of active layers. In practice this
622 * means, for every primitive:
623 * - Switch active frame buffer
624 * - Change viewport, clip and projection matrix
625 * - Issue the drawing
626 *
627 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700628 * To avoid this, layers are implemented in a different way here, at least in the
629 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
630 * is set. When this flag is set we can redirect all drawing operations into a
631 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700632 *
633 * This implementation relies on the frame buffer being at least RGBA 8888. When
634 * a layer is created, only a texture is created, not an FBO. The content of the
635 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700636 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700637 * buffer and drawing continues as normal. This technique therefore treats the
638 * frame buffer as a scratch buffer for the layers.
639 *
640 * To compose the layers back onto the frame buffer, each layer texture
641 * (containing the original frame buffer data) is drawn as a simple quad over
642 * the frame buffer. The trick is that the quad is set as the composition
643 * destination in the blending equation, and the frame buffer becomes the source
644 * of the composition.
645 *
646 * Drawing layers with an alpha value requires an extra step before composition.
647 * An empty quad is drawn over the layer's region in the frame buffer. This quad
648 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
649 * quad is used to multiply the colors in the frame buffer. This is achieved by
650 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
651 * GL_ZERO, GL_SRC_ALPHA.
652 *
653 * Because glCopyTexImage2D() can be slow, an alternative implementation might
654 * be use to draw a single clipped layer. The implementation described above
655 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700656 *
657 * (1) The frame buffer is actually not cleared right away. To allow the GPU
658 * to potentially optimize series of calls to glCopyTexImage2D, the frame
659 * buffer is left untouched until the first drawing operation. Only when
660 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700661 */
Chet Haased48885a2012-08-28 17:43:28 -0700662bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700663 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800664 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
665 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700666
Romain Guyeb993562010-10-05 18:14:38 -0700667 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
668
Romain Guyf607bdc2010-09-10 19:20:06 -0700669 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700670 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700671 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700672 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000673 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700674
675 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400676 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700677 return false;
678 }
Romain Guyf18fd992010-07-08 11:45:51 -0700679
Chris Craik44eb2c02015-01-29 09:45:09 -0800680 mCaches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700681 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700682 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700683 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700684 }
685
Derek Sollenberger674554f2014-02-19 16:47:32 +0000686 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700687 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700688 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
689 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500690
Chet Haasea23eed82012-04-12 15:19:04 -0700691 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700692 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700693 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700694
Romain Guy8fb95422010-08-17 18:38:51 -0700695 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400696 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
697 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700698
Chris Craika8bea8e2014-09-24 11:29:43 -0700699 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
700 fboLayer ? "" : "unclipped ",
701 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700702 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700703 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700704 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700705 } else {
706 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700707 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800708 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700709 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700710 // Workaround for some GL drivers. When reading pixels lying outside
711 // of the window we should get undefined values for those pixels.
712 // Unfortunately some drivers will turn the entire target texture black
713 // when reading outside of the window.
714 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800715 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700716 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800717 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700718
Chris Craika64a2be2014-05-14 14:17:01 -0700719 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
720 bounds.left, getViewportHeight() - bounds.bottom,
721 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700722
Romain Guy54be1cd2011-06-13 19:04:27 -0700723 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800724 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700725 }
Romain Guyeb993562010-10-05 18:14:38 -0700726 }
Romain Guyf86ef572010-07-01 11:05:42 -0700727
Romain Guyd55a8612010-06-28 17:42:46 -0700728 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700729}
730
Chris Craike63f7c622013-10-17 10:30:55 -0700731bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800732 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700733 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700734
Tom Hudson984162f2014-10-10 13:38:16 -0400735 writableSnapshot()->region = &writableSnapshot()->layer->region;
736 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
737 writableSnapshot()->fbo = layer->getFbo();
738 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
739 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
740 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800741 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700742
Romain Guy2b7028e2012-09-19 17:25:38 -0700743 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700744 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700745 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700746 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700747 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700748
749 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700750 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700751 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700752 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700753 }
754
755 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Chris Craikf27133d2015-02-19 09:51:53 -0800756 layer->getTextureId(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700757
henry.uh_chen33f5a592014-07-02 19:36:56 +0800758 // Expand the startTiling region by 1
759 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700760
761 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800762 mRenderState.scissor().setEnabled(true);
763 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700764 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700765 glClear(GL_COLOR_BUFFER_BIT);
766
767 dirtyClip();
768
769 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700770 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700771 return true;
772}
773
Romain Guy1c740bc2010-09-13 18:00:09 -0700774/**
775 * Read the documentation of createLayer() before doing anything in this method.
776 */
Chris Craik14e51302013-12-30 15:32:54 -0800777void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
778 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000779 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700780 return;
781 }
782
Chris Craik14e51302013-12-30 15:32:54 -0800783 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800784 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800785 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700786
Chris Craik39a908c2013-06-13 14:39:01 -0700787 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400788 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800789 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800790 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700791
Romain Guyeb993562010-10-05 18:14:38 -0700792 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700793 endTiling();
794
Romain Guye0aa84b2012-04-03 19:30:26 -0700795 // Detach the texture from the FBO
796 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800797
798 layer->removeFbo(false);
799
Romain Guyeb993562010-10-05 18:14:38 -0700800 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700801 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700802 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700803
Chris Craikd6b65f62014-01-01 14:45:21 -0800804 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700805 }
806
Romain Guy9ace8f52011-07-07 20:50:11 -0700807 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500808 SkPaint layerPaint;
809 layerPaint.setAlpha(layer->getAlpha());
810 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
811 layerPaint.setColorFilter(layer->getColorFilter());
812
813 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700814 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700815 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700816 }
817
Chris Craik96a5c4c2015-01-27 15:46:35 -0800818 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700819
Chris Craik44eb2c02015-01-29 09:45:09 -0800820 mCaches.textureState().activateTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700821
Romain Guy5b3b3522010-10-27 18:57:51 -0700822 // When the layer is stored in an FBO, we can save a bit of fillrate by
823 // drawing only the dirty region
824 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800825 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700826 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700827 } else if (!rect.isEmpty()) {
828 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530829
830 save(0);
831 // the layer contains screen buffer content that shouldn't be alpha modulated
832 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400833 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700834 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530835 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700836 }
Romain Guy8b55f372010-08-18 17:10:07 -0700837
Romain Guy746b7402010-10-26 16:27:31 -0700838 dirtyClip();
839
Romain Guyeb993562010-10-05 18:14:38 -0700840 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800841 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700842 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800843 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800844 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700845 }
846}
847
Romain Guyaa6c24c2011-04-28 18:40:04 -0700848void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craik26bf3422015-02-26 16:28:17 -0800849 if (USE_GLOPS) {
850 bool snap = !layer->getForceFilter()
851 && layer->getWidth() == (uint32_t) rect.getWidth()
852 && layer->getHeight() == (uint32_t) rect.getHeight();
853 Glop glop;
Chris Craik6b109c72015-02-27 10:55:28 -0800854 GlopBuilder(mRenderState, mCaches, &glop)
855 .setMeshTexturedUvQuad(nullptr, Rect(0, 1, 1, 0)) // TODO: simplify with VBO
Chris Craik26bf3422015-02-26 16:28:17 -0800856 .setFillTextureLayer(*layer, getLayerAlpha(layer))
857 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
858 .setModelViewMapUnitToRectOptionalSnap(snap, rect)
859 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
860 .build();
Chris Craik31dbfc12015-03-30 15:42:23 -0700861 renderGlop(glop);
862 return;
Chris Craik26bf3422015-02-26 16:28:17 -0800863 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700864
Chris Craik26bf3422015-02-26 16:28:17 -0800865 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700866 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700867 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700868 setupDrawWithTexture();
869 } else {
870 setupDrawWithExternalTexture();
871 }
872 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700873 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500874 setupDrawColorFilter(layer->getColorFilter());
875 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700876 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700877 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500878 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700879 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Chris Craikf27133d2015-02-19 09:51:53 -0800880 setupDrawTexture(layer->getTextureId());
Romain Guy8f0095c2011-05-02 17:24:22 -0700881 } else {
Chris Craikf27133d2015-02-19 09:51:53 -0800882 setupDrawExternalTexture(layer->getTextureId());
Romain Guy8f0095c2011-05-02 17:24:22 -0700883 }
Chris Craik26bf3422015-02-26 16:28:17 -0800884 if (currentTransform()->isPureTranslate()
885 && !layer->getForceFilter()
886 && layer->getWidth() == (uint32_t) rect.getWidth()
887 && layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craika6b52192015-02-27 17:43:02 -0800888 const float x = floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
889 const float y = floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700890
Romain Guyd21b6e12011-11-30 20:21:23 -0800891 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800892 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
893 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700894 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800895 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800896 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
897 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700898 }
899 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700900 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700901
Chris Craik117bdbc2015-02-05 10:12:38 -0800902 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700903}
904
Romain Guy5b3b3522010-10-27 18:57:51 -0700905void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700906 if (layer->isTextureLayer()) {
907 EVENT_LOGD("composeTextureLayerRect");
908 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
909 drawTextureLayer(layer, rect);
910 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
911 } else {
912 EVENT_LOGD("composeHardwareLayerRect");
Chris Craik182952f2015-03-09 14:17:29 -0700913
914 if (USE_GLOPS) {
915 Blend::ModeOrderSwap modeUsage = swap ?
916 Blend::ModeOrderSwap::Swap : Blend::ModeOrderSwap::NoSwap;
917 const Matrix4& transform = swap ? Matrix4::identity() : *currentTransform();
918 bool snap = !swap
919 && layer->getWidth() == static_cast<uint32_t>(rect.getWidth())
920 && layer->getHeight() == static_cast<uint32_t>(rect.getHeight());
921 Glop glop;
922 GlopBuilder(mRenderState, mCaches, &glop)
923 .setMeshTexturedUvQuad(nullptr, layer->texCoords)
924 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), modeUsage)
925 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
926 .setModelViewMapUnitToRectOptionalSnap(snap, rect)
927 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
928 .build();
929 renderGlop(glop);
930 return;
931 }
932
Romain Guyaa6c24c2011-04-28 18:40:04 -0700933 const Rect& texCoords = layer->texCoords;
934 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
935 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700936
Romain Guy9ace8f52011-07-07 20:50:11 -0700937 float x = rect.left;
938 float y = rect.top;
Chris Craik182952f2015-03-09 14:17:29 -0700939 bool simpleTransform = currentTransform()->isPureTranslate()
940 && layer->getWidth() == (uint32_t) rect.getWidth()
941 && layer->getHeight() == (uint32_t) rect.getHeight();
Romain Guyb2479152011-07-08 11:57:29 -0700942
943 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700944 // When we're swapping, the layer is already in screen coordinates
945 if (!swap) {
Chris Craika6b52192015-02-27 17:43:02 -0800946 x = floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
947 y = floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700948 }
949
Romain Guyd21b6e12011-11-30 20:21:23 -0800950 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700951 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800952 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700953 }
954
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500955 SkPaint layerPaint;
956 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
957 layerPaint.setXfermodeMode(layer->getMode());
958 layerPaint.setColorFilter(layer->getColorFilter());
959
960 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700961 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craikf27133d2015-02-19 09:51:53 -0800962 layer->getTextureId(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -0700963 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik117bdbc2015-02-05 10:12:38 -0800964 GL_TRIANGLE_STRIP, kUnitQuadCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700965
Romain Guyaa6c24c2011-04-28 18:40:04 -0700966 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700967 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700968}
969
Chris Craik34416ea2013-04-15 16:08:28 -0700970/**
971 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
972 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
973 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
974 * by saveLayer's restore
975 */
Chris Craik6b109c72015-02-27 10:55:28 -0800976#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
977 DRAW_COMMAND; \
978 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
979 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
980 DRAW_COMMAND; \
981 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
982 } \
Chris Craik34416ea2013-04-15 16:08:28 -0700983 }
984
985#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
986
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400987// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
988// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
989class LayerShader : public SkShader {
990public:
991 LayerShader(Layer* layer, const SkMatrix* localMatrix)
992 : INHERITED(localMatrix)
993 , mLayer(layer) {
994 }
995
Chris Craike84a2082014-12-22 14:28:49 -0800996 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400997 if (data) {
998 *data = static_cast<void*>(mLayer);
999 }
1000 return true;
1001 }
1002
Chris Craike84a2082014-12-22 14:28:49 -08001003 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001004 return !mLayer->isBlend();
1005 }
1006
1007protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +00001008 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001009 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
1010 }
1011
Chris Craike84a2082014-12-22 14:28:49 -08001012 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001013 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1014 }
1015
Chris Craike84a2082014-12-22 14:28:49 -08001016 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001017 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -08001018 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001019 }
1020private:
1021 // Unowned.
1022 Layer* mLayer;
1023 typedef SkShader INHERITED;
1024};
1025
Romain Guy5b3b3522010-10-27 18:57:51 -07001026void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001027 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1028
1029 if (layer->getConvexMask()) {
1030 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1031
1032 // clip to the area of the layer the mask can be larger
1033 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1034
1035 SkPaint paint;
1036 paint.setAntiAlias(true);
1037 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1038
Chris Craik3f0854292014-04-15 16:18:08 -07001039 // create LayerShader to map SaveLayer content into subsequent draw
1040 SkMatrix shaderMatrix;
1041 shaderMatrix.setTranslate(rect.left, rect.bottom);
1042 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001043 LayerShader layerShader(layer, &shaderMatrix);
1044 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001045
1046 // Since the drawing primitive is defined in local drawing space,
1047 // we don't need to modify the draw matrix
1048 const SkPath* maskPath = layer->getConvexMask();
1049 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1050
Chris Craike84a2082014-12-22 14:28:49 -08001051 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -07001052 restore();
1053
1054 return;
1055 }
1056
Romain Guy5b3b3522010-10-27 18:57:51 -07001057 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001058 layer->setRegionAsRect();
1059
Chris Craik34416ea2013-04-15 16:08:28 -07001060 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001061
Romain Guy5b3b3522010-10-27 18:57:51 -07001062 layer->region.clear();
1063 return;
1064 }
1065
Chris Craik62d307c2014-07-29 10:35:13 -07001066 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001067 // standard Region based draw
1068 size_t count;
1069 const android::Rect* rects;
1070 Region safeRegion;
1071 if (CC_LIKELY(hasRectToRectTransform())) {
1072 rects = layer->region.getArray(&count);
1073 } else {
1074 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1075 rects = safeRegion.getArray(&count);
1076 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001077
Chris Craik3f0854292014-04-15 16:18:08 -07001078 const float texX = 1.0f / float(layer->getWidth());
1079 const float texY = 1.0f / float(layer->getHeight());
1080 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001081
Chris Craik182952f2015-03-09 14:17:29 -07001082 if (USE_GLOPS) {
1083 TextureVertex quadVertices[count * 4];
1084 //std::unique_ptr<TextureVertex[]> quadVertices(new TextureVertex[count * 4]);
1085 TextureVertex* mesh = &quadVertices[0];
1086 for (size_t i = 0; i < count; i++) {
1087 const android::Rect* r = &rects[i];
1088
1089 const float u1 = r->left * texX;
1090 const float v1 = (height - r->top) * texY;
1091 const float u2 = r->right * texX;
1092 const float v2 = (height - r->bottom) * texY;
1093
1094 // TODO: Reject quads outside of the clip
1095 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1096 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1097 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1098 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1099 }
1100 Glop glop;
1101 GlopBuilder(mRenderState, mCaches, &glop)
1102 .setMeshTexturedIndexedQuads(&quadVertices[0], count * 6)
1103 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
1104 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
1105 .setModelViewOffsetRectSnap(0, 0, rect)
1106 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1107 .build();
1108 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
1109 return;
1110 }
1111
1112 const float alpha = getLayerAlpha(layer);
1113
Chris Craik3f0854292014-04-15 16:18:08 -07001114 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001115
Chris Craik3f0854292014-04-15 16:18:08 -07001116 // We must get (and therefore bind) the region mesh buffer
1117 // after we setup drawing in case we need to mess with the
1118 // stencil buffer in setupDraw()
1119 TextureVertex* mesh = mCaches.getRegionMesh();
1120 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001121
Chris Craik3f0854292014-04-15 16:18:08 -07001122 setupDrawWithTexture();
1123 setupDrawColor(alpha, alpha, alpha, alpha);
1124 setupDrawColorFilter(layer->getColorFilter());
1125 setupDrawBlending(layer);
1126 setupDrawProgram();
1127 setupDrawDirtyRegionsDisabled();
1128 setupDrawPureColorUniforms();
1129 setupDrawColorFilterUniforms(layer->getColorFilter());
Chris Craikf27133d2015-02-19 09:51:53 -08001130 setupDrawTexture(layer->getTextureId());
Chris Craik3f0854292014-04-15 16:18:08 -07001131 if (currentTransform()->isPureTranslate()) {
Chris Craika6b52192015-02-27 17:43:02 -08001132 const float x = floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1133 const float y = floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001134
Chris Craik3f0854292014-04-15 16:18:08 -07001135 layer->setFilter(GL_NEAREST);
1136 setupDrawModelView(kModelViewMode_Translate, false,
1137 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1138 } else {
1139 layer->setFilter(GL_LINEAR);
1140 setupDrawModelView(kModelViewMode_Translate, false,
1141 rect.left, rect.top, rect.right, rect.bottom);
1142 }
1143 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001144
Chris Craik3f0854292014-04-15 16:18:08 -07001145 for (size_t i = 0; i < count; i++) {
1146 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001147
Chris Craik3f0854292014-04-15 16:18:08 -07001148 const float u1 = r->left * texX;
1149 const float v1 = (height - r->top) * texY;
1150 const float u2 = r->right * texX;
1151 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001152
Chris Craik3f0854292014-04-15 16:18:08 -07001153 // TODO: Reject quads outside of the clip
1154 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1155 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1156 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1157 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001158
Chris Craik3f0854292014-04-15 16:18:08 -07001159 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001160
Chris Craik96a5c4c2015-01-27 15:46:35 -08001161 if (numQuads >= kMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001162 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001163 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001164 numQuads = 0;
1165 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001166 }
Chris Craik3f0854292014-04-15 16:18:08 -07001167 }
1168
1169 if (numQuads > 0) {
1170 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001171 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001172 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001173
Romain Guy5b3b3522010-10-27 18:57:51 -07001174#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001175 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001176#endif
1177
Chris Craik3f0854292014-04-15 16:18:08 -07001178 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001179}
1180
Romain Guy3a3133d2011-02-01 22:59:58 -08001181#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001182void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001183 size_t count;
1184 const android::Rect* rects = region.getArray(&count);
1185
1186 uint32_t colors[] = {
1187 0x7fff0000, 0x7f00ff00,
1188 0x7f0000ff, 0x7fff00ff,
1189 };
1190
1191 int offset = 0;
1192 int32_t top = rects[0].top;
1193
1194 for (size_t i = 0; i < count; i++) {
1195 if (top != rects[i].top) {
1196 offset ^= 0x2;
1197 top = rects[i].top;
1198 }
1199
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001200 SkPaint paint;
1201 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001202 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001203 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001204 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001205}
Chris Craike63f7c622013-10-17 10:30:55 -07001206#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001207
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001208void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001209 Vector<float> rects;
1210
1211 SkRegion::Iterator it(region);
1212 while (!it.done()) {
1213 const SkIRect& r = it.rect();
1214 rects.push(r.fLeft);
1215 rects.push(r.fTop);
1216 rects.push(r.fRight);
1217 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001218 it.next();
1219 }
1220
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001221 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001222}
1223
Romain Guy5b3b3522010-10-27 18:57:51 -07001224void OpenGLRenderer::dirtyLayer(const float left, const float top,
Chris Craik083e7332015-02-27 17:04:20 -08001225 const float right, const float bottom, const Matrix4& transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001226 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001227 Rect bounds(left, top, right, bottom);
1228 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001229 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001230 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001231}
1232
1233void OpenGLRenderer::dirtyLayer(const float left, const float top,
1234 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001235 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001236 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001237 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001238 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001239}
1240
1241void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001242 if (bounds.intersect(mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001243 bounds.snapToPixelBoundaries();
1244 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1245 if (!dirty.isEmpty()) {
1246 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001247 }
1248 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001249}
1250
Chris Craik4063a0e2013-11-15 16:06:56 -08001251void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001252 GLsizei elementsCount = quadsCount * 6;
1253 while (elementsCount > 0) {
Chris Craik922d3a72015-02-13 17:47:21 -08001254 GLsizei drawCount = MathUtils::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Romain Guy448455f2013-07-22 13:57:50 -07001255
Romain Guy3380cfd2013-08-15 16:57:57 -07001256 setupDrawIndexedVertices(&mesh[0].x);
Chris Craike84a2082014-12-22 14:28:49 -08001257 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy448455f2013-07-22 13:57:50 -07001258
1259 elementsCount -= drawCount;
1260 // Though there are 4 vertices in a quad, we use 6 indices per
1261 // quad to draw with GL_TRIANGLES
1262 mesh += (drawCount / 6) * 4;
1263 }
1264}
1265
Romain Guy54be1cd2011-06-13 19:04:27 -07001266void OpenGLRenderer::clearLayerRegions() {
Chris Craik2bb8f562015-02-17 16:42:02 -08001267 const size_t quadCount = mLayers.size();
1268 if (quadCount == 0) return;
Romain Guy54be1cd2011-06-13 19:04:27 -07001269
Tom Hudson984162f2014-10-10 13:38:16 -04001270 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001271 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001272 // Doing several glScissor/glClear here can negatively impact
1273 // GPUs with a tiler architecture, instead we draw quads with
1274 // the Clear blending mode
1275
1276 // The list contains bounds that have already been clipped
1277 // against their initial clip rect, and the current clip
1278 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001279 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001280
Chris Craik2bb8f562015-02-17 16:42:02 -08001281 Vertex mesh[quadCount * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001282 Vertex* vertex = mesh;
1283
Chris Craik2bb8f562015-02-17 16:42:02 -08001284 for (uint32_t i = 0; i < quadCount; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001285 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001286
Chris Craik51d6a3d2014-12-22 17:16:56 -08001287 Vertex::set(vertex++, bounds.left, bounds.top);
1288 Vertex::set(vertex++, bounds.right, bounds.top);
1289 Vertex::set(vertex++, bounds.left, bounds.bottom);
1290 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001291 }
Romain Guye67307c2013-02-11 18:01:20 -08001292 // We must clear the list of dirty rects before we
1293 // call setupDraw() to prevent stencil setup to do
1294 // the same thing again
1295 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001296
Chris Craik2bb8f562015-02-17 16:42:02 -08001297 if (USE_GLOPS) {
1298 Glop glop;
Chris Craik6b109c72015-02-27 10:55:28 -08001299 GlopBuilder(mRenderState, mCaches, &glop)
1300 .setMeshIndexedQuads(&mesh[0], quadCount)
Chris Craik2bb8f562015-02-17 16:42:02 -08001301 .setFillClear()
Chris Craikf27133d2015-02-19 09:51:53 -08001302 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
Chris Craikef250742015-02-25 17:16:16 -08001303 .setModelViewOffsetRect(0, 0, Rect(currentSnapshot()->getClipRect()))
Chris Craik2bb8f562015-02-17 16:42:02 -08001304 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1305 .build();
Chris Craik6b109c72015-02-27 10:55:28 -08001306 renderGlop(glop, false);
Chris Craik2bb8f562015-02-17 16:42:02 -08001307 } else {
1308 SkPaint clearPaint;
1309 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001310
Chris Craik2bb8f562015-02-17 16:42:02 -08001311 setupDraw(false);
1312 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1313 setupDrawBlending(&clearPaint, true);
1314 setupDrawProgram();
1315 setupDrawPureColorUniforms();
1316 setupDrawModelView(kModelViewMode_Translate, false,
1317 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001318
Chris Craik2bb8f562015-02-17 16:42:02 -08001319 issueIndexedQuadDraw(&mesh[0], quadCount);
1320 }
Romain Guy8a4ac612012-07-17 17:32:48 -07001321
Chris Craik65fe5ee2015-01-26 18:06:29 -08001322 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001323 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001324 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001325 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001326}
1327
Romain Guybd6b79b2010-06-26 00:13:53 -07001328///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001329// State Deferral
1330///////////////////////////////////////////////////////////////////////////////
1331
Chris Craikff785832013-03-08 13:12:16 -08001332bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001333 const Rect& currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001334 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001335
Chris Craikff785832013-03-08 13:12:16 -08001336 if (stateDeferFlags & kStateDeferFlag_Draw) {
1337 // state has bounds initialized in local coordinates
1338 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001339 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001340 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001341 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1342 // is used, it should more closely duplicate the quickReject logic (in how it uses
1343 // snapToPixelBoundaries)
1344
Rob Tsuk487a92c2015-01-06 13:22:54 -08001345 if (!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001346 // quick rejected
1347 return true;
1348 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001349
Chris Craika02c4ed2013-06-14 13:43:58 -07001350 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001351 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001352 int& flags = state.mClipSideFlags;
1353 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001354 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1355 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1356 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1357 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001358 }
1359 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001360 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001361 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1362 // overdraw avoidance (since we don't know what it overlaps)
1363 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001364 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001365 }
1366 }
1367
Chris Craik527a3aa2013-03-04 10:19:31 -08001368 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1369 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001370 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001371 }
1372
Chris Craik7273daa2013-03-28 11:25:24 -07001373 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1374 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001375 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001376 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001377 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001378
1379 // always store/restore, since it's just a pointer
1380 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001381 return false;
1382}
1383
Chris Craik527a3aa2013-03-04 10:19:31 -08001384void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001385 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001386 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001387 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001388 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001389
Chris Craik527a3aa2013-03-04 10:19:31 -08001390 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001391 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001392 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001393 dirtyClip();
1394 }
Chris Craikc3566d02013-02-04 16:16:33 -08001395}
1396
Chris Craik28ce94a2013-05-31 11:38:03 -07001397/**
1398 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1399 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1400 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1401 *
1402 * This method should be called when restoreDisplayState() won't be restoring the clip
1403 */
1404void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001405 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001406 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001407 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001408 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001409 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001410 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001411 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1412 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001413}
1414
Chris Craikc3566d02013-02-04 16:16:33 -08001415///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001416// Clipping
1417///////////////////////////////////////////////////////////////////////////////
1418
Romain Guybb9524b2010-06-22 18:56:38 -07001419void OpenGLRenderer::setScissorFromClip() {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001420 Rect clip(mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001421 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001422
Chris Craik65fe5ee2015-01-26 18:06:29 -08001423 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001424 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001425 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001426 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001427}
1428
Romain Guy8ce00302013-01-15 18:51:42 -08001429void OpenGLRenderer::ensureStencilBuffer() {
1430 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1431 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1432 // just hope we have one when hasLayer() returns false.
1433 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001434 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001435 }
1436}
1437
1438void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1439 // The layer's FBO is already bound when we reach this stage
1440 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001441 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1442 // is attached after we initiated tiling. We must turn it off,
1443 // attach the new render buffer then turn tiling back on
1444 endTiling();
1445
Romain Guy8d4aeb72013-02-12 16:08:55 -08001446 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Chris Craik117bdbc2015-02-05 10:12:38 -08001447 Stencil::getSmallestStencilFormat(),
1448 layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001449 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001450
Romain Guyf735c8e2013-01-31 17:45:55 -08001451 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001452 }
1453}
1454
Rob Tsuk487a92c2015-01-06 13:22:54 -08001455static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1456 float x, float y) {
1457 Vertex v;
1458 v.x = x;
1459 v.y = y;
1460 transform.mapPoint(v.x, v.y);
1461 rectangleVertices.push_back(v);
1462}
1463
1464static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1465 Vertex v;
1466 v.x = x;
1467 v.y = y;
1468 rectangleVertices.push_back(v);
1469}
1470
1471void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
Chris Craik2bb8f562015-02-17 16:42:02 -08001472 int quadCount = rectangleList.getTransformedRectanglesCount();
1473 std::vector<Vertex> rectangleVertices(quadCount * 4);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001474 Rect scissorBox = rectangleList.calculateBounds();
1475 scissorBox.snapToPixelBoundaries();
Chris Craik2bb8f562015-02-17 16:42:02 -08001476 for (int i = 0; i < quadCount; ++i) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001477 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1478 const Matrix4& transform = tr.getTransform();
1479 Rect bounds = tr.getBounds();
1480 if (transform.rectToRect()) {
1481 transform.mapRect(bounds);
1482 if (!bounds.intersect(scissorBox)) {
1483 bounds.setEmpty();
1484 } else {
1485 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1486 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1487 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1488 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1489 }
1490 } else {
1491 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1492 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1493 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1494 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1495 }
1496 }
1497
Chris Craik65fe5ee2015-01-26 18:06:29 -08001498 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001499 scissorBox.getWidth(), scissorBox.getHeight());
1500
Chris Craik2bb8f562015-02-17 16:42:02 -08001501 if (USE_GLOPS) {
1502 Glop glop;
Chris Craik6b109c72015-02-27 10:55:28 -08001503 GlopBuilder(mRenderState, mCaches, &glop)
1504 .setMeshIndexedQuads(&rectangleVertices[0], rectangleVertices.size() / 4)
Chris Craik2bb8f562015-02-17 16:42:02 -08001505 .setFillBlack()
Chris Craikf27133d2015-02-19 09:51:53 -08001506 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
Chris Craik2bb8f562015-02-17 16:42:02 -08001507 .setModelViewOffsetRect(0, 0, scissorBox)
1508 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1509 .build();
1510 renderGlop(glop);
1511 return;
1512 }
1513
Rob Tsuk487a92c2015-01-06 13:22:54 -08001514 const SkPaint* paint = nullptr;
1515 setupDraw();
1516 setupDrawNoTexture();
1517 setupDrawColor(0, 0xff * currentSnapshot()->alpha);
1518 setupDrawShader(getShader(paint));
1519 setupDrawColorFilter(getColorFilter(paint));
1520 setupDrawBlending(paint);
1521 setupDrawProgram();
1522 setupDrawDirtyRegionsDisabled();
1523 setupDrawModelView(kModelViewMode_Translate, false,
1524 0.0f, 0.0f, 0.0f, 0.0f, true);
1525 setupDrawColorUniforms(getShader(paint));
1526 setupDrawShaderUniforms(getShader(paint));
1527 setupDrawColorFilterUniforms(getColorFilter(paint));
1528
1529 issueIndexedQuadDraw(&rectangleVertices[0], rectangleVertices.size() / 4);
1530}
1531
Romain Guy8ce00302013-01-15 18:51:42 -08001532void OpenGLRenderer::setStencilFromClip() {
1533 if (!mCaches.debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001534 if (!currentSnapshot()->clipIsSimple()) {
1535 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001536 EVENT_LOGD("setStencilFromClip - enabling");
1537
Romain Guy8ce00302013-01-15 18:51:42 -08001538 // NOTE: The order here is important, we must set dirtyClip to false
1539 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001540 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001541
1542 ensureStencilBuffer();
1543
Rob Tsuk487a92c2015-01-06 13:22:54 -08001544 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001545
Rob Tsuk487a92c2015-01-06 13:22:54 -08001546 bool isRectangleList = clipArea.isRectangleList();
1547 if (isRectangleList) {
1548 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1549 } else {
1550 incrementThreshold = 0;
1551 }
1552
Chris Craik96a5c4c2015-01-27 15:46:35 -08001553 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001554
1555 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001556 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001557 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001558 if (resetScissor) {
1559 // The scissor was not set so we now need to update it
1560 setScissorFromClip();
1561 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001562
Chris Craik96a5c4c2015-01-27 15:46:35 -08001563 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001564
1565 // stash and disable the outline clip state, since stencil doesn't account for outline
1566 bool storedSkipOutlineClip = mSkipOutlineClip;
1567 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001568
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001569 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001570 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001571 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1572
Rob Tsuk487a92c2015-01-06 13:22:54 -08001573 if (isRectangleList) {
1574 drawRectangleList(clipArea.getRectangleList());
1575 } else {
1576 // NOTE: We could use the region contour path to generate a smaller mesh
1577 // Since we are using the stencil we could use the red book path
1578 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001579
Rob Tsuk487a92c2015-01-06 13:22:54 -08001580 // The last parameter is important: we are not drawing in the color buffer
1581 // so we don't want to dirty the current layer, if any
1582 drawRegionRects(clipArea.getClipRegion(), paint, false);
1583 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001584 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001585 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001586
Chris Craik96a5c4c2015-01-27 15:46:35 -08001587 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001588
1589 // Draw the region used to generate the stencil if the appropriate debug
1590 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001591 // TODO: Implement for rectangle list clip areas
1592 if (mCaches.debugStencilClip == Caches::kStencilShowRegion &&
1593 !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001594 paint.setColor(0x7f0000ff);
1595 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001596 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001597 }
Romain Guy8ce00302013-01-15 18:51:42 -08001598 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001599 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001600 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001601 }
1602 }
1603}
1604
Chris Craikf0a59072013-11-19 18:00:46 -08001605/**
1606 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1607 *
1608 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1609 * style, and tessellated AA ramp
1610 */
1611bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001612 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001613 bool snapOut = paint && paint->isAntiAlias();
1614
1615 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1616 float outset = paint->getStrokeWidth() * 0.5f;
1617 left -= outset;
1618 top -= outset;
1619 right += outset;
1620 bottom += outset;
1621 }
1622
Chris Craikdeeda3d2014-05-05 19:09:33 -07001623 bool clipRequired = false;
1624 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001625 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001626 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001627 return true;
1628 }
1629
Chris Craikf23b25a2014-06-26 15:46:20 -07001630 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001631 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001632 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001633 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001634}
1635
Romain Guy8ce00302013-01-15 18:51:42 -08001636void OpenGLRenderer::debugClip() {
1637#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001638 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001639 SkPaint paint;
1640 paint.setColor(0x7f00ff00);
1641 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1642
Romain Guy8ce00302013-01-15 18:51:42 -08001643 }
1644#endif
1645}
1646
Chris Craik6b109c72015-02-27 10:55:28 -08001647void OpenGLRenderer::renderGlop(const Glop& glop, bool clearLayer) {
1648 // TODO: It would be best if we could do this before quickRejectSetupScissor()
1649 // changes the scissor test state
1650 if (clearLayer) clearLayerRegions();
1651
Chris Craik117bdbc2015-02-05 10:12:38 -08001652 if (mState.getDirtyClip()) {
1653 if (mRenderState.scissor().isEnabled()) {
1654 setScissorFromClip();
1655 }
1656
1657 setStencilFromClip();
1658 }
1659 mRenderState.render(glop);
Chris Craik2ab95d72015-02-06 15:25:51 -08001660 if (!mRenderState.stencil().isWriteEnabled()) {
1661 // TODO: specify more clearly when a draw should dirty the layer.
1662 // is writing to the stencil the only time we should ignore this?
1663 dirtyLayer(glop.bounds.left, glop.bounds.top, glop.bounds.right, glop.bounds.bottom);
Chris Craik0519c8102015-02-11 13:17:06 -08001664 mDirty = true;
Chris Craik2ab95d72015-02-06 15:25:51 -08001665 }
Chris Craik117bdbc2015-02-05 10:12:38 -08001666}
1667
Romain Guyf6a11b82010-06-23 17:47:49 -07001668///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001669// Drawing commands
1670///////////////////////////////////////////////////////////////////////////////
1671
Chris Craik62d307c2014-07-29 10:35:13 -07001672void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001673 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001674 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001675 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001676 // Make sure setScissor & setStencil happen at the beginning of
1677 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001678 if (mState.getDirtyClip()) {
Chris Craik65fe5ee2015-01-26 18:06:29 -08001679 if (mRenderState.scissor().isEnabled()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001680 setScissorFromClip();
1681 }
Chris Craik62d307c2014-07-29 10:35:13 -07001682
Dohyun Leeadc0d9d2014-11-24 21:08:15 +09001683 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001684 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001685
Romain Guy70ca14e2010-12-13 18:24:33 -08001686 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001687
Romain Guy70ca14e2010-12-13 18:24:33 -08001688 mSetShaderColor = false;
1689 mColorSet = false;
Chris Craik0519c8102015-02-11 13:17:06 -08001690 mColor.a = mColor.r = mColor.g = mColor.b = 0.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001691 mTextureUnit = 0;
1692 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001693
1694 // Enable debug highlight when what we're about to draw is tested against
1695 // the stencil buffer and if stencil highlight debugging is on
Chris Craik117bdbc2015-02-05 10:12:38 -08001696 mDescription.hasDebugHighlight = !mCaches.debugOverdraw
1697 && mCaches.debugStencilClip == Caches::kStencilShowHighlight
1698 && mRenderState.stencil().isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001699}
1700
1701void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1702 mDescription.hasTexture = true;
1703 mDescription.hasAlpha8Texture = isAlpha8;
1704}
1705
Romain Guyff316ec2013-02-13 18:39:43 -08001706void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1707 mDescription.hasTexture = true;
1708 mDescription.hasColors = true;
1709 mDescription.hasAlpha8Texture = isAlpha8;
1710}
1711
Romain Guyaa6c24c2011-04-28 18:40:04 -07001712void OpenGLRenderer::setupDrawWithExternalTexture() {
1713 mDescription.hasExternalTexture = true;
1714}
1715
Romain Guy15bc6432011-12-13 13:11:32 -08001716void OpenGLRenderer::setupDrawNoTexture() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001717 mRenderState.meshState().disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001718}
1719
Chris Craik91a8c7c2014-08-12 14:31:35 -07001720void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1721 mDescription.hasVertexAlpha = true;
1722 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001723}
1724
Romain Guy8d0d4782010-12-14 20:13:35 -08001725void OpenGLRenderer::setupDrawColor(int color, int alpha) {
Chris Craik0519c8102015-02-11 13:17:06 -08001726 mColor.a = alpha / 255.0f;
1727 mColor.r = mColor.a * ((color >> 16) & 0xFF) / 255.0f;
1728 mColor.g = mColor.a * ((color >> 8) & 0xFF) / 255.0f;
1729 mColor.b = mColor.a * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001730 mColorSet = true;
Chris Craik0519c8102015-02-11 13:17:06 -08001731 mSetShaderColor = mDescription.setColorModulate(mColor.a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001732}
1733
Romain Guy86568192010-12-14 15:55:39 -08001734void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
Chris Craik0519c8102015-02-11 13:17:06 -08001735 mColor.a = alpha / 255.0f;
1736 mColor.r = mColor.a * ((color >> 16) & 0xFF) / 255.0f;
1737 mColor.g = mColor.a * ((color >> 8) & 0xFF) / 255.0f;
1738 mColor.b = mColor.a * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001739 mColorSet = true;
Chris Craik0519c8102015-02-11 13:17:06 -08001740 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColor.r, mColor.g, mColor.b, mColor.a);
Romain Guy86568192010-12-14 15:55:39 -08001741}
1742
Romain Guy41210632012-07-16 17:04:24 -07001743void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1744 mCaches.fontRenderer->describe(mDescription, paint);
1745}
1746
Romain Guy70ca14e2010-12-13 18:24:33 -08001747void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
Chris Craik0519c8102015-02-11 13:17:06 -08001748 mColor.a = a;
1749 mColor.r = r;
1750 mColor.g = g;
1751 mColor.b = b;
Romain Guy70ca14e2010-12-13 18:24:33 -08001752 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001753 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001754}
1755
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001756void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
Chris Craike84a2082014-12-22 14:28:49 -08001757 if (shader != nullptr) {
Chris Craik117bdbc2015-02-05 10:12:38 -08001758 SkiaShader::describe(&mCaches, mDescription, mCaches.extensions(), *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001759 }
1760}
1761
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001762void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001763 if (filter == nullptr) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001764 return;
1765 }
1766
1767 SkXfermode::Mode mode;
Chris Craike84a2082014-12-22 14:28:49 -08001768 if (filter->asColorMode(nullptr, &mode)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001769 mDescription.colorOp = ProgramDescription::kColorBlend;
1770 mDescription.colorMode = mode;
Chris Craike84a2082014-12-22 14:28:49 -08001771 } else if (filter->asColorMatrix(nullptr)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001772 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001773 }
1774}
1775
Romain Guyf09ef512011-05-27 11:43:46 -07001776void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1777 if (mColorSet && mode == SkXfermode::kClear_Mode) {
Chris Craik0519c8102015-02-11 13:17:06 -08001778 mColor.a = 1.0f;
1779 mColor.r = mColor.g = mColor.b = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001780 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001781 }
1782}
1783
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001784void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1785 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001786 // When the blending mode is kClear_Mode, we need to use a modulate color
1787 // argb=1,0,0,0
1788 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001789 // TODO: check shader blending, once we have shader drawing support for layers.
Tom Hudson8dfaa492014-12-09 15:03:44 -05001790 bool blend = layer->isBlend()
1791 || getLayerAlpha(layer) < 1.0f
Chris Craik0519c8102015-02-11 13:17:06 -08001792 || (mColorSet && mColor.a < 1.0f)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001793 || PaintUtils::isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001794 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001795}
1796
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001797void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1798 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001799 // When the blending mode is kClear_Mode, we need to use a modulate color
1800 // argb=1,0,0,0
1801 accountForClear(mode);
Chris Craik0519c8102015-02-11 13:17:06 -08001802 blend |= (mColorSet && mColor.a < 1.0f)
Chris Craik03188872015-02-02 18:39:33 -08001803 || (getShader(paint) && !getShader(paint)->isOpaque())
1804 || PaintUtils::isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001805 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001806}
1807
1808void OpenGLRenderer::setupDrawProgram() {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001809 mCaches.setProgram(mDescription);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001810 if (mDescription.hasRoundRectClip) {
1811 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001812 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001813 const Rect& innerRect = state->innerRect;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001814 glUniform4f(mCaches.program().getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001815 innerRect.left, innerRect.top,
1816 innerRect.right, innerRect.bottom);
Chris Craik6c15ffa2015-02-02 13:50:55 -08001817 glUniformMatrix4fv(mCaches.program().getUniform("roundRectInvTransform"),
Chris Craikdeeda3d2014-05-05 19:09:33 -07001818 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001819
1820 // add half pixel to round out integer rect space to cover pixel centers
1821 float roundedOutRadius = state->radius + 0.5f;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001822 glUniform1f(mCaches.program().getUniform("roundRectRadius"),
Chris Craik4340c262014-09-11 18:58:45 -07001823 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001824 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001825}
1826
1827void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1828 mTrackDirtyRegions = false;
1829}
1830
Chris Craik4063a0e2013-11-15 16:06:56 -08001831void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1832 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001833 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001834 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001835 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001836 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001837
Romain Guy86568192010-12-14 15:55:39 -08001838 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001839 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001840
1841 mCaches.program().set(currentSnapshot()->getOrthoMatrix(),
Chris Craike84a2082014-12-22 14:28:49 -08001842 mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001843 if (dirty && mTrackDirtyRegions) {
1844 if (!ignoreTransform) {
1845 dirtyLayer(left, top, right, bottom, *currentTransform());
1846 } else {
1847 dirtyLayer(left, top, right, bottom);
1848 }
Romain Guy86568192010-12-14 15:55:39 -08001849 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001850}
1851
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001852void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1853 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Chris Craik0519c8102015-02-11 13:17:06 -08001854 mCaches.program().setColor(mColor);
Romain Guy70ca14e2010-12-13 18:24:33 -08001855 }
1856}
1857
Romain Guy86568192010-12-14 15:55:39 -08001858void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001859 if (mSetShaderColor) {
Chris Craik0519c8102015-02-11 13:17:06 -08001860 mCaches.program().setColor(mColor);
Romain Guy55368412010-12-14 10:59:41 -08001861 }
1862}
1863
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001864void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
Chris Craike84a2082014-12-22 14:28:49 -08001865 if (shader == nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001866 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001867 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001868
1869 if (ignoreTransform) {
1870 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1871 // because it was built into modelView / the geometry, and the description needs to
1872 // compensate.
1873 mat4 modelViewWithoutTransform;
1874 modelViewWithoutTransform.loadInverse(*currentTransform());
1875 modelViewWithoutTransform.multiply(mModelViewMatrix);
1876 mModelViewMatrix.load(modelViewWithoutTransform);
1877 }
1878
Chris Craik117bdbc2015-02-05 10:12:38 -08001879 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit,
1880 mCaches.extensions(), *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001881}
1882
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001883void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001884 if (nullptr == filter) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001885 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001886 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001887
1888 SkColor color;
1889 SkXfermode::Mode mode;
1890 if (filter->asColorMode(&color, &mode)) {
1891 const int alpha = SkColorGetA(color);
1892 const GLfloat a = alpha / 255.0f;
1893 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1894 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1895 const GLfloat b = a * SkColorGetB(color) / 255.0f;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001896 glUniform4f(mCaches.program().getUniform("colorBlend"), r, g, b, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001897 return;
1898 }
1899
1900 SkScalar srcColorMatrix[20];
1901 if (filter->asColorMatrix(srcColorMatrix)) {
1902
1903 float colorMatrix[16];
1904 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1905 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1906 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1907 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1908
1909 // Skia uses the range [0..255] for the addition vector, but we need
1910 // the [0..1] range to apply the vector in GLSL
1911 float colorVector[4];
1912 colorVector[0] = srcColorMatrix[4] / 255.0f;
1913 colorVector[1] = srcColorMatrix[9] / 255.0f;
1914 colorVector[2] = srcColorMatrix[14] / 255.0f;
1915 colorVector[3] = srcColorMatrix[19] / 255.0f;
1916
Chris Craik6c15ffa2015-02-02 13:50:55 -08001917 glUniformMatrix4fv(mCaches.program().getUniform("colorMatrix"), 1,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001918 GL_FALSE, colorMatrix);
Chris Craik6c15ffa2015-02-02 13:50:55 -08001919 glUniform4fv(mCaches.program().getUniform("colorMatrixVector"), 1, colorVector);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001920 return;
1921 }
1922
1923 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001924}
1925
Romain Guy41210632012-07-16 17:04:24 -07001926void OpenGLRenderer::setupDrawTextGammaUniforms() {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001927 mCaches.fontRenderer->setupProgram(mDescription, mCaches.program());
Romain Guy41210632012-07-16 17:04:24 -07001928}
1929
Romain Guy70ca14e2010-12-13 18:24:33 -08001930void OpenGLRenderer::setupDrawSimpleMesh() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001931 bool force = mRenderState.meshState().bindMeshBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001932 mRenderState.meshState().bindPositionVertexPointer(force, nullptr);
Chris Craik96a5c4c2015-01-27 15:46:35 -08001933 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001934}
1935
1936void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001937 if (texture) mCaches.textureState().bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001938 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001939 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001940}
1941
Romain Guyaa6c24c2011-04-28 18:40:04 -07001942void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001943 mCaches.textureState().bindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001944 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001945 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001946}
1947
Romain Guy8f0095c2011-05-02 17:24:22 -07001948void OpenGLRenderer::setupDrawTextureTransform() {
1949 mDescription.hasTextureTransform = true;
1950}
1951
1952void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001953 glUniformMatrix4fv(mCaches.program().getUniform("mainTextureTransform"), 1,
Romain Guyaa6c24c2011-04-28 18:40:04 -07001954 GL_FALSE, &transform.data[0]);
1955}
1956
Chris Craik564acf72014-01-02 16:46:18 -08001957void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1958 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001959 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001960 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001961 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001962 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001963 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001964 }
Romain Guyd71dd362011-12-12 19:03:35 -08001965
Chris Craik6c15ffa2015-02-02 13:50:55 -08001966 mRenderState.meshState().bindPositionVertexPointer(force, vertices);
1967 if (mCaches.program().texCoords >= 0) {
1968 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001969 }
1970
Chris Craik96a5c4c2015-01-27 15:46:35 -08001971 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -08001972}
1973
Chris Craik564acf72014-01-02 16:46:18 -08001974void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1975 const GLvoid* texCoords, const GLvoid* colors) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001976 bool force = mRenderState.meshState().unbindMeshBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001977 GLsizei stride = sizeof(ColorTextureVertex);
1978
Chris Craik6c15ffa2015-02-02 13:50:55 -08001979 mRenderState.meshState().bindPositionVertexPointer(force, vertices, stride);
1980 if (mCaches.program().texCoords >= 0) {
1981 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords, stride);
Romain Guyff316ec2013-02-13 18:39:43 -08001982 }
Chris Craik6c15ffa2015-02-02 13:50:55 -08001983 int slot = mCaches.program().getAttrib("colors");
Romain Guyff316ec2013-02-13 18:39:43 -08001984 if (slot >= 0) {
1985 glEnableVertexAttribArray(slot);
1986 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1987 }
1988
Chris Craik96a5c4c2015-01-27 15:46:35 -08001989 mRenderState.meshState().unbindIndicesBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001990}
1991
Chris Craik564acf72014-01-02 16:46:18 -08001992void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1993 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001994 bool force = false;
1995 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1996 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
Chris Craik96a5c4c2015-01-27 15:46:35 -08001997 // use the default VBO found in RenderState
Romain Guy3b748a42013-04-17 18:54:38 -07001998 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001999 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy3b748a42013-04-17 18:54:38 -07002000 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002001 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07002002 }
Chris Craik96a5c4c2015-01-27 15:46:35 -08002003 mRenderState.meshState().bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07002004
Chris Craik6c15ffa2015-02-02 13:50:55 -08002005 mRenderState.meshState().bindPositionVertexPointer(force, vertices);
2006 if (mCaches.program().texCoords >= 0) {
2007 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08002008 }
Romain Guy70ca14e2010-12-13 18:24:33 -08002009}
2010
Romain Guy448455f2013-07-22 13:57:50 -07002011void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002012 bool force = mRenderState.meshState().unbindMeshBuffer();
2013 mRenderState.meshState().bindQuadIndicesBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08002014 mRenderState.meshState().bindPositionVertexPointer(force, vertices, kVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07002015}
2016
Romain Guy70ca14e2010-12-13 18:24:33 -08002017///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07002018// Drawing
2019///////////////////////////////////////////////////////////////////////////////
2020
Tom Hudson107843d2014-09-08 11:26:26 -04002021void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08002022 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
2023 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07002024 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07002025 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07002026 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07002027 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002028 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08002029 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07002030 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04002031 return;
Chris Craikc3566d02013-02-04 16:16:33 -08002032 }
2033
Chris Craikef8d6f22014-12-17 11:10:28 -08002034 // Don't avoid overdraw when visualizing, since that makes it harder to
2035 // debug where it's coming from, and when the problem occurs.
2036 bool avoidOverdraw = !mCaches.debugOverdraw;
Rob Tsuk487a92c2015-01-06 13:22:54 -08002037 DeferredDisplayList deferredList(mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002038 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07002039 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002040
2041 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04002042 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002043
Tom Hudson107843d2014-09-08 11:26:26 -04002044 deferredList.flush(*this, dirty);
2045 } else {
2046 // Even if there is no drawing command(Ex: invisible),
2047 // it still needs startFrame to clear buffer and start tiling.
2048 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08002049 }
2050}
2051
Chris Craik0519c8102015-02-11 13:17:06 -08002052void OpenGLRenderer::drawAlphaBitmap(Texture* texture, const SkPaint* paint) {
Chris Craik0519c8102015-02-11 13:17:06 -08002053 float x = 0;
2054 float y = 0;
Romain Guya168d732011-03-18 16:50:13 -07002055
Romain Guy886b2752013-01-04 12:26:18 -08002056 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2057
Romain Guya168d732011-03-18 16:50:13 -07002058 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08002059 if (currentTransform()->isPureTranslate()) {
Chris Craika6b52192015-02-27 17:43:02 -08002060 x = floorf(currentTransform()->getTranslateX() + 0.5f);
2061 y = floorf(currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002062 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002063
2064 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002065 } else {
Chris Craik0519c8102015-02-11 13:17:06 -08002066 texture->setFilter(PaintUtils::getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002067 }
2068
Romain Guy3b748a42013-04-17 18:54:38 -07002069 // No need to check for a UV mapper on the texture object, only ARGB_8888
2070 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002071 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Chris Craik96a5c4c2015-01-27 15:46:35 -08002072 paint, (GLvoid*) nullptr, (GLvoid*) kMeshTextureOffset,
Chris Craik117bdbc2015-02-05 10:12:38 -08002073 GL_TRIANGLE_STRIP, kUnitQuadCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002074}
2075
Romain Guy03c00b52013-06-20 18:30:28 -07002076/**
2077 * Important note: this method is intended to draw batches of bitmaps and
2078 * will not set the scissor enable or dirty the current layer, if any.
2079 * The caller is responsible for properly dirtying the current layer.
2080 */
Tom Hudson107843d2014-09-08 11:26:26 -04002081void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002082 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
2083 const Rect& bounds, const SkPaint* paint) {
Romain Guy55b6f952013-06-27 15:27:09 -07002084 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002085 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07002086
Chris Craik527a3aa2013-03-04 10:19:31 -08002087 const AutoTexture autoCleanup(texture);
2088
Chris Craika6b52192015-02-27 17:43:02 -08002089 if (USE_GLOPS) {
2090 // TODO: remove layer dirty in multi-draw callers
2091 // TODO: snap doesn't need to touch transform, only texture filter.
2092 bool snap = pureTranslate;
2093 const float x = floorf(bounds.left + 0.5f);
2094 const float y = floorf(bounds.top + 0.5f);
2095 int textureFillFlags = static_cast<int>((bitmap->colorType() == kAlpha_8_SkColorType)
2096 ? TextureFillFlags::kIsAlphaMaskTexture : TextureFillFlags::kNone);
2097 Glop glop;
2098 GlopBuilder(mRenderState, mCaches, &glop)
2099 .setMeshTexturedMesh(vertices, bitmapCount * 6)
2100 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
2101 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
2102 .setModelViewOffsetRectOptionalSnap(snap, x, y, Rect(0, 0, bounds.getWidth(), bounds.getHeight()))
2103 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2104 .build();
2105 renderGlop(glop);
2106 return;
2107 }
2108
2109 mCaches.textureState().activateTexture(0);
Chris Craik527a3aa2013-03-04 10:19:31 -08002110 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik0519c8102015-02-11 13:17:06 -08002111 texture->setFilter(pureTranslate ? GL_NEAREST : PaintUtils::getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002112
Chris Craika6b52192015-02-27 17:43:02 -08002113 const float x = floorf(bounds.left + 0.5f);
2114 const float y = floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04002115 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002116 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002117 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002118 GL_TRIANGLES, bitmapCount * 6, true,
2119 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002120 } else {
2121 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002122 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002123 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2124 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002125 }
2126
Tom Hudson107843d2014-09-08 11:26:26 -04002127 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08002128}
2129
Tom Hudson107843d2014-09-08 11:26:26 -04002130void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002131 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002132 return;
Romain Guy6926c722010-07-12 20:20:03 -07002133 }
2134
Chris Craik44eb2c02015-01-29 09:45:09 -08002135 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002136 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002137 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002138 const AutoTexture autoCleanup(texture);
2139
Chris Craikf27133d2015-02-19 09:51:53 -08002140 if (USE_GLOPS) {
Chris Craika6b52192015-02-27 17:43:02 -08002141 int textureFillFlags = static_cast<int>((bitmap->colorType() == kAlpha_8_SkColorType)
2142 ? TextureFillFlags::kIsAlphaMaskTexture : TextureFillFlags::kNone);
Chris Craikf27133d2015-02-19 09:51:53 -08002143 Glop glop;
Chris Craik6b109c72015-02-27 10:55:28 -08002144 GlopBuilder(mRenderState, mCaches, &glop)
2145 .setMeshTexturedUnitQuad(texture->uvMapper)
Chris Craika6b52192015-02-27 17:43:02 -08002146 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craikf27133d2015-02-19 09:51:53 -08002147 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
2148 .setModelViewMapUnitToRectSnap(Rect(0, 0, texture->width, texture->height))
2149 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2150 .build();
2151 renderGlop(glop);
2152 return;
2153 }
2154
Mike Reed1103b322014-07-08 12:36:44 -04002155 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik0519c8102015-02-11 13:17:06 -08002156 drawAlphaBitmap(texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002157 } else {
Chris Craik0519c8102015-02-11 13:17:06 -08002158 drawTextureRect(texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002159 }
Chet Haase48659092012-05-31 15:21:51 -07002160
Tom Hudson107843d2014-09-08 11:26:26 -04002161 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002162}
2163
Tom Hudson107843d2014-09-08 11:26:26 -04002164void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002165 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002166 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002167 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002168 }
2169
Romain Guyb18d2d02011-02-10 15:52:54 -08002170 float left = FLT_MAX;
2171 float top = FLT_MAX;
2172 float right = FLT_MIN;
2173 float bottom = FLT_MIN;
2174
Chris Craikef250742015-02-25 17:16:16 -08002175 const uint32_t elementCount = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002176
Chris Craikef250742015-02-25 17:16:16 -08002177 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[elementCount]);
Chris Craik51d6a3d2014-12-22 17:16:56 -08002178 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08002179
Chris Craik51d6a3d2014-12-22 17:16:56 -08002180 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002181 if (!colors) {
2182 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08002183 tempColors.reset(new int[colorsCount]);
2184 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
2185 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08002186 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002187
John Reckebd52612014-12-10 16:47:36 -08002188 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002189 const UvMapper& mapper(getMapper(texture));
2190
Romain Guy5a7b4662011-01-20 19:09:30 -08002191 for (int32_t y = 0; y < meshHeight; y++) {
2192 for (int32_t x = 0; x < meshWidth; x++) {
2193 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2194
2195 float u1 = float(x) / meshWidth;
2196 float u2 = float(x + 1) / meshWidth;
2197 float v1 = float(y) / meshHeight;
2198 float v2 = float(y + 1) / meshHeight;
2199
Romain Guy3b748a42013-04-17 18:54:38 -07002200 mapper.map(u1, v1, u2, v2);
2201
Romain Guy5a7b4662011-01-20 19:09:30 -08002202 int ax = i + (meshWidth + 1) * 2;
2203 int ay = ax + 1;
2204 int bx = i;
2205 int by = bx + 1;
2206 int cx = i + 2;
2207 int cy = cx + 1;
2208 int dx = i + (meshWidth + 1) * 2 + 2;
2209 int dy = dx + 1;
2210
Romain Guyff316ec2013-02-13 18:39:43 -08002211 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2212 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2213 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002214
Romain Guyff316ec2013-02-13 18:39:43 -08002215 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2216 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2217 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002218
Romain Guya92bb4d2012-10-16 11:08:44 -07002219 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2220 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2221 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2222 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002223 }
2224 }
2225
Chris Craikf0a59072013-11-19 18:00:46 -08002226 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002227 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002228 }
2229
Romain Guyff316ec2013-02-13 18:39:43 -08002230 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002231 texture = mCaches.textureCache.get(bitmap);
2232 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04002233 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002234 }
Romain Guyff316ec2013-02-13 18:39:43 -08002235 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002236 const AutoTexture autoCleanup(texture);
2237
Chris Craikef250742015-02-25 17:16:16 -08002238 if (USE_GLOPS) {
2239 /*
2240 * TODO: handle alpha_8 textures correctly by applying paint color, but *not*
2241 * shader in that case to mimic the behavior in SkiaCanvas::drawBitmapMesh.
2242 */
Chris Craikef250742015-02-25 17:16:16 -08002243 Glop glop;
Chris Craik6b109c72015-02-27 10:55:28 -08002244 GlopBuilder(mRenderState, mCaches, &glop)
2245 .setMeshColoredTexturedMesh(mesh.get(), elementCount)
Chris Craika6b52192015-02-27 17:43:02 -08002246 .setFillTexturePaint(*texture, static_cast<int>(TextureFillFlags::kNone), paint, currentSnapshot()->alpha)
Chris Craikef250742015-02-25 17:16:16 -08002247 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
2248 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
2249 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2250 .build();
2251 renderGlop(glop);
2252 return;
2253 }
2254
2255 mCaches.textureState().activateTexture(0);
Romain Guya92bb4d2012-10-16 11:08:44 -07002256 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik0519c8102015-02-11 13:17:06 -08002257 texture->setFilter(PaintUtils::getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002258
2259 int alpha;
2260 SkXfermode::Mode mode;
2261 getAlphaAndMode(paint, &alpha, &mode);
2262
Chris Craikef250742015-02-25 17:16:16 -08002263
2264 dirtyLayer(left, top, right, bottom, *currentTransform());
2265
Romain Guyff316ec2013-02-13 18:39:43 -08002266 float a = alpha / 255.0f;
Romain Guyff316ec2013-02-13 18:39:43 -08002267 setupDraw();
2268 setupDrawWithTextureAndColor();
2269 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002270 setupDrawColorFilter(getColorFilter(paint));
2271 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002272 setupDrawProgram();
2273 setupDrawDirtyRegionsDisabled();
Chris Craik117bdbc2015-02-05 10:12:38 -08002274 setupDrawModelView(kModelViewMode_Translate, false, 0, 0, 0, 0);
Romain Guyff316ec2013-02-13 18:39:43 -08002275 setupDrawTexture(texture->id);
2276 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002277 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002278 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002279
Chris Craikef250742015-02-25 17:16:16 -08002280 glDrawArrays(GL_TRIANGLES, 0, elementCount);
Romain Guyff316ec2013-02-13 18:39:43 -08002281
Chris Craik6c15ffa2015-02-02 13:50:55 -08002282 int slot = mCaches.program().getAttrib("colors");
Romain Guyff316ec2013-02-13 18:39:43 -08002283 if (slot >= 0) {
2284 glDisableVertexAttribArray(slot);
2285 }
2286
Tom Hudson107843d2014-09-08 11:26:26 -04002287 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002288}
2289
Chris Craik14100ac2015-02-24 13:46:29 -08002290void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, Rect src, Rect dst, const SkPaint* paint) {
2291 if (quickRejectSetupScissor(dst)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002292 return;
Romain Guy6926c722010-07-12 20:20:03 -07002293 }
2294
Romain Guy3b748a42013-04-17 18:54:38 -07002295 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002296 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002297 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002298
Chris Craik14100ac2015-02-24 13:46:29 -08002299 if (USE_GLOPS) {
2300 Rect uv(fmax(0.0f, src.left / texture->width),
2301 fmax(0.0f, src.top / texture->height),
2302 fmin(1.0f, src.right / texture->width),
2303 fmin(1.0f, src.bottom / texture->height));
2304
Chris Craika6b52192015-02-27 17:43:02 -08002305 int textureFillFlags = static_cast<int>((bitmap->colorType() == kAlpha_8_SkColorType)
2306 ? TextureFillFlags::kIsAlphaMaskTexture : TextureFillFlags::kNone);
Chris Craik14100ac2015-02-24 13:46:29 -08002307 Glop glop;
Chris Craik6b109c72015-02-27 10:55:28 -08002308 GlopBuilder(mRenderState, mCaches, &glop)
2309 .setMeshTexturedUvQuad(texture->uvMapper, uv)
Chris Craika6b52192015-02-27 17:43:02 -08002310 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik14100ac2015-02-24 13:46:29 -08002311 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
2312 .setModelViewMapUnitToRectSnap(dst)
2313 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2314 .build();
2315 renderGlop(glop);
2316 return;
2317 }
2318
2319 mCaches.textureState().activateTexture(0);
2320
Romain Guy8ba548f2010-06-30 19:21:21 -07002321 const float width = texture->width;
2322 const float height = texture->height;
2323
Chris Craik14100ac2015-02-24 13:46:29 -08002324 float u1 = fmax(0.0f, src.left / width);
2325 float v1 = fmax(0.0f, src.top / height);
2326 float u2 = fmin(1.0f, src.right / width);
2327 float v2 = fmin(1.0f, src.bottom / height);
Romain Guy3b748a42013-04-17 18:54:38 -07002328
2329 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002330
Chris Craik96a5c4c2015-01-27 15:46:35 -08002331 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002332 resetDrawTextureTexCoords(u1, v1, u2, v2);
2333
Romain Guyd21b6e12011-11-30 20:21:23 -08002334 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2335
Chris Craik14100ac2015-02-24 13:46:29 -08002336 float scaleX = (dst.right - dst.left) / (src.right - src.left);
2337 float scaleY = (dst.bottom - dst.top) / (src.bottom - src.top);
Romain Guy6620c6d2010-12-06 18:07:02 -08002338
Romain Guy886b2752013-01-04 12:26:18 -08002339 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
Romain Guy886b2752013-01-04 12:26:18 -08002340 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002341
Chris Craik14100ac2015-02-24 13:46:29 -08002342 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
Chris Craika6b52192015-02-27 17:43:02 -08002343 float x = floorf(dst.left + currentTransform()->getTranslateX() + 0.5f);
2344 float y = floorf(dst.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002345
Chris Craik14100ac2015-02-24 13:46:29 -08002346 dst.right = x + (dst.right - dst.left);
2347 dst.bottom = y + (dst.bottom - dst.top);
Romain Guy886b2752013-01-04 12:26:18 -08002348
Chris Craik14100ac2015-02-24 13:46:29 -08002349 dst.left = x;
2350 dst.top = y;
Romain Guy886b2752013-01-04 12:26:18 -08002351
Chris Craik0519c8102015-02-11 13:17:06 -08002352 texture->setFilter(scaled ? PaintUtils::getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002353 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002354 } else {
Chris Craik0519c8102015-02-11 13:17:06 -08002355 texture->setFilter(PaintUtils::getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002356 }
2357
Mike Reed1103b322014-07-08 12:36:44 -04002358 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik14100ac2015-02-24 13:46:29 -08002359 drawAlpha8TextureMesh(dst.left, dst.top, dst.right, dst.bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002360 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002361 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik117bdbc2015-02-05 10:12:38 -08002362 GL_TRIANGLE_STRIP, kUnitQuadCount, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002363 } else {
Chris Craik14100ac2015-02-24 13:46:29 -08002364 drawTextureMesh(dst.left, dst.top, dst.right, dst.bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002365 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002366 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik117bdbc2015-02-05 10:12:38 -08002367 GL_TRIANGLE_STRIP, kUnitQuadCount, false, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002368 }
2369
Romain Guy8ba548f2010-06-30 19:21:21 -07002370 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002371
Tom Hudson107843d2014-09-08 11:26:26 -04002372 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002373}
2374
Tom Hudson107843d2014-09-08 11:26:26 -04002375void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002376 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2377 const SkPaint* paint) {
Chris Craika6b52192015-02-27 17:43:02 -08002378 if (!mesh || !mesh->verticesCount || quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002379 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002380 }
2381
Chris Craika6b52192015-02-27 17:43:02 -08002382 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2383 if (!texture) return;
Chris Craik0556d902015-03-03 09:54:14 -08002384
Chris Craik8820fd12015-03-03 14:20:47 -08002385 if (USE_GLOPS) {
Chris Craik0556d902015-03-03 09:54:14 -08002386 // 9 patches are built for stretching - always filter
2387 int textureFillFlags = static_cast<int>(TextureFillFlags::kForceFilter);
2388 if (bitmap->colorType() == kAlpha_8_SkColorType) {
2389 textureFillFlags |= TextureFillFlags::kIsAlphaMaskTexture;
2390 }
2391 Glop glop;
2392 GlopBuilder(mRenderState, mCaches, &glop)
2393 .setMeshPatchQuads(*mesh)
2394 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
2395 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
2396 .setModelViewOffsetRectSnap(left, top, Rect(0, 0, right - left, bottom - top)) // TODO: get minimal bounds from patch
2397 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2398 .build();
2399 renderGlop(glop);
2400 return;
2401 }
2402
2403 mCaches.textureState().activateTexture(0);
Chris Craika6b52192015-02-27 17:43:02 -08002404 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002405
Chris Craika6b52192015-02-27 17:43:02 -08002406 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2407 texture->setFilter(GL_LINEAR, true);
Romain Guya4adcf02013-02-28 12:15:35 -08002408
Chris Craika6b52192015-02-27 17:43:02 -08002409 const bool pureTranslate = currentTransform()->isPureTranslate();
2410 // Mark the current layer dirty where we are going to draw the patch
2411 if (hasLayer() && mesh->hasEmptyQuads) {
2412 const float offsetX = left + currentTransform()->getTranslateX();
2413 const float offsetY = top + currentTransform()->getTranslateY();
2414 const size_t count = mesh->quads.size();
2415 for (size_t i = 0; i < count; i++) {
2416 const Rect& bounds = mesh->quads.itemAt(i);
2417 if (CC_LIKELY(pureTranslate)) {
2418 const float x = floorf(bounds.left + offsetX + 0.5f);
2419 const float y = floorf(bounds.top + offsetY + 0.5f);
2420 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
2421 } else {
2422 dirtyLayer(left + bounds.left, top + bounds.top,
2423 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy5b3b3522010-10-27 18:57:51 -07002424 }
2425 }
Romain Guy054dc182010-10-15 17:55:25 -07002426 }
Chet Haase48659092012-05-31 15:21:51 -07002427
Chris Craika6b52192015-02-27 17:43:02 -08002428 bool ignoreTransform = false;
2429 if (CC_LIKELY(pureTranslate)) {
2430 const float x = floorf(left + currentTransform()->getTranslateX() + 0.5f);
2431 const float y = floorf(top + currentTransform()->getTranslateY() + 0.5f);
2432
2433 right = x + right - left;
2434 bottom = y + bottom - top;
2435 left = x;
2436 top = y;
2437 ignoreTransform = true;
2438 }
2439 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
Chris Craik8820fd12015-03-03 14:20:47 -08002440 texture->blend, (GLvoid*) mesh->positionOffset, (GLvoid*) mesh->textureOffset,
Chris Craika6b52192015-02-27 17:43:02 -08002441 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2442 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
2443
Tom Hudson107843d2014-09-08 11:26:26 -04002444 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002445}
2446
Romain Guy03c00b52013-06-20 18:30:28 -07002447/**
2448 * Important note: this method is intended to draw batches of 9-patch objects and
2449 * will not set the scissor enable or dirty the current layer, if any.
2450 * The caller is responsible for properly dirtying the current layer.
2451 */
Tom Hudson107843d2014-09-08 11:26:26 -04002452void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craika6b52192015-02-27 17:43:02 -08002453 TextureVertex* vertices, uint32_t elementCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002454 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07002455 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002456 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002457 const AutoTexture autoCleanup(texture);
2458
Chris Craika6b52192015-02-27 17:43:02 -08002459 if (USE_GLOPS) {
2460 // TODO: get correct bounds from caller
2461 // 9 patches are built for stretching - always filter
2462 int textureFillFlags = static_cast<int>(TextureFillFlags::kForceFilter);
2463 if (bitmap->colorType() == kAlpha_8_SkColorType) {
2464 textureFillFlags |= TextureFillFlags::kIsAlphaMaskTexture;
2465 }
2466 Glop glop;
2467 GlopBuilder(mRenderState, mCaches, &glop)
2468 .setMeshTexturedIndexedQuads(vertices, elementCount)
2469 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
2470 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
2471 .setModelViewOffsetRect(0, 0, Rect(0, 0, 0, 0))
2472 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2473 .build();
2474 renderGlop(glop);
2475 return;
2476 }
2477
Romain Guy03c00b52013-06-20 18:30:28 -07002478 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2479 texture->setFilter(GL_LINEAR, true);
2480
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002481 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2482 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craika6b52192015-02-27 17:43:02 -08002483 GL_TRIANGLES, elementCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002484
Tom Hudson107843d2014-09-08 11:26:26 -04002485 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002486}
2487
Tom Hudson107843d2014-09-08 11:26:26 -04002488void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002489 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002490 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002491 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002492 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002493 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002494 }
2495
Chris Craik922d3a72015-02-13 17:47:21 -08002496 if (USE_GLOPS) {
Chris Craik117bdbc2015-02-05 10:12:38 -08002497 bool fudgeOffset = displayFlags & kVertexBuffer_Offset;
2498 bool shadowInterp = displayFlags & kVertexBuffer_ShadowInterp;
Chris Craik6b109c72015-02-27 10:55:28 -08002499 Glop glop;
2500 GlopBuilder(mRenderState, mCaches, &glop)
2501 .setMeshVertexBuffer(vertexBuffer, shadowInterp)
Chris Craik0519c8102015-02-11 13:17:06 -08002502 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craikf27133d2015-02-19 09:51:53 -08002503 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), fudgeOffset)
Chris Craik117bdbc2015-02-05 10:12:38 -08002504 .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds())
Chris Craik0519c8102015-02-11 13:17:06 -08002505 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik117bdbc2015-02-05 10:12:38 -08002506 .build();
2507 renderGlop(glop);
2508 return;
2509 }
2510
Chris Craik117bdbc2015-02-05 10:12:38 -08002511 const VertexBuffer::MeshFeatureFlags meshFeatureFlags = vertexBuffer.getMeshFeatureFlags();
Chris Craik0d5ac952014-07-15 13:01:02 -07002512 Rect bounds(vertexBuffer.getBounds());
2513 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002514 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2515
Chris Craikcb4d6002012-09-25 12:00:29 -07002516 int color = paint->getColor();
Chris Craik117bdbc2015-02-05 10:12:38 -08002517 bool isAA = meshFeatureFlags & VertexBuffer::kAlpha;
Chris Craikcb4d6002012-09-25 12:00:29 -07002518
Chris Craik710f46d2012-09-17 17:25:49 -07002519 setupDraw();
2520 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002521 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Chris Craik117bdbc2015-02-05 10:12:38 -08002522 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002523 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002524 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002525 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002526 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002527 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2528 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002529 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002530 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002531 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002532
ztenghui55bfb4e2013-12-03 10:38:55 -08002533 const void* vertices = vertexBuffer.getBuffer();
Chris Craik96a5c4c2015-01-27 15:46:35 -08002534 mRenderState.meshState().unbindMeshBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08002535 mRenderState.meshState().bindPositionVertexPointer(true, vertices,
2536 isAA ? kAlphaVertexStride : kVertexStride);
Chris Craik96a5c4c2015-01-27 15:46:35 -08002537 mRenderState.meshState().resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002538
Chris Craik710f46d2012-09-17 17:25:49 -07002539 int alphaSlot = -1;
2540 if (isAA) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002541 void* alphaCoords = ((GLbyte*) vertices) + kVertexAlphaOffset;
Chris Craik6c15ffa2015-02-02 13:50:55 -08002542 alphaSlot = mCaches.program().getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002543 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002544 glEnableVertexAttribArray(alphaSlot);
Chris Craik96a5c4c2015-01-27 15:46:35 -08002545 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, kAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002546 }
Romain Guy04299382012-07-18 17:15:41 -07002547
Chris Craik117bdbc2015-02-05 10:12:38 -08002548 if (meshFeatureFlags & VertexBuffer::kIndices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002549 mRenderState.meshState().unbindIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002550 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(),
2551 GL_UNSIGNED_SHORT, vertexBuffer.getIndices());
Chris Craik117bdbc2015-02-05 10:12:38 -08002552 } else {
2553 mRenderState.meshState().unbindIndicesBuffer();
2554 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
ztenghui63d41ab2014-02-14 13:13:41 -08002555 }
Romain Guy04299382012-07-18 17:15:41 -07002556
Chris Craik710f46d2012-09-17 17:25:49 -07002557 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002558 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002559 }
Chris Craik65cd6122012-12-10 17:56:27 -08002560
Tom Hudson107843d2014-09-08 11:26:26 -04002561 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002562}
2563
2564/**
Chris Craik65cd6122012-12-10 17:56:27 -08002565 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2566 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2567 * screen space in all directions. However, instead of using a fragment shader to compute the
2568 * translucency of the color from its position, we simply use a varying parameter to define how far
2569 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2570 *
2571 * Doesn't yet support joins, caps, or path effects.
2572 */
Tom Hudson107843d2014-09-08 11:26:26 -04002573void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002574 VertexBuffer vertexBuffer;
2575 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002576 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002577 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002578}
2579
2580/**
2581 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2582 * and additional geometry for defining an alpha slope perimeter.
2583 *
2584 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2585 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2586 * in-shader alpha region, but found it to be taxing on some GPUs.
2587 *
2588 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2589 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002590 */
Tom Hudson107843d2014-09-08 11:26:26 -04002591void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002592 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002593
Chris Craik65cd6122012-12-10 17:56:27 -08002594 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002595
Chris Craik65cd6122012-12-10 17:56:27 -08002596 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002597 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2598 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002599
Chris Craik05f3d6e2014-06-02 16:27:04 -07002600 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002601 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002602 }
2603
Chris Craikbf759452014-08-11 16:00:44 -07002604 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002605 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002606}
2607
Tom Hudson107843d2014-09-08 11:26:26 -04002608void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002609 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002610
Chris Craik6d29c8d2013-05-08 18:35:44 -07002611 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002612
Chris Craik6d29c8d2013-05-08 18:35:44 -07002613 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002614 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002615
Chris Craik05f3d6e2014-06-02 16:27:04 -07002616 const Rect& bounds = buffer.getBounds();
2617 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002618 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002619 }
2620
Chris Craikbf759452014-08-11 16:00:44 -07002621 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002622 drawVertexBuffer(buffer, paint, displayFlags);
2623
2624 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002625}
2626
Tom Hudson107843d2014-09-08 11:26:26 -04002627void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002628 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002629 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002630
Rob Tsuk487a92c2015-01-06 13:22:54 -08002631 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002632 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002633
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002634 SkPaint paint;
2635 paint.setColor(color);
2636 paint.setXfermodeMode(mode);
2637
2638 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002639
Tom Hudson107843d2014-09-08 11:26:26 -04002640 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002641}
Romain Guy9d5316e2010-06-24 19:30:36 -07002642
Chris Craik30036092015-02-12 10:41:39 -08002643void OpenGLRenderer::drawShape(float left, float top, PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002644 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002645 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002646 const AutoTexture autoCleanup(texture);
2647
2648 const float x = left + texture->left - texture->offset;
2649 const float y = top + texture->top - texture->offset;
2650
2651 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002652
Tom Hudson107843d2014-09-08 11:26:26 -04002653 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002654}
2655
Tom Hudson107843d2014-09-08 11:26:26 -04002656void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002657 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002658 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002659 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002660 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002661 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002662 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002663
Chris Craike84a2082014-12-22 14:28:49 -08002664 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002665 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08002666 PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002667 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002668 drawShape(left, top, texture, p);
2669 } else {
2670 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2671 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2672 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002673 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002674}
2675
Tom Hudson107843d2014-09-08 11:26:26 -04002676void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002677 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002678 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002679 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002680 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002681 }
Chris Craike84a2082014-12-22 14:28:49 -08002682 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002683 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08002684 PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002685 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002686 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002687 SkPath path;
2688 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2689 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2690 } else {
2691 path.addCircle(x, y, radius);
2692 }
2693 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002694 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002695}
Romain Guy01d58e42011-01-19 21:54:02 -08002696
Tom Hudson107843d2014-09-08 11:26:26 -04002697void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002698 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002699 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002700 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002701 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002702 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002703 }
Romain Guy01d58e42011-01-19 21:54:02 -08002704
Chris Craike84a2082014-12-22 14:28:49 -08002705 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002706 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08002707 PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002708 drawShape(left, top, texture, p);
2709 } else {
2710 SkPath path;
2711 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2712 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2713 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2714 }
2715 path.addOval(rect);
2716 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002717 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002718}
2719
Tom Hudson107843d2014-09-08 11:26:26 -04002720void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002721 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002722 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002723 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002724 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002725 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002726 }
2727
Chris Craik780c1282012-10-04 14:10:49 -07002728 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08002729 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002730 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08002731 PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002732 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002733 drawShape(left, top, texture, p);
2734 return;
Chris Craik780c1282012-10-04 14:10:49 -07002735 }
Chris Craik780c1282012-10-04 14:10:49 -07002736 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2737 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2738 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2739 }
2740
2741 SkPath path;
2742 if (useCenter) {
2743 path.moveTo(rect.centerX(), rect.centerY());
2744 }
2745 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2746 if (useCenter) {
2747 path.close();
2748 }
Tom Hudson107843d2014-09-08 11:26:26 -04002749 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002750}
2751
Romain Guycf8675e2012-10-02 12:32:25 -07002752// See SkPaintDefaults.h
2753#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2754
Tom Hudson107843d2014-09-08 11:26:26 -04002755void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002756 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002757 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002758 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002759 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002760 return;
Romain Guy6926c722010-07-12 20:20:03 -07002761 }
2762
Chris Craik710f46d2012-09-17 17:25:49 -07002763 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002764 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08002765 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07002766 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002767 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08002768 PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002769 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002770 drawShape(left, top, texture, p);
2771 } else {
2772 SkPath path;
2773 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2774 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2775 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2776 }
2777 path.addRect(rect);
2778 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002779 }
Chet Haase858aa932011-05-12 09:06:00 -07002780 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002781 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2782 SkPath path;
2783 path.addRect(left, top, right, bottom);
2784 drawConvexPath(path, p);
2785 } else {
2786 drawColorRect(left, top, right, bottom, p);
2787
2788 mDirty = true;
2789 }
Chet Haase858aa932011-05-12 09:06:00 -07002790 }
Romain Guyc7d53492010-06-25 13:41:57 -07002791}
Romain Guy9d5316e2010-06-24 19:30:36 -07002792
Chris Craikd218a922014-01-02 17:13:34 -08002793void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2794 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002795 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002796 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07002797
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002798 TextShadow textShadow;
2799 if (!getTextShadow(paint, &textShadow)) {
2800 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2801 }
2802
Raph Levien416a8472012-07-19 22:48:17 -07002803 // NOTE: The drop shadow will not perform gamma correction
2804 // if shader-based correction is enabled
2805 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Chris Craik2bb8f562015-02-17 16:42:02 -08002806 ShadowTexture* texture = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002807 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002808 // If the drop shadow exceeds the max texture size or couldn't be
2809 // allocated, skip drawing
Chris Craik2bb8f562015-02-17 16:42:02 -08002810 if (!texture) return;
2811 const AutoTexture autoCleanup(texture);
Raph Levien416a8472012-07-19 22:48:17 -07002812
Chris Craik2bb8f562015-02-17 16:42:02 -08002813 const float sx = x - texture->left + textShadow.dx;
2814 const float sy = y - texture->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002815
Chris Craik2bb8f562015-02-17 16:42:02 -08002816 if (USE_GLOPS) {
2817 Glop glop;
Chris Craik6b109c72015-02-27 10:55:28 -08002818 GlopBuilder(mRenderState, mCaches, &glop)
2819 .setMeshTexturedUnitQuad(nullptr)
Chris Craik2bb8f562015-02-17 16:42:02 -08002820 .setFillShadowTexturePaint(*texture, textShadow.color, *paint, currentSnapshot()->alpha)
Chris Craikf27133d2015-02-19 09:51:53 -08002821 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
Chris Craik2bb8f562015-02-17 16:42:02 -08002822 .setModelViewMapUnitToRect(Rect(sx, sy, sx + texture->width, sy + texture->height))
2823 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2824 .build();
2825 renderGlop(glop);
2826 return;
2827 }
2828
2829 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * currentSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002830 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002831 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002832 }
2833
2834 setupDraw();
2835 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002836 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002837 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002838 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002839 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002840 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002841 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
Chris Craik2bb8f562015-02-17 16:42:02 -08002842 sx, sy, sx + texture->width, sy + texture->height);
2843 setupDrawTexture(texture->id);
Raph Levien416a8472012-07-19 22:48:17 -07002844 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002845 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002846 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08002847 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Raph Levien416a8472012-07-19 22:48:17 -07002848
Chris Craik117bdbc2015-02-05 10:12:38 -08002849 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Raph Levien416a8472012-07-19 22:48:17 -07002850}
2851
Romain Guy768bffc2013-02-27 13:50:45 -08002852bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002853 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05002854 return MathUtils::isZero(alpha)
2855 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002856}
2857
Tom Hudson107843d2014-09-08 11:26:26 -04002858void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002859 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002860 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002861 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002862 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002863
Romain Guy671d6cf2012-01-18 12:39:17 -08002864 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002865 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002866 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002867 }
2868
Chris Craik65fe5ee2015-01-26 18:06:29 -08002869 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002870
Romain Guy671d6cf2012-01-18 12:39:17 -08002871 float x = 0.0f;
2872 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002873 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002874 if (pureTranslate) {
Chris Craika6b52192015-02-27 17:43:02 -08002875 x = floorf(x + currentTransform()->getTranslateX() + 0.5f);
2876 y = floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002877 }
2878
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002879 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002880 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002881
2882 int alpha;
2883 SkXfermode::Mode mode;
2884 getAlphaAndMode(paint, &alpha, &mode);
2885
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002886 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002887 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002888 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002889 }
2890
Romain Guy671d6cf2012-01-18 12:39:17 -08002891 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002892 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002893 if (pureTranslate && !linearFilter) {
2894 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2895 }
Romain Guy257ae352013-03-20 16:31:12 -07002896 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002897
Rob Tsuk487a92c2015-01-06 13:22:54 -08002898 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002899 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2900
Victoria Lease1e546812013-06-25 14:25:17 -07002901 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002902 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craik083e7332015-02-27 17:04:20 -08002903 positions, hasLayer() ? &bounds : nullptr, &functor)) {
2904 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2905 mDirty = true;
Romain Guy671d6cf2012-01-18 12:39:17 -08002906 }
Chet Haase48659092012-05-31 15:21:51 -07002907
Romain Guyeb9a5362012-01-17 17:39:26 -08002908}
2909
Chris Craik59744b72014-07-01 17:56:52 -07002910bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002911 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002912 outMatrix->setIdentity();
2913 return false;
2914 } else if (CC_UNLIKELY(transform.isPerspective())) {
2915 outMatrix->setIdentity();
2916 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002917 }
Chris Craik59744b72014-07-01 17:56:52 -07002918
2919 /**
Chris Craika736cd92014-08-04 13:18:38 -07002920 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2921 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002922 */
2923 float sx, sy;
2924 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002925 outMatrix->setScale(
2926 roundf(fmaxf(1.0f, sx)),
2927 roundf(fmaxf(1.0f, sy)));
2928 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002929}
2930
Tom Hudson984162f2014-10-10 13:38:16 -04002931int OpenGLRenderer::getSaveCount() const {
2932 return mState.getSaveCount();
2933}
2934
2935int OpenGLRenderer::save(int flags) {
2936 return mState.save(flags);
2937}
2938
2939void OpenGLRenderer::restore() {
Chris Craike2bb3802015-03-13 15:07:52 -07002940 mState.restore();
Tom Hudson984162f2014-10-10 13:38:16 -04002941}
2942
2943void OpenGLRenderer::restoreToCount(int saveCount) {
Chris Craike2bb3802015-03-13 15:07:52 -07002944 mState.restoreToCount(saveCount);
Tom Hudson984162f2014-10-10 13:38:16 -04002945}
2946
2947void OpenGLRenderer::translate(float dx, float dy, float dz) {
Chris Craike2bb3802015-03-13 15:07:52 -07002948 mState.translate(dx, dy, dz);
Tom Hudson984162f2014-10-10 13:38:16 -04002949}
2950
2951void OpenGLRenderer::rotate(float degrees) {
Chris Craike2bb3802015-03-13 15:07:52 -07002952 mState.rotate(degrees);
Tom Hudson984162f2014-10-10 13:38:16 -04002953}
2954
2955void OpenGLRenderer::scale(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002956 mState.scale(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002957}
2958
2959void OpenGLRenderer::skew(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002960 mState.skew(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002961}
2962
2963void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2964 mState.setMatrix(matrix);
2965}
2966
2967void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2968 mState.concatMatrix(matrix);
2969}
2970
2971bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2972 return mState.clipRect(left, top, right, bottom, op);
2973}
2974
2975bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2976 return mState.clipPath(path, op);
2977}
2978
2979bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2980 return mState.clipRegion(region, op);
2981}
2982
2983void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2984 mState.setClippingOutline(allocator, outline);
2985}
2986
2987void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2988 const Rect& rect, float radius, bool highPriority) {
2989 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2990}
2991
Tom Hudson107843d2014-09-08 11:26:26 -04002992void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002993 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002994 DrawOpMode drawOpMode) {
2995
Chris Craik03ae2722015-02-25 11:01:09 -08002996 if (drawOpMode == DrawOpMode::kImmediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002997 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2998 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002999 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08003000 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003001 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07003002 }
Romain Guycac5fd32011-12-01 20:08:50 -08003003 }
3004
Romain Guy3a3fa1b2010-12-06 18:47:50 -08003005 const float oldX = x;
3006 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08003007
Chris Craikd6b65f62014-01-01 14:45:21 -08003008 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08003009 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08003010
Romain Guy211370f2012-02-01 16:10:55 -08003011 if (CC_LIKELY(pureTranslate)) {
Chris Craika6b52192015-02-27 17:43:02 -08003012 x = floorf(x + transform.getTranslateX() + 0.5f);
3013 y = floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003014 }
3015
Romain Guy86568192010-12-14 15:55:39 -08003016 int alpha;
3017 SkXfermode::Mode mode;
3018 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07003019
Romain Guyc74f45a2013-02-26 19:10:14 -08003020 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
3021
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04003022 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07003023 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08003024 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003025 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07003026 }
3027
Romain Guy19d4dd82013-03-04 11:14:26 -08003028 const bool hasActiveLayer = hasLayer();
3029
Romain Guy624234f2013-03-05 16:43:31 -08003030 // We only pass a partial transform to the font renderer. That partial
3031 // matrix defines how glyphs are rasterized. Typically we want glyphs
3032 // to be rasterized at their final size on screen, which means the partial
3033 // matrix needs to take the scale factor into account.
3034 // When a partial matrix is used to transform glyphs during rasterization,
3035 // the mesh is generated with the inverse transform (in the case of scale,
3036 // the mesh is generated at 1.0 / scale for instance.) This allows us to
3037 // apply the full transform matrix at draw time in the vertex shader.
3038 // Applying the full matrix in the shader is the easiest way to handle
3039 // rotation and perspective and allows us to always generated quads in the
3040 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07003041 SkMatrix fontTransform;
3042 bool linearFilter = findBestFontTransform(transform, &fontTransform)
3043 || fabs(y - (int) y) > 0.0f
3044 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08003045 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07003046 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07003047
Romain Guy624234f2013-03-05 16:43:31 -08003048 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08003049 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07003050 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 -07003051
Raph Levien996e57c2012-07-23 15:22:52 -07003052 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07003053 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08003054
3055 // don't call issuedrawcommand, do it at end of batch
Chris Craik03ae2722015-02-25 11:01:09 -08003056 bool forceFinish = (drawOpMode != DrawOpMode::kDefer);
Romain Guya3dc55f2012-09-28 13:55:44 -07003057 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07003058 SkPaint paintCopy(*paint);
3059 paintCopy.setTextAlign(SkPaint::kLeft_Align);
3060 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08003061 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003062 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07003063 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08003064 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003065 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003066
Chris Craik03ae2722015-02-25 11:01:09 -08003067 if ((status || drawOpMode != DrawOpMode::kImmediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08003068 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07003069 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08003070 }
Chris Craik41541822013-05-03 16:35:54 -07003071 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07003072 }
Romain Guy694b5192010-07-21 21:33:20 -07003073
Chris Craike63f7c622013-10-17 10:30:55 -07003074 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07003075
Tom Hudson107843d2014-09-08 11:26:26 -04003076 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07003077}
3078
Tom Hudson107843d2014-09-08 11:26:26 -04003079void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08003080 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08003081 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003082 return;
Romain Guy03d58522012-02-24 17:54:07 -08003083 }
3084
Chris Craik39a908c2013-06-13 14:39:01 -07003085 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08003086 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07003087
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003088 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07003089 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07003090 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003091
3092 int alpha;
3093 SkXfermode::Mode mode;
3094 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07003095 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08003096
Tom Hudson984162f2014-10-10 13:38:16 -04003097 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08003098 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 -08003099
Romain Guy97771732012-02-28 18:17:02 -08003100 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chih-Hung Hsieh9ad6ac02015-02-27 16:11:36 -08003101 hOffset, vOffset, hasLayer() ? &bounds : nullptr, &functor)) {
Chris Craik083e7332015-02-27 17:04:20 -08003102 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
3103 mDirty = true;
Romain Guy97771732012-02-28 18:17:02 -08003104 }
Romain Guy325740f2012-02-24 16:48:34 -08003105}
3106
Tom Hudson107843d2014-09-08 11:26:26 -04003107void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003108 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07003109
Chris Craik44eb2c02015-01-29 09:45:09 -08003110 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003111
Chris Craik30036092015-02-12 10:41:39 -08003112 PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04003113 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07003114 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003115
Romain Guy8b55f372010-08-18 17:10:07 -07003116 const float x = texture->left - texture->offset;
3117 const float y = texture->top - texture->offset;
3118
Romain Guy01d58e42011-01-19 21:54:02 -08003119 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04003120 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07003121}
3122
Tom Hudson107843d2014-09-08 11:26:26 -04003123void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003124 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04003125 return;
Romain Guy35643dd2012-09-18 15:40:58 -07003126 }
3127
Chris Craike84a2082014-12-22 14:28:49 -08003128 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07003129 if (layer->isTextureLayer()) {
3130 transform = &layer->getTransform();
3131 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07003132 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08003133 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003134 }
3135 }
3136
Chris Craik39a908c2013-06-13 14:39:01 -07003137 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08003138 const bool rejected = mState.calculateQuickRejectForScissor(
3139 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3140 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07003141
3142 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003143 if (transform && !transform->isIdentity()) {
3144 restore();
3145 }
Tom Hudson107843d2014-09-08 11:26:26 -04003146 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08003147 }
3148
Chris Craik62d307c2014-07-29 10:35:13 -07003149 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
3150 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
3151
Romain Guy5bb3c732012-11-29 17:52:58 -08003152 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003153
Chris Craik65fe5ee2015-01-26 18:06:29 -08003154 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08003155 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003156
Romain Guy211370f2012-02-01 16:10:55 -08003157 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08003158 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003159 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3160 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003161 } else if (layer->mesh) {
Chris Craikf27133d2015-02-19 09:51:53 -08003162 if (USE_GLOPS) {
3163 Glop glop;
Chris Craik6b109c72015-02-27 10:55:28 -08003164 GlopBuilder(mRenderState, mCaches, &glop)
3165 .setMeshTexturedIndexedQuads(layer->mesh, layer->meshElementCount)
Chris Craik182952f2015-03-09 14:17:29 -07003166 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
Chris Craikf27133d2015-02-19 09:51:53 -08003167 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
3168 .setModelViewOffsetRectSnap(x, y, Rect(0, 0, layer->layer.getWidth(), layer->layer.getHeight()))
3169 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
3170 .build();
3171 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy9ace8f52011-07-07 20:50:11 -07003172 } else {
Chris Craikf27133d2015-02-19 09:51:53 -08003173 const float a = getLayerAlpha(layer);
3174 setupDraw();
3175 setupDrawWithTexture();
3176 setupDrawColor(a, a, a, a);
3177 setupDrawColorFilter(layer->getColorFilter());
3178 setupDrawBlending(layer);
3179 setupDrawProgram();
3180 setupDrawPureColorUniforms();
3181 setupDrawColorFilterUniforms(layer->getColorFilter());
3182 setupDrawTexture(layer->getTextureId());
3183 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3184 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3185 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
3186
3187 layer->setFilter(GL_NEAREST);
3188 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
3189 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
3190 } else {
3191 layer->setFilter(GL_LINEAR);
3192 setupDrawModelView(kModelViewMode_Translate, false, x, y,
3193 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3194 }
3195
Chris Craikf27133d2015-02-19 09:51:53 -08003196 TextureVertex* mesh = &layer->mesh[0];
3197 GLsizei elementsCount = layer->meshElementCount;
3198
Chris Craikf27133d2015-02-19 09:51:53 -08003199 while (elementsCount > 0) {
3200 GLsizei drawCount = MathUtils::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
3201
3202 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
3203 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3204 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr));
3205
3206 elementsCount -= drawCount;
3207 // Though there are 4 vertices in a quad, we use 6 indices per
3208 // quad to draw with GL_TRIANGLES
3209 mesh += (drawCount / 6) * 4;
3210 }
Romain Guy9ace8f52011-07-07 20:50:11 -07003211 }
Romain Guy3a3133d2011-02-01 22:59:58 -08003212#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003213 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003214#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003215 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003216
Romain Guy5bb3c732012-11-29 17:52:58 -08003217 if (layer->debugDrawUpdate) {
3218 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003219
3220 SkPaint paint;
3221 paint.setColor(0x7f00ff00);
3222 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003223 }
Romain Guyf219da52011-01-16 12:54:25 -08003224 }
Chris Craik34416ea2013-04-15 16:08:28 -07003225 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003226
Romain Guyb2e2f242012-10-17 18:18:35 -07003227 if (transform && !transform->isIdentity()) {
3228 restore();
3229 }
3230
Tom Hudson107843d2014-09-08 11:26:26 -04003231 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003232}
3233
Romain Guy6926c722010-07-12 20:20:03 -07003234///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003235// Draw filters
3236///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003237void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3238 // We should never get here since we apply the draw filter when stashing
3239 // the paints in the DisplayList.
3240 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003241}
3242
3243///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003244// Drawing implementation
3245///////////////////////////////////////////////////////////////////////////////
3246
Chris Craikd218a922014-01-02 17:13:34 -08003247Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003248 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003249 if (!texture) {
3250 return mCaches.textureCache.get(bitmap);
3251 }
3252 return texture;
3253}
3254
Chris Craik2bb8f562015-02-17 16:42:02 -08003255void OpenGLRenderer::drawPathTexture(PathTexture* texture, float x, float y,
3256 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003257 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003258 return;
3259 }
3260
Chris Craik922d3a72015-02-13 17:47:21 -08003261 if (USE_GLOPS) {
Chris Craik30036092015-02-12 10:41:39 -08003262 Glop glop;
Chris Craik6b109c72015-02-27 10:55:28 -08003263 GlopBuilder(mRenderState, mCaches, &glop)
3264 .setMeshTexturedUnitQuad(nullptr)
Chris Craik30036092015-02-12 10:41:39 -08003265 .setFillPathTexturePaint(*texture, *paint, currentSnapshot()->alpha)
Chris Craikf27133d2015-02-19 09:51:53 -08003266 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
Chris Craik30036092015-02-12 10:41:39 -08003267 .setModelViewMapUnitToRect(Rect(x, y, x + texture->width, y + texture->height))
3268 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
3269 .build();
3270 renderGlop(glop);
3271 return;
3272 }
3273
3274
Romain Guy01d58e42011-01-19 21:54:02 -08003275 int alpha;
3276 SkXfermode::Mode mode;
3277 getAlphaAndMode(paint, &alpha, &mode);
3278
3279 setupDraw();
3280 setupDrawWithTexture(true);
3281 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003282 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003283 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003284 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003285 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003286 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3287 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003288 setupDrawTexture(texture->id);
3289 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003290 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003291 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08003292 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Romain Guy01d58e42011-01-19 21:54:02 -08003293
Chris Craik117bdbc2015-02-05 10:12:38 -08003294 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003295}
3296
Romain Guyf607bdc2010-09-10 19:20:06 -07003297// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003298#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3299#define kStdUnderline_Offset (1.0f / 9.0f)
3300#define kStdUnderline_Thickness (1.0f / 18.0f)
3301
Chris Craikd218a922014-01-02 17:13:34 -08003302void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3303 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003304 // Handle underline and strike-through
3305 uint32_t flags = paint->getFlags();
3306 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003307 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003308
Romain Guy211370f2012-02-01 16:10:55 -08003309 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003310 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003311 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003312
Raph Levien8b4072d2012-07-30 15:50:00 -07003313 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003314 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003315
Romain Guyf6834472011-01-23 13:32:12 -08003316 int linesCount = 0;
3317 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3318 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3319
3320 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003321 float points[pointsCount];
3322 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003323
3324 if (flags & SkPaint::kUnderlineText_Flag) {
3325 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003326 points[currentPoint++] = left;
3327 points[currentPoint++] = top;
3328 points[currentPoint++] = left + underlineWidth;
3329 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003330 }
3331
3332 if (flags & SkPaint::kStrikeThruText_Flag) {
3333 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003334 points[currentPoint++] = left;
3335 points[currentPoint++] = top;
3336 points[currentPoint++] = left + underlineWidth;
3337 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003338 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003339
Romain Guy726aeba2011-06-01 14:52:00 -07003340 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003341
Romain Guy726aeba2011-06-01 14:52:00 -07003342 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003343 }
3344 }
3345}
3346
Tom Hudson107843d2014-09-08 11:26:26 -04003347void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003348 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003349 return;
Romain Guy672433d2013-01-04 19:05:13 -08003350 }
3351
Tom Hudson107843d2014-09-08 11:26:26 -04003352 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003353}
3354
Tom Hudson107843d2014-09-08 11:26:26 -04003355void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003356 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003357 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003358
Chris Craik15a07a22014-01-26 13:43:53 -08003359 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08003360 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07003361
3362 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003363 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003364
ztenghui14a4e352014-08-13 10:44:39 -07003365 // The caller has made sure casterAlpha > 0.
3366 float ambientShadowAlpha = mAmbientShadowAlpha;
3367 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3368 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3369 }
3370 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3371 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003372 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003373 }
ztenghui7b4516e2014-01-07 10:42:55 -08003374
ztenghui14a4e352014-08-13 10:44:39 -07003375 float spotShadowAlpha = mSpotShadowAlpha;
3376 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3377 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3378 }
3379 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3380 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003381 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003382 }
ztenghui7b4516e2014-01-07 10:42:55 -08003383
Tom Hudson107843d2014-09-08 11:26:26 -04003384 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003385}
3386
Tom Hudson107843d2014-09-08 11:26:26 -04003387void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003388 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003389 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003390 return;
Romain Guy3b753822013-03-05 10:27:35 -08003391 }
Romain Guy735738c2012-12-03 12:34:51 -08003392
Romain Guy672433d2013-01-04 19:05:13 -08003393 float left = FLT_MAX;
3394 float top = FLT_MAX;
3395 float right = FLT_MIN;
3396 float bottom = FLT_MIN;
3397
Romain Guy448455f2013-07-22 13:57:50 -07003398 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003399 Vertex* vertex = mesh;
3400
Chris Craik2af46352012-11-26 18:30:17 -08003401 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003402 float l = rects[index + 0];
3403 float t = rects[index + 1];
3404 float r = rects[index + 2];
3405 float b = rects[index + 3];
3406
Romain Guy3b753822013-03-05 10:27:35 -08003407 Vertex::set(vertex++, l, t);
3408 Vertex::set(vertex++, r, t);
3409 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003410 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003411
Romain Guy3b753822013-03-05 10:27:35 -08003412 left = fminf(left, l);
3413 top = fminf(top, t);
3414 right = fmaxf(right, r);
3415 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003416 }
3417
Chris Craikf0a59072013-11-19 18:00:46 -08003418 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003419 return;
Romain Guya362c692013-02-04 13:50:16 -08003420 }
Romain Guy672433d2013-01-04 19:05:13 -08003421
Chris Craik922d3a72015-02-13 17:47:21 -08003422 if (USE_GLOPS) {
Chris Craik2ab95d72015-02-06 15:25:51 -08003423 const Matrix4& transform = ignoreTransform ? Matrix4::identity() : *currentTransform();
3424 Glop glop;
Chris Craik6b109c72015-02-27 10:55:28 -08003425 GlopBuilder(mRenderState, mCaches, &glop)
3426 .setMeshIndexedQuads(&mesh[0], count / 4)
Chris Craik0519c8102015-02-11 13:17:06 -08003427 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craikf27133d2015-02-19 09:51:53 -08003428 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
Chris Craik2ab95d72015-02-06 15:25:51 -08003429 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
Chris Craik0519c8102015-02-11 13:17:06 -08003430 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik2ab95d72015-02-06 15:25:51 -08003431 .build();
3432 renderGlop(glop);
3433 return;
3434 }
3435
3436 int color = paint->getColor();
3437 // If a shader is set, preserve only the alpha
3438 if (getShader(paint)) {
3439 color |= 0x00ffffff;
3440 }
3441
Romain Guy672433d2013-01-04 19:05:13 -08003442 setupDraw();
3443 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003444 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003445 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003446 setupDrawColorFilter(getColorFilter(paint));
3447 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003448 setupDrawProgram();
3449 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003450 setupDrawModelView(kModelViewMode_Translate, false,
3451 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003452 setupDrawColorUniforms(getShader(paint));
3453 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003454 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003455
Romain Guy8ce00302013-01-15 18:51:42 -08003456 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003457 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003458 }
3459
Chris Craik4063a0e2013-11-15 16:06:56 -08003460 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003461
Tom Hudson107843d2014-09-08 11:26:26 -04003462 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003463}
3464
Romain Guy026c5e162010-06-28 17:12:22 -07003465void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003466 const SkPaint* paint, bool ignoreTransform) {
Chris Craik117bdbc2015-02-05 10:12:38 -08003467
Chris Craik922d3a72015-02-13 17:47:21 -08003468 if (USE_GLOPS) {
Chris Craik117bdbc2015-02-05 10:12:38 -08003469 const Matrix4& transform = ignoreTransform ? Matrix4::identity() : *currentTransform();
3470 Glop glop;
Chris Craik6b109c72015-02-27 10:55:28 -08003471 GlopBuilder(mRenderState, mCaches, &glop)
3472 .setMeshUnitQuad()
Chris Craik0519c8102015-02-11 13:17:06 -08003473 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craikf27133d2015-02-19 09:51:53 -08003474 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
Chris Craik117bdbc2015-02-05 10:12:38 -08003475 .setModelViewMapUnitToRect(Rect(left, top, right, bottom))
Chris Craik0519c8102015-02-11 13:17:06 -08003476 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik117bdbc2015-02-05 10:12:38 -08003477 .build();
3478 renderGlop(glop);
3479 return;
3480 }
3481
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003482 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003483 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003484 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003485 color |= 0x00ffffff;
3486 }
3487
Romain Guy70ca14e2010-12-13 18:24:33 -08003488 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003489 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003490 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003491 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003492 setupDrawColorFilter(getColorFilter(paint));
3493 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003494 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003495 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3496 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003497 setupDrawColorUniforms(getShader(paint));
3498 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003499 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003500 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003501
Chris Craik117bdbc2015-02-05 10:12:38 -08003502 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Romain Guyc95c8d62010-09-17 15:31:32 -07003503}
3504
Chris Craik0519c8102015-02-11 13:17:06 -08003505void OpenGLRenderer::drawTextureRect(Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003506 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003507
Chris Craike84a2082014-12-22 14:28:49 -08003508 GLvoid* vertices = (GLvoid*) nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -08003509 GLvoid* texCoords = (GLvoid*) kMeshTextureOffset;
Romain Guy3b748a42013-04-17 18:54:38 -07003510
3511 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003512 vertices = &mMeshVertices[0].x;
3513 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003514
3515 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3516 texture->uvMapper->map(uvs);
3517
3518 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3519 }
3520
Chris Craikd6b65f62014-01-01 14:45:21 -08003521 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
Chris Craika6b52192015-02-27 17:43:02 -08003522 const float x = floorf(currentTransform()->getTranslateX() + 0.5f);
3523 const float y = floorf(currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003524
Romain Guyd21b6e12011-11-30 20:21:23 -08003525 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003526 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003527 paint, texture->blend, vertices, texCoords,
Chris Craik117bdbc2015-02-05 10:12:38 -08003528 GL_TRIANGLE_STRIP, kUnitQuadCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003529 } else {
Chris Craik0519c8102015-02-11 13:17:06 -08003530 texture->setFilter(PaintUtils::getFilter(paint), true);
3531 drawTextureMesh(0, 0, texture->width, texture->height, texture->id, paint,
Chris Craik117bdbc2015-02-05 10:12:38 -08003532 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, kUnitQuadCount);
Romain Guy3b748a42013-04-17 18:54:38 -07003533 }
3534
3535 if (texture->uvMapper) {
3536 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003537 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003538}
3539
Romain Guyf7f93552010-07-08 19:17:03 -07003540void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003541 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003542 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003543 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3544 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003545
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003546 int a;
3547 SkXfermode::Mode mode;
3548 getAlphaAndMode(paint, &a, &mode);
3549 const float alpha = a / 255.0f;
3550
Romain Guy746b7402010-10-26 16:27:31 -07003551 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003552 setupDrawWithTexture();
3553 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003554 setupDrawColorFilter(getColorFilter(paint));
3555 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003556 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003557 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003558 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003559 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003560 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003561 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003562 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003563
Romain Guy6820ac82010-09-15 18:11:50 -07003564 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003565}
3566
Romain Guy3b748a42013-04-17 18:54:38 -07003567void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003568 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003569 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003570 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3571 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003572
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003573 int a;
3574 SkXfermode::Mode mode;
3575 getAlphaAndMode(paint, &a, &mode);
3576 const float alpha = a / 255.0f;
3577
Romain Guy3b748a42013-04-17 18:54:38 -07003578 setupDraw();
3579 setupDrawWithTexture();
3580 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003581 setupDrawColorFilter(getColorFilter(paint));
3582 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003583 setupDrawProgram();
3584 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003585 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003586 setupDrawTexture(texture);
3587 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003588 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003589 setupDrawMeshIndices(vertices, texCoords, vbo);
3590
Chris Craike84a2082014-12-22 14:28:49 -08003591 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy3b748a42013-04-17 18:54:38 -07003592}
3593
Romain Guy886b2752013-01-04 12:26:18 -08003594void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003595 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003596 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003597 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003598
Chris Craike84a2082014-12-22 14:28:49 -08003599 int color = paint != nullptr ? paint->getColor() : 0;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003600 int alpha;
3601 SkXfermode::Mode mode;
3602 getAlphaAndMode(paint, &alpha, &mode);
3603
Romain Guy886b2752013-01-04 12:26:18 -08003604 setupDraw();
3605 setupDrawWithTexture(true);
Chris Craike84a2082014-12-22 14:28:49 -08003606 if (paint != nullptr) {
Romain Guy886b2752013-01-04 12:26:18 -08003607 setupDrawAlpha8Color(color, alpha);
3608 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003609 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003610 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003611 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003612 setupDrawProgram();
3613 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003614 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003615 setupDrawTexture(texture);
3616 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003617 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003618 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003619 setupDrawMesh(vertices, texCoords);
3620
3621 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003622}
3623
Romain Guya5aed0d2010-09-09 14:42:43 -07003624void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003625 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003626
Chris Craik117bdbc2015-02-05 10:12:38 -08003627 if (currentSnapshot()->roundRectClipState != nullptr /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003628 blend = true;
3629 mDescription.hasRoundRectClip = true;
3630 }
3631 mSkipOutlineClip = true;
3632
Romain Guy82ba8142010-07-09 13:25:56 -07003633 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003634
Romain Guy82ba8142010-07-09 13:25:56 -07003635 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003636 // These blend modes are not supported by OpenGL directly and have
3637 // to be implemented using shaders. Since the shader will perform
3638 // the blending, turn blending off here
3639 // If the blend mode cannot be implemented using shaders, fall
3640 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003641 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Chris Craik117bdbc2015-02-05 10:12:38 -08003642 if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003643 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003644 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003645
Chris Craik44eb2c02015-01-29 09:45:09 -08003646 mRenderState.blend().disable();
Romain Guy82bc7a72012-01-03 14:13:39 -08003647 return;
3648 } else {
3649 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003650 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003651 }
Chris Craik182952f2015-03-09 14:17:29 -07003652 mRenderState.blend().enable(mode,
3653 swapSrcDst ? Blend::ModeOrderSwap::Swap : Blend::ModeOrderSwap::NoSwap);
Chris Craik44eb2c02015-01-29 09:45:09 -08003654 } else {
3655 mRenderState.blend().disable();
Romain Guy82ba8142010-07-09 13:25:56 -07003656 }
Romain Guybd6b79b2010-06-26 00:13:53 -07003657}
3658
Romain Guy026c5e162010-06-28 17:12:22 -07003659void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003660 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003661 TextureVertex::setUV(v++, u1, v1);
3662 TextureVertex::setUV(v++, u2, v1);
3663 TextureVertex::setUV(v++, u1, v2);
3664 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003665}
3666
Chris Craike84a2082014-12-22 14:28:49 -08003667void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
3668 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003669 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003670 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3671 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003672 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003673 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003674 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003675}
3676
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003677float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003678 float alpha;
3679 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3680 alpha = mDrawModifiers.mOverrideLayerAlpha;
3681 } else {
3682 alpha = layer->getAlpha() / 255.0f;
3683 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003684 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003685}
3686
Romain Guy9d5316e2010-06-24 19:30:36 -07003687}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003688}; // namespace android