blob: d4f65b635d4c81fc4885ead380f7fa48e3150547 [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>
Chris Craikfca52b752015-04-28 11:45:59 -070043#include <SkPathOps.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040044#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070045#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070046
Romain Guye4d01122010-06-16 18:44:05 -070047#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070048#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070049
Romain Guy08aa2cb2011-03-17 11:06:57 -070050#include <private/hwui/DrawGlInfo.h>
51
Romain Guy5b3b3522010-10-27 18:57:51 -070052#include <ui/Rect.h>
53
Chris Craik62d307c2014-07-29 10:35:13 -070054#if DEBUG_DETAILED_EVENTS
55 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
56#else
57 #define EVENT_LOGD(...)
58#endif
59
Romain Guye4d01122010-06-16 18:44:05 -070060namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070061namespace uirenderer {
62
Romain Guy9d5316e2010-06-24 19:30:36 -070063///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -070064// Constructors/destructor
65///////////////////////////////////////////////////////////////////////////////
66
John Reck3b202512014-06-23 13:13:08 -070067OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -040068 : mState(*this)
Chris Craik058fc642014-07-23 18:19:28 -070069 , mCaches(Caches::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -070070 , mRenderState(renderState)
Chris Craik65fe5ee2015-01-26 18:06:29 -080071 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -070072 , mScissorOptimizationDisabled(false)
Tom Hudson107843d2014-09-08 11:26:26 -040073 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -070074 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -070075 , mLightRadius(FLT_MIN)
76 , mAmbientShadowAlpha(0)
77 , mSpotShadowAlpha(0) {
Romain Guye4d01122010-06-16 18:44:05 -070078}
79
Romain Guy85bf02f2010-06-22 13:11:24 -070080OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -070081 // The context has already been destroyed at this point, do not call
82 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -070083}
84
Romain Guy87e2f7572012-09-24 11:37:12 -070085void OpenGLRenderer::initProperties() {
86 char property[PROPERTY_VALUE_MAX];
87 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
88 mScissorOptimizationDisabled = !strcasecmp(property, "true");
89 INIT_LOGD(" Scissor optimization %s",
90 mScissorOptimizationDisabled ? "disabled" : "enabled");
91 } else {
92 INIT_LOGD(" Scissor optimization enabled");
93 }
Romain Guy13631f32012-01-30 17:41:55 -080094}
95
Alan Viverette50210d92015-05-14 18:05:36 -070096void OpenGLRenderer::initLight(float lightRadius, uint8_t ambientShadowAlpha,
97 uint8_t spotShadowAlpha) {
Chris Craik058fc642014-07-23 18:19:28 -070098 mLightRadius = lightRadius;
99 mAmbientShadowAlpha = ambientShadowAlpha;
100 mSpotShadowAlpha = spotShadowAlpha;
101}
102
Alan Viverette50210d92015-05-14 18:05:36 -0700103void OpenGLRenderer::setLightCenter(const Vector3& lightCenter) {
104 mLightCenter = lightCenter;
105}
106
Romain Guy13631f32012-01-30 17:41:55 -0800107///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700108// Setup
109///////////////////////////////////////////////////////////////////////////////
110
Chris Craik797b95b22014-05-20 18:10:25 -0700111void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700112 glDisable(GL_DITHER);
113 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy35643dd2012-09-18 15:40:58 -0700114}
115
Chris Craik64e445b2015-09-02 14:23:49 -0700116void OpenGLRenderer::setupFrameState(int viewportWidth, int viewportHeight,
117 float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800118 mCaches.clearGarbage();
Chris Craik64e445b2015-09-02 14:23:49 -0700119 mState.initializeSaveStack(viewportWidth, viewportHeight,
120 left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700121 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700122 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700123}
124
Tom Hudson107843d2014-09-08 11:26:26 -0400125void OpenGLRenderer::startFrame() {
126 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700127 mFrameStarted = true;
128
Tom Hudson984162f2014-10-10 13:38:16 -0400129 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700130
Romain Guy96885eb2013-03-26 15:05:58 -0700131 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700132
Tom Hudson984162f2014-10-10 13:38:16 -0400133 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800134
Romain Guy7c450aa2012-09-21 19:15:00 -0700135 debugOverdraw(true, true);
136
Tom Hudson107843d2014-09-08 11:26:26 -0400137 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700138 mTilingClip.right, mTilingClip.bottom, mOpaque);
139}
140
Chris Craik64e445b2015-09-02 14:23:49 -0700141void OpenGLRenderer::prepareDirty(int viewportWidth, int viewportHeight,
142 float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700143
Chris Craik64e445b2015-09-02 14:23:49 -0700144 setupFrameState(viewportWidth, viewportHeight, left, top, right, bottom, opaque);
Romain Guy96885eb2013-03-26 15:05:58 -0700145
146 // Layer renderers will start the frame immediately
147 // The framebuffer renderer will first defer the display list
148 // for each layer and wait until the first drawing command
149 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800150 if (currentSnapshot()->fbo == 0) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800151 mRenderState.blend().syncEnabled();
Romain Guy96885eb2013-03-26 15:05:58 -0700152 updateLayers();
153 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400154 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700155 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700156}
157
Romain Guydcfc8362013-01-03 13:08:57 -0800158void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
159 // If we know that we are going to redraw the entire framebuffer,
160 // perform a discard to let the driver know we don't need to preserve
161 // the back buffer for this frame.
Chris Craik117bdbc2015-02-05 10:12:38 -0800162 if (mCaches.extensions().hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400163 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
Chris Craik6b109c72015-02-27 10:55:28 -0800164 const bool isFbo = getTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800165 const GLenum attachments[] = {
166 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
167 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800168 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
169 }
170}
171
Tom Hudson107843d2014-09-08 11:26:26 -0400172void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700173 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800174 mRenderState.scissor().setEnabled(true);
175 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700176 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400177 mDirty = true;
178 return;
Romain Guyddf74372012-05-22 14:07:07 -0700179 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700180
Chris Craik65fe5ee2015-01-26 18:06:29 -0800181 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700182}
183
Tom Hudson107843d2014-09-08 11:26:26 -0400184bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700185 renderOverdraw();
Chris Craik4ac36f82014-12-09 16:54:03 -0800186 mTempPaths.clear();
187
Romain Guyca89e2a2013-03-08 17:44:20 -0800188 // When finish() is invoked on FBO 0 we've reached the end
189 // of the current frame
Chris Craik6b109c72015-02-27 10:55:28 -0800190 if (getTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800191 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700192 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800193 }
194
Romain Guy11cb6422012-09-21 00:39:43 -0700195 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700196#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700197 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700198#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700199
Romain Guyc15008e2010-11-10 11:59:15 -0800200#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800201 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700202#else
Chris Craik2507c342015-05-04 14:36:49 -0700203 if (Properties::debugLevel & kDebugMemory) {
Romain Guy11cb6422012-09-21 00:39:43 -0700204 mCaches.dumpMemoryUsage();
205 }
Romain Guyc15008e2010-11-10 11:59:15 -0800206#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700207 }
Romain Guy96885eb2013-03-26 15:05:58 -0700208
209 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400210
211 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700212}
213
Romain Guy35643dd2012-09-18 15:40:58 -0700214void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700215 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
216 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700217 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700218
Chris Craik65fe5ee2015-01-26 18:06:29 -0800219 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700220 dirtyClip();
221}
222
Andreas Gampe64bb4132014-11-22 00:35:09 +0000223void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400224 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700225
Chris Craik6fe991e52015-10-20 09:39:42 -0700226 Rect clip(mState.currentRenderTargetClip());
Romain Guy80911b82011-03-16 15:30:12 -0700227 clip.snapToPixelBoundaries();
228
Romain Guyd643bb52011-03-01 14:55:21 -0800229 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800230 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800231 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800232 dirtyLayerUnchecked(clip, getRegion());
233 }
Romain Guyd643bb52011-03-01 14:55:21 -0800234
Romain Guy08aa2cb2011-03-17 11:06:57 -0700235 DrawGlInfo info;
236 info.clipLeft = clip.left;
237 info.clipTop = clip.top;
238 info.clipRight = clip.right;
239 info.clipBottom = clip.bottom;
240 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700241 info.width = getViewportWidth();
242 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800243 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700244
Tom Hudson984162f2014-10-10 13:38:16 -0400245 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700246 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400247 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700248 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
249 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800250 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700251 setScissorFromClip();
252 }
Chris Craik54f574a2013-08-26 11:23:46 -0700253
John Reck3b202512014-06-23 13:13:08 -0700254 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
255 // Scissor may have been modified, reset dirty clip
256 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800257
Tom Hudson107843d2014-09-08 11:26:26 -0400258 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800259}
260
Romain Guyf6a11b82010-06-23 17:47:49 -0700261///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700262// Debug
263///////////////////////////////////////////////////////////////////////////////
264
Chris Craik62d307c2014-07-29 10:35:13 -0700265void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
266#if DEBUG_DETAILED_EVENTS
267 const int BUFFER_SIZE = 256;
268 va_list ap;
269 char buf[BUFFER_SIZE];
270
271 va_start(ap, fmt);
272 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
273 va_end(ap);
274
275 eventMark(buf);
276#endif
277}
278
279
Romain Guy0f6675332013-03-01 14:31:04 -0800280void OpenGLRenderer::eventMark(const char* name) const {
281 mCaches.eventMark(0, name);
282}
283
Romain Guy87e2f7572012-09-24 11:37:12 -0700284void OpenGLRenderer::startMark(const char* name) const {
285 mCaches.startMark(0, name);
286}
287
288void OpenGLRenderer::endMark() const {
289 mCaches.endMark();
290}
291
292void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700293 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700294}
295
296void OpenGLRenderer::renderOverdraw() {
Chris Craik2507c342015-05-04 14:36:49 -0700297 if (Properties::debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700298 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700299
Chris Craik65fe5ee2015-01-26 18:06:29 -0800300 mRenderState.scissor().setEnabled(true);
301 mRenderState.scissor().set(clip->left,
302 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
303 clip->right - clip->left,
304 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700305
Romain Guy627c6fd2013-08-21 11:53:18 -0700306 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800307 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700308 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
309
310 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800311 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700312 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
313
314 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800315 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700316 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
317
318 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800319 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700320 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
321
Chris Craik96a5c4c2015-01-27 15:46:35 -0800322 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700323 }
324}
325
326///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700327// Layers
328///////////////////////////////////////////////////////////////////////////////
329
330bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700331 if (layer->deferredUpdateScheduled && layer->renderer
332 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700333
Romain Guy7c450aa2012-09-21 19:15:00 -0700334 if (inFrame) {
Romain Guy7c450aa2012-09-21 19:15:00 -0700335 debugOverdraw(false, false);
336 }
Romain Guy11cb6422012-09-21 00:39:43 -0700337
Chris Craik2507c342015-05-04 14:36:49 -0700338 if (CC_UNLIKELY(inFrame || Properties::drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700339 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700340 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700341 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700342 }
Romain Guy11cb6422012-09-21 00:39:43 -0700343
344 if (inFrame) {
345 resumeAfterLayer();
Romain Guy11cb6422012-09-21 00:39:43 -0700346 }
347
Chris Craik2507c342015-05-04 14:36:49 -0700348 layer->debugDrawUpdate = Properties::debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700349 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700350
351 return true;
352 }
353
354 return false;
355}
356
357void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700358 // If draw deferring is enabled this method will simply defer
359 // the display list of each individual layer. The layers remain
360 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700361 int count = mLayerUpdates.size();
362 if (count > 0) {
Chris Craik2507c342015-05-04 14:36:49 -0700363 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -0700364 startMark("Layer Updates");
365 } else {
366 startMark("Defer Layer Updates");
367 }
Romain Guy11cb6422012-09-21 00:39:43 -0700368
Chris Craik1206b9b2013-04-04 14:46:24 -0700369 // Note: it is very important to update the layers in order
370 for (int i = 0; i < count; i++) {
John Reck272a6852015-07-29 16:48:58 -0700371 Layer* layer = mLayerUpdates[i].get();
Romain Guy11cb6422012-09-21 00:39:43 -0700372 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700373 }
Romain Guy11cb6422012-09-21 00:39:43 -0700374
Chris Craik2507c342015-05-04 14:36:49 -0700375 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -0700376 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800377 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700378 }
379 endMark();
380 }
381}
382
383void OpenGLRenderer::flushLayers() {
384 int count = mLayerUpdates.size();
385 if (count > 0) {
386 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700387
Chris Craik1206b9b2013-04-04 14:46:24 -0700388 // Note: it is very important to update the layers in order
389 for (int i = 0; i < count; i++) {
John Reck272a6852015-07-29 16:48:58 -0700390 mLayerUpdates[i]->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700391 }
392
393 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800394 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700395
Romain Guy11cb6422012-09-21 00:39:43 -0700396 endMark();
397 }
398}
399
400void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
401 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700402 // Make sure we don't introduce duplicates.
403 // SortedVector would do this automatically but we need to respect
404 // the insertion order. The linear search is not an issue since
405 // this list is usually very short (typically one item, at most a few)
406 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
John Reck272a6852015-07-29 16:48:58 -0700407 if (mLayerUpdates[i] == layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700408 return;
409 }
410 }
Romain Guy11cb6422012-09-21 00:39:43 -0700411 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700412 }
413}
414
Romain Guye93482f2013-06-17 13:14:51 -0700415void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
416 if (layer) {
417 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
John Reck272a6852015-07-29 16:48:58 -0700418 if (mLayerUpdates[i] == layer) {
419 mLayerUpdates.erase(mLayerUpdates.begin() + i);
Romain Guye93482f2013-06-17 13:14:51 -0700420 break;
421 }
422 }
423 }
424}
425
Romain Guy40543602013-06-12 15:31:28 -0700426void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800427 ATRACE_NAME("Update HW Layers");
Chris Craik44eb2c02015-01-29 09:45:09 -0800428 mRenderState.blend().syncEnabled();
Romain Guy40543602013-06-12 15:31:28 -0700429 updateLayers();
430 flushLayers();
431 // Wait for all the layer updates to be executed
John Reck55156372015-01-21 07:46:37 -0800432 glFinish();
Romain Guy40543602013-06-12 15:31:28 -0700433}
434
John Reck443a7142014-09-04 17:40:05 -0700435void OpenGLRenderer::markLayersAsBuildLayers() {
436 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
437 mLayerUpdates[i]->wasBuildLayered = true;
438 }
439}
440
Romain Guy11cb6422012-09-21 00:39:43 -0700441///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700442// State management
443///////////////////////////////////////////////////////////////////////////////
444
Chris Craik14e51302013-12-30 15:32:54 -0800445void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700446 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800447 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
448 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700449
Chris Craika64a2be2014-05-14 14:17:01 -0700450 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700451 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700452 }
453
Romain Guy2542d192010-08-18 11:47:12 -0700454 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700455 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700456 }
Romain Guy2542d192010-08-18 11:47:12 -0700457
Romain Guy5ec99242010-11-03 16:19:08 -0700458 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700459 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700460 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700461 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800462 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700463 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700464 }
Romain Guybb9524b2010-06-22 18:56:38 -0700465}
466
Romain Guyf6a11b82010-06-23 17:47:49 -0700467///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700468// Layers
469///////////////////////////////////////////////////////////////////////////////
470
471int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700472 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700473 // force matrix/clip isolation for layer
474 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
475
Tom Hudson984162f2014-10-10 13:38:16 -0400476 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700477
Tom Hudson984162f2014-10-10 13:38:16 -0400478 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700479 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700480 }
Romain Guyd55a8612010-06-28 17:42:46 -0700481
482 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700483}
484
Chris Craikd90144d2013-03-19 15:03:48 -0700485void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
486 const Rect untransformedBounds(bounds);
487
Chris Craikd6b65f62014-01-01 14:45:21 -0800488 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700489
490 // Layers only make sense if they are in the framebuffer's bounds
Chris Craik6fe991e52015-10-20 09:39:42 -0700491 bounds.doIntersect(mState.currentRenderTargetClip());
Chris Craikac02eb92015-10-05 12:23:46 -0700492 if (!bounds.isEmpty()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700493 // We cannot work with sub-pixels in this case
494 bounds.snapToPixelBoundaries();
495
496 // When the layer is not an FBO, we may use glCopyTexImage so we
497 // need to make sure the layer does not extend outside the bounds
498 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700499 const Snapshot& previous = *(currentSnapshot()->previous);
500 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
Chris Craikac02eb92015-10-05 12:23:46 -0700501
502 bounds.doIntersect(previousViewport);
503 if (!bounds.isEmpty() && fboLayer) {
Chris Craikd90144d2013-03-19 15:03:48 -0700504 clip.set(bounds);
505 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800506 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700507 inverse.mapRect(clip);
508 clip.snapToPixelBoundaries();
Chris Craikac02eb92015-10-05 12:23:46 -0700509 clip.doIntersect(untransformedBounds);
510 if (!clip.isEmpty()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700511 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
512 bounds.set(untransformedBounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700513 }
514 }
Chris Craikd90144d2013-03-19 15:03:48 -0700515 }
516}
517
Chris Craik408eb122013-03-26 18:55:15 -0700518void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
519 bool fboLayer, int alpha) {
520 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
521 bounds.getHeight() > mCaches.maxTextureSize ||
522 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400523 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700524 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400525 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700526 }
527}
528
Chris Craikd90144d2013-03-19 15:03:48 -0700529int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500530 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400531 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700532
Tom Hudson984162f2014-10-10 13:38:16 -0400533 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700534 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
535 // operations will be able to store and restore the current clip and transform info, and
536 // quick rejection will be correct (for display lists)
537
538 Rect bounds(left, top, right, bottom);
539 Rect clip;
540 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craikbf6f0f22015-10-01 12:36:07 -0700541 updateSnapshotIgnoreForLayer(bounds, clip, true, PaintUtils::getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700542
Tom Hudson984162f2014-10-10 13:38:16 -0400543 if (!mState.currentlyIgnored()) {
544 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
545 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
546 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800547 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700548 }
549 }
550
551 return count;
552}
553
Romain Guy1c740bc2010-09-13 18:00:09 -0700554/**
555 * Layers are viewed by Skia are slightly different than layers in image editing
556 * programs (for instance.) When a layer is created, previously created layers
557 * and the frame buffer still receive every drawing command. For instance, if a
558 * layer is created and a shape intersecting the bounds of the layers and the
559 * framebuffer is draw, the shape will be drawn on both (unless the layer was
560 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
561 *
562 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
563 * texture. Unfortunately, this is inefficient as it requires every primitive to
564 * be drawn n + 1 times, where n is the number of active layers. In practice this
565 * means, for every primitive:
566 * - Switch active frame buffer
567 * - Change viewport, clip and projection matrix
568 * - Issue the drawing
569 *
570 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700571 * To avoid this, layers are implemented in a different way here, at least in the
572 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
573 * is set. When this flag is set we can redirect all drawing operations into a
574 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700575 *
576 * This implementation relies on the frame buffer being at least RGBA 8888. When
577 * a layer is created, only a texture is created, not an FBO. The content of the
578 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700579 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700580 * buffer and drawing continues as normal. This technique therefore treats the
581 * frame buffer as a scratch buffer for the layers.
582 *
583 * To compose the layers back onto the frame buffer, each layer texture
584 * (containing the original frame buffer data) is drawn as a simple quad over
585 * the frame buffer. The trick is that the quad is set as the composition
586 * destination in the blending equation, and the frame buffer becomes the source
587 * of the composition.
588 *
589 * Drawing layers with an alpha value requires an extra step before composition.
590 * An empty quad is drawn over the layer's region in the frame buffer. This quad
591 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
592 * quad is used to multiply the colors in the frame buffer. This is achieved by
593 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
594 * GL_ZERO, GL_SRC_ALPHA.
595 *
596 * Because glCopyTexImage2D() can be slow, an alternative implementation might
597 * be use to draw a single clipped layer. The implementation described above
598 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700599 *
600 * (1) The frame buffer is actually not cleared right away. To allow the GPU
601 * to potentially optimize series of calls to glCopyTexImage2D, the frame
602 * buffer is left untouched until the first drawing operation. Only when
603 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700604 */
Chet Haased48885a2012-08-28 17:43:28 -0700605bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700606 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800607 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
608 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700609
Romain Guyeb993562010-10-05 18:14:38 -0700610 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
611
Romain Guyf607bdc2010-09-10 19:20:06 -0700612 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700613 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700614 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700615 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craikbf6f0f22015-10-01 12:36:07 -0700616 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, PaintUtils::getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700617
618 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400619 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700620 return false;
621 }
Romain Guyf18fd992010-07-08 11:45:51 -0700622
Chris Craik44eb2c02015-01-29 09:45:09 -0800623 mCaches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700624 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700625 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700626 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700627 }
628
Derek Sollenberger674554f2014-02-19 16:47:32 +0000629 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700630 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700631 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
632 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500633
Chet Haasea23eed82012-04-12 15:19:04 -0700634 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700635 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700636 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700637
Romain Guy8fb95422010-08-17 18:38:51 -0700638 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400639 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
640 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700641
Chris Craika8bea8e2014-09-24 11:29:43 -0700642 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
643 fboLayer ? "" : "unclipped ",
644 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700645 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700646 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700647 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700648 } else {
649 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700650 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800651 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700652 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700653 // Workaround for some GL drivers. When reading pixels lying outside
654 // of the window we should get undefined values for those pixels.
655 // Unfortunately some drivers will turn the entire target texture black
656 // when reading outside of the window.
657 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800658 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700659 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800660 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700661
Chris Craika64a2be2014-05-14 14:17:01 -0700662 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
663 bounds.left, getViewportHeight() - bounds.bottom,
664 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700665
Romain Guy54be1cd2011-06-13 19:04:27 -0700666 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800667 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700668 }
Romain Guyeb993562010-10-05 18:14:38 -0700669 }
Romain Guyf86ef572010-07-01 11:05:42 -0700670
Romain Guyd55a8612010-06-28 17:42:46 -0700671 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700672}
673
Chris Craike63f7c622013-10-17 10:30:55 -0700674bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800675 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700676 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700677
Tom Hudson984162f2014-10-10 13:38:16 -0400678 writableSnapshot()->region = &writableSnapshot()->layer->region;
679 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
680 writableSnapshot()->fbo = layer->getFbo();
681 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
682 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
683 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800684 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700685
Romain Guy7c450aa2012-09-21 19:15:00 -0700686 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700687 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700688 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700689 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700690
691 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700692 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700693 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700694 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700695 }
696
697 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Chris Craikf27133d2015-02-19 09:51:53 -0800698 layer->getTextureId(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700699
Romain Guy5b3b3522010-10-27 18:57:51 -0700700 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800701 mRenderState.scissor().setEnabled(true);
702 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700703 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700704 glClear(GL_COLOR_BUFFER_BIT);
705
706 dirtyClip();
707
708 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700709 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700710 return true;
711}
712
Romain Guy1c740bc2010-09-13 18:00:09 -0700713/**
714 * Read the documentation of createLayer() before doing anything in this method.
715 */
Chris Craik14e51302013-12-30 15:32:54 -0800716void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
717 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000718 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700719 return;
720 }
721
Chris Craik14e51302013-12-30 15:32:54 -0800722 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800723 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800724 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700725
Chris Craik39a908c2013-06-13 14:39:01 -0700726 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400727 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800728 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800729 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700730
Romain Guyeb993562010-10-05 18:14:38 -0700731 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700732 // Detach the texture from the FBO
733 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800734
735 layer->removeFbo(false);
736
Romain Guyeb993562010-10-05 18:14:38 -0700737 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700738 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700739 debugOverdraw(true, false);
Romain Guyeb993562010-10-05 18:14:38 -0700740 }
741
Romain Guy9ace8f52011-07-07 20:50:11 -0700742 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500743 SkPaint layerPaint;
744 layerPaint.setAlpha(layer->getAlpha());
745 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
746 layerPaint.setColorFilter(layer->getColorFilter());
747
748 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700749 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700750 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700751 }
752
Chris Craik96a5c4c2015-01-27 15:46:35 -0800753 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700754
Chris Craik44eb2c02015-01-29 09:45:09 -0800755 mCaches.textureState().activateTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700756
Romain Guy5b3b3522010-10-27 18:57:51 -0700757 // When the layer is stored in an FBO, we can save a bit of fillrate by
758 // drawing only the dirty region
759 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800760 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700761 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700762 } else if (!rect.isEmpty()) {
763 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530764
765 save(0);
766 // the layer contains screen buffer content that shouldn't be alpha modulated
767 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400768 writableSnapshot()->alpha = 1.0f;
Chris Craik53e51e42015-06-01 10:35:35 -0700769 composeLayerRectSwapped(layer, rect);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530770 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700771 }
Romain Guy8b55f372010-08-18 17:10:07 -0700772
Romain Guy746b7402010-10-26 16:27:31 -0700773 dirtyClip();
774
Romain Guyeb993562010-10-05 18:14:38 -0700775 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800776 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700777 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800778 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800779 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700780 }
781}
782
Romain Guyaa6c24c2011-04-28 18:40:04 -0700783void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craik1e4c8072015-05-26 14:25:02 -0700784 const bool tryToSnap = !layer->getForceFilter()
Chris Craik26bf3422015-02-26 16:28:17 -0800785 && layer->getWidth() == (uint32_t) rect.getWidth()
Chris Craik82840732015-04-03 09:37:49 -0700786 && layer->getHeight() == (uint32_t) rect.getHeight();
787 Glop glop;
788 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700789 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -0700790 .setMeshTexturedUvQuad(nullptr, Rect(0, 1, 1, 0)) // TODO: simplify with VBO
791 .setFillTextureLayer(*layer, getLayerAlpha(layer))
Chris Craik53e51e42015-06-01 10:35:35 -0700792 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik1e4c8072015-05-26 14:25:02 -0700793 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, rect)
Chris Craik82840732015-04-03 09:37:49 -0700794 .build();
795 renderGlop(glop);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700796}
797
Chris Craik53e51e42015-06-01 10:35:35 -0700798void OpenGLRenderer::composeLayerRectSwapped(Layer* layer, const Rect& rect) {
799 Glop glop;
800 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700801 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik53e51e42015-06-01 10:35:35 -0700802 .setMeshTexturedUvQuad(nullptr, layer->texCoords)
803 .setFillLayer(layer->getTexture(), layer->getColorFilter(),
804 getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::Swap)
805 .setTransform(*currentSnapshot(), TransformFlags::MeshIgnoresCanvasTransform)
806 .setModelViewMapUnitToRect(rect)
Chris Craik53e51e42015-06-01 10:35:35 -0700807 .build();
808 renderGlop(glop);
809}
810
811void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect) {
Chris Craik62d307c2014-07-29 10:35:13 -0700812 if (layer->isTextureLayer()) {
813 EVENT_LOGD("composeTextureLayerRect");
Chris Craik62d307c2014-07-29 10:35:13 -0700814 drawTextureLayer(layer, rect);
Chris Craik62d307c2014-07-29 10:35:13 -0700815 } else {
816 EVENT_LOGD("composeHardwareLayerRect");
Chris Craik182952f2015-03-09 14:17:29 -0700817
Chris Craik53e51e42015-06-01 10:35:35 -0700818 const bool tryToSnap = layer->getWidth() == static_cast<uint32_t>(rect.getWidth())
Chris Craik82840732015-04-03 09:37:49 -0700819 && layer->getHeight() == static_cast<uint32_t>(rect.getHeight());
820 Glop glop;
821 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700822 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -0700823 .setMeshTexturedUvQuad(nullptr, layer->texCoords)
Chris Craik53e51e42015-06-01 10:35:35 -0700824 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
825 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik1e4c8072015-05-26 14:25:02 -0700826 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, rect)
Chris Craik82840732015-04-03 09:37:49 -0700827 .build();
828 renderGlop(glop);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700829 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700830}
831
Chris Craik34416ea2013-04-15 16:08:28 -0700832/**
833 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
834 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
835 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
836 * by saveLayer's restore
837 */
Chris Craik2507c342015-05-04 14:36:49 -0700838#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
839 DRAW_COMMAND; \
840 if (CC_UNLIKELY(Properties::debugOverdraw && getTargetFbo() == 0 && COND)) { \
841 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
842 DRAW_COMMAND; \
843 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
844 } \
Chris Craik34416ea2013-04-15 16:08:28 -0700845 }
846
847#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
848
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400849// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
850// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
851class LayerShader : public SkShader {
852public:
853 LayerShader(Layer* layer, const SkMatrix* localMatrix)
854 : INHERITED(localMatrix)
855 , mLayer(layer) {
856 }
857
Chris Craike84a2082014-12-22 14:28:49 -0800858 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400859 if (data) {
860 *data = static_cast<void*>(mLayer);
861 }
862 return true;
863 }
864
Chris Craike84a2082014-12-22 14:28:49 -0800865 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400866 return !mLayer->isBlend();
867 }
868
869protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000870 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400871 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
872 }
873
Chris Craike84a2082014-12-22 14:28:49 -0800874 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400875 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
876 }
877
Chris Craike84a2082014-12-22 14:28:49 -0800878 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400879 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -0800880 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400881 }
882private:
883 // Unowned.
884 Layer* mLayer;
885 typedef SkShader INHERITED;
886};
887
Romain Guy5b3b3522010-10-27 18:57:51 -0700888void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -0700889 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
890
891 if (layer->getConvexMask()) {
892 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
893
894 // clip to the area of the layer the mask can be larger
895 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
896
897 SkPaint paint;
898 paint.setAntiAlias(true);
899 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
900
Chris Craik3f0854292014-04-15 16:18:08 -0700901 // create LayerShader to map SaveLayer content into subsequent draw
902 SkMatrix shaderMatrix;
903 shaderMatrix.setTranslate(rect.left, rect.bottom);
904 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400905 LayerShader layerShader(layer, &shaderMatrix);
906 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -0700907
908 // Since the drawing primitive is defined in local drawing space,
909 // we don't need to modify the draw matrix
910 const SkPath* maskPath = layer->getConvexMask();
911 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
912
Chris Craike84a2082014-12-22 14:28:49 -0800913 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -0700914 restore();
915
916 return;
917 }
918
Romain Guy5b3b3522010-10-27 18:57:51 -0700919 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700920 layer->setRegionAsRect();
921
Chris Craik34416ea2013-04-15 16:08:28 -0700922 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -0700923
Romain Guy5b3b3522010-10-27 18:57:51 -0700924 layer->region.clear();
925 return;
926 }
927
Chris Craik62d307c2014-07-29 10:35:13 -0700928 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -0700929 // standard Region based draw
930 size_t count;
931 const android::Rect* rects;
932 Region safeRegion;
933 if (CC_LIKELY(hasRectToRectTransform())) {
934 rects = layer->region.getArray(&count);
935 } else {
936 safeRegion = Region::createTJunctionFreeRegion(layer->region);
937 rects = safeRegion.getArray(&count);
938 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700939
Chris Craik3f0854292014-04-15 16:18:08 -0700940 const float texX = 1.0f / float(layer->getWidth());
941 const float texY = 1.0f / float(layer->getHeight());
942 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700943
Chris Craik82840732015-04-03 09:37:49 -0700944 TextureVertex quadVertices[count * 4];
945 TextureVertex* mesh = &quadVertices[0];
Chris Craik3f0854292014-04-15 16:18:08 -0700946 for (size_t i = 0; i < count; i++) {
947 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -0700948
Chris Craik3f0854292014-04-15 16:18:08 -0700949 const float u1 = r->left * texX;
950 const float v1 = (height - r->top) * texY;
951 const float u2 = r->right * texX;
952 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700953
Chris Craik3f0854292014-04-15 16:18:08 -0700954 // TODO: Reject quads outside of the clip
955 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
956 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
957 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
958 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Chris Craik3f0854292014-04-15 16:18:08 -0700959 }
Tom Hudson040b6d82015-04-16 10:50:53 -0400960 Rect modelRect = Rect(rect.getWidth(), rect.getHeight());
Chris Craik82840732015-04-03 09:37:49 -0700961 Glop glop;
962 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700963 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -0700964 .setMeshTexturedIndexedQuads(&quadVertices[0], count * 6)
965 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
Chris Craik53e51e42015-06-01 10:35:35 -0700966 .setTransform(*currentSnapshot(), TransformFlags::None)
Tom Hudson040b6d82015-04-16 10:50:53 -0400967 .setModelViewOffsetRectSnap(rect.left, rect.top, modelRect)
Chris Craik82840732015-04-03 09:37:49 -0700968 .build();
969 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy5b3b3522010-10-27 18:57:51 -0700970
Romain Guy5b3b3522010-10-27 18:57:51 -0700971#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -0700972 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700973#endif
974
Chris Craik3f0854292014-04-15 16:18:08 -0700975 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -0700976}
977
Romain Guy3a3133d2011-02-01 22:59:58 -0800978#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -0700979void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -0800980 size_t count;
981 const android::Rect* rects = region.getArray(&count);
982
983 uint32_t colors[] = {
984 0x7fff0000, 0x7f00ff00,
985 0x7f0000ff, 0x7fff00ff,
986 };
987
988 int offset = 0;
989 int32_t top = rects[0].top;
990
991 for (size_t i = 0; i < count; i++) {
992 if (top != rects[i].top) {
993 offset ^= 0x2;
994 top = rects[i].top;
995 }
996
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500997 SkPaint paint;
998 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -0800999 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001000 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001001 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001002}
Chris Craike63f7c622013-10-17 10:30:55 -07001003#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001004
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001005void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001006 Vector<float> rects;
1007
1008 SkRegion::Iterator it(region);
1009 while (!it.done()) {
1010 const SkIRect& r = it.rect();
1011 rects.push(r.fLeft);
1012 rects.push(r.fTop);
1013 rects.push(r.fRight);
1014 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001015 it.next();
1016 }
1017
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001018 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001019}
1020
Romain Guy5b3b3522010-10-27 18:57:51 -07001021void OpenGLRenderer::dirtyLayer(const float left, const float top,
Chris Craik083e7332015-02-27 17:04:20 -08001022 const float right, const float bottom, const Matrix4& transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001023 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001024 Rect bounds(left, top, right, bottom);
1025 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001026 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001027 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001028}
1029
1030void OpenGLRenderer::dirtyLayer(const float left, const float top,
1031 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001032 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001033 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001034 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001035 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001036}
1037
1038void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Chris Craik6fe991e52015-10-20 09:39:42 -07001039 bounds.doIntersect(mState.currentRenderTargetClip());
Chris Craikac02eb92015-10-05 12:23:46 -07001040 if (!bounds.isEmpty()) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001041 bounds.snapToPixelBoundaries();
1042 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1043 if (!dirty.isEmpty()) {
1044 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001045 }
1046 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001047}
1048
Romain Guy54be1cd2011-06-13 19:04:27 -07001049void OpenGLRenderer::clearLayerRegions() {
Chris Craik2bb8f562015-02-17 16:42:02 -08001050 const size_t quadCount = mLayers.size();
1051 if (quadCount == 0) return;
Romain Guy54be1cd2011-06-13 19:04:27 -07001052
Tom Hudson984162f2014-10-10 13:38:16 -04001053 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001054 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001055 // Doing several glScissor/glClear here can negatively impact
1056 // GPUs with a tiler architecture, instead we draw quads with
1057 // the Clear blending mode
1058
1059 // The list contains bounds that have already been clipped
1060 // against their initial clip rect, and the current clip
1061 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001062 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001063
Chris Craik2bb8f562015-02-17 16:42:02 -08001064 Vertex mesh[quadCount * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001065 Vertex* vertex = mesh;
1066
Chris Craik2bb8f562015-02-17 16:42:02 -08001067 for (uint32_t i = 0; i < quadCount; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001068 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001069
Chris Craik51d6a3d2014-12-22 17:16:56 -08001070 Vertex::set(vertex++, bounds.left, bounds.top);
1071 Vertex::set(vertex++, bounds.right, bounds.top);
1072 Vertex::set(vertex++, bounds.left, bounds.bottom);
1073 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001074 }
Romain Guye67307c2013-02-11 18:01:20 -08001075 // We must clear the list of dirty rects before we
Chris Craik82840732015-04-03 09:37:49 -07001076 // call clearLayerRegions() in renderGlop to prevent
1077 // stencil setup from doing the same thing again
Romain Guye67307c2013-02-11 18:01:20 -08001078 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001079
Chris Craik53e51e42015-06-01 10:35:35 -07001080 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001081 Glop glop;
1082 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001083 .setRoundRectClipState(nullptr) // clear ignores clip state
Chris Craik82840732015-04-03 09:37:49 -07001084 .setMeshIndexedQuads(&mesh[0], quadCount)
1085 .setFillClear()
Chris Craik53e51e42015-06-01 10:35:35 -07001086 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik6fe991e52015-10-20 09:39:42 -07001087 .setModelViewOffsetRect(0, 0, Rect(currentSnapshot()->getRenderTargetClip()))
Chris Craik82840732015-04-03 09:37:49 -07001088 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001089 renderGlop(glop, GlopRenderType::LayerClear);
Romain Guy8a4ac612012-07-17 17:32:48 -07001090
Chris Craik65fe5ee2015-01-26 18:06:29 -08001091 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001092 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001093 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001094 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001095}
1096
Romain Guybd6b79b2010-06-26 00:13:53 -07001097///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001098// State Deferral
1099///////////////////////////////////////////////////////////////////////////////
1100
Chris Craikff785832013-03-08 13:12:16 -08001101bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craik6fe991e52015-10-20 09:39:42 -07001102 const Rect& currentClip = mState.currentRenderTargetClip();
Chris Craikd6b65f62014-01-01 14:45:21 -08001103 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001104
Chris Craikff785832013-03-08 13:12:16 -08001105 if (stateDeferFlags & kStateDeferFlag_Draw) {
1106 // state has bounds initialized in local coordinates
1107 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001108 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001109 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001110 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1111 // is used, it should more closely duplicate the quickReject logic (in how it uses
1112 // snapToPixelBoundaries)
1113
Chris Craikac02eb92015-10-05 12:23:46 -07001114 clippedBounds.doIntersect(currentClip);
1115 if (clippedBounds.isEmpty()) {
Chris Craikff785832013-03-08 13:12:16 -08001116 // quick rejected
1117 return true;
1118 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001119
Chris Craika02c4ed2013-06-14 13:43:58 -07001120 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001121 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001122 int& flags = state.mClipSideFlags;
1123 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001124 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1125 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1126 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1127 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001128 }
1129 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001130 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001131 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1132 // overdraw avoidance (since we don't know what it overlaps)
1133 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001134 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001135 }
1136 }
1137
Chris Craik527a3aa2013-03-04 10:19:31 -08001138 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1139 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001140 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001141 }
1142
Chris Craik8df5ffa2015-04-28 17:47:20 -07001143 // Transform and alpha always deferred, since they are used by state operations
Chris Craik7273daa2013-03-28 11:25:24 -07001144 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craik7c85c542015-08-19 15:10:24 -07001145 state.mMatrix = *currentMatrix;
Chris Craikd6b65f62014-01-01 14:45:21 -08001146 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001147
Chris Craikfca52b752015-04-28 11:45:59 -07001148 // always store/restore, since these are just pointers
Chris Craikdeeda3d2014-05-05 19:09:33 -07001149 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikfca52b752015-04-28 11:45:59 -07001150 state.mProjectionPathMask = currentSnapshot()->projectionPathMask;
Chris Craikc3566d02013-02-04 16:16:33 -08001151 return false;
1152}
1153
Chris Craik527a3aa2013-03-04 10:19:31 -08001154void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik6daa13c2015-08-19 13:32:12 -07001155 setGlobalMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001156 writableSnapshot()->alpha = state.mAlpha;
Tom Hudson984162f2014-10-10 13:38:16 -04001157 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikfca52b752015-04-28 11:45:59 -07001158 writableSnapshot()->projectionPathMask = state.mProjectionPathMask;
Chris Craikff785832013-03-08 13:12:16 -08001159
Chris Craik527a3aa2013-03-04 10:19:31 -08001160 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001161 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001162 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001163 dirtyClip();
1164 }
Chris Craikc3566d02013-02-04 16:16:33 -08001165}
1166
Chris Craik28ce94a2013-05-31 11:38:03 -07001167/**
1168 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1169 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1170 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1171 *
1172 * This method should be called when restoreDisplayState() won't be restoring the clip
1173 */
1174void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001175 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001176 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001177 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001178 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001179 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001180 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001181 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1182 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001183}
1184
Chris Craikc3566d02013-02-04 16:16:33 -08001185///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001186// Clipping
1187///////////////////////////////////////////////////////////////////////////////
1188
Romain Guybb9524b2010-06-22 18:56:38 -07001189void OpenGLRenderer::setScissorFromClip() {
Chris Craik6fe991e52015-10-20 09:39:42 -07001190 Rect clip(mState.currentRenderTargetClip());
Romain Guye5ebcb02010-10-15 13:57:28 -07001191 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001192
Chris Craik65fe5ee2015-01-26 18:06:29 -08001193 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001194 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001195 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001196 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001197}
1198
Romain Guy8ce00302013-01-15 18:51:42 -08001199void OpenGLRenderer::ensureStencilBuffer() {
1200 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1201 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1202 // just hope we have one when hasLayer() returns false.
1203 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001204 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001205 }
1206}
1207
1208void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1209 // The layer's FBO is already bound when we reach this stage
1210 if (!layer->getStencilRenderBuffer()) {
Romain Guy8d4aeb72013-02-12 16:08:55 -08001211 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Chris Craike1450132015-04-28 17:55:50 -07001212 Stencil::getLayerStencilFormat(),
Chris Craik117bdbc2015-02-05 10:12:38 -08001213 layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001214 layer->setStencilRenderBuffer(buffer);
1215 }
1216}
1217
Rob Tsuk487a92c2015-01-06 13:22:54 -08001218static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1219 float x, float y) {
1220 Vertex v;
1221 v.x = x;
1222 v.y = y;
1223 transform.mapPoint(v.x, v.y);
1224 rectangleVertices.push_back(v);
1225}
1226
1227static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1228 Vertex v;
1229 v.x = x;
1230 v.y = y;
1231 rectangleVertices.push_back(v);
1232}
1233
1234void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
Chris Craik2bb8f562015-02-17 16:42:02 -08001235 int quadCount = rectangleList.getTransformedRectanglesCount();
1236 std::vector<Vertex> rectangleVertices(quadCount * 4);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001237 Rect scissorBox = rectangleList.calculateBounds();
1238 scissorBox.snapToPixelBoundaries();
Chris Craik2bb8f562015-02-17 16:42:02 -08001239 for (int i = 0; i < quadCount; ++i) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001240 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1241 const Matrix4& transform = tr.getTransform();
1242 Rect bounds = tr.getBounds();
1243 if (transform.rectToRect()) {
1244 transform.mapRect(bounds);
Chris Craikac02eb92015-10-05 12:23:46 -07001245 bounds.doIntersect(scissorBox);
1246 if (!bounds.isEmpty()) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001247 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1248 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1249 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1250 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1251 }
1252 } else {
1253 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1254 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1255 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1256 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1257 }
1258 }
1259
Chris Craik65fe5ee2015-01-26 18:06:29 -08001260 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001261 scissorBox.getWidth(), scissorBox.getHeight());
Chris Craik53e51e42015-06-01 10:35:35 -07001262 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001263 Glop glop;
1264 Vertex* vertices = &rectangleVertices[0];
1265 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001266 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001267 .setMeshIndexedQuads(vertices, rectangleVertices.size() / 4)
1268 .setFillBlack()
Chris Craik53e51e42015-06-01 10:35:35 -07001269 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001270 .setModelViewOffsetRect(0, 0, scissorBox)
Chris Craik82840732015-04-03 09:37:49 -07001271 .build();
1272 renderGlop(glop);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001273}
1274
Romain Guy8ce00302013-01-15 18:51:42 -08001275void OpenGLRenderer::setStencilFromClip() {
Chris Craik2507c342015-05-04 14:36:49 -07001276 if (!Properties::debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001277 if (!currentSnapshot()->clipIsSimple()) {
1278 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001279 EVENT_LOGD("setStencilFromClip - enabling");
1280
Romain Guy8ce00302013-01-15 18:51:42 -08001281 // NOTE: The order here is important, we must set dirtyClip to false
1282 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001283 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001284
1285 ensureStencilBuffer();
1286
Rob Tsuk487a92c2015-01-06 13:22:54 -08001287 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001288
Rob Tsuk487a92c2015-01-06 13:22:54 -08001289 bool isRectangleList = clipArea.isRectangleList();
1290 if (isRectangleList) {
1291 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1292 } else {
1293 incrementThreshold = 0;
1294 }
1295
Chris Craik96a5c4c2015-01-27 15:46:35 -08001296 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001297
1298 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001299 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001300 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001301 if (resetScissor) {
1302 // The scissor was not set so we now need to update it
1303 setScissorFromClip();
1304 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001305
Chris Craik96a5c4c2015-01-27 15:46:35 -08001306 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001307
1308 // stash and disable the outline clip state, since stencil doesn't account for outline
1309 bool storedSkipOutlineClip = mSkipOutlineClip;
1310 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001311
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001312 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001313 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001314 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1315
Rob Tsuk487a92c2015-01-06 13:22:54 -08001316 if (isRectangleList) {
1317 drawRectangleList(clipArea.getRectangleList());
1318 } else {
1319 // NOTE: We could use the region contour path to generate a smaller mesh
1320 // Since we are using the stencil we could use the red book path
1321 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001322
Rob Tsuk487a92c2015-01-06 13:22:54 -08001323 // The last parameter is important: we are not drawing in the color buffer
1324 // so we don't want to dirty the current layer, if any
1325 drawRegionRects(clipArea.getClipRegion(), paint, false);
1326 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001327 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001328 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001329
Chris Craik96a5c4c2015-01-27 15:46:35 -08001330 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001331
1332 // Draw the region used to generate the stencil if the appropriate debug
1333 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001334 // TODO: Implement for rectangle list clip areas
Chris Craik2507c342015-05-04 14:36:49 -07001335 if (Properties::debugStencilClip == StencilClipDebug::ShowRegion
1336 && !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001337 paint.setColor(0x7f0000ff);
1338 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001339 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001340 }
Romain Guy8ce00302013-01-15 18:51:42 -08001341 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001342 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001343 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001344 }
1345 }
1346}
1347
Chris Craikf0a59072013-11-19 18:00:46 -08001348/**
1349 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1350 *
1351 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1352 * style, and tessellated AA ramp
1353 */
1354bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001355 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001356 bool snapOut = paint && paint->isAntiAlias();
1357
1358 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1359 float outset = paint->getStrokeWidth() * 0.5f;
1360 left -= outset;
1361 top -= outset;
1362 right += outset;
1363 bottom += outset;
1364 }
1365
Chris Craikdeeda3d2014-05-05 19:09:33 -07001366 bool clipRequired = false;
1367 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001368 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001369 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001370 return true;
1371 }
1372
Chris Craikf23b25a2014-06-26 15:46:20 -07001373 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001374 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001375 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001376 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001377}
1378
Romain Guy8ce00302013-01-15 18:51:42 -08001379void OpenGLRenderer::debugClip() {
1380#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001381 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001382 SkPaint paint;
1383 paint.setColor(0x7f00ff00);
1384 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1385
Romain Guy8ce00302013-01-15 18:51:42 -08001386 }
1387#endif
1388}
1389
Chris Craik3375f8a2015-06-23 17:33:06 -07001390void OpenGLRenderer::renderGlop(const Glop& glop, GlopRenderType type) {
Chris Craik6b109c72015-02-27 10:55:28 -08001391 // TODO: It would be best if we could do this before quickRejectSetupScissor()
1392 // changes the scissor test state
Chris Craik3375f8a2015-06-23 17:33:06 -07001393 if (type != GlopRenderType::LayerClear) {
1394 // Regular draws need to clear the dirty area on the layer before they start drawing on top
1395 // of it. If this draw *is* a layer clear, it skips the clear step (since it would
1396 // infinitely recurse)
1397 clearLayerRegions();
1398 }
Chris Craik6b109c72015-02-27 10:55:28 -08001399
Chris Craik117bdbc2015-02-05 10:12:38 -08001400 if (mState.getDirtyClip()) {
1401 if (mRenderState.scissor().isEnabled()) {
1402 setScissorFromClip();
1403 }
1404
1405 setStencilFromClip();
1406 }
Chris Craik12efe642015-09-28 15:52:12 -07001407 mRenderState.render(glop, currentSnapshot()->getOrthoMatrix());
Chris Craik3375f8a2015-06-23 17:33:06 -07001408 if (type == GlopRenderType::Standard && !mRenderState.stencil().isWriteEnabled()) {
Chris Craik2ab95d72015-02-06 15:25:51 -08001409 // TODO: specify more clearly when a draw should dirty the layer.
1410 // is writing to the stencil the only time we should ignore this?
1411 dirtyLayer(glop.bounds.left, glop.bounds.top, glop.bounds.right, glop.bounds.bottom);
Chris Craik0519c8102015-02-11 13:17:06 -08001412 mDirty = true;
Chris Craik2ab95d72015-02-06 15:25:51 -08001413 }
Chris Craik117bdbc2015-02-05 10:12:38 -08001414}
1415
Romain Guyf6a11b82010-06-23 17:47:49 -07001416///////////////////////////////////////////////////////////////////////////////
1417// Drawing
1418///////////////////////////////////////////////////////////////////////////////
1419
Tom Hudson107843d2014-09-08 11:26:26 -04001420void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001421 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1422 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001423 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001424 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001425 renderNode->computeOrdering();
Chris Craik2507c342015-05-04 14:36:49 -07001426 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001427 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001428 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001429 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001430 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001431 }
1432
Chris Craik6fe991e52015-10-20 09:39:42 -07001433 DeferredDisplayList deferredList(mState.currentRenderTargetClip());
Chris Craikff785832013-03-08 13:12:16 -08001434 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001435 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001436
1437 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001438 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001439
Tom Hudson107843d2014-09-08 11:26:26 -04001440 deferredList.flush(*this, dirty);
1441 } else {
1442 // Even if there is no drawing command(Ex: invisible),
1443 // it still needs startFrame to clear buffer and start tiling.
1444 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001445 }
1446}
1447
Romain Guy03c00b52013-06-20 18:30:28 -07001448/**
1449 * Important note: this method is intended to draw batches of bitmaps and
1450 * will not set the scissor enable or dirty the current layer, if any.
1451 * The caller is responsible for properly dirtying the current layer.
1452 */
Tom Hudson107843d2014-09-08 11:26:26 -04001453void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001454 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1455 const Rect& bounds, const SkPaint* paint) {
Romain Guy55b6f952013-06-27 15:27:09 -07001456 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001457 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001458
Chris Craik527a3aa2013-03-04 10:19:31 -08001459 const AutoTexture autoCleanup(texture);
1460
Chris Craik82840732015-04-03 09:37:49 -07001461 // TODO: remove layer dirty in multi-draw callers
1462 // TODO: snap doesn't need to touch transform, only texture filter.
1463 bool snap = pureTranslate;
Chris Craika6b52192015-02-27 17:43:02 -08001464 const float x = floorf(bounds.left + 0.5f);
1465 const float y = floorf(bounds.top + 0.5f);
Chris Craik53e51e42015-06-01 10:35:35 -07001466
1467 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1468 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
1469 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001470 Glop glop;
1471 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001472 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001473 .setMeshTexturedMesh(vertices, bitmapCount * 6)
1474 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001475 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001476 .setModelViewOffsetRectOptionalSnap(snap, x, y, Rect(0, 0, bounds.getWidth(), bounds.getHeight()))
Chris Craik82840732015-04-03 09:37:49 -07001477 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001478 renderGlop(glop, GlopRenderType::Multi);
Chris Craik527a3aa2013-03-04 10:19:31 -08001479}
1480
Tom Hudson107843d2014-09-08 11:26:26 -04001481void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07001482 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04001483 return;
Romain Guy6926c722010-07-12 20:20:03 -07001484 }
1485
Chris Craik44eb2c02015-01-29 09:45:09 -08001486 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07001487 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001488 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001489 const AutoTexture autoCleanup(texture);
1490
Chris Craik53e51e42015-06-01 10:35:35 -07001491 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1492 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07001493 Glop glop;
1494 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001495 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001496 .setMeshTexturedUnitQuad(texture->uvMapper)
1497 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001498 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001499 .setModelViewMapUnitToRectSnap(Rect(0, 0, texture->width, texture->height))
Chris Craik82840732015-04-03 09:37:49 -07001500 .build();
1501 renderGlop(glop);
Romain Guyce0537b2010-06-29 21:05:21 -07001502}
1503
Tom Hudson107843d2014-09-08 11:26:26 -04001504void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08001505 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001506 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04001507 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08001508 }
1509
Romain Guyb18d2d02011-02-10 15:52:54 -08001510 float left = FLT_MAX;
1511 float top = FLT_MAX;
1512 float right = FLT_MIN;
1513 float bottom = FLT_MIN;
1514
Chris Craikef250742015-02-25 17:16:16 -08001515 const uint32_t elementCount = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001516
Chris Craikef250742015-02-25 17:16:16 -08001517 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[elementCount]);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001518 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08001519
Chris Craik51d6a3d2014-12-22 17:16:56 -08001520 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08001521 if (!colors) {
1522 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001523 tempColors.reset(new int[colorsCount]);
1524 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
1525 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08001526 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001527
John Reckebd52612014-12-10 16:47:36 -08001528 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07001529 const UvMapper& mapper(getMapper(texture));
1530
Romain Guy5a7b4662011-01-20 19:09:30 -08001531 for (int32_t y = 0; y < meshHeight; y++) {
1532 for (int32_t x = 0; x < meshWidth; x++) {
1533 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1534
1535 float u1 = float(x) / meshWidth;
1536 float u2 = float(x + 1) / meshWidth;
1537 float v1 = float(y) / meshHeight;
1538 float v2 = float(y + 1) / meshHeight;
1539
Romain Guy3b748a42013-04-17 18:54:38 -07001540 mapper.map(u1, v1, u2, v2);
1541
Romain Guy5a7b4662011-01-20 19:09:30 -08001542 int ax = i + (meshWidth + 1) * 2;
1543 int ay = ax + 1;
1544 int bx = i;
1545 int by = bx + 1;
1546 int cx = i + 2;
1547 int cy = cx + 1;
1548 int dx = i + (meshWidth + 1) * 2 + 2;
1549 int dy = dx + 1;
1550
Romain Guyff316ec2013-02-13 18:39:43 -08001551 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1552 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
1553 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08001554
Romain Guyff316ec2013-02-13 18:39:43 -08001555 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1556 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
1557 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08001558
Chris Craikdf72b632015-06-30 17:56:13 -07001559 left = std::min(left, std::min(vertices[ax], std::min(vertices[bx], vertices[cx])));
1560 top = std::min(top, std::min(vertices[ay], std::min(vertices[by], vertices[cy])));
1561 right = std::max(right, std::max(vertices[ax], std::max(vertices[bx], vertices[cx])));
1562 bottom = std::max(bottom, std::max(vertices[ay], std::max(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001563 }
1564 }
1565
Chris Craikf0a59072013-11-19 18:00:46 -08001566 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001567 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07001568 }
1569
Romain Guyff316ec2013-02-13 18:39:43 -08001570 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07001571 texture = mCaches.textureCache.get(bitmap);
1572 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04001573 return;
Romain Guy3b748a42013-04-17 18:54:38 -07001574 }
Romain Guyff316ec2013-02-13 18:39:43 -08001575 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001576 const AutoTexture autoCleanup(texture);
1577
Chris Craik82840732015-04-03 09:37:49 -07001578 /*
1579 * TODO: handle alpha_8 textures correctly by applying paint color, but *not*
1580 * shader in that case to mimic the behavior in SkiaCanvas::drawBitmapMesh.
1581 */
Chris Craik53e51e42015-06-01 10:35:35 -07001582 const int textureFillFlags = TextureFillFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07001583 Glop glop;
1584 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001585 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001586 .setMeshColoredTexturedMesh(mesh.get(), elementCount)
Chris Craik53e51e42015-06-01 10:35:35 -07001587 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1588 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001589 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07001590 .build();
1591 renderGlop(glop);
Romain Guy5a7b4662011-01-20 19:09:30 -08001592}
1593
Chris Craik14100ac2015-02-24 13:46:29 -08001594void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, Rect src, Rect dst, const SkPaint* paint) {
1595 if (quickRejectSetupScissor(dst)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001596 return;
Romain Guy6926c722010-07-12 20:20:03 -07001597 }
1598
Romain Guy3b748a42013-04-17 18:54:38 -07001599 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001600 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001601 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001602
Chris Craike6a15ee2015-07-07 18:42:17 -07001603 Rect uv(std::max(0.0f, src.left / texture->width),
1604 std::max(0.0f, src.top / texture->height),
1605 std::min(1.0f, src.right / texture->width),
1606 std::min(1.0f, src.bottom / texture->height));
Chris Craik14100ac2015-02-24 13:46:29 -08001607
Chris Craik53e51e42015-06-01 10:35:35 -07001608 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1609 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
Chris Craik1e4c8072015-05-26 14:25:02 -07001610 const bool tryToSnap = MathUtils::areEqual(src.getWidth(), dst.getWidth())
1611 && MathUtils::areEqual(src.getHeight(), dst.getHeight());
Chris Craik82840732015-04-03 09:37:49 -07001612 Glop glop;
1613 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001614 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001615 .setMeshTexturedUvQuad(texture->uvMapper, uv)
1616 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001617 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik1e4c8072015-05-26 14:25:02 -07001618 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, dst)
Chris Craik82840732015-04-03 09:37:49 -07001619 .build();
1620 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07001621}
1622
Tom Hudson107843d2014-09-08 11:26:26 -04001623void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08001624 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
1625 const SkPaint* paint) {
Chris Craika6b52192015-02-27 17:43:02 -08001626 if (!mesh || !mesh->verticesCount || quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001627 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07001628 }
1629
Chris Craika6b52192015-02-27 17:43:02 -08001630 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
1631 if (!texture) return;
Chris Craik0556d902015-03-03 09:54:14 -08001632
Chris Craik82840732015-04-03 09:37:49 -07001633 // 9 patches are built for stretching - always filter
Chris Craik53e51e42015-06-01 10:35:35 -07001634 int textureFillFlags = TextureFillFlags::ForceFilter;
Chris Craik82840732015-04-03 09:37:49 -07001635 if (bitmap->colorType() == kAlpha_8_SkColorType) {
Chris Craik53e51e42015-06-01 10:35:35 -07001636 textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture;
Chris Craik0556d902015-03-03 09:54:14 -08001637 }
Chris Craik82840732015-04-03 09:37:49 -07001638 Glop glop;
1639 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001640 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001641 .setMeshPatchQuads(*mesh)
1642 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001643 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001644 .setModelViewOffsetRectSnap(left, top, Rect(0, 0, right - left, bottom - top)) // TODO: get minimal bounds from patch
Chris Craik82840732015-04-03 09:37:49 -07001645 .build();
1646 renderGlop(glop);
Romain Guyf7f93552010-07-08 19:17:03 -07001647}
1648
Romain Guy03c00b52013-06-20 18:30:28 -07001649/**
1650 * Important note: this method is intended to draw batches of 9-patch objects and
1651 * will not set the scissor enable or dirty the current layer, if any.
1652 * The caller is responsible for properly dirtying the current layer.
1653 */
Tom Hudson107843d2014-09-08 11:26:26 -04001654void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craika6b52192015-02-27 17:43:02 -08001655 TextureVertex* vertices, uint32_t elementCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001656 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07001657 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001658 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07001659 const AutoTexture autoCleanup(texture);
1660
Chris Craik82840732015-04-03 09:37:49 -07001661 // TODO: get correct bounds from caller
Chris Craik53e51e42015-06-01 10:35:35 -07001662 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001663 // 9 patches are built for stretching - always filter
Chris Craik53e51e42015-06-01 10:35:35 -07001664 int textureFillFlags = TextureFillFlags::ForceFilter;
Chris Craik82840732015-04-03 09:37:49 -07001665 if (bitmap->colorType() == kAlpha_8_SkColorType) {
Chris Craik53e51e42015-06-01 10:35:35 -07001666 textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture;
Chris Craika6b52192015-02-27 17:43:02 -08001667 }
Chris Craik82840732015-04-03 09:37:49 -07001668 Glop glop;
1669 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001670 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001671 .setMeshTexturedIndexedQuads(vertices, elementCount)
1672 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001673 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001674 .setModelViewOffsetRect(0, 0, Rect(0, 0, 0, 0))
Chris Craik82840732015-04-03 09:37:49 -07001675 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001676 renderGlop(glop, GlopRenderType::Multi);
Romain Guy03c00b52013-06-20 18:30:28 -07001677}
1678
Tom Hudson107843d2014-09-08 11:26:26 -04001679void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07001680 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001681 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07001682 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08001683 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04001684 return;
Chris Craik65cd6122012-12-10 17:56:27 -08001685 }
1686
Chris Craik82840732015-04-03 09:37:49 -07001687 bool shadowInterp = displayFlags & kVertexBuffer_ShadowInterp;
Chris Craik53e51e42015-06-01 10:35:35 -07001688 const int transformFlags = TransformFlags::OffsetByFudgeFactor;
Chris Craik82840732015-04-03 09:37:49 -07001689 Glop glop;
1690 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001691 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001692 .setMeshVertexBuffer(vertexBuffer, shadowInterp)
1693 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001694 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001695 .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds())
Chris Craik82840732015-04-03 09:37:49 -07001696 .build();
1697 renderGlop(glop);
Chet Haase858aa932011-05-12 09:06:00 -07001698}
1699
1700/**
Chris Craik65cd6122012-12-10 17:56:27 -08001701 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
1702 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
1703 * screen space in all directions. However, instead of using a fragment shader to compute the
1704 * translucency of the color from its position, we simply use a varying parameter to define how far
1705 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
1706 *
1707 * Doesn't yet support joins, caps, or path effects.
1708 */
Tom Hudson107843d2014-09-08 11:26:26 -04001709void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08001710 VertexBuffer vertexBuffer;
1711 // TODO: try clipping large paths to viewport
Chris Craikfca52b752015-04-28 11:45:59 -07001712
Chris Craikd6b65f62014-01-01 14:45:21 -08001713 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04001714 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08001715}
1716
1717/**
1718 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
1719 * and additional geometry for defining an alpha slope perimeter.
1720 *
1721 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
1722 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
1723 * in-shader alpha region, but found it to be taxing on some GPUs.
1724 *
1725 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
1726 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07001727 */
Tom Hudson107843d2014-09-08 11:26:26 -04001728void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001729 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07001730
Chris Craik65cd6122012-12-10 17:56:27 -08001731 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07001732
Chris Craik65cd6122012-12-10 17:56:27 -08001733 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001734 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
1735 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08001736
Chris Craik05f3d6e2014-06-02 16:27:04 -07001737 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001738 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08001739 }
1740
Chris Craikbf759452014-08-11 16:00:44 -07001741 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001742 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07001743}
1744
Tom Hudson107843d2014-09-08 11:26:26 -04001745void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001746 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001747
Chris Craik6d29c8d2013-05-08 18:35:44 -07001748 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07001749
Chris Craik6d29c8d2013-05-08 18:35:44 -07001750 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001751 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08001752
Chris Craik05f3d6e2014-06-02 16:27:04 -07001753 const Rect& bounds = buffer.getBounds();
1754 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001755 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001756 }
1757
Chris Craikbf759452014-08-11 16:00:44 -07001758 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001759 drawVertexBuffer(buffer, paint, displayFlags);
1760
1761 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07001762}
1763
Tom Hudson107843d2014-09-08 11:26:26 -04001764void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001765 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04001766 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001767
Chris Craik6fe991e52015-10-20 09:39:42 -07001768 Rect clip(mState.currentRenderTargetClip());
Romain Guyae88e5e2010-10-22 17:49:18 -07001769 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001770
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001771 SkPaint paint;
1772 paint.setColor(color);
1773 paint.setXfermodeMode(mode);
1774
1775 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07001776
Tom Hudson107843d2014-09-08 11:26:26 -04001777 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07001778}
Romain Guy9d5316e2010-06-24 19:30:36 -07001779
Chris Craik30036092015-02-12 10:41:39 -08001780void OpenGLRenderer::drawShape(float left, float top, PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08001781 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04001782 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001783 const AutoTexture autoCleanup(texture);
1784
1785 const float x = left + texture->left - texture->offset;
1786 const float y = top + texture->top - texture->offset;
1787
1788 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07001789
Tom Hudson107843d2014-09-08 11:26:26 -04001790 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08001791}
1792
Tom Hudson107843d2014-09-08 11:26:26 -04001793void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001794 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001795 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001796 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001797 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001798 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001799 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001800
Chris Craike84a2082014-12-22 14:28:49 -08001801 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001802 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001803 PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07001804 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001805 drawShape(left, top, texture, p);
1806 } else {
1807 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
1808 *currentTransform(), *p, right - left, bottom - top, rx, ry);
1809 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001810 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001811}
1812
Tom Hudson107843d2014-09-08 11:26:26 -04001813void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001814 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001815 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001816 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001817 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001818 }
Chris Craikfca52b752015-04-28 11:45:59 -07001819
Chris Craike84a2082014-12-22 14:28:49 -08001820 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001821 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001822 PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001823 drawShape(x - radius, y - radius, texture, p);
Chris Craikfca52b752015-04-28 11:45:59 -07001824 return;
Chris Craikcb4d6002012-09-25 12:00:29 -07001825 }
Chris Craikfca52b752015-04-28 11:45:59 -07001826
1827 SkPath path;
1828 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1829 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
1830 } else {
1831 path.addCircle(x, y, radius);
1832 }
1833
1834 if (CC_UNLIKELY(currentSnapshot()->projectionPathMask != nullptr)) {
1835 // mask ripples with projection mask
1836 SkPath maskPath = *(currentSnapshot()->projectionPathMask->projectionMask);
1837
1838 Matrix4 screenSpaceTransform;
1839 currentSnapshot()->buildScreenSpaceTransform(&screenSpaceTransform);
1840
1841 Matrix4 totalTransform;
1842 totalTransform.loadInverse(screenSpaceTransform);
1843 totalTransform.multiply(currentSnapshot()->projectionPathMask->projectionMaskTransform);
1844
1845 SkMatrix skTotalTransform;
1846 totalTransform.copyTo(skTotalTransform);
1847 maskPath.transform(skTotalTransform);
1848
1849 // Mask the ripple path by the projection mask, now that it's
1850 // in local space. Note that this can create CCW paths.
Tom Hudson02a26302015-06-24 11:32:42 -04001851 Op(path, maskPath, kIntersect_SkPathOp, &path);
Chris Craikfca52b752015-04-28 11:45:59 -07001852 }
1853 drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001854}
Romain Guy01d58e42011-01-19 21:54:02 -08001855
Tom Hudson107843d2014-09-08 11:26:26 -04001856void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001857 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001858 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001859 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001860 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001861 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001862 }
Romain Guy01d58e42011-01-19 21:54:02 -08001863
Chris Craike84a2082014-12-22 14:28:49 -08001864 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001865 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001866 PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001867 drawShape(left, top, texture, p);
1868 } else {
1869 SkPath path;
1870 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1871 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1872 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1873 }
1874 path.addOval(rect);
1875 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001876 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001877}
1878
Tom Hudson107843d2014-09-08 11:26:26 -04001879void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001880 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001881 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001882 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001883 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001884 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08001885 }
1886
Chris Craik780c1282012-10-04 14:10:49 -07001887 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08001888 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001889 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001890 PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07001891 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001892 drawShape(left, top, texture, p);
1893 return;
Chris Craik780c1282012-10-04 14:10:49 -07001894 }
Chris Craik780c1282012-10-04 14:10:49 -07001895 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1896 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1897 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1898 }
1899
1900 SkPath path;
1901 if (useCenter) {
1902 path.moveTo(rect.centerX(), rect.centerY());
1903 }
1904 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
1905 if (useCenter) {
1906 path.close();
1907 }
Tom Hudson107843d2014-09-08 11:26:26 -04001908 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08001909}
1910
Romain Guycf8675e2012-10-02 12:32:25 -07001911// See SkPaintDefaults.h
1912#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
1913
Tom Hudson107843d2014-09-08 11:26:26 -04001914void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001915 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001916 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001917 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001918 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001919 return;
Romain Guy6926c722010-07-12 20:20:03 -07001920 }
1921
Chris Craik710f46d2012-09-17 17:25:49 -07001922 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07001923 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08001924 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07001925 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001926 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001927 PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07001928 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001929 drawShape(left, top, texture, p);
1930 } else {
1931 SkPath path;
1932 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1933 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1934 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1935 }
1936 path.addRect(rect);
1937 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07001938 }
Chet Haase858aa932011-05-12 09:06:00 -07001939 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04001940 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
1941 SkPath path;
1942 path.addRect(left, top, right, bottom);
1943 drawConvexPath(path, p);
1944 } else {
1945 drawColorRect(left, top, right, bottom, p);
1946
1947 mDirty = true;
1948 }
Chet Haase858aa932011-05-12 09:06:00 -07001949 }
Romain Guyc7d53492010-06-25 13:41:57 -07001950}
Romain Guy9d5316e2010-06-24 19:30:36 -07001951
Chris Craikd218a922014-01-02 17:13:34 -08001952void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
1953 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001954 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001955 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07001956
Chris Craikbf6f0f22015-10-01 12:36:07 -07001957 PaintUtils::TextShadow textShadow;
1958 if (!PaintUtils::getTextShadow(paint, &textShadow)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04001959 LOG_ALWAYS_FATAL("failed to query shadow attributes");
1960 }
1961
Raph Levien416a8472012-07-19 22:48:17 -07001962 // NOTE: The drop shadow will not perform gamma correction
1963 // if shader-based correction is enabled
1964 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Chris Craik2bb8f562015-02-17 16:42:02 -08001965 ShadowTexture* texture = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04001966 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07001967 // If the drop shadow exceeds the max texture size or couldn't be
1968 // allocated, skip drawing
Chris Craik2bb8f562015-02-17 16:42:02 -08001969 if (!texture) return;
1970 const AutoTexture autoCleanup(texture);
Raph Levien416a8472012-07-19 22:48:17 -07001971
Chris Craik2bb8f562015-02-17 16:42:02 -08001972 const float sx = x - texture->left + textShadow.dx;
1973 const float sy = y - texture->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07001974
Chris Craik82840732015-04-03 09:37:49 -07001975 Glop glop;
1976 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001977 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001978 .setMeshTexturedUnitQuad(nullptr)
1979 .setFillShadowTexturePaint(*texture, textShadow.color, *paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001980 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001981 .setModelViewMapUnitToRect(Rect(sx, sy, sx + texture->width, sy + texture->height))
Chris Craik82840732015-04-03 09:37:49 -07001982 .build();
1983 renderGlop(glop);
Raph Levien416a8472012-07-19 22:48:17 -07001984}
1985
Chris Craikbf6f0f22015-10-01 12:36:07 -07001986// TODO: remove this, once mState.currentlyIgnored captures snapshot alpha
Romain Guy768bffc2013-02-27 13:50:45 -08001987bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Chris Craikbf6f0f22015-10-01 12:36:07 -07001988 float alpha = (PaintUtils::hasTextShadow(paint)
1989 ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05001990 return MathUtils::isZero(alpha)
1991 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08001992}
1993
Tom Hudson107843d2014-09-08 11:26:26 -04001994void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08001995 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08001996 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001997 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08001998 }
Romain Guyeb9a5362012-01-17 17:39:26 -08001999
Romain Guy671d6cf2012-01-18 12:39:17 -08002000 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002001 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002002 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002003 }
2004
Chris Craik65fe5ee2015-01-26 18:06:29 -08002005 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002006
Romain Guy671d6cf2012-01-18 12:39:17 -08002007 float x = 0.0f;
2008 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002009 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002010 if (pureTranslate) {
Chris Craika6b52192015-02-27 17:43:02 -08002011 x = floorf(x + currentTransform()->getTranslateX() + 0.5f);
2012 y = floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002013 }
2014
Chris Craikc08820f2015-09-22 14:22:29 -07002015 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
Chris Craik59744b72014-07-01 17:56:52 -07002016 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002017
Chris Craikbf6f0f22015-10-01 12:36:07 -07002018 int alpha = PaintUtils::getAlphaDirect(paint) * currentSnapshot()->alpha;
2019 SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002020
Chris Craikbf6f0f22015-10-01 12:36:07 -07002021 if (CC_UNLIKELY(PaintUtils::hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002022 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002023 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002024 }
2025
Romain Guy671d6cf2012-01-18 12:39:17 -08002026 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002027 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002028 if (pureTranslate && !linearFilter) {
2029 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2030 }
Romain Guy257ae352013-03-20 16:31:12 -07002031 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002032
Chris Craik6fe991e52015-10-20 09:39:42 -07002033 const Rect& clip(pureTranslate ? writableSnapshot()->getRenderTargetClip() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002034 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2035
Chris Craik82840732015-04-03 09:37:49 -07002036 TextDrawFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002037 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craik083e7332015-02-27 17:04:20 -08002038 positions, hasLayer() ? &bounds : nullptr, &functor)) {
2039 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2040 mDirty = true;
Romain Guy671d6cf2012-01-18 12:39:17 -08002041 }
Chet Haase48659092012-05-31 15:21:51 -07002042
Romain Guyeb9a5362012-01-17 17:39:26 -08002043}
2044
Chris Craik59744b72014-07-01 17:56:52 -07002045bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002046 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002047 outMatrix->setIdentity();
2048 return false;
2049 } else if (CC_UNLIKELY(transform.isPerspective())) {
2050 outMatrix->setIdentity();
2051 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002052 }
Chris Craik59744b72014-07-01 17:56:52 -07002053
2054 /**
Chris Craika736cd92014-08-04 13:18:38 -07002055 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2056 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002057 */
2058 float sx, sy;
2059 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002060 outMatrix->setScale(
Chris Craikdf72b632015-06-30 17:56:13 -07002061 roundf(std::max(1.0f, sx)),
2062 roundf(std::max(1.0f, sy)));
Chris Craika736cd92014-08-04 13:18:38 -07002063 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002064}
2065
Tom Hudson984162f2014-10-10 13:38:16 -04002066int OpenGLRenderer::getSaveCount() const {
2067 return mState.getSaveCount();
2068}
2069
2070int OpenGLRenderer::save(int flags) {
2071 return mState.save(flags);
2072}
2073
2074void OpenGLRenderer::restore() {
Chris Craike2bb3802015-03-13 15:07:52 -07002075 mState.restore();
Tom Hudson984162f2014-10-10 13:38:16 -04002076}
2077
2078void OpenGLRenderer::restoreToCount(int saveCount) {
Chris Craike2bb3802015-03-13 15:07:52 -07002079 mState.restoreToCount(saveCount);
Tom Hudson984162f2014-10-10 13:38:16 -04002080}
2081
Tom Hudsonac7b6d32015-06-30 11:26:13 -04002082
Tom Hudson984162f2014-10-10 13:38:16 -04002083void OpenGLRenderer::translate(float dx, float dy, float dz) {
Chris Craike2bb3802015-03-13 15:07:52 -07002084 mState.translate(dx, dy, dz);
Tom Hudson984162f2014-10-10 13:38:16 -04002085}
2086
2087void OpenGLRenderer::rotate(float degrees) {
Chris Craike2bb3802015-03-13 15:07:52 -07002088 mState.rotate(degrees);
Tom Hudson984162f2014-10-10 13:38:16 -04002089}
2090
2091void OpenGLRenderer::scale(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002092 mState.scale(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002093}
2094
2095void OpenGLRenderer::skew(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002096 mState.skew(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002097}
2098
Chris Craik6daa13c2015-08-19 13:32:12 -07002099void OpenGLRenderer::setLocalMatrix(const Matrix4& matrix) {
2100 mState.setMatrix(mBaseTransform);
2101 mState.concatMatrix(matrix);
Tom Hudson984162f2014-10-10 13:38:16 -04002102}
2103
Tom Hudsonac7b6d32015-06-30 11:26:13 -04002104void OpenGLRenderer::setLocalMatrix(const SkMatrix& matrix) {
2105 mState.setMatrix(mBaseTransform);
2106 mState.concatMatrix(matrix);
2107}
2108
Tom Hudson984162f2014-10-10 13:38:16 -04002109void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2110 mState.concatMatrix(matrix);
2111}
2112
2113bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2114 return mState.clipRect(left, top, right, bottom, op);
2115}
2116
2117bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2118 return mState.clipPath(path, op);
2119}
2120
2121bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2122 return mState.clipRegion(region, op);
2123}
2124
2125void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2126 mState.setClippingOutline(allocator, outline);
2127}
2128
2129void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2130 const Rect& rect, float radius, bool highPriority) {
2131 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2132}
2133
Chris Craikfca52b752015-04-28 11:45:59 -07002134void OpenGLRenderer::setProjectionPathMask(LinearAllocator& allocator, const SkPath* path) {
2135 mState.setProjectionPathMask(allocator, path);
2136}
2137
Tom Hudson107843d2014-09-08 11:26:26 -04002138void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002139 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002140 DrawOpMode drawOpMode) {
2141
Chris Craik03ae2722015-02-25 11:01:09 -08002142 if (drawOpMode == DrawOpMode::kImmediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002143 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2144 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002145 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002146 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002147 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002148 }
Romain Guycac5fd32011-12-01 20:08:50 -08002149 }
2150
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002151 const float oldX = x;
2152 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002153
Chris Craikd6b65f62014-01-01 14:45:21 -08002154 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002155 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002156
Romain Guy211370f2012-02-01 16:10:55 -08002157 if (CC_LIKELY(pureTranslate)) {
Chris Craika6b52192015-02-27 17:43:02 -08002158 x = floorf(x + transform.getTranslateX() + 0.5f);
2159 y = floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002160 }
2161
Chris Craikbf6f0f22015-10-01 12:36:07 -07002162 int alpha = PaintUtils::getAlphaDirect(paint) * currentSnapshot()->alpha;
2163 SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(paint);
Romain Guy9d13fe252010-10-15 16:06:03 -07002164
Chris Craikc08820f2015-09-22 14:22:29 -07002165 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
Romain Guyc74f45a2013-02-26 19:10:14 -08002166
Chris Craikbf6f0f22015-10-01 12:36:07 -07002167 if (CC_UNLIKELY(PaintUtils::hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002168 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002169 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002170 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002171 }
2172
Romain Guy19d4dd82013-03-04 11:14:26 -08002173 const bool hasActiveLayer = hasLayer();
2174
Romain Guy624234f2013-03-05 16:43:31 -08002175 // We only pass a partial transform to the font renderer. That partial
2176 // matrix defines how glyphs are rasterized. Typically we want glyphs
2177 // to be rasterized at their final size on screen, which means the partial
2178 // matrix needs to take the scale factor into account.
2179 // When a partial matrix is used to transform glyphs during rasterization,
2180 // the mesh is generated with the inverse transform (in the case of scale,
2181 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2182 // apply the full transform matrix at draw time in the vertex shader.
2183 // Applying the full matrix in the shader is the easiest way to handle
2184 // rotation and perspective and allows us to always generated quads in the
2185 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002186 SkMatrix fontTransform;
2187 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2188 || fabs(y - (int) y) > 0.0f
2189 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002190 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002191 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002192
Romain Guy624234f2013-03-05 16:43:31 -08002193 // TODO: Implement better clipping for scaled/rotated text
Chris Craik6fe991e52015-10-20 09:39:42 -07002194 const Rect* clip = !pureTranslate ? nullptr : &mState.currentRenderTargetClip();
Chris Craik41541822013-05-03 16:35:54 -07002195 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 -07002196
Raph Levien996e57c2012-07-23 15:22:52 -07002197 bool status;
Chris Craik82840732015-04-03 09:37:49 -07002198 TextDrawFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002199
2200 // don't call issuedrawcommand, do it at end of batch
Chris Craik03ae2722015-02-25 11:01:09 -08002201 bool forceFinish = (drawOpMode != DrawOpMode::kDefer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002202 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002203 SkPaint paintCopy(*paint);
2204 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2205 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002206 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002207 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002208 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002209 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002210 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002211
Chris Craik03ae2722015-02-25 11:01:09 -08002212 if ((status || drawOpMode != DrawOpMode::kImmediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002213 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002214 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002215 }
Chris Craik41541822013-05-03 16:35:54 -07002216 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002217 }
Romain Guy694b5192010-07-21 21:33:20 -07002218
Chris Craike63f7c622013-10-17 10:30:55 -07002219 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002220
Tom Hudson107843d2014-09-08 11:26:26 -04002221 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002222}
2223
Tom Hudson107843d2014-09-08 11:26:26 -04002224void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002225 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002226 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002227 return;
Romain Guy03d58522012-02-24 17:54:07 -08002228 }
2229
Chris Craik39a908c2013-06-13 14:39:01 -07002230 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002231 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002232
Chris Craikc08820f2015-09-22 14:22:29 -07002233 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
Chris Craik59744b72014-07-01 17:56:52 -07002234 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002235 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002236
Chris Craikbf6f0f22015-10-01 12:36:07 -07002237 int alpha = PaintUtils::getAlphaDirect(paint) * currentSnapshot()->alpha;
2238 SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(paint);
Chris Craik82840732015-04-03 09:37:49 -07002239 TextDrawFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002240
Tom Hudson984162f2014-10-10 13:38:16 -04002241 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002242 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 -08002243
Romain Guy97771732012-02-28 18:17:02 -08002244 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chih-Hung Hsieh9ad6ac02015-02-27 16:11:36 -08002245 hOffset, vOffset, hasLayer() ? &bounds : nullptr, &functor)) {
Chris Craik083e7332015-02-27 17:04:20 -08002246 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2247 mDirty = true;
Romain Guy97771732012-02-28 18:17:02 -08002248 }
Romain Guy325740f2012-02-24 16:48:34 -08002249}
2250
Tom Hudson107843d2014-09-08 11:26:26 -04002251void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002252 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002253
Chris Craik44eb2c02015-01-29 09:45:09 -08002254 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002255
Chris Craik30036092015-02-12 10:41:39 -08002256 PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002257 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002258 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002259
Romain Guy8b55f372010-08-18 17:10:07 -07002260 const float x = texture->left - texture->offset;
2261 const float y = texture->top - texture->offset;
2262
Romain Guy01d58e42011-01-19 21:54:02 -08002263 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002264 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002265}
2266
Chris Craik3aadd602015-08-20 12:41:40 -07002267void OpenGLRenderer::drawLayer(Layer* layer) {
Romain Guy35643dd2012-09-18 15:40:58 -07002268 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002269 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002270 }
2271
Chris Craike84a2082014-12-22 14:28:49 -08002272 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002273 if (layer->isTextureLayer()) {
2274 transform = &layer->getTransform();
2275 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002276 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002277 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002278 }
2279 }
2280
Chris Craik39a908c2013-06-13 14:39:01 -07002281 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08002282 const bool rejected = mState.calculateQuickRejectForScissor(
Chris Craik3aadd602015-08-20 12:41:40 -07002283 0, 0, layer->layer.getWidth(), layer->layer.getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -08002284 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002285
2286 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002287 if (transform && !transform->isIdentity()) {
2288 restore();
2289 }
Tom Hudson107843d2014-09-08 11:26:26 -04002290 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002291 }
2292
Chris Craik62d307c2014-07-29 10:35:13 -07002293 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2294 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2295
Romain Guy5bb3c732012-11-29 17:52:58 -08002296 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002297
Chris Craik65fe5ee2015-01-26 18:06:29 -08002298 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08002299 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002300
Romain Guy211370f2012-02-01 16:10:55 -08002301 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002302 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002303 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2304 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002305 } else if (layer->mesh) {
Chris Craik82840732015-04-03 09:37:49 -07002306 Glop glop;
2307 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002308 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002309 .setMeshTexturedIndexedQuads(layer->mesh, layer->meshElementCount)
2310 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
Chris Craik53e51e42015-06-01 10:35:35 -07002311 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik3aadd602015-08-20 12:41:40 -07002312 .setModelViewOffsetRectSnap(0, 0, Rect(0, 0, layer->layer.getWidth(), layer->layer.getHeight()))
Chris Craik82840732015-04-03 09:37:49 -07002313 .build();
2314 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy3a3133d2011-02-01 22:59:58 -08002315#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07002316 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08002317#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002318 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002319
Romain Guy5bb3c732012-11-29 17:52:58 -08002320 if (layer->debugDrawUpdate) {
2321 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002322
2323 SkPaint paint;
2324 paint.setColor(0x7f00ff00);
Chris Craik3aadd602015-08-20 12:41:40 -07002325 drawColorRect(0, 0, layer->layer.getWidth(), layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002326 }
Romain Guyf219da52011-01-16 12:54:25 -08002327 }
Chris Craik34416ea2013-04-15 16:08:28 -07002328 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07002329
Romain Guyb2e2f242012-10-17 18:18:35 -07002330 if (transform && !transform->isIdentity()) {
2331 restore();
2332 }
2333
Tom Hudson107843d2014-09-08 11:26:26 -04002334 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08002335}
2336
Romain Guy6926c722010-07-12 20:20:03 -07002337///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002338// Draw filters
2339///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04002340void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
2341 // We should never get here since we apply the draw filter when stashing
2342 // the paints in the DisplayList.
2343 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08002344}
2345
2346///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002347// Drawing implementation
2348///////////////////////////////////////////////////////////////////////////////
2349
Chris Craikd218a922014-01-02 17:13:34 -08002350Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08002351 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002352 if (!texture) {
2353 return mCaches.textureCache.get(bitmap);
2354 }
2355 return texture;
2356}
2357
Chris Craik2bb8f562015-02-17 16:42:02 -08002358void OpenGLRenderer::drawPathTexture(PathTexture* texture, float x, float y,
2359 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002360 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08002361 return;
2362 }
2363
Chris Craik82840732015-04-03 09:37:49 -07002364 Glop glop;
2365 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002366 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002367 .setMeshTexturedUnitQuad(nullptr)
2368 .setFillPathTexturePaint(*texture, *paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002369 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07002370 .setModelViewMapUnitToRect(Rect(x, y, x + texture->width, y + texture->height))
Chris Craik82840732015-04-03 09:37:49 -07002371 .build();
2372 renderGlop(glop);
Romain Guy01d58e42011-01-19 21:54:02 -08002373}
2374
Romain Guyf607bdc2010-09-10 19:20:06 -07002375// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002376#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2377#define kStdUnderline_Offset (1.0f / 9.0f)
2378#define kStdUnderline_Thickness (1.0f / 18.0f)
2379
Chris Craikd218a922014-01-02 17:13:34 -08002380void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
2381 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07002382 // Handle underline and strike-through
2383 uint32_t flags = paint->getFlags();
2384 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002385 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002386
Romain Guy211370f2012-02-01 16:10:55 -08002387 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002388 const float textSize = paintCopy.getTextSize();
Chris Craike6a15ee2015-07-07 18:42:17 -07002389 const float strokeWidth = std::max(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002390
Raph Levien8b4072d2012-07-30 15:50:00 -07002391 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002392 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002393
Romain Guyf6834472011-01-23 13:32:12 -08002394 int linesCount = 0;
2395 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2396 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2397
2398 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002399 float points[pointsCount];
2400 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002401
2402 if (flags & SkPaint::kUnderlineText_Flag) {
2403 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002404 points[currentPoint++] = left;
2405 points[currentPoint++] = top;
2406 points[currentPoint++] = left + underlineWidth;
2407 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002408 }
2409
2410 if (flags & SkPaint::kStrikeThruText_Flag) {
2411 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002412 points[currentPoint++] = left;
2413 points[currentPoint++] = top;
2414 points[currentPoint++] = left + underlineWidth;
2415 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002416 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002417
Romain Guy726aeba2011-06-01 14:52:00 -07002418 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002419
Romain Guy726aeba2011-06-01 14:52:00 -07002420 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002421 }
2422 }
2423}
2424
Tom Hudson107843d2014-09-08 11:26:26 -04002425void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002426 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002427 return;
Romain Guy672433d2013-01-04 19:05:13 -08002428 }
2429
Tom Hudson107843d2014-09-08 11:26:26 -04002430 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08002431}
2432
Tom Hudson107843d2014-09-08 11:26:26 -04002433void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07002434 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04002435 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07002436
Chris Craik15a07a22014-01-26 13:43:53 -08002437 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08002438 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07002439
2440 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08002441 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07002442
ztenghui14a4e352014-08-13 10:44:39 -07002443 // The caller has made sure casterAlpha > 0.
2444 float ambientShadowAlpha = mAmbientShadowAlpha;
Chris Craik2507c342015-05-04 14:36:49 -07002445 if (CC_UNLIKELY(Properties::overrideAmbientShadowStrength >= 0)) {
2446 ambientShadowAlpha = Properties::overrideAmbientShadowStrength;
ztenghui14a4e352014-08-13 10:44:39 -07002447 }
2448 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
2449 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002450 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002451 }
ztenghui7b4516e2014-01-07 10:42:55 -08002452
ztenghui14a4e352014-08-13 10:44:39 -07002453 float spotShadowAlpha = mSpotShadowAlpha;
Chris Craik2507c342015-05-04 14:36:49 -07002454 if (CC_UNLIKELY(Properties::overrideSpotShadowStrength >= 0)) {
2455 spotShadowAlpha = Properties::overrideSpotShadowStrength;
ztenghui14a4e352014-08-13 10:44:39 -07002456 }
2457 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
2458 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002459 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002460 }
ztenghui7b4516e2014-01-07 10:42:55 -08002461
Tom Hudson107843d2014-09-08 11:26:26 -04002462 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07002463}
2464
Tom Hudson107843d2014-09-08 11:26:26 -04002465void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002466 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08002467 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04002468 return;
Romain Guy3b753822013-03-05 10:27:35 -08002469 }
Romain Guy735738c2012-12-03 12:34:51 -08002470
Romain Guy672433d2013-01-04 19:05:13 -08002471 float left = FLT_MAX;
2472 float top = FLT_MAX;
2473 float right = FLT_MIN;
2474 float bottom = FLT_MIN;
2475
Romain Guy448455f2013-07-22 13:57:50 -07002476 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08002477 Vertex* vertex = mesh;
2478
Chris Craik2af46352012-11-26 18:30:17 -08002479 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08002480 float l = rects[index + 0];
2481 float t = rects[index + 1];
2482 float r = rects[index + 2];
2483 float b = rects[index + 3];
2484
Romain Guy3b753822013-03-05 10:27:35 -08002485 Vertex::set(vertex++, l, t);
2486 Vertex::set(vertex++, r, t);
2487 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08002488 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08002489
Chris Craikdf72b632015-06-30 17:56:13 -07002490 left = std::min(left, l);
2491 top = std::min(top, t);
2492 right = std::max(right, r);
2493 bottom = std::max(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08002494 }
2495
Chris Craikf0a59072013-11-19 18:00:46 -08002496 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002497 return;
Romain Guya362c692013-02-04 13:50:16 -08002498 }
Romain Guy672433d2013-01-04 19:05:13 -08002499
Chris Craik53e51e42015-06-01 10:35:35 -07002500 const int transformFlags = ignoreTransform
2501 ? TransformFlags::MeshIgnoresCanvasTransform : TransformFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07002502 Glop glop;
2503 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002504 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002505 .setMeshIndexedQuads(&mesh[0], count / 4)
2506 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002507 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07002508 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07002509 .build();
2510 renderGlop(glop);
Romain Guy672433d2013-01-04 19:05:13 -08002511}
2512
Romain Guy026c5e162010-06-28 17:12:22 -07002513void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002514 const SkPaint* paint, bool ignoreTransform) {
Chris Craik53e51e42015-06-01 10:35:35 -07002515 const int transformFlags = ignoreTransform
2516 ? TransformFlags::MeshIgnoresCanvasTransform : TransformFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07002517 Glop glop;
2518 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002519 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002520 .setMeshUnitQuad()
2521 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002522 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07002523 .setModelViewMapUnitToRect(Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07002524 .build();
2525 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07002526}
2527
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002528float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik8df5ffa2015-04-28 17:47:20 -07002529 return (layer->getAlpha() / 255.0f) * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07002530}
2531
Romain Guy9d5316e2010-06-24 19:30:36 -07002532}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002533}; // namespace android