blob: a00a2bc1ff47940c6657af03d9912cfde321e7e7 [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 Craik0519c812015-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///////////////////////////////////////////////////////////////////////////////
66// Globals
67///////////////////////////////////////////////////////////////////////////////
68
Romain Guyf607bdc2010-09-10 19:20:06 -070069
Romain Guyf6a11b82010-06-23 17:47:49 -070070///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -070071// Functions
72///////////////////////////////////////////////////////////////////////////////
73
74template<typename T>
75static inline T min(T a, T b) {
76 return a < b ? a : b;
77}
78
79///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -070080// Constructors/destructor
81///////////////////////////////////////////////////////////////////////////////
82
John Reck3b202512014-06-23 13:13:08 -070083OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -040084 : mState(*this)
Chris Craik058fc642014-07-23 18:19:28 -070085 , mCaches(Caches::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -070086 , mRenderState(renderState)
Chris Craik65fe5ee2015-01-26 18:06:29 -080087 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -070088 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -070089 , mSuppressTiling(false)
90 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -040091 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -070092 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -070093 , mLightRadius(FLT_MIN)
94 , mAmbientShadowAlpha(0)
95 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -080096 // *set* draw modifiers to be 0
97 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -070098 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -070099
Chris Craik03188872015-02-02 18:39:33 -0800100 memcpy(mMeshVertices, kUnitQuadVertices, sizeof(kUnitQuadVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700101}
102
Romain Guy85bf02f2010-06-22 13:11:24 -0700103OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700104 // The context has already been destroyed at this point, do not call
105 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700106}
107
Romain Guy87e2f7572012-09-24 11:37:12 -0700108void OpenGLRenderer::initProperties() {
109 char property[PROPERTY_VALUE_MAX];
110 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
111 mScissorOptimizationDisabled = !strcasecmp(property, "true");
112 INIT_LOGD(" Scissor optimization %s",
113 mScissorOptimizationDisabled ? "disabled" : "enabled");
114 } else {
115 INIT_LOGD(" Scissor optimization enabled");
116 }
Romain Guy13631f32012-01-30 17:41:55 -0800117}
118
Chris Craik058fc642014-07-23 18:19:28 -0700119void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
120 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
121 mLightCenter = lightCenter;
122 mLightRadius = lightRadius;
123 mAmbientShadowAlpha = ambientShadowAlpha;
124 mSpotShadowAlpha = spotShadowAlpha;
125}
126
Romain Guy13631f32012-01-30 17:41:55 -0800127///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700128// Setup
129///////////////////////////////////////////////////////////////////////////////
130
Chris Craik797b95b2014-05-20 18:10:25 -0700131void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700132 glDisable(GL_DITHER);
133 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Chris Craik284b2432014-09-18 16:05:35 -0700134 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700135}
136
Romain Guy96885eb2013-03-26 15:05:58 -0700137void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800138 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800139 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400140 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700141 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700142 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700143}
144
Tom Hudson107843d2014-09-08 11:26:26 -0400145void OpenGLRenderer::startFrame() {
146 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700147 mFrameStarted = true;
148
Tom Hudson984162f2014-10-10 13:38:16 -0400149 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700150
Romain Guy96885eb2013-03-26 15:05:58 -0700151 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700152
Tom Hudson984162f2014-10-10 13:38:16 -0400153 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800154
Romain Guy54c1a642012-09-27 17:55:46 -0700155 // Functors break the tiling extension in pretty spectacular ways
156 // This ensures we don't use tiling when a functor is going to be
157 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700158 mSuppressTiling = mCaches.hasRegisteredFunctors()
159 || mFirstFrameAfterResize;
160 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700161
Chris Craikd6b65f62014-01-01 14:45:21 -0800162 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700163
Romain Guy7c450aa2012-09-21 19:15:00 -0700164 debugOverdraw(true, true);
165
Tom Hudson107843d2014-09-08 11:26:26 -0400166 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700167 mTilingClip.right, mTilingClip.bottom, mOpaque);
168}
169
Tom Hudson107843d2014-09-08 11:26:26 -0400170void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700171 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700172
Romain Guy96885eb2013-03-26 15:05:58 -0700173 setupFrameState(left, top, right, bottom, opaque);
174
175 // Layer renderers will start the frame immediately
176 // The framebuffer renderer will first defer the display list
177 // for each layer and wait until the first drawing command
178 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800179 if (currentSnapshot()->fbo == 0) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800180 mRenderState.blend().syncEnabled();
Romain Guy96885eb2013-03-26 15:05:58 -0700181 updateLayers();
182 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400183 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700184 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700185}
186
Romain Guydcfc8362013-01-03 13:08:57 -0800187void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
188 // If we know that we are going to redraw the entire framebuffer,
189 // perform a discard to let the driver know we don't need to preserve
190 // the back buffer for this frame.
Chris Craik117bdbc2015-02-05 10:12:38 -0800191 if (mCaches.extensions().hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400192 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
193 const bool isFbo = onGetTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800194 const GLenum attachments[] = {
195 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
196 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800197 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
198 }
199}
200
Tom Hudson107843d2014-09-08 11:26:26 -0400201void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700202 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800203 mRenderState.scissor().setEnabled(true);
204 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700205 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400206 mDirty = true;
207 return;
Romain Guyddf74372012-05-22 14:07:07 -0700208 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700209
Chris Craik65fe5ee2015-01-26 18:06:29 -0800210 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700211}
212
henry.uh_chen33f5a592014-07-02 19:36:56 +0800213void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700214 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800215 const Snapshot* snapshot = currentSnapshot();
216
Chris Craik14e51302013-12-30 15:32:54 -0800217 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800218 if (snapshot->flags & Snapshot::kFlagFboTarget) {
219 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700220 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700221
henry.uh_chen33f5a592014-07-02 19:36:56 +0800222 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800223 }
224}
225
henry.uh_chen33f5a592014-07-02 19:36:56 +0800226void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800227 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800228 if(expand) {
229 // Expand the startTiling region by 1
230 int leftNotZero = (clip.left > 0) ? 1 : 0;
231 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
232
233 mCaches.startTiling(
234 clip.left - leftNotZero,
235 windowHeight - clip.bottom - topNotZero,
236 clip.right - clip.left + leftNotZero + 1,
237 clip.bottom - clip.top + topNotZero + 1,
238 opaque);
239 } else {
240 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800241 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800242 }
Romain Guy54c1a642012-09-27 17:55:46 -0700243 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700244}
245
246void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700247 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700248}
249
Tom Hudson107843d2014-09-08 11:26:26 -0400250bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700251 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700252 endTiling();
Chris Craik4ac36f82014-12-09 16:54:03 -0800253 mTempPaths.clear();
254
Romain Guyca89e2a2013-03-08 17:44:20 -0800255 // When finish() is invoked on FBO 0 we've reached the end
256 // of the current frame
Tom Hudson984162f2014-10-10 13:38:16 -0400257 if (onGetTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800258 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700259 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800260 }
261
Romain Guy11cb6422012-09-21 00:39:43 -0700262 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700263#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700264 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700265#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700266
Romain Guyc15008e2010-11-10 11:59:15 -0800267#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800268 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700269#else
270 if (mCaches.getDebugLevel() & kDebugMemory) {
271 mCaches.dumpMemoryUsage();
272 }
Romain Guyc15008e2010-11-10 11:59:15 -0800273#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700274 }
Romain Guy96885eb2013-03-26 15:05:58 -0700275
276 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400277
278 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700279}
280
Romain Guy35643dd2012-09-18 15:40:58 -0700281void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700282 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
283 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700284 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700285
Chris Craik65fe5ee2015-01-26 18:06:29 -0800286 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700287 dirtyClip();
288}
289
Andreas Gampe64bb4132014-11-22 00:35:09 +0000290void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400291 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700292
Rob Tsuk487a92c2015-01-06 13:22:54 -0800293 Rect clip(mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700294 clip.snapToPixelBoundaries();
295
Romain Guyd643bb52011-03-01 14:55:21 -0800296 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800297 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800298 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800299 dirtyLayerUnchecked(clip, getRegion());
300 }
Romain Guyd643bb52011-03-01 14:55:21 -0800301
Romain Guy08aa2cb2011-03-17 11:06:57 -0700302 DrawGlInfo info;
303 info.clipLeft = clip.left;
304 info.clipTop = clip.top;
305 info.clipRight = clip.right;
306 info.clipBottom = clip.bottom;
307 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700308 info.width = getViewportWidth();
309 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800310 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700311
Tom Hudson984162f2014-10-10 13:38:16 -0400312 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700313 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400314 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700315 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
316 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800317 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700318 setScissorFromClip();
319 }
Chris Craik54f574a2013-08-26 11:23:46 -0700320
John Reck3b202512014-06-23 13:13:08 -0700321 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
322 // Scissor may have been modified, reset dirty clip
323 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800324
Tom Hudson107843d2014-09-08 11:26:26 -0400325 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800326}
327
Romain Guyf6a11b82010-06-23 17:47:49 -0700328///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700329// Debug
330///////////////////////////////////////////////////////////////////////////////
331
Chris Craik62d307c2014-07-29 10:35:13 -0700332void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
333#if DEBUG_DETAILED_EVENTS
334 const int BUFFER_SIZE = 256;
335 va_list ap;
336 char buf[BUFFER_SIZE];
337
338 va_start(ap, fmt);
339 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
340 va_end(ap);
341
342 eventMark(buf);
343#endif
344}
345
346
Romain Guy0f667532013-03-01 14:31:04 -0800347void OpenGLRenderer::eventMark(const char* name) const {
348 mCaches.eventMark(0, name);
349}
350
Romain Guy87e2f7572012-09-24 11:37:12 -0700351void OpenGLRenderer::startMark(const char* name) const {
352 mCaches.startMark(0, name);
353}
354
355void OpenGLRenderer::endMark() const {
356 mCaches.endMark();
357}
358
359void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700360 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700361}
362
363void OpenGLRenderer::renderOverdraw() {
Tom Hudson984162f2014-10-10 13:38:16 -0400364 if (mCaches.debugOverdraw && onGetTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700365 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700366
Chris Craik65fe5ee2015-01-26 18:06:29 -0800367 mRenderState.scissor().setEnabled(true);
368 mRenderState.scissor().set(clip->left,
369 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
370 clip->right - clip->left,
371 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700372
Romain Guy627c6fd2013-08-21 11:53:18 -0700373 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800374 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700375 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
376
377 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800378 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700379 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
380
381 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800382 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700383 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
384
385 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800386 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700387 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
388
Chris Craik96a5c4c2015-01-27 15:46:35 -0800389 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700390 }
391}
392
393///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700394// Layers
395///////////////////////////////////////////////////////////////////////////////
396
397bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700398 if (layer->deferredUpdateScheduled && layer->renderer
399 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700400
Romain Guy7c450aa2012-09-21 19:15:00 -0700401 if (inFrame) {
402 endTiling();
403 debugOverdraw(false, false);
404 }
Romain Guy11cb6422012-09-21 00:39:43 -0700405
Romain Guy96885eb2013-03-26 15:05:58 -0700406 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700407 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700408 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700409 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700410 }
Romain Guy11cb6422012-09-21 00:39:43 -0700411
412 if (inFrame) {
413 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800414 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700415 }
416
Romain Guy5bb3c732012-11-29 17:52:58 -0800417 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700418 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700419
420 return true;
421 }
422
423 return false;
424}
425
426void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700427 // If draw deferring is enabled this method will simply defer
428 // the display list of each individual layer. The layers remain
429 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700430 int count = mLayerUpdates.size();
431 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700432 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
433 startMark("Layer Updates");
434 } else {
435 startMark("Defer Layer Updates");
436 }
Romain Guy11cb6422012-09-21 00:39:43 -0700437
Chris Craik1206b9b2013-04-04 14:46:24 -0700438 // Note: it is very important to update the layers in order
439 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700440 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700441 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700442 }
Romain Guy11cb6422012-09-21 00:39:43 -0700443
Romain Guy96885eb2013-03-26 15:05:58 -0700444 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
445 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400446 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700447 }
448 endMark();
449 }
450}
451
452void OpenGLRenderer::flushLayers() {
453 int count = mLayerUpdates.size();
454 if (count > 0) {
455 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700456
Chris Craik1206b9b2013-04-04 14:46:24 -0700457 // Note: it is very important to update the layers in order
458 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800459 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700460 }
461
462 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400463 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700464
Romain Guy11cb6422012-09-21 00:39:43 -0700465 endMark();
466 }
467}
468
469void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
470 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700471 // Make sure we don't introduce duplicates.
472 // SortedVector would do this automatically but we need to respect
473 // the insertion order. The linear search is not an issue since
474 // this list is usually very short (typically one item, at most a few)
475 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
476 if (mLayerUpdates.itemAt(i) == layer) {
477 return;
478 }
479 }
Romain Guy11cb6422012-09-21 00:39:43 -0700480 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700481 }
482}
483
Romain Guye93482f2013-06-17 13:14:51 -0700484void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
485 if (layer) {
486 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
487 if (mLayerUpdates.itemAt(i) == layer) {
488 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700489 break;
490 }
491 }
492 }
493}
494
Romain Guy40543602013-06-12 15:31:28 -0700495void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800496 ATRACE_NAME("Update HW Layers");
Chris Craik44eb2c02015-01-29 09:45:09 -0800497 mRenderState.blend().syncEnabled();
Romain Guy40543602013-06-12 15:31:28 -0700498 updateLayers();
499 flushLayers();
500 // Wait for all the layer updates to be executed
John Reck55156372015-01-21 07:46:37 -0800501 glFinish();
Romain Guy40543602013-06-12 15:31:28 -0700502}
503
John Reck443a7142014-09-04 17:40:05 -0700504void OpenGLRenderer::markLayersAsBuildLayers() {
505 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
506 mLayerUpdates[i]->wasBuildLayered = true;
507 }
508}
509
Romain Guy11cb6422012-09-21 00:39:43 -0700510///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700511// State management
512///////////////////////////////////////////////////////////////////////////////
513
Chris Craik14e51302013-12-30 15:32:54 -0800514void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700515 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800516 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
517 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700518
Chris Craika64a2be2014-05-14 14:17:01 -0700519 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700520 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700521 }
522
Romain Guy2542d192010-08-18 11:47:12 -0700523 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700524 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700525 }
Romain Guy2542d192010-08-18 11:47:12 -0700526
Romain Guy5ec99242010-11-03 16:19:08 -0700527 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700528 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700529 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700530 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800531 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700532 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700533 }
Romain Guybb9524b2010-06-22 18:56:38 -0700534}
535
Romain Guyf6a11b82010-06-23 17:47:49 -0700536///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700537// Layers
538///////////////////////////////////////////////////////////////////////////////
539
540int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700541 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700542 // force matrix/clip isolation for layer
543 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
544
Tom Hudson984162f2014-10-10 13:38:16 -0400545 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700546
Tom Hudson984162f2014-10-10 13:38:16 -0400547 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700548 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700549 }
Romain Guyd55a8612010-06-28 17:42:46 -0700550
551 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700552}
553
Chris Craikd90144d2013-03-19 15:03:48 -0700554void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
555 const Rect untransformedBounds(bounds);
556
Chris Craikd6b65f62014-01-01 14:45:21 -0800557 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700558
559 // Layers only make sense if they are in the framebuffer's bounds
Rob Tsuk487a92c2015-01-06 13:22:54 -0800560 if (bounds.intersect(mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700561 // We cannot work with sub-pixels in this case
562 bounds.snapToPixelBoundaries();
563
564 // When the layer is not an FBO, we may use glCopyTexImage so we
565 // need to make sure the layer does not extend outside the bounds
566 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700567 const Snapshot& previous = *(currentSnapshot()->previous);
568 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
569 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700570 bounds.setEmpty();
571 } else if (fboLayer) {
572 clip.set(bounds);
573 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800574 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700575 inverse.mapRect(clip);
576 clip.snapToPixelBoundaries();
577 if (clip.intersect(untransformedBounds)) {
578 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
579 bounds.set(untransformedBounds);
580 } else {
581 clip.setEmpty();
582 }
583 }
584 } else {
585 bounds.setEmpty();
586 }
587}
588
Chris Craik408eb122013-03-26 18:55:15 -0700589void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
590 bool fboLayer, int alpha) {
591 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
592 bounds.getHeight() > mCaches.maxTextureSize ||
593 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400594 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700595 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400596 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700597 }
598}
599
Chris Craikd90144d2013-03-19 15:03:48 -0700600int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500601 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400602 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700603
Tom Hudson984162f2014-10-10 13:38:16 -0400604 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700605 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
606 // operations will be able to store and restore the current clip and transform info, and
607 // quick rejection will be correct (for display lists)
608
609 Rect bounds(left, top, right, bottom);
610 Rect clip;
611 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500612 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700613
Tom Hudson984162f2014-10-10 13:38:16 -0400614 if (!mState.currentlyIgnored()) {
615 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
616 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
617 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800618 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700619 }
620 }
621
622 return count;
623}
624
Romain Guy1c740bc2010-09-13 18:00:09 -0700625/**
626 * Layers are viewed by Skia are slightly different than layers in image editing
627 * programs (for instance.) When a layer is created, previously created layers
628 * and the frame buffer still receive every drawing command. For instance, if a
629 * layer is created and a shape intersecting the bounds of the layers and the
630 * framebuffer is draw, the shape will be drawn on both (unless the layer was
631 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
632 *
633 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
634 * texture. Unfortunately, this is inefficient as it requires every primitive to
635 * be drawn n + 1 times, where n is the number of active layers. In practice this
636 * means, for every primitive:
637 * - Switch active frame buffer
638 * - Change viewport, clip and projection matrix
639 * - Issue the drawing
640 *
641 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700642 * To avoid this, layers are implemented in a different way here, at least in the
643 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
644 * is set. When this flag is set we can redirect all drawing operations into a
645 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700646 *
647 * This implementation relies on the frame buffer being at least RGBA 8888. When
648 * a layer is created, only a texture is created, not an FBO. The content of the
649 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700650 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700651 * buffer and drawing continues as normal. This technique therefore treats the
652 * frame buffer as a scratch buffer for the layers.
653 *
654 * To compose the layers back onto the frame buffer, each layer texture
655 * (containing the original frame buffer data) is drawn as a simple quad over
656 * the frame buffer. The trick is that the quad is set as the composition
657 * destination in the blending equation, and the frame buffer becomes the source
658 * of the composition.
659 *
660 * Drawing layers with an alpha value requires an extra step before composition.
661 * An empty quad is drawn over the layer's region in the frame buffer. This quad
662 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
663 * quad is used to multiply the colors in the frame buffer. This is achieved by
664 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
665 * GL_ZERO, GL_SRC_ALPHA.
666 *
667 * Because glCopyTexImage2D() can be slow, an alternative implementation might
668 * be use to draw a single clipped layer. The implementation described above
669 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700670 *
671 * (1) The frame buffer is actually not cleared right away. To allow the GPU
672 * to potentially optimize series of calls to glCopyTexImage2D, the frame
673 * buffer is left untouched until the first drawing operation. Only when
674 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700675 */
Chet Haased48885a2012-08-28 17:43:28 -0700676bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700677 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800678 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
679 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700680
Romain Guyeb993562010-10-05 18:14:38 -0700681 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
682
Romain Guyf607bdc2010-09-10 19:20:06 -0700683 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700684 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700685 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700686 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000687 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700688
689 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400690 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700691 return false;
692 }
Romain Guyf18fd992010-07-08 11:45:51 -0700693
Chris Craik44eb2c02015-01-29 09:45:09 -0800694 mCaches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700695 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700696 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700697 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700698 }
699
Derek Sollenberger674554f2014-02-19 16:47:32 +0000700 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700701 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700702 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
703 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500704
Chet Haasea23eed82012-04-12 15:19:04 -0700705 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700706 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700707 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda57022010-07-06 11:39:32 -0700708
Romain Guy8fb95422010-08-17 18:38:51 -0700709 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400710 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
711 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700712
Chris Craika8bea8e2014-09-24 11:29:43 -0700713 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
714 fboLayer ? "" : "unclipped ",
715 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700716 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700717 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700718 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700719 } else {
720 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700721 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800722 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700723 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700724 // Workaround for some GL drivers. When reading pixels lying outside
725 // of the window we should get undefined values for those pixels.
726 // Unfortunately some drivers will turn the entire target texture black
727 // when reading outside of the window.
728 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800729 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700730 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800731 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700732
Chris Craika64a2be2014-05-14 14:17:01 -0700733 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
734 bounds.left, getViewportHeight() - bounds.bottom,
735 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700736
Romain Guy54be1cd2011-06-13 19:04:27 -0700737 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800738 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700739 }
Romain Guyeb993562010-10-05 18:14:38 -0700740 }
Romain Guyf86ef572010-07-01 11:05:42 -0700741
Romain Guyd55a8612010-06-28 17:42:46 -0700742 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700743}
744
Chris Craike63f7c622013-10-17 10:30:55 -0700745bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800746 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700747 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700748
Tom Hudson984162f2014-10-10 13:38:16 -0400749 writableSnapshot()->region = &writableSnapshot()->layer->region;
750 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
751 writableSnapshot()->fbo = layer->getFbo();
752 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
753 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
754 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800755 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700756
Romain Guy2b7028e2012-09-19 17:25:38 -0700757 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700758 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700759 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700760 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700761 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700762
763 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700764 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700765 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700766 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700767 }
768
769 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700770 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700771
henry.uh_chen33f5a592014-07-02 19:36:56 +0800772 // Expand the startTiling region by 1
773 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700774
775 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800776 mRenderState.scissor().setEnabled(true);
777 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700778 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700779 glClear(GL_COLOR_BUFFER_BIT);
780
781 dirtyClip();
782
783 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700784 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700785 return true;
786}
787
Romain Guy1c740bc2010-09-13 18:00:09 -0700788/**
789 * Read the documentation of createLayer() before doing anything in this method.
790 */
Chris Craik14e51302013-12-30 15:32:54 -0800791void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
792 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000793 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700794 return;
795 }
796
Chris Craik14e51302013-12-30 15:32:54 -0800797 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800798 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800799 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700800
Chris Craik39a908c2013-06-13 14:39:01 -0700801 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400802 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800803 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800804 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700805
Romain Guyeb993562010-10-05 18:14:38 -0700806 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700807 endTiling();
808
Romain Guye0aa84b2012-04-03 19:30:26 -0700809 // Detach the texture from the FBO
810 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800811
812 layer->removeFbo(false);
813
Romain Guyeb993562010-10-05 18:14:38 -0700814 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700815 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700816 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700817
Chris Craikd6b65f62014-01-01 14:45:21 -0800818 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700819 }
820
Romain Guy9ace8f52011-07-07 20:50:11 -0700821 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500822 SkPaint layerPaint;
823 layerPaint.setAlpha(layer->getAlpha());
824 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
825 layerPaint.setColorFilter(layer->getColorFilter());
826
827 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700828 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700829 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700830 }
831
Chris Craik96a5c4c2015-01-27 15:46:35 -0800832 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700833
Chris Craik44eb2c02015-01-29 09:45:09 -0800834 mCaches.textureState().activateTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700835
Romain Guy5b3b3522010-10-27 18:57:51 -0700836 // When the layer is stored in an FBO, we can save a bit of fillrate by
837 // drawing only the dirty region
838 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800839 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700840 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700841 } else if (!rect.isEmpty()) {
842 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530843
844 save(0);
845 // the layer contains screen buffer content that shouldn't be alpha modulated
846 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400847 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700848 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530849 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700850 }
Romain Guy8b55f372010-08-18 17:10:07 -0700851
Romain Guy746b7402010-10-26 16:27:31 -0700852 dirtyClip();
853
Romain Guyeb993562010-10-05 18:14:38 -0700854 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800855 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700856 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800857 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800858 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700859 }
860}
861
Romain Guyaa6c24c2011-04-28 18:40:04 -0700862void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700863 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700864
865 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700866 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700867 setupDrawWithTexture();
868 } else {
869 setupDrawWithExternalTexture();
870 }
871 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700872 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500873 setupDrawColorFilter(layer->getColorFilter());
874 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700875 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700876 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500877 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700878 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
879 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700880 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700881 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700882 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800883 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800884 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700885 layer->getWidth() == (uint32_t) rect.getWidth() &&
886 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800887 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
888 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700889
Romain Guyd21b6e12011-11-30 20:21:23 -0800890 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800891 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
892 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700893 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800894 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800895 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
896 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700897 }
898 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700899 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700900
Chris Craik117bdbc2015-02-05 10:12:38 -0800901 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700902}
903
Romain Guy5b3b3522010-10-27 18:57:51 -0700904void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700905 if (layer->isTextureLayer()) {
906 EVENT_LOGD("composeTextureLayerRect");
907 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
908 drawTextureLayer(layer, rect);
909 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
910 } else {
911 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700912 const Rect& texCoords = layer->texCoords;
913 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
914 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700915
Romain Guy9ace8f52011-07-07 20:50:11 -0700916 float x = rect.left;
917 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -0800918 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700919 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700920 layer->getHeight() == (uint32_t) rect.getHeight();
921
922 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700923 // When we're swapping, the layer is already in screen coordinates
924 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800925 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
926 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700927 }
928
Romain Guyd21b6e12011-11-30 20:21:23 -0800929 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700930 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800931 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700932 }
933
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500934 SkPaint layerPaint;
935 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
936 layerPaint.setXfermodeMode(layer->getMode());
937 layerPaint.setColorFilter(layer->getColorFilter());
938
939 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700940 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500941 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -0700942 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik117bdbc2015-02-05 10:12:38 -0800943 GL_TRIANGLE_STRIP, kUnitQuadCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700944
Romain Guyaa6c24c2011-04-28 18:40:04 -0700945 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700946 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700947}
948
Chris Craik34416ea2013-04-15 16:08:28 -0700949/**
950 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
951 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
952 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
953 * by saveLayer's restore
954 */
Tom Hudson984162f2014-10-10 13:38:16 -0400955#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
956 DRAW_COMMAND; \
957 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
958 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
959 DRAW_COMMAND; \
960 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
961 } \
Chris Craik34416ea2013-04-15 16:08:28 -0700962 }
963
964#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
965
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400966// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
967// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
968class LayerShader : public SkShader {
969public:
970 LayerShader(Layer* layer, const SkMatrix* localMatrix)
971 : INHERITED(localMatrix)
972 , mLayer(layer) {
973 }
974
Chris Craike84a2082014-12-22 14:28:49 -0800975 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400976 if (data) {
977 *data = static_cast<void*>(mLayer);
978 }
979 return true;
980 }
981
Chris Craike84a2082014-12-22 14:28:49 -0800982 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400983 return !mLayer->isBlend();
984 }
985
986protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000987 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400988 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
989 }
990
Chris Craike84a2082014-12-22 14:28:49 -0800991 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400992 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
993 }
994
Chris Craike84a2082014-12-22 14:28:49 -0800995 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400996 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -0800997 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400998 }
999private:
1000 // Unowned.
1001 Layer* mLayer;
1002 typedef SkShader INHERITED;
1003};
1004
Romain Guy5b3b3522010-10-27 18:57:51 -07001005void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001006 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1007
1008 if (layer->getConvexMask()) {
1009 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1010
1011 // clip to the area of the layer the mask can be larger
1012 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1013
1014 SkPaint paint;
1015 paint.setAntiAlias(true);
1016 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1017
Chris Craik3f0854292014-04-15 16:18:08 -07001018 // create LayerShader to map SaveLayer content into subsequent draw
1019 SkMatrix shaderMatrix;
1020 shaderMatrix.setTranslate(rect.left, rect.bottom);
1021 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001022 LayerShader layerShader(layer, &shaderMatrix);
1023 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001024
1025 // Since the drawing primitive is defined in local drawing space,
1026 // we don't need to modify the draw matrix
1027 const SkPath* maskPath = layer->getConvexMask();
1028 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1029
Chris Craike84a2082014-12-22 14:28:49 -08001030 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -07001031 restore();
1032
1033 return;
1034 }
1035
Romain Guy5b3b3522010-10-27 18:57:51 -07001036 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001037 layer->setRegionAsRect();
1038
Chris Craik34416ea2013-04-15 16:08:28 -07001039 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001040
Romain Guy5b3b3522010-10-27 18:57:51 -07001041 layer->region.clear();
1042 return;
1043 }
1044
Chris Craik62d307c2014-07-29 10:35:13 -07001045 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001046 // standard Region based draw
1047 size_t count;
1048 const android::Rect* rects;
1049 Region safeRegion;
1050 if (CC_LIKELY(hasRectToRectTransform())) {
1051 rects = layer->region.getArray(&count);
1052 } else {
1053 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1054 rects = safeRegion.getArray(&count);
1055 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001056
Chris Craik3f0854292014-04-15 16:18:08 -07001057 const float alpha = getLayerAlpha(layer);
1058 const float texX = 1.0f / float(layer->getWidth());
1059 const float texY = 1.0f / float(layer->getHeight());
1060 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001061
Chris Craik3f0854292014-04-15 16:18:08 -07001062 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001063
Chris Craik3f0854292014-04-15 16:18:08 -07001064 // We must get (and therefore bind) the region mesh buffer
1065 // after we setup drawing in case we need to mess with the
1066 // stencil buffer in setupDraw()
1067 TextureVertex* mesh = mCaches.getRegionMesh();
1068 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001069
Chris Craik3f0854292014-04-15 16:18:08 -07001070 setupDrawWithTexture();
1071 setupDrawColor(alpha, alpha, alpha, alpha);
1072 setupDrawColorFilter(layer->getColorFilter());
1073 setupDrawBlending(layer);
1074 setupDrawProgram();
1075 setupDrawDirtyRegionsDisabled();
1076 setupDrawPureColorUniforms();
1077 setupDrawColorFilterUniforms(layer->getColorFilter());
1078 setupDrawTexture(layer->getTexture());
1079 if (currentTransform()->isPureTranslate()) {
1080 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1081 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001082
Chris Craik3f0854292014-04-15 16:18:08 -07001083 layer->setFilter(GL_NEAREST);
1084 setupDrawModelView(kModelViewMode_Translate, false,
1085 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1086 } else {
1087 layer->setFilter(GL_LINEAR);
1088 setupDrawModelView(kModelViewMode_Translate, false,
1089 rect.left, rect.top, rect.right, rect.bottom);
1090 }
1091 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001092
Chris Craik3f0854292014-04-15 16:18:08 -07001093 for (size_t i = 0; i < count; i++) {
1094 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001095
Chris Craik3f0854292014-04-15 16:18:08 -07001096 const float u1 = r->left * texX;
1097 const float v1 = (height - r->top) * texY;
1098 const float u2 = r->right * texX;
1099 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001100
Chris Craik3f0854292014-04-15 16:18:08 -07001101 // TODO: Reject quads outside of the clip
1102 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1103 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1104 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1105 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001106
Chris Craik3f0854292014-04-15 16:18:08 -07001107 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001108
Chris Craik96a5c4c2015-01-27 15:46:35 -08001109 if (numQuads >= kMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001110 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001111 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001112 numQuads = 0;
1113 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001114 }
Chris Craik3f0854292014-04-15 16:18:08 -07001115 }
1116
1117 if (numQuads > 0) {
1118 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001119 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001120 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001121
Romain Guy5b3b3522010-10-27 18:57:51 -07001122#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001123 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001124#endif
1125
Chris Craik3f0854292014-04-15 16:18:08 -07001126 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001127}
1128
Romain Guy3a3133d2011-02-01 22:59:58 -08001129#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001130void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001131 size_t count;
1132 const android::Rect* rects = region.getArray(&count);
1133
1134 uint32_t colors[] = {
1135 0x7fff0000, 0x7f00ff00,
1136 0x7f0000ff, 0x7fff00ff,
1137 };
1138
1139 int offset = 0;
1140 int32_t top = rects[0].top;
1141
1142 for (size_t i = 0; i < count; i++) {
1143 if (top != rects[i].top) {
1144 offset ^= 0x2;
1145 top = rects[i].top;
1146 }
1147
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001148 SkPaint paint;
1149 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001150 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001151 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001152 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001153}
Chris Craike63f7c622013-10-17 10:30:55 -07001154#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001155
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001156void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001157 Vector<float> rects;
1158
1159 SkRegion::Iterator it(region);
1160 while (!it.done()) {
1161 const SkIRect& r = it.rect();
1162 rects.push(r.fLeft);
1163 rects.push(r.fTop);
1164 rects.push(r.fRight);
1165 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001166 it.next();
1167 }
1168
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001169 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001170}
1171
Romain Guy5b3b3522010-10-27 18:57:51 -07001172void OpenGLRenderer::dirtyLayer(const float left, const float top,
1173 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001174 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001175 Rect bounds(left, top, right, bottom);
1176 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001177 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001178 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001179}
1180
1181void OpenGLRenderer::dirtyLayer(const float left, const float top,
1182 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001183 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001184 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001185 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001186 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001187}
1188
1189void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001190 if (bounds.intersect(mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001191 bounds.snapToPixelBoundaries();
1192 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1193 if (!dirty.isEmpty()) {
1194 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001195 }
1196 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001197}
1198
Chris Craik4063a0e2013-11-15 16:06:56 -08001199void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001200 GLsizei elementsCount = quadsCount * 6;
1201 while (elementsCount > 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001202 GLsizei drawCount = min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Romain Guy448455f2013-07-22 13:57:50 -07001203
Romain Guy3380cfd2013-08-15 16:57:57 -07001204 setupDrawIndexedVertices(&mesh[0].x);
Chris Craike84a2082014-12-22 14:28:49 -08001205 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy448455f2013-07-22 13:57:50 -07001206
1207 elementsCount -= drawCount;
1208 // Though there are 4 vertices in a quad, we use 6 indices per
1209 // quad to draw with GL_TRIANGLES
1210 mesh += (drawCount / 6) * 4;
1211 }
1212}
1213
Romain Guy54be1cd2011-06-13 19:04:27 -07001214void OpenGLRenderer::clearLayerRegions() {
1215 const size_t count = mLayers.size();
1216 if (count == 0) return;
1217
Tom Hudson984162f2014-10-10 13:38:16 -04001218 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001219 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001220 // Doing several glScissor/glClear here can negatively impact
1221 // GPUs with a tiler architecture, instead we draw quads with
1222 // the Clear blending mode
1223
1224 // The list contains bounds that have already been clipped
1225 // against their initial clip rect, and the current clip
1226 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001227 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001228
Romain Guy448455f2013-07-22 13:57:50 -07001229 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001230 Vertex* vertex = mesh;
1231
1232 for (uint32_t i = 0; i < count; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001233 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001234
Chris Craik51d6a3d2014-12-22 17:16:56 -08001235 Vertex::set(vertex++, bounds.left, bounds.top);
1236 Vertex::set(vertex++, bounds.right, bounds.top);
1237 Vertex::set(vertex++, bounds.left, bounds.bottom);
1238 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001239 }
Romain Guye67307c2013-02-11 18:01:20 -08001240 // We must clear the list of dirty rects before we
1241 // call setupDraw() to prevent stencil setup to do
1242 // the same thing again
1243 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001244
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001245 SkPaint clearPaint;
1246 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1247
Romain Guy54be1cd2011-06-13 19:04:27 -07001248 setupDraw(false);
1249 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001250 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001251 setupDrawProgram();
1252 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001253 setupDrawModelView(kModelViewMode_Translate, false,
1254 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001255
Chris Craik4063a0e2013-11-15 16:06:56 -08001256 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001257
Chris Craik65fe5ee2015-01-26 18:06:29 -08001258 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001259 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001260 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001261 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001262}
1263
Romain Guybd6b79b2010-06-26 00:13:53 -07001264///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001265// State Deferral
1266///////////////////////////////////////////////////////////////////////////////
1267
Chris Craikff785832013-03-08 13:12:16 -08001268bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001269 const Rect& currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001270 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001271
Chris Craikff785832013-03-08 13:12:16 -08001272 if (stateDeferFlags & kStateDeferFlag_Draw) {
1273 // state has bounds initialized in local coordinates
1274 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001275 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001276 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001277 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1278 // is used, it should more closely duplicate the quickReject logic (in how it uses
1279 // snapToPixelBoundaries)
1280
Rob Tsuk487a92c2015-01-06 13:22:54 -08001281 if (!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001282 // quick rejected
1283 return true;
1284 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001285
Chris Craika02c4ed2013-06-14 13:43:58 -07001286 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001287 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001288 int& flags = state.mClipSideFlags;
1289 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001290 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1291 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1292 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1293 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001294 }
1295 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001296 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001297 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1298 // overdraw avoidance (since we don't know what it overlaps)
1299 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001300 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001301 }
1302 }
1303
Chris Craik527a3aa2013-03-04 10:19:31 -08001304 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1305 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001306 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001307 }
1308
Chris Craik7273daa2013-03-28 11:25:24 -07001309 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1310 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001311 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001312 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001313 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001314
1315 // always store/restore, since it's just a pointer
1316 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001317 return false;
1318}
1319
Chris Craik527a3aa2013-03-04 10:19:31 -08001320void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001321 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001322 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001323 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001324 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001325
Chris Craik527a3aa2013-03-04 10:19:31 -08001326 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001327 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001328 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001329 dirtyClip();
1330 }
Chris Craikc3566d02013-02-04 16:16:33 -08001331}
1332
Chris Craik28ce94a2013-05-31 11:38:03 -07001333/**
1334 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1335 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1336 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1337 *
1338 * This method should be called when restoreDisplayState() won't be restoring the clip
1339 */
1340void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001341 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001342 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001343 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001344 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001345 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001346 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001347 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1348 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001349}
1350
Chris Craikc3566d02013-02-04 16:16:33 -08001351///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001352// Clipping
1353///////////////////////////////////////////////////////////////////////////////
1354
Romain Guybb9524b2010-06-22 18:56:38 -07001355void OpenGLRenderer::setScissorFromClip() {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001356 Rect clip(mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001357 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001358
Chris Craik65fe5ee2015-01-26 18:06:29 -08001359 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001360 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001361 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001362 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001363}
1364
Romain Guy8ce00302013-01-15 18:51:42 -08001365void OpenGLRenderer::ensureStencilBuffer() {
1366 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1367 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1368 // just hope we have one when hasLayer() returns false.
1369 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001370 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001371 }
1372}
1373
1374void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1375 // The layer's FBO is already bound when we reach this stage
1376 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001377 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1378 // is attached after we initiated tiling. We must turn it off,
1379 // attach the new render buffer then turn tiling back on
1380 endTiling();
1381
Romain Guy8d4aeb72013-02-12 16:08:55 -08001382 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Chris Craik117bdbc2015-02-05 10:12:38 -08001383 Stencil::getSmallestStencilFormat(),
1384 layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001385 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001386
Romain Guyf735c8e2013-01-31 17:45:55 -08001387 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001388 }
1389}
1390
Rob Tsuk487a92c2015-01-06 13:22:54 -08001391static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1392 float x, float y) {
1393 Vertex v;
1394 v.x = x;
1395 v.y = y;
1396 transform.mapPoint(v.x, v.y);
1397 rectangleVertices.push_back(v);
1398}
1399
1400static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1401 Vertex v;
1402 v.x = x;
1403 v.y = y;
1404 rectangleVertices.push_back(v);
1405}
1406
1407void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
1408 int count = rectangleList.getTransformedRectanglesCount();
1409 std::vector<Vertex> rectangleVertices(count * 4);
1410 Rect scissorBox = rectangleList.calculateBounds();
1411 scissorBox.snapToPixelBoundaries();
1412 for (int i = 0; i < count; ++i) {
1413 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1414 const Matrix4& transform = tr.getTransform();
1415 Rect bounds = tr.getBounds();
1416 if (transform.rectToRect()) {
1417 transform.mapRect(bounds);
1418 if (!bounds.intersect(scissorBox)) {
1419 bounds.setEmpty();
1420 } else {
1421 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1422 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1423 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1424 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1425 }
1426 } else {
1427 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1428 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1429 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1430 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1431 }
1432 }
1433
Chris Craik65fe5ee2015-01-26 18:06:29 -08001434 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001435 scissorBox.getWidth(), scissorBox.getHeight());
1436
1437 const SkPaint* paint = nullptr;
1438 setupDraw();
1439 setupDrawNoTexture();
1440 setupDrawColor(0, 0xff * currentSnapshot()->alpha);
1441 setupDrawShader(getShader(paint));
1442 setupDrawColorFilter(getColorFilter(paint));
1443 setupDrawBlending(paint);
1444 setupDrawProgram();
1445 setupDrawDirtyRegionsDisabled();
1446 setupDrawModelView(kModelViewMode_Translate, false,
1447 0.0f, 0.0f, 0.0f, 0.0f, true);
1448 setupDrawColorUniforms(getShader(paint));
1449 setupDrawShaderUniforms(getShader(paint));
1450 setupDrawColorFilterUniforms(getColorFilter(paint));
1451
1452 issueIndexedQuadDraw(&rectangleVertices[0], rectangleVertices.size() / 4);
1453}
1454
Romain Guy8ce00302013-01-15 18:51:42 -08001455void OpenGLRenderer::setStencilFromClip() {
1456 if (!mCaches.debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001457 if (!currentSnapshot()->clipIsSimple()) {
1458 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001459 EVENT_LOGD("setStencilFromClip - enabling");
1460
Romain Guy8ce00302013-01-15 18:51:42 -08001461 // NOTE: The order here is important, we must set dirtyClip to false
1462 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001463 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001464
1465 ensureStencilBuffer();
1466
Rob Tsuk487a92c2015-01-06 13:22:54 -08001467 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001468
Rob Tsuk487a92c2015-01-06 13:22:54 -08001469 bool isRectangleList = clipArea.isRectangleList();
1470 if (isRectangleList) {
1471 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1472 } else {
1473 incrementThreshold = 0;
1474 }
1475
Chris Craik96a5c4c2015-01-27 15:46:35 -08001476 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001477
1478 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001479 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001480 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001481 if (resetScissor) {
1482 // The scissor was not set so we now need to update it
1483 setScissorFromClip();
1484 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001485
Chris Craik96a5c4c2015-01-27 15:46:35 -08001486 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001487
1488 // stash and disable the outline clip state, since stencil doesn't account for outline
1489 bool storedSkipOutlineClip = mSkipOutlineClip;
1490 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001491
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001492 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001493 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001494 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1495
Rob Tsuk487a92c2015-01-06 13:22:54 -08001496 if (isRectangleList) {
1497 drawRectangleList(clipArea.getRectangleList());
1498 } else {
1499 // NOTE: We could use the region contour path to generate a smaller mesh
1500 // Since we are using the stencil we could use the red book path
1501 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001502
Rob Tsuk487a92c2015-01-06 13:22:54 -08001503 // The last parameter is important: we are not drawing in the color buffer
1504 // so we don't want to dirty the current layer, if any
1505 drawRegionRects(clipArea.getClipRegion(), paint, false);
1506 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001507 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001508 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001509
Chris Craik96a5c4c2015-01-27 15:46:35 -08001510 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001511
1512 // Draw the region used to generate the stencil if the appropriate debug
1513 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001514 // TODO: Implement for rectangle list clip areas
1515 if (mCaches.debugStencilClip == Caches::kStencilShowRegion &&
1516 !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001517 paint.setColor(0x7f0000ff);
1518 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001519 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001520 }
Romain Guy8ce00302013-01-15 18:51:42 -08001521 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001522 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001523 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001524 }
1525 }
1526}
1527
Chris Craikf0a59072013-11-19 18:00:46 -08001528/**
1529 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1530 *
1531 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1532 * style, and tessellated AA ramp
1533 */
1534bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001535 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001536 bool snapOut = paint && paint->isAntiAlias();
1537
1538 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1539 float outset = paint->getStrokeWidth() * 0.5f;
1540 left -= outset;
1541 top -= outset;
1542 right += outset;
1543 bottom += outset;
1544 }
1545
Chris Craikdeeda3d2014-05-05 19:09:33 -07001546 bool clipRequired = false;
1547 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001548 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001549 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001550 return true;
1551 }
1552
Chris Craikf23b25a2014-06-26 15:46:20 -07001553 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001554 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001555 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001556 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001557}
1558
Romain Guy8ce00302013-01-15 18:51:42 -08001559void OpenGLRenderer::debugClip() {
1560#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001561 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001562 SkPaint paint;
1563 paint.setColor(0x7f00ff00);
1564 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1565
Romain Guy8ce00302013-01-15 18:51:42 -08001566 }
1567#endif
1568}
1569
Chris Craik117bdbc2015-02-05 10:12:38 -08001570void OpenGLRenderer::renderGlop(const Glop& glop) {
1571 if (mState.getDirtyClip()) {
1572 if (mRenderState.scissor().isEnabled()) {
1573 setScissorFromClip();
1574 }
1575
1576 setStencilFromClip();
1577 }
1578 mRenderState.render(glop);
Chris Craik2ab95d72015-02-06 15:25:51 -08001579
1580 if (!mRenderState.stencil().isWriteEnabled()) {
1581 // TODO: specify more clearly when a draw should dirty the layer.
1582 // is writing to the stencil the only time we should ignore this?
1583 dirtyLayer(glop.bounds.left, glop.bounds.top, glop.bounds.right, glop.bounds.bottom);
Chris Craik0519c812015-02-11 13:17:06 -08001584 mDirty = true;
Chris Craik2ab95d72015-02-06 15:25:51 -08001585 }
Chris Craik117bdbc2015-02-05 10:12:38 -08001586}
1587
Romain Guyf6a11b82010-06-23 17:47:49 -07001588///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001589// Drawing commands
1590///////////////////////////////////////////////////////////////////////////////
1591
Chris Craik62d307c2014-07-29 10:35:13 -07001592void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001593 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001594 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001595 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001596 // Make sure setScissor & setStencil happen at the beginning of
1597 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001598 if (mState.getDirtyClip()) {
Chris Craik65fe5ee2015-01-26 18:06:29 -08001599 if (mRenderState.scissor().isEnabled()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001600 setScissorFromClip();
1601 }
Chris Craik62d307c2014-07-29 10:35:13 -07001602
Dohyun Leeadc0d9d2014-11-24 21:08:15 +09001603 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001604 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001605
Romain Guy70ca14e2010-12-13 18:24:33 -08001606 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001607
Romain Guy70ca14e2010-12-13 18:24:33 -08001608 mSetShaderColor = false;
1609 mColorSet = false;
Chris Craik0519c812015-02-11 13:17:06 -08001610 mColor.a = mColor.r = mColor.g = mColor.b = 0.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001611 mTextureUnit = 0;
1612 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001613
1614 // Enable debug highlight when what we're about to draw is tested against
1615 // the stencil buffer and if stencil highlight debugging is on
Chris Craik117bdbc2015-02-05 10:12:38 -08001616 mDescription.hasDebugHighlight = !mCaches.debugOverdraw
1617 && mCaches.debugStencilClip == Caches::kStencilShowHighlight
1618 && mRenderState.stencil().isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001619}
1620
1621void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1622 mDescription.hasTexture = true;
1623 mDescription.hasAlpha8Texture = isAlpha8;
1624}
1625
Romain Guyff316ec2013-02-13 18:39:43 -08001626void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1627 mDescription.hasTexture = true;
1628 mDescription.hasColors = true;
1629 mDescription.hasAlpha8Texture = isAlpha8;
1630}
1631
Romain Guyaa6c24c2011-04-28 18:40:04 -07001632void OpenGLRenderer::setupDrawWithExternalTexture() {
1633 mDescription.hasExternalTexture = true;
1634}
1635
Romain Guy15bc6432011-12-13 13:11:32 -08001636void OpenGLRenderer::setupDrawNoTexture() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001637 mRenderState.meshState().disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001638}
1639
Chris Craik91a8c7c2014-08-12 14:31:35 -07001640void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1641 mDescription.hasVertexAlpha = true;
1642 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001643}
1644
Romain Guy8d0d4782010-12-14 20:13:35 -08001645void OpenGLRenderer::setupDrawColor(int color, int alpha) {
Chris Craik0519c812015-02-11 13:17:06 -08001646 mColor.a = alpha / 255.0f;
1647 mColor.r = mColor.a * ((color >> 16) & 0xFF) / 255.0f;
1648 mColor.g = mColor.a * ((color >> 8) & 0xFF) / 255.0f;
1649 mColor.b = mColor.a * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001650 mColorSet = true;
Chris Craik0519c812015-02-11 13:17:06 -08001651 mSetShaderColor = mDescription.setColorModulate(mColor.a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001652}
1653
Romain Guy86568192010-12-14 15:55:39 -08001654void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
Chris Craik0519c812015-02-11 13:17:06 -08001655 mColor.a = alpha / 255.0f;
1656 mColor.r = mColor.a * ((color >> 16) & 0xFF) / 255.0f;
1657 mColor.g = mColor.a * ((color >> 8) & 0xFF) / 255.0f;
1658 mColor.b = mColor.a * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001659 mColorSet = true;
Chris Craik0519c812015-02-11 13:17:06 -08001660 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColor.r, mColor.g, mColor.b, mColor.a);
Romain Guy86568192010-12-14 15:55:39 -08001661}
1662
Romain Guy41210632012-07-16 17:04:24 -07001663void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1664 mCaches.fontRenderer->describe(mDescription, paint);
1665}
1666
Romain Guy70ca14e2010-12-13 18:24:33 -08001667void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
Chris Craik0519c812015-02-11 13:17:06 -08001668 mColor.a = a;
1669 mColor.r = r;
1670 mColor.g = g;
1671 mColor.b = b;
Romain Guy70ca14e2010-12-13 18:24:33 -08001672 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001673 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001674}
1675
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001676void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
Chris Craike84a2082014-12-22 14:28:49 -08001677 if (shader != nullptr) {
Chris Craik117bdbc2015-02-05 10:12:38 -08001678 SkiaShader::describe(&mCaches, mDescription, mCaches.extensions(), *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001679 }
1680}
1681
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001682void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001683 if (filter == nullptr) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001684 return;
1685 }
1686
1687 SkXfermode::Mode mode;
Chris Craike84a2082014-12-22 14:28:49 -08001688 if (filter->asColorMode(nullptr, &mode)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001689 mDescription.colorOp = ProgramDescription::kColorBlend;
1690 mDescription.colorMode = mode;
Chris Craike84a2082014-12-22 14:28:49 -08001691 } else if (filter->asColorMatrix(nullptr)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001692 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001693 }
1694}
1695
Romain Guyf09ef512011-05-27 11:43:46 -07001696void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1697 if (mColorSet && mode == SkXfermode::kClear_Mode) {
Chris Craik0519c812015-02-11 13:17:06 -08001698 mColor.a = 1.0f;
1699 mColor.r = mColor.g = mColor.b = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001700 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001701 }
1702}
1703
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001704void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1705 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001706 // When the blending mode is kClear_Mode, we need to use a modulate color
1707 // argb=1,0,0,0
1708 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001709 // TODO: check shader blending, once we have shader drawing support for layers.
Tom Hudson8dfaa492014-12-09 15:03:44 -05001710 bool blend = layer->isBlend()
1711 || getLayerAlpha(layer) < 1.0f
Chris Craik0519c812015-02-11 13:17:06 -08001712 || (mColorSet && mColor.a < 1.0f)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001713 || PaintUtils::isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001714 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001715}
1716
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001717void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1718 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001719 // When the blending mode is kClear_Mode, we need to use a modulate color
1720 // argb=1,0,0,0
1721 accountForClear(mode);
Chris Craik0519c812015-02-11 13:17:06 -08001722 blend |= (mColorSet && mColor.a < 1.0f)
Chris Craik03188872015-02-02 18:39:33 -08001723 || (getShader(paint) && !getShader(paint)->isOpaque())
1724 || PaintUtils::isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001725 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001726}
1727
1728void OpenGLRenderer::setupDrawProgram() {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001729 mCaches.setProgram(mDescription);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001730 if (mDescription.hasRoundRectClip) {
1731 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001732 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001733 const Rect& innerRect = state->innerRect;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001734 glUniform4f(mCaches.program().getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001735 innerRect.left, innerRect.top,
1736 innerRect.right, innerRect.bottom);
Chris Craik6c15ffa2015-02-02 13:50:55 -08001737 glUniformMatrix4fv(mCaches.program().getUniform("roundRectInvTransform"),
Chris Craikdeeda3d2014-05-05 19:09:33 -07001738 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001739
1740 // add half pixel to round out integer rect space to cover pixel centers
1741 float roundedOutRadius = state->radius + 0.5f;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001742 glUniform1f(mCaches.program().getUniform("roundRectRadius"),
Chris Craik4340c262014-09-11 18:58:45 -07001743 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001744 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001745}
1746
1747void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1748 mTrackDirtyRegions = false;
1749}
1750
Chris Craik4063a0e2013-11-15 16:06:56 -08001751void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1752 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001753 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001754 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001755 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001756 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001757
Romain Guy86568192010-12-14 15:55:39 -08001758 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001759 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001760
1761 mCaches.program().set(currentSnapshot()->getOrthoMatrix(),
Chris Craike84a2082014-12-22 14:28:49 -08001762 mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001763 if (dirty && mTrackDirtyRegions) {
1764 if (!ignoreTransform) {
1765 dirtyLayer(left, top, right, bottom, *currentTransform());
1766 } else {
1767 dirtyLayer(left, top, right, bottom);
1768 }
Romain Guy86568192010-12-14 15:55:39 -08001769 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001770}
1771
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001772void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1773 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Chris Craik0519c812015-02-11 13:17:06 -08001774 mCaches.program().setColor(mColor);
Romain Guy70ca14e2010-12-13 18:24:33 -08001775 }
1776}
1777
Romain Guy86568192010-12-14 15:55:39 -08001778void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001779 if (mSetShaderColor) {
Chris Craik0519c812015-02-11 13:17:06 -08001780 mCaches.program().setColor(mColor);
Romain Guy55368412010-12-14 10:59:41 -08001781 }
1782}
1783
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001784void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
Chris Craike84a2082014-12-22 14:28:49 -08001785 if (shader == nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001786 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001787 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001788
1789 if (ignoreTransform) {
1790 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1791 // because it was built into modelView / the geometry, and the description needs to
1792 // compensate.
1793 mat4 modelViewWithoutTransform;
1794 modelViewWithoutTransform.loadInverse(*currentTransform());
1795 modelViewWithoutTransform.multiply(mModelViewMatrix);
1796 mModelViewMatrix.load(modelViewWithoutTransform);
1797 }
1798
Chris Craik117bdbc2015-02-05 10:12:38 -08001799 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit,
1800 mCaches.extensions(), *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001801}
1802
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001803void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001804 if (nullptr == filter) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001805 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001806 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001807
1808 SkColor color;
1809 SkXfermode::Mode mode;
1810 if (filter->asColorMode(&color, &mode)) {
1811 const int alpha = SkColorGetA(color);
1812 const GLfloat a = alpha / 255.0f;
1813 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1814 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1815 const GLfloat b = a * SkColorGetB(color) / 255.0f;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001816 glUniform4f(mCaches.program().getUniform("colorBlend"), r, g, b, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001817 return;
1818 }
1819
1820 SkScalar srcColorMatrix[20];
1821 if (filter->asColorMatrix(srcColorMatrix)) {
1822
1823 float colorMatrix[16];
1824 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1825 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1826 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1827 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1828
1829 // Skia uses the range [0..255] for the addition vector, but we need
1830 // the [0..1] range to apply the vector in GLSL
1831 float colorVector[4];
1832 colorVector[0] = srcColorMatrix[4] / 255.0f;
1833 colorVector[1] = srcColorMatrix[9] / 255.0f;
1834 colorVector[2] = srcColorMatrix[14] / 255.0f;
1835 colorVector[3] = srcColorMatrix[19] / 255.0f;
1836
Chris Craik6c15ffa2015-02-02 13:50:55 -08001837 glUniformMatrix4fv(mCaches.program().getUniform("colorMatrix"), 1,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001838 GL_FALSE, colorMatrix);
Chris Craik6c15ffa2015-02-02 13:50:55 -08001839 glUniform4fv(mCaches.program().getUniform("colorMatrixVector"), 1, colorVector);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001840 return;
1841 }
1842
1843 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001844}
1845
Romain Guy41210632012-07-16 17:04:24 -07001846void OpenGLRenderer::setupDrawTextGammaUniforms() {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001847 mCaches.fontRenderer->setupProgram(mDescription, mCaches.program());
Romain Guy41210632012-07-16 17:04:24 -07001848}
1849
Romain Guy70ca14e2010-12-13 18:24:33 -08001850void OpenGLRenderer::setupDrawSimpleMesh() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001851 bool force = mRenderState.meshState().bindMeshBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001852 mRenderState.meshState().bindPositionVertexPointer(force, nullptr);
Chris Craik96a5c4c2015-01-27 15:46:35 -08001853 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001854}
1855
1856void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001857 if (texture) mCaches.textureState().bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001858 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001859 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001860}
1861
Romain Guyaa6c24c2011-04-28 18:40:04 -07001862void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001863 mCaches.textureState().bindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001864 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001865 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001866}
1867
Romain Guy8f0095c2011-05-02 17:24:22 -07001868void OpenGLRenderer::setupDrawTextureTransform() {
1869 mDescription.hasTextureTransform = true;
1870}
1871
1872void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001873 glUniformMatrix4fv(mCaches.program().getUniform("mainTextureTransform"), 1,
Romain Guyaa6c24c2011-04-28 18:40:04 -07001874 GL_FALSE, &transform.data[0]);
1875}
1876
Chris Craik564acf72014-01-02 16:46:18 -08001877void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1878 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001879 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001880 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001881 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001882 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001883 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001884 }
Romain Guyd71dd362011-12-12 19:03:35 -08001885
Chris Craik6c15ffa2015-02-02 13:50:55 -08001886 mRenderState.meshState().bindPositionVertexPointer(force, vertices);
1887 if (mCaches.program().texCoords >= 0) {
1888 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001889 }
1890
Chris Craik96a5c4c2015-01-27 15:46:35 -08001891 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -08001892}
1893
Chris Craik564acf72014-01-02 16:46:18 -08001894void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1895 const GLvoid* texCoords, const GLvoid* colors) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001896 bool force = mRenderState.meshState().unbindMeshBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001897 GLsizei stride = sizeof(ColorTextureVertex);
1898
Chris Craik6c15ffa2015-02-02 13:50:55 -08001899 mRenderState.meshState().bindPositionVertexPointer(force, vertices, stride);
1900 if (mCaches.program().texCoords >= 0) {
1901 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords, stride);
Romain Guyff316ec2013-02-13 18:39:43 -08001902 }
Chris Craik6c15ffa2015-02-02 13:50:55 -08001903 int slot = mCaches.program().getAttrib("colors");
Romain Guyff316ec2013-02-13 18:39:43 -08001904 if (slot >= 0) {
1905 glEnableVertexAttribArray(slot);
1906 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1907 }
1908
Chris Craik96a5c4c2015-01-27 15:46:35 -08001909 mRenderState.meshState().unbindIndicesBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001910}
1911
Chris Craik564acf72014-01-02 16:46:18 -08001912void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1913 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001914 bool force = false;
1915 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1916 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
Chris Craik96a5c4c2015-01-27 15:46:35 -08001917 // use the default VBO found in RenderState
Romain Guy3b748a42013-04-17 18:54:38 -07001918 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001919 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy3b748a42013-04-17 18:54:38 -07001920 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001921 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001922 }
Chris Craik96a5c4c2015-01-27 15:46:35 -08001923 mRenderState.meshState().bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001924
Chris Craik6c15ffa2015-02-02 13:50:55 -08001925 mRenderState.meshState().bindPositionVertexPointer(force, vertices);
1926 if (mCaches.program().texCoords >= 0) {
1927 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001928 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001929}
1930
Romain Guy448455f2013-07-22 13:57:50 -07001931void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001932 bool force = mRenderState.meshState().unbindMeshBuffer();
1933 mRenderState.meshState().bindQuadIndicesBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001934 mRenderState.meshState().bindPositionVertexPointer(force, vertices, kVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001935}
1936
Romain Guy70ca14e2010-12-13 18:24:33 -08001937///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001938// Drawing
1939///////////////////////////////////////////////////////////////////////////////
1940
Tom Hudson107843d2014-09-08 11:26:26 -04001941void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001942 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1943 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001944 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001945 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001946 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001947 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001948 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001949 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001950 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001951 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001952 }
1953
Chris Craikef8d6f22014-12-17 11:10:28 -08001954 // Don't avoid overdraw when visualizing, since that makes it harder to
1955 // debug where it's coming from, and when the problem occurs.
1956 bool avoidOverdraw = !mCaches.debugOverdraw;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001957 DeferredDisplayList deferredList(mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001958 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001959 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001960
1961 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001962 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001963
Tom Hudson107843d2014-09-08 11:26:26 -04001964 deferredList.flush(*this, dirty);
1965 } else {
1966 // Even if there is no drawing command(Ex: invisible),
1967 // it still needs startFrame to clear buffer and start tiling.
1968 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001969 }
1970}
1971
Chris Craik0519c812015-02-11 13:17:06 -08001972void OpenGLRenderer::drawAlphaBitmap(Texture* texture, const SkPaint* paint) {
1973 if (USE_GLOPS && (!paint || !paint->getShader())) {
1974 Glop glop;
1975 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
1976 aBuilder.setMeshTexturedUnitQuad(texture->uvMapper, true)
1977 .setFillTexturePaint(*texture, true, paint, currentSnapshot()->alpha)
1978 .setTransformClip(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
1979 .setModelViewMapUnitToRectSnap(Rect(0, 0, texture->width, texture->height))
1980 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1981 .build();
1982 renderGlop(glop);
1983 return;
1984 }
1985
1986 float x = 0;
1987 float y = 0;
Romain Guya168d732011-03-18 16:50:13 -07001988
Romain Guy886b2752013-01-04 12:26:18 -08001989 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1990
Romain Guya168d732011-03-18 16:50:13 -07001991 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001992 if (currentTransform()->isPureTranslate()) {
Chris Craik0519c812015-02-11 13:17:06 -08001993 x = (int) floorf(currentTransform()->getTranslateX() + 0.5f);
1994 y = (int) floorf(currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001995 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001996
1997 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001998 } else {
Chris Craik0519c812015-02-11 13:17:06 -08001999 texture->setFilter(PaintUtils::getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002000 }
2001
Romain Guy3b748a42013-04-17 18:54:38 -07002002 // No need to check for a UV mapper on the texture object, only ARGB_8888
2003 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002004 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Chris Craik96a5c4c2015-01-27 15:46:35 -08002005 paint, (GLvoid*) nullptr, (GLvoid*) kMeshTextureOffset,
Chris Craik117bdbc2015-02-05 10:12:38 -08002006 GL_TRIANGLE_STRIP, kUnitQuadCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002007}
2008
Romain Guy03c00b52013-06-20 18:30:28 -07002009/**
2010 * Important note: this method is intended to draw batches of bitmaps and
2011 * will not set the scissor enable or dirty the current layer, if any.
2012 * The caller is responsible for properly dirtying the current layer.
2013 */
Tom Hudson107843d2014-09-08 11:26:26 -04002014void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002015 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
2016 const Rect& bounds, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002017 mCaches.textureState().activateTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002018 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002019 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07002020
Chris Craik527a3aa2013-03-04 10:19:31 -08002021 const AutoTexture autoCleanup(texture);
2022
Chris Craik527a3aa2013-03-04 10:19:31 -08002023 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik0519c812015-02-11 13:17:06 -08002024 texture->setFilter(pureTranslate ? GL_NEAREST : PaintUtils::getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002025
2026 const float x = (int) floorf(bounds.left + 0.5f);
2027 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04002028 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002029 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002030 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002031 GL_TRIANGLES, bitmapCount * 6, true,
2032 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002033 } else {
2034 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002035 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002036 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2037 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002038 }
2039
Tom Hudson107843d2014-09-08 11:26:26 -04002040 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08002041}
2042
Tom Hudson107843d2014-09-08 11:26:26 -04002043void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002044 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002045 return;
Romain Guy6926c722010-07-12 20:20:03 -07002046 }
2047
Chris Craik44eb2c02015-01-29 09:45:09 -08002048 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002049 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002050 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002051 const AutoTexture autoCleanup(texture);
2052
Mike Reed1103b322014-07-08 12:36:44 -04002053 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik0519c812015-02-11 13:17:06 -08002054 drawAlphaBitmap(texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002055 } else {
Chris Craik0519c812015-02-11 13:17:06 -08002056 drawTextureRect(texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002057 }
Chet Haase48659092012-05-31 15:21:51 -07002058
Tom Hudson107843d2014-09-08 11:26:26 -04002059 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002060}
2061
Tom Hudson107843d2014-09-08 11:26:26 -04002062void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002063 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002064 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002065 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002066 }
2067
Chris Craik39a908c2013-06-13 14:39:01 -07002068 // TODO: use quickReject on bounds from vertices
Chris Craik65fe5ee2015-01-26 18:06:29 -08002069 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002070
Romain Guyb18d2d02011-02-10 15:52:54 -08002071 float left = FLT_MAX;
2072 float top = FLT_MAX;
2073 float right = FLT_MIN;
2074 float bottom = FLT_MIN;
2075
Romain Guya92bb4d2012-10-16 11:08:44 -07002076 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002077
Chris Craik51d6a3d2014-12-22 17:16:56 -08002078 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[count]);
2079 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08002080
Chris Craik51d6a3d2014-12-22 17:16:56 -08002081 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002082 if (!colors) {
2083 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08002084 tempColors.reset(new int[colorsCount]);
2085 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
2086 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08002087 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002088
Chris Craik44eb2c02015-01-29 09:45:09 -08002089 mCaches.textureState().activateTexture(0);
John Reckebd52612014-12-10 16:47:36 -08002090 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002091 const UvMapper& mapper(getMapper(texture));
2092
Romain Guy5a7b4662011-01-20 19:09:30 -08002093 for (int32_t y = 0; y < meshHeight; y++) {
2094 for (int32_t x = 0; x < meshWidth; x++) {
2095 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2096
2097 float u1 = float(x) / meshWidth;
2098 float u2 = float(x + 1) / meshWidth;
2099 float v1 = float(y) / meshHeight;
2100 float v2 = float(y + 1) / meshHeight;
2101
Romain Guy3b748a42013-04-17 18:54:38 -07002102 mapper.map(u1, v1, u2, v2);
2103
Romain Guy5a7b4662011-01-20 19:09:30 -08002104 int ax = i + (meshWidth + 1) * 2;
2105 int ay = ax + 1;
2106 int bx = i;
2107 int by = bx + 1;
2108 int cx = i + 2;
2109 int cy = cx + 1;
2110 int dx = i + (meshWidth + 1) * 2 + 2;
2111 int dy = dx + 1;
2112
Romain Guyff316ec2013-02-13 18:39:43 -08002113 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2114 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2115 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002116
Romain Guyff316ec2013-02-13 18:39:43 -08002117 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2118 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2119 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002120
Romain Guya92bb4d2012-10-16 11:08:44 -07002121 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2122 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2123 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2124 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002125 }
2126 }
2127
Chris Craikf0a59072013-11-19 18:00:46 -08002128 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002129 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002130 }
2131
Romain Guyff316ec2013-02-13 18:39:43 -08002132 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002133 texture = mCaches.textureCache.get(bitmap);
2134 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04002135 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002136 }
Romain Guyff316ec2013-02-13 18:39:43 -08002137 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002138 const AutoTexture autoCleanup(texture);
2139
2140 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik0519c812015-02-11 13:17:06 -08002141 texture->setFilter(PaintUtils::getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002142
2143 int alpha;
2144 SkXfermode::Mode mode;
2145 getAlphaAndMode(paint, &alpha, &mode);
2146
Romain Guyff316ec2013-02-13 18:39:43 -08002147 float a = alpha / 255.0f;
2148
Romain Guya92bb4d2012-10-16 11:08:44 -07002149 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002150 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002151 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002152
Romain Guyff316ec2013-02-13 18:39:43 -08002153 setupDraw();
2154 setupDrawWithTextureAndColor();
2155 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002156 setupDrawColorFilter(getColorFilter(paint));
2157 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002158 setupDrawProgram();
2159 setupDrawDirtyRegionsDisabled();
Chris Craik117bdbc2015-02-05 10:12:38 -08002160 setupDrawModelView(kModelViewMode_Translate, false, 0, 0, 0, 0);
Romain Guyff316ec2013-02-13 18:39:43 -08002161 setupDrawTexture(texture->id);
2162 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002163 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002164 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002165
2166 glDrawArrays(GL_TRIANGLES, 0, count);
2167
Chris Craik6c15ffa2015-02-02 13:50:55 -08002168 int slot = mCaches.program().getAttrib("colors");
Romain Guyff316ec2013-02-13 18:39:43 -08002169 if (slot >= 0) {
2170 glDisableVertexAttribArray(slot);
2171 }
2172
Tom Hudson107843d2014-09-08 11:26:26 -04002173 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002174}
2175
Tom Hudson107843d2014-09-08 11:26:26 -04002176void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002177 float srcLeft, float srcTop, float srcRight, float srcBottom,
2178 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002179 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002180 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002181 return;
Romain Guy6926c722010-07-12 20:20:03 -07002182 }
2183
Chris Craik44eb2c02015-01-29 09:45:09 -08002184 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002185 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002186 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002187 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002188
Romain Guy8ba548f2010-06-30 19:21:21 -07002189 const float width = texture->width;
2190 const float height = texture->height;
2191
Romain Guy3b748a42013-04-17 18:54:38 -07002192 float u1 = fmax(0.0f, srcLeft / width);
2193 float v1 = fmax(0.0f, srcTop / height);
2194 float u2 = fmin(1.0f, srcRight / width);
2195 float v2 = fmin(1.0f, srcBottom / height);
2196
2197 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002198
Chris Craik96a5c4c2015-01-27 15:46:35 -08002199 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002200 resetDrawTextureTexCoords(u1, v1, u2, v2);
2201
Romain Guyd21b6e12011-11-30 20:21:23 -08002202 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2203
Romain Guy886b2752013-01-04 12:26:18 -08002204 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2205 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002206
Romain Guy886b2752013-01-04 12:26:18 -08002207 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2208 // Apply a scale transform on the canvas only when a shader is in use
2209 // Skia handles the ratio between the dst and src rects as a scale factor
2210 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002211 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002212 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002213
Chris Craikd6b65f62014-01-01 14:45:21 -08002214 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2215 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2216 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002217
2218 dstRight = x + (dstRight - dstLeft);
2219 dstBottom = y + (dstBottom - dstTop);
2220
2221 dstLeft = x;
2222 dstTop = y;
2223
Chris Craik0519c812015-02-11 13:17:06 -08002224 texture->setFilter(scaled ? PaintUtils::getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002225 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002226 } else {
Chris Craik0519c812015-02-11 13:17:06 -08002227 texture->setFilter(PaintUtils::getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002228 }
2229
2230 if (CC_UNLIKELY(useScaleTransform)) {
2231 save(SkCanvas::kMatrix_SaveFlag);
2232 translate(dstLeft, dstTop);
2233 scale(scaleX, scaleY);
2234
2235 dstLeft = 0.0f;
2236 dstTop = 0.0f;
2237
2238 dstRight = srcRight - srcLeft;
2239 dstBottom = srcBottom - srcTop;
2240 }
2241
Mike Reed1103b322014-07-08 12:36:44 -04002242 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002243 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002244 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002245 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik117bdbc2015-02-05 10:12:38 -08002246 GL_TRIANGLE_STRIP, kUnitQuadCount, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002247 } else {
2248 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002249 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002250 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik117bdbc2015-02-05 10:12:38 -08002251 GL_TRIANGLE_STRIP, kUnitQuadCount, false, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002252 }
2253
2254 if (CC_UNLIKELY(useScaleTransform)) {
2255 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002256 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002257
2258 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002259
Tom Hudson107843d2014-09-08 11:26:26 -04002260 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002261}
2262
Tom Hudson107843d2014-09-08 11:26:26 -04002263void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002264 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002265 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002266 return;
Romain Guy6926c722010-07-12 20:20:03 -07002267 }
2268
John Reckebd52612014-12-10 16:47:36 -08002269 AssetAtlas::Entry* entry = mRenderState.assetAtlas().getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002270 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2271 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002272
Tom Hudson107843d2014-09-08 11:26:26 -04002273 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002274}
2275
Tom Hudson107843d2014-09-08 11:26:26 -04002276void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002277 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2278 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002279 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002280 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002281 }
2282
Romain Guy211370f2012-02-01 16:10:55 -08002283 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002284 mCaches.textureState().activateTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002285 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002286 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002287 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002288
Romain Guya4adcf02013-02-28 12:15:35 -08002289 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2290 texture->setFilter(GL_LINEAR, true);
2291
Chris Craikd6b65f62014-01-01 14:45:21 -08002292 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002293 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002294 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002295 const float offsetX = left + currentTransform()->getTranslateX();
2296 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002297 const size_t count = mesh->quads.size();
2298 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002299 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002300 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002301 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2302 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2303 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002304 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002305 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002306 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002307 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002308 }
2309 }
2310
Chris Craik4063a0e2013-11-15 16:06:56 -08002311 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002312 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002313 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2314 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002315
Romain Guy3b748a42013-04-17 18:54:38 -07002316 right = x + right - left;
2317 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002318 left = x;
2319 top = y;
2320 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002321 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002322 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2323 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002324 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2325 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002326 }
Chet Haase48659092012-05-31 15:21:51 -07002327
Tom Hudson107843d2014-09-08 11:26:26 -04002328 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002329}
2330
Romain Guy03c00b52013-06-20 18:30:28 -07002331/**
2332 * Important note: this method is intended to draw batches of 9-patch objects and
2333 * will not set the scissor enable or dirty the current layer, if any.
2334 * The caller is responsible for properly dirtying the current layer.
2335 */
Tom Hudson107843d2014-09-08 11:26:26 -04002336void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002337 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002338 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07002339 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002340 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002341 const AutoTexture autoCleanup(texture);
2342
2343 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2344 texture->setFilter(GL_LINEAR, true);
2345
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002346 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2347 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002348 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002349
Tom Hudson107843d2014-09-08 11:26:26 -04002350 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002351}
2352
Tom Hudson107843d2014-09-08 11:26:26 -04002353void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002354 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002355 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002356 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002357 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002358 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002359 }
2360
Chris Craik0519c812015-02-11 13:17:06 -08002361 if (USE_GLOPS && !paint->getShader()) {
Chris Craik117bdbc2015-02-05 10:12:38 -08002362 Glop glop;
2363 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
2364 bool fudgeOffset = displayFlags & kVertexBuffer_Offset;
2365 bool shadowInterp = displayFlags & kVertexBuffer_ShadowInterp;
2366 aBuilder.setMeshVertexBuffer(vertexBuffer, shadowInterp)
Chris Craik0519c812015-02-11 13:17:06 -08002367 .setFillPaint(*paint, currentSnapshot()->alpha)
2368 .setTransformClip(currentSnapshot()->getOrthoMatrix(), *currentTransform(), fudgeOffset)
Chris Craik117bdbc2015-02-05 10:12:38 -08002369 .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds())
Chris Craik0519c812015-02-11 13:17:06 -08002370 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik117bdbc2015-02-05 10:12:38 -08002371 .build();
2372 renderGlop(glop);
2373 return;
2374 }
2375
Chris Craik117bdbc2015-02-05 10:12:38 -08002376 const VertexBuffer::MeshFeatureFlags meshFeatureFlags = vertexBuffer.getMeshFeatureFlags();
Chris Craik0d5ac952014-07-15 13:01:02 -07002377 Rect bounds(vertexBuffer.getBounds());
2378 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002379 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2380
Chris Craikcb4d6002012-09-25 12:00:29 -07002381 int color = paint->getColor();
Chris Craik117bdbc2015-02-05 10:12:38 -08002382 bool isAA = meshFeatureFlags & VertexBuffer::kAlpha;
Chris Craikcb4d6002012-09-25 12:00:29 -07002383
Chris Craik710f46d2012-09-17 17:25:49 -07002384 setupDraw();
2385 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002386 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Chris Craik117bdbc2015-02-05 10:12:38 -08002387 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002388 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002389 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002390 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002391 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002392 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2393 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002394 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002395 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002396 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002397
ztenghui55bfb4e2013-12-03 10:38:55 -08002398 const void* vertices = vertexBuffer.getBuffer();
Chris Craik96a5c4c2015-01-27 15:46:35 -08002399 mRenderState.meshState().unbindMeshBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08002400 mRenderState.meshState().bindPositionVertexPointer(true, vertices,
2401 isAA ? kAlphaVertexStride : kVertexStride);
Chris Craik96a5c4c2015-01-27 15:46:35 -08002402 mRenderState.meshState().resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002403
Chris Craik710f46d2012-09-17 17:25:49 -07002404 int alphaSlot = -1;
2405 if (isAA) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002406 void* alphaCoords = ((GLbyte*) vertices) + kVertexAlphaOffset;
Chris Craik6c15ffa2015-02-02 13:50:55 -08002407 alphaSlot = mCaches.program().getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002408 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002409 glEnableVertexAttribArray(alphaSlot);
Chris Craik96a5c4c2015-01-27 15:46:35 -08002410 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, kAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002411 }
Romain Guy04299382012-07-18 17:15:41 -07002412
Chris Craik117bdbc2015-02-05 10:12:38 -08002413 if (meshFeatureFlags & VertexBuffer::kIndices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002414 mRenderState.meshState().unbindIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002415 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(),
2416 GL_UNSIGNED_SHORT, vertexBuffer.getIndices());
Chris Craik117bdbc2015-02-05 10:12:38 -08002417 } else {
2418 mRenderState.meshState().unbindIndicesBuffer();
2419 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
ztenghui63d41ab2014-02-14 13:13:41 -08002420 }
Romain Guy04299382012-07-18 17:15:41 -07002421
Chris Craik710f46d2012-09-17 17:25:49 -07002422 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002423 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002424 }
Chris Craik65cd6122012-12-10 17:56:27 -08002425
Tom Hudson107843d2014-09-08 11:26:26 -04002426 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002427}
2428
2429/**
Chris Craik65cd6122012-12-10 17:56:27 -08002430 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2431 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2432 * screen space in all directions. However, instead of using a fragment shader to compute the
2433 * translucency of the color from its position, we simply use a varying parameter to define how far
2434 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2435 *
2436 * Doesn't yet support joins, caps, or path effects.
2437 */
Tom Hudson107843d2014-09-08 11:26:26 -04002438void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002439 VertexBuffer vertexBuffer;
2440 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002441 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002442 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002443}
2444
2445/**
2446 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2447 * and additional geometry for defining an alpha slope perimeter.
2448 *
2449 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2450 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2451 * in-shader alpha region, but found it to be taxing on some GPUs.
2452 *
2453 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2454 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002455 */
Tom Hudson107843d2014-09-08 11:26:26 -04002456void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002457 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002458
Chris Craik65cd6122012-12-10 17:56:27 -08002459 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002460
Chris Craik65cd6122012-12-10 17:56:27 -08002461 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002462 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2463 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002464
Chris Craik05f3d6e2014-06-02 16:27:04 -07002465 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002466 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002467 }
2468
Chris Craikbf759452014-08-11 16:00:44 -07002469 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002470 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002471}
2472
Tom Hudson107843d2014-09-08 11:26:26 -04002473void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002474 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002475
Chris Craik6d29c8d2013-05-08 18:35:44 -07002476 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002477
Chris Craik6d29c8d2013-05-08 18:35:44 -07002478 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002479 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002480
Chris Craik05f3d6e2014-06-02 16:27:04 -07002481 const Rect& bounds = buffer.getBounds();
2482 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002483 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002484 }
2485
Chris Craikbf759452014-08-11 16:00:44 -07002486 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002487 drawVertexBuffer(buffer, paint, displayFlags);
2488
2489 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002490}
2491
Tom Hudson107843d2014-09-08 11:26:26 -04002492void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002493 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002494 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002495
Rob Tsuk487a92c2015-01-06 13:22:54 -08002496 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002497 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002498
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002499 SkPaint paint;
2500 paint.setColor(color);
2501 paint.setXfermodeMode(mode);
2502
2503 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002504
Tom Hudson107843d2014-09-08 11:26:26 -04002505 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002506}
Romain Guy9d5316e2010-06-24 19:30:36 -07002507
Chris Craik30036092015-02-12 10:41:39 -08002508void OpenGLRenderer::drawShape(float left, float top, PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002509 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002510 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002511 const AutoTexture autoCleanup(texture);
2512
2513 const float x = left + texture->left - texture->offset;
2514 const float y = top + texture->top - texture->offset;
2515
2516 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002517
Tom Hudson107843d2014-09-08 11:26:26 -04002518 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002519}
2520
Tom Hudson107843d2014-09-08 11:26:26 -04002521void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002522 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002523 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002524 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002525 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002526 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002527 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002528
Chris Craike84a2082014-12-22 14:28:49 -08002529 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002530 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08002531 PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002532 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002533 drawShape(left, top, texture, p);
2534 } else {
2535 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2536 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2537 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002538 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002539}
2540
Tom Hudson107843d2014-09-08 11:26:26 -04002541void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002542 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002543 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002544 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002545 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002546 }
Chris Craike84a2082014-12-22 14:28:49 -08002547 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002548 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08002549 PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002550 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002551 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002552 SkPath path;
2553 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2554 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2555 } else {
2556 path.addCircle(x, y, radius);
2557 }
2558 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002559 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002560}
Romain Guy01d58e42011-01-19 21:54:02 -08002561
Tom Hudson107843d2014-09-08 11:26:26 -04002562void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002563 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002564 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002565 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002566 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002567 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002568 }
Romain Guy01d58e42011-01-19 21:54:02 -08002569
Chris Craike84a2082014-12-22 14:28:49 -08002570 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002571 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08002572 PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002573 drawShape(left, top, texture, p);
2574 } else {
2575 SkPath path;
2576 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2577 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2578 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2579 }
2580 path.addOval(rect);
2581 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002582 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002583}
2584
Tom Hudson107843d2014-09-08 11:26:26 -04002585void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002586 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002587 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002588 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002589 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002590 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002591 }
2592
Chris Craik780c1282012-10-04 14:10:49 -07002593 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08002594 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002595 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08002596 PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002597 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002598 drawShape(left, top, texture, p);
2599 return;
Chris Craik780c1282012-10-04 14:10:49 -07002600 }
Chris Craik780c1282012-10-04 14:10:49 -07002601 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2602 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2603 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2604 }
2605
2606 SkPath path;
2607 if (useCenter) {
2608 path.moveTo(rect.centerX(), rect.centerY());
2609 }
2610 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2611 if (useCenter) {
2612 path.close();
2613 }
Tom Hudson107843d2014-09-08 11:26:26 -04002614 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002615}
2616
Romain Guycf8675e2012-10-02 12:32:25 -07002617// See SkPaintDefaults.h
2618#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2619
Tom Hudson107843d2014-09-08 11:26:26 -04002620void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002621 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002622 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002623 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002624 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002625 return;
Romain Guy6926c722010-07-12 20:20:03 -07002626 }
2627
Chris Craik710f46d2012-09-17 17:25:49 -07002628 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002629 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08002630 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07002631 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002632 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08002633 PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002634 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002635 drawShape(left, top, texture, p);
2636 } else {
2637 SkPath path;
2638 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2639 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2640 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2641 }
2642 path.addRect(rect);
2643 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002644 }
Chet Haase858aa932011-05-12 09:06:00 -07002645 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002646 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2647 SkPath path;
2648 path.addRect(left, top, right, bottom);
2649 drawConvexPath(path, p);
2650 } else {
2651 drawColorRect(left, top, right, bottom, p);
2652
2653 mDirty = true;
2654 }
Chet Haase858aa932011-05-12 09:06:00 -07002655 }
Romain Guyc7d53492010-06-25 13:41:57 -07002656}
Romain Guy9d5316e2010-06-24 19:30:36 -07002657
Chris Craikd218a922014-01-02 17:13:34 -08002658void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2659 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002660 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002661 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07002662
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002663 TextShadow textShadow;
2664 if (!getTextShadow(paint, &textShadow)) {
2665 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2666 }
2667
Raph Levien416a8472012-07-19 22:48:17 -07002668 // NOTE: The drop shadow will not perform gamma correction
2669 // if shader-based correction is enabled
2670 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2671 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002672 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002673 // If the drop shadow exceeds the max texture size or couldn't be
2674 // allocated, skip drawing
2675 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002676 const AutoTexture autoCleanup(shadow);
2677
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002678 const float sx = x - shadow->left + textShadow.dx;
2679 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002680
Tom Hudson984162f2014-10-10 13:38:16 -04002681 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002682 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002683 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002684 }
2685
2686 setupDraw();
2687 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002688 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002689 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002690 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002691 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002692 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002693 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2694 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002695 setupDrawTexture(shadow->id);
2696 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002697 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002698 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08002699 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Raph Levien416a8472012-07-19 22:48:17 -07002700
Chris Craik117bdbc2015-02-05 10:12:38 -08002701 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Raph Levien416a8472012-07-19 22:48:17 -07002702}
2703
Romain Guy768bffc2013-02-27 13:50:45 -08002704bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002705 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05002706 return MathUtils::isZero(alpha)
2707 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002708}
2709
Tom Hudson107843d2014-09-08 11:26:26 -04002710void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002711 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002712 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002713 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002714 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002715
Romain Guy671d6cf2012-01-18 12:39:17 -08002716 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002717 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002718 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002719 }
2720
Chris Craik65fe5ee2015-01-26 18:06:29 -08002721 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002722
Romain Guy671d6cf2012-01-18 12:39:17 -08002723 float x = 0.0f;
2724 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002725 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002726 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002727 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2728 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002729 }
2730
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002731 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002732 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002733
2734 int alpha;
2735 SkXfermode::Mode mode;
2736 getAlphaAndMode(paint, &alpha, &mode);
2737
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002738 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002739 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002740 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002741 }
2742
Romain Guy671d6cf2012-01-18 12:39:17 -08002743 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002744 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002745 if (pureTranslate && !linearFilter) {
2746 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2747 }
Romain Guy257ae352013-03-20 16:31:12 -07002748 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002749
Rob Tsuk487a92c2015-01-06 13:22:54 -08002750 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002751 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2752
Romain Guy211370f2012-02-01 16:10:55 -08002753 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002754
Victoria Lease1e546812013-06-25 14:25:17 -07002755 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002756 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002757 positions, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002758 if (hasActiveLayer) {
2759 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002760 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002761 }
2762 dirtyLayerUnchecked(bounds, getRegion());
2763 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002764 }
Chet Haase48659092012-05-31 15:21:51 -07002765
Tom Hudson107843d2014-09-08 11:26:26 -04002766 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002767}
2768
Chris Craik59744b72014-07-01 17:56:52 -07002769bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002770 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002771 outMatrix->setIdentity();
2772 return false;
2773 } else if (CC_UNLIKELY(transform.isPerspective())) {
2774 outMatrix->setIdentity();
2775 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002776 }
Chris Craik59744b72014-07-01 17:56:52 -07002777
2778 /**
Chris Craika736cd92014-08-04 13:18:38 -07002779 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2780 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002781 */
2782 float sx, sy;
2783 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002784 outMatrix->setScale(
2785 roundf(fmaxf(1.0f, sx)),
2786 roundf(fmaxf(1.0f, sy)));
2787 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002788}
2789
Tom Hudson984162f2014-10-10 13:38:16 -04002790int OpenGLRenderer::getSaveCount() const {
2791 return mState.getSaveCount();
2792}
2793
2794int OpenGLRenderer::save(int flags) {
2795 return mState.save(flags);
2796}
2797
2798void OpenGLRenderer::restore() {
2799 return mState.restore();
2800}
2801
2802void OpenGLRenderer::restoreToCount(int saveCount) {
2803 return mState.restoreToCount(saveCount);
2804}
2805
2806void OpenGLRenderer::translate(float dx, float dy, float dz) {
2807 return mState.translate(dx, dy, dz);
2808}
2809
2810void OpenGLRenderer::rotate(float degrees) {
2811 return mState.rotate(degrees);
2812}
2813
2814void OpenGLRenderer::scale(float sx, float sy) {
2815 return mState.scale(sx, sy);
2816}
2817
2818void OpenGLRenderer::skew(float sx, float sy) {
2819 return mState.skew(sx, sy);
2820}
2821
2822void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2823 mState.setMatrix(matrix);
2824}
2825
2826void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2827 mState.concatMatrix(matrix);
2828}
2829
2830bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2831 return mState.clipRect(left, top, right, bottom, op);
2832}
2833
2834bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2835 return mState.clipPath(path, op);
2836}
2837
2838bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2839 return mState.clipRegion(region, op);
2840}
2841
2842void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2843 mState.setClippingOutline(allocator, outline);
2844}
2845
2846void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2847 const Rect& rect, float radius, bool highPriority) {
2848 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2849}
2850
Tom Hudson107843d2014-09-08 11:26:26 -04002851void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002852 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002853 DrawOpMode drawOpMode) {
2854
Chris Craik527a3aa2013-03-04 10:19:31 -08002855 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002856 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2857 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002858 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002859 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002860 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002861 }
Romain Guycac5fd32011-12-01 20:08:50 -08002862 }
2863
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002864 const float oldX = x;
2865 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002866
Chris Craikd6b65f62014-01-01 14:45:21 -08002867 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002868 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002869
Romain Guy211370f2012-02-01 16:10:55 -08002870 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002871 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2872 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002873 }
2874
Romain Guy86568192010-12-14 15:55:39 -08002875 int alpha;
2876 SkXfermode::Mode mode;
2877 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002878
Romain Guyc74f45a2013-02-26 19:10:14 -08002879 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2880
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002881 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002882 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002883 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002884 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002885 }
2886
Romain Guy19d4dd82013-03-04 11:14:26 -08002887 const bool hasActiveLayer = hasLayer();
2888
Romain Guy624234f2013-03-05 16:43:31 -08002889 // We only pass a partial transform to the font renderer. That partial
2890 // matrix defines how glyphs are rasterized. Typically we want glyphs
2891 // to be rasterized at their final size on screen, which means the partial
2892 // matrix needs to take the scale factor into account.
2893 // When a partial matrix is used to transform glyphs during rasterization,
2894 // the mesh is generated with the inverse transform (in the case of scale,
2895 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2896 // apply the full transform matrix at draw time in the vertex shader.
2897 // Applying the full matrix in the shader is the easiest way to handle
2898 // rotation and perspective and allows us to always generated quads in the
2899 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002900 SkMatrix fontTransform;
2901 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2902 || fabs(y - (int) y) > 0.0f
2903 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002904 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002905 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002906
Romain Guy624234f2013-03-05 16:43:31 -08002907 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08002908 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002909 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 -07002910
Raph Levien996e57c2012-07-23 15:22:52 -07002911 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002912 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002913
2914 // don't call issuedrawcommand, do it at end of batch
2915 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002916 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002917 SkPaint paintCopy(*paint);
2918 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2919 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002920 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002921 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002922 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002923 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002924 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002925
Chris Craik527a3aa2013-03-04 10:19:31 -08002926 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002927 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002928 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002929 }
Chris Craik41541822013-05-03 16:35:54 -07002930 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002931 }
Romain Guy694b5192010-07-21 21:33:20 -07002932
Chris Craike63f7c622013-10-17 10:30:55 -07002933 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002934
Tom Hudson107843d2014-09-08 11:26:26 -04002935 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002936}
2937
Tom Hudson107843d2014-09-08 11:26:26 -04002938void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002939 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002940 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002941 return;
Romain Guy03d58522012-02-24 17:54:07 -08002942 }
2943
Chris Craik39a908c2013-06-13 14:39:01 -07002944 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002945 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002946
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002947 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002948 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002949 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002950
2951 int alpha;
2952 SkXfermode::Mode mode;
2953 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002954 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002955
Tom Hudson984162f2014-10-10 13:38:16 -04002956 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002957 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 -08002958
Romain Guy97771732012-02-28 18:17:02 -08002959 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002960
2961 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chris Craike84a2082014-12-22 14:28:49 -08002962 hOffset, vOffset, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002963 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002964 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002965 dirtyLayerUnchecked(bounds, getRegion());
2966 }
Romain Guy97771732012-02-28 18:17:02 -08002967 }
Chet Haase48659092012-05-31 15:21:51 -07002968
Tom Hudson107843d2014-09-08 11:26:26 -04002969 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002970}
2971
Tom Hudson107843d2014-09-08 11:26:26 -04002972void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002973 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002974
Chris Craik44eb2c02015-01-29 09:45:09 -08002975 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002976
Chris Craik30036092015-02-12 10:41:39 -08002977 PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002978 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002979 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002980
Romain Guy8b55f372010-08-18 17:10:07 -07002981 const float x = texture->left - texture->offset;
2982 const float y = texture->top - texture->offset;
2983
Romain Guy01d58e42011-01-19 21:54:02 -08002984 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002985 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002986}
2987
Tom Hudson107843d2014-09-08 11:26:26 -04002988void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002989 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002990 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002991 }
2992
Chris Craike84a2082014-12-22 14:28:49 -08002993 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002994 if (layer->isTextureLayer()) {
2995 transform = &layer->getTransform();
2996 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002997 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002998 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002999 }
3000 }
3001
Chris Craik39a908c2013-06-13 14:39:01 -07003002 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08003003 const bool rejected = mState.calculateQuickRejectForScissor(
3004 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3005 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07003006
3007 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003008 if (transform && !transform->isIdentity()) {
3009 restore();
3010 }
Tom Hudson107843d2014-09-08 11:26:26 -04003011 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08003012 }
3013
Chris Craik62d307c2014-07-29 10:35:13 -07003014 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
3015 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
3016
Romain Guy5bb3c732012-11-29 17:52:58 -08003017 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003018
Chris Craik65fe5ee2015-01-26 18:06:29 -08003019 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08003020 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003021
Romain Guy211370f2012-02-01 16:10:55 -08003022 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08003023 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003024 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3025 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003026 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003027
Chris Craik16ecda52013-03-29 10:59:59 -07003028 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003029 setupDraw();
3030 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003031 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003032 setupDrawColorFilter(layer->getColorFilter());
3033 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003034 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003035 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003036 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07003037 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08003038 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3039 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3040 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003041
Romain Guyd21b6e12011-11-30 20:21:23 -08003042 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003043 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003044 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003045 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003046 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003047 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003048 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3049 }
Romain Guyf219da52011-01-16 12:54:25 -08003050
Romain Guy448455f2013-07-22 13:57:50 -07003051 TextureVertex* mesh = &layer->mesh[0];
3052 GLsizei elementsCount = layer->meshElementCount;
3053
3054 while (elementsCount > 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08003055 GLsizei drawCount = min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Romain Guy448455f2013-07-22 13:57:50 -07003056
Romain Guy3380cfd2013-08-15 16:57:57 -07003057 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003058 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
Chris Craike84a2082014-12-22 14:28:49 -08003059 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr));
Romain Guy448455f2013-07-22 13:57:50 -07003060
3061 elementsCount -= drawCount;
3062 // Though there are 4 vertices in a quad, we use 6 indices per
3063 // quad to draw with GL_TRIANGLES
3064 mesh += (drawCount / 6) * 4;
3065 }
Romain Guyf219da52011-01-16 12:54:25 -08003066
Romain Guy3a3133d2011-02-01 22:59:58 -08003067#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003068 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003069#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003070 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003071
Romain Guy5bb3c732012-11-29 17:52:58 -08003072 if (layer->debugDrawUpdate) {
3073 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003074
3075 SkPaint paint;
3076 paint.setColor(0x7f00ff00);
3077 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003078 }
Romain Guyf219da52011-01-16 12:54:25 -08003079 }
Chris Craik34416ea2013-04-15 16:08:28 -07003080 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003081
Romain Guyb2e2f242012-10-17 18:18:35 -07003082 if (transform && !transform->isIdentity()) {
3083 restore();
3084 }
3085
Tom Hudson107843d2014-09-08 11:26:26 -04003086 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003087}
3088
Romain Guy6926c722010-07-12 20:20:03 -07003089///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003090// Draw filters
3091///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003092void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3093 // We should never get here since we apply the draw filter when stashing
3094 // the paints in the DisplayList.
3095 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003096}
3097
3098///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003099// Drawing implementation
3100///////////////////////////////////////////////////////////////////////////////
3101
Chris Craikd218a922014-01-02 17:13:34 -08003102Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003103 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003104 if (!texture) {
3105 return mCaches.textureCache.get(bitmap);
3106 }
3107 return texture;
3108}
3109
Chris Craik30036092015-02-12 10:41:39 -08003110void OpenGLRenderer::drawPathTexture(PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003111 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003112 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003113 return;
3114 }
3115
Chris Craik30036092015-02-12 10:41:39 -08003116 if (USE_GLOPS && !paint->getShader()) {
3117 Glop glop;
3118 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
3119 aBuilder.setMeshTexturedUnitQuad(nullptr, true)
3120 .setFillPathTexturePaint(*texture, *paint, currentSnapshot()->alpha)
3121 .setTransformClip(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
3122 .setModelViewMapUnitToRect(Rect(x, y, x + texture->width, y + texture->height))
3123 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
3124 .build();
3125 renderGlop(glop);
3126 return;
3127 }
3128
3129
Romain Guy01d58e42011-01-19 21:54:02 -08003130 int alpha;
3131 SkXfermode::Mode mode;
3132 getAlphaAndMode(paint, &alpha, &mode);
3133
3134 setupDraw();
3135 setupDrawWithTexture(true);
3136 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003137 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003138 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003139 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003140 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003141 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3142 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003143 setupDrawTexture(texture->id);
3144 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003145 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003146 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08003147 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Romain Guy01d58e42011-01-19 21:54:02 -08003148
Chris Craik117bdbc2015-02-05 10:12:38 -08003149 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003150}
3151
Romain Guyf607bdc2010-09-10 19:20:06 -07003152// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003153#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3154#define kStdUnderline_Offset (1.0f / 9.0f)
3155#define kStdUnderline_Thickness (1.0f / 18.0f)
3156
Chris Craikd218a922014-01-02 17:13:34 -08003157void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3158 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003159 // Handle underline and strike-through
3160 uint32_t flags = paint->getFlags();
3161 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003162 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003163
Romain Guy211370f2012-02-01 16:10:55 -08003164 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003165 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003166 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003167
Raph Levien8b4072d2012-07-30 15:50:00 -07003168 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003169 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003170
Romain Guyf6834472011-01-23 13:32:12 -08003171 int linesCount = 0;
3172 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3173 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3174
3175 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003176 float points[pointsCount];
3177 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003178
3179 if (flags & SkPaint::kUnderlineText_Flag) {
3180 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003181 points[currentPoint++] = left;
3182 points[currentPoint++] = top;
3183 points[currentPoint++] = left + underlineWidth;
3184 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003185 }
3186
3187 if (flags & SkPaint::kStrikeThruText_Flag) {
3188 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003189 points[currentPoint++] = left;
3190 points[currentPoint++] = top;
3191 points[currentPoint++] = left + underlineWidth;
3192 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003193 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003194
Romain Guy726aeba2011-06-01 14:52:00 -07003195 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003196
Romain Guy726aeba2011-06-01 14:52:00 -07003197 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003198 }
3199 }
3200}
3201
Tom Hudson107843d2014-09-08 11:26:26 -04003202void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003203 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003204 return;
Romain Guy672433d2013-01-04 19:05:13 -08003205 }
3206
Tom Hudson107843d2014-09-08 11:26:26 -04003207 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003208}
3209
Tom Hudson107843d2014-09-08 11:26:26 -04003210void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003211 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003212 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003213
Chris Craik15a07a22014-01-26 13:43:53 -08003214 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08003215 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07003216
3217 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003218 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003219
ztenghui14a4e352014-08-13 10:44:39 -07003220 // The caller has made sure casterAlpha > 0.
3221 float ambientShadowAlpha = mAmbientShadowAlpha;
3222 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3223 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3224 }
3225 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3226 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003227 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003228 }
ztenghui7b4516e2014-01-07 10:42:55 -08003229
ztenghui14a4e352014-08-13 10:44:39 -07003230 float spotShadowAlpha = mSpotShadowAlpha;
3231 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3232 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3233 }
3234 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3235 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003236 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003237 }
ztenghui7b4516e2014-01-07 10:42:55 -08003238
Tom Hudson107843d2014-09-08 11:26:26 -04003239 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003240}
3241
Tom Hudson107843d2014-09-08 11:26:26 -04003242void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003243 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003244 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003245 return;
Romain Guy3b753822013-03-05 10:27:35 -08003246 }
Romain Guy735738c2012-12-03 12:34:51 -08003247
Romain Guy672433d2013-01-04 19:05:13 -08003248 float left = FLT_MAX;
3249 float top = FLT_MAX;
3250 float right = FLT_MIN;
3251 float bottom = FLT_MIN;
3252
Romain Guy448455f2013-07-22 13:57:50 -07003253 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003254 Vertex* vertex = mesh;
3255
Chris Craik2af46352012-11-26 18:30:17 -08003256 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003257 float l = rects[index + 0];
3258 float t = rects[index + 1];
3259 float r = rects[index + 2];
3260 float b = rects[index + 3];
3261
Romain Guy3b753822013-03-05 10:27:35 -08003262 Vertex::set(vertex++, l, t);
3263 Vertex::set(vertex++, r, t);
3264 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003265 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003266
Romain Guy3b753822013-03-05 10:27:35 -08003267 left = fminf(left, l);
3268 top = fminf(top, t);
3269 right = fmaxf(right, r);
3270 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003271 }
3272
Chris Craikf0a59072013-11-19 18:00:46 -08003273 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003274 return;
Romain Guya362c692013-02-04 13:50:16 -08003275 }
Romain Guy672433d2013-01-04 19:05:13 -08003276
Chris Craik0519c812015-02-11 13:17:06 -08003277 if (USE_GLOPS && !paint->getShader()) {
Chris Craik2ab95d72015-02-06 15:25:51 -08003278 const Matrix4& transform = ignoreTransform ? Matrix4::identity() : *currentTransform();
3279 Glop glop;
3280 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
3281 aBuilder.setMeshIndexedQuads(&mesh[0], count / 4)
Chris Craik0519c812015-02-11 13:17:06 -08003282 .setFillPaint(*paint, currentSnapshot()->alpha)
3283 .setTransformClip(currentSnapshot()->getOrthoMatrix(), transform, false)
Chris Craik2ab95d72015-02-06 15:25:51 -08003284 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
Chris Craik0519c812015-02-11 13:17:06 -08003285 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik2ab95d72015-02-06 15:25:51 -08003286 .build();
3287 renderGlop(glop);
3288 return;
3289 }
3290
3291 int color = paint->getColor();
3292 // If a shader is set, preserve only the alpha
3293 if (getShader(paint)) {
3294 color |= 0x00ffffff;
3295 }
3296
Romain Guy672433d2013-01-04 19:05:13 -08003297 setupDraw();
3298 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003299 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003300 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003301 setupDrawColorFilter(getColorFilter(paint));
3302 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003303 setupDrawProgram();
3304 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003305 setupDrawModelView(kModelViewMode_Translate, false,
3306 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003307 setupDrawColorUniforms(getShader(paint));
3308 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003309 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003310
Romain Guy8ce00302013-01-15 18:51:42 -08003311 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003312 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003313 }
3314
Chris Craik4063a0e2013-11-15 16:06:56 -08003315 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003316
Tom Hudson107843d2014-09-08 11:26:26 -04003317 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003318}
3319
Romain Guy026c5e162010-06-28 17:12:22 -07003320void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003321 const SkPaint* paint, bool ignoreTransform) {
Chris Craik117bdbc2015-02-05 10:12:38 -08003322
Chris Craik0519c812015-02-11 13:17:06 -08003323 if (USE_GLOPS && !paint->getShader()) {
Chris Craik117bdbc2015-02-05 10:12:38 -08003324 const Matrix4& transform = ignoreTransform ? Matrix4::identity() : *currentTransform();
3325 Glop glop;
3326 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
3327 aBuilder.setMeshUnitQuad()
Chris Craik0519c812015-02-11 13:17:06 -08003328 .setFillPaint(*paint, currentSnapshot()->alpha)
3329 .setTransformClip(currentSnapshot()->getOrthoMatrix(), transform, false)
Chris Craik117bdbc2015-02-05 10:12:38 -08003330 .setModelViewMapUnitToRect(Rect(left, top, right, bottom))
Chris Craik0519c812015-02-11 13:17:06 -08003331 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik117bdbc2015-02-05 10:12:38 -08003332 .build();
3333 renderGlop(glop);
3334 return;
3335 }
3336
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003337 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003338 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003339 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003340 color |= 0x00ffffff;
3341 }
3342
Romain Guy70ca14e2010-12-13 18:24:33 -08003343 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003344 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003345 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003346 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003347 setupDrawColorFilter(getColorFilter(paint));
3348 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003349 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003350 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3351 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003352 setupDrawColorUniforms(getShader(paint));
3353 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003354 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003355 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003356
Chris Craik117bdbc2015-02-05 10:12:38 -08003357 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Romain Guyc95c8d62010-09-17 15:31:32 -07003358}
3359
Chris Craik0519c812015-02-11 13:17:06 -08003360void OpenGLRenderer::drawTextureRect(Texture* texture, const SkPaint* paint) {
3361 if (USE_GLOPS) {
3362 Glop glop;
3363 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
3364 aBuilder.setMeshTexturedUnitQuad(texture->uvMapper, false)
3365 .setFillTexturePaint(*texture, false, paint, currentSnapshot()->alpha)
3366 .setTransformClip(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
3367 .setModelViewMapUnitToRectSnap(Rect(0, 0, texture->width, texture->height))
3368 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
3369 .build();
3370 renderGlop(glop);
3371 return;
3372 }
3373
Romain Guyd21b6e12011-11-30 20:21:23 -08003374 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003375
Chris Craike84a2082014-12-22 14:28:49 -08003376 GLvoid* vertices = (GLvoid*) nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -08003377 GLvoid* texCoords = (GLvoid*) kMeshTextureOffset;
Romain Guy3b748a42013-04-17 18:54:38 -07003378
3379 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003380 vertices = &mMeshVertices[0].x;
3381 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003382
3383 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3384 texture->uvMapper->map(uvs);
3385
3386 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3387 }
3388
Chris Craikd6b65f62014-01-01 14:45:21 -08003389 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
Chris Craik0519c812015-02-11 13:17:06 -08003390 const float x = (int) floorf(currentTransform()->getTranslateX() + 0.5f);
3391 const float y = (int) floorf(currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003392
Romain Guyd21b6e12011-11-30 20:21:23 -08003393 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003394 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003395 paint, texture->blend, vertices, texCoords,
Chris Craik117bdbc2015-02-05 10:12:38 -08003396 GL_TRIANGLE_STRIP, kUnitQuadCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003397 } else {
Chris Craik0519c812015-02-11 13:17:06 -08003398 texture->setFilter(PaintUtils::getFilter(paint), true);
3399 drawTextureMesh(0, 0, texture->width, texture->height, texture->id, paint,
Chris Craik117bdbc2015-02-05 10:12:38 -08003400 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, kUnitQuadCount);
Romain Guy3b748a42013-04-17 18:54:38 -07003401 }
3402
3403 if (texture->uvMapper) {
3404 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003405 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003406}
3407
Romain Guyf7f93552010-07-08 19:17:03 -07003408void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003409 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003410 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003411 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3412 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003413
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003414 int a;
3415 SkXfermode::Mode mode;
3416 getAlphaAndMode(paint, &a, &mode);
3417 const float alpha = a / 255.0f;
3418
Romain Guy746b7402010-10-26 16:27:31 -07003419 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003420 setupDrawWithTexture();
3421 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003422 setupDrawColorFilter(getColorFilter(paint));
3423 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003424 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003425 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003426 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003427 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003428 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003429 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003430 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003431
Romain Guy6820ac82010-09-15 18:11:50 -07003432 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003433}
3434
Romain Guy3b748a42013-04-17 18:54:38 -07003435void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003436 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003437 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003438 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3439 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003440
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003441 int a;
3442 SkXfermode::Mode mode;
3443 getAlphaAndMode(paint, &a, &mode);
3444 const float alpha = a / 255.0f;
3445
Romain Guy3b748a42013-04-17 18:54:38 -07003446 setupDraw();
3447 setupDrawWithTexture();
3448 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003449 setupDrawColorFilter(getColorFilter(paint));
3450 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003451 setupDrawProgram();
3452 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003453 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003454 setupDrawTexture(texture);
3455 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003456 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003457 setupDrawMeshIndices(vertices, texCoords, vbo);
3458
Chris Craike84a2082014-12-22 14:28:49 -08003459 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy3b748a42013-04-17 18:54:38 -07003460}
3461
Romain Guy886b2752013-01-04 12:26:18 -08003462void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003463 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003464 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003465 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003466
Chris Craike84a2082014-12-22 14:28:49 -08003467 int color = paint != nullptr ? paint->getColor() : 0;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003468 int alpha;
3469 SkXfermode::Mode mode;
3470 getAlphaAndMode(paint, &alpha, &mode);
3471
Romain Guy886b2752013-01-04 12:26:18 -08003472 setupDraw();
3473 setupDrawWithTexture(true);
Chris Craike84a2082014-12-22 14:28:49 -08003474 if (paint != nullptr) {
Romain Guy886b2752013-01-04 12:26:18 -08003475 setupDrawAlpha8Color(color, alpha);
3476 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003477 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003478 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003479 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003480 setupDrawProgram();
3481 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003482 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003483 setupDrawTexture(texture);
3484 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003485 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003486 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003487 setupDrawMesh(vertices, texCoords);
3488
3489 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003490}
3491
Romain Guya5aed0d2010-09-09 14:42:43 -07003492void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003493 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003494
Chris Craik117bdbc2015-02-05 10:12:38 -08003495 if (currentSnapshot()->roundRectClipState != nullptr /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003496 blend = true;
3497 mDescription.hasRoundRectClip = true;
3498 }
3499 mSkipOutlineClip = true;
3500
Romain Guy82ba8142010-07-09 13:25:56 -07003501 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003502
Romain Guy82ba8142010-07-09 13:25:56 -07003503 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003504 // These blend modes are not supported by OpenGL directly and have
3505 // to be implemented using shaders. Since the shader will perform
3506 // the blending, turn blending off here
3507 // If the blend mode cannot be implemented using shaders, fall
3508 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003509 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Chris Craik117bdbc2015-02-05 10:12:38 -08003510 if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003511 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003512 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003513
Chris Craik44eb2c02015-01-29 09:45:09 -08003514 mRenderState.blend().disable();
Romain Guy82bc7a72012-01-03 14:13:39 -08003515 return;
3516 } else {
3517 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003518 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003519 }
Chris Craik44eb2c02015-01-29 09:45:09 -08003520 mRenderState.blend().enable(mode, swapSrcDst);
3521 } else {
3522 mRenderState.blend().disable();
Romain Guy82ba8142010-07-09 13:25:56 -07003523 }
Romain Guybd6b79b2010-06-26 00:13:53 -07003524}
3525
Romain Guy026c5e162010-06-28 17:12:22 -07003526void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003527 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003528 TextureVertex::setUV(v++, u1, v1);
3529 TextureVertex::setUV(v++, u2, v1);
3530 TextureVertex::setUV(v++, u1, v2);
3531 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003532}
3533
Chris Craike84a2082014-12-22 14:28:49 -08003534void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
3535 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003536 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003537 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3538 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003539 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003540 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003541 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003542}
3543
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003544float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003545 float alpha;
3546 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3547 alpha = mDrawModifiers.mOverrideLayerAlpha;
3548 } else {
3549 alpha = layer->getAlpha() / 255.0f;
3550 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003551 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003552}
3553
Romain Guy9d5316e2010-06-24 19:30:36 -07003554}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003555}; // namespace android