blob: 827404fc22d44da913b62f53877012e9d7d056e5 [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
Romain Guy96885eb2013-03-26 15:05:58 -0700116void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800117 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800118 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400119 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700120 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700121 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700122}
123
Tom Hudson107843d2014-09-08 11:26:26 -0400124void OpenGLRenderer::startFrame() {
125 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700126 mFrameStarted = true;
127
Tom Hudson984162f2014-10-10 13:38:16 -0400128 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700129
Romain Guy96885eb2013-03-26 15:05:58 -0700130 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700131
Tom Hudson984162f2014-10-10 13:38:16 -0400132 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800133
Romain Guy7c450aa2012-09-21 19:15:00 -0700134 debugOverdraw(true, true);
135
Tom Hudson107843d2014-09-08 11:26:26 -0400136 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700137 mTilingClip.right, mTilingClip.bottom, mOpaque);
138}
139
Tom Hudson107843d2014-09-08 11:26:26 -0400140void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700141 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700142
Romain Guy96885eb2013-03-26 15:05:58 -0700143 setupFrameState(left, top, right, bottom, opaque);
144
145 // Layer renderers will start the frame immediately
146 // The framebuffer renderer will first defer the display list
147 // for each layer and wait until the first drawing command
148 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800149 if (currentSnapshot()->fbo == 0) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800150 mRenderState.blend().syncEnabled();
Romain Guy96885eb2013-03-26 15:05:58 -0700151 updateLayers();
152 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400153 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700154 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700155}
156
Romain Guydcfc8362013-01-03 13:08:57 -0800157void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
158 // If we know that we are going to redraw the entire framebuffer,
159 // perform a discard to let the driver know we don't need to preserve
160 // the back buffer for this frame.
Chris Craik117bdbc2015-02-05 10:12:38 -0800161 if (mCaches.extensions().hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400162 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
Chris Craik6b109c72015-02-27 10:55:28 -0800163 const bool isFbo = getTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800164 const GLenum attachments[] = {
165 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
166 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800167 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
168 }
169}
170
Tom Hudson107843d2014-09-08 11:26:26 -0400171void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700172 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800173 mRenderState.scissor().setEnabled(true);
174 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700175 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400176 mDirty = true;
177 return;
Romain Guyddf74372012-05-22 14:07:07 -0700178 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700179
Chris Craik65fe5ee2015-01-26 18:06:29 -0800180 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700181}
182
Tom Hudson107843d2014-09-08 11:26:26 -0400183bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700184 renderOverdraw();
Chris Craik4ac36f82014-12-09 16:54:03 -0800185 mTempPaths.clear();
186
Romain Guyca89e2a2013-03-08 17:44:20 -0800187 // When finish() is invoked on FBO 0 we've reached the end
188 // of the current frame
Chris Craik6b109c72015-02-27 10:55:28 -0800189 if (getTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800190 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700191 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800192 }
193
Romain Guy11cb6422012-09-21 00:39:43 -0700194 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700195#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700196 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700197#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700198
Romain Guyc15008e2010-11-10 11:59:15 -0800199#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800200 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700201#else
Chris Craik2507c342015-05-04 14:36:49 -0700202 if (Properties::debugLevel & kDebugMemory) {
Romain Guy11cb6422012-09-21 00:39:43 -0700203 mCaches.dumpMemoryUsage();
204 }
Romain Guyc15008e2010-11-10 11:59:15 -0800205#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700206 }
Romain Guy96885eb2013-03-26 15:05:58 -0700207
208 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400209
210 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700211}
212
Romain Guy35643dd2012-09-18 15:40:58 -0700213void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700214 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
215 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700216 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700217
Chris Craik65fe5ee2015-01-26 18:06:29 -0800218 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700219 dirtyClip();
220}
221
Andreas Gampe64bb4132014-11-22 00:35:09 +0000222void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400223 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700224
Rob Tsuk487a92c2015-01-06 13:22:54 -0800225 Rect clip(mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700226 clip.snapToPixelBoundaries();
227
Romain Guyd643bb52011-03-01 14:55:21 -0800228 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800229 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800230 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800231 dirtyLayerUnchecked(clip, getRegion());
232 }
Romain Guyd643bb52011-03-01 14:55:21 -0800233
Romain Guy08aa2cb2011-03-17 11:06:57 -0700234 DrawGlInfo info;
235 info.clipLeft = clip.left;
236 info.clipTop = clip.top;
237 info.clipRight = clip.right;
238 info.clipBottom = clip.bottom;
239 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700240 info.width = getViewportWidth();
241 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800242 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700243
Tom Hudson984162f2014-10-10 13:38:16 -0400244 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700245 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400246 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700247 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
248 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800249 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700250 setScissorFromClip();
251 }
Chris Craik54f574a2013-08-26 11:23:46 -0700252
John Reck3b202512014-06-23 13:13:08 -0700253 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
254 // Scissor may have been modified, reset dirty clip
255 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800256
Tom Hudson107843d2014-09-08 11:26:26 -0400257 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800258}
259
Romain Guyf6a11b82010-06-23 17:47:49 -0700260///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700261// Debug
262///////////////////////////////////////////////////////////////////////////////
263
Chris Craik62d307c2014-07-29 10:35:13 -0700264void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
265#if DEBUG_DETAILED_EVENTS
266 const int BUFFER_SIZE = 256;
267 va_list ap;
268 char buf[BUFFER_SIZE];
269
270 va_start(ap, fmt);
271 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
272 va_end(ap);
273
274 eventMark(buf);
275#endif
276}
277
278
Romain Guy0f6675332013-03-01 14:31:04 -0800279void OpenGLRenderer::eventMark(const char* name) const {
280 mCaches.eventMark(0, name);
281}
282
Romain Guy87e2f7572012-09-24 11:37:12 -0700283void OpenGLRenderer::startMark(const char* name) const {
284 mCaches.startMark(0, name);
285}
286
287void OpenGLRenderer::endMark() const {
288 mCaches.endMark();
289}
290
291void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700292 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700293}
294
295void OpenGLRenderer::renderOverdraw() {
Chris Craik2507c342015-05-04 14:36:49 -0700296 if (Properties::debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700297 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700298
Chris Craik65fe5ee2015-01-26 18:06:29 -0800299 mRenderState.scissor().setEnabled(true);
300 mRenderState.scissor().set(clip->left,
301 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
302 clip->right - clip->left,
303 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700304
Romain Guy627c6fd2013-08-21 11:53:18 -0700305 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800306 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700307 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
308
309 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800310 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700311 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
312
313 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800314 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700315 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
316
317 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800318 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700319 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
320
Chris Craik96a5c4c2015-01-27 15:46:35 -0800321 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700322 }
323}
324
325///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700326// Layers
327///////////////////////////////////////////////////////////////////////////////
328
329bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700330 if (layer->deferredUpdateScheduled && layer->renderer
331 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700332
Romain Guy7c450aa2012-09-21 19:15:00 -0700333 if (inFrame) {
Romain Guy7c450aa2012-09-21 19:15:00 -0700334 debugOverdraw(false, false);
335 }
Romain Guy11cb6422012-09-21 00:39:43 -0700336
Chris Craik2507c342015-05-04 14:36:49 -0700337 if (CC_UNLIKELY(inFrame || Properties::drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700338 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700339 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700340 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700341 }
Romain Guy11cb6422012-09-21 00:39:43 -0700342
343 if (inFrame) {
344 resumeAfterLayer();
Romain Guy11cb6422012-09-21 00:39:43 -0700345 }
346
Chris Craik2507c342015-05-04 14:36:49 -0700347 layer->debugDrawUpdate = Properties::debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700348 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700349
350 return true;
351 }
352
353 return false;
354}
355
356void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700357 // If draw deferring is enabled this method will simply defer
358 // the display list of each individual layer. The layers remain
359 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700360 int count = mLayerUpdates.size();
361 if (count > 0) {
Chris Craik2507c342015-05-04 14:36:49 -0700362 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -0700363 startMark("Layer Updates");
364 } else {
365 startMark("Defer Layer Updates");
366 }
Romain Guy11cb6422012-09-21 00:39:43 -0700367
Chris Craik1206b9b2013-04-04 14:46:24 -0700368 // Note: it is very important to update the layers in order
369 for (int i = 0; i < count; i++) {
John Reck272a6852015-07-29 16:48:58 -0700370 Layer* layer = mLayerUpdates[i].get();
Romain Guy11cb6422012-09-21 00:39:43 -0700371 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700372 }
Romain Guy11cb6422012-09-21 00:39:43 -0700373
Chris Craik2507c342015-05-04 14:36:49 -0700374 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -0700375 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800376 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700377 }
378 endMark();
379 }
380}
381
382void OpenGLRenderer::flushLayers() {
383 int count = mLayerUpdates.size();
384 if (count > 0) {
385 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700386
Chris Craik1206b9b2013-04-04 14:46:24 -0700387 // Note: it is very important to update the layers in order
388 for (int i = 0; i < count; i++) {
John Reck272a6852015-07-29 16:48:58 -0700389 mLayerUpdates[i]->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700390 }
391
392 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800393 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700394
Romain Guy11cb6422012-09-21 00:39:43 -0700395 endMark();
396 }
397}
398
399void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
400 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700401 // Make sure we don't introduce duplicates.
402 // SortedVector would do this automatically but we need to respect
403 // the insertion order. The linear search is not an issue since
404 // this list is usually very short (typically one item, at most a few)
405 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
John Reck272a6852015-07-29 16:48:58 -0700406 if (mLayerUpdates[i] == layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700407 return;
408 }
409 }
Romain Guy11cb6422012-09-21 00:39:43 -0700410 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700411 }
412}
413
Romain Guye93482f2013-06-17 13:14:51 -0700414void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
415 if (layer) {
416 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
John Reck272a6852015-07-29 16:48:58 -0700417 if (mLayerUpdates[i] == layer) {
418 mLayerUpdates.erase(mLayerUpdates.begin() + i);
Romain Guye93482f2013-06-17 13:14:51 -0700419 break;
420 }
421 }
422 }
423}
424
Romain Guy40543602013-06-12 15:31:28 -0700425void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800426 ATRACE_NAME("Update HW Layers");
Chris Craik44eb2c02015-01-29 09:45:09 -0800427 mRenderState.blend().syncEnabled();
Romain Guy40543602013-06-12 15:31:28 -0700428 updateLayers();
429 flushLayers();
430 // Wait for all the layer updates to be executed
John Reck55156372015-01-21 07:46:37 -0800431 glFinish();
Romain Guy40543602013-06-12 15:31:28 -0700432}
433
John Reck443a7142014-09-04 17:40:05 -0700434void OpenGLRenderer::markLayersAsBuildLayers() {
435 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
436 mLayerUpdates[i]->wasBuildLayered = true;
437 }
438}
439
Romain Guy11cb6422012-09-21 00:39:43 -0700440///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700441// State management
442///////////////////////////////////////////////////////////////////////////////
443
Chris Craik14e51302013-12-30 15:32:54 -0800444void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700445 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800446 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
447 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700448
Chris Craika64a2be2014-05-14 14:17:01 -0700449 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700450 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700451 }
452
Romain Guy2542d192010-08-18 11:47:12 -0700453 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700454 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700455 }
Romain Guy2542d192010-08-18 11:47:12 -0700456
Romain Guy5ec99242010-11-03 16:19:08 -0700457 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700458 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700459 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700460 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800461 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700462 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700463 }
Romain Guybb9524b2010-06-22 18:56:38 -0700464}
465
Romain Guyf6a11b82010-06-23 17:47:49 -0700466///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700467// Layers
468///////////////////////////////////////////////////////////////////////////////
469
470int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700471 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700472 // force matrix/clip isolation for layer
473 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
474
Tom Hudson984162f2014-10-10 13:38:16 -0400475 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700476
Tom Hudson984162f2014-10-10 13:38:16 -0400477 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700478 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700479 }
Romain Guyd55a8612010-06-28 17:42:46 -0700480
481 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700482}
483
Chris Craikd90144d2013-03-19 15:03:48 -0700484void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
485 const Rect untransformedBounds(bounds);
486
Chris Craikd6b65f62014-01-01 14:45:21 -0800487 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700488
489 // Layers only make sense if they are in the framebuffer's bounds
Rob Tsuk487a92c2015-01-06 13:22:54 -0800490 if (bounds.intersect(mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700491 // We cannot work with sub-pixels in this case
492 bounds.snapToPixelBoundaries();
493
494 // When the layer is not an FBO, we may use glCopyTexImage so we
495 // need to make sure the layer does not extend outside the bounds
496 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700497 const Snapshot& previous = *(currentSnapshot()->previous);
498 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
499 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700500 bounds.setEmpty();
501 } else if (fboLayer) {
502 clip.set(bounds);
503 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800504 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700505 inverse.mapRect(clip);
506 clip.snapToPixelBoundaries();
507 if (clip.intersect(untransformedBounds)) {
508 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
509 bounds.set(untransformedBounds);
510 } else {
511 clip.setEmpty();
512 }
513 }
514 } else {
515 bounds.setEmpty();
516 }
517}
518
Chris Craik408eb122013-03-26 18:55:15 -0700519void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
520 bool fboLayer, int alpha) {
521 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
522 bounds.getHeight() > mCaches.maxTextureSize ||
523 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400524 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700525 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400526 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700527 }
528}
529
Chris Craikd90144d2013-03-19 15:03:48 -0700530int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500531 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400532 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700533
Tom Hudson984162f2014-10-10 13:38:16 -0400534 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700535 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
536 // operations will be able to store and restore the current clip and transform info, and
537 // quick rejection will be correct (for display lists)
538
539 Rect bounds(left, top, right, bottom);
540 Rect clip;
541 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500542 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700543
Tom Hudson984162f2014-10-10 13:38:16 -0400544 if (!mState.currentlyIgnored()) {
545 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
546 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
547 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800548 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700549 }
550 }
551
552 return count;
553}
554
Romain Guy1c740bc2010-09-13 18:00:09 -0700555/**
556 * Layers are viewed by Skia are slightly different than layers in image editing
557 * programs (for instance.) When a layer is created, previously created layers
558 * and the frame buffer still receive every drawing command. For instance, if a
559 * layer is created and a shape intersecting the bounds of the layers and the
560 * framebuffer is draw, the shape will be drawn on both (unless the layer was
561 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
562 *
563 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
564 * texture. Unfortunately, this is inefficient as it requires every primitive to
565 * be drawn n + 1 times, where n is the number of active layers. In practice this
566 * means, for every primitive:
567 * - Switch active frame buffer
568 * - Change viewport, clip and projection matrix
569 * - Issue the drawing
570 *
571 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700572 * To avoid this, layers are implemented in a different way here, at least in the
573 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
574 * is set. When this flag is set we can redirect all drawing operations into a
575 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700576 *
577 * This implementation relies on the frame buffer being at least RGBA 8888. When
578 * a layer is created, only a texture is created, not an FBO. The content of the
579 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700580 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700581 * buffer and drawing continues as normal. This technique therefore treats the
582 * frame buffer as a scratch buffer for the layers.
583 *
584 * To compose the layers back onto the frame buffer, each layer texture
585 * (containing the original frame buffer data) is drawn as a simple quad over
586 * the frame buffer. The trick is that the quad is set as the composition
587 * destination in the blending equation, and the frame buffer becomes the source
588 * of the composition.
589 *
590 * Drawing layers with an alpha value requires an extra step before composition.
591 * An empty quad is drawn over the layer's region in the frame buffer. This quad
592 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
593 * quad is used to multiply the colors in the frame buffer. This is achieved by
594 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
595 * GL_ZERO, GL_SRC_ALPHA.
596 *
597 * Because glCopyTexImage2D() can be slow, an alternative implementation might
598 * be use to draw a single clipped layer. The implementation described above
599 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700600 *
601 * (1) The frame buffer is actually not cleared right away. To allow the GPU
602 * to potentially optimize series of calls to glCopyTexImage2D, the frame
603 * buffer is left untouched until the first drawing operation. Only when
604 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700605 */
Chet Haased48885a2012-08-28 17:43:28 -0700606bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700607 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800608 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
609 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700610
Romain Guyeb993562010-10-05 18:14:38 -0700611 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
612
Romain Guyf607bdc2010-09-10 19:20:06 -0700613 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700614 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700615 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700616 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000617 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700618
619 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400620 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700621 return false;
622 }
Romain Guyf18fd992010-07-08 11:45:51 -0700623
Chris Craik44eb2c02015-01-29 09:45:09 -0800624 mCaches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700625 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700626 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700627 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700628 }
629
Derek Sollenberger674554f2014-02-19 16:47:32 +0000630 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700631 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700632 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
633 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500634
Chet Haasea23eed82012-04-12 15:19:04 -0700635 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700636 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700637 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700638
Romain Guy8fb95422010-08-17 18:38:51 -0700639 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400640 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
641 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700642
Chris Craika8bea8e2014-09-24 11:29:43 -0700643 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
644 fboLayer ? "" : "unclipped ",
645 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700646 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700647 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700648 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700649 } else {
650 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700651 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800652 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700653 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700654 // Workaround for some GL drivers. When reading pixels lying outside
655 // of the window we should get undefined values for those pixels.
656 // Unfortunately some drivers will turn the entire target texture black
657 // when reading outside of the window.
658 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800659 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700660 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800661 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700662
Chris Craika64a2be2014-05-14 14:17:01 -0700663 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
664 bounds.left, getViewportHeight() - bounds.bottom,
665 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700666
Romain Guy54be1cd2011-06-13 19:04:27 -0700667 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800668 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700669 }
Romain Guyeb993562010-10-05 18:14:38 -0700670 }
Romain Guyf86ef572010-07-01 11:05:42 -0700671
Romain Guyd55a8612010-06-28 17:42:46 -0700672 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700673}
674
Chris Craike63f7c622013-10-17 10:30:55 -0700675bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800676 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700677 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700678
Tom Hudson984162f2014-10-10 13:38:16 -0400679 writableSnapshot()->region = &writableSnapshot()->layer->region;
680 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
681 writableSnapshot()->fbo = layer->getFbo();
682 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
683 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
684 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800685 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700686
Romain Guy7c450aa2012-09-21 19:15:00 -0700687 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700688 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700689 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700690 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700691
692 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700693 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700694 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700695 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700696 }
697
698 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Chris Craikf27133d2015-02-19 09:51:53 -0800699 layer->getTextureId(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700700
Romain Guy5b3b3522010-10-27 18:57:51 -0700701 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800702 mRenderState.scissor().setEnabled(true);
703 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700704 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700705 glClear(GL_COLOR_BUFFER_BIT);
706
707 dirtyClip();
708
709 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700710 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700711 return true;
712}
713
Romain Guy1c740bc2010-09-13 18:00:09 -0700714/**
715 * Read the documentation of createLayer() before doing anything in this method.
716 */
Chris Craik14e51302013-12-30 15:32:54 -0800717void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
718 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000719 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700720 return;
721 }
722
Chris Craik14e51302013-12-30 15:32:54 -0800723 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800724 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800725 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700726
Chris Craik39a908c2013-06-13 14:39:01 -0700727 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400728 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800729 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800730 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700731
Romain Guyeb993562010-10-05 18:14:38 -0700732 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700733 // Detach the texture from the FBO
734 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800735
736 layer->removeFbo(false);
737
Romain Guyeb993562010-10-05 18:14:38 -0700738 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700739 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700740 debugOverdraw(true, false);
Romain Guyeb993562010-10-05 18:14:38 -0700741 }
742
Romain Guy9ace8f52011-07-07 20:50:11 -0700743 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500744 SkPaint layerPaint;
745 layerPaint.setAlpha(layer->getAlpha());
746 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
747 layerPaint.setColorFilter(layer->getColorFilter());
748
749 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700750 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700751 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700752 }
753
Chris Craik96a5c4c2015-01-27 15:46:35 -0800754 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700755
Chris Craik44eb2c02015-01-29 09:45:09 -0800756 mCaches.textureState().activateTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700757
Romain Guy5b3b3522010-10-27 18:57:51 -0700758 // When the layer is stored in an FBO, we can save a bit of fillrate by
759 // drawing only the dirty region
760 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800761 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700762 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700763 } else if (!rect.isEmpty()) {
764 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530765
766 save(0);
767 // the layer contains screen buffer content that shouldn't be alpha modulated
768 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400769 writableSnapshot()->alpha = 1.0f;
Chris Craik53e51e42015-06-01 10:35:35 -0700770 composeLayerRectSwapped(layer, rect);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530771 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700772 }
Romain Guy8b55f372010-08-18 17:10:07 -0700773
Romain Guy746b7402010-10-26 16:27:31 -0700774 dirtyClip();
775
Romain Guyeb993562010-10-05 18:14:38 -0700776 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800777 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700778 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800779 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800780 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700781 }
782}
783
Romain Guyaa6c24c2011-04-28 18:40:04 -0700784void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craik1e4c8072015-05-26 14:25:02 -0700785 const bool tryToSnap = !layer->getForceFilter()
Chris Craik26bf3422015-02-26 16:28:17 -0800786 && layer->getWidth() == (uint32_t) rect.getWidth()
Chris Craik82840732015-04-03 09:37:49 -0700787 && layer->getHeight() == (uint32_t) rect.getHeight();
788 Glop glop;
789 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700790 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -0700791 .setMeshTexturedUvQuad(nullptr, Rect(0, 1, 1, 0)) // TODO: simplify with VBO
792 .setFillTextureLayer(*layer, getLayerAlpha(layer))
Chris Craik53e51e42015-06-01 10:35:35 -0700793 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik1e4c8072015-05-26 14:25:02 -0700794 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, rect)
Chris Craik82840732015-04-03 09:37:49 -0700795 .build();
796 renderGlop(glop);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700797}
798
Chris Craik53e51e42015-06-01 10:35:35 -0700799void OpenGLRenderer::composeLayerRectSwapped(Layer* layer, const Rect& rect) {
800 Glop glop;
801 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700802 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik53e51e42015-06-01 10:35:35 -0700803 .setMeshTexturedUvQuad(nullptr, layer->texCoords)
804 .setFillLayer(layer->getTexture(), layer->getColorFilter(),
805 getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::Swap)
806 .setTransform(*currentSnapshot(), TransformFlags::MeshIgnoresCanvasTransform)
807 .setModelViewMapUnitToRect(rect)
Chris Craik53e51e42015-06-01 10:35:35 -0700808 .build();
809 renderGlop(glop);
810}
811
812void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect) {
Chris Craik62d307c2014-07-29 10:35:13 -0700813 if (layer->isTextureLayer()) {
814 EVENT_LOGD("composeTextureLayerRect");
Chris Craik62d307c2014-07-29 10:35:13 -0700815 drawTextureLayer(layer, rect);
Chris Craik62d307c2014-07-29 10:35:13 -0700816 } else {
817 EVENT_LOGD("composeHardwareLayerRect");
Chris Craik182952f2015-03-09 14:17:29 -0700818
Chris Craik53e51e42015-06-01 10:35:35 -0700819 const bool tryToSnap = layer->getWidth() == static_cast<uint32_t>(rect.getWidth())
Chris Craik82840732015-04-03 09:37:49 -0700820 && layer->getHeight() == static_cast<uint32_t>(rect.getHeight());
821 Glop glop;
822 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700823 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -0700824 .setMeshTexturedUvQuad(nullptr, layer->texCoords)
Chris Craik53e51e42015-06-01 10:35:35 -0700825 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
826 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik1e4c8072015-05-26 14:25:02 -0700827 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, rect)
Chris Craik82840732015-04-03 09:37:49 -0700828 .build();
829 renderGlop(glop);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700830 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700831}
832
Chris Craik34416ea2013-04-15 16:08:28 -0700833/**
834 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
835 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
836 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
837 * by saveLayer's restore
838 */
Chris Craik2507c342015-05-04 14:36:49 -0700839#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
840 DRAW_COMMAND; \
841 if (CC_UNLIKELY(Properties::debugOverdraw && getTargetFbo() == 0 && COND)) { \
842 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
843 DRAW_COMMAND; \
844 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
845 } \
Chris Craik34416ea2013-04-15 16:08:28 -0700846 }
847
848#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
849
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400850// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
851// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
852class LayerShader : public SkShader {
853public:
854 LayerShader(Layer* layer, const SkMatrix* localMatrix)
855 : INHERITED(localMatrix)
856 , mLayer(layer) {
857 }
858
Chris Craike84a2082014-12-22 14:28:49 -0800859 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400860 if (data) {
861 *data = static_cast<void*>(mLayer);
862 }
863 return true;
864 }
865
Chris Craike84a2082014-12-22 14:28:49 -0800866 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400867 return !mLayer->isBlend();
868 }
869
870protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000871 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400872 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
873 }
874
Chris Craike84a2082014-12-22 14:28:49 -0800875 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400876 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
877 }
878
Chris Craike84a2082014-12-22 14:28:49 -0800879 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400880 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -0800881 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400882 }
883private:
884 // Unowned.
885 Layer* mLayer;
886 typedef SkShader INHERITED;
887};
888
Romain Guy5b3b3522010-10-27 18:57:51 -0700889void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -0700890 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
891
892 if (layer->getConvexMask()) {
893 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
894
895 // clip to the area of the layer the mask can be larger
896 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
897
898 SkPaint paint;
899 paint.setAntiAlias(true);
900 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
901
Chris Craik3f0854292014-04-15 16:18:08 -0700902 // create LayerShader to map SaveLayer content into subsequent draw
903 SkMatrix shaderMatrix;
904 shaderMatrix.setTranslate(rect.left, rect.bottom);
905 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400906 LayerShader layerShader(layer, &shaderMatrix);
907 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -0700908
909 // Since the drawing primitive is defined in local drawing space,
910 // we don't need to modify the draw matrix
911 const SkPath* maskPath = layer->getConvexMask();
912 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
913
Chris Craike84a2082014-12-22 14:28:49 -0800914 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -0700915 restore();
916
917 return;
918 }
919
Romain Guy5b3b3522010-10-27 18:57:51 -0700920 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700921 layer->setRegionAsRect();
922
Chris Craik34416ea2013-04-15 16:08:28 -0700923 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -0700924
Romain Guy5b3b3522010-10-27 18:57:51 -0700925 layer->region.clear();
926 return;
927 }
928
Chris Craik62d307c2014-07-29 10:35:13 -0700929 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -0700930 // standard Region based draw
931 size_t count;
932 const android::Rect* rects;
933 Region safeRegion;
934 if (CC_LIKELY(hasRectToRectTransform())) {
935 rects = layer->region.getArray(&count);
936 } else {
937 safeRegion = Region::createTJunctionFreeRegion(layer->region);
938 rects = safeRegion.getArray(&count);
939 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700940
Chris Craik3f0854292014-04-15 16:18:08 -0700941 const float texX = 1.0f / float(layer->getWidth());
942 const float texY = 1.0f / float(layer->getHeight());
943 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700944
Chris Craik82840732015-04-03 09:37:49 -0700945 TextureVertex quadVertices[count * 4];
946 TextureVertex* mesh = &quadVertices[0];
Chris Craik3f0854292014-04-15 16:18:08 -0700947 for (size_t i = 0; i < count; i++) {
948 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -0700949
Chris Craik3f0854292014-04-15 16:18:08 -0700950 const float u1 = r->left * texX;
951 const float v1 = (height - r->top) * texY;
952 const float u2 = r->right * texX;
953 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700954
Chris Craik3f0854292014-04-15 16:18:08 -0700955 // TODO: Reject quads outside of the clip
956 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
957 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
958 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
959 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Chris Craik3f0854292014-04-15 16:18:08 -0700960 }
Tom Hudson040b6d82015-04-16 10:50:53 -0400961 Rect modelRect = Rect(rect.getWidth(), rect.getHeight());
Chris Craik82840732015-04-03 09:37:49 -0700962 Glop glop;
963 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700964 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -0700965 .setMeshTexturedIndexedQuads(&quadVertices[0], count * 6)
966 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
Chris Craik53e51e42015-06-01 10:35:35 -0700967 .setTransform(*currentSnapshot(), TransformFlags::None)
Tom Hudson040b6d82015-04-16 10:50:53 -0400968 .setModelViewOffsetRectSnap(rect.left, rect.top, modelRect)
Chris Craik82840732015-04-03 09:37:49 -0700969 .build();
970 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy5b3b3522010-10-27 18:57:51 -0700971
Romain Guy5b3b3522010-10-27 18:57:51 -0700972#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -0700973 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700974#endif
975
Chris Craik3f0854292014-04-15 16:18:08 -0700976 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -0700977}
978
Romain Guy3a3133d2011-02-01 22:59:58 -0800979#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -0700980void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -0800981 size_t count;
982 const android::Rect* rects = region.getArray(&count);
983
984 uint32_t colors[] = {
985 0x7fff0000, 0x7f00ff00,
986 0x7f0000ff, 0x7fff00ff,
987 };
988
989 int offset = 0;
990 int32_t top = rects[0].top;
991
992 for (size_t i = 0; i < count; i++) {
993 if (top != rects[i].top) {
994 offset ^= 0x2;
995 top = rects[i].top;
996 }
997
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500998 SkPaint paint;
999 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001000 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001001 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001002 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001003}
Chris Craike63f7c622013-10-17 10:30:55 -07001004#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001005
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001006void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001007 Vector<float> rects;
1008
1009 SkRegion::Iterator it(region);
1010 while (!it.done()) {
1011 const SkIRect& r = it.rect();
1012 rects.push(r.fLeft);
1013 rects.push(r.fTop);
1014 rects.push(r.fRight);
1015 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001016 it.next();
1017 }
1018
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001019 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001020}
1021
Romain Guy5b3b3522010-10-27 18:57:51 -07001022void OpenGLRenderer::dirtyLayer(const float left, const float top,
Chris Craik083e7332015-02-27 17:04:20 -08001023 const float right, const float bottom, const Matrix4& transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001024 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001025 Rect bounds(left, top, right, bottom);
1026 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001027 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001028 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001029}
1030
1031void OpenGLRenderer::dirtyLayer(const float left, const float top,
1032 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001033 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001034 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001035 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001036 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001037}
1038
1039void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Chris Craik3375f8a2015-06-23 17:33:06 -07001040 if (CC_LIKELY(!bounds.isEmpty() && bounds.intersect(mState.currentClipRect()))) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001041 bounds.snapToPixelBoundaries();
1042 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1043 if (!dirty.isEmpty()) {
1044 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001045 }
1046 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001047}
1048
Romain Guy54be1cd2011-06-13 19:04:27 -07001049void OpenGLRenderer::clearLayerRegions() {
Chris Craik2bb8f562015-02-17 16:42:02 -08001050 const size_t quadCount = mLayers.size();
1051 if (quadCount == 0) return;
Romain Guy54be1cd2011-06-13 19:04:27 -07001052
Tom Hudson984162f2014-10-10 13:38:16 -04001053 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001054 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001055 // Doing several glScissor/glClear here can negatively impact
1056 // GPUs with a tiler architecture, instead we draw quads with
1057 // the Clear blending mode
1058
1059 // The list contains bounds that have already been clipped
1060 // against their initial clip rect, and the current clip
1061 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001062 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001063
Chris Craik2bb8f562015-02-17 16:42:02 -08001064 Vertex mesh[quadCount * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001065 Vertex* vertex = mesh;
1066
Chris Craik2bb8f562015-02-17 16:42:02 -08001067 for (uint32_t i = 0; i < quadCount; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001068 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001069
Chris Craik51d6a3d2014-12-22 17:16:56 -08001070 Vertex::set(vertex++, bounds.left, bounds.top);
1071 Vertex::set(vertex++, bounds.right, bounds.top);
1072 Vertex::set(vertex++, bounds.left, bounds.bottom);
1073 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001074 }
Romain Guye67307c2013-02-11 18:01:20 -08001075 // We must clear the list of dirty rects before we
Chris Craik82840732015-04-03 09:37:49 -07001076 // call clearLayerRegions() in renderGlop to prevent
1077 // stencil setup from doing the same thing again
Romain Guye67307c2013-02-11 18:01:20 -08001078 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001079
Chris Craik53e51e42015-06-01 10:35:35 -07001080 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001081 Glop glop;
1082 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001083 .setRoundRectClipState(nullptr) // clear ignores clip state
Chris Craik82840732015-04-03 09:37:49 -07001084 .setMeshIndexedQuads(&mesh[0], quadCount)
1085 .setFillClear()
Chris Craik53e51e42015-06-01 10:35:35 -07001086 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001087 .setModelViewOffsetRect(0, 0, Rect(currentSnapshot()->getClipRect()))
Chris Craik82840732015-04-03 09:37:49 -07001088 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001089 renderGlop(glop, GlopRenderType::LayerClear);
Romain Guy8a4ac612012-07-17 17:32:48 -07001090
Chris Craik65fe5ee2015-01-26 18:06:29 -08001091 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001092 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001093 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001094 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001095}
1096
Romain Guybd6b79b2010-06-26 00:13:53 -07001097///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001098// State Deferral
1099///////////////////////////////////////////////////////////////////////////////
1100
Chris Craikff785832013-03-08 13:12:16 -08001101bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001102 const Rect& currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001103 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001104
Chris Craikff785832013-03-08 13:12:16 -08001105 if (stateDeferFlags & kStateDeferFlag_Draw) {
1106 // state has bounds initialized in local coordinates
1107 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001108 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001109 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001110 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1111 // is used, it should more closely duplicate the quickReject logic (in how it uses
1112 // snapToPixelBoundaries)
1113
Rob Tsuk487a92c2015-01-06 13:22:54 -08001114 if (!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001115 // quick rejected
1116 return true;
1117 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001118
Chris Craika02c4ed2013-06-14 13:43:58 -07001119 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001120 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001121 int& flags = state.mClipSideFlags;
1122 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001123 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1124 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1125 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1126 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001127 }
1128 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001129 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001130 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1131 // overdraw avoidance (since we don't know what it overlaps)
1132 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001133 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001134 }
1135 }
1136
Chris Craik527a3aa2013-03-04 10:19:31 -08001137 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1138 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001139 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001140 }
1141
Chris Craik8df5ffa2015-04-28 17:47:20 -07001142 // Transform and alpha always deferred, since they are used by state operations
Chris Craik7273daa2013-03-28 11:25:24 -07001143 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001144 state.mMatrix.load(*currentMatrix);
Chris Craikd6b65f62014-01-01 14:45:21 -08001145 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001146
Chris Craikfca52b752015-04-28 11:45:59 -07001147 // always store/restore, since these are just pointers
Chris Craikdeeda3d2014-05-05 19:09:33 -07001148 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikfca52b752015-04-28 11:45:59 -07001149 state.mProjectionPathMask = currentSnapshot()->projectionPathMask;
Chris Craikc3566d02013-02-04 16:16:33 -08001150 return false;
1151}
1152
Chris Craik527a3aa2013-03-04 10:19:31 -08001153void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik6daa13c2015-08-19 13:32:12 -07001154 setGlobalMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001155 writableSnapshot()->alpha = state.mAlpha;
Tom Hudson984162f2014-10-10 13:38:16 -04001156 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikfca52b752015-04-28 11:45:59 -07001157 writableSnapshot()->projectionPathMask = state.mProjectionPathMask;
Chris Craikff785832013-03-08 13:12:16 -08001158
Chris Craik527a3aa2013-03-04 10:19:31 -08001159 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001160 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001161 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001162 dirtyClip();
1163 }
Chris Craikc3566d02013-02-04 16:16:33 -08001164}
1165
Chris Craik28ce94a2013-05-31 11:38:03 -07001166/**
1167 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1168 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1169 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1170 *
1171 * This method should be called when restoreDisplayState() won't be restoring the clip
1172 */
1173void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001174 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001175 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001176 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001177 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001178 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001179 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001180 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1181 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001182}
1183
Chris Craikc3566d02013-02-04 16:16:33 -08001184///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001185// Clipping
1186///////////////////////////////////////////////////////////////////////////////
1187
Romain Guybb9524b2010-06-22 18:56:38 -07001188void OpenGLRenderer::setScissorFromClip() {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001189 Rect clip(mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001190 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001191
Chris Craik65fe5ee2015-01-26 18:06:29 -08001192 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001193 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001194 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001195 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001196}
1197
Romain Guy8ce00302013-01-15 18:51:42 -08001198void OpenGLRenderer::ensureStencilBuffer() {
1199 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1200 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1201 // just hope we have one when hasLayer() returns false.
1202 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001203 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001204 }
1205}
1206
1207void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1208 // The layer's FBO is already bound when we reach this stage
1209 if (!layer->getStencilRenderBuffer()) {
Romain Guy8d4aeb72013-02-12 16:08:55 -08001210 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Chris Craike1450132015-04-28 17:55:50 -07001211 Stencil::getLayerStencilFormat(),
Chris Craik117bdbc2015-02-05 10:12:38 -08001212 layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001213 layer->setStencilRenderBuffer(buffer);
1214 }
1215}
1216
Rob Tsuk487a92c2015-01-06 13:22:54 -08001217static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1218 float x, float y) {
1219 Vertex v;
1220 v.x = x;
1221 v.y = y;
1222 transform.mapPoint(v.x, v.y);
1223 rectangleVertices.push_back(v);
1224}
1225
1226static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1227 Vertex v;
1228 v.x = x;
1229 v.y = y;
1230 rectangleVertices.push_back(v);
1231}
1232
1233void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
Chris Craik2bb8f562015-02-17 16:42:02 -08001234 int quadCount = rectangleList.getTransformedRectanglesCount();
1235 std::vector<Vertex> rectangleVertices(quadCount * 4);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001236 Rect scissorBox = rectangleList.calculateBounds();
1237 scissorBox.snapToPixelBoundaries();
Chris Craik2bb8f562015-02-17 16:42:02 -08001238 for (int i = 0; i < quadCount; ++i) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001239 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1240 const Matrix4& transform = tr.getTransform();
1241 Rect bounds = tr.getBounds();
1242 if (transform.rectToRect()) {
1243 transform.mapRect(bounds);
1244 if (!bounds.intersect(scissorBox)) {
1245 bounds.setEmpty();
1246 } else {
1247 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1248 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1249 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1250 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1251 }
1252 } else {
1253 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1254 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1255 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1256 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1257 }
1258 }
1259
Chris Craik65fe5ee2015-01-26 18:06:29 -08001260 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001261 scissorBox.getWidth(), scissorBox.getHeight());
Chris Craik53e51e42015-06-01 10:35:35 -07001262 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001263 Glop glop;
1264 Vertex* vertices = &rectangleVertices[0];
1265 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001266 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001267 .setMeshIndexedQuads(vertices, rectangleVertices.size() / 4)
1268 .setFillBlack()
Chris Craik53e51e42015-06-01 10:35:35 -07001269 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001270 .setModelViewOffsetRect(0, 0, scissorBox)
Chris Craik82840732015-04-03 09:37:49 -07001271 .build();
1272 renderGlop(glop);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001273}
1274
Romain Guy8ce00302013-01-15 18:51:42 -08001275void OpenGLRenderer::setStencilFromClip() {
Chris Craik2507c342015-05-04 14:36:49 -07001276 if (!Properties::debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001277 if (!currentSnapshot()->clipIsSimple()) {
1278 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001279 EVENT_LOGD("setStencilFromClip - enabling");
1280
Romain Guy8ce00302013-01-15 18:51:42 -08001281 // NOTE: The order here is important, we must set dirtyClip to false
1282 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001283 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001284
1285 ensureStencilBuffer();
1286
Rob Tsuk487a92c2015-01-06 13:22:54 -08001287 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001288
Rob Tsuk487a92c2015-01-06 13:22:54 -08001289 bool isRectangleList = clipArea.isRectangleList();
1290 if (isRectangleList) {
1291 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1292 } else {
1293 incrementThreshold = 0;
1294 }
1295
Chris Craik96a5c4c2015-01-27 15:46:35 -08001296 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001297
1298 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001299 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001300 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001301 if (resetScissor) {
1302 // The scissor was not set so we now need to update it
1303 setScissorFromClip();
1304 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001305
Chris Craik96a5c4c2015-01-27 15:46:35 -08001306 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001307
1308 // stash and disable the outline clip state, since stencil doesn't account for outline
1309 bool storedSkipOutlineClip = mSkipOutlineClip;
1310 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001311
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001312 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001313 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001314 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1315
Rob Tsuk487a92c2015-01-06 13:22:54 -08001316 if (isRectangleList) {
1317 drawRectangleList(clipArea.getRectangleList());
1318 } else {
1319 // NOTE: We could use the region contour path to generate a smaller mesh
1320 // Since we are using the stencil we could use the red book path
1321 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001322
Rob Tsuk487a92c2015-01-06 13:22:54 -08001323 // The last parameter is important: we are not drawing in the color buffer
1324 // so we don't want to dirty the current layer, if any
1325 drawRegionRects(clipArea.getClipRegion(), paint, false);
1326 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001327 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001328 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001329
Chris Craik96a5c4c2015-01-27 15:46:35 -08001330 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001331
1332 // Draw the region used to generate the stencil if the appropriate debug
1333 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001334 // TODO: Implement for rectangle list clip areas
Chris Craik2507c342015-05-04 14:36:49 -07001335 if (Properties::debugStencilClip == StencilClipDebug::ShowRegion
1336 && !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001337 paint.setColor(0x7f0000ff);
1338 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001339 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001340 }
Romain Guy8ce00302013-01-15 18:51:42 -08001341 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001342 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001343 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001344 }
1345 }
1346}
1347
Chris Craikf0a59072013-11-19 18:00:46 -08001348/**
1349 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1350 *
1351 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1352 * style, and tessellated AA ramp
1353 */
1354bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001355 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001356 bool snapOut = paint && paint->isAntiAlias();
1357
1358 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1359 float outset = paint->getStrokeWidth() * 0.5f;
1360 left -= outset;
1361 top -= outset;
1362 right += outset;
1363 bottom += outset;
1364 }
1365
Chris Craikdeeda3d2014-05-05 19:09:33 -07001366 bool clipRequired = false;
1367 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001368 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001369 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001370 return true;
1371 }
1372
Chris Craikf23b25a2014-06-26 15:46:20 -07001373 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001374 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001375 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001376 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001377}
1378
Romain Guy8ce00302013-01-15 18:51:42 -08001379void OpenGLRenderer::debugClip() {
1380#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001381 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001382 SkPaint paint;
1383 paint.setColor(0x7f00ff00);
1384 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1385
Romain Guy8ce00302013-01-15 18:51:42 -08001386 }
1387#endif
1388}
1389
Chris Craik3375f8a2015-06-23 17:33:06 -07001390void OpenGLRenderer::renderGlop(const Glop& glop, GlopRenderType type) {
Chris Craik6b109c72015-02-27 10:55:28 -08001391 // TODO: It would be best if we could do this before quickRejectSetupScissor()
1392 // changes the scissor test state
Chris Craik3375f8a2015-06-23 17:33:06 -07001393 if (type != GlopRenderType::LayerClear) {
1394 // Regular draws need to clear the dirty area on the layer before they start drawing on top
1395 // of it. If this draw *is* a layer clear, it skips the clear step (since it would
1396 // infinitely recurse)
1397 clearLayerRegions();
1398 }
Chris Craik6b109c72015-02-27 10:55:28 -08001399
Chris Craik117bdbc2015-02-05 10:12:38 -08001400 if (mState.getDirtyClip()) {
1401 if (mRenderState.scissor().isEnabled()) {
1402 setScissorFromClip();
1403 }
1404
1405 setStencilFromClip();
1406 }
1407 mRenderState.render(glop);
Chris Craik3375f8a2015-06-23 17:33:06 -07001408 if (type == GlopRenderType::Standard && !mRenderState.stencil().isWriteEnabled()) {
Chris Craik2ab95d72015-02-06 15:25:51 -08001409 // TODO: specify more clearly when a draw should dirty the layer.
1410 // is writing to the stencil the only time we should ignore this?
1411 dirtyLayer(glop.bounds.left, glop.bounds.top, glop.bounds.right, glop.bounds.bottom);
Chris Craik0519c8102015-02-11 13:17:06 -08001412 mDirty = true;
Chris Craik2ab95d72015-02-06 15:25:51 -08001413 }
Chris Craik117bdbc2015-02-05 10:12:38 -08001414}
1415
Romain Guyf6a11b82010-06-23 17:47:49 -07001416///////////////////////////////////////////////////////////////////////////////
1417// Drawing
1418///////////////////////////////////////////////////////////////////////////////
1419
Tom Hudson107843d2014-09-08 11:26:26 -04001420void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001421 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1422 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001423 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001424 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001425 renderNode->computeOrdering();
Chris Craik2507c342015-05-04 14:36:49 -07001426 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001427 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001428 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001429 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001430 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001431 }
1432
Chris Craikef8d6f22014-12-17 11:10:28 -08001433 // Don't avoid overdraw when visualizing, since that makes it harder to
1434 // debug where it's coming from, and when the problem occurs.
Chris Craik2507c342015-05-04 14:36:49 -07001435 bool avoidOverdraw = !Properties::debugOverdraw;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001436 DeferredDisplayList deferredList(mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001437 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001438 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001439
1440 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001441 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001442
Tom Hudson107843d2014-09-08 11:26:26 -04001443 deferredList.flush(*this, dirty);
1444 } else {
1445 // Even if there is no drawing command(Ex: invisible),
1446 // it still needs startFrame to clear buffer and start tiling.
1447 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001448 }
1449}
1450
Romain Guy03c00b52013-06-20 18:30:28 -07001451/**
1452 * Important note: this method is intended to draw batches of bitmaps and
1453 * will not set the scissor enable or dirty the current layer, if any.
1454 * The caller is responsible for properly dirtying the current layer.
1455 */
Tom Hudson107843d2014-09-08 11:26:26 -04001456void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001457 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1458 const Rect& bounds, const SkPaint* paint) {
Romain Guy55b6f952013-06-27 15:27:09 -07001459 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001460 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001461
Chris Craik527a3aa2013-03-04 10:19:31 -08001462 const AutoTexture autoCleanup(texture);
1463
Chris Craik82840732015-04-03 09:37:49 -07001464 // TODO: remove layer dirty in multi-draw callers
1465 // TODO: snap doesn't need to touch transform, only texture filter.
1466 bool snap = pureTranslate;
Chris Craika6b52192015-02-27 17:43:02 -08001467 const float x = floorf(bounds.left + 0.5f);
1468 const float y = floorf(bounds.top + 0.5f);
Chris Craik53e51e42015-06-01 10:35:35 -07001469
1470 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1471 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
1472 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001473 Glop glop;
1474 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001475 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001476 .setMeshTexturedMesh(vertices, bitmapCount * 6)
1477 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001478 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001479 .setModelViewOffsetRectOptionalSnap(snap, x, y, Rect(0, 0, bounds.getWidth(), bounds.getHeight()))
Chris Craik82840732015-04-03 09:37:49 -07001480 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001481 renderGlop(glop, GlopRenderType::Multi);
Chris Craik527a3aa2013-03-04 10:19:31 -08001482}
1483
Tom Hudson107843d2014-09-08 11:26:26 -04001484void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07001485 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04001486 return;
Romain Guy6926c722010-07-12 20:20:03 -07001487 }
1488
Chris Craik44eb2c02015-01-29 09:45:09 -08001489 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07001490 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001491 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001492 const AutoTexture autoCleanup(texture);
1493
Chris Craik53e51e42015-06-01 10:35:35 -07001494 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1495 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07001496 Glop glop;
1497 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001498 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001499 .setMeshTexturedUnitQuad(texture->uvMapper)
1500 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001501 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001502 .setModelViewMapUnitToRectSnap(Rect(0, 0, texture->width, texture->height))
Chris Craik82840732015-04-03 09:37:49 -07001503 .build();
1504 renderGlop(glop);
Romain Guyce0537b2010-06-29 21:05:21 -07001505}
1506
Tom Hudson107843d2014-09-08 11:26:26 -04001507void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08001508 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001509 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04001510 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08001511 }
1512
Romain Guyb18d2d02011-02-10 15:52:54 -08001513 float left = FLT_MAX;
1514 float top = FLT_MAX;
1515 float right = FLT_MIN;
1516 float bottom = FLT_MIN;
1517
Chris Craikef250742015-02-25 17:16:16 -08001518 const uint32_t elementCount = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001519
Chris Craikef250742015-02-25 17:16:16 -08001520 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[elementCount]);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001521 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08001522
Chris Craik51d6a3d2014-12-22 17:16:56 -08001523 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08001524 if (!colors) {
1525 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001526 tempColors.reset(new int[colorsCount]);
1527 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
1528 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08001529 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001530
John Reckebd52612014-12-10 16:47:36 -08001531 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07001532 const UvMapper& mapper(getMapper(texture));
1533
Romain Guy5a7b4662011-01-20 19:09:30 -08001534 for (int32_t y = 0; y < meshHeight; y++) {
1535 for (int32_t x = 0; x < meshWidth; x++) {
1536 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1537
1538 float u1 = float(x) / meshWidth;
1539 float u2 = float(x + 1) / meshWidth;
1540 float v1 = float(y) / meshHeight;
1541 float v2 = float(y + 1) / meshHeight;
1542
Romain Guy3b748a42013-04-17 18:54:38 -07001543 mapper.map(u1, v1, u2, v2);
1544
Romain Guy5a7b4662011-01-20 19:09:30 -08001545 int ax = i + (meshWidth + 1) * 2;
1546 int ay = ax + 1;
1547 int bx = i;
1548 int by = bx + 1;
1549 int cx = i + 2;
1550 int cy = cx + 1;
1551 int dx = i + (meshWidth + 1) * 2 + 2;
1552 int dy = dx + 1;
1553
Romain Guyff316ec2013-02-13 18:39:43 -08001554 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1555 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
1556 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08001557
Romain Guyff316ec2013-02-13 18:39:43 -08001558 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1559 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
1560 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08001561
Chris Craikdf72b632015-06-30 17:56:13 -07001562 left = std::min(left, std::min(vertices[ax], std::min(vertices[bx], vertices[cx])));
1563 top = std::min(top, std::min(vertices[ay], std::min(vertices[by], vertices[cy])));
1564 right = std::max(right, std::max(vertices[ax], std::max(vertices[bx], vertices[cx])));
1565 bottom = std::max(bottom, std::max(vertices[ay], std::max(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001566 }
1567 }
1568
Chris Craikf0a59072013-11-19 18:00:46 -08001569 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001570 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07001571 }
1572
Romain Guyff316ec2013-02-13 18:39:43 -08001573 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07001574 texture = mCaches.textureCache.get(bitmap);
1575 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04001576 return;
Romain Guy3b748a42013-04-17 18:54:38 -07001577 }
Romain Guyff316ec2013-02-13 18:39:43 -08001578 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001579 const AutoTexture autoCleanup(texture);
1580
Chris Craik82840732015-04-03 09:37:49 -07001581 /*
1582 * TODO: handle alpha_8 textures correctly by applying paint color, but *not*
1583 * shader in that case to mimic the behavior in SkiaCanvas::drawBitmapMesh.
1584 */
Chris Craik53e51e42015-06-01 10:35:35 -07001585 const int textureFillFlags = TextureFillFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07001586 Glop glop;
1587 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001588 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001589 .setMeshColoredTexturedMesh(mesh.get(), elementCount)
Chris Craik53e51e42015-06-01 10:35:35 -07001590 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1591 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001592 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07001593 .build();
1594 renderGlop(glop);
Romain Guy5a7b4662011-01-20 19:09:30 -08001595}
1596
Chris Craik14100ac2015-02-24 13:46:29 -08001597void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, Rect src, Rect dst, const SkPaint* paint) {
1598 if (quickRejectSetupScissor(dst)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001599 return;
Romain Guy6926c722010-07-12 20:20:03 -07001600 }
1601
Romain Guy3b748a42013-04-17 18:54:38 -07001602 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001603 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001604 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001605
Chris Craike6a15ee2015-07-07 18:42:17 -07001606 Rect uv(std::max(0.0f, src.left / texture->width),
1607 std::max(0.0f, src.top / texture->height),
1608 std::min(1.0f, src.right / texture->width),
1609 std::min(1.0f, src.bottom / texture->height));
Chris Craik14100ac2015-02-24 13:46:29 -08001610
Chris Craik53e51e42015-06-01 10:35:35 -07001611 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1612 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
Chris Craik1e4c8072015-05-26 14:25:02 -07001613 const bool tryToSnap = MathUtils::areEqual(src.getWidth(), dst.getWidth())
1614 && MathUtils::areEqual(src.getHeight(), dst.getHeight());
Chris Craik82840732015-04-03 09:37:49 -07001615 Glop glop;
1616 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001617 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001618 .setMeshTexturedUvQuad(texture->uvMapper, uv)
1619 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001620 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik1e4c8072015-05-26 14:25:02 -07001621 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, dst)
Chris Craik82840732015-04-03 09:37:49 -07001622 .build();
1623 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07001624}
1625
Tom Hudson107843d2014-09-08 11:26:26 -04001626void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08001627 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
1628 const SkPaint* paint) {
Chris Craika6b52192015-02-27 17:43:02 -08001629 if (!mesh || !mesh->verticesCount || quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001630 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07001631 }
1632
Chris Craika6b52192015-02-27 17:43:02 -08001633 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
1634 if (!texture) return;
Chris Craik0556d902015-03-03 09:54:14 -08001635
Chris Craik82840732015-04-03 09:37:49 -07001636 // 9 patches are built for stretching - always filter
Chris Craik53e51e42015-06-01 10:35:35 -07001637 int textureFillFlags = TextureFillFlags::ForceFilter;
Chris Craik82840732015-04-03 09:37:49 -07001638 if (bitmap->colorType() == kAlpha_8_SkColorType) {
Chris Craik53e51e42015-06-01 10:35:35 -07001639 textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture;
Chris Craik0556d902015-03-03 09:54:14 -08001640 }
Chris Craik82840732015-04-03 09:37:49 -07001641 Glop glop;
1642 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001643 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001644 .setMeshPatchQuads(*mesh)
1645 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001646 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001647 .setModelViewOffsetRectSnap(left, top, Rect(0, 0, right - left, bottom - top)) // TODO: get minimal bounds from patch
Chris Craik82840732015-04-03 09:37:49 -07001648 .build();
1649 renderGlop(glop);
Romain Guyf7f93552010-07-08 19:17:03 -07001650}
1651
Romain Guy03c00b52013-06-20 18:30:28 -07001652/**
1653 * Important note: this method is intended to draw batches of 9-patch objects and
1654 * will not set the scissor enable or dirty the current layer, if any.
1655 * The caller is responsible for properly dirtying the current layer.
1656 */
Tom Hudson107843d2014-09-08 11:26:26 -04001657void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craika6b52192015-02-27 17:43:02 -08001658 TextureVertex* vertices, uint32_t elementCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001659 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07001660 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001661 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07001662 const AutoTexture autoCleanup(texture);
1663
Chris Craik82840732015-04-03 09:37:49 -07001664 // TODO: get correct bounds from caller
Chris Craik53e51e42015-06-01 10:35:35 -07001665 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001666 // 9 patches are built for stretching - always filter
Chris Craik53e51e42015-06-01 10:35:35 -07001667 int textureFillFlags = TextureFillFlags::ForceFilter;
Chris Craik82840732015-04-03 09:37:49 -07001668 if (bitmap->colorType() == kAlpha_8_SkColorType) {
Chris Craik53e51e42015-06-01 10:35:35 -07001669 textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture;
Chris Craika6b52192015-02-27 17:43:02 -08001670 }
Chris Craik82840732015-04-03 09:37:49 -07001671 Glop glop;
1672 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001673 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001674 .setMeshTexturedIndexedQuads(vertices, elementCount)
1675 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001676 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001677 .setModelViewOffsetRect(0, 0, Rect(0, 0, 0, 0))
Chris Craik82840732015-04-03 09:37:49 -07001678 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001679 renderGlop(glop, GlopRenderType::Multi);
Romain Guy03c00b52013-06-20 18:30:28 -07001680}
1681
Tom Hudson107843d2014-09-08 11:26:26 -04001682void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07001683 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001684 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07001685 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08001686 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04001687 return;
Chris Craik65cd6122012-12-10 17:56:27 -08001688 }
1689
Chris Craik82840732015-04-03 09:37:49 -07001690 bool shadowInterp = displayFlags & kVertexBuffer_ShadowInterp;
Chris Craik53e51e42015-06-01 10:35:35 -07001691 const int transformFlags = TransformFlags::OffsetByFudgeFactor;
Chris Craik82840732015-04-03 09:37:49 -07001692 Glop glop;
1693 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001694 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001695 .setMeshVertexBuffer(vertexBuffer, shadowInterp)
1696 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001697 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001698 .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds())
Chris Craik82840732015-04-03 09:37:49 -07001699 .build();
1700 renderGlop(glop);
Chet Haase858aa932011-05-12 09:06:00 -07001701}
1702
1703/**
Chris Craik65cd6122012-12-10 17:56:27 -08001704 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
1705 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
1706 * screen space in all directions. However, instead of using a fragment shader to compute the
1707 * translucency of the color from its position, we simply use a varying parameter to define how far
1708 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
1709 *
1710 * Doesn't yet support joins, caps, or path effects.
1711 */
Tom Hudson107843d2014-09-08 11:26:26 -04001712void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08001713 VertexBuffer vertexBuffer;
1714 // TODO: try clipping large paths to viewport
Chris Craikfca52b752015-04-28 11:45:59 -07001715
Chris Craikd6b65f62014-01-01 14:45:21 -08001716 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04001717 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08001718}
1719
1720/**
1721 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
1722 * and additional geometry for defining an alpha slope perimeter.
1723 *
1724 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
1725 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
1726 * in-shader alpha region, but found it to be taxing on some GPUs.
1727 *
1728 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
1729 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07001730 */
Tom Hudson107843d2014-09-08 11:26:26 -04001731void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001732 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07001733
Chris Craik65cd6122012-12-10 17:56:27 -08001734 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07001735
Chris Craik65cd6122012-12-10 17:56:27 -08001736 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001737 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
1738 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08001739
Chris Craik05f3d6e2014-06-02 16:27:04 -07001740 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001741 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08001742 }
1743
Chris Craikbf759452014-08-11 16:00:44 -07001744 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001745 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07001746}
1747
Tom Hudson107843d2014-09-08 11:26:26 -04001748void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001749 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001750
Chris Craik6d29c8d2013-05-08 18:35:44 -07001751 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07001752
Chris Craik6d29c8d2013-05-08 18:35:44 -07001753 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001754 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08001755
Chris Craik05f3d6e2014-06-02 16:27:04 -07001756 const Rect& bounds = buffer.getBounds();
1757 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001758 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001759 }
1760
Chris Craikbf759452014-08-11 16:00:44 -07001761 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001762 drawVertexBuffer(buffer, paint, displayFlags);
1763
1764 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07001765}
1766
Tom Hudson107843d2014-09-08 11:26:26 -04001767void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001768 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04001769 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001770
Rob Tsuk487a92c2015-01-06 13:22:54 -08001771 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07001772 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001773
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001774 SkPaint paint;
1775 paint.setColor(color);
1776 paint.setXfermodeMode(mode);
1777
1778 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07001779
Tom Hudson107843d2014-09-08 11:26:26 -04001780 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07001781}
Romain Guy9d5316e2010-06-24 19:30:36 -07001782
Chris Craik30036092015-02-12 10:41:39 -08001783void OpenGLRenderer::drawShape(float left, float top, PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08001784 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04001785 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001786 const AutoTexture autoCleanup(texture);
1787
1788 const float x = left + texture->left - texture->offset;
1789 const float y = top + texture->top - texture->offset;
1790
1791 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07001792
Tom Hudson107843d2014-09-08 11:26:26 -04001793 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08001794}
1795
Tom Hudson107843d2014-09-08 11:26:26 -04001796void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001797 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001798 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001799 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001800 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001801 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001802 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001803
Chris Craike84a2082014-12-22 14:28:49 -08001804 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001805 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001806 PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07001807 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001808 drawShape(left, top, texture, p);
1809 } else {
1810 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
1811 *currentTransform(), *p, right - left, bottom - top, rx, ry);
1812 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001813 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001814}
1815
Tom Hudson107843d2014-09-08 11:26:26 -04001816void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001817 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001818 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001819 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001820 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001821 }
Chris Craikfca52b752015-04-28 11:45:59 -07001822
Chris Craike84a2082014-12-22 14:28:49 -08001823 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001824 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001825 PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001826 drawShape(x - radius, y - radius, texture, p);
Chris Craikfca52b752015-04-28 11:45:59 -07001827 return;
Chris Craikcb4d6002012-09-25 12:00:29 -07001828 }
Chris Craikfca52b752015-04-28 11:45:59 -07001829
1830 SkPath path;
1831 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1832 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
1833 } else {
1834 path.addCircle(x, y, radius);
1835 }
1836
1837 if (CC_UNLIKELY(currentSnapshot()->projectionPathMask != nullptr)) {
1838 // mask ripples with projection mask
1839 SkPath maskPath = *(currentSnapshot()->projectionPathMask->projectionMask);
1840
1841 Matrix4 screenSpaceTransform;
1842 currentSnapshot()->buildScreenSpaceTransform(&screenSpaceTransform);
1843
1844 Matrix4 totalTransform;
1845 totalTransform.loadInverse(screenSpaceTransform);
1846 totalTransform.multiply(currentSnapshot()->projectionPathMask->projectionMaskTransform);
1847
1848 SkMatrix skTotalTransform;
1849 totalTransform.copyTo(skTotalTransform);
1850 maskPath.transform(skTotalTransform);
1851
1852 // Mask the ripple path by the projection mask, now that it's
1853 // in local space. Note that this can create CCW paths.
Tom Hudson02a26302015-06-24 11:32:42 -04001854 Op(path, maskPath, kIntersect_SkPathOp, &path);
Chris Craikfca52b752015-04-28 11:45:59 -07001855 }
1856 drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001857}
Romain Guy01d58e42011-01-19 21:54:02 -08001858
Tom Hudson107843d2014-09-08 11:26:26 -04001859void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001860 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001861 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001862 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001863 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001864 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001865 }
Romain Guy01d58e42011-01-19 21:54:02 -08001866
Chris Craike84a2082014-12-22 14:28:49 -08001867 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001868 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001869 PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001870 drawShape(left, top, texture, p);
1871 } else {
1872 SkPath path;
1873 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1874 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1875 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1876 }
1877 path.addOval(rect);
1878 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001879 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001880}
1881
Tom Hudson107843d2014-09-08 11:26:26 -04001882void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001883 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001884 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001885 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001886 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001887 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08001888 }
1889
Chris Craik780c1282012-10-04 14:10:49 -07001890 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08001891 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001892 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001893 PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07001894 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001895 drawShape(left, top, texture, p);
1896 return;
Chris Craik780c1282012-10-04 14:10:49 -07001897 }
Chris Craik780c1282012-10-04 14:10:49 -07001898 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1899 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1900 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1901 }
1902
1903 SkPath path;
1904 if (useCenter) {
1905 path.moveTo(rect.centerX(), rect.centerY());
1906 }
1907 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
1908 if (useCenter) {
1909 path.close();
1910 }
Tom Hudson107843d2014-09-08 11:26:26 -04001911 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08001912}
1913
Romain Guycf8675e2012-10-02 12:32:25 -07001914// See SkPaintDefaults.h
1915#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
1916
Tom Hudson107843d2014-09-08 11:26:26 -04001917void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001918 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001919 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001920 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001921 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001922 return;
Romain Guy6926c722010-07-12 20:20:03 -07001923 }
1924
Chris Craik710f46d2012-09-17 17:25:49 -07001925 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07001926 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08001927 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07001928 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001929 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001930 PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07001931 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001932 drawShape(left, top, texture, p);
1933 } else {
1934 SkPath path;
1935 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1936 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1937 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1938 }
1939 path.addRect(rect);
1940 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07001941 }
Chet Haase858aa932011-05-12 09:06:00 -07001942 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04001943 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
1944 SkPath path;
1945 path.addRect(left, top, right, bottom);
1946 drawConvexPath(path, p);
1947 } else {
1948 drawColorRect(left, top, right, bottom, p);
1949
1950 mDirty = true;
1951 }
Chet Haase858aa932011-05-12 09:06:00 -07001952 }
Romain Guyc7d53492010-06-25 13:41:57 -07001953}
Romain Guy9d5316e2010-06-24 19:30:36 -07001954
Chris Craikd218a922014-01-02 17:13:34 -08001955void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
1956 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001957 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001958 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07001959
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04001960 TextShadow textShadow;
1961 if (!getTextShadow(paint, &textShadow)) {
1962 LOG_ALWAYS_FATAL("failed to query shadow attributes");
1963 }
1964
Raph Levien416a8472012-07-19 22:48:17 -07001965 // NOTE: The drop shadow will not perform gamma correction
1966 // if shader-based correction is enabled
1967 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Chris Craik2bb8f562015-02-17 16:42:02 -08001968 ShadowTexture* texture = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04001969 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07001970 // If the drop shadow exceeds the max texture size or couldn't be
1971 // allocated, skip drawing
Chris Craik2bb8f562015-02-17 16:42:02 -08001972 if (!texture) return;
1973 const AutoTexture autoCleanup(texture);
Raph Levien416a8472012-07-19 22:48:17 -07001974
Chris Craik2bb8f562015-02-17 16:42:02 -08001975 const float sx = x - texture->left + textShadow.dx;
1976 const float sy = y - texture->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07001977
Chris Craik82840732015-04-03 09:37:49 -07001978 Glop glop;
1979 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001980 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001981 .setMeshTexturedUnitQuad(nullptr)
1982 .setFillShadowTexturePaint(*texture, textShadow.color, *paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001983 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001984 .setModelViewMapUnitToRect(Rect(sx, sy, sx + texture->width, sy + texture->height))
Chris Craik82840732015-04-03 09:37:49 -07001985 .build();
1986 renderGlop(glop);
Raph Levien416a8472012-07-19 22:48:17 -07001987}
1988
Romain Guy768bffc2013-02-27 13:50:45 -08001989bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04001990 float alpha = (hasTextShadow(paint) ? 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
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002016 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002017 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002018
2019 int alpha;
2020 SkXfermode::Mode mode;
2021 getAlphaAndMode(paint, &alpha, &mode);
2022
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002023 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002024 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002025 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002026 }
2027
Romain Guy671d6cf2012-01-18 12:39:17 -08002028 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002029 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002030 if (pureTranslate && !linearFilter) {
2031 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2032 }
Romain Guy257ae352013-03-20 16:31:12 -07002033 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002034
Rob Tsuk487a92c2015-01-06 13:22:54 -08002035 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002036 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2037
Chris Craik82840732015-04-03 09:37:49 -07002038 TextDrawFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002039 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craik083e7332015-02-27 17:04:20 -08002040 positions, hasLayer() ? &bounds : nullptr, &functor)) {
2041 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2042 mDirty = true;
Romain Guy671d6cf2012-01-18 12:39:17 -08002043 }
Chet Haase48659092012-05-31 15:21:51 -07002044
Romain Guyeb9a5362012-01-17 17:39:26 -08002045}
2046
Chris Craik59744b72014-07-01 17:56:52 -07002047bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002048 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002049 outMatrix->setIdentity();
2050 return false;
2051 } else if (CC_UNLIKELY(transform.isPerspective())) {
2052 outMatrix->setIdentity();
2053 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002054 }
Chris Craik59744b72014-07-01 17:56:52 -07002055
2056 /**
Chris Craika736cd92014-08-04 13:18:38 -07002057 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2058 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002059 */
2060 float sx, sy;
2061 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002062 outMatrix->setScale(
Chris Craikdf72b632015-06-30 17:56:13 -07002063 roundf(std::max(1.0f, sx)),
2064 roundf(std::max(1.0f, sy)));
Chris Craika736cd92014-08-04 13:18:38 -07002065 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002066}
2067
Tom Hudson984162f2014-10-10 13:38:16 -04002068int OpenGLRenderer::getSaveCount() const {
2069 return mState.getSaveCount();
2070}
2071
2072int OpenGLRenderer::save(int flags) {
2073 return mState.save(flags);
2074}
2075
2076void OpenGLRenderer::restore() {
Chris Craike2bb3802015-03-13 15:07:52 -07002077 mState.restore();
Tom Hudson984162f2014-10-10 13:38:16 -04002078}
2079
2080void OpenGLRenderer::restoreToCount(int saveCount) {
Chris Craike2bb3802015-03-13 15:07:52 -07002081 mState.restoreToCount(saveCount);
Tom Hudson984162f2014-10-10 13:38:16 -04002082}
2083
Tom Hudsonac7b6d32015-06-30 11:26:13 -04002084
Tom Hudson984162f2014-10-10 13:38:16 -04002085void OpenGLRenderer::translate(float dx, float dy, float dz) {
Chris Craike2bb3802015-03-13 15:07:52 -07002086 mState.translate(dx, dy, dz);
Tom Hudson984162f2014-10-10 13:38:16 -04002087}
2088
2089void OpenGLRenderer::rotate(float degrees) {
Chris Craike2bb3802015-03-13 15:07:52 -07002090 mState.rotate(degrees);
Tom Hudson984162f2014-10-10 13:38:16 -04002091}
2092
2093void OpenGLRenderer::scale(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002094 mState.scale(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002095}
2096
2097void OpenGLRenderer::skew(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002098 mState.skew(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002099}
2100
Chris Craik6daa13c2015-08-19 13:32:12 -07002101void OpenGLRenderer::setLocalMatrix(const Matrix4& matrix) {
2102 mState.setMatrix(mBaseTransform);
2103 mState.concatMatrix(matrix);
Tom Hudson984162f2014-10-10 13:38:16 -04002104}
2105
Tom Hudsonac7b6d32015-06-30 11:26:13 -04002106void OpenGLRenderer::setLocalMatrix(const SkMatrix& matrix) {
2107 mState.setMatrix(mBaseTransform);
2108 mState.concatMatrix(matrix);
2109}
2110
Tom Hudson984162f2014-10-10 13:38:16 -04002111void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2112 mState.concatMatrix(matrix);
2113}
2114
2115bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2116 return mState.clipRect(left, top, right, bottom, op);
2117}
2118
2119bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2120 return mState.clipPath(path, op);
2121}
2122
2123bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2124 return mState.clipRegion(region, op);
2125}
2126
2127void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2128 mState.setClippingOutline(allocator, outline);
2129}
2130
2131void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2132 const Rect& rect, float radius, bool highPriority) {
2133 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2134}
2135
Chris Craikfca52b752015-04-28 11:45:59 -07002136void OpenGLRenderer::setProjectionPathMask(LinearAllocator& allocator, const SkPath* path) {
2137 mState.setProjectionPathMask(allocator, path);
2138}
2139
Tom Hudson107843d2014-09-08 11:26:26 -04002140void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002141 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002142 DrawOpMode drawOpMode) {
2143
Chris Craik03ae2722015-02-25 11:01:09 -08002144 if (drawOpMode == DrawOpMode::kImmediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002145 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2146 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002147 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002148 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002149 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002150 }
Romain Guycac5fd32011-12-01 20:08:50 -08002151 }
2152
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002153 const float oldX = x;
2154 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002155
Chris Craikd6b65f62014-01-01 14:45:21 -08002156 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002157 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002158
Romain Guy211370f2012-02-01 16:10:55 -08002159 if (CC_LIKELY(pureTranslate)) {
Chris Craika6b52192015-02-27 17:43:02 -08002160 x = floorf(x + transform.getTranslateX() + 0.5f);
2161 y = floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002162 }
2163
Romain Guy86568192010-12-14 15:55:39 -08002164 int alpha;
2165 SkXfermode::Mode mode;
2166 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002167
Romain Guyc74f45a2013-02-26 19:10:14 -08002168 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2169
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002170 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002171 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002172 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002173 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002174 }
2175
Romain Guy19d4dd82013-03-04 11:14:26 -08002176 const bool hasActiveLayer = hasLayer();
2177
Romain Guy624234f2013-03-05 16:43:31 -08002178 // We only pass a partial transform to the font renderer. That partial
2179 // matrix defines how glyphs are rasterized. Typically we want glyphs
2180 // to be rasterized at their final size on screen, which means the partial
2181 // matrix needs to take the scale factor into account.
2182 // When a partial matrix is used to transform glyphs during rasterization,
2183 // the mesh is generated with the inverse transform (in the case of scale,
2184 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2185 // apply the full transform matrix at draw time in the vertex shader.
2186 // Applying the full matrix in the shader is the easiest way to handle
2187 // rotation and perspective and allows us to always generated quads in the
2188 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002189 SkMatrix fontTransform;
2190 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2191 || fabs(y - (int) y) > 0.0f
2192 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002193 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002194 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002195
Romain Guy624234f2013-03-05 16:43:31 -08002196 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08002197 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002198 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 -07002199
Raph Levien996e57c2012-07-23 15:22:52 -07002200 bool status;
Chris Craik82840732015-04-03 09:37:49 -07002201 TextDrawFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002202
2203 // don't call issuedrawcommand, do it at end of batch
Chris Craik03ae2722015-02-25 11:01:09 -08002204 bool forceFinish = (drawOpMode != DrawOpMode::kDefer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002205 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002206 SkPaint paintCopy(*paint);
2207 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2208 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002209 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002210 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002211 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002212 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002213 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002214
Chris Craik03ae2722015-02-25 11:01:09 -08002215 if ((status || drawOpMode != DrawOpMode::kImmediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002216 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002217 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002218 }
Chris Craik41541822013-05-03 16:35:54 -07002219 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002220 }
Romain Guy694b5192010-07-21 21:33:20 -07002221
Chris Craike63f7c622013-10-17 10:30:55 -07002222 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002223
Tom Hudson107843d2014-09-08 11:26:26 -04002224 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002225}
2226
Tom Hudson107843d2014-09-08 11:26:26 -04002227void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002228 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002229 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002230 return;
Romain Guy03d58522012-02-24 17:54:07 -08002231 }
2232
Chris Craik39a908c2013-06-13 14:39:01 -07002233 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002234 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002235
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002236 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002237 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002238 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002239
2240 int alpha;
2241 SkXfermode::Mode mode;
2242 getAlphaAndMode(paint, &alpha, &mode);
Chris Craik82840732015-04-03 09:37:49 -07002243 TextDrawFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002244
Tom Hudson984162f2014-10-10 13:38:16 -04002245 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002246 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 -08002247
Romain Guy97771732012-02-28 18:17:02 -08002248 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chih-Hung Hsieh9ad6ac02015-02-27 16:11:36 -08002249 hOffset, vOffset, hasLayer() ? &bounds : nullptr, &functor)) {
Chris Craik083e7332015-02-27 17:04:20 -08002250 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2251 mDirty = true;
Romain Guy97771732012-02-28 18:17:02 -08002252 }
Romain Guy325740f2012-02-24 16:48:34 -08002253}
2254
Tom Hudson107843d2014-09-08 11:26:26 -04002255void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002256 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002257
Chris Craik44eb2c02015-01-29 09:45:09 -08002258 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002259
Chris Craik30036092015-02-12 10:41:39 -08002260 PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002261 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002262 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002263
Romain Guy8b55f372010-08-18 17:10:07 -07002264 const float x = texture->left - texture->offset;
2265 const float y = texture->top - texture->offset;
2266
Romain Guy01d58e42011-01-19 21:54:02 -08002267 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002268 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002269}
2270
Tom Hudson107843d2014-09-08 11:26:26 -04002271void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002272 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002273 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002274 }
2275
Chris Craike84a2082014-12-22 14:28:49 -08002276 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002277 if (layer->isTextureLayer()) {
2278 transform = &layer->getTransform();
2279 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002280 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002281 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002282 }
2283 }
2284
Chris Craik39a908c2013-06-13 14:39:01 -07002285 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08002286 const bool rejected = mState.calculateQuickRejectForScissor(
2287 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2288 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002289
2290 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002291 if (transform && !transform->isIdentity()) {
2292 restore();
2293 }
Tom Hudson107843d2014-09-08 11:26:26 -04002294 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002295 }
2296
Chris Craik62d307c2014-07-29 10:35:13 -07002297 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2298 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2299
Romain Guy5bb3c732012-11-29 17:52:58 -08002300 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002301
Chris Craik65fe5ee2015-01-26 18:06:29 -08002302 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08002303 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002304
Romain Guy211370f2012-02-01 16:10:55 -08002305 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002306 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002307 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2308 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002309 } else if (layer->mesh) {
Chris Craik82840732015-04-03 09:37:49 -07002310 Glop glop;
2311 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002312 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002313 .setMeshTexturedIndexedQuads(layer->mesh, layer->meshElementCount)
2314 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
Chris Craik53e51e42015-06-01 10:35:35 -07002315 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07002316 .setModelViewOffsetRectSnap(x, y, Rect(0, 0, layer->layer.getWidth(), layer->layer.getHeight()))
Chris Craik82840732015-04-03 09:37:49 -07002317 .build();
2318 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy3a3133d2011-02-01 22:59:58 -08002319#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07002320 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08002321#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002322 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002323
Romain Guy5bb3c732012-11-29 17:52:58 -08002324 if (layer->debugDrawUpdate) {
2325 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002326
2327 SkPaint paint;
2328 paint.setColor(0x7f00ff00);
2329 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002330 }
Romain Guyf219da52011-01-16 12:54:25 -08002331 }
Chris Craik34416ea2013-04-15 16:08:28 -07002332 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07002333
Romain Guyb2e2f242012-10-17 18:18:35 -07002334 if (transform && !transform->isIdentity()) {
2335 restore();
2336 }
2337
Tom Hudson107843d2014-09-08 11:26:26 -04002338 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08002339}
2340
Romain Guy6926c722010-07-12 20:20:03 -07002341///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002342// Draw filters
2343///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04002344void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
2345 // We should never get here since we apply the draw filter when stashing
2346 // the paints in the DisplayList.
2347 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08002348}
2349
2350///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002351// Drawing implementation
2352///////////////////////////////////////////////////////////////////////////////
2353
Chris Craikd218a922014-01-02 17:13:34 -08002354Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08002355 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002356 if (!texture) {
2357 return mCaches.textureCache.get(bitmap);
2358 }
2359 return texture;
2360}
2361
Chris Craik2bb8f562015-02-17 16:42:02 -08002362void OpenGLRenderer::drawPathTexture(PathTexture* texture, float x, float y,
2363 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002364 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08002365 return;
2366 }
2367
Chris Craik82840732015-04-03 09:37:49 -07002368 Glop glop;
2369 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002370 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002371 .setMeshTexturedUnitQuad(nullptr)
2372 .setFillPathTexturePaint(*texture, *paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002373 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07002374 .setModelViewMapUnitToRect(Rect(x, y, x + texture->width, y + texture->height))
Chris Craik82840732015-04-03 09:37:49 -07002375 .build();
2376 renderGlop(glop);
Romain Guy01d58e42011-01-19 21:54:02 -08002377}
2378
Romain Guyf607bdc2010-09-10 19:20:06 -07002379// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002380#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2381#define kStdUnderline_Offset (1.0f / 9.0f)
2382#define kStdUnderline_Thickness (1.0f / 18.0f)
2383
Chris Craikd218a922014-01-02 17:13:34 -08002384void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
2385 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07002386 // Handle underline and strike-through
2387 uint32_t flags = paint->getFlags();
2388 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002389 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002390
Romain Guy211370f2012-02-01 16:10:55 -08002391 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002392 const float textSize = paintCopy.getTextSize();
Chris Craike6a15ee2015-07-07 18:42:17 -07002393 const float strokeWidth = std::max(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002394
Raph Levien8b4072d2012-07-30 15:50:00 -07002395 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002396 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002397
Romain Guyf6834472011-01-23 13:32:12 -08002398 int linesCount = 0;
2399 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2400 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2401
2402 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002403 float points[pointsCount];
2404 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002405
2406 if (flags & SkPaint::kUnderlineText_Flag) {
2407 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002408 points[currentPoint++] = left;
2409 points[currentPoint++] = top;
2410 points[currentPoint++] = left + underlineWidth;
2411 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002412 }
2413
2414 if (flags & SkPaint::kStrikeThruText_Flag) {
2415 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002416 points[currentPoint++] = left;
2417 points[currentPoint++] = top;
2418 points[currentPoint++] = left + underlineWidth;
2419 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002420 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002421
Romain Guy726aeba2011-06-01 14:52:00 -07002422 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002423
Romain Guy726aeba2011-06-01 14:52:00 -07002424 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002425 }
2426 }
2427}
2428
Tom Hudson107843d2014-09-08 11:26:26 -04002429void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002430 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002431 return;
Romain Guy672433d2013-01-04 19:05:13 -08002432 }
2433
Tom Hudson107843d2014-09-08 11:26:26 -04002434 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08002435}
2436
Tom Hudson107843d2014-09-08 11:26:26 -04002437void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07002438 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04002439 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07002440
Chris Craik15a07a22014-01-26 13:43:53 -08002441 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08002442 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07002443
2444 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08002445 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07002446
ztenghui14a4e352014-08-13 10:44:39 -07002447 // The caller has made sure casterAlpha > 0.
2448 float ambientShadowAlpha = mAmbientShadowAlpha;
Chris Craik2507c342015-05-04 14:36:49 -07002449 if (CC_UNLIKELY(Properties::overrideAmbientShadowStrength >= 0)) {
2450 ambientShadowAlpha = Properties::overrideAmbientShadowStrength;
ztenghui14a4e352014-08-13 10:44:39 -07002451 }
2452 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
2453 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002454 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002455 }
ztenghui7b4516e2014-01-07 10:42:55 -08002456
ztenghui14a4e352014-08-13 10:44:39 -07002457 float spotShadowAlpha = mSpotShadowAlpha;
Chris Craik2507c342015-05-04 14:36:49 -07002458 if (CC_UNLIKELY(Properties::overrideSpotShadowStrength >= 0)) {
2459 spotShadowAlpha = Properties::overrideSpotShadowStrength;
ztenghui14a4e352014-08-13 10:44:39 -07002460 }
2461 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
2462 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002463 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002464 }
ztenghui7b4516e2014-01-07 10:42:55 -08002465
Tom Hudson107843d2014-09-08 11:26:26 -04002466 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07002467}
2468
Tom Hudson107843d2014-09-08 11:26:26 -04002469void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002470 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08002471 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04002472 return;
Romain Guy3b753822013-03-05 10:27:35 -08002473 }
Romain Guy735738c2012-12-03 12:34:51 -08002474
Romain Guy672433d2013-01-04 19:05:13 -08002475 float left = FLT_MAX;
2476 float top = FLT_MAX;
2477 float right = FLT_MIN;
2478 float bottom = FLT_MIN;
2479
Romain Guy448455f2013-07-22 13:57:50 -07002480 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08002481 Vertex* vertex = mesh;
2482
Chris Craik2af46352012-11-26 18:30:17 -08002483 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08002484 float l = rects[index + 0];
2485 float t = rects[index + 1];
2486 float r = rects[index + 2];
2487 float b = rects[index + 3];
2488
Romain Guy3b753822013-03-05 10:27:35 -08002489 Vertex::set(vertex++, l, t);
2490 Vertex::set(vertex++, r, t);
2491 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08002492 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08002493
Chris Craikdf72b632015-06-30 17:56:13 -07002494 left = std::min(left, l);
2495 top = std::min(top, t);
2496 right = std::max(right, r);
2497 bottom = std::max(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08002498 }
2499
Chris Craikf0a59072013-11-19 18:00:46 -08002500 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002501 return;
Romain Guya362c692013-02-04 13:50:16 -08002502 }
Romain Guy672433d2013-01-04 19:05:13 -08002503
Chris Craik53e51e42015-06-01 10:35:35 -07002504 const int transformFlags = ignoreTransform
2505 ? TransformFlags::MeshIgnoresCanvasTransform : TransformFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07002506 Glop glop;
2507 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002508 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002509 .setMeshIndexedQuads(&mesh[0], count / 4)
2510 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002511 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07002512 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07002513 .build();
2514 renderGlop(glop);
Romain Guy672433d2013-01-04 19:05:13 -08002515}
2516
Romain Guy026c5e162010-06-28 17:12:22 -07002517void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002518 const SkPaint* paint, bool ignoreTransform) {
Chris Craik53e51e42015-06-01 10:35:35 -07002519 const int transformFlags = ignoreTransform
2520 ? TransformFlags::MeshIgnoresCanvasTransform : TransformFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07002521 Glop glop;
2522 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002523 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002524 .setMeshUnitQuad()
2525 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002526 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07002527 .setModelViewMapUnitToRect(Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07002528 .build();
2529 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07002530}
2531
Chris Craike84a2082014-12-22 14:28:49 -08002532void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
2533 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002534 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craikd6b65f62014-01-01 14:45:21 -08002535 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002536}
2537
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002538float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik8df5ffa2015-04-28 17:47:20 -07002539 return (layer->getAlpha() / 255.0f) * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07002540}
2541
Romain Guy9d5316e2010-06-24 19:30:36 -07002542}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002543}; // namespace android