blob: 9f24e379e4181afde0dd8fe611654a1b052dca29 [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
Rob Tsuk487a92c2015-01-06 13:22:54 -0800226 Rect clip(mState.currentClipRect());
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
Rob Tsuk487a92c2015-01-06 13:22:54 -0800491 if (bounds.intersect(mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700492 // We cannot work with sub-pixels in this case
493 bounds.snapToPixelBoundaries();
494
495 // When the layer is not an FBO, we may use glCopyTexImage so we
496 // need to make sure the layer does not extend outside the bounds
497 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700498 const Snapshot& previous = *(currentSnapshot()->previous);
499 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
500 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700501 bounds.setEmpty();
502 } else if (fboLayer) {
503 clip.set(bounds);
504 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800505 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700506 inverse.mapRect(clip);
507 clip.snapToPixelBoundaries();
508 if (clip.intersect(untransformedBounds)) {
509 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
510 bounds.set(untransformedBounds);
511 } else {
512 clip.setEmpty();
513 }
514 }
515 } else {
516 bounds.setEmpty();
517 }
518}
519
Chris Craik408eb122013-03-26 18:55:15 -0700520void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
521 bool fboLayer, int alpha) {
522 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
523 bounds.getHeight() > mCaches.maxTextureSize ||
524 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400525 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700526 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400527 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700528 }
529}
530
Chris Craikd90144d2013-03-19 15:03:48 -0700531int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500532 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400533 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700534
Tom Hudson984162f2014-10-10 13:38:16 -0400535 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700536 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
537 // operations will be able to store and restore the current clip and transform info, and
538 // quick rejection will be correct (for display lists)
539
540 Rect bounds(left, top, right, bottom);
541 Rect clip;
542 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craikbf6f0f22015-10-01 12:36:07 -0700543 updateSnapshotIgnoreForLayer(bounds, clip, true, PaintUtils::getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700544
Tom Hudson984162f2014-10-10 13:38:16 -0400545 if (!mState.currentlyIgnored()) {
546 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
547 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
548 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800549 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700550 }
551 }
552
553 return count;
554}
555
Romain Guy1c740bc2010-09-13 18:00:09 -0700556/**
557 * Layers are viewed by Skia are slightly different than layers in image editing
558 * programs (for instance.) When a layer is created, previously created layers
559 * and the frame buffer still receive every drawing command. For instance, if a
560 * layer is created and a shape intersecting the bounds of the layers and the
561 * framebuffer is draw, the shape will be drawn on both (unless the layer was
562 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
563 *
564 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
565 * texture. Unfortunately, this is inefficient as it requires every primitive to
566 * be drawn n + 1 times, where n is the number of active layers. In practice this
567 * means, for every primitive:
568 * - Switch active frame buffer
569 * - Change viewport, clip and projection matrix
570 * - Issue the drawing
571 *
572 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700573 * To avoid this, layers are implemented in a different way here, at least in the
574 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
575 * is set. When this flag is set we can redirect all drawing operations into a
576 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700577 *
578 * This implementation relies on the frame buffer being at least RGBA 8888. When
579 * a layer is created, only a texture is created, not an FBO. The content of the
580 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700581 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700582 * buffer and drawing continues as normal. This technique therefore treats the
583 * frame buffer as a scratch buffer for the layers.
584 *
585 * To compose the layers back onto the frame buffer, each layer texture
586 * (containing the original frame buffer data) is drawn as a simple quad over
587 * the frame buffer. The trick is that the quad is set as the composition
588 * destination in the blending equation, and the frame buffer becomes the source
589 * of the composition.
590 *
591 * Drawing layers with an alpha value requires an extra step before composition.
592 * An empty quad is drawn over the layer's region in the frame buffer. This quad
593 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
594 * quad is used to multiply the colors in the frame buffer. This is achieved by
595 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
596 * GL_ZERO, GL_SRC_ALPHA.
597 *
598 * Because glCopyTexImage2D() can be slow, an alternative implementation might
599 * be use to draw a single clipped layer. The implementation described above
600 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700601 *
602 * (1) The frame buffer is actually not cleared right away. To allow the GPU
603 * to potentially optimize series of calls to glCopyTexImage2D, the frame
604 * buffer is left untouched until the first drawing operation. Only when
605 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700606 */
Chet Haased48885a2012-08-28 17:43:28 -0700607bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700608 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800609 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
610 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700611
Romain Guyeb993562010-10-05 18:14:38 -0700612 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
613
Romain Guyf607bdc2010-09-10 19:20:06 -0700614 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700615 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700616 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700617 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craikbf6f0f22015-10-01 12:36:07 -0700618 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, PaintUtils::getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700619
620 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400621 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700622 return false;
623 }
Romain Guyf18fd992010-07-08 11:45:51 -0700624
Chris Craik44eb2c02015-01-29 09:45:09 -0800625 mCaches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700626 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700627 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700628 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700629 }
630
Derek Sollenberger674554f2014-02-19 16:47:32 +0000631 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700632 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700633 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
634 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500635
Chet Haasea23eed82012-04-12 15:19:04 -0700636 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700637 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700638 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700639
Romain Guy8fb95422010-08-17 18:38:51 -0700640 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400641 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
642 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700643
Chris Craika8bea8e2014-09-24 11:29:43 -0700644 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
645 fboLayer ? "" : "unclipped ",
646 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700647 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700648 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700649 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700650 } else {
651 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700652 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800653 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700654 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700655 // Workaround for some GL drivers. When reading pixels lying outside
656 // of the window we should get undefined values for those pixels.
657 // Unfortunately some drivers will turn the entire target texture black
658 // when reading outside of the window.
659 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800660 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700661 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800662 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700663
Chris Craika64a2be2014-05-14 14:17:01 -0700664 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
665 bounds.left, getViewportHeight() - bounds.bottom,
666 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700667
Romain Guy54be1cd2011-06-13 19:04:27 -0700668 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800669 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700670 }
Romain Guyeb993562010-10-05 18:14:38 -0700671 }
Romain Guyf86ef572010-07-01 11:05:42 -0700672
Romain Guyd55a8612010-06-28 17:42:46 -0700673 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700674}
675
Chris Craike63f7c622013-10-17 10:30:55 -0700676bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800677 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700678 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700679
Tom Hudson984162f2014-10-10 13:38:16 -0400680 writableSnapshot()->region = &writableSnapshot()->layer->region;
681 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
682 writableSnapshot()->fbo = layer->getFbo();
683 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
684 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
685 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800686 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700687
Romain Guy7c450aa2012-09-21 19:15:00 -0700688 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700689 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700690 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700691 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700692
693 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700694 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700695 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700696 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700697 }
698
699 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Chris Craikf27133d2015-02-19 09:51:53 -0800700 layer->getTextureId(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700701
Romain Guy5b3b3522010-10-27 18:57:51 -0700702 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800703 mRenderState.scissor().setEnabled(true);
704 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700705 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700706 glClear(GL_COLOR_BUFFER_BIT);
707
708 dirtyClip();
709
710 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700711 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700712 return true;
713}
714
Romain Guy1c740bc2010-09-13 18:00:09 -0700715/**
716 * Read the documentation of createLayer() before doing anything in this method.
717 */
Chris Craik14e51302013-12-30 15:32:54 -0800718void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
719 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000720 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700721 return;
722 }
723
Chris Craik14e51302013-12-30 15:32:54 -0800724 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800725 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800726 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700727
Chris Craik39a908c2013-06-13 14:39:01 -0700728 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400729 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800730 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800731 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700732
Romain Guyeb993562010-10-05 18:14:38 -0700733 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700734 // Detach the texture from the FBO
735 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800736
737 layer->removeFbo(false);
738
Romain Guyeb993562010-10-05 18:14:38 -0700739 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700740 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700741 debugOverdraw(true, false);
Romain Guyeb993562010-10-05 18:14:38 -0700742 }
743
Romain Guy9ace8f52011-07-07 20:50:11 -0700744 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500745 SkPaint layerPaint;
746 layerPaint.setAlpha(layer->getAlpha());
747 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
748 layerPaint.setColorFilter(layer->getColorFilter());
749
750 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700751 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700752 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700753 }
754
Chris Craik96a5c4c2015-01-27 15:46:35 -0800755 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700756
Chris Craik44eb2c02015-01-29 09:45:09 -0800757 mCaches.textureState().activateTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700758
Romain Guy5b3b3522010-10-27 18:57:51 -0700759 // When the layer is stored in an FBO, we can save a bit of fillrate by
760 // drawing only the dirty region
761 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800762 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700763 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700764 } else if (!rect.isEmpty()) {
765 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530766
767 save(0);
768 // the layer contains screen buffer content that shouldn't be alpha modulated
769 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400770 writableSnapshot()->alpha = 1.0f;
Chris Craik53e51e42015-06-01 10:35:35 -0700771 composeLayerRectSwapped(layer, rect);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530772 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700773 }
Romain Guy8b55f372010-08-18 17:10:07 -0700774
Romain Guy746b7402010-10-26 16:27:31 -0700775 dirtyClip();
776
Romain Guyeb993562010-10-05 18:14:38 -0700777 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800778 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700779 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800780 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800781 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700782 }
783}
784
Romain Guyaa6c24c2011-04-28 18:40:04 -0700785void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craik1e4c8072015-05-26 14:25:02 -0700786 const bool tryToSnap = !layer->getForceFilter()
Chris Craik26bf3422015-02-26 16:28:17 -0800787 && layer->getWidth() == (uint32_t) rect.getWidth()
Chris Craik82840732015-04-03 09:37:49 -0700788 && layer->getHeight() == (uint32_t) rect.getHeight();
789 Glop glop;
790 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700791 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -0700792 .setMeshTexturedUvQuad(nullptr, Rect(0, 1, 1, 0)) // TODO: simplify with VBO
793 .setFillTextureLayer(*layer, getLayerAlpha(layer))
Chris Craik53e51e42015-06-01 10:35:35 -0700794 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik1e4c8072015-05-26 14:25:02 -0700795 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, rect)
Chris Craik82840732015-04-03 09:37:49 -0700796 .build();
797 renderGlop(glop);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700798}
799
Chris Craik53e51e42015-06-01 10:35:35 -0700800void OpenGLRenderer::composeLayerRectSwapped(Layer* layer, const Rect& rect) {
801 Glop glop;
802 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700803 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik53e51e42015-06-01 10:35:35 -0700804 .setMeshTexturedUvQuad(nullptr, layer->texCoords)
805 .setFillLayer(layer->getTexture(), layer->getColorFilter(),
806 getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::Swap)
807 .setTransform(*currentSnapshot(), TransformFlags::MeshIgnoresCanvasTransform)
808 .setModelViewMapUnitToRect(rect)
Chris Craik53e51e42015-06-01 10:35:35 -0700809 .build();
810 renderGlop(glop);
811}
812
813void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect) {
Chris Craik62d307c2014-07-29 10:35:13 -0700814 if (layer->isTextureLayer()) {
815 EVENT_LOGD("composeTextureLayerRect");
Chris Craik62d307c2014-07-29 10:35:13 -0700816 drawTextureLayer(layer, rect);
Chris Craik62d307c2014-07-29 10:35:13 -0700817 } else {
818 EVENT_LOGD("composeHardwareLayerRect");
Chris Craik182952f2015-03-09 14:17:29 -0700819
Chris Craik53e51e42015-06-01 10:35:35 -0700820 const bool tryToSnap = layer->getWidth() == static_cast<uint32_t>(rect.getWidth())
Chris Craik82840732015-04-03 09:37:49 -0700821 && layer->getHeight() == static_cast<uint32_t>(rect.getHeight());
822 Glop glop;
823 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700824 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -0700825 .setMeshTexturedUvQuad(nullptr, layer->texCoords)
Chris Craik53e51e42015-06-01 10:35:35 -0700826 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
827 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik1e4c8072015-05-26 14:25:02 -0700828 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, rect)
Chris Craik82840732015-04-03 09:37:49 -0700829 .build();
830 renderGlop(glop);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700831 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700832}
833
Chris Craik34416ea2013-04-15 16:08:28 -0700834/**
835 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
836 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
837 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
838 * by saveLayer's restore
839 */
Chris Craik2507c342015-05-04 14:36:49 -0700840#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
841 DRAW_COMMAND; \
842 if (CC_UNLIKELY(Properties::debugOverdraw && getTargetFbo() == 0 && COND)) { \
843 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
844 DRAW_COMMAND; \
845 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
846 } \
Chris Craik34416ea2013-04-15 16:08:28 -0700847 }
848
849#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
850
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400851// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
852// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
853class LayerShader : public SkShader {
854public:
855 LayerShader(Layer* layer, const SkMatrix* localMatrix)
856 : INHERITED(localMatrix)
857 , mLayer(layer) {
858 }
859
Chris Craike84a2082014-12-22 14:28:49 -0800860 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400861 if (data) {
862 *data = static_cast<void*>(mLayer);
863 }
864 return true;
865 }
866
Chris Craike84a2082014-12-22 14:28:49 -0800867 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400868 return !mLayer->isBlend();
869 }
870
871protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000872 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400873 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
874 }
875
Chris Craike84a2082014-12-22 14:28:49 -0800876 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400877 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
878 }
879
Chris Craike84a2082014-12-22 14:28:49 -0800880 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400881 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -0800882 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400883 }
884private:
885 // Unowned.
886 Layer* mLayer;
887 typedef SkShader INHERITED;
888};
889
Romain Guy5b3b3522010-10-27 18:57:51 -0700890void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -0700891 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
892
893 if (layer->getConvexMask()) {
894 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
895
896 // clip to the area of the layer the mask can be larger
897 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
898
899 SkPaint paint;
900 paint.setAntiAlias(true);
901 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
902
Chris Craik3f0854292014-04-15 16:18:08 -0700903 // create LayerShader to map SaveLayer content into subsequent draw
904 SkMatrix shaderMatrix;
905 shaderMatrix.setTranslate(rect.left, rect.bottom);
906 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400907 LayerShader layerShader(layer, &shaderMatrix);
908 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -0700909
910 // Since the drawing primitive is defined in local drawing space,
911 // we don't need to modify the draw matrix
912 const SkPath* maskPath = layer->getConvexMask();
913 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
914
Chris Craike84a2082014-12-22 14:28:49 -0800915 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -0700916 restore();
917
918 return;
919 }
920
Romain Guy5b3b3522010-10-27 18:57:51 -0700921 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700922 layer->setRegionAsRect();
923
Chris Craik34416ea2013-04-15 16:08:28 -0700924 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -0700925
Romain Guy5b3b3522010-10-27 18:57:51 -0700926 layer->region.clear();
927 return;
928 }
929
Chris Craik62d307c2014-07-29 10:35:13 -0700930 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -0700931 // standard Region based draw
932 size_t count;
933 const android::Rect* rects;
934 Region safeRegion;
935 if (CC_LIKELY(hasRectToRectTransform())) {
936 rects = layer->region.getArray(&count);
937 } else {
938 safeRegion = Region::createTJunctionFreeRegion(layer->region);
939 rects = safeRegion.getArray(&count);
940 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700941
Chris Craik3f0854292014-04-15 16:18:08 -0700942 const float texX = 1.0f / float(layer->getWidth());
943 const float texY = 1.0f / float(layer->getHeight());
944 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700945
Chris Craik82840732015-04-03 09:37:49 -0700946 TextureVertex quadVertices[count * 4];
947 TextureVertex* mesh = &quadVertices[0];
Chris Craik3f0854292014-04-15 16:18:08 -0700948 for (size_t i = 0; i < count; i++) {
949 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -0700950
Chris Craik3f0854292014-04-15 16:18:08 -0700951 const float u1 = r->left * texX;
952 const float v1 = (height - r->top) * texY;
953 const float u2 = r->right * texX;
954 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700955
Chris Craik3f0854292014-04-15 16:18:08 -0700956 // TODO: Reject quads outside of the clip
957 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
958 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
959 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
960 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Chris Craik3f0854292014-04-15 16:18:08 -0700961 }
Tom Hudson040b6d82015-04-16 10:50:53 -0400962 Rect modelRect = Rect(rect.getWidth(), rect.getHeight());
Chris Craik82840732015-04-03 09:37:49 -0700963 Glop glop;
964 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700965 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -0700966 .setMeshTexturedIndexedQuads(&quadVertices[0], count * 6)
967 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
Chris Craik53e51e42015-06-01 10:35:35 -0700968 .setTransform(*currentSnapshot(), TransformFlags::None)
Tom Hudson040b6d82015-04-16 10:50:53 -0400969 .setModelViewOffsetRectSnap(rect.left, rect.top, modelRect)
Chris Craik82840732015-04-03 09:37:49 -0700970 .build();
971 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy5b3b3522010-10-27 18:57:51 -0700972
Romain Guy5b3b3522010-10-27 18:57:51 -0700973#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -0700974 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700975#endif
976
Chris Craik3f0854292014-04-15 16:18:08 -0700977 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -0700978}
979
Romain Guy3a3133d2011-02-01 22:59:58 -0800980#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -0700981void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -0800982 size_t count;
983 const android::Rect* rects = region.getArray(&count);
984
985 uint32_t colors[] = {
986 0x7fff0000, 0x7f00ff00,
987 0x7f0000ff, 0x7fff00ff,
988 };
989
990 int offset = 0;
991 int32_t top = rects[0].top;
992
993 for (size_t i = 0; i < count; i++) {
994 if (top != rects[i].top) {
995 offset ^= 0x2;
996 top = rects[i].top;
997 }
998
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500999 SkPaint paint;
1000 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001001 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001002 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001003 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001004}
Chris Craike63f7c622013-10-17 10:30:55 -07001005#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001006
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001007void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001008 Vector<float> rects;
1009
1010 SkRegion::Iterator it(region);
1011 while (!it.done()) {
1012 const SkIRect& r = it.rect();
1013 rects.push(r.fLeft);
1014 rects.push(r.fTop);
1015 rects.push(r.fRight);
1016 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001017 it.next();
1018 }
1019
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001020 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001021}
1022
Romain Guy5b3b3522010-10-27 18:57:51 -07001023void OpenGLRenderer::dirtyLayer(const float left, const float top,
Chris Craik083e7332015-02-27 17:04:20 -08001024 const float right, const float bottom, const Matrix4& transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001025 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001026 Rect bounds(left, top, right, bottom);
1027 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001028 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001029 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001030}
1031
1032void OpenGLRenderer::dirtyLayer(const float left, const float top,
1033 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001034 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001035 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001036 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001037 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001038}
1039
1040void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Chris Craik3375f8a2015-06-23 17:33:06 -07001041 if (CC_LIKELY(!bounds.isEmpty() && bounds.intersect(mState.currentClipRect()))) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001042 bounds.snapToPixelBoundaries();
1043 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1044 if (!dirty.isEmpty()) {
1045 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001046 }
1047 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001048}
1049
Romain Guy54be1cd2011-06-13 19:04:27 -07001050void OpenGLRenderer::clearLayerRegions() {
Chris Craik2bb8f562015-02-17 16:42:02 -08001051 const size_t quadCount = mLayers.size();
1052 if (quadCount == 0) return;
Romain Guy54be1cd2011-06-13 19:04:27 -07001053
Tom Hudson984162f2014-10-10 13:38:16 -04001054 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001055 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001056 // Doing several glScissor/glClear here can negatively impact
1057 // GPUs with a tiler architecture, instead we draw quads with
1058 // the Clear blending mode
1059
1060 // The list contains bounds that have already been clipped
1061 // against their initial clip rect, and the current clip
1062 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001063 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001064
Chris Craik2bb8f562015-02-17 16:42:02 -08001065 Vertex mesh[quadCount * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001066 Vertex* vertex = mesh;
1067
Chris Craik2bb8f562015-02-17 16:42:02 -08001068 for (uint32_t i = 0; i < quadCount; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001069 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001070
Chris Craik51d6a3d2014-12-22 17:16:56 -08001071 Vertex::set(vertex++, bounds.left, bounds.top);
1072 Vertex::set(vertex++, bounds.right, bounds.top);
1073 Vertex::set(vertex++, bounds.left, bounds.bottom);
1074 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001075 }
Romain Guye67307c2013-02-11 18:01:20 -08001076 // We must clear the list of dirty rects before we
Chris Craik82840732015-04-03 09:37:49 -07001077 // call clearLayerRegions() in renderGlop to prevent
1078 // stencil setup from doing the same thing again
Romain Guye67307c2013-02-11 18:01:20 -08001079 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001080
Chris Craik53e51e42015-06-01 10:35:35 -07001081 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001082 Glop glop;
1083 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001084 .setRoundRectClipState(nullptr) // clear ignores clip state
Chris Craik82840732015-04-03 09:37:49 -07001085 .setMeshIndexedQuads(&mesh[0], quadCount)
1086 .setFillClear()
Chris Craik53e51e42015-06-01 10:35:35 -07001087 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001088 .setModelViewOffsetRect(0, 0, Rect(currentSnapshot()->getClipRect()))
Chris Craik82840732015-04-03 09:37:49 -07001089 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001090 renderGlop(glop, GlopRenderType::LayerClear);
Romain Guy8a4ac612012-07-17 17:32:48 -07001091
Chris Craik65fe5ee2015-01-26 18:06:29 -08001092 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001093 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001094 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001095 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001096}
1097
Romain Guybd6b79b2010-06-26 00:13:53 -07001098///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001099// State Deferral
1100///////////////////////////////////////////////////////////////////////////////
1101
Chris Craikff785832013-03-08 13:12:16 -08001102bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001103 const Rect& currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001104 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001105
Chris Craikff785832013-03-08 13:12:16 -08001106 if (stateDeferFlags & kStateDeferFlag_Draw) {
1107 // state has bounds initialized in local coordinates
1108 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001109 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001110 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001111 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1112 // is used, it should more closely duplicate the quickReject logic (in how it uses
1113 // snapToPixelBoundaries)
1114
Rob Tsuk487a92c2015-01-06 13:22:54 -08001115 if (!clippedBounds.intersect(currentClip)) {
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() {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001190 Rect clip(mState.currentClipRect());
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);
1245 if (!bounds.intersect(scissorBox)) {
1246 bounds.setEmpty();
1247 } else {
1248 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1249 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1250 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1251 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1252 }
1253 } else {
1254 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1255 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1256 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1257 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1258 }
1259 }
1260
Chris Craik65fe5ee2015-01-26 18:06:29 -08001261 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001262 scissorBox.getWidth(), scissorBox.getHeight());
Chris Craik53e51e42015-06-01 10:35:35 -07001263 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001264 Glop glop;
1265 Vertex* vertices = &rectangleVertices[0];
1266 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001267 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001268 .setMeshIndexedQuads(vertices, rectangleVertices.size() / 4)
1269 .setFillBlack()
Chris Craik53e51e42015-06-01 10:35:35 -07001270 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001271 .setModelViewOffsetRect(0, 0, scissorBox)
Chris Craik82840732015-04-03 09:37:49 -07001272 .build();
1273 renderGlop(glop);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001274}
1275
Romain Guy8ce00302013-01-15 18:51:42 -08001276void OpenGLRenderer::setStencilFromClip() {
Chris Craik2507c342015-05-04 14:36:49 -07001277 if (!Properties::debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001278 if (!currentSnapshot()->clipIsSimple()) {
1279 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001280 EVENT_LOGD("setStencilFromClip - enabling");
1281
Romain Guy8ce00302013-01-15 18:51:42 -08001282 // NOTE: The order here is important, we must set dirtyClip to false
1283 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001284 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001285
1286 ensureStencilBuffer();
1287
Rob Tsuk487a92c2015-01-06 13:22:54 -08001288 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001289
Rob Tsuk487a92c2015-01-06 13:22:54 -08001290 bool isRectangleList = clipArea.isRectangleList();
1291 if (isRectangleList) {
1292 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1293 } else {
1294 incrementThreshold = 0;
1295 }
1296
Chris Craik96a5c4c2015-01-27 15:46:35 -08001297 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001298
1299 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001300 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001301 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001302 if (resetScissor) {
1303 // The scissor was not set so we now need to update it
1304 setScissorFromClip();
1305 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001306
Chris Craik96a5c4c2015-01-27 15:46:35 -08001307 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001308
1309 // stash and disable the outline clip state, since stencil doesn't account for outline
1310 bool storedSkipOutlineClip = mSkipOutlineClip;
1311 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001312
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001313 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001314 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001315 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1316
Rob Tsuk487a92c2015-01-06 13:22:54 -08001317 if (isRectangleList) {
1318 drawRectangleList(clipArea.getRectangleList());
1319 } else {
1320 // NOTE: We could use the region contour path to generate a smaller mesh
1321 // Since we are using the stencil we could use the red book path
1322 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001323
Rob Tsuk487a92c2015-01-06 13:22:54 -08001324 // The last parameter is important: we are not drawing in the color buffer
1325 // so we don't want to dirty the current layer, if any
1326 drawRegionRects(clipArea.getClipRegion(), paint, false);
1327 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001328 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001329 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001330
Chris Craik96a5c4c2015-01-27 15:46:35 -08001331 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001332
1333 // Draw the region used to generate the stencil if the appropriate debug
1334 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001335 // TODO: Implement for rectangle list clip areas
Chris Craik2507c342015-05-04 14:36:49 -07001336 if (Properties::debugStencilClip == StencilClipDebug::ShowRegion
1337 && !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001338 paint.setColor(0x7f0000ff);
1339 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001340 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001341 }
Romain Guy8ce00302013-01-15 18:51:42 -08001342 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001343 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001344 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001345 }
1346 }
1347}
1348
Chris Craikf0a59072013-11-19 18:00:46 -08001349/**
1350 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1351 *
1352 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1353 * style, and tessellated AA ramp
1354 */
1355bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001356 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001357 bool snapOut = paint && paint->isAntiAlias();
1358
1359 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1360 float outset = paint->getStrokeWidth() * 0.5f;
1361 left -= outset;
1362 top -= outset;
1363 right += outset;
1364 bottom += outset;
1365 }
1366
Chris Craikdeeda3d2014-05-05 19:09:33 -07001367 bool clipRequired = false;
1368 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001369 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001370 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001371 return true;
1372 }
1373
Chris Craikf23b25a2014-06-26 15:46:20 -07001374 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001375 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001376 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001377 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001378}
1379
Romain Guy8ce00302013-01-15 18:51:42 -08001380void OpenGLRenderer::debugClip() {
1381#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001382 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001383 SkPaint paint;
1384 paint.setColor(0x7f00ff00);
1385 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1386
Romain Guy8ce00302013-01-15 18:51:42 -08001387 }
1388#endif
1389}
1390
Chris Craik3375f8a2015-06-23 17:33:06 -07001391void OpenGLRenderer::renderGlop(const Glop& glop, GlopRenderType type) {
Chris Craik6b109c72015-02-27 10:55:28 -08001392 // TODO: It would be best if we could do this before quickRejectSetupScissor()
1393 // changes the scissor test state
Chris Craik3375f8a2015-06-23 17:33:06 -07001394 if (type != GlopRenderType::LayerClear) {
1395 // Regular draws need to clear the dirty area on the layer before they start drawing on top
1396 // of it. If this draw *is* a layer clear, it skips the clear step (since it would
1397 // infinitely recurse)
1398 clearLayerRegions();
1399 }
Chris Craik6b109c72015-02-27 10:55:28 -08001400
Chris Craik117bdbc2015-02-05 10:12:38 -08001401 if (mState.getDirtyClip()) {
1402 if (mRenderState.scissor().isEnabled()) {
1403 setScissorFromClip();
1404 }
1405
1406 setStencilFromClip();
1407 }
Chris Craik12efe642015-09-28 15:52:12 -07001408 mRenderState.render(glop, currentSnapshot()->getOrthoMatrix());
Chris Craik3375f8a2015-06-23 17:33:06 -07001409 if (type == GlopRenderType::Standard && !mRenderState.stencil().isWriteEnabled()) {
Chris Craik2ab95d72015-02-06 15:25:51 -08001410 // TODO: specify more clearly when a draw should dirty the layer.
1411 // is writing to the stencil the only time we should ignore this?
1412 dirtyLayer(glop.bounds.left, glop.bounds.top, glop.bounds.right, glop.bounds.bottom);
Chris Craik0519c8102015-02-11 13:17:06 -08001413 mDirty = true;
Chris Craik2ab95d72015-02-06 15:25:51 -08001414 }
Chris Craik117bdbc2015-02-05 10:12:38 -08001415}
1416
Romain Guyf6a11b82010-06-23 17:47:49 -07001417///////////////////////////////////////////////////////////////////////////////
1418// Drawing
1419///////////////////////////////////////////////////////////////////////////////
1420
Tom Hudson107843d2014-09-08 11:26:26 -04001421void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001422 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1423 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001424 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001425 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001426 renderNode->computeOrdering();
Chris Craik2507c342015-05-04 14:36:49 -07001427 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001428 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001429 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001430 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001431 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001432 }
1433
Chris Craikb45c6aa2015-09-28 15:41:27 -07001434 DeferredDisplayList deferredList(mState.currentClipRect());
Chris Craikff785832013-03-08 13:12:16 -08001435 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001436 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001437
1438 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001439 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001440
Tom Hudson107843d2014-09-08 11:26:26 -04001441 deferredList.flush(*this, dirty);
1442 } else {
1443 // Even if there is no drawing command(Ex: invisible),
1444 // it still needs startFrame to clear buffer and start tiling.
1445 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001446 }
1447}
1448
Romain Guy03c00b52013-06-20 18:30:28 -07001449/**
1450 * Important note: this method is intended to draw batches of bitmaps and
1451 * will not set the scissor enable or dirty the current layer, if any.
1452 * The caller is responsible for properly dirtying the current layer.
1453 */
Tom Hudson107843d2014-09-08 11:26:26 -04001454void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001455 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1456 const Rect& bounds, const SkPaint* paint) {
Romain Guy55b6f952013-06-27 15:27:09 -07001457 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001458 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001459
Chris Craik527a3aa2013-03-04 10:19:31 -08001460 const AutoTexture autoCleanup(texture);
1461
Chris Craik82840732015-04-03 09:37:49 -07001462 // TODO: remove layer dirty in multi-draw callers
1463 // TODO: snap doesn't need to touch transform, only texture filter.
1464 bool snap = pureTranslate;
Chris Craika6b52192015-02-27 17:43:02 -08001465 const float x = floorf(bounds.left + 0.5f);
1466 const float y = floorf(bounds.top + 0.5f);
Chris Craik53e51e42015-06-01 10:35:35 -07001467
1468 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1469 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
1470 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001471 Glop glop;
1472 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001473 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001474 .setMeshTexturedMesh(vertices, bitmapCount * 6)
1475 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001476 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001477 .setModelViewOffsetRectOptionalSnap(snap, x, y, Rect(0, 0, bounds.getWidth(), bounds.getHeight()))
Chris Craik82840732015-04-03 09:37:49 -07001478 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001479 renderGlop(glop, GlopRenderType::Multi);
Chris Craik527a3aa2013-03-04 10:19:31 -08001480}
1481
Tom Hudson107843d2014-09-08 11:26:26 -04001482void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07001483 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04001484 return;
Romain Guy6926c722010-07-12 20:20:03 -07001485 }
1486
Chris Craik44eb2c02015-01-29 09:45:09 -08001487 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07001488 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001489 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001490 const AutoTexture autoCleanup(texture);
1491
Chris Craik53e51e42015-06-01 10:35:35 -07001492 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1493 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07001494 Glop glop;
1495 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001496 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001497 .setMeshTexturedUnitQuad(texture->uvMapper)
1498 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001499 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001500 .setModelViewMapUnitToRectSnap(Rect(0, 0, texture->width, texture->height))
Chris Craik82840732015-04-03 09:37:49 -07001501 .build();
1502 renderGlop(glop);
Romain Guyce0537b2010-06-29 21:05:21 -07001503}
1504
Tom Hudson107843d2014-09-08 11:26:26 -04001505void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08001506 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001507 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04001508 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08001509 }
1510
Romain Guyb18d2d02011-02-10 15:52:54 -08001511 float left = FLT_MAX;
1512 float top = FLT_MAX;
1513 float right = FLT_MIN;
1514 float bottom = FLT_MIN;
1515
Chris Craikef250742015-02-25 17:16:16 -08001516 const uint32_t elementCount = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001517
Chris Craikef250742015-02-25 17:16:16 -08001518 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[elementCount]);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001519 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08001520
Chris Craik51d6a3d2014-12-22 17:16:56 -08001521 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08001522 if (!colors) {
1523 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001524 tempColors.reset(new int[colorsCount]);
1525 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
1526 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08001527 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001528
John Reckebd52612014-12-10 16:47:36 -08001529 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07001530 const UvMapper& mapper(getMapper(texture));
1531
Romain Guy5a7b4662011-01-20 19:09:30 -08001532 for (int32_t y = 0; y < meshHeight; y++) {
1533 for (int32_t x = 0; x < meshWidth; x++) {
1534 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1535
1536 float u1 = float(x) / meshWidth;
1537 float u2 = float(x + 1) / meshWidth;
1538 float v1 = float(y) / meshHeight;
1539 float v2 = float(y + 1) / meshHeight;
1540
Romain Guy3b748a42013-04-17 18:54:38 -07001541 mapper.map(u1, v1, u2, v2);
1542
Romain Guy5a7b4662011-01-20 19:09:30 -08001543 int ax = i + (meshWidth + 1) * 2;
1544 int ay = ax + 1;
1545 int bx = i;
1546 int by = bx + 1;
1547 int cx = i + 2;
1548 int cy = cx + 1;
1549 int dx = i + (meshWidth + 1) * 2 + 2;
1550 int dy = dx + 1;
1551
Romain Guyff316ec2013-02-13 18:39:43 -08001552 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1553 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
1554 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08001555
Romain Guyff316ec2013-02-13 18:39:43 -08001556 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1557 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
1558 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08001559
Chris Craikdf72b632015-06-30 17:56:13 -07001560 left = std::min(left, std::min(vertices[ax], std::min(vertices[bx], vertices[cx])));
1561 top = std::min(top, std::min(vertices[ay], std::min(vertices[by], vertices[cy])));
1562 right = std::max(right, std::max(vertices[ax], std::max(vertices[bx], vertices[cx])));
1563 bottom = std::max(bottom, std::max(vertices[ay], std::max(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001564 }
1565 }
1566
Chris Craikf0a59072013-11-19 18:00:46 -08001567 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001568 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07001569 }
1570
Romain Guyff316ec2013-02-13 18:39:43 -08001571 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07001572 texture = mCaches.textureCache.get(bitmap);
1573 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04001574 return;
Romain Guy3b748a42013-04-17 18:54:38 -07001575 }
Romain Guyff316ec2013-02-13 18:39:43 -08001576 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001577 const AutoTexture autoCleanup(texture);
1578
Chris Craik82840732015-04-03 09:37:49 -07001579 /*
1580 * TODO: handle alpha_8 textures correctly by applying paint color, but *not*
1581 * shader in that case to mimic the behavior in SkiaCanvas::drawBitmapMesh.
1582 */
Chris Craik53e51e42015-06-01 10:35:35 -07001583 const int textureFillFlags = TextureFillFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07001584 Glop glop;
1585 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001586 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001587 .setMeshColoredTexturedMesh(mesh.get(), elementCount)
Chris Craik53e51e42015-06-01 10:35:35 -07001588 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1589 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001590 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07001591 .build();
1592 renderGlop(glop);
Romain Guy5a7b4662011-01-20 19:09:30 -08001593}
1594
Chris Craik14100ac2015-02-24 13:46:29 -08001595void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, Rect src, Rect dst, const SkPaint* paint) {
1596 if (quickRejectSetupScissor(dst)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001597 return;
Romain Guy6926c722010-07-12 20:20:03 -07001598 }
1599
Romain Guy3b748a42013-04-17 18:54:38 -07001600 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001601 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001602 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001603
Chris Craike6a15ee2015-07-07 18:42:17 -07001604 Rect uv(std::max(0.0f, src.left / texture->width),
1605 std::max(0.0f, src.top / texture->height),
1606 std::min(1.0f, src.right / texture->width),
1607 std::min(1.0f, src.bottom / texture->height));
Chris Craik14100ac2015-02-24 13:46:29 -08001608
Chris Craik53e51e42015-06-01 10:35:35 -07001609 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1610 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
Chris Craik1e4c8072015-05-26 14:25:02 -07001611 const bool tryToSnap = MathUtils::areEqual(src.getWidth(), dst.getWidth())
1612 && MathUtils::areEqual(src.getHeight(), dst.getHeight());
Chris Craik82840732015-04-03 09:37:49 -07001613 Glop glop;
1614 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001615 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001616 .setMeshTexturedUvQuad(texture->uvMapper, uv)
1617 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001618 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik1e4c8072015-05-26 14:25:02 -07001619 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, dst)
Chris Craik82840732015-04-03 09:37:49 -07001620 .build();
1621 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07001622}
1623
Tom Hudson107843d2014-09-08 11:26:26 -04001624void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08001625 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
1626 const SkPaint* paint) {
Chris Craika6b52192015-02-27 17:43:02 -08001627 if (!mesh || !mesh->verticesCount || quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001628 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07001629 }
1630
Chris Craika6b52192015-02-27 17:43:02 -08001631 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
1632 if (!texture) return;
Chris Craik0556d902015-03-03 09:54:14 -08001633
Chris Craik82840732015-04-03 09:37:49 -07001634 // 9 patches are built for stretching - always filter
Chris Craik53e51e42015-06-01 10:35:35 -07001635 int textureFillFlags = TextureFillFlags::ForceFilter;
Chris Craik82840732015-04-03 09:37:49 -07001636 if (bitmap->colorType() == kAlpha_8_SkColorType) {
Chris Craik53e51e42015-06-01 10:35:35 -07001637 textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture;
Chris Craik0556d902015-03-03 09:54:14 -08001638 }
Chris Craik82840732015-04-03 09:37:49 -07001639 Glop glop;
1640 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001641 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001642 .setMeshPatchQuads(*mesh)
1643 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001644 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001645 .setModelViewOffsetRectSnap(left, top, Rect(0, 0, right - left, bottom - top)) // TODO: get minimal bounds from patch
Chris Craik82840732015-04-03 09:37:49 -07001646 .build();
1647 renderGlop(glop);
Romain Guyf7f93552010-07-08 19:17:03 -07001648}
1649
Romain Guy03c00b52013-06-20 18:30:28 -07001650/**
1651 * Important note: this method is intended to draw batches of 9-patch objects and
1652 * will not set the scissor enable or dirty the current layer, if any.
1653 * The caller is responsible for properly dirtying the current layer.
1654 */
Tom Hudson107843d2014-09-08 11:26:26 -04001655void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craika6b52192015-02-27 17:43:02 -08001656 TextureVertex* vertices, uint32_t elementCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001657 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07001658 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001659 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07001660 const AutoTexture autoCleanup(texture);
1661
Chris Craik82840732015-04-03 09:37:49 -07001662 // TODO: get correct bounds from caller
Chris Craik53e51e42015-06-01 10:35:35 -07001663 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001664 // 9 patches are built for stretching - always filter
Chris Craik53e51e42015-06-01 10:35:35 -07001665 int textureFillFlags = TextureFillFlags::ForceFilter;
Chris Craik82840732015-04-03 09:37:49 -07001666 if (bitmap->colorType() == kAlpha_8_SkColorType) {
Chris Craik53e51e42015-06-01 10:35:35 -07001667 textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture;
Chris Craika6b52192015-02-27 17:43:02 -08001668 }
Chris Craik82840732015-04-03 09:37:49 -07001669 Glop glop;
1670 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001671 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001672 .setMeshTexturedIndexedQuads(vertices, elementCount)
1673 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001674 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001675 .setModelViewOffsetRect(0, 0, Rect(0, 0, 0, 0))
Chris Craik82840732015-04-03 09:37:49 -07001676 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001677 renderGlop(glop, GlopRenderType::Multi);
Romain Guy03c00b52013-06-20 18:30:28 -07001678}
1679
Tom Hudson107843d2014-09-08 11:26:26 -04001680void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07001681 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001682 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07001683 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08001684 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04001685 return;
Chris Craik65cd6122012-12-10 17:56:27 -08001686 }
1687
Chris Craik82840732015-04-03 09:37:49 -07001688 bool shadowInterp = displayFlags & kVertexBuffer_ShadowInterp;
Chris Craik53e51e42015-06-01 10:35:35 -07001689 const int transformFlags = TransformFlags::OffsetByFudgeFactor;
Chris Craik82840732015-04-03 09:37:49 -07001690 Glop glop;
1691 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001692 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001693 .setMeshVertexBuffer(vertexBuffer, shadowInterp)
1694 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001695 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001696 .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds())
Chris Craik82840732015-04-03 09:37:49 -07001697 .build();
1698 renderGlop(glop);
Chet Haase858aa932011-05-12 09:06:00 -07001699}
1700
1701/**
Chris Craik65cd6122012-12-10 17:56:27 -08001702 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
1703 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
1704 * screen space in all directions. However, instead of using a fragment shader to compute the
1705 * translucency of the color from its position, we simply use a varying parameter to define how far
1706 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
1707 *
1708 * Doesn't yet support joins, caps, or path effects.
1709 */
Tom Hudson107843d2014-09-08 11:26:26 -04001710void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08001711 VertexBuffer vertexBuffer;
1712 // TODO: try clipping large paths to viewport
Chris Craikfca52b752015-04-28 11:45:59 -07001713
Chris Craikd6b65f62014-01-01 14:45:21 -08001714 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04001715 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08001716}
1717
1718/**
1719 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
1720 * and additional geometry for defining an alpha slope perimeter.
1721 *
1722 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
1723 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
1724 * in-shader alpha region, but found it to be taxing on some GPUs.
1725 *
1726 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
1727 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07001728 */
Tom Hudson107843d2014-09-08 11:26:26 -04001729void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001730 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07001731
Chris Craik65cd6122012-12-10 17:56:27 -08001732 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07001733
Chris Craik65cd6122012-12-10 17:56:27 -08001734 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001735 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
1736 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08001737
Chris Craik05f3d6e2014-06-02 16:27:04 -07001738 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001739 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08001740 }
1741
Chris Craikbf759452014-08-11 16:00:44 -07001742 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001743 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07001744}
1745
Tom Hudson107843d2014-09-08 11:26:26 -04001746void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001747 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001748
Chris Craik6d29c8d2013-05-08 18:35:44 -07001749 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07001750
Chris Craik6d29c8d2013-05-08 18:35:44 -07001751 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001752 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08001753
Chris Craik05f3d6e2014-06-02 16:27:04 -07001754 const Rect& bounds = buffer.getBounds();
1755 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001756 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001757 }
1758
Chris Craikbf759452014-08-11 16:00:44 -07001759 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001760 drawVertexBuffer(buffer, paint, displayFlags);
1761
1762 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07001763}
1764
Tom Hudson107843d2014-09-08 11:26:26 -04001765void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001766 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04001767 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001768
Rob Tsuk487a92c2015-01-06 13:22:54 -08001769 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07001770 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001771
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001772 SkPaint paint;
1773 paint.setColor(color);
1774 paint.setXfermodeMode(mode);
1775
1776 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07001777
Tom Hudson107843d2014-09-08 11:26:26 -04001778 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07001779}
Romain Guy9d5316e2010-06-24 19:30:36 -07001780
Chris Craik30036092015-02-12 10:41:39 -08001781void OpenGLRenderer::drawShape(float left, float top, PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08001782 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04001783 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001784 const AutoTexture autoCleanup(texture);
1785
1786 const float x = left + texture->left - texture->offset;
1787 const float y = top + texture->top - texture->offset;
1788
1789 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07001790
Tom Hudson107843d2014-09-08 11:26:26 -04001791 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08001792}
1793
Tom Hudson107843d2014-09-08 11:26:26 -04001794void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001795 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001796 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001797 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001798 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001799 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001800 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001801
Chris Craike84a2082014-12-22 14:28:49 -08001802 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001803 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001804 PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07001805 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001806 drawShape(left, top, texture, p);
1807 } else {
1808 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
1809 *currentTransform(), *p, right - left, bottom - top, rx, ry);
1810 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001811 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001812}
1813
Tom Hudson107843d2014-09-08 11:26:26 -04001814void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001815 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001816 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001817 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001818 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001819 }
Chris Craikfca52b752015-04-28 11:45:59 -07001820
Chris Craike84a2082014-12-22 14:28:49 -08001821 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001822 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001823 PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001824 drawShape(x - radius, y - radius, texture, p);
Chris Craikfca52b752015-04-28 11:45:59 -07001825 return;
Chris Craikcb4d6002012-09-25 12:00:29 -07001826 }
Chris Craikfca52b752015-04-28 11:45:59 -07001827
1828 SkPath path;
1829 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1830 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
1831 } else {
1832 path.addCircle(x, y, radius);
1833 }
1834
1835 if (CC_UNLIKELY(currentSnapshot()->projectionPathMask != nullptr)) {
1836 // mask ripples with projection mask
1837 SkPath maskPath = *(currentSnapshot()->projectionPathMask->projectionMask);
1838
1839 Matrix4 screenSpaceTransform;
1840 currentSnapshot()->buildScreenSpaceTransform(&screenSpaceTransform);
1841
1842 Matrix4 totalTransform;
1843 totalTransform.loadInverse(screenSpaceTransform);
1844 totalTransform.multiply(currentSnapshot()->projectionPathMask->projectionMaskTransform);
1845
1846 SkMatrix skTotalTransform;
1847 totalTransform.copyTo(skTotalTransform);
1848 maskPath.transform(skTotalTransform);
1849
1850 // Mask the ripple path by the projection mask, now that it's
1851 // in local space. Note that this can create CCW paths.
Tom Hudson02a26302015-06-24 11:32:42 -04001852 Op(path, maskPath, kIntersect_SkPathOp, &path);
Chris Craikfca52b752015-04-28 11:45:59 -07001853 }
1854 drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001855}
Romain Guy01d58e42011-01-19 21:54:02 -08001856
Tom Hudson107843d2014-09-08 11:26:26 -04001857void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001858 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001859 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001860 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001861 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001862 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001863 }
Romain Guy01d58e42011-01-19 21:54:02 -08001864
Chris Craike84a2082014-12-22 14:28:49 -08001865 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001866 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001867 PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001868 drawShape(left, top, texture, p);
1869 } else {
1870 SkPath path;
1871 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1872 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1873 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1874 }
1875 path.addOval(rect);
1876 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001877 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001878}
1879
Tom Hudson107843d2014-09-08 11:26:26 -04001880void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001881 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001882 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001883 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001884 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001885 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08001886 }
1887
Chris Craik780c1282012-10-04 14:10:49 -07001888 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08001889 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001890 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001891 PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07001892 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001893 drawShape(left, top, texture, p);
1894 return;
Chris Craik780c1282012-10-04 14:10:49 -07001895 }
Chris Craik780c1282012-10-04 14:10:49 -07001896 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1897 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1898 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1899 }
1900
1901 SkPath path;
1902 if (useCenter) {
1903 path.moveTo(rect.centerX(), rect.centerY());
1904 }
1905 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
1906 if (useCenter) {
1907 path.close();
1908 }
Tom Hudson107843d2014-09-08 11:26:26 -04001909 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08001910}
1911
Romain Guycf8675e2012-10-02 12:32:25 -07001912// See SkPaintDefaults.h
1913#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
1914
Tom Hudson107843d2014-09-08 11:26:26 -04001915void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001916 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001917 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001918 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001919 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001920 return;
Romain Guy6926c722010-07-12 20:20:03 -07001921 }
1922
Chris Craik710f46d2012-09-17 17:25:49 -07001923 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07001924 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08001925 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07001926 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001927 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001928 PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07001929 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001930 drawShape(left, top, texture, p);
1931 } else {
1932 SkPath path;
1933 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1934 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1935 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1936 }
1937 path.addRect(rect);
1938 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07001939 }
Chet Haase858aa932011-05-12 09:06:00 -07001940 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04001941 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
1942 SkPath path;
1943 path.addRect(left, top, right, bottom);
1944 drawConvexPath(path, p);
1945 } else {
1946 drawColorRect(left, top, right, bottom, p);
1947
1948 mDirty = true;
1949 }
Chet Haase858aa932011-05-12 09:06:00 -07001950 }
Romain Guyc7d53492010-06-25 13:41:57 -07001951}
Romain Guy9d5316e2010-06-24 19:30:36 -07001952
Chris Craikd218a922014-01-02 17:13:34 -08001953void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
1954 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001955 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001956 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07001957
Chris Craikbf6f0f22015-10-01 12:36:07 -07001958 PaintUtils::TextShadow textShadow;
1959 if (!PaintUtils::getTextShadow(paint, &textShadow)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04001960 LOG_ALWAYS_FATAL("failed to query shadow attributes");
1961 }
1962
Raph Levien416a8472012-07-19 22:48:17 -07001963 // NOTE: The drop shadow will not perform gamma correction
1964 // if shader-based correction is enabled
1965 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Chris Craik2bb8f562015-02-17 16:42:02 -08001966 ShadowTexture* texture = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04001967 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07001968 // If the drop shadow exceeds the max texture size or couldn't be
1969 // allocated, skip drawing
Chris Craik2bb8f562015-02-17 16:42:02 -08001970 if (!texture) return;
1971 const AutoTexture autoCleanup(texture);
Raph Levien416a8472012-07-19 22:48:17 -07001972
Chris Craik2bb8f562015-02-17 16:42:02 -08001973 const float sx = x - texture->left + textShadow.dx;
1974 const float sy = y - texture->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07001975
Chris Craik82840732015-04-03 09:37:49 -07001976 Glop glop;
1977 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001978 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001979 .setMeshTexturedUnitQuad(nullptr)
1980 .setFillShadowTexturePaint(*texture, textShadow.color, *paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001981 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001982 .setModelViewMapUnitToRect(Rect(sx, sy, sx + texture->width, sy + texture->height))
Chris Craik82840732015-04-03 09:37:49 -07001983 .build();
1984 renderGlop(glop);
Raph Levien416a8472012-07-19 22:48:17 -07001985}
1986
Chris Craikbf6f0f22015-10-01 12:36:07 -07001987// TODO: remove this, once mState.currentlyIgnored captures snapshot alpha
Romain Guy768bffc2013-02-27 13:50:45 -08001988bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Chris Craikbf6f0f22015-10-01 12:36:07 -07001989 float alpha = (PaintUtils::hasTextShadow(paint)
1990 ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05001991 return MathUtils::isZero(alpha)
1992 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08001993}
1994
Tom Hudson107843d2014-09-08 11:26:26 -04001995void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08001996 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08001997 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001998 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08001999 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002000
Romain Guy671d6cf2012-01-18 12:39:17 -08002001 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002002 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002003 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002004 }
2005
Chris Craik65fe5ee2015-01-26 18:06:29 -08002006 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002007
Romain Guy671d6cf2012-01-18 12:39:17 -08002008 float x = 0.0f;
2009 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002010 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002011 if (pureTranslate) {
Chris Craika6b52192015-02-27 17:43:02 -08002012 x = floorf(x + currentTransform()->getTranslateX() + 0.5f);
2013 y = floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002014 }
2015
Chris Craikc08820f2015-09-22 14:22:29 -07002016 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
Chris Craik59744b72014-07-01 17:56:52 -07002017 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002018
Chris Craikbf6f0f22015-10-01 12:36:07 -07002019 int alpha = PaintUtils::getAlphaDirect(paint) * currentSnapshot()->alpha;
2020 SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002021
Chris Craikbf6f0f22015-10-01 12:36:07 -07002022 if (CC_UNLIKELY(PaintUtils::hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002023 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002024 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002025 }
2026
Romain Guy671d6cf2012-01-18 12:39:17 -08002027 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002028 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002029 if (pureTranslate && !linearFilter) {
2030 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2031 }
Romain Guy257ae352013-03-20 16:31:12 -07002032 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002033
Rob Tsuk487a92c2015-01-06 13:22:54 -08002034 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002035 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2036
Chris Craik82840732015-04-03 09:37:49 -07002037 TextDrawFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002038 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craik083e7332015-02-27 17:04:20 -08002039 positions, hasLayer() ? &bounds : nullptr, &functor)) {
2040 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2041 mDirty = true;
Romain Guy671d6cf2012-01-18 12:39:17 -08002042 }
Chet Haase48659092012-05-31 15:21:51 -07002043
Romain Guyeb9a5362012-01-17 17:39:26 -08002044}
2045
Chris Craik59744b72014-07-01 17:56:52 -07002046bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002047 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002048 outMatrix->setIdentity();
2049 return false;
2050 } else if (CC_UNLIKELY(transform.isPerspective())) {
2051 outMatrix->setIdentity();
2052 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002053 }
Chris Craik59744b72014-07-01 17:56:52 -07002054
2055 /**
Chris Craika736cd92014-08-04 13:18:38 -07002056 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2057 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002058 */
2059 float sx, sy;
2060 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002061 outMatrix->setScale(
Chris Craikdf72b632015-06-30 17:56:13 -07002062 roundf(std::max(1.0f, sx)),
2063 roundf(std::max(1.0f, sy)));
Chris Craika736cd92014-08-04 13:18:38 -07002064 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002065}
2066
Tom Hudson984162f2014-10-10 13:38:16 -04002067int OpenGLRenderer::getSaveCount() const {
2068 return mState.getSaveCount();
2069}
2070
2071int OpenGLRenderer::save(int flags) {
2072 return mState.save(flags);
2073}
2074
2075void OpenGLRenderer::restore() {
Chris Craike2bb3802015-03-13 15:07:52 -07002076 mState.restore();
Tom Hudson984162f2014-10-10 13:38:16 -04002077}
2078
2079void OpenGLRenderer::restoreToCount(int saveCount) {
Chris Craike2bb3802015-03-13 15:07:52 -07002080 mState.restoreToCount(saveCount);
Tom Hudson984162f2014-10-10 13:38:16 -04002081}
2082
Tom Hudsonac7b6d32015-06-30 11:26:13 -04002083
Tom Hudson984162f2014-10-10 13:38:16 -04002084void OpenGLRenderer::translate(float dx, float dy, float dz) {
Chris Craike2bb3802015-03-13 15:07:52 -07002085 mState.translate(dx, dy, dz);
Tom Hudson984162f2014-10-10 13:38:16 -04002086}
2087
2088void OpenGLRenderer::rotate(float degrees) {
Chris Craike2bb3802015-03-13 15:07:52 -07002089 mState.rotate(degrees);
Tom Hudson984162f2014-10-10 13:38:16 -04002090}
2091
2092void OpenGLRenderer::scale(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002093 mState.scale(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002094}
2095
2096void OpenGLRenderer::skew(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002097 mState.skew(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002098}
2099
Chris Craik6daa13c2015-08-19 13:32:12 -07002100void OpenGLRenderer::setLocalMatrix(const Matrix4& matrix) {
2101 mState.setMatrix(mBaseTransform);
2102 mState.concatMatrix(matrix);
Tom Hudson984162f2014-10-10 13:38:16 -04002103}
2104
Tom Hudsonac7b6d32015-06-30 11:26:13 -04002105void OpenGLRenderer::setLocalMatrix(const SkMatrix& matrix) {
2106 mState.setMatrix(mBaseTransform);
2107 mState.concatMatrix(matrix);
2108}
2109
Tom Hudson984162f2014-10-10 13:38:16 -04002110void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2111 mState.concatMatrix(matrix);
2112}
2113
2114bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2115 return mState.clipRect(left, top, right, bottom, op);
2116}
2117
2118bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2119 return mState.clipPath(path, op);
2120}
2121
2122bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2123 return mState.clipRegion(region, op);
2124}
2125
2126void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2127 mState.setClippingOutline(allocator, outline);
2128}
2129
2130void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2131 const Rect& rect, float radius, bool highPriority) {
2132 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2133}
2134
Chris Craikfca52b752015-04-28 11:45:59 -07002135void OpenGLRenderer::setProjectionPathMask(LinearAllocator& allocator, const SkPath* path) {
2136 mState.setProjectionPathMask(allocator, path);
2137}
2138
Tom Hudson107843d2014-09-08 11:26:26 -04002139void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002140 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002141 DrawOpMode drawOpMode) {
2142
Chris Craik03ae2722015-02-25 11:01:09 -08002143 if (drawOpMode == DrawOpMode::kImmediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002144 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2145 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002146 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002147 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002148 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002149 }
Romain Guycac5fd32011-12-01 20:08:50 -08002150 }
2151
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002152 const float oldX = x;
2153 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002154
Chris Craikd6b65f62014-01-01 14:45:21 -08002155 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002156 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002157
Romain Guy211370f2012-02-01 16:10:55 -08002158 if (CC_LIKELY(pureTranslate)) {
Chris Craika6b52192015-02-27 17:43:02 -08002159 x = floorf(x + transform.getTranslateX() + 0.5f);
2160 y = floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002161 }
2162
Chris Craikbf6f0f22015-10-01 12:36:07 -07002163 int alpha = PaintUtils::getAlphaDirect(paint) * currentSnapshot()->alpha;
2164 SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(paint);
Romain Guy9d13fe252010-10-15 16:06:03 -07002165
Chris Craikc08820f2015-09-22 14:22:29 -07002166 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
Romain Guyc74f45a2013-02-26 19:10:14 -08002167
Chris Craikbf6f0f22015-10-01 12:36:07 -07002168 if (CC_UNLIKELY(PaintUtils::hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002169 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002170 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002171 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002172 }
2173
Romain Guy19d4dd82013-03-04 11:14:26 -08002174 const bool hasActiveLayer = hasLayer();
2175
Romain Guy624234f2013-03-05 16:43:31 -08002176 // We only pass a partial transform to the font renderer. That partial
2177 // matrix defines how glyphs are rasterized. Typically we want glyphs
2178 // to be rasterized at their final size on screen, which means the partial
2179 // matrix needs to take the scale factor into account.
2180 // When a partial matrix is used to transform glyphs during rasterization,
2181 // the mesh is generated with the inverse transform (in the case of scale,
2182 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2183 // apply the full transform matrix at draw time in the vertex shader.
2184 // Applying the full matrix in the shader is the easiest way to handle
2185 // rotation and perspective and allows us to always generated quads in the
2186 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002187 SkMatrix fontTransform;
2188 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2189 || fabs(y - (int) y) > 0.0f
2190 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002191 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002192 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002193
Romain Guy624234f2013-03-05 16:43:31 -08002194 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08002195 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002196 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 -07002197
Raph Levien996e57c2012-07-23 15:22:52 -07002198 bool status;
Chris Craik82840732015-04-03 09:37:49 -07002199 TextDrawFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002200
2201 // don't call issuedrawcommand, do it at end of batch
Chris Craik03ae2722015-02-25 11:01:09 -08002202 bool forceFinish = (drawOpMode != DrawOpMode::kDefer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002203 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002204 SkPaint paintCopy(*paint);
2205 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2206 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002207 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002208 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002209 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002210 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002211 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002212
Chris Craik03ae2722015-02-25 11:01:09 -08002213 if ((status || drawOpMode != DrawOpMode::kImmediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002214 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002215 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002216 }
Chris Craik41541822013-05-03 16:35:54 -07002217 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002218 }
Romain Guy694b5192010-07-21 21:33:20 -07002219
Chris Craike63f7c622013-10-17 10:30:55 -07002220 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002221
Tom Hudson107843d2014-09-08 11:26:26 -04002222 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002223}
2224
Tom Hudson107843d2014-09-08 11:26:26 -04002225void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002226 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002227 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002228 return;
Romain Guy03d58522012-02-24 17:54:07 -08002229 }
2230
Chris Craik39a908c2013-06-13 14:39:01 -07002231 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002232 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002233
Chris Craikc08820f2015-09-22 14:22:29 -07002234 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
Chris Craik59744b72014-07-01 17:56:52 -07002235 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002236 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002237
Chris Craikbf6f0f22015-10-01 12:36:07 -07002238 int alpha = PaintUtils::getAlphaDirect(paint) * currentSnapshot()->alpha;
2239 SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(paint);
Chris Craik82840732015-04-03 09:37:49 -07002240 TextDrawFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002241
Tom Hudson984162f2014-10-10 13:38:16 -04002242 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002243 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 -08002244
Romain Guy97771732012-02-28 18:17:02 -08002245 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chih-Hung Hsieh9ad6ac02015-02-27 16:11:36 -08002246 hOffset, vOffset, hasLayer() ? &bounds : nullptr, &functor)) {
Chris Craik083e7332015-02-27 17:04:20 -08002247 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2248 mDirty = true;
Romain Guy97771732012-02-28 18:17:02 -08002249 }
Romain Guy325740f2012-02-24 16:48:34 -08002250}
2251
Tom Hudson107843d2014-09-08 11:26:26 -04002252void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002253 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002254
Chris Craik44eb2c02015-01-29 09:45:09 -08002255 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002256
Chris Craik30036092015-02-12 10:41:39 -08002257 PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002258 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002259 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002260
Romain Guy8b55f372010-08-18 17:10:07 -07002261 const float x = texture->left - texture->offset;
2262 const float y = texture->top - texture->offset;
2263
Romain Guy01d58e42011-01-19 21:54:02 -08002264 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002265 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002266}
2267
Chris Craik3aadd602015-08-20 12:41:40 -07002268void OpenGLRenderer::drawLayer(Layer* layer) {
Romain Guy35643dd2012-09-18 15:40:58 -07002269 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002270 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002271 }
2272
Chris Craike84a2082014-12-22 14:28:49 -08002273 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002274 if (layer->isTextureLayer()) {
2275 transform = &layer->getTransform();
2276 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002277 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002278 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002279 }
2280 }
2281
Chris Craik39a908c2013-06-13 14:39:01 -07002282 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08002283 const bool rejected = mState.calculateQuickRejectForScissor(
Chris Craik3aadd602015-08-20 12:41:40 -07002284 0, 0, layer->layer.getWidth(), layer->layer.getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -08002285 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002286
2287 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002288 if (transform && !transform->isIdentity()) {
2289 restore();
2290 }
Tom Hudson107843d2014-09-08 11:26:26 -04002291 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002292 }
2293
Chris Craik62d307c2014-07-29 10:35:13 -07002294 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2295 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2296
Romain Guy5bb3c732012-11-29 17:52:58 -08002297 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002298
Chris Craik65fe5ee2015-01-26 18:06:29 -08002299 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08002300 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002301
Romain Guy211370f2012-02-01 16:10:55 -08002302 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002303 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002304 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2305 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002306 } else if (layer->mesh) {
Chris Craik82840732015-04-03 09:37:49 -07002307 Glop glop;
2308 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002309 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002310 .setMeshTexturedIndexedQuads(layer->mesh, layer->meshElementCount)
2311 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
Chris Craik53e51e42015-06-01 10:35:35 -07002312 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik3aadd602015-08-20 12:41:40 -07002313 .setModelViewOffsetRectSnap(0, 0, Rect(0, 0, layer->layer.getWidth(), layer->layer.getHeight()))
Chris Craik82840732015-04-03 09:37:49 -07002314 .build();
2315 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy3a3133d2011-02-01 22:59:58 -08002316#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07002317 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08002318#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002319 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002320
Romain Guy5bb3c732012-11-29 17:52:58 -08002321 if (layer->debugDrawUpdate) {
2322 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002323
2324 SkPaint paint;
2325 paint.setColor(0x7f00ff00);
Chris Craik3aadd602015-08-20 12:41:40 -07002326 drawColorRect(0, 0, layer->layer.getWidth(), layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002327 }
Romain Guyf219da52011-01-16 12:54:25 -08002328 }
Chris Craik34416ea2013-04-15 16:08:28 -07002329 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07002330
Romain Guyb2e2f242012-10-17 18:18:35 -07002331 if (transform && !transform->isIdentity()) {
2332 restore();
2333 }
2334
Tom Hudson107843d2014-09-08 11:26:26 -04002335 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08002336}
2337
Romain Guy6926c722010-07-12 20:20:03 -07002338///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002339// Draw filters
2340///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04002341void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
2342 // We should never get here since we apply the draw filter when stashing
2343 // the paints in the DisplayList.
2344 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08002345}
2346
2347///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002348// Drawing implementation
2349///////////////////////////////////////////////////////////////////////////////
2350
Chris Craikd218a922014-01-02 17:13:34 -08002351Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08002352 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002353 if (!texture) {
2354 return mCaches.textureCache.get(bitmap);
2355 }
2356 return texture;
2357}
2358
Chris Craik2bb8f562015-02-17 16:42:02 -08002359void OpenGLRenderer::drawPathTexture(PathTexture* texture, float x, float y,
2360 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002361 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08002362 return;
2363 }
2364
Chris Craik82840732015-04-03 09:37:49 -07002365 Glop glop;
2366 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002367 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002368 .setMeshTexturedUnitQuad(nullptr)
2369 .setFillPathTexturePaint(*texture, *paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002370 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07002371 .setModelViewMapUnitToRect(Rect(x, y, x + texture->width, y + texture->height))
Chris Craik82840732015-04-03 09:37:49 -07002372 .build();
2373 renderGlop(glop);
Romain Guy01d58e42011-01-19 21:54:02 -08002374}
2375
Romain Guyf607bdc2010-09-10 19:20:06 -07002376// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002377#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2378#define kStdUnderline_Offset (1.0f / 9.0f)
2379#define kStdUnderline_Thickness (1.0f / 18.0f)
2380
Chris Craikd218a922014-01-02 17:13:34 -08002381void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
2382 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07002383 // Handle underline and strike-through
2384 uint32_t flags = paint->getFlags();
2385 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002386 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002387
Romain Guy211370f2012-02-01 16:10:55 -08002388 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002389 const float textSize = paintCopy.getTextSize();
Chris Craike6a15ee2015-07-07 18:42:17 -07002390 const float strokeWidth = std::max(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002391
Raph Levien8b4072d2012-07-30 15:50:00 -07002392 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002393 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002394
Romain Guyf6834472011-01-23 13:32:12 -08002395 int linesCount = 0;
2396 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2397 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2398
2399 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002400 float points[pointsCount];
2401 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002402
2403 if (flags & SkPaint::kUnderlineText_Flag) {
2404 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002405 points[currentPoint++] = left;
2406 points[currentPoint++] = top;
2407 points[currentPoint++] = left + underlineWidth;
2408 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002409 }
2410
2411 if (flags & SkPaint::kStrikeThruText_Flag) {
2412 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002413 points[currentPoint++] = left;
2414 points[currentPoint++] = top;
2415 points[currentPoint++] = left + underlineWidth;
2416 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002417 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002418
Romain Guy726aeba2011-06-01 14:52:00 -07002419 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002420
Romain Guy726aeba2011-06-01 14:52:00 -07002421 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002422 }
2423 }
2424}
2425
Tom Hudson107843d2014-09-08 11:26:26 -04002426void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002427 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002428 return;
Romain Guy672433d2013-01-04 19:05:13 -08002429 }
2430
Tom Hudson107843d2014-09-08 11:26:26 -04002431 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08002432}
2433
Tom Hudson107843d2014-09-08 11:26:26 -04002434void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07002435 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04002436 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07002437
Chris Craik15a07a22014-01-26 13:43:53 -08002438 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08002439 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07002440
2441 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08002442 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07002443
ztenghui14a4e352014-08-13 10:44:39 -07002444 // The caller has made sure casterAlpha > 0.
2445 float ambientShadowAlpha = mAmbientShadowAlpha;
Chris Craik2507c342015-05-04 14:36:49 -07002446 if (CC_UNLIKELY(Properties::overrideAmbientShadowStrength >= 0)) {
2447 ambientShadowAlpha = Properties::overrideAmbientShadowStrength;
ztenghui14a4e352014-08-13 10:44:39 -07002448 }
2449 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
2450 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002451 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002452 }
ztenghui7b4516e2014-01-07 10:42:55 -08002453
ztenghui14a4e352014-08-13 10:44:39 -07002454 float spotShadowAlpha = mSpotShadowAlpha;
Chris Craik2507c342015-05-04 14:36:49 -07002455 if (CC_UNLIKELY(Properties::overrideSpotShadowStrength >= 0)) {
2456 spotShadowAlpha = Properties::overrideSpotShadowStrength;
ztenghui14a4e352014-08-13 10:44:39 -07002457 }
2458 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
2459 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002460 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002461 }
ztenghui7b4516e2014-01-07 10:42:55 -08002462
Tom Hudson107843d2014-09-08 11:26:26 -04002463 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07002464}
2465
Tom Hudson107843d2014-09-08 11:26:26 -04002466void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002467 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08002468 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04002469 return;
Romain Guy3b753822013-03-05 10:27:35 -08002470 }
Romain Guy735738c2012-12-03 12:34:51 -08002471
Romain Guy672433d2013-01-04 19:05:13 -08002472 float left = FLT_MAX;
2473 float top = FLT_MAX;
2474 float right = FLT_MIN;
2475 float bottom = FLT_MIN;
2476
Romain Guy448455f2013-07-22 13:57:50 -07002477 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08002478 Vertex* vertex = mesh;
2479
Chris Craik2af46352012-11-26 18:30:17 -08002480 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08002481 float l = rects[index + 0];
2482 float t = rects[index + 1];
2483 float r = rects[index + 2];
2484 float b = rects[index + 3];
2485
Romain Guy3b753822013-03-05 10:27:35 -08002486 Vertex::set(vertex++, l, t);
2487 Vertex::set(vertex++, r, t);
2488 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08002489 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08002490
Chris Craikdf72b632015-06-30 17:56:13 -07002491 left = std::min(left, l);
2492 top = std::min(top, t);
2493 right = std::max(right, r);
2494 bottom = std::max(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08002495 }
2496
Chris Craikf0a59072013-11-19 18:00:46 -08002497 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002498 return;
Romain Guya362c692013-02-04 13:50:16 -08002499 }
Romain Guy672433d2013-01-04 19:05:13 -08002500
Chris Craik53e51e42015-06-01 10:35:35 -07002501 const int transformFlags = ignoreTransform
2502 ? TransformFlags::MeshIgnoresCanvasTransform : TransformFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07002503 Glop glop;
2504 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002505 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002506 .setMeshIndexedQuads(&mesh[0], count / 4)
2507 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002508 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07002509 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07002510 .build();
2511 renderGlop(glop);
Romain Guy672433d2013-01-04 19:05:13 -08002512}
2513
Romain Guy026c5e162010-06-28 17:12:22 -07002514void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002515 const SkPaint* paint, bool ignoreTransform) {
Chris Craik53e51e42015-06-01 10:35:35 -07002516 const int transformFlags = ignoreTransform
2517 ? TransformFlags::MeshIgnoresCanvasTransform : TransformFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07002518 Glop glop;
2519 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002520 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002521 .setMeshUnitQuad()
2522 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002523 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07002524 .setModelViewMapUnitToRect(Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07002525 .build();
2526 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07002527}
2528
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002529float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik8df5ffa2015-04-28 17:47:20 -07002530 return (layer->getAlpha() / 255.0f) * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07002531}
2532
Romain Guy9d5316e2010-06-24 19:30:36 -07002533}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002534}; // namespace android