blob: 09674f73ca602a99218120ae0c5f3ad826596d69 [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"
Chris Craik65fe5ee2015-01-26 18:06:29 -080020#include "GammaFontRenderer.h"
Chris Craik03188872015-02-02 18:39:33 -080021#include "Glop.h"
22#include "GlopBuilder.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080023#include "Patch.h"
24#include "PathTessellator.h"
25#include "Properties.h"
26#include "RenderNode.h"
Chris Craik03188872015-02-02 18:39:33 -080027#include "renderstate/MeshState.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080028#include "renderstate/RenderState.h"
29#include "ShadowTessellator.h"
30#include "SkiaShader.h"
31#include "Vector.h"
32#include "VertexBuffer.h"
33#include "utils/GLUtils.h"
34#include "utils/PaintUtils.h"
35#include "utils/TraceUtils.h"
36
Romain Guye4d01122010-06-16 18:44:05 -070037#include <stdlib.h>
38#include <stdint.h>
39#include <sys/types.h>
40
Romain Guy5cbbce52010-06-27 22:59:20 -070041#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070042#include <SkColor.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040043#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070044#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070045
Romain Guye4d01122010-06-16 18:44:05 -070046#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070047#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070048
Romain Guy08aa2cb2011-03-17 11:06:57 -070049#include <private/hwui/DrawGlInfo.h>
50
Romain Guy5b3b3522010-10-27 18:57:51 -070051#include <ui/Rect.h>
52
Chris Craik62d307c2014-07-29 10:35:13 -070053#if DEBUG_DETAILED_EVENTS
54 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
55#else
56 #define EVENT_LOGD(...)
57#endif
58
Romain Guye4d01122010-06-16 18:44:05 -070059namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070060namespace uirenderer {
61
Romain Guy9d5316e2010-06-24 19:30:36 -070062///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -070063// Constructors/destructor
64///////////////////////////////////////////////////////////////////////////////
65
John Reck3b202512014-06-23 13:13:08 -070066OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -040067 : mState(*this)
Chris Craik058fc642014-07-23 18:19:28 -070068 , mCaches(Caches::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -070069 , mRenderState(renderState)
Chris Craik65fe5ee2015-01-26 18:06:29 -080070 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -070071 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -070072 , mSuppressTiling(false)
73 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -040074 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -070075 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -070076 , mLightRadius(FLT_MIN)
77 , mAmbientShadowAlpha(0)
78 , mSpotShadowAlpha(0) {
Romain Guye4d01122010-06-16 18:44:05 -070079}
80
Romain Guy85bf02f2010-06-22 13:11:24 -070081OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -070082 // The context has already been destroyed at this point, do not call
83 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -070084}
85
Romain Guy87e2f7572012-09-24 11:37:12 -070086void OpenGLRenderer::initProperties() {
87 char property[PROPERTY_VALUE_MAX];
88 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
89 mScissorOptimizationDisabled = !strcasecmp(property, "true");
90 INIT_LOGD(" Scissor optimization %s",
91 mScissorOptimizationDisabled ? "disabled" : "enabled");
92 } else {
93 INIT_LOGD(" Scissor optimization enabled");
94 }
Romain Guy13631f32012-01-30 17:41:55 -080095}
96
Chris Craik058fc642014-07-23 18:19:28 -070097void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
98 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
99 mLightCenter = lightCenter;
100 mLightRadius = lightRadius;
101 mAmbientShadowAlpha = ambientShadowAlpha;
102 mSpotShadowAlpha = spotShadowAlpha;
103}
104
Romain Guy13631f32012-01-30 17:41:55 -0800105///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700106// Setup
107///////////////////////////////////////////////////////////////////////////////
108
Chris Craik797b95b22014-05-20 18:10:25 -0700109void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700110 glDisable(GL_DITHER);
111 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Chris Craik284b2432014-09-18 16:05:35 -0700112 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700113}
114
Romain Guy96885eb2013-03-26 15:05:58 -0700115void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800116 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800117 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400118 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700119 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700120 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700121}
122
Tom Hudson107843d2014-09-08 11:26:26 -0400123void OpenGLRenderer::startFrame() {
124 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700125 mFrameStarted = true;
126
Tom Hudson984162f2014-10-10 13:38:16 -0400127 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700128
Romain Guy96885eb2013-03-26 15:05:58 -0700129 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700130
Tom Hudson984162f2014-10-10 13:38:16 -0400131 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800132
Romain Guy54c1a642012-09-27 17:55:46 -0700133 // Functors break the tiling extension in pretty spectacular ways
134 // This ensures we don't use tiling when a functor is going to be
135 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700136 mSuppressTiling = mCaches.hasRegisteredFunctors()
137 || mFirstFrameAfterResize;
138 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700139
Chris Craikd6b65f62014-01-01 14:45:21 -0800140 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700141
Romain Guy7c450aa2012-09-21 19:15:00 -0700142 debugOverdraw(true, true);
143
Tom Hudson107843d2014-09-08 11:26:26 -0400144 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700145 mTilingClip.right, mTilingClip.bottom, mOpaque);
146}
147
Tom Hudson107843d2014-09-08 11:26:26 -0400148void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700149 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700150
Romain Guy96885eb2013-03-26 15:05:58 -0700151 setupFrameState(left, top, right, bottom, opaque);
152
153 // Layer renderers will start the frame immediately
154 // The framebuffer renderer will first defer the display list
155 // for each layer and wait until the first drawing command
156 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800157 if (currentSnapshot()->fbo == 0) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800158 mRenderState.blend().syncEnabled();
Romain Guy96885eb2013-03-26 15:05:58 -0700159 updateLayers();
160 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400161 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700162 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700163}
164
Romain Guydcfc8362013-01-03 13:08:57 -0800165void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
166 // If we know that we are going to redraw the entire framebuffer,
167 // perform a discard to let the driver know we don't need to preserve
168 // the back buffer for this frame.
Chris Craik117bdbc2015-02-05 10:12:38 -0800169 if (mCaches.extensions().hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400170 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
Chris Craik6b109c72015-02-27 10:55:28 -0800171 const bool isFbo = getTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800172 const GLenum attachments[] = {
173 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
174 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800175 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
176 }
177}
178
Tom Hudson107843d2014-09-08 11:26:26 -0400179void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700180 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800181 mRenderState.scissor().setEnabled(true);
182 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700183 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400184 mDirty = true;
185 return;
Romain Guyddf74372012-05-22 14:07:07 -0700186 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700187
Chris Craik65fe5ee2015-01-26 18:06:29 -0800188 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700189}
190
henry.uh_chen33f5a592014-07-02 19:36:56 +0800191void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700192 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800193 const Snapshot* snapshot = currentSnapshot();
194
Chris Craik14e51302013-12-30 15:32:54 -0800195 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800196 if (snapshot->flags & Snapshot::kFlagFboTarget) {
197 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700198 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700199
henry.uh_chen33f5a592014-07-02 19:36:56 +0800200 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800201 }
202}
203
henry.uh_chen33f5a592014-07-02 19:36:56 +0800204void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800205 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800206 if(expand) {
207 // Expand the startTiling region by 1
208 int leftNotZero = (clip.left > 0) ? 1 : 0;
209 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
210
211 mCaches.startTiling(
212 clip.left - leftNotZero,
213 windowHeight - clip.bottom - topNotZero,
214 clip.right - clip.left + leftNotZero + 1,
215 clip.bottom - clip.top + topNotZero + 1,
216 opaque);
217 } else {
218 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800219 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800220 }
Romain Guy54c1a642012-09-27 17:55:46 -0700221 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700222}
223
224void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700225 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700226}
227
Tom Hudson107843d2014-09-08 11:26:26 -0400228bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700229 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700230 endTiling();
Chris Craik4ac36f82014-12-09 16:54:03 -0800231 mTempPaths.clear();
232
Romain Guyca89e2a2013-03-08 17:44:20 -0800233 // When finish() is invoked on FBO 0 we've reached the end
234 // of the current frame
Chris Craik6b109c72015-02-27 10:55:28 -0800235 if (getTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800236 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700237 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800238 }
239
Romain Guy11cb6422012-09-21 00:39:43 -0700240 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700241#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700242 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700243#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700244
Romain Guyc15008e2010-11-10 11:59:15 -0800245#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800246 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700247#else
Chris Craik2507c342015-05-04 14:36:49 -0700248 if (Properties::debugLevel & kDebugMemory) {
Romain Guy11cb6422012-09-21 00:39:43 -0700249 mCaches.dumpMemoryUsage();
250 }
Romain Guyc15008e2010-11-10 11:59:15 -0800251#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700252 }
Romain Guy96885eb2013-03-26 15:05:58 -0700253
254 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400255
256 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700257}
258
Romain Guy35643dd2012-09-18 15:40:58 -0700259void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700260 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
261 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700262 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700263
Chris Craik65fe5ee2015-01-26 18:06:29 -0800264 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700265 dirtyClip();
266}
267
Andreas Gampe64bb4132014-11-22 00:35:09 +0000268void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400269 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700270
Rob Tsuk487a92c2015-01-06 13:22:54 -0800271 Rect clip(mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700272 clip.snapToPixelBoundaries();
273
Romain Guyd643bb52011-03-01 14:55:21 -0800274 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800275 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800276 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800277 dirtyLayerUnchecked(clip, getRegion());
278 }
Romain Guyd643bb52011-03-01 14:55:21 -0800279
Romain Guy08aa2cb2011-03-17 11:06:57 -0700280 DrawGlInfo info;
281 info.clipLeft = clip.left;
282 info.clipTop = clip.top;
283 info.clipRight = clip.right;
284 info.clipBottom = clip.bottom;
285 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700286 info.width = getViewportWidth();
287 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800288 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700289
Tom Hudson984162f2014-10-10 13:38:16 -0400290 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700291 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400292 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700293 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
294 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800295 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700296 setScissorFromClip();
297 }
Chris Craik54f574a2013-08-26 11:23:46 -0700298
John Reck3b202512014-06-23 13:13:08 -0700299 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
300 // Scissor may have been modified, reset dirty clip
301 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800302
Tom Hudson107843d2014-09-08 11:26:26 -0400303 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800304}
305
Romain Guyf6a11b82010-06-23 17:47:49 -0700306///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700307// Debug
308///////////////////////////////////////////////////////////////////////////////
309
Chris Craik62d307c2014-07-29 10:35:13 -0700310void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
311#if DEBUG_DETAILED_EVENTS
312 const int BUFFER_SIZE = 256;
313 va_list ap;
314 char buf[BUFFER_SIZE];
315
316 va_start(ap, fmt);
317 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
318 va_end(ap);
319
320 eventMark(buf);
321#endif
322}
323
324
Romain Guy0f6675332013-03-01 14:31:04 -0800325void OpenGLRenderer::eventMark(const char* name) const {
326 mCaches.eventMark(0, name);
327}
328
Romain Guy87e2f7572012-09-24 11:37:12 -0700329void OpenGLRenderer::startMark(const char* name) const {
330 mCaches.startMark(0, name);
331}
332
333void OpenGLRenderer::endMark() const {
334 mCaches.endMark();
335}
336
337void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700338 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700339}
340
341void OpenGLRenderer::renderOverdraw() {
Chris Craik2507c342015-05-04 14:36:49 -0700342 if (Properties::debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700343 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700344
Chris Craik65fe5ee2015-01-26 18:06:29 -0800345 mRenderState.scissor().setEnabled(true);
346 mRenderState.scissor().set(clip->left,
347 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
348 clip->right - clip->left,
349 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700350
Romain Guy627c6fd2013-08-21 11:53:18 -0700351 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800352 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700353 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
354
355 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800356 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700357 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
358
359 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800360 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700361 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
362
363 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800364 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700365 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
366
Chris Craik96a5c4c2015-01-27 15:46:35 -0800367 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700368 }
369}
370
371///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700372// Layers
373///////////////////////////////////////////////////////////////////////////////
374
375bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700376 if (layer->deferredUpdateScheduled && layer->renderer
377 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700378
Romain Guy7c450aa2012-09-21 19:15:00 -0700379 if (inFrame) {
380 endTiling();
381 debugOverdraw(false, false);
382 }
Romain Guy11cb6422012-09-21 00:39:43 -0700383
Chris Craik2507c342015-05-04 14:36:49 -0700384 if (CC_UNLIKELY(inFrame || Properties::drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700385 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700386 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700387 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700388 }
Romain Guy11cb6422012-09-21 00:39:43 -0700389
390 if (inFrame) {
391 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800392 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700393 }
394
Chris Craik2507c342015-05-04 14:36:49 -0700395 layer->debugDrawUpdate = Properties::debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700396 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700397
398 return true;
399 }
400
401 return false;
402}
403
404void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700405 // If draw deferring is enabled this method will simply defer
406 // the display list of each individual layer. The layers remain
407 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700408 int count = mLayerUpdates.size();
409 if (count > 0) {
Chris Craik2507c342015-05-04 14:36:49 -0700410 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -0700411 startMark("Layer Updates");
412 } else {
413 startMark("Defer Layer Updates");
414 }
Romain Guy11cb6422012-09-21 00:39:43 -0700415
Chris Craik1206b9b2013-04-04 14:46:24 -0700416 // Note: it is very important to update the layers in order
417 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700418 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700419 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700420 }
Romain Guy11cb6422012-09-21 00:39:43 -0700421
Chris Craik2507c342015-05-04 14:36:49 -0700422 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -0700423 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800424 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700425 }
426 endMark();
427 }
428}
429
430void OpenGLRenderer::flushLayers() {
431 int count = mLayerUpdates.size();
432 if (count > 0) {
433 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700434
Chris Craik1206b9b2013-04-04 14:46:24 -0700435 // Note: it is very important to update the layers in order
436 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800437 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700438 }
439
440 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800441 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700442
Romain Guy11cb6422012-09-21 00:39:43 -0700443 endMark();
444 }
445}
446
447void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
448 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700449 // Make sure we don't introduce duplicates.
450 // SortedVector would do this automatically but we need to respect
451 // the insertion order. The linear search is not an issue since
452 // this list is usually very short (typically one item, at most a few)
453 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
454 if (mLayerUpdates.itemAt(i) == layer) {
455 return;
456 }
457 }
Romain Guy11cb6422012-09-21 00:39:43 -0700458 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700459 }
460}
461
Romain Guye93482f2013-06-17 13:14:51 -0700462void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
463 if (layer) {
464 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
465 if (mLayerUpdates.itemAt(i) == layer) {
466 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700467 break;
468 }
469 }
470 }
471}
472
Romain Guy40543602013-06-12 15:31:28 -0700473void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800474 ATRACE_NAME("Update HW Layers");
Chris Craik44eb2c02015-01-29 09:45:09 -0800475 mRenderState.blend().syncEnabled();
Romain Guy40543602013-06-12 15:31:28 -0700476 updateLayers();
477 flushLayers();
478 // Wait for all the layer updates to be executed
John Reck55156372015-01-21 07:46:37 -0800479 glFinish();
Romain Guy40543602013-06-12 15:31:28 -0700480}
481
John Reck443a7142014-09-04 17:40:05 -0700482void OpenGLRenderer::markLayersAsBuildLayers() {
483 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
484 mLayerUpdates[i]->wasBuildLayered = true;
485 }
486}
487
Romain Guy11cb6422012-09-21 00:39:43 -0700488///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700489// State management
490///////////////////////////////////////////////////////////////////////////////
491
Chris Craik14e51302013-12-30 15:32:54 -0800492void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700493 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800494 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
495 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700496
Chris Craika64a2be2014-05-14 14:17:01 -0700497 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700498 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700499 }
500
Romain Guy2542d192010-08-18 11:47:12 -0700501 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700502 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700503 }
Romain Guy2542d192010-08-18 11:47:12 -0700504
Romain Guy5ec99242010-11-03 16:19:08 -0700505 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700506 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700507 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700508 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800509 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700510 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700511 }
Romain Guybb9524b2010-06-22 18:56:38 -0700512}
513
Romain Guyf6a11b82010-06-23 17:47:49 -0700514///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700515// Layers
516///////////////////////////////////////////////////////////////////////////////
517
518int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700519 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700520 // force matrix/clip isolation for layer
521 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
522
Tom Hudson984162f2014-10-10 13:38:16 -0400523 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700524
Tom Hudson984162f2014-10-10 13:38:16 -0400525 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700526 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700527 }
Romain Guyd55a8612010-06-28 17:42:46 -0700528
529 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700530}
531
Chris Craikd90144d2013-03-19 15:03:48 -0700532void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
533 const Rect untransformedBounds(bounds);
534
Chris Craikd6b65f62014-01-01 14:45:21 -0800535 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700536
537 // Layers only make sense if they are in the framebuffer's bounds
Rob Tsuk487a92c2015-01-06 13:22:54 -0800538 if (bounds.intersect(mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700539 // We cannot work with sub-pixels in this case
540 bounds.snapToPixelBoundaries();
541
542 // When the layer is not an FBO, we may use glCopyTexImage so we
543 // need to make sure the layer does not extend outside the bounds
544 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700545 const Snapshot& previous = *(currentSnapshot()->previous);
546 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
547 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700548 bounds.setEmpty();
549 } else if (fboLayer) {
550 clip.set(bounds);
551 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800552 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700553 inverse.mapRect(clip);
554 clip.snapToPixelBoundaries();
555 if (clip.intersect(untransformedBounds)) {
556 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
557 bounds.set(untransformedBounds);
558 } else {
559 clip.setEmpty();
560 }
561 }
562 } else {
563 bounds.setEmpty();
564 }
565}
566
Chris Craik408eb122013-03-26 18:55:15 -0700567void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
568 bool fboLayer, int alpha) {
569 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
570 bounds.getHeight() > mCaches.maxTextureSize ||
571 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400572 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700573 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400574 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700575 }
576}
577
Chris Craikd90144d2013-03-19 15:03:48 -0700578int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500579 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400580 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700581
Tom Hudson984162f2014-10-10 13:38:16 -0400582 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700583 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
584 // operations will be able to store and restore the current clip and transform info, and
585 // quick rejection will be correct (for display lists)
586
587 Rect bounds(left, top, right, bottom);
588 Rect clip;
589 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500590 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700591
Tom Hudson984162f2014-10-10 13:38:16 -0400592 if (!mState.currentlyIgnored()) {
593 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
594 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
595 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800596 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700597 }
598 }
599
600 return count;
601}
602
Romain Guy1c740bc2010-09-13 18:00:09 -0700603/**
604 * Layers are viewed by Skia are slightly different than layers in image editing
605 * programs (for instance.) When a layer is created, previously created layers
606 * and the frame buffer still receive every drawing command. For instance, if a
607 * layer is created and a shape intersecting the bounds of the layers and the
608 * framebuffer is draw, the shape will be drawn on both (unless the layer was
609 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
610 *
611 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
612 * texture. Unfortunately, this is inefficient as it requires every primitive to
613 * be drawn n + 1 times, where n is the number of active layers. In practice this
614 * means, for every primitive:
615 * - Switch active frame buffer
616 * - Change viewport, clip and projection matrix
617 * - Issue the drawing
618 *
619 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700620 * To avoid this, layers are implemented in a different way here, at least in the
621 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
622 * is set. When this flag is set we can redirect all drawing operations into a
623 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700624 *
625 * This implementation relies on the frame buffer being at least RGBA 8888. When
626 * a layer is created, only a texture is created, not an FBO. The content of the
627 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700628 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700629 * buffer and drawing continues as normal. This technique therefore treats the
630 * frame buffer as a scratch buffer for the layers.
631 *
632 * To compose the layers back onto the frame buffer, each layer texture
633 * (containing the original frame buffer data) is drawn as a simple quad over
634 * the frame buffer. The trick is that the quad is set as the composition
635 * destination in the blending equation, and the frame buffer becomes the source
636 * of the composition.
637 *
638 * Drawing layers with an alpha value requires an extra step before composition.
639 * An empty quad is drawn over the layer's region in the frame buffer. This quad
640 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
641 * quad is used to multiply the colors in the frame buffer. This is achieved by
642 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
643 * GL_ZERO, GL_SRC_ALPHA.
644 *
645 * Because glCopyTexImage2D() can be slow, an alternative implementation might
646 * be use to draw a single clipped layer. The implementation described above
647 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700648 *
649 * (1) The frame buffer is actually not cleared right away. To allow the GPU
650 * to potentially optimize series of calls to glCopyTexImage2D, the frame
651 * buffer is left untouched until the first drawing operation. Only when
652 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700653 */
Chet Haased48885a2012-08-28 17:43:28 -0700654bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700655 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800656 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
657 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700658
Romain Guyeb993562010-10-05 18:14:38 -0700659 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
660
Romain Guyf607bdc2010-09-10 19:20:06 -0700661 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700662 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700663 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700664 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000665 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700666
667 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400668 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700669 return false;
670 }
Romain Guyf18fd992010-07-08 11:45:51 -0700671
Chris Craik44eb2c02015-01-29 09:45:09 -0800672 mCaches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700673 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700674 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700675 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700676 }
677
Derek Sollenberger674554f2014-02-19 16:47:32 +0000678 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700679 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700680 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
681 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500682
Chet Haasea23eed82012-04-12 15:19:04 -0700683 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700684 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700685 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700686
Romain Guy8fb95422010-08-17 18:38:51 -0700687 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400688 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
689 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700690
Chris Craika8bea8e2014-09-24 11:29:43 -0700691 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
692 fboLayer ? "" : "unclipped ",
693 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700694 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700695 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700696 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700697 } else {
698 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700699 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800700 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700701 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700702 // Workaround for some GL drivers. When reading pixels lying outside
703 // of the window we should get undefined values for those pixels.
704 // Unfortunately some drivers will turn the entire target texture black
705 // when reading outside of the window.
706 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800707 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700708 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800709 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700710
Chris Craika64a2be2014-05-14 14:17:01 -0700711 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
712 bounds.left, getViewportHeight() - bounds.bottom,
713 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700714
Romain Guy54be1cd2011-06-13 19:04:27 -0700715 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800716 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700717 }
Romain Guyeb993562010-10-05 18:14:38 -0700718 }
Romain Guyf86ef572010-07-01 11:05:42 -0700719
Romain Guyd55a8612010-06-28 17:42:46 -0700720 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700721}
722
Chris Craike63f7c622013-10-17 10:30:55 -0700723bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800724 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700725 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700726
Tom Hudson984162f2014-10-10 13:38:16 -0400727 writableSnapshot()->region = &writableSnapshot()->layer->region;
728 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
729 writableSnapshot()->fbo = layer->getFbo();
730 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
731 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
732 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800733 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700734
Romain Guy2b7028e2012-09-19 17:25:38 -0700735 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700736 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700737 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700738 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700739 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700740
741 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700742 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700743 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700744 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700745 }
746
747 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Chris Craikf27133d2015-02-19 09:51:53 -0800748 layer->getTextureId(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700749
henry.uh_chen33f5a592014-07-02 19:36:56 +0800750 // Expand the startTiling region by 1
751 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700752
753 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800754 mRenderState.scissor().setEnabled(true);
755 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700756 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700757 glClear(GL_COLOR_BUFFER_BIT);
758
759 dirtyClip();
760
761 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700762 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700763 return true;
764}
765
Romain Guy1c740bc2010-09-13 18:00:09 -0700766/**
767 * Read the documentation of createLayer() before doing anything in this method.
768 */
Chris Craik14e51302013-12-30 15:32:54 -0800769void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
770 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000771 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700772 return;
773 }
774
Chris Craik14e51302013-12-30 15:32:54 -0800775 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800776 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800777 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700778
Chris Craik39a908c2013-06-13 14:39:01 -0700779 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400780 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800781 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800782 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700783
Romain Guyeb993562010-10-05 18:14:38 -0700784 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700785 endTiling();
786
Romain Guye0aa84b2012-04-03 19:30:26 -0700787 // Detach the texture from the FBO
788 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800789
790 layer->removeFbo(false);
791
Romain Guyeb993562010-10-05 18:14:38 -0700792 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700793 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700794 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700795
Chris Craikd6b65f62014-01-01 14:45:21 -0800796 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700797 }
798
Romain Guy9ace8f52011-07-07 20:50:11 -0700799 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500800 SkPaint layerPaint;
801 layerPaint.setAlpha(layer->getAlpha());
802 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
803 layerPaint.setColorFilter(layer->getColorFilter());
804
805 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700806 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700807 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700808 }
809
Chris Craik96a5c4c2015-01-27 15:46:35 -0800810 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700811
Chris Craik44eb2c02015-01-29 09:45:09 -0800812 mCaches.textureState().activateTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700813
Romain Guy5b3b3522010-10-27 18:57:51 -0700814 // When the layer is stored in an FBO, we can save a bit of fillrate by
815 // drawing only the dirty region
816 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800817 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700818 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700819 } else if (!rect.isEmpty()) {
820 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530821
822 save(0);
823 // the layer contains screen buffer content that shouldn't be alpha modulated
824 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400825 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700826 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530827 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700828 }
Romain Guy8b55f372010-08-18 17:10:07 -0700829
Romain Guy746b7402010-10-26 16:27:31 -0700830 dirtyClip();
831
Romain Guyeb993562010-10-05 18:14:38 -0700832 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800833 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700834 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800835 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800836 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700837 }
838}
839
Romain Guyaa6c24c2011-04-28 18:40:04 -0700840void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craik82840732015-04-03 09:37:49 -0700841 bool snap = !layer->getForceFilter()
Chris Craik26bf3422015-02-26 16:28:17 -0800842 && layer->getWidth() == (uint32_t) rect.getWidth()
Chris Craik82840732015-04-03 09:37:49 -0700843 && layer->getHeight() == (uint32_t) rect.getHeight();
844 Glop glop;
845 GlopBuilder(mRenderState, mCaches, &glop)
846 .setMeshTexturedUvQuad(nullptr, Rect(0, 1, 1, 0)) // TODO: simplify with VBO
847 .setFillTextureLayer(*layer, getLayerAlpha(layer))
848 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
849 .setModelViewMapUnitToRectOptionalSnap(snap, rect)
850 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
851 .build();
852 renderGlop(glop);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700853}
854
Romain Guy5b3b3522010-10-27 18:57:51 -0700855void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700856 if (layer->isTextureLayer()) {
857 EVENT_LOGD("composeTextureLayerRect");
Chris Craik62d307c2014-07-29 10:35:13 -0700858 drawTextureLayer(layer, rect);
Chris Craik62d307c2014-07-29 10:35:13 -0700859 } else {
860 EVENT_LOGD("composeHardwareLayerRect");
Chris Craik182952f2015-03-09 14:17:29 -0700861
Chris Craik82840732015-04-03 09:37:49 -0700862 Blend::ModeOrderSwap modeUsage = swap ?
863 Blend::ModeOrderSwap::Swap : Blend::ModeOrderSwap::NoSwap;
864 const Matrix4& transform = swap ? Matrix4::identity() : *currentTransform();
865 bool snap = !swap
866 && layer->getWidth() == static_cast<uint32_t>(rect.getWidth())
867 && layer->getHeight() == static_cast<uint32_t>(rect.getHeight());
868 Glop glop;
869 GlopBuilder(mRenderState, mCaches, &glop)
870 .setMeshTexturedUvQuad(nullptr, layer->texCoords)
871 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), modeUsage)
872 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
873 .setModelViewMapUnitToRectOptionalSnap(snap, rect)
874 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
875 .build();
876 renderGlop(glop);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700877 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700878}
879
Chris Craik34416ea2013-04-15 16:08:28 -0700880/**
881 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
882 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
883 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
884 * by saveLayer's restore
885 */
Chris Craik2507c342015-05-04 14:36:49 -0700886#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
887 DRAW_COMMAND; \
888 if (CC_UNLIKELY(Properties::debugOverdraw && getTargetFbo() == 0 && COND)) { \
889 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
890 DRAW_COMMAND; \
891 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
892 } \
Chris Craik34416ea2013-04-15 16:08:28 -0700893 }
894
895#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
896
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400897// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
898// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
899class LayerShader : public SkShader {
900public:
901 LayerShader(Layer* layer, const SkMatrix* localMatrix)
902 : INHERITED(localMatrix)
903 , mLayer(layer) {
904 }
905
Chris Craike84a2082014-12-22 14:28:49 -0800906 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400907 if (data) {
908 *data = static_cast<void*>(mLayer);
909 }
910 return true;
911 }
912
Chris Craike84a2082014-12-22 14:28:49 -0800913 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400914 return !mLayer->isBlend();
915 }
916
917protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000918 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400919 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
920 }
921
Chris Craike84a2082014-12-22 14:28:49 -0800922 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400923 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
924 }
925
Chris Craike84a2082014-12-22 14:28:49 -0800926 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400927 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -0800928 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400929 }
930private:
931 // Unowned.
932 Layer* mLayer;
933 typedef SkShader INHERITED;
934};
935
Romain Guy5b3b3522010-10-27 18:57:51 -0700936void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -0700937 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
938
939 if (layer->getConvexMask()) {
940 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
941
942 // clip to the area of the layer the mask can be larger
943 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
944
945 SkPaint paint;
946 paint.setAntiAlias(true);
947 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
948
Chris Craik3f0854292014-04-15 16:18:08 -0700949 // create LayerShader to map SaveLayer content into subsequent draw
950 SkMatrix shaderMatrix;
951 shaderMatrix.setTranslate(rect.left, rect.bottom);
952 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400953 LayerShader layerShader(layer, &shaderMatrix);
954 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -0700955
956 // Since the drawing primitive is defined in local drawing space,
957 // we don't need to modify the draw matrix
958 const SkPath* maskPath = layer->getConvexMask();
959 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
960
Chris Craike84a2082014-12-22 14:28:49 -0800961 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -0700962 restore();
963
964 return;
965 }
966
Romain Guy5b3b3522010-10-27 18:57:51 -0700967 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700968 layer->setRegionAsRect();
969
Chris Craik34416ea2013-04-15 16:08:28 -0700970 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -0700971
Romain Guy5b3b3522010-10-27 18:57:51 -0700972 layer->region.clear();
973 return;
974 }
975
Chris Craik62d307c2014-07-29 10:35:13 -0700976 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -0700977 // standard Region based draw
978 size_t count;
979 const android::Rect* rects;
980 Region safeRegion;
981 if (CC_LIKELY(hasRectToRectTransform())) {
982 rects = layer->region.getArray(&count);
983 } else {
984 safeRegion = Region::createTJunctionFreeRegion(layer->region);
985 rects = safeRegion.getArray(&count);
986 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700987
Chris Craik3f0854292014-04-15 16:18:08 -0700988 const float texX = 1.0f / float(layer->getWidth());
989 const float texY = 1.0f / float(layer->getHeight());
990 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700991
Chris Craik82840732015-04-03 09:37:49 -0700992 TextureVertex quadVertices[count * 4];
993 TextureVertex* mesh = &quadVertices[0];
Chris Craik3f0854292014-04-15 16:18:08 -0700994 for (size_t i = 0; i < count; i++) {
995 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -0700996
Chris Craik3f0854292014-04-15 16:18:08 -0700997 const float u1 = r->left * texX;
998 const float v1 = (height - r->top) * texY;
999 const float u2 = r->right * texX;
1000 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001001
Chris Craik3f0854292014-04-15 16:18:08 -07001002 // TODO: Reject quads outside of the clip
1003 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1004 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1005 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1006 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Chris Craik3f0854292014-04-15 16:18:08 -07001007 }
Tom Hudson040b6d82015-04-16 10:50:53 -04001008 Rect modelRect = Rect(rect.getWidth(), rect.getHeight());
Chris Craik82840732015-04-03 09:37:49 -07001009 Glop glop;
1010 GlopBuilder(mRenderState, mCaches, &glop)
1011 .setMeshTexturedIndexedQuads(&quadVertices[0], count * 6)
1012 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
1013 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
Tom Hudson040b6d82015-04-16 10:50:53 -04001014 .setModelViewOffsetRectSnap(rect.left, rect.top, modelRect)
Chris Craik82840732015-04-03 09:37:49 -07001015 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1016 .build();
1017 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy5b3b3522010-10-27 18:57:51 -07001018
Romain Guy5b3b3522010-10-27 18:57:51 -07001019#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001020 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001021#endif
1022
Chris Craik3f0854292014-04-15 16:18:08 -07001023 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001024}
1025
Romain Guy3a3133d2011-02-01 22:59:58 -08001026#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001027void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001028 size_t count;
1029 const android::Rect* rects = region.getArray(&count);
1030
1031 uint32_t colors[] = {
1032 0x7fff0000, 0x7f00ff00,
1033 0x7f0000ff, 0x7fff00ff,
1034 };
1035
1036 int offset = 0;
1037 int32_t top = rects[0].top;
1038
1039 for (size_t i = 0; i < count; i++) {
1040 if (top != rects[i].top) {
1041 offset ^= 0x2;
1042 top = rects[i].top;
1043 }
1044
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001045 SkPaint paint;
1046 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001047 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001048 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001049 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001050}
Chris Craike63f7c622013-10-17 10:30:55 -07001051#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001052
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001053void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001054 Vector<float> rects;
1055
1056 SkRegion::Iterator it(region);
1057 while (!it.done()) {
1058 const SkIRect& r = it.rect();
1059 rects.push(r.fLeft);
1060 rects.push(r.fTop);
1061 rects.push(r.fRight);
1062 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001063 it.next();
1064 }
1065
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001066 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001067}
1068
Romain Guy5b3b3522010-10-27 18:57:51 -07001069void OpenGLRenderer::dirtyLayer(const float left, const float top,
Chris Craik083e7332015-02-27 17:04:20 -08001070 const float right, const float bottom, const Matrix4& transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001071 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001072 Rect bounds(left, top, right, bottom);
1073 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001074 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001075 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001076}
1077
1078void OpenGLRenderer::dirtyLayer(const float left, const float top,
1079 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001080 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001081 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001082 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001083 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001084}
1085
1086void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001087 if (bounds.intersect(mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001088 bounds.snapToPixelBoundaries();
1089 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1090 if (!dirty.isEmpty()) {
1091 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001092 }
1093 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001094}
1095
Romain Guy54be1cd2011-06-13 19:04:27 -07001096void OpenGLRenderer::clearLayerRegions() {
Chris Craik2bb8f562015-02-17 16:42:02 -08001097 const size_t quadCount = mLayers.size();
1098 if (quadCount == 0) return;
Romain Guy54be1cd2011-06-13 19:04:27 -07001099
Tom Hudson984162f2014-10-10 13:38:16 -04001100 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001101 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001102 // Doing several glScissor/glClear here can negatively impact
1103 // GPUs with a tiler architecture, instead we draw quads with
1104 // the Clear blending mode
1105
1106 // The list contains bounds that have already been clipped
1107 // against their initial clip rect, and the current clip
1108 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001109 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001110
Chris Craik2bb8f562015-02-17 16:42:02 -08001111 Vertex mesh[quadCount * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001112 Vertex* vertex = mesh;
1113
Chris Craik2bb8f562015-02-17 16:42:02 -08001114 for (uint32_t i = 0; i < quadCount; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001115 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001116
Chris Craik51d6a3d2014-12-22 17:16:56 -08001117 Vertex::set(vertex++, bounds.left, bounds.top);
1118 Vertex::set(vertex++, bounds.right, bounds.top);
1119 Vertex::set(vertex++, bounds.left, bounds.bottom);
1120 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001121 }
Romain Guye67307c2013-02-11 18:01:20 -08001122 // We must clear the list of dirty rects before we
Chris Craik82840732015-04-03 09:37:49 -07001123 // call clearLayerRegions() in renderGlop to prevent
1124 // stencil setup from doing the same thing again
Romain Guye67307c2013-02-11 18:01:20 -08001125 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001126
Chris Craik82840732015-04-03 09:37:49 -07001127 Glop glop;
1128 GlopBuilder(mRenderState, mCaches, &glop)
1129 .setMeshIndexedQuads(&mesh[0], quadCount)
1130 .setFillClear()
1131 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
1132 .setModelViewOffsetRect(0, 0, Rect(currentSnapshot()->getClipRect()))
1133 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1134 .build();
1135 renderGlop(glop, false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001136
Chris Craik65fe5ee2015-01-26 18:06:29 -08001137 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001138 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001139 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001140 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001141}
1142
Romain Guybd6b79b2010-06-26 00:13:53 -07001143///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001144// State Deferral
1145///////////////////////////////////////////////////////////////////////////////
1146
Chris Craikff785832013-03-08 13:12:16 -08001147bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001148 const Rect& currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001149 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001150
Chris Craikff785832013-03-08 13:12:16 -08001151 if (stateDeferFlags & kStateDeferFlag_Draw) {
1152 // state has bounds initialized in local coordinates
1153 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001154 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001155 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001156 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1157 // is used, it should more closely duplicate the quickReject logic (in how it uses
1158 // snapToPixelBoundaries)
1159
Rob Tsuk487a92c2015-01-06 13:22:54 -08001160 if (!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001161 // quick rejected
1162 return true;
1163 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001164
Chris Craika02c4ed2013-06-14 13:43:58 -07001165 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001166 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001167 int& flags = state.mClipSideFlags;
1168 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001169 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1170 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1171 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1172 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001173 }
1174 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001175 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001176 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1177 // overdraw avoidance (since we don't know what it overlaps)
1178 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001179 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001180 }
1181 }
1182
Chris Craik527a3aa2013-03-04 10:19:31 -08001183 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1184 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001185 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001186 }
1187
Chris Craik8df5ffa2015-04-28 17:47:20 -07001188 // Transform and alpha always deferred, since they are used by state operations
Chris Craik7273daa2013-03-28 11:25:24 -07001189 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001190 state.mMatrix.load(*currentMatrix);
Chris Craikd6b65f62014-01-01 14:45:21 -08001191 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001192
1193 // always store/restore, since it's just a pointer
1194 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001195 return false;
1196}
1197
Chris Craik527a3aa2013-03-04 10:19:31 -08001198void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001199 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001200 writableSnapshot()->alpha = state.mAlpha;
Tom Hudson984162f2014-10-10 13:38:16 -04001201 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001202
Chris Craik527a3aa2013-03-04 10:19:31 -08001203 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001204 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001205 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001206 dirtyClip();
1207 }
Chris Craikc3566d02013-02-04 16:16:33 -08001208}
1209
Chris Craik28ce94a2013-05-31 11:38:03 -07001210/**
1211 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1212 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1213 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1214 *
1215 * This method should be called when restoreDisplayState() won't be restoring the clip
1216 */
1217void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001218 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001219 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001220 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001221 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001222 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001223 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001224 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1225 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001226}
1227
Chris Craikc3566d02013-02-04 16:16:33 -08001228///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001229// Clipping
1230///////////////////////////////////////////////////////////////////////////////
1231
Romain Guybb9524b2010-06-22 18:56:38 -07001232void OpenGLRenderer::setScissorFromClip() {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001233 Rect clip(mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001234 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001235
Chris Craik65fe5ee2015-01-26 18:06:29 -08001236 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001237 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001238 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001239 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001240}
1241
Romain Guy8ce00302013-01-15 18:51:42 -08001242void OpenGLRenderer::ensureStencilBuffer() {
1243 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1244 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1245 // just hope we have one when hasLayer() returns false.
1246 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001247 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001248 }
1249}
1250
1251void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1252 // The layer's FBO is already bound when we reach this stage
1253 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001254 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1255 // is attached after we initiated tiling. We must turn it off,
1256 // attach the new render buffer then turn tiling back on
1257 endTiling();
1258
Romain Guy8d4aeb72013-02-12 16:08:55 -08001259 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Chris Craike1450132015-04-28 17:55:50 -07001260 Stencil::getLayerStencilFormat(),
Chris Craik117bdbc2015-02-05 10:12:38 -08001261 layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001262 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001263
Romain Guyf735c8e2013-01-31 17:45:55 -08001264 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001265 }
1266}
1267
Rob Tsuk487a92c2015-01-06 13:22:54 -08001268static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1269 float x, float y) {
1270 Vertex v;
1271 v.x = x;
1272 v.y = y;
1273 transform.mapPoint(v.x, v.y);
1274 rectangleVertices.push_back(v);
1275}
1276
1277static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1278 Vertex v;
1279 v.x = x;
1280 v.y = y;
1281 rectangleVertices.push_back(v);
1282}
1283
1284void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
Chris Craik2bb8f562015-02-17 16:42:02 -08001285 int quadCount = rectangleList.getTransformedRectanglesCount();
1286 std::vector<Vertex> rectangleVertices(quadCount * 4);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001287 Rect scissorBox = rectangleList.calculateBounds();
1288 scissorBox.snapToPixelBoundaries();
Chris Craik2bb8f562015-02-17 16:42:02 -08001289 for (int i = 0; i < quadCount; ++i) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001290 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1291 const Matrix4& transform = tr.getTransform();
1292 Rect bounds = tr.getBounds();
1293 if (transform.rectToRect()) {
1294 transform.mapRect(bounds);
1295 if (!bounds.intersect(scissorBox)) {
1296 bounds.setEmpty();
1297 } else {
1298 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1299 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1300 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1301 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1302 }
1303 } else {
1304 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1305 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1306 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1307 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1308 }
1309 }
1310
Chris Craik65fe5ee2015-01-26 18:06:29 -08001311 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001312 scissorBox.getWidth(), scissorBox.getHeight());
1313
Chris Craik82840732015-04-03 09:37:49 -07001314 Glop glop;
1315 Vertex* vertices = &rectangleVertices[0];
1316 GlopBuilder(mRenderState, mCaches, &glop)
1317 .setMeshIndexedQuads(vertices, rectangleVertices.size() / 4)
1318 .setFillBlack()
1319 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
1320 .setModelViewOffsetRect(0, 0, scissorBox)
1321 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1322 .build();
1323 renderGlop(glop);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001324}
1325
Romain Guy8ce00302013-01-15 18:51:42 -08001326void OpenGLRenderer::setStencilFromClip() {
Chris Craik2507c342015-05-04 14:36:49 -07001327 if (!Properties::debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001328 if (!currentSnapshot()->clipIsSimple()) {
1329 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001330 EVENT_LOGD("setStencilFromClip - enabling");
1331
Romain Guy8ce00302013-01-15 18:51:42 -08001332 // NOTE: The order here is important, we must set dirtyClip to false
1333 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001334 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001335
1336 ensureStencilBuffer();
1337
Rob Tsuk487a92c2015-01-06 13:22:54 -08001338 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001339
Rob Tsuk487a92c2015-01-06 13:22:54 -08001340 bool isRectangleList = clipArea.isRectangleList();
1341 if (isRectangleList) {
1342 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1343 } else {
1344 incrementThreshold = 0;
1345 }
1346
Chris Craik96a5c4c2015-01-27 15:46:35 -08001347 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001348
1349 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001350 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001351 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001352 if (resetScissor) {
1353 // The scissor was not set so we now need to update it
1354 setScissorFromClip();
1355 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001356
Chris Craik96a5c4c2015-01-27 15:46:35 -08001357 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001358
1359 // stash and disable the outline clip state, since stencil doesn't account for outline
1360 bool storedSkipOutlineClip = mSkipOutlineClip;
1361 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001362
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001363 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001364 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001365 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1366
Rob Tsuk487a92c2015-01-06 13:22:54 -08001367 if (isRectangleList) {
1368 drawRectangleList(clipArea.getRectangleList());
1369 } else {
1370 // NOTE: We could use the region contour path to generate a smaller mesh
1371 // Since we are using the stencil we could use the red book path
1372 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001373
Rob Tsuk487a92c2015-01-06 13:22:54 -08001374 // The last parameter is important: we are not drawing in the color buffer
1375 // so we don't want to dirty the current layer, if any
1376 drawRegionRects(clipArea.getClipRegion(), paint, false);
1377 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001378 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001379 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001380
Chris Craik96a5c4c2015-01-27 15:46:35 -08001381 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001382
1383 // Draw the region used to generate the stencil if the appropriate debug
1384 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001385 // TODO: Implement for rectangle list clip areas
Chris Craik2507c342015-05-04 14:36:49 -07001386 if (Properties::debugStencilClip == StencilClipDebug::ShowRegion
1387 && !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001388 paint.setColor(0x7f0000ff);
1389 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001390 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001391 }
Romain Guy8ce00302013-01-15 18:51:42 -08001392 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001393 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001394 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001395 }
1396 }
1397}
1398
Chris Craikf0a59072013-11-19 18:00:46 -08001399/**
1400 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1401 *
1402 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1403 * style, and tessellated AA ramp
1404 */
1405bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001406 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001407 bool snapOut = paint && paint->isAntiAlias();
1408
1409 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1410 float outset = paint->getStrokeWidth() * 0.5f;
1411 left -= outset;
1412 top -= outset;
1413 right += outset;
1414 bottom += outset;
1415 }
1416
Chris Craikdeeda3d2014-05-05 19:09:33 -07001417 bool clipRequired = false;
1418 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001419 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001420 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001421 return true;
1422 }
1423
Chris Craikf23b25a2014-06-26 15:46:20 -07001424 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001425 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001426 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001427 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001428}
1429
Romain Guy8ce00302013-01-15 18:51:42 -08001430void OpenGLRenderer::debugClip() {
1431#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001432 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001433 SkPaint paint;
1434 paint.setColor(0x7f00ff00);
1435 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1436
Romain Guy8ce00302013-01-15 18:51:42 -08001437 }
1438#endif
1439}
1440
Chris Craik6b109c72015-02-27 10:55:28 -08001441void OpenGLRenderer::renderGlop(const Glop& glop, bool clearLayer) {
1442 // TODO: It would be best if we could do this before quickRejectSetupScissor()
1443 // changes the scissor test state
1444 if (clearLayer) clearLayerRegions();
1445
Chris Craik117bdbc2015-02-05 10:12:38 -08001446 if (mState.getDirtyClip()) {
1447 if (mRenderState.scissor().isEnabled()) {
1448 setScissorFromClip();
1449 }
1450
1451 setStencilFromClip();
1452 }
1453 mRenderState.render(glop);
Chris Craik2ab95d72015-02-06 15:25:51 -08001454 if (!mRenderState.stencil().isWriteEnabled()) {
1455 // TODO: specify more clearly when a draw should dirty the layer.
1456 // is writing to the stencil the only time we should ignore this?
1457 dirtyLayer(glop.bounds.left, glop.bounds.top, glop.bounds.right, glop.bounds.bottom);
Chris Craik0519c8102015-02-11 13:17:06 -08001458 mDirty = true;
Chris Craik2ab95d72015-02-06 15:25:51 -08001459 }
Chris Craik117bdbc2015-02-05 10:12:38 -08001460}
1461
Romain Guyf6a11b82010-06-23 17:47:49 -07001462///////////////////////////////////////////////////////////////////////////////
1463// Drawing
1464///////////////////////////////////////////////////////////////////////////////
1465
Tom Hudson107843d2014-09-08 11:26:26 -04001466void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001467 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1468 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001469 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001470 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001471 renderNode->computeOrdering();
Chris Craik2507c342015-05-04 14:36:49 -07001472 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001473 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001474 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001475 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001476 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001477 }
1478
Chris Craikef8d6f22014-12-17 11:10:28 -08001479 // Don't avoid overdraw when visualizing, since that makes it harder to
1480 // debug where it's coming from, and when the problem occurs.
Chris Craik2507c342015-05-04 14:36:49 -07001481 bool avoidOverdraw = !Properties::debugOverdraw;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001482 DeferredDisplayList deferredList(mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001483 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001484 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001485
1486 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001487 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001488
Tom Hudson107843d2014-09-08 11:26:26 -04001489 deferredList.flush(*this, dirty);
1490 } else {
1491 // Even if there is no drawing command(Ex: invisible),
1492 // it still needs startFrame to clear buffer and start tiling.
1493 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001494 }
1495}
1496
Romain Guy03c00b52013-06-20 18:30:28 -07001497/**
1498 * Important note: this method is intended to draw batches of bitmaps and
1499 * will not set the scissor enable or dirty the current layer, if any.
1500 * The caller is responsible for properly dirtying the current layer.
1501 */
Tom Hudson107843d2014-09-08 11:26:26 -04001502void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001503 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1504 const Rect& bounds, const SkPaint* paint) {
Romain Guy55b6f952013-06-27 15:27:09 -07001505 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001506 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001507
Chris Craik527a3aa2013-03-04 10:19:31 -08001508 const AutoTexture autoCleanup(texture);
1509
Chris Craik82840732015-04-03 09:37:49 -07001510 // TODO: remove layer dirty in multi-draw callers
1511 // TODO: snap doesn't need to touch transform, only texture filter.
1512 bool snap = pureTranslate;
Chris Craika6b52192015-02-27 17:43:02 -08001513 const float x = floorf(bounds.left + 0.5f);
1514 const float y = floorf(bounds.top + 0.5f);
Chris Craik82840732015-04-03 09:37:49 -07001515 int textureFillFlags = static_cast<int>((bitmap->colorType() == kAlpha_8_SkColorType)
1516 ? TextureFillFlags::kIsAlphaMaskTexture : TextureFillFlags::kNone);
1517 Glop glop;
1518 GlopBuilder(mRenderState, mCaches, &glop)
1519 .setMeshTexturedMesh(vertices, bitmapCount * 6)
1520 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1521 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
1522 .setModelViewOffsetRectOptionalSnap(snap, x, y, Rect(0, 0, bounds.getWidth(), bounds.getHeight()))
1523 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1524 .build();
1525 renderGlop(glop);
Chris Craik527a3aa2013-03-04 10:19:31 -08001526}
1527
Tom Hudson107843d2014-09-08 11:26:26 -04001528void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07001529 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04001530 return;
Romain Guy6926c722010-07-12 20:20:03 -07001531 }
1532
Chris Craik44eb2c02015-01-29 09:45:09 -08001533 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07001534 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001535 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001536 const AutoTexture autoCleanup(texture);
1537
Chris Craik82840732015-04-03 09:37:49 -07001538 int textureFillFlags = static_cast<int>((bitmap->colorType() == kAlpha_8_SkColorType)
1539 ? TextureFillFlags::kIsAlphaMaskTexture : TextureFillFlags::kNone);
1540 Glop glop;
1541 GlopBuilder(mRenderState, mCaches, &glop)
1542 .setMeshTexturedUnitQuad(texture->uvMapper)
1543 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1544 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
1545 .setModelViewMapUnitToRectSnap(Rect(0, 0, texture->width, texture->height))
1546 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1547 .build();
1548 renderGlop(glop);
Romain Guyce0537b2010-06-29 21:05:21 -07001549}
1550
Tom Hudson107843d2014-09-08 11:26:26 -04001551void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08001552 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001553 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04001554 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08001555 }
1556
Romain Guyb18d2d02011-02-10 15:52:54 -08001557 float left = FLT_MAX;
1558 float top = FLT_MAX;
1559 float right = FLT_MIN;
1560 float bottom = FLT_MIN;
1561
Chris Craikef250742015-02-25 17:16:16 -08001562 const uint32_t elementCount = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001563
Chris Craikef250742015-02-25 17:16:16 -08001564 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[elementCount]);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001565 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08001566
Chris Craik51d6a3d2014-12-22 17:16:56 -08001567 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08001568 if (!colors) {
1569 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001570 tempColors.reset(new int[colorsCount]);
1571 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
1572 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08001573 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001574
John Reckebd52612014-12-10 16:47:36 -08001575 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07001576 const UvMapper& mapper(getMapper(texture));
1577
Romain Guy5a7b4662011-01-20 19:09:30 -08001578 for (int32_t y = 0; y < meshHeight; y++) {
1579 for (int32_t x = 0; x < meshWidth; x++) {
1580 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1581
1582 float u1 = float(x) / meshWidth;
1583 float u2 = float(x + 1) / meshWidth;
1584 float v1 = float(y) / meshHeight;
1585 float v2 = float(y + 1) / meshHeight;
1586
Romain Guy3b748a42013-04-17 18:54:38 -07001587 mapper.map(u1, v1, u2, v2);
1588
Romain Guy5a7b4662011-01-20 19:09:30 -08001589 int ax = i + (meshWidth + 1) * 2;
1590 int ay = ax + 1;
1591 int bx = i;
1592 int by = bx + 1;
1593 int cx = i + 2;
1594 int cy = cx + 1;
1595 int dx = i + (meshWidth + 1) * 2 + 2;
1596 int dy = dx + 1;
1597
Romain Guyff316ec2013-02-13 18:39:43 -08001598 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1599 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
1600 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08001601
Romain Guyff316ec2013-02-13 18:39:43 -08001602 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1603 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
1604 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08001605
Romain Guya92bb4d2012-10-16 11:08:44 -07001606 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1607 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1608 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1609 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001610 }
1611 }
1612
Chris Craikf0a59072013-11-19 18:00:46 -08001613 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001614 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07001615 }
1616
Romain Guyff316ec2013-02-13 18:39:43 -08001617 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07001618 texture = mCaches.textureCache.get(bitmap);
1619 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04001620 return;
Romain Guy3b748a42013-04-17 18:54:38 -07001621 }
Romain Guyff316ec2013-02-13 18:39:43 -08001622 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001623 const AutoTexture autoCleanup(texture);
1624
Chris Craik82840732015-04-03 09:37:49 -07001625 /*
1626 * TODO: handle alpha_8 textures correctly by applying paint color, but *not*
1627 * shader in that case to mimic the behavior in SkiaCanvas::drawBitmapMesh.
1628 */
1629 Glop glop;
1630 GlopBuilder(mRenderState, mCaches, &glop)
1631 .setMeshColoredTexturedMesh(mesh.get(), elementCount)
1632 .setFillTexturePaint(*texture, static_cast<int>(TextureFillFlags::kNone), paint, currentSnapshot()->alpha)
1633 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
1634 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
1635 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1636 .build();
1637 renderGlop(glop);
Romain Guy5a7b4662011-01-20 19:09:30 -08001638}
1639
Chris Craik14100ac2015-02-24 13:46:29 -08001640void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, Rect src, Rect dst, const SkPaint* paint) {
1641 if (quickRejectSetupScissor(dst)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001642 return;
Romain Guy6926c722010-07-12 20:20:03 -07001643 }
1644
Romain Guy3b748a42013-04-17 18:54:38 -07001645 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001646 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001647 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001648
Chris Craik82840732015-04-03 09:37:49 -07001649 Rect uv(fmax(0.0f, src.left / texture->width),
1650 fmax(0.0f, src.top / texture->height),
1651 fmin(1.0f, src.right / texture->width),
1652 fmin(1.0f, src.bottom / texture->height));
Chris Craik14100ac2015-02-24 13:46:29 -08001653
Chris Craik82840732015-04-03 09:37:49 -07001654 int textureFillFlags = static_cast<int>((bitmap->colorType() == kAlpha_8_SkColorType)
1655 ? TextureFillFlags::kIsAlphaMaskTexture : TextureFillFlags::kNone);
1656 Glop glop;
1657 GlopBuilder(mRenderState, mCaches, &glop)
1658 .setMeshTexturedUvQuad(texture->uvMapper, uv)
1659 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1660 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
1661 .setModelViewMapUnitToRectSnap(dst)
1662 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1663 .build();
1664 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07001665}
1666
Tom Hudson107843d2014-09-08 11:26:26 -04001667void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08001668 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
1669 const SkPaint* paint) {
Chris Craika6b52192015-02-27 17:43:02 -08001670 if (!mesh || !mesh->verticesCount || quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001671 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07001672 }
1673
Chris Craika6b52192015-02-27 17:43:02 -08001674 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
1675 if (!texture) return;
Chris Craik0556d902015-03-03 09:54:14 -08001676
Chris Craik82840732015-04-03 09:37:49 -07001677 // 9 patches are built for stretching - always filter
1678 int textureFillFlags = static_cast<int>(TextureFillFlags::kForceFilter);
1679 if (bitmap->colorType() == kAlpha_8_SkColorType) {
1680 textureFillFlags |= TextureFillFlags::kIsAlphaMaskTexture;
Chris Craik0556d902015-03-03 09:54:14 -08001681 }
Chris Craik82840732015-04-03 09:37:49 -07001682 Glop glop;
1683 GlopBuilder(mRenderState, mCaches, &glop)
1684 .setMeshPatchQuads(*mesh)
1685 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1686 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
1687 .setModelViewOffsetRectSnap(left, top, Rect(0, 0, right - left, bottom - top)) // TODO: get minimal bounds from patch
1688 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1689 .build();
1690 renderGlop(glop);
Romain Guyf7f93552010-07-08 19:17:03 -07001691}
1692
Romain Guy03c00b52013-06-20 18:30:28 -07001693/**
1694 * Important note: this method is intended to draw batches of 9-patch objects and
1695 * will not set the scissor enable or dirty the current layer, if any.
1696 * The caller is responsible for properly dirtying the current layer.
1697 */
Tom Hudson107843d2014-09-08 11:26:26 -04001698void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craika6b52192015-02-27 17:43:02 -08001699 TextureVertex* vertices, uint32_t elementCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001700 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07001701 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001702 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07001703 const AutoTexture autoCleanup(texture);
1704
Chris Craik82840732015-04-03 09:37:49 -07001705 // TODO: get correct bounds from caller
1706 // 9 patches are built for stretching - always filter
1707 int textureFillFlags = static_cast<int>(TextureFillFlags::kForceFilter);
1708 if (bitmap->colorType() == kAlpha_8_SkColorType) {
1709 textureFillFlags |= TextureFillFlags::kIsAlphaMaskTexture;
Chris Craika6b52192015-02-27 17:43:02 -08001710 }
Chris Craik82840732015-04-03 09:37:49 -07001711 Glop glop;
1712 GlopBuilder(mRenderState, mCaches, &glop)
1713 .setMeshTexturedIndexedQuads(vertices, elementCount)
1714 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1715 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
1716 .setModelViewOffsetRect(0, 0, Rect(0, 0, 0, 0))
1717 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1718 .build();
1719 renderGlop(glop);
Romain Guy03c00b52013-06-20 18:30:28 -07001720}
1721
Tom Hudson107843d2014-09-08 11:26:26 -04001722void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07001723 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001724 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07001725 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08001726 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04001727 return;
Chris Craik65cd6122012-12-10 17:56:27 -08001728 }
1729
Chris Craik82840732015-04-03 09:37:49 -07001730 bool fudgeOffset = displayFlags & kVertexBuffer_Offset;
1731 bool shadowInterp = displayFlags & kVertexBuffer_ShadowInterp;
1732 Glop glop;
1733 GlopBuilder(mRenderState, mCaches, &glop)
1734 .setMeshVertexBuffer(vertexBuffer, shadowInterp)
1735 .setFillPaint(*paint, currentSnapshot()->alpha)
1736 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), fudgeOffset)
1737 .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds())
1738 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1739 .build();
1740 renderGlop(glop);
Chet Haase858aa932011-05-12 09:06:00 -07001741}
1742
1743/**
Chris Craik65cd6122012-12-10 17:56:27 -08001744 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
1745 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
1746 * screen space in all directions. However, instead of using a fragment shader to compute the
1747 * translucency of the color from its position, we simply use a varying parameter to define how far
1748 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
1749 *
1750 * Doesn't yet support joins, caps, or path effects.
1751 */
Tom Hudson107843d2014-09-08 11:26:26 -04001752void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08001753 VertexBuffer vertexBuffer;
1754 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08001755 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04001756 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08001757}
1758
1759/**
1760 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
1761 * and additional geometry for defining an alpha slope perimeter.
1762 *
1763 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
1764 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
1765 * in-shader alpha region, but found it to be taxing on some GPUs.
1766 *
1767 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
1768 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07001769 */
Tom Hudson107843d2014-09-08 11:26:26 -04001770void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001771 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07001772
Chris Craik65cd6122012-12-10 17:56:27 -08001773 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07001774
Chris Craik65cd6122012-12-10 17:56:27 -08001775 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001776 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
1777 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08001778
Chris Craik05f3d6e2014-06-02 16:27:04 -07001779 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001780 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08001781 }
1782
Chris Craikbf759452014-08-11 16:00:44 -07001783 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001784 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07001785}
1786
Tom Hudson107843d2014-09-08 11:26:26 -04001787void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001788 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001789
Chris Craik6d29c8d2013-05-08 18:35:44 -07001790 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07001791
Chris Craik6d29c8d2013-05-08 18:35:44 -07001792 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001793 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08001794
Chris Craik05f3d6e2014-06-02 16:27:04 -07001795 const Rect& bounds = buffer.getBounds();
1796 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001797 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001798 }
1799
Chris Craikbf759452014-08-11 16:00:44 -07001800 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001801 drawVertexBuffer(buffer, paint, displayFlags);
1802
1803 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07001804}
1805
Tom Hudson107843d2014-09-08 11:26:26 -04001806void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001807 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04001808 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001809
Rob Tsuk487a92c2015-01-06 13:22:54 -08001810 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07001811 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001812
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001813 SkPaint paint;
1814 paint.setColor(color);
1815 paint.setXfermodeMode(mode);
1816
1817 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07001818
Tom Hudson107843d2014-09-08 11:26:26 -04001819 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07001820}
Romain Guy9d5316e2010-06-24 19:30:36 -07001821
Chris Craik30036092015-02-12 10:41:39 -08001822void OpenGLRenderer::drawShape(float left, float top, PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08001823 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04001824 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001825 const AutoTexture autoCleanup(texture);
1826
1827 const float x = left + texture->left - texture->offset;
1828 const float y = top + texture->top - texture->offset;
1829
1830 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07001831
Tom Hudson107843d2014-09-08 11:26:26 -04001832 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08001833}
1834
Tom Hudson107843d2014-09-08 11:26:26 -04001835void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001836 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001837 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001838 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001839 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001840 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001841 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001842
Chris Craike84a2082014-12-22 14:28:49 -08001843 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001844 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001845 PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07001846 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001847 drawShape(left, top, texture, p);
1848 } else {
1849 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
1850 *currentTransform(), *p, right - left, bottom - top, rx, ry);
1851 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001852 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001853}
1854
Tom Hudson107843d2014-09-08 11:26:26 -04001855void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001856 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001857 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001858 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001859 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001860 }
Chris Craike84a2082014-12-22 14:28:49 -08001861 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001862 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001863 PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001864 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07001865 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04001866 SkPath path;
1867 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1868 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
1869 } else {
1870 path.addCircle(x, y, radius);
1871 }
1872 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07001873 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001874}
Romain Guy01d58e42011-01-19 21:54:02 -08001875
Tom Hudson107843d2014-09-08 11:26:26 -04001876void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001877 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001878 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001879 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001880 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001881 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001882 }
Romain Guy01d58e42011-01-19 21:54:02 -08001883
Chris Craike84a2082014-12-22 14:28:49 -08001884 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001885 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001886 PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001887 drawShape(left, top, texture, p);
1888 } else {
1889 SkPath path;
1890 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1891 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1892 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1893 }
1894 path.addOval(rect);
1895 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001896 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001897}
1898
Tom Hudson107843d2014-09-08 11:26:26 -04001899void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001900 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001901 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001902 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001903 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001904 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08001905 }
1906
Chris Craik780c1282012-10-04 14:10:49 -07001907 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08001908 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001909 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001910 PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07001911 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001912 drawShape(left, top, texture, p);
1913 return;
Chris Craik780c1282012-10-04 14:10:49 -07001914 }
Chris Craik780c1282012-10-04 14:10:49 -07001915 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1916 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1917 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1918 }
1919
1920 SkPath path;
1921 if (useCenter) {
1922 path.moveTo(rect.centerX(), rect.centerY());
1923 }
1924 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
1925 if (useCenter) {
1926 path.close();
1927 }
Tom Hudson107843d2014-09-08 11:26:26 -04001928 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08001929}
1930
Romain Guycf8675e2012-10-02 12:32:25 -07001931// See SkPaintDefaults.h
1932#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
1933
Tom Hudson107843d2014-09-08 11:26:26 -04001934void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001935 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001936 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001937 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001938 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001939 return;
Romain Guy6926c722010-07-12 20:20:03 -07001940 }
1941
Chris Craik710f46d2012-09-17 17:25:49 -07001942 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07001943 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08001944 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07001945 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001946 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001947 PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07001948 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001949 drawShape(left, top, texture, p);
1950 } else {
1951 SkPath path;
1952 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1953 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1954 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1955 }
1956 path.addRect(rect);
1957 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07001958 }
Chet Haase858aa932011-05-12 09:06:00 -07001959 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04001960 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
1961 SkPath path;
1962 path.addRect(left, top, right, bottom);
1963 drawConvexPath(path, p);
1964 } else {
1965 drawColorRect(left, top, right, bottom, p);
1966
1967 mDirty = true;
1968 }
Chet Haase858aa932011-05-12 09:06:00 -07001969 }
Romain Guyc7d53492010-06-25 13:41:57 -07001970}
Romain Guy9d5316e2010-06-24 19:30:36 -07001971
Chris Craikd218a922014-01-02 17:13:34 -08001972void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
1973 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001974 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001975 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07001976
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04001977 TextShadow textShadow;
1978 if (!getTextShadow(paint, &textShadow)) {
1979 LOG_ALWAYS_FATAL("failed to query shadow attributes");
1980 }
1981
Raph Levien416a8472012-07-19 22:48:17 -07001982 // NOTE: The drop shadow will not perform gamma correction
1983 // if shader-based correction is enabled
1984 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Chris Craik2bb8f562015-02-17 16:42:02 -08001985 ShadowTexture* texture = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04001986 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07001987 // If the drop shadow exceeds the max texture size or couldn't be
1988 // allocated, skip drawing
Chris Craik2bb8f562015-02-17 16:42:02 -08001989 if (!texture) return;
1990 const AutoTexture autoCleanup(texture);
Raph Levien416a8472012-07-19 22:48:17 -07001991
Chris Craik2bb8f562015-02-17 16:42:02 -08001992 const float sx = x - texture->left + textShadow.dx;
1993 const float sy = y - texture->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07001994
Chris Craik82840732015-04-03 09:37:49 -07001995 Glop glop;
1996 GlopBuilder(mRenderState, mCaches, &glop)
1997 .setMeshTexturedUnitQuad(nullptr)
1998 .setFillShadowTexturePaint(*texture, textShadow.color, *paint, currentSnapshot()->alpha)
1999 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
2000 .setModelViewMapUnitToRect(Rect(sx, sy, sx + texture->width, sy + texture->height))
2001 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2002 .build();
2003 renderGlop(glop);
Raph Levien416a8472012-07-19 22:48:17 -07002004}
2005
Romain Guy768bffc2013-02-27 13:50:45 -08002006bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002007 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05002008 return MathUtils::isZero(alpha)
2009 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002010}
2011
Tom Hudson107843d2014-09-08 11:26:26 -04002012void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002013 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002014 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002015 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002016 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002017
Romain Guy671d6cf2012-01-18 12:39:17 -08002018 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002019 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002020 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002021 }
2022
Chris Craik65fe5ee2015-01-26 18:06:29 -08002023 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002024
Romain Guy671d6cf2012-01-18 12:39:17 -08002025 float x = 0.0f;
2026 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002027 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002028 if (pureTranslate) {
Chris Craika6b52192015-02-27 17:43:02 -08002029 x = floorf(x + currentTransform()->getTranslateX() + 0.5f);
2030 y = floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002031 }
2032
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002033 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002034 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002035
2036 int alpha;
2037 SkXfermode::Mode mode;
2038 getAlphaAndMode(paint, &alpha, &mode);
2039
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002040 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002041 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002042 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002043 }
2044
Romain Guy671d6cf2012-01-18 12:39:17 -08002045 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002046 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002047 if (pureTranslate && !linearFilter) {
2048 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2049 }
Romain Guy257ae352013-03-20 16:31:12 -07002050 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002051
Rob Tsuk487a92c2015-01-06 13:22:54 -08002052 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002053 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2054
Chris Craik82840732015-04-03 09:37:49 -07002055 TextDrawFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002056 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craik083e7332015-02-27 17:04:20 -08002057 positions, hasLayer() ? &bounds : nullptr, &functor)) {
2058 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2059 mDirty = true;
Romain Guy671d6cf2012-01-18 12:39:17 -08002060 }
Chet Haase48659092012-05-31 15:21:51 -07002061
Romain Guyeb9a5362012-01-17 17:39:26 -08002062}
2063
Chris Craik59744b72014-07-01 17:56:52 -07002064bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002065 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002066 outMatrix->setIdentity();
2067 return false;
2068 } else if (CC_UNLIKELY(transform.isPerspective())) {
2069 outMatrix->setIdentity();
2070 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002071 }
Chris Craik59744b72014-07-01 17:56:52 -07002072
2073 /**
Chris Craika736cd92014-08-04 13:18:38 -07002074 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2075 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002076 */
2077 float sx, sy;
2078 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002079 outMatrix->setScale(
2080 roundf(fmaxf(1.0f, sx)),
2081 roundf(fmaxf(1.0f, sy)));
2082 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002083}
2084
Tom Hudson984162f2014-10-10 13:38:16 -04002085int OpenGLRenderer::getSaveCount() const {
2086 return mState.getSaveCount();
2087}
2088
2089int OpenGLRenderer::save(int flags) {
2090 return mState.save(flags);
2091}
2092
2093void OpenGLRenderer::restore() {
Chris Craike2bb3802015-03-13 15:07:52 -07002094 mState.restore();
Tom Hudson984162f2014-10-10 13:38:16 -04002095}
2096
2097void OpenGLRenderer::restoreToCount(int saveCount) {
Chris Craike2bb3802015-03-13 15:07:52 -07002098 mState.restoreToCount(saveCount);
Tom Hudson984162f2014-10-10 13:38:16 -04002099}
2100
2101void OpenGLRenderer::translate(float dx, float dy, float dz) {
Chris Craike2bb3802015-03-13 15:07:52 -07002102 mState.translate(dx, dy, dz);
Tom Hudson984162f2014-10-10 13:38:16 -04002103}
2104
2105void OpenGLRenderer::rotate(float degrees) {
Chris Craike2bb3802015-03-13 15:07:52 -07002106 mState.rotate(degrees);
Tom Hudson984162f2014-10-10 13:38:16 -04002107}
2108
2109void OpenGLRenderer::scale(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002110 mState.scale(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002111}
2112
2113void OpenGLRenderer::skew(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002114 mState.skew(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002115}
2116
2117void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2118 mState.setMatrix(matrix);
2119}
2120
2121void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2122 mState.concatMatrix(matrix);
2123}
2124
2125bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2126 return mState.clipRect(left, top, right, bottom, op);
2127}
2128
2129bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2130 return mState.clipPath(path, op);
2131}
2132
2133bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2134 return mState.clipRegion(region, op);
2135}
2136
2137void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2138 mState.setClippingOutline(allocator, outline);
2139}
2140
2141void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2142 const Rect& rect, float radius, bool highPriority) {
2143 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2144}
2145
Tom Hudson107843d2014-09-08 11:26:26 -04002146void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002147 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002148 DrawOpMode drawOpMode) {
2149
Chris Craik03ae2722015-02-25 11:01:09 -08002150 if (drawOpMode == DrawOpMode::kImmediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002151 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2152 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002153 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002154 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002155 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002156 }
Romain Guycac5fd32011-12-01 20:08:50 -08002157 }
2158
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002159 const float oldX = x;
2160 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002161
Chris Craikd6b65f62014-01-01 14:45:21 -08002162 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002163 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002164
Romain Guy211370f2012-02-01 16:10:55 -08002165 if (CC_LIKELY(pureTranslate)) {
Chris Craika6b52192015-02-27 17:43:02 -08002166 x = floorf(x + transform.getTranslateX() + 0.5f);
2167 y = floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002168 }
2169
Romain Guy86568192010-12-14 15:55:39 -08002170 int alpha;
2171 SkXfermode::Mode mode;
2172 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002173
Romain Guyc74f45a2013-02-26 19:10:14 -08002174 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2175
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002176 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002177 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002178 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002179 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002180 }
2181
Romain Guy19d4dd82013-03-04 11:14:26 -08002182 const bool hasActiveLayer = hasLayer();
2183
Romain Guy624234f2013-03-05 16:43:31 -08002184 // We only pass a partial transform to the font renderer. That partial
2185 // matrix defines how glyphs are rasterized. Typically we want glyphs
2186 // to be rasterized at their final size on screen, which means the partial
2187 // matrix needs to take the scale factor into account.
2188 // When a partial matrix is used to transform glyphs during rasterization,
2189 // the mesh is generated with the inverse transform (in the case of scale,
2190 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2191 // apply the full transform matrix at draw time in the vertex shader.
2192 // Applying the full matrix in the shader is the easiest way to handle
2193 // rotation and perspective and allows us to always generated quads in the
2194 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002195 SkMatrix fontTransform;
2196 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2197 || fabs(y - (int) y) > 0.0f
2198 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002199 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002200 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002201
Romain Guy624234f2013-03-05 16:43:31 -08002202 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08002203 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002204 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 -07002205
Raph Levien996e57c2012-07-23 15:22:52 -07002206 bool status;
Chris Craik82840732015-04-03 09:37:49 -07002207 TextDrawFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002208
2209 // don't call issuedrawcommand, do it at end of batch
Chris Craik03ae2722015-02-25 11:01:09 -08002210 bool forceFinish = (drawOpMode != DrawOpMode::kDefer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002211 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002212 SkPaint paintCopy(*paint);
2213 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2214 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002215 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002216 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002217 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002218 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002219 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002220
Chris Craik03ae2722015-02-25 11:01:09 -08002221 if ((status || drawOpMode != DrawOpMode::kImmediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002222 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002223 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002224 }
Chris Craik41541822013-05-03 16:35:54 -07002225 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002226 }
Romain Guy694b5192010-07-21 21:33:20 -07002227
Chris Craike63f7c622013-10-17 10:30:55 -07002228 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002229
Tom Hudson107843d2014-09-08 11:26:26 -04002230 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002231}
2232
Tom Hudson107843d2014-09-08 11:26:26 -04002233void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002234 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002235 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002236 return;
Romain Guy03d58522012-02-24 17:54:07 -08002237 }
2238
Chris Craik39a908c2013-06-13 14:39:01 -07002239 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002240 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002241
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002242 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002243 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002244 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002245
2246 int alpha;
2247 SkXfermode::Mode mode;
2248 getAlphaAndMode(paint, &alpha, &mode);
Chris Craik82840732015-04-03 09:37:49 -07002249 TextDrawFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002250
Tom Hudson984162f2014-10-10 13:38:16 -04002251 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002252 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 -08002253
Romain Guy97771732012-02-28 18:17:02 -08002254 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chih-Hung Hsieh9ad6ac02015-02-27 16:11:36 -08002255 hOffset, vOffset, hasLayer() ? &bounds : nullptr, &functor)) {
Chris Craik083e7332015-02-27 17:04:20 -08002256 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2257 mDirty = true;
Romain Guy97771732012-02-28 18:17:02 -08002258 }
Romain Guy325740f2012-02-24 16:48:34 -08002259}
2260
Tom Hudson107843d2014-09-08 11:26:26 -04002261void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002262 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002263
Chris Craik44eb2c02015-01-29 09:45:09 -08002264 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002265
Chris Craik30036092015-02-12 10:41:39 -08002266 PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002267 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002268 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002269
Romain Guy8b55f372010-08-18 17:10:07 -07002270 const float x = texture->left - texture->offset;
2271 const float y = texture->top - texture->offset;
2272
Romain Guy01d58e42011-01-19 21:54:02 -08002273 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002274 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002275}
2276
Tom Hudson107843d2014-09-08 11:26:26 -04002277void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002278 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002279 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002280 }
2281
Chris Craike84a2082014-12-22 14:28:49 -08002282 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002283 if (layer->isTextureLayer()) {
2284 transform = &layer->getTransform();
2285 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002286 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002287 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002288 }
2289 }
2290
Chris Craik39a908c2013-06-13 14:39:01 -07002291 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08002292 const bool rejected = mState.calculateQuickRejectForScissor(
2293 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2294 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002295
2296 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002297 if (transform && !transform->isIdentity()) {
2298 restore();
2299 }
Tom Hudson107843d2014-09-08 11:26:26 -04002300 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002301 }
2302
Chris Craik62d307c2014-07-29 10:35:13 -07002303 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2304 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2305
Romain Guy5bb3c732012-11-29 17:52:58 -08002306 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002307
Chris Craik65fe5ee2015-01-26 18:06:29 -08002308 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08002309 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002310
Romain Guy211370f2012-02-01 16:10:55 -08002311 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002312 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002313 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2314 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002315 } else if (layer->mesh) {
Chris Craik82840732015-04-03 09:37:49 -07002316 Glop glop;
2317 GlopBuilder(mRenderState, mCaches, &glop)
2318 .setMeshTexturedIndexedQuads(layer->mesh, layer->meshElementCount)
2319 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
2320 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
2321 .setModelViewOffsetRectSnap(x, y, Rect(0, 0, layer->layer.getWidth(), layer->layer.getHeight()))
2322 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2323 .build();
2324 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy3a3133d2011-02-01 22:59:58 -08002325#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07002326 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08002327#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002328 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002329
Romain Guy5bb3c732012-11-29 17:52:58 -08002330 if (layer->debugDrawUpdate) {
2331 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002332
2333 SkPaint paint;
2334 paint.setColor(0x7f00ff00);
2335 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002336 }
Romain Guyf219da52011-01-16 12:54:25 -08002337 }
Chris Craik34416ea2013-04-15 16:08:28 -07002338 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07002339
Romain Guyb2e2f242012-10-17 18:18:35 -07002340 if (transform && !transform->isIdentity()) {
2341 restore();
2342 }
2343
Tom Hudson107843d2014-09-08 11:26:26 -04002344 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08002345}
2346
Romain Guy6926c722010-07-12 20:20:03 -07002347///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002348// Draw filters
2349///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04002350void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
2351 // We should never get here since we apply the draw filter when stashing
2352 // the paints in the DisplayList.
2353 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08002354}
2355
2356///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002357// Drawing implementation
2358///////////////////////////////////////////////////////////////////////////////
2359
Chris Craikd218a922014-01-02 17:13:34 -08002360Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08002361 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002362 if (!texture) {
2363 return mCaches.textureCache.get(bitmap);
2364 }
2365 return texture;
2366}
2367
Chris Craik2bb8f562015-02-17 16:42:02 -08002368void OpenGLRenderer::drawPathTexture(PathTexture* texture, float x, float y,
2369 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002370 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08002371 return;
2372 }
2373
Chris Craik82840732015-04-03 09:37:49 -07002374 Glop glop;
2375 GlopBuilder(mRenderState, mCaches, &glop)
2376 .setMeshTexturedUnitQuad(nullptr)
2377 .setFillPathTexturePaint(*texture, *paint, currentSnapshot()->alpha)
2378 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
2379 .setModelViewMapUnitToRect(Rect(x, y, x + texture->width, y + texture->height))
2380 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2381 .build();
2382 renderGlop(glop);
Romain Guy01d58e42011-01-19 21:54:02 -08002383}
2384
Romain Guyf607bdc2010-09-10 19:20:06 -07002385// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002386#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2387#define kStdUnderline_Offset (1.0f / 9.0f)
2388#define kStdUnderline_Thickness (1.0f / 18.0f)
2389
Chris Craikd218a922014-01-02 17:13:34 -08002390void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
2391 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07002392 // Handle underline and strike-through
2393 uint32_t flags = paint->getFlags();
2394 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002395 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002396
Romain Guy211370f2012-02-01 16:10:55 -08002397 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002398 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002399 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002400
Raph Levien8b4072d2012-07-30 15:50:00 -07002401 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002402 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002403
Romain Guyf6834472011-01-23 13:32:12 -08002404 int linesCount = 0;
2405 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2406 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2407
2408 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002409 float points[pointsCount];
2410 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002411
2412 if (flags & SkPaint::kUnderlineText_Flag) {
2413 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002414 points[currentPoint++] = left;
2415 points[currentPoint++] = top;
2416 points[currentPoint++] = left + underlineWidth;
2417 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002418 }
2419
2420 if (flags & SkPaint::kStrikeThruText_Flag) {
2421 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002422 points[currentPoint++] = left;
2423 points[currentPoint++] = top;
2424 points[currentPoint++] = left + underlineWidth;
2425 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002426 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002427
Romain Guy726aeba2011-06-01 14:52:00 -07002428 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002429
Romain Guy726aeba2011-06-01 14:52:00 -07002430 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002431 }
2432 }
2433}
2434
Tom Hudson107843d2014-09-08 11:26:26 -04002435void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002436 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002437 return;
Romain Guy672433d2013-01-04 19:05:13 -08002438 }
2439
Tom Hudson107843d2014-09-08 11:26:26 -04002440 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08002441}
2442
Tom Hudson107843d2014-09-08 11:26:26 -04002443void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07002444 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04002445 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07002446
Chris Craik15a07a22014-01-26 13:43:53 -08002447 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08002448 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07002449
2450 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08002451 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07002452
ztenghui14a4e352014-08-13 10:44:39 -07002453 // The caller has made sure casterAlpha > 0.
2454 float ambientShadowAlpha = mAmbientShadowAlpha;
Chris Craik2507c342015-05-04 14:36:49 -07002455 if (CC_UNLIKELY(Properties::overrideAmbientShadowStrength >= 0)) {
2456 ambientShadowAlpha = Properties::overrideAmbientShadowStrength;
ztenghui14a4e352014-08-13 10:44:39 -07002457 }
2458 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
2459 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002460 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002461 }
ztenghui7b4516e2014-01-07 10:42:55 -08002462
ztenghui14a4e352014-08-13 10:44:39 -07002463 float spotShadowAlpha = mSpotShadowAlpha;
Chris Craik2507c342015-05-04 14:36:49 -07002464 if (CC_UNLIKELY(Properties::overrideSpotShadowStrength >= 0)) {
2465 spotShadowAlpha = Properties::overrideSpotShadowStrength;
ztenghui14a4e352014-08-13 10:44:39 -07002466 }
2467 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
2468 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002469 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002470 }
ztenghui7b4516e2014-01-07 10:42:55 -08002471
Tom Hudson107843d2014-09-08 11:26:26 -04002472 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07002473}
2474
Tom Hudson107843d2014-09-08 11:26:26 -04002475void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002476 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08002477 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04002478 return;
Romain Guy3b753822013-03-05 10:27:35 -08002479 }
Romain Guy735738c2012-12-03 12:34:51 -08002480
Romain Guy672433d2013-01-04 19:05:13 -08002481 float left = FLT_MAX;
2482 float top = FLT_MAX;
2483 float right = FLT_MIN;
2484 float bottom = FLT_MIN;
2485
Romain Guy448455f2013-07-22 13:57:50 -07002486 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08002487 Vertex* vertex = mesh;
2488
Chris Craik2af46352012-11-26 18:30:17 -08002489 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08002490 float l = rects[index + 0];
2491 float t = rects[index + 1];
2492 float r = rects[index + 2];
2493 float b = rects[index + 3];
2494
Romain Guy3b753822013-03-05 10:27:35 -08002495 Vertex::set(vertex++, l, t);
2496 Vertex::set(vertex++, r, t);
2497 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08002498 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08002499
Romain Guy3b753822013-03-05 10:27:35 -08002500 left = fminf(left, l);
2501 top = fminf(top, t);
2502 right = fmaxf(right, r);
2503 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08002504 }
2505
Chris Craikf0a59072013-11-19 18:00:46 -08002506 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002507 return;
Romain Guya362c692013-02-04 13:50:16 -08002508 }
Romain Guy672433d2013-01-04 19:05:13 -08002509
Chris Craik82840732015-04-03 09:37:49 -07002510 const Matrix4& transform = ignoreTransform ? Matrix4::identity() : *currentTransform();
2511 Glop glop;
2512 GlopBuilder(mRenderState, mCaches, &glop)
2513 .setMeshIndexedQuads(&mesh[0], count / 4)
2514 .setFillPaint(*paint, currentSnapshot()->alpha)
2515 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
2516 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
2517 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2518 .build();
2519 renderGlop(glop);
Romain Guy672433d2013-01-04 19:05:13 -08002520}
2521
Romain Guy026c5e162010-06-28 17:12:22 -07002522void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002523 const SkPaint* paint, bool ignoreTransform) {
Chris Craik82840732015-04-03 09:37:49 -07002524 const Matrix4& transform = ignoreTransform ? Matrix4::identity() : *currentTransform();
2525 Glop glop;
2526 GlopBuilder(mRenderState, mCaches, &glop)
2527 .setMeshUnitQuad()
2528 .setFillPaint(*paint, currentSnapshot()->alpha)
2529 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
2530 .setModelViewMapUnitToRect(Rect(left, top, right, bottom))
2531 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2532 .build();
2533 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07002534}
2535
Chris Craike84a2082014-12-22 14:28:49 -08002536void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
2537 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002538 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craikd6b65f62014-01-01 14:45:21 -08002539 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002540}
2541
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002542float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik8df5ffa2015-04-28 17:47:20 -07002543 return (layer->getAlpha() / 255.0f) * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07002544}
2545
Romain Guy9d5316e2010-06-24 19:30:36 -07002546}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002547}; // namespace android