blob: f49237c9c069720c73da89ed6fcf779679bf9a79 [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 Craik386aa032015-12-07 17:08:25 -080043#include <SkPaintDefaults.h>
Chris Craikfca52b752015-04-28 11:45:59 -070044#include <SkPathOps.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040045#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070046#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070047
Romain Guye4d01122010-06-16 18:44:05 -070048#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070049#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070050
Romain Guy08aa2cb2011-03-17 11:06:57 -070051#include <private/hwui/DrawGlInfo.h>
52
Romain Guy5b3b3522010-10-27 18:57:51 -070053#include <ui/Rect.h>
54
Chris Craik62d307c2014-07-29 10:35:13 -070055#if DEBUG_DETAILED_EVENTS
56 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
57#else
58 #define EVENT_LOGD(...)
59#endif
60
Romain Guye4d01122010-06-16 18:44:05 -070061namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070062namespace uirenderer {
63
Romain Guy9d5316e2010-06-24 19:30:36 -070064///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -070065// Constructors/destructor
66///////////////////////////////////////////////////////////////////////////////
67
John Reck3b202512014-06-23 13:13:08 -070068OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -040069 : mState(*this)
Chris Craik058fc642014-07-23 18:19:28 -070070 , mCaches(Caches::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -070071 , mRenderState(renderState)
Chris Craik65fe5ee2015-01-26 18:06:29 -080072 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -070073 , mScissorOptimizationDisabled(false)
Tom Hudson107843d2014-09-08 11:26:26 -040074 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -070075 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -070076 , mLightRadius(FLT_MIN)
77 , mAmbientShadowAlpha(0)
78 , mSpotShadowAlpha(0) {
Romain Guye4d01122010-06-16 18:44:05 -070079}
80
Romain Guy85bf02f2010-06-22 13:11:24 -070081OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -070082 // The context has already been destroyed at this point, do not call
83 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -070084}
85
Romain Guy87e2f7572012-09-24 11:37:12 -070086void OpenGLRenderer::initProperties() {
87 char property[PROPERTY_VALUE_MAX];
88 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
89 mScissorOptimizationDisabled = !strcasecmp(property, "true");
90 INIT_LOGD(" Scissor optimization %s",
91 mScissorOptimizationDisabled ? "disabled" : "enabled");
92 } else {
93 INIT_LOGD(" Scissor optimization enabled");
94 }
Romain Guy13631f32012-01-30 17:41:55 -080095}
96
Alan Viverette50210d92015-05-14 18:05:36 -070097void OpenGLRenderer::initLight(float lightRadius, uint8_t ambientShadowAlpha,
98 uint8_t spotShadowAlpha) {
Chris Craik058fc642014-07-23 18:19:28 -070099 mLightRadius = lightRadius;
100 mAmbientShadowAlpha = ambientShadowAlpha;
101 mSpotShadowAlpha = spotShadowAlpha;
102}
103
Alan Viverette50210d92015-05-14 18:05:36 -0700104void OpenGLRenderer::setLightCenter(const Vector3& lightCenter) {
105 mLightCenter = lightCenter;
106}
107
Romain Guy13631f32012-01-30 17:41:55 -0800108///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700109// Setup
110///////////////////////////////////////////////////////////////////////////////
111
Chris Craik797b95b2014-05-20 18:10:25 -0700112void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700113 glDisable(GL_DITHER);
114 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy35643dd2012-09-18 15:40:58 -0700115}
116
Chris Craik64e445b2015-09-02 14:23:49 -0700117void OpenGLRenderer::setupFrameState(int viewportWidth, int viewportHeight,
118 float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800119 mCaches.clearGarbage();
Chris Craik64e445b2015-09-02 14:23:49 -0700120 mState.initializeSaveStack(viewportWidth, viewportHeight,
121 left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700122 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700123 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700124}
125
Tom Hudson107843d2014-09-08 11:26:26 -0400126void OpenGLRenderer::startFrame() {
127 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700128 mFrameStarted = true;
129
Tom Hudson984162f2014-10-10 13:38:16 -0400130 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700131
Romain Guy96885eb2013-03-26 15:05:58 -0700132 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700133
Tom Hudson984162f2014-10-10 13:38:16 -0400134 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800135
Romain Guy7c450aa2012-09-21 19:15:00 -0700136 debugOverdraw(true, true);
137
Tom Hudson107843d2014-09-08 11:26:26 -0400138 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700139 mTilingClip.right, mTilingClip.bottom, mOpaque);
140}
141
Chris Craik64e445b2015-09-02 14:23:49 -0700142void OpenGLRenderer::prepareDirty(int viewportWidth, int viewportHeight,
143 float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700144
Chris Craik64e445b2015-09-02 14:23:49 -0700145 setupFrameState(viewportWidth, viewportHeight, left, top, right, bottom, opaque);
Romain Guy96885eb2013-03-26 15:05:58 -0700146
147 // Layer renderers will start the frame immediately
148 // The framebuffer renderer will first defer the display list
149 // for each layer and wait until the first drawing command
150 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800151 if (currentSnapshot()->fbo == 0) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800152 mRenderState.blend().syncEnabled();
Romain Guy96885eb2013-03-26 15:05:58 -0700153 updateLayers();
154 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400155 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700156 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700157}
158
Romain Guydcfc8362013-01-03 13:08:57 -0800159void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
160 // If we know that we are going to redraw the entire framebuffer,
161 // perform a discard to let the driver know we don't need to preserve
162 // the back buffer for this frame.
Chris Craik117bdbc2015-02-05 10:12:38 -0800163 if (mCaches.extensions().hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400164 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
Chris Craik6b109c72015-02-27 10:55:28 -0800165 const bool isFbo = getTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800166 const GLenum attachments[] = {
167 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
168 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800169 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
170 }
171}
172
Tom Hudson107843d2014-09-08 11:26:26 -0400173void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700174 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800175 mRenderState.scissor().setEnabled(true);
176 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700177 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400178 mDirty = true;
179 return;
Romain Guyddf74372012-05-22 14:07:07 -0700180 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700181
Chris Craik65fe5ee2015-01-26 18:06:29 -0800182 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700183}
184
Tom Hudson107843d2014-09-08 11:26:26 -0400185bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700186 renderOverdraw();
Chris Craik4ac36f82014-12-09 16:54:03 -0800187 mTempPaths.clear();
188
Romain Guyca89e2a2013-03-08 17:44:20 -0800189 // When finish() is invoked on FBO 0 we've reached the end
190 // of the current frame
Chris Craik6b109c72015-02-27 10:55:28 -0800191 if (getTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800192 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700193 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800194 }
195
Romain Guy11cb6422012-09-21 00:39:43 -0700196 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700197#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700198 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700199#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700200
Romain Guyc15008e2010-11-10 11:59:15 -0800201#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800202 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700203#else
Chris Craik2507c342015-05-04 14:36:49 -0700204 if (Properties::debugLevel & kDebugMemory) {
Romain Guy11cb6422012-09-21 00:39:43 -0700205 mCaches.dumpMemoryUsage();
206 }
Romain Guyc15008e2010-11-10 11:59:15 -0800207#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700208 }
Romain Guy96885eb2013-03-26 15:05:58 -0700209
210 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400211
212 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700213}
214
Romain Guy35643dd2012-09-18 15:40:58 -0700215void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700216 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
217 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700218 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700219
Chris Craik65fe5ee2015-01-26 18:06:29 -0800220 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700221 dirtyClip();
222}
223
Andreas Gampe64bb4132014-11-22 00:35:09 +0000224void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400225 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700226
Chris Craik6fe991e52015-10-20 09:39:42 -0700227 Rect clip(mState.currentRenderTargetClip());
Romain Guy80911b82011-03-16 15:30:12 -0700228 clip.snapToPixelBoundaries();
229
Romain Guyd643bb52011-03-01 14:55:21 -0800230 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800231 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800232 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800233 dirtyLayerUnchecked(clip, getRegion());
234 }
Romain Guyd643bb52011-03-01 14:55:21 -0800235
Romain Guy08aa2cb2011-03-17 11:06:57 -0700236 DrawGlInfo info;
237 info.clipLeft = clip.left;
238 info.clipTop = clip.top;
239 info.clipRight = clip.right;
240 info.clipBottom = clip.bottom;
241 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700242 info.width = getViewportWidth();
243 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800244 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700245
Tom Hudson984162f2014-10-10 13:38:16 -0400246 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700247 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400248 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700249 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
250 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800251 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700252 setScissorFromClip();
253 }
Chris Craik54f574a2013-08-26 11:23:46 -0700254
John Reck3b202512014-06-23 13:13:08 -0700255 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
256 // Scissor may have been modified, reset dirty clip
257 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800258
Tom Hudson107843d2014-09-08 11:26:26 -0400259 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800260}
261
Romain Guyf6a11b82010-06-23 17:47:49 -0700262///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700263// Debug
264///////////////////////////////////////////////////////////////////////////////
265
Chris Craik62d307c2014-07-29 10:35:13 -0700266void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
267#if DEBUG_DETAILED_EVENTS
268 const int BUFFER_SIZE = 256;
269 va_list ap;
270 char buf[BUFFER_SIZE];
271
272 va_start(ap, fmt);
273 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
274 va_end(ap);
275
276 eventMark(buf);
277#endif
278}
279
280
Romain Guy0f667532013-03-01 14:31:04 -0800281void OpenGLRenderer::eventMark(const char* name) const {
282 mCaches.eventMark(0, name);
283}
284
Romain Guy87e2f7572012-09-24 11:37:12 -0700285void OpenGLRenderer::startMark(const char* name) const {
286 mCaches.startMark(0, name);
287}
288
289void OpenGLRenderer::endMark() const {
290 mCaches.endMark();
291}
292
293void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700294 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700295}
296
297void OpenGLRenderer::renderOverdraw() {
Chris Craik2507c342015-05-04 14:36:49 -0700298 if (Properties::debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700299 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700300
Chris Craik65fe5ee2015-01-26 18:06:29 -0800301 mRenderState.scissor().setEnabled(true);
302 mRenderState.scissor().set(clip->left,
303 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
304 clip->right - clip->left,
305 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700306
Romain Guy627c6fd2013-08-21 11:53:18 -0700307 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800308 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700309 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
310
311 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800312 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700313 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
314
315 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800316 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700317 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
318
319 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800320 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700321 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
322
Chris Craik96a5c4c2015-01-27 15:46:35 -0800323 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700324 }
325}
326
327///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700328// Layers
329///////////////////////////////////////////////////////////////////////////////
330
331bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700332 if (layer->deferredUpdateScheduled && layer->renderer
333 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700334
Romain Guy7c450aa2012-09-21 19:15:00 -0700335 if (inFrame) {
Romain Guy7c450aa2012-09-21 19:15:00 -0700336 debugOverdraw(false, false);
337 }
Romain Guy11cb6422012-09-21 00:39:43 -0700338
Chris Craik2507c342015-05-04 14:36:49 -0700339 if (CC_UNLIKELY(inFrame || Properties::drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700340 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700341 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700342 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700343 }
Romain Guy11cb6422012-09-21 00:39:43 -0700344
345 if (inFrame) {
346 resumeAfterLayer();
Romain Guy11cb6422012-09-21 00:39:43 -0700347 }
348
Chris Craik2507c342015-05-04 14:36:49 -0700349 layer->debugDrawUpdate = Properties::debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700350 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700351
352 return true;
353 }
354
355 return false;
356}
357
358void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700359 // If draw deferring is enabled this method will simply defer
360 // the display list of each individual layer. The layers remain
361 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700362 int count = mLayerUpdates.size();
363 if (count > 0) {
Chris Craik2507c342015-05-04 14:36:49 -0700364 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -0700365 startMark("Layer Updates");
366 } else {
367 startMark("Defer Layer Updates");
368 }
Romain Guy11cb6422012-09-21 00:39:43 -0700369
Chris Craik1206b9b2013-04-04 14:46:24 -0700370 // Note: it is very important to update the layers in order
371 for (int i = 0; i < count; i++) {
John Reck272a6852015-07-29 16:48:58 -0700372 Layer* layer = mLayerUpdates[i].get();
Romain Guy11cb6422012-09-21 00:39:43 -0700373 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700374 }
Romain Guy11cb6422012-09-21 00:39:43 -0700375
Chris Craik2507c342015-05-04 14:36:49 -0700376 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -0700377 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800378 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700379 }
380 endMark();
381 }
382}
383
384void OpenGLRenderer::flushLayers() {
385 int count = mLayerUpdates.size();
386 if (count > 0) {
387 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700388
Chris Craik1206b9b2013-04-04 14:46:24 -0700389 // Note: it is very important to update the layers in order
390 for (int i = 0; i < count; i++) {
John Reck272a6852015-07-29 16:48:58 -0700391 mLayerUpdates[i]->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700392 }
393
394 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800395 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700396
Romain Guy11cb6422012-09-21 00:39:43 -0700397 endMark();
398 }
399}
400
401void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
402 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700403 // Make sure we don't introduce duplicates.
404 // SortedVector would do this automatically but we need to respect
405 // the insertion order. The linear search is not an issue since
406 // this list is usually very short (typically one item, at most a few)
407 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
John Reck272a6852015-07-29 16:48:58 -0700408 if (mLayerUpdates[i] == layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700409 return;
410 }
411 }
Romain Guy11cb6422012-09-21 00:39:43 -0700412 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700413 }
414}
415
Romain Guye93482f2013-06-17 13:14:51 -0700416void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
417 if (layer) {
418 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
John Reck272a6852015-07-29 16:48:58 -0700419 if (mLayerUpdates[i] == layer) {
420 mLayerUpdates.erase(mLayerUpdates.begin() + i);
Romain Guye93482f2013-06-17 13:14:51 -0700421 break;
422 }
423 }
424 }
425}
426
Romain Guy40543602013-06-12 15:31:28 -0700427void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800428 ATRACE_NAME("Update HW Layers");
Chris Craik44eb2c02015-01-29 09:45:09 -0800429 mRenderState.blend().syncEnabled();
Romain Guy40543602013-06-12 15:31:28 -0700430 updateLayers();
431 flushLayers();
432 // Wait for all the layer updates to be executed
John Reck55156372015-01-21 07:46:37 -0800433 glFinish();
Romain Guy40543602013-06-12 15:31:28 -0700434}
435
John Reck443a7142014-09-04 17:40:05 -0700436void OpenGLRenderer::markLayersAsBuildLayers() {
437 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
438 mLayerUpdates[i]->wasBuildLayered = true;
439 }
440}
441
Romain Guy11cb6422012-09-21 00:39:43 -0700442///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700443// State management
444///////////////////////////////////////////////////////////////////////////////
445
Chris Craik14e51302013-12-30 15:32:54 -0800446void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700447 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800448 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
449 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700450
Chris Craika64a2be2014-05-14 14:17:01 -0700451 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700452 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700453 }
454
Romain Guy2542d192010-08-18 11:47:12 -0700455 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700456 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700457 }
Romain Guy2542d192010-08-18 11:47:12 -0700458
Romain Guy5ec99242010-11-03 16:19:08 -0700459 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700460 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700461 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700462 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800463 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700464 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700465 }
Romain Guybb9524b2010-06-22 18:56:38 -0700466}
467
Romain Guyf6a11b82010-06-23 17:47:49 -0700468///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700469// Layers
470///////////////////////////////////////////////////////////////////////////////
471
472int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700473 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700474 // force matrix/clip isolation for layer
475 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
476
Tom Hudson984162f2014-10-10 13:38:16 -0400477 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700478
Tom Hudson984162f2014-10-10 13:38:16 -0400479 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700480 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700481 }
Romain Guyd55a8612010-06-28 17:42:46 -0700482
483 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700484}
485
Chris Craikd90144d2013-03-19 15:03:48 -0700486void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
487 const Rect untransformedBounds(bounds);
488
Chris Craikd6b65f62014-01-01 14:45:21 -0800489 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700490
491 // Layers only make sense if they are in the framebuffer's bounds
Chris Craik6fe991e52015-10-20 09:39:42 -0700492 bounds.doIntersect(mState.currentRenderTargetClip());
Chris Craikac02eb92015-10-05 12:23:46 -0700493 if (!bounds.isEmpty()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700494 // We cannot work with sub-pixels in this case
495 bounds.snapToPixelBoundaries();
496
497 // When the layer is not an FBO, we may use glCopyTexImage so we
498 // need to make sure the layer does not extend outside the bounds
499 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700500 const Snapshot& previous = *(currentSnapshot()->previous);
501 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
Chris Craikac02eb92015-10-05 12:23:46 -0700502
503 bounds.doIntersect(previousViewport);
504 if (!bounds.isEmpty() && fboLayer) {
Chris Craikd90144d2013-03-19 15:03:48 -0700505 clip.set(bounds);
506 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800507 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700508 inverse.mapRect(clip);
509 clip.snapToPixelBoundaries();
Chris Craikac02eb92015-10-05 12:23:46 -0700510 clip.doIntersect(untransformedBounds);
511 if (!clip.isEmpty()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700512 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
513 bounds.set(untransformedBounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700514 }
515 }
Chris Craikd90144d2013-03-19 15:03:48 -0700516 }
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);
Chris Craikbf6f0f22015-10-01 12:36:07 -0700542 updateSnapshotIgnoreForLayer(bounds, clip, true, PaintUtils::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);
Chris Craikbf6f0f22015-10-01 12:36:07 -0700617 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, PaintUtils::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 Guydda57022010-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 Guydda57022010-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);
Chris Craik818c9fb2015-10-23 14:33:42 -0700677 layer->setFbo(mRenderState.genFramebuffer());
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 Craik6fe991e52015-10-20 09:39:42 -07001040 bounds.doIntersect(mState.currentRenderTargetClip());
Chris Craikac02eb92015-10-05 12:23:46 -07001041 if (!bounds.isEmpty()) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001042 bounds.snapToPixelBoundaries();
1043 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1044 if (!dirty.isEmpty()) {
1045 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001046 }
1047 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001048}
1049
Romain Guy54be1cd2011-06-13 19:04:27 -07001050void OpenGLRenderer::clearLayerRegions() {
Chris Craik2bb8f562015-02-17 16:42:02 -08001051 const size_t quadCount = mLayers.size();
1052 if (quadCount == 0) return;
Romain Guy54be1cd2011-06-13 19:04:27 -07001053
Tom Hudson984162f2014-10-10 13:38:16 -04001054 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001055 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001056 // Doing several glScissor/glClear here can negatively impact
1057 // GPUs with a tiler architecture, instead we draw quads with
1058 // the Clear blending mode
1059
1060 // The list contains bounds that have already been clipped
1061 // against their initial clip rect, and the current clip
1062 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001063 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001064
Chris Craik2bb8f562015-02-17 16:42:02 -08001065 Vertex mesh[quadCount * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001066 Vertex* vertex = mesh;
1067
Chris Craik2bb8f562015-02-17 16:42:02 -08001068 for (uint32_t i = 0; i < quadCount; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001069 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001070
Chris Craik51d6a3d2014-12-22 17:16:56 -08001071 Vertex::set(vertex++, bounds.left, bounds.top);
1072 Vertex::set(vertex++, bounds.right, bounds.top);
1073 Vertex::set(vertex++, bounds.left, bounds.bottom);
1074 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001075 }
Romain Guye67307c2013-02-11 18:01:20 -08001076 // We must clear the list of dirty rects before we
Chris Craik82840732015-04-03 09:37:49 -07001077 // call clearLayerRegions() in renderGlop to prevent
1078 // stencil setup from doing the same thing again
Romain Guye67307c2013-02-11 18:01:20 -08001079 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001080
Chris Craik53e51e42015-06-01 10:35:35 -07001081 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001082 Glop glop;
1083 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001084 .setRoundRectClipState(nullptr) // clear ignores clip state
Chris Craik82840732015-04-03 09:37:49 -07001085 .setMeshIndexedQuads(&mesh[0], quadCount)
1086 .setFillClear()
Chris Craik53e51e42015-06-01 10:35:35 -07001087 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik6fe991e52015-10-20 09:39:42 -07001088 .setModelViewOffsetRect(0, 0, Rect(currentSnapshot()->getRenderTargetClip()))
Chris Craik82840732015-04-03 09:37:49 -07001089 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001090 renderGlop(glop, GlopRenderType::LayerClear);
Romain Guy8a4ac612012-07-17 17:32:48 -07001091
Chris Craik65fe5ee2015-01-26 18:06:29 -08001092 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001093 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001094 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001095 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001096}
1097
Romain Guybd6b79b2010-06-26 00:13:53 -07001098///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001099// State Deferral
1100///////////////////////////////////////////////////////////////////////////////
1101
Chris Craikff785832013-03-08 13:12:16 -08001102bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craik6fe991e52015-10-20 09:39:42 -07001103 const Rect& currentClip = mState.currentRenderTargetClip();
Chris Craikd6b65f62014-01-01 14:45:21 -08001104 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001105
Chris Craikff785832013-03-08 13:12:16 -08001106 if (stateDeferFlags & kStateDeferFlag_Draw) {
1107 // state has bounds initialized in local coordinates
1108 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001109 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001110 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001111 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1112 // is used, it should more closely duplicate the quickReject logic (in how it uses
1113 // snapToPixelBoundaries)
1114
Chris Craikac02eb92015-10-05 12:23:46 -07001115 clippedBounds.doIntersect(currentClip);
1116 if (clippedBounds.isEmpty()) {
Chris Craikff785832013-03-08 13:12:16 -08001117 // quick rejected
1118 return true;
1119 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001120
Chris Craika02c4ed2013-06-14 13:43:58 -07001121 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001122 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001123 int& flags = state.mClipSideFlags;
1124 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001125 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1126 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1127 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1128 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001129 }
1130 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001131 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001132 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1133 // overdraw avoidance (since we don't know what it overlaps)
1134 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001135 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001136 }
1137 }
1138
Chris Craik527a3aa2013-03-04 10:19:31 -08001139 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1140 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001141 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001142 }
1143
Chris Craik8df5ffa2015-04-28 17:47:20 -07001144 // Transform and alpha always deferred, since they are used by state operations
Chris Craik7273daa2013-03-28 11:25:24 -07001145 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craik7c85c542015-08-19 15:10:24 -07001146 state.mMatrix = *currentMatrix;
Chris Craikd6b65f62014-01-01 14:45:21 -08001147 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001148
Chris Craikfca52b752015-04-28 11:45:59 -07001149 // always store/restore, since these are just pointers
Chris Craikdeeda3d2014-05-05 19:09:33 -07001150 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikfca52b752015-04-28 11:45:59 -07001151 state.mProjectionPathMask = currentSnapshot()->projectionPathMask;
Chris Craikc3566d02013-02-04 16:16:33 -08001152 return false;
1153}
1154
Chris Craik527a3aa2013-03-04 10:19:31 -08001155void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik6daa13c2015-08-19 13:32:12 -07001156 setGlobalMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001157 writableSnapshot()->alpha = state.mAlpha;
Tom Hudson984162f2014-10-10 13:38:16 -04001158 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikfca52b752015-04-28 11:45:59 -07001159 writableSnapshot()->projectionPathMask = state.mProjectionPathMask;
Chris Craikff785832013-03-08 13:12:16 -08001160
Chris Craik527a3aa2013-03-04 10:19:31 -08001161 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001162 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001163 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001164 dirtyClip();
1165 }
Chris Craikc3566d02013-02-04 16:16:33 -08001166}
1167
Chris Craik28ce94a2013-05-31 11:38:03 -07001168/**
1169 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1170 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1171 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1172 *
1173 * This method should be called when restoreDisplayState() won't be restoring the clip
1174 */
1175void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001176 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001177 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001178 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001179 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001180 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001181 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001182 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1183 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001184}
1185
Chris Craikc3566d02013-02-04 16:16:33 -08001186///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001187// Clipping
1188///////////////////////////////////////////////////////////////////////////////
1189
Romain Guybb9524b2010-06-22 18:56:38 -07001190void OpenGLRenderer::setScissorFromClip() {
Chris Craik6fe991e52015-10-20 09:39:42 -07001191 Rect clip(mState.currentRenderTargetClip());
Romain Guye5ebcb02010-10-15 13:57:28 -07001192 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001193
Chris Craik65fe5ee2015-01-26 18:06:29 -08001194 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001195 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001196 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001197 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001198}
1199
Romain Guy8ce00302013-01-15 18:51:42 -08001200void OpenGLRenderer::ensureStencilBuffer() {
1201 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1202 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1203 // just hope we have one when hasLayer() returns false.
1204 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001205 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001206 }
1207}
1208
1209void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1210 // The layer's FBO is already bound when we reach this stage
1211 if (!layer->getStencilRenderBuffer()) {
Romain Guy8d4aeb72013-02-12 16:08:55 -08001212 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Chris Craike1450132015-04-28 17:55:50 -07001213 Stencil::getLayerStencilFormat(),
Chris Craik117bdbc2015-02-05 10:12:38 -08001214 layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001215 layer->setStencilRenderBuffer(buffer);
1216 }
1217}
1218
Rob Tsuk487a92c2015-01-06 13:22:54 -08001219static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1220 float x, float y) {
1221 Vertex v;
1222 v.x = x;
1223 v.y = y;
1224 transform.mapPoint(v.x, v.y);
1225 rectangleVertices.push_back(v);
1226}
1227
1228static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1229 Vertex v;
1230 v.x = x;
1231 v.y = y;
1232 rectangleVertices.push_back(v);
1233}
1234
1235void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
Chris Craik2bb8f562015-02-17 16:42:02 -08001236 int quadCount = rectangleList.getTransformedRectanglesCount();
1237 std::vector<Vertex> rectangleVertices(quadCount * 4);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001238 Rect scissorBox = rectangleList.calculateBounds();
1239 scissorBox.snapToPixelBoundaries();
Chris Craik2bb8f562015-02-17 16:42:02 -08001240 for (int i = 0; i < quadCount; ++i) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001241 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1242 const Matrix4& transform = tr.getTransform();
1243 Rect bounds = tr.getBounds();
1244 if (transform.rectToRect()) {
1245 transform.mapRect(bounds);
Chris Craikac02eb92015-10-05 12:23:46 -07001246 bounds.doIntersect(scissorBox);
1247 if (!bounds.isEmpty()) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001248 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1249 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1250 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1251 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1252 }
1253 } else {
1254 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1255 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1256 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1257 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1258 }
1259 }
1260
Chris Craik65fe5ee2015-01-26 18:06:29 -08001261 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001262 scissorBox.getWidth(), scissorBox.getHeight());
Chris Craik53e51e42015-06-01 10:35:35 -07001263 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001264 Glop glop;
1265 Vertex* vertices = &rectangleVertices[0];
1266 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001267 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001268 .setMeshIndexedQuads(vertices, rectangleVertices.size() / 4)
1269 .setFillBlack()
Chris Craik53e51e42015-06-01 10:35:35 -07001270 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001271 .setModelViewOffsetRect(0, 0, scissorBox)
Chris Craik82840732015-04-03 09:37:49 -07001272 .build();
1273 renderGlop(glop);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001274}
1275
Romain Guy8ce00302013-01-15 18:51:42 -08001276void OpenGLRenderer::setStencilFromClip() {
Chris Craik2507c342015-05-04 14:36:49 -07001277 if (!Properties::debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001278 if (!currentSnapshot()->clipIsSimple()) {
1279 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001280 EVENT_LOGD("setStencilFromClip - enabling");
1281
Romain Guy8ce00302013-01-15 18:51:42 -08001282 // NOTE: The order here is important, we must set dirtyClip to false
1283 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001284 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001285
1286 ensureStencilBuffer();
1287
Rob Tsuk487a92c2015-01-06 13:22:54 -08001288 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001289
Rob Tsuk487a92c2015-01-06 13:22:54 -08001290 bool isRectangleList = clipArea.isRectangleList();
1291 if (isRectangleList) {
1292 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1293 } else {
1294 incrementThreshold = 0;
1295 }
1296
Chris Craik96a5c4c2015-01-27 15:46:35 -08001297 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001298
1299 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001300 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001301 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001302 if (resetScissor) {
1303 // The scissor was not set so we now need to update it
1304 setScissorFromClip();
1305 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001306
Chris Craik96a5c4c2015-01-27 15:46:35 -08001307 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001308
1309 // stash and disable the outline clip state, since stencil doesn't account for outline
1310 bool storedSkipOutlineClip = mSkipOutlineClip;
1311 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001312
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001313 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001314 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001315 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1316
Rob Tsuk487a92c2015-01-06 13:22:54 -08001317 if (isRectangleList) {
1318 drawRectangleList(clipArea.getRectangleList());
1319 } else {
1320 // NOTE: We could use the region contour path to generate a smaller mesh
1321 // Since we are using the stencil we could use the red book path
1322 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001323
Rob Tsuk487a92c2015-01-06 13:22:54 -08001324 // The last parameter is important: we are not drawing in the color buffer
1325 // so we don't want to dirty the current layer, if any
1326 drawRegionRects(clipArea.getClipRegion(), paint, false);
1327 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001328 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001329 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001330
Chris Craik96a5c4c2015-01-27 15:46:35 -08001331 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001332
1333 // Draw the region used to generate the stencil if the appropriate debug
1334 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001335 // TODO: Implement for rectangle list clip areas
Chris Craik2507c342015-05-04 14:36:49 -07001336 if (Properties::debugStencilClip == StencilClipDebug::ShowRegion
1337 && !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001338 paint.setColor(0x7f0000ff);
1339 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001340 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001341 }
Romain Guy8ce00302013-01-15 18:51:42 -08001342 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001343 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001344 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001345 }
1346 }
1347}
1348
Chris Craikf0a59072013-11-19 18:00:46 -08001349/**
1350 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1351 *
1352 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1353 * style, and tessellated AA ramp
1354 */
1355bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001356 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001357 bool snapOut = paint && paint->isAntiAlias();
1358
1359 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1360 float outset = paint->getStrokeWidth() * 0.5f;
1361 left -= outset;
1362 top -= outset;
1363 right += outset;
1364 bottom += outset;
1365 }
1366
Chris Craikdeeda3d2014-05-05 19:09:33 -07001367 bool clipRequired = false;
1368 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001369 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001370 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001371 return true;
1372 }
1373
Chris Craikf23b25a2014-06-26 15:46:20 -07001374 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001375 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001376 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001377 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001378}
1379
Romain Guy8ce00302013-01-15 18:51:42 -08001380void OpenGLRenderer::debugClip() {
1381#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001382 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001383 SkPaint paint;
1384 paint.setColor(0x7f00ff00);
1385 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1386
Romain Guy8ce00302013-01-15 18:51:42 -08001387 }
1388#endif
1389}
1390
Chris Craik3375f8a2015-06-23 17:33:06 -07001391void OpenGLRenderer::renderGlop(const Glop& glop, GlopRenderType type) {
Chris Craik6b109c72015-02-27 10:55:28 -08001392 // TODO: It would be best if we could do this before quickRejectSetupScissor()
1393 // changes the scissor test state
Chris Craik3375f8a2015-06-23 17:33:06 -07001394 if (type != GlopRenderType::LayerClear) {
1395 // Regular draws need to clear the dirty area on the layer before they start drawing on top
1396 // of it. If this draw *is* a layer clear, it skips the clear step (since it would
1397 // infinitely recurse)
1398 clearLayerRegions();
1399 }
Chris Craik6b109c72015-02-27 10:55:28 -08001400
Chris Craik117bdbc2015-02-05 10:12:38 -08001401 if (mState.getDirtyClip()) {
1402 if (mRenderState.scissor().isEnabled()) {
1403 setScissorFromClip();
1404 }
1405
1406 setStencilFromClip();
1407 }
Chris Craik12efe642015-09-28 15:52:12 -07001408 mRenderState.render(glop, currentSnapshot()->getOrthoMatrix());
Chris Craik3375f8a2015-06-23 17:33:06 -07001409 if (type == GlopRenderType::Standard && !mRenderState.stencil().isWriteEnabled()) {
Chris Craik2ab95d72015-02-06 15:25:51 -08001410 // TODO: specify more clearly when a draw should dirty the layer.
1411 // is writing to the stencil the only time we should ignore this?
1412 dirtyLayer(glop.bounds.left, glop.bounds.top, glop.bounds.right, glop.bounds.bottom);
Chris Craik0519c812015-02-11 13:17:06 -08001413 mDirty = true;
Chris Craik2ab95d72015-02-06 15:25:51 -08001414 }
Chris Craik117bdbc2015-02-05 10:12:38 -08001415}
1416
Romain Guyf6a11b82010-06-23 17:47:49 -07001417///////////////////////////////////////////////////////////////////////////////
1418// Drawing
1419///////////////////////////////////////////////////////////////////////////////
1420
Tom Hudson107843d2014-09-08 11:26:26 -04001421void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001422 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1423 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001424 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001425 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001426 renderNode->computeOrdering();
Chris Craik2507c342015-05-04 14:36:49 -07001427 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001428 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001429 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001430 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001431 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001432 }
1433
Chris Craik6fe991e52015-10-20 09:39:42 -07001434 DeferredDisplayList deferredList(mState.currentRenderTargetClip());
Chris Craikff785832013-03-08 13:12:16 -08001435 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001436 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001437
1438 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001439 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001440
Tom Hudson107843d2014-09-08 11:26:26 -04001441 deferredList.flush(*this, dirty);
1442 } else {
1443 // Even if there is no drawing command(Ex: invisible),
1444 // it still needs startFrame to clear buffer and start tiling.
1445 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001446 }
1447}
1448
Romain Guy03c00b52013-06-20 18:30:28 -07001449/**
1450 * Important note: this method is intended to draw batches of bitmaps and
1451 * will not set the scissor enable or dirty the current layer, if any.
1452 * The caller is responsible for properly dirtying the current layer.
1453 */
Tom Hudson107843d2014-09-08 11:26:26 -04001454void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001455 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1456 const Rect& bounds, const SkPaint* paint) {
Romain Guy55b6f952013-06-27 15:27:09 -07001457 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001458 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001459
Chris Craik527a3aa2013-03-04 10:19:31 -08001460 const AutoTexture autoCleanup(texture);
1461
Chris Craik82840732015-04-03 09:37:49 -07001462 // TODO: remove layer dirty in multi-draw callers
1463 // TODO: snap doesn't need to touch transform, only texture filter.
1464 bool snap = pureTranslate;
Chris Craika6b52192015-02-27 17:43:02 -08001465 const float x = floorf(bounds.left + 0.5f);
1466 const float y = floorf(bounds.top + 0.5f);
Chris Craik53e51e42015-06-01 10:35:35 -07001467
1468 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1469 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
1470 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001471 Glop glop;
1472 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001473 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001474 .setMeshTexturedMesh(vertices, bitmapCount * 6)
1475 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001476 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001477 .setModelViewOffsetRectOptionalSnap(snap, x, y, Rect(0, 0, bounds.getWidth(), bounds.getHeight()))
Chris Craik82840732015-04-03 09:37:49 -07001478 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001479 renderGlop(glop, GlopRenderType::Multi);
Chris Craik527a3aa2013-03-04 10:19:31 -08001480}
1481
Tom Hudson107843d2014-09-08 11:26:26 -04001482void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07001483 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04001484 return;
Romain Guy6926c722010-07-12 20:20:03 -07001485 }
1486
Chris Craik44eb2c02015-01-29 09:45:09 -08001487 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07001488 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001489 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001490 const AutoTexture autoCleanup(texture);
1491
Chris Craik53e51e42015-06-01 10:35:35 -07001492 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1493 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07001494 Glop glop;
1495 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001496 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001497 .setMeshTexturedUnitQuad(texture->uvMapper)
1498 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001499 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001500 .setModelViewMapUnitToRectSnap(Rect(0, 0, texture->width, texture->height))
Chris Craik82840732015-04-03 09:37:49 -07001501 .build();
1502 renderGlop(glop);
Romain Guyce0537b2010-06-29 21:05:21 -07001503}
1504
Tom Hudson107843d2014-09-08 11:26:26 -04001505void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08001506 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001507 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04001508 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08001509 }
1510
Romain Guyb18d2d02011-02-10 15:52:54 -08001511 float left = FLT_MAX;
1512 float top = FLT_MAX;
1513 float right = FLT_MIN;
1514 float bottom = FLT_MIN;
1515
Chris Craikef250742015-02-25 17:16:16 -08001516 const uint32_t elementCount = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001517
Chris Craikef250742015-02-25 17:16:16 -08001518 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[elementCount]);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001519 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08001520
Chris Craik51d6a3d2014-12-22 17:16:56 -08001521 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08001522 if (!colors) {
1523 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001524 tempColors.reset(new int[colorsCount]);
1525 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
1526 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08001527 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001528
Chris Craik15c3f192015-12-03 12:16:56 -08001529 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap->pixelRef());
Romain Guy3b748a42013-04-17 18:54:38 -07001530 const UvMapper& mapper(getMapper(texture));
1531
Romain Guy5a7b4662011-01-20 19:09:30 -08001532 for (int32_t y = 0; y < meshHeight; y++) {
1533 for (int32_t x = 0; x < meshWidth; x++) {
1534 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1535
1536 float u1 = float(x) / meshWidth;
1537 float u2 = float(x + 1) / meshWidth;
1538 float v1 = float(y) / meshHeight;
1539 float v2 = float(y + 1) / meshHeight;
1540
Romain Guy3b748a42013-04-17 18:54:38 -07001541 mapper.map(u1, v1, u2, v2);
1542
Romain Guy5a7b4662011-01-20 19:09:30 -08001543 int ax = i + (meshWidth + 1) * 2;
1544 int ay = ax + 1;
1545 int bx = i;
1546 int by = bx + 1;
1547 int cx = i + 2;
1548 int cy = cx + 1;
1549 int dx = i + (meshWidth + 1) * 2 + 2;
1550 int dy = dx + 1;
1551
Romain Guyff316ec2013-02-13 18:39:43 -08001552 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1553 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
1554 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08001555
Romain Guyff316ec2013-02-13 18:39:43 -08001556 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1557 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
1558 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08001559
Chris Craikdf72b632015-06-30 17:56:13 -07001560 left = std::min(left, std::min(vertices[ax], std::min(vertices[bx], vertices[cx])));
1561 top = std::min(top, std::min(vertices[ay], std::min(vertices[by], vertices[cy])));
1562 right = std::max(right, std::max(vertices[ax], std::max(vertices[bx], vertices[cx])));
1563 bottom = std::max(bottom, std::max(vertices[ay], std::max(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001564 }
1565 }
1566
Chris Craikf0a59072013-11-19 18:00:46 -08001567 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001568 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07001569 }
1570
Romain Guyff316ec2013-02-13 18:39:43 -08001571 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07001572 texture = mCaches.textureCache.get(bitmap);
1573 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04001574 return;
Romain Guy3b748a42013-04-17 18:54:38 -07001575 }
Romain Guyff316ec2013-02-13 18:39:43 -08001576 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001577 const AutoTexture autoCleanup(texture);
1578
Chris Craik82840732015-04-03 09:37:49 -07001579 /*
1580 * TODO: handle alpha_8 textures correctly by applying paint color, but *not*
1581 * shader in that case to mimic the behavior in SkiaCanvas::drawBitmapMesh.
1582 */
Chris Craik53e51e42015-06-01 10:35:35 -07001583 const int textureFillFlags = TextureFillFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07001584 Glop glop;
1585 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001586 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001587 .setMeshColoredTexturedMesh(mesh.get(), elementCount)
Chris Craik53e51e42015-06-01 10:35:35 -07001588 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1589 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001590 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07001591 .build();
1592 renderGlop(glop);
Romain Guy5a7b4662011-01-20 19:09:30 -08001593}
1594
Chris Craik14100ac2015-02-24 13:46:29 -08001595void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, Rect src, Rect dst, const SkPaint* paint) {
1596 if (quickRejectSetupScissor(dst)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001597 return;
Romain Guy6926c722010-07-12 20:20:03 -07001598 }
1599
Romain Guy3b748a42013-04-17 18:54:38 -07001600 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001601 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001602 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001603
Chris Craike6a15ee2015-07-07 18:42:17 -07001604 Rect uv(std::max(0.0f, src.left / texture->width),
1605 std::max(0.0f, src.top / texture->height),
1606 std::min(1.0f, src.right / texture->width),
1607 std::min(1.0f, src.bottom / texture->height));
Chris Craik14100ac2015-02-24 13:46:29 -08001608
Chris Craik53e51e42015-06-01 10:35:35 -07001609 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1610 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
Chris Craik1e4c8072015-05-26 14:25:02 -07001611 const bool tryToSnap = MathUtils::areEqual(src.getWidth(), dst.getWidth())
1612 && MathUtils::areEqual(src.getHeight(), dst.getHeight());
Chris Craik82840732015-04-03 09:37:49 -07001613 Glop glop;
1614 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001615 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001616 .setMeshTexturedUvQuad(texture->uvMapper, uv)
1617 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001618 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik1e4c8072015-05-26 14:25:02 -07001619 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, dst)
Chris Craik82840732015-04-03 09:37:49 -07001620 .build();
1621 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07001622}
1623
Tom Hudson107843d2014-09-08 11:26:26 -04001624void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08001625 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
1626 const SkPaint* paint) {
Chris Craika6b52192015-02-27 17:43:02 -08001627 if (!mesh || !mesh->verticesCount || quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001628 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07001629 }
1630
Chris Craika6b52192015-02-27 17:43:02 -08001631 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
1632 if (!texture) return;
Chris Craik0556d902015-03-03 09:54:14 -08001633
Chris Craik82840732015-04-03 09:37:49 -07001634 // 9 patches are built for stretching - always filter
Chris Craik53e51e42015-06-01 10:35:35 -07001635 int textureFillFlags = TextureFillFlags::ForceFilter;
Chris Craik82840732015-04-03 09:37:49 -07001636 if (bitmap->colorType() == kAlpha_8_SkColorType) {
Chris Craik53e51e42015-06-01 10:35:35 -07001637 textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture;
Chris Craik0556d902015-03-03 09:54:14 -08001638 }
Chris Craik82840732015-04-03 09:37:49 -07001639 Glop glop;
1640 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001641 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001642 .setMeshPatchQuads(*mesh)
1643 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001644 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001645 .setModelViewOffsetRectSnap(left, top, Rect(0, 0, right - left, bottom - top)) // TODO: get minimal bounds from patch
Chris Craik82840732015-04-03 09:37:49 -07001646 .build();
1647 renderGlop(glop);
Romain Guyf7f93552010-07-08 19:17:03 -07001648}
1649
Romain Guy03c00b52013-06-20 18:30:28 -07001650/**
1651 * Important note: this method is intended to draw batches of 9-patch objects and
1652 * will not set the scissor enable or dirty the current layer, if any.
1653 * The caller is responsible for properly dirtying the current layer.
1654 */
Tom Hudson107843d2014-09-08 11:26:26 -04001655void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craika6b52192015-02-27 17:43:02 -08001656 TextureVertex* vertices, uint32_t elementCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001657 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07001658 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001659 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07001660 const AutoTexture autoCleanup(texture);
1661
Chris Craik82840732015-04-03 09:37:49 -07001662 // TODO: get correct bounds from caller
Chris Craik53e51e42015-06-01 10:35:35 -07001663 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001664 // 9 patches are built for stretching - always filter
Chris Craik53e51e42015-06-01 10:35:35 -07001665 int textureFillFlags = TextureFillFlags::ForceFilter;
Chris Craik82840732015-04-03 09:37:49 -07001666 if (bitmap->colorType() == kAlpha_8_SkColorType) {
Chris Craik53e51e42015-06-01 10:35:35 -07001667 textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture;
Chris Craika6b52192015-02-27 17:43:02 -08001668 }
Chris Craik82840732015-04-03 09:37:49 -07001669 Glop glop;
1670 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001671 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001672 .setMeshTexturedIndexedQuads(vertices, elementCount)
1673 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001674 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001675 .setModelViewOffsetRect(0, 0, Rect(0, 0, 0, 0))
Chris Craik82840732015-04-03 09:37:49 -07001676 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001677 renderGlop(glop, GlopRenderType::Multi);
Romain Guy03c00b52013-06-20 18:30:28 -07001678}
1679
Tom Hudson107843d2014-09-08 11:26:26 -04001680void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07001681 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001682 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07001683 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08001684 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04001685 return;
Chris Craik65cd6122012-12-10 17:56:27 -08001686 }
1687
Chris Craik82840732015-04-03 09:37:49 -07001688 bool shadowInterp = displayFlags & kVertexBuffer_ShadowInterp;
Chris Craik53e51e42015-06-01 10:35:35 -07001689 const int transformFlags = TransformFlags::OffsetByFudgeFactor;
Chris Craik82840732015-04-03 09:37:49 -07001690 Glop glop;
1691 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001692 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001693 .setMeshVertexBuffer(vertexBuffer, shadowInterp)
1694 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001695 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001696 .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds())
Chris Craik82840732015-04-03 09:37:49 -07001697 .build();
1698 renderGlop(glop);
Chet Haase858aa932011-05-12 09:06:00 -07001699}
1700
1701/**
Chris Craik65cd6122012-12-10 17:56:27 -08001702 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
1703 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
1704 * screen space in all directions. However, instead of using a fragment shader to compute the
1705 * translucency of the color from its position, we simply use a varying parameter to define how far
1706 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
1707 *
1708 * Doesn't yet support joins, caps, or path effects.
1709 */
Tom Hudson107843d2014-09-08 11:26:26 -04001710void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08001711 VertexBuffer vertexBuffer;
1712 // TODO: try clipping large paths to viewport
Chris Craikfca52b752015-04-28 11:45:59 -07001713
Chris Craikd6b65f62014-01-01 14:45:21 -08001714 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04001715 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08001716}
1717
1718/**
1719 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
1720 * and additional geometry for defining an alpha slope perimeter.
1721 *
1722 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
1723 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
1724 * in-shader alpha region, but found it to be taxing on some GPUs.
1725 *
1726 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
1727 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07001728 */
Tom Hudson107843d2014-09-08 11:26:26 -04001729void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001730 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07001731
Chris Craik65cd6122012-12-10 17:56:27 -08001732 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07001733
Chris Craik65cd6122012-12-10 17:56:27 -08001734 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001735 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
1736 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08001737
Chris Craik05f3d6e2014-06-02 16:27:04 -07001738 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001739 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08001740 }
1741
Chris Craikbf759452014-08-11 16:00:44 -07001742 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001743 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07001744}
1745
Tom Hudson107843d2014-09-08 11:26:26 -04001746void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001747 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001748
Chris Craik6d29c8d2013-05-08 18:35:44 -07001749 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07001750
Chris Craik6d29c8d2013-05-08 18:35:44 -07001751 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001752 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08001753
Chris Craik05f3d6e2014-06-02 16:27:04 -07001754 const Rect& bounds = buffer.getBounds();
1755 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001756 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001757 }
1758
Chris Craikbf759452014-08-11 16:00:44 -07001759 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001760 drawVertexBuffer(buffer, paint, displayFlags);
1761
1762 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07001763}
1764
Tom Hudson107843d2014-09-08 11:26:26 -04001765void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001766 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04001767 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001768
Chris Craik6fe991e52015-10-20 09:39:42 -07001769 Rect clip(mState.currentRenderTargetClip());
Romain Guyae88e5e2010-10-22 17:49:18 -07001770 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001771
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001772 SkPaint paint;
1773 paint.setColor(color);
1774 paint.setXfermodeMode(mode);
1775
1776 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07001777
Tom Hudson107843d2014-09-08 11:26:26 -04001778 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07001779}
Romain Guy9d5316e2010-06-24 19:30:36 -07001780
Chris Craik30036092015-02-12 10:41:39 -08001781void OpenGLRenderer::drawShape(float left, float top, PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08001782 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04001783 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001784 const AutoTexture autoCleanup(texture);
1785
1786 const float x = left + texture->left - texture->offset;
1787 const float y = top + texture->top - texture->offset;
1788
1789 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07001790
Tom Hudson107843d2014-09-08 11:26:26 -04001791 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08001792}
1793
Tom Hudson107843d2014-09-08 11:26:26 -04001794void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001795 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001796 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001797 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001798 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001799 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001800 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001801
Chris Craike84a2082014-12-22 14:28:49 -08001802 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001803 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001804 PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07001805 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001806 drawShape(left, top, texture, p);
1807 } else {
1808 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
1809 *currentTransform(), *p, right - left, bottom - top, rx, ry);
1810 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001811 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001812}
1813
Tom Hudson107843d2014-09-08 11:26:26 -04001814void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001815 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001816 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001817 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001818 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001819 }
Chris Craikfca52b752015-04-28 11:45:59 -07001820
Chris Craike84a2082014-12-22 14:28:49 -08001821 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001822 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001823 PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001824 drawShape(x - radius, y - radius, texture, p);
Chris Craikfca52b752015-04-28 11:45:59 -07001825 return;
Chris Craikcb4d6002012-09-25 12:00:29 -07001826 }
Chris Craikfca52b752015-04-28 11:45:59 -07001827
1828 SkPath path;
1829 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1830 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
1831 } else {
1832 path.addCircle(x, y, radius);
1833 }
1834
1835 if (CC_UNLIKELY(currentSnapshot()->projectionPathMask != nullptr)) {
1836 // mask ripples with projection mask
1837 SkPath maskPath = *(currentSnapshot()->projectionPathMask->projectionMask);
1838
1839 Matrix4 screenSpaceTransform;
1840 currentSnapshot()->buildScreenSpaceTransform(&screenSpaceTransform);
1841
1842 Matrix4 totalTransform;
1843 totalTransform.loadInverse(screenSpaceTransform);
1844 totalTransform.multiply(currentSnapshot()->projectionPathMask->projectionMaskTransform);
1845
1846 SkMatrix skTotalTransform;
1847 totalTransform.copyTo(skTotalTransform);
1848 maskPath.transform(skTotalTransform);
1849
1850 // Mask the ripple path by the projection mask, now that it's
1851 // in local space. Note that this can create CCW paths.
Tom Hudson02a26302015-06-24 11:32:42 -04001852 Op(path, maskPath, kIntersect_SkPathOp, &path);
Chris Craikfca52b752015-04-28 11:45:59 -07001853 }
1854 drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001855}
Romain Guy01d58e42011-01-19 21:54:02 -08001856
Tom Hudson107843d2014-09-08 11:26:26 -04001857void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001858 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001859 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001860 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001861 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001862 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001863 }
Romain Guy01d58e42011-01-19 21:54:02 -08001864
Chris Craike84a2082014-12-22 14:28:49 -08001865 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001866 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001867 PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001868 drawShape(left, top, texture, p);
1869 } else {
1870 SkPath path;
1871 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1872 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1873 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1874 }
1875 path.addOval(rect);
1876 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001877 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001878}
1879
Tom Hudson107843d2014-09-08 11:26:26 -04001880void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001881 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001882 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001883 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001884 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001885 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08001886 }
1887
Chris Craik780c1282012-10-04 14:10:49 -07001888 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08001889 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001890 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001891 PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07001892 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001893 drawShape(left, top, texture, p);
1894 return;
Chris Craik780c1282012-10-04 14:10:49 -07001895 }
Chris Craik780c1282012-10-04 14:10:49 -07001896 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1897 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1898 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1899 }
1900
1901 SkPath path;
1902 if (useCenter) {
1903 path.moveTo(rect.centerX(), rect.centerY());
1904 }
1905 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
1906 if (useCenter) {
1907 path.close();
1908 }
Tom Hudson107843d2014-09-08 11:26:26 -04001909 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08001910}
1911
Tom Hudson107843d2014-09-08 11:26:26 -04001912void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001913 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001914 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001915 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001916 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001917 return;
Romain Guy6926c722010-07-12 20:20:03 -07001918 }
1919
Chris Craik710f46d2012-09-17 17:25:49 -07001920 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07001921 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craik386aa032015-12-07 17:08:25 -08001922 static_assert(SkPaintDefaults_MiterLimit == 4.0f, "Miter limit has changed");
Chris Craike84a2082014-12-22 14:28:49 -08001923 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07001924 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001925 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001926 PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07001927 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001928 drawShape(left, top, texture, p);
1929 } else {
1930 SkPath path;
1931 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1932 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1933 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1934 }
1935 path.addRect(rect);
1936 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07001937 }
Chet Haase858aa932011-05-12 09:06:00 -07001938 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04001939 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
1940 SkPath path;
1941 path.addRect(left, top, right, bottom);
1942 drawConvexPath(path, p);
1943 } else {
1944 drawColorRect(left, top, right, bottom, p);
1945
1946 mDirty = true;
1947 }
Chet Haase858aa932011-05-12 09:06:00 -07001948 }
Romain Guyc7d53492010-06-25 13:41:57 -07001949}
Romain Guy9d5316e2010-06-24 19:30:36 -07001950
Chris Craikd218a922014-01-02 17:13:34 -08001951void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
Chris Craika1717272015-11-19 13:02:43 -08001952 int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001953 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001954 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07001955
Chris Craikbf6f0f22015-10-01 12:36:07 -07001956 PaintUtils::TextShadow textShadow;
1957 if (!PaintUtils::getTextShadow(paint, &textShadow)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04001958 LOG_ALWAYS_FATAL("failed to query shadow attributes");
1959 }
1960
Raph Levien416a8472012-07-19 22:48:17 -07001961 // NOTE: The drop shadow will not perform gamma correction
1962 // if shader-based correction is enabled
1963 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Chris Craik2bb8f562015-02-17 16:42:02 -08001964 ShadowTexture* texture = mCaches.dropShadowCache.get(
Chris Craika1717272015-11-19 13:02:43 -08001965 paint, text, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07001966 // If the drop shadow exceeds the max texture size or couldn't be
1967 // allocated, skip drawing
Chris Craik2bb8f562015-02-17 16:42:02 -08001968 if (!texture) return;
1969 const AutoTexture autoCleanup(texture);
Raph Levien416a8472012-07-19 22:48:17 -07001970
Chris Craik2bb8f562015-02-17 16:42:02 -08001971 const float sx = x - texture->left + textShadow.dx;
1972 const float sy = y - texture->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07001973
Chris Craik82840732015-04-03 09:37:49 -07001974 Glop glop;
1975 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001976 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001977 .setMeshTexturedUnitQuad(nullptr)
1978 .setFillShadowTexturePaint(*texture, textShadow.color, *paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001979 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001980 .setModelViewMapUnitToRect(Rect(sx, sy, sx + texture->width, sy + texture->height))
Chris Craik82840732015-04-03 09:37:49 -07001981 .build();
1982 renderGlop(glop);
Raph Levien416a8472012-07-19 22:48:17 -07001983}
1984
Chris Craikbf6f0f22015-10-01 12:36:07 -07001985// TODO: remove this, once mState.currentlyIgnored captures snapshot alpha
Romain Guy768bffc2013-02-27 13:50:45 -08001986bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Chris Craikbf6f0f22015-10-01 12:36:07 -07001987 float alpha = (PaintUtils::hasTextShadow(paint)
1988 ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05001989 return MathUtils::isZero(alpha)
1990 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08001991}
1992
Chris Craik59744b72014-07-01 17:56:52 -07001993bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08001994 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07001995 outMatrix->setIdentity();
1996 return false;
1997 } else if (CC_UNLIKELY(transform.isPerspective())) {
1998 outMatrix->setIdentity();
1999 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002000 }
Chris Craik59744b72014-07-01 17:56:52 -07002001
2002 /**
Chris Craika736cd92014-08-04 13:18:38 -07002003 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2004 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002005 */
2006 float sx, sy;
2007 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002008 outMatrix->setScale(
Chris Craikdf72b632015-06-30 17:56:13 -07002009 roundf(std::max(1.0f, sx)),
2010 roundf(std::max(1.0f, sy)));
Chris Craika736cd92014-08-04 13:18:38 -07002011 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002012}
2013
Tom Hudson984162f2014-10-10 13:38:16 -04002014int OpenGLRenderer::getSaveCount() const {
2015 return mState.getSaveCount();
2016}
2017
2018int OpenGLRenderer::save(int flags) {
2019 return mState.save(flags);
2020}
2021
2022void OpenGLRenderer::restore() {
Chris Craike2bb3802015-03-13 15:07:52 -07002023 mState.restore();
Tom Hudson984162f2014-10-10 13:38:16 -04002024}
2025
2026void OpenGLRenderer::restoreToCount(int saveCount) {
Chris Craike2bb3802015-03-13 15:07:52 -07002027 mState.restoreToCount(saveCount);
Tom Hudson984162f2014-10-10 13:38:16 -04002028}
2029
Tom Hudsonac7b6d32015-06-30 11:26:13 -04002030
Tom Hudson984162f2014-10-10 13:38:16 -04002031void OpenGLRenderer::translate(float dx, float dy, float dz) {
Chris Craike2bb3802015-03-13 15:07:52 -07002032 mState.translate(dx, dy, dz);
Tom Hudson984162f2014-10-10 13:38:16 -04002033}
2034
2035void OpenGLRenderer::rotate(float degrees) {
Chris Craike2bb3802015-03-13 15:07:52 -07002036 mState.rotate(degrees);
Tom Hudson984162f2014-10-10 13:38:16 -04002037}
2038
2039void OpenGLRenderer::scale(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002040 mState.scale(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002041}
2042
2043void OpenGLRenderer::skew(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002044 mState.skew(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002045}
2046
Chris Craik6daa13c2015-08-19 13:32:12 -07002047void OpenGLRenderer::setLocalMatrix(const Matrix4& matrix) {
2048 mState.setMatrix(mBaseTransform);
2049 mState.concatMatrix(matrix);
Tom Hudson984162f2014-10-10 13:38:16 -04002050}
2051
Tom Hudsonac7b6d32015-06-30 11:26:13 -04002052void OpenGLRenderer::setLocalMatrix(const SkMatrix& matrix) {
2053 mState.setMatrix(mBaseTransform);
2054 mState.concatMatrix(matrix);
2055}
2056
Tom Hudson984162f2014-10-10 13:38:16 -04002057void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2058 mState.concatMatrix(matrix);
2059}
2060
2061bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2062 return mState.clipRect(left, top, right, bottom, op);
2063}
2064
2065bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2066 return mState.clipPath(path, op);
2067}
2068
2069bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2070 return mState.clipRegion(region, op);
2071}
2072
2073void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2074 mState.setClippingOutline(allocator, outline);
2075}
2076
2077void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2078 const Rect& rect, float radius, bool highPriority) {
2079 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2080}
2081
Chris Craikfca52b752015-04-28 11:45:59 -07002082void OpenGLRenderer::setProjectionPathMask(LinearAllocator& allocator, const SkPath* path) {
2083 mState.setProjectionPathMask(allocator, path);
2084}
2085
Tom Hudson107843d2014-09-08 11:26:26 -04002086void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002087 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002088 DrawOpMode drawOpMode) {
2089
Chris Craik03ae2722015-02-25 11:01:09 -08002090 if (drawOpMode == DrawOpMode::kImmediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002091 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2092 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002093 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002094 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002095 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002096 }
Romain Guycac5fd32011-12-01 20:08:50 -08002097 }
2098
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002099 const float oldX = x;
2100 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002101
Chris Craikd6b65f62014-01-01 14:45:21 -08002102 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002103 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002104
Romain Guy211370f2012-02-01 16:10:55 -08002105 if (CC_LIKELY(pureTranslate)) {
Chris Craika6b52192015-02-27 17:43:02 -08002106 x = floorf(x + transform.getTranslateX() + 0.5f);
2107 y = floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002108 }
2109
Chris Craikbf6f0f22015-10-01 12:36:07 -07002110 int alpha = PaintUtils::getAlphaDirect(paint) * currentSnapshot()->alpha;
2111 SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(paint);
Romain Guy9d13fe252010-10-15 16:06:03 -07002112
Chris Craikc08820f2015-09-22 14:22:29 -07002113 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
Romain Guyc74f45a2013-02-26 19:10:14 -08002114
Chris Craikbf6f0f22015-10-01 12:36:07 -07002115 if (CC_UNLIKELY(PaintUtils::hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002116 fontRenderer.setFont(paint, SkMatrix::I());
Chris Craika1717272015-11-19 13:02:43 -08002117 drawTextShadow(paint, text, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002118 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002119 }
2120
Romain Guy19d4dd82013-03-04 11:14:26 -08002121 const bool hasActiveLayer = hasLayer();
2122
Romain Guy624234f2013-03-05 16:43:31 -08002123 // We only pass a partial transform to the font renderer. That partial
2124 // matrix defines how glyphs are rasterized. Typically we want glyphs
2125 // to be rasterized at their final size on screen, which means the partial
2126 // matrix needs to take the scale factor into account.
2127 // When a partial matrix is used to transform glyphs during rasterization,
2128 // the mesh is generated with the inverse transform (in the case of scale,
2129 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2130 // apply the full transform matrix at draw time in the vertex shader.
2131 // Applying the full matrix in the shader is the easiest way to handle
2132 // rotation and perspective and allows us to always generated quads in the
2133 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002134 SkMatrix fontTransform;
2135 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2136 || fabs(y - (int) y) > 0.0f
2137 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002138 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002139 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002140
Romain Guy624234f2013-03-05 16:43:31 -08002141 // TODO: Implement better clipping for scaled/rotated text
Chris Craik6fe991e52015-10-20 09:39:42 -07002142 const Rect* clip = !pureTranslate ? nullptr : &mState.currentRenderTargetClip();
Chris Craik41541822013-05-03 16:35:54 -07002143 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 -07002144
Raph Levien996e57c2012-07-23 15:22:52 -07002145 bool status;
Chris Craika1717272015-11-19 13:02:43 -08002146#if HWUI_NEW_OPS
2147 LOG_ALWAYS_FATAL("unsupported");
Chris Craik15c3f192015-12-03 12:16:56 -08002148 TextDrawFunctor functor(nullptr, nullptr, nullptr, x, y, pureTranslate, alpha, mode, paint);
Chris Craika1717272015-11-19 13:02:43 -08002149#else
Chris Craik82840732015-04-03 09:37:49 -07002150 TextDrawFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craika1717272015-11-19 13:02:43 -08002151#endif
Chris Craik527a3aa2013-03-04 10:19:31 -08002152
2153 // don't call issuedrawcommand, do it at end of batch
Chris Craik03ae2722015-02-25 11:01:09 -08002154 bool forceFinish = (drawOpMode != DrawOpMode::kDefer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002155 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002156 SkPaint paintCopy(*paint);
2157 paintCopy.setTextAlign(SkPaint::kLeft_Align);
Chris Craika1717272015-11-19 13:02:43 -08002158 status = fontRenderer.renderPosText(&paintCopy, clip, text, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002159 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002160 } else {
Chris Craika1717272015-11-19 13:02:43 -08002161 status = fontRenderer.renderPosText(paint, clip, text, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002162 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002163 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002164
Chris Craik03ae2722015-02-25 11:01:09 -08002165 if ((status || drawOpMode != DrawOpMode::kImmediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002166 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002167 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002168 }
Chris Craik41541822013-05-03 16:35:54 -07002169 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002170 }
Romain Guy694b5192010-07-21 21:33:20 -07002171
Tom Hudson107843d2014-09-08 11:26:26 -04002172 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002173}
2174
Tom Hudson107843d2014-09-08 11:26:26 -04002175void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002176 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002177 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002178 return;
Romain Guy03d58522012-02-24 17:54:07 -08002179 }
2180
Chris Craik39a908c2013-06-13 14:39:01 -07002181 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002182 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002183
Chris Craikc08820f2015-09-22 14:22:29 -07002184 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
Chris Craik59744b72014-07-01 17:56:52 -07002185 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002186 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002187
Chris Craikbf6f0f22015-10-01 12:36:07 -07002188 int alpha = PaintUtils::getAlphaDirect(paint) * currentSnapshot()->alpha;
2189 SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(paint);
Chris Craika1717272015-11-19 13:02:43 -08002190#if HWUI_NEW_OPS
2191 LOG_ALWAYS_FATAL("unsupported");
Chris Craik15c3f192015-12-03 12:16:56 -08002192 TextDrawFunctor functor(nullptr, nullptr, nullptr, 0.0f, 0.0f, false, alpha, mode, paint);
Chris Craika1717272015-11-19 13:02:43 -08002193#else
Chris Craik82840732015-04-03 09:37:49 -07002194 TextDrawFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Chris Craika1717272015-11-19 13:02:43 -08002195#endif
Romain Guy03d58522012-02-24 17:54:07 -08002196
Tom Hudson984162f2014-10-10 13:38:16 -04002197 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002198 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 -08002199
Chris Craika1717272015-11-19 13:02:43 -08002200 if (fontRenderer.renderTextOnPath(paint, clip, text, count, path,
Chih-Hung Hsieh9ad6ac02015-02-27 16:11:36 -08002201 hOffset, vOffset, hasLayer() ? &bounds : nullptr, &functor)) {
Chris Craik083e7332015-02-27 17:04:20 -08002202 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2203 mDirty = true;
Romain Guy97771732012-02-28 18:17:02 -08002204 }
Romain Guy325740f2012-02-24 16:48:34 -08002205}
2206
Tom Hudson107843d2014-09-08 11:26:26 -04002207void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002208 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002209
Chris Craik44eb2c02015-01-29 09:45:09 -08002210 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002211
Chris Craik30036092015-02-12 10:41:39 -08002212 PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002213 if (!texture) return;
Romain Guy7fbcc042010-08-04 15:40:07 -07002214
Romain Guy8b55f372010-08-18 17:10:07 -07002215 const float x = texture->left - texture->offset;
2216 const float y = texture->top - texture->offset;
2217
Romain Guy01d58e42011-01-19 21:54:02 -08002218 drawPathTexture(texture, x, y, paint);
Digish Pandya2e4f67c2015-11-04 11:00:28 +05302219
2220 if (texture->cleanup) {
2221 mCaches.pathCache.remove(path, paint);
2222 }
Tom Hudson107843d2014-09-08 11:26:26 -04002223 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002224}
2225
Chris Craik3aadd602015-08-20 12:41:40 -07002226void OpenGLRenderer::drawLayer(Layer* layer) {
Romain Guy35643dd2012-09-18 15:40:58 -07002227 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002228 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002229 }
2230
Chris Craike84a2082014-12-22 14:28:49 -08002231 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002232 if (layer->isTextureLayer()) {
2233 transform = &layer->getTransform();
2234 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002235 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002236 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002237 }
2238 }
2239
Chris Craik39a908c2013-06-13 14:39:01 -07002240 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08002241 const bool rejected = mState.calculateQuickRejectForScissor(
Chris Craik3aadd602015-08-20 12:41:40 -07002242 0, 0, layer->layer.getWidth(), layer->layer.getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -08002243 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002244
2245 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002246 if (transform && !transform->isIdentity()) {
2247 restore();
2248 }
Tom Hudson107843d2014-09-08 11:26:26 -04002249 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002250 }
2251
Chris Craik62d307c2014-07-29 10:35:13 -07002252 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2253 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2254
Romain Guy5bb3c732012-11-29 17:52:58 -08002255 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002256
Chris Craik65fe5ee2015-01-26 18:06:29 -08002257 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08002258 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002259
Romain Guy211370f2012-02-01 16:10:55 -08002260 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002261 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002262 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2263 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002264 } else if (layer->mesh) {
Chris Craik82840732015-04-03 09:37:49 -07002265 Glop glop;
2266 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002267 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002268 .setMeshTexturedIndexedQuads(layer->mesh, layer->meshElementCount)
2269 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
Chris Craik53e51e42015-06-01 10:35:35 -07002270 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik3aadd602015-08-20 12:41:40 -07002271 .setModelViewOffsetRectSnap(0, 0, Rect(0, 0, layer->layer.getWidth(), layer->layer.getHeight()))
Chris Craik82840732015-04-03 09:37:49 -07002272 .build();
2273 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy3a3133d2011-02-01 22:59:58 -08002274#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07002275 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08002276#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002277 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002278
Romain Guy5bb3c732012-11-29 17:52:58 -08002279 if (layer->debugDrawUpdate) {
2280 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002281
2282 SkPaint paint;
2283 paint.setColor(0x7f00ff00);
Chris Craik3aadd602015-08-20 12:41:40 -07002284 drawColorRect(0, 0, layer->layer.getWidth(), layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002285 }
Romain Guyf219da52011-01-16 12:54:25 -08002286 }
Chris Craik34416ea2013-04-15 16:08:28 -07002287 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07002288
Romain Guyb2e2f242012-10-17 18:18:35 -07002289 if (transform && !transform->isIdentity()) {
2290 restore();
2291 }
2292
Tom Hudson107843d2014-09-08 11:26:26 -04002293 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08002294}
2295
Romain Guy6926c722010-07-12 20:20:03 -07002296///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002297// Draw filters
2298///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04002299void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
2300 // We should never get here since we apply the draw filter when stashing
2301 // the paints in the DisplayList.
2302 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08002303}
2304
2305///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002306// Drawing implementation
2307///////////////////////////////////////////////////////////////////////////////
2308
Chris Craikd218a922014-01-02 17:13:34 -08002309Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
Chris Craik15c3f192015-12-03 12:16:56 -08002310 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap->pixelRef());
Romain Guy3b748a42013-04-17 18:54:38 -07002311 if (!texture) {
2312 return mCaches.textureCache.get(bitmap);
2313 }
2314 return texture;
2315}
2316
Chris Craik2bb8f562015-02-17 16:42:02 -08002317void OpenGLRenderer::drawPathTexture(PathTexture* texture, float x, float y,
2318 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002319 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08002320 return;
2321 }
2322
Chris Craik82840732015-04-03 09:37:49 -07002323 Glop glop;
2324 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002325 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002326 .setMeshTexturedUnitQuad(nullptr)
2327 .setFillPathTexturePaint(*texture, *paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002328 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07002329 .setModelViewMapUnitToRect(Rect(x, y, x + texture->width, y + texture->height))
Chris Craik82840732015-04-03 09:37:49 -07002330 .build();
2331 renderGlop(glop);
Romain Guy01d58e42011-01-19 21:54:02 -08002332}
2333
Tom Hudson107843d2014-09-08 11:26:26 -04002334void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002335 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002336 return;
Romain Guy672433d2013-01-04 19:05:13 -08002337 }
2338
Tom Hudson107843d2014-09-08 11:26:26 -04002339 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08002340}
2341
Tom Hudson107843d2014-09-08 11:26:26 -04002342void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07002343 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04002344 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07002345
Chris Craik15a07a22014-01-26 13:43:53 -08002346 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08002347 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07002348
2349 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08002350 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07002351
ztenghui14a4e352014-08-13 10:44:39 -07002352 // The caller has made sure casterAlpha > 0.
2353 float ambientShadowAlpha = mAmbientShadowAlpha;
Chris Craik2507c342015-05-04 14:36:49 -07002354 if (CC_UNLIKELY(Properties::overrideAmbientShadowStrength >= 0)) {
2355 ambientShadowAlpha = Properties::overrideAmbientShadowStrength;
ztenghui14a4e352014-08-13 10:44:39 -07002356 }
2357 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
2358 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002359 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002360 }
ztenghui7b4516e2014-01-07 10:42:55 -08002361
ztenghui14a4e352014-08-13 10:44:39 -07002362 float spotShadowAlpha = mSpotShadowAlpha;
Chris Craik2507c342015-05-04 14:36:49 -07002363 if (CC_UNLIKELY(Properties::overrideSpotShadowStrength >= 0)) {
2364 spotShadowAlpha = Properties::overrideSpotShadowStrength;
ztenghui14a4e352014-08-13 10:44:39 -07002365 }
2366 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
2367 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002368 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002369 }
ztenghui7b4516e2014-01-07 10:42:55 -08002370
Tom Hudson107843d2014-09-08 11:26:26 -04002371 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07002372}
2373
Tom Hudson107843d2014-09-08 11:26:26 -04002374void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002375 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08002376 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04002377 return;
Romain Guy3b753822013-03-05 10:27:35 -08002378 }
Romain Guy735738c2012-12-03 12:34:51 -08002379
Romain Guy672433d2013-01-04 19:05:13 -08002380 float left = FLT_MAX;
2381 float top = FLT_MAX;
2382 float right = FLT_MIN;
2383 float bottom = FLT_MIN;
2384
Romain Guy448455f2013-07-22 13:57:50 -07002385 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08002386 Vertex* vertex = mesh;
2387
Chris Craik2af46352012-11-26 18:30:17 -08002388 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08002389 float l = rects[index + 0];
2390 float t = rects[index + 1];
2391 float r = rects[index + 2];
2392 float b = rects[index + 3];
2393
Romain Guy3b753822013-03-05 10:27:35 -08002394 Vertex::set(vertex++, l, t);
2395 Vertex::set(vertex++, r, t);
2396 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08002397 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08002398
Chris Craikdf72b632015-06-30 17:56:13 -07002399 left = std::min(left, l);
2400 top = std::min(top, t);
2401 right = std::max(right, r);
2402 bottom = std::max(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08002403 }
2404
Chris Craikf0a59072013-11-19 18:00:46 -08002405 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002406 return;
Romain Guya362c692013-02-04 13:50:16 -08002407 }
Romain Guy672433d2013-01-04 19:05:13 -08002408
Chris Craik53e51e42015-06-01 10:35:35 -07002409 const int transformFlags = ignoreTransform
2410 ? TransformFlags::MeshIgnoresCanvasTransform : TransformFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07002411 Glop glop;
2412 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002413 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002414 .setMeshIndexedQuads(&mesh[0], count / 4)
2415 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002416 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07002417 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07002418 .build();
2419 renderGlop(glop);
Romain Guy672433d2013-01-04 19:05:13 -08002420}
2421
Romain Guy026c5e162010-06-28 17:12:22 -07002422void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002423 const SkPaint* paint, bool ignoreTransform) {
Chris Craik53e51e42015-06-01 10:35:35 -07002424 const int transformFlags = ignoreTransform
2425 ? TransformFlags::MeshIgnoresCanvasTransform : TransformFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07002426 Glop glop;
2427 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002428 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002429 .setMeshUnitQuad()
2430 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002431 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07002432 .setModelViewMapUnitToRect(Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07002433 .build();
2434 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07002435}
2436
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002437float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik8df5ffa2015-04-28 17:47:20 -07002438 return (layer->getAlpha() / 255.0f) * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07002439}
2440
Romain Guy9d5316e2010-06-24 19:30:36 -07002441}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002442}; // namespace android