blob: 0cd763df78d1faf2468538aefdb4ae9d8654e41a [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
John Reck38e0c322015-11-10 12:19:17 -080017#include <GpuMemoryTracker.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080018#include "OpenGLRenderer.h"
19
20#include "DeferredDisplayList.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080021#include "GammaFontRenderer.h"
Chris Craik03188872015-02-02 18:39:33 -080022#include "Glop.h"
23#include "GlopBuilder.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080024#include "Patch.h"
25#include "PathTessellator.h"
26#include "Properties.h"
27#include "RenderNode.h"
Chris Craik03188872015-02-02 18:39:33 -080028#include "renderstate/MeshState.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080029#include "renderstate/RenderState.h"
30#include "ShadowTessellator.h"
31#include "SkiaShader.h"
32#include "Vector.h"
33#include "VertexBuffer.h"
34#include "utils/GLUtils.h"
35#include "utils/PaintUtils.h"
36#include "utils/TraceUtils.h"
37
Romain Guye4d01122010-06-16 18:44:05 -070038#include <stdlib.h>
39#include <stdint.h>
40#include <sys/types.h>
41
Romain Guy5cbbce52010-06-27 22:59:20 -070042#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070043#include <SkColor.h>
Chris Craik386aa032015-12-07 17:08:25 -080044#include <SkPaintDefaults.h>
Chris Craikfca52b752015-04-28 11:45:59 -070045#include <SkPathOps.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040046#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070047#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070048
Romain Guye4d01122010-06-16 18:44:05 -070049#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070050#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070051
Romain Guy08aa2cb2011-03-17 11:06:57 -070052#include <private/hwui/DrawGlInfo.h>
53
Romain Guy5b3b3522010-10-27 18:57:51 -070054#include <ui/Rect.h>
55
Chris Craik62d307c2014-07-29 10:35:13 -070056#if DEBUG_DETAILED_EVENTS
57 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
58#else
59 #define EVENT_LOGD(...)
60#endif
61
Romain Guye4d01122010-06-16 18:44:05 -070062namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070063namespace uirenderer {
64
Romain Guy9d5316e2010-06-24 19:30:36 -070065///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -070066// Constructors/destructor
67///////////////////////////////////////////////////////////////////////////////
68
John Reck3b202512014-06-23 13:13:08 -070069OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -040070 : mState(*this)
Chris Craik058fc642014-07-23 18:19:28 -070071 , mCaches(Caches::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -070072 , mRenderState(renderState)
Chris Craik65fe5ee2015-01-26 18:06:29 -080073 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -070074 , mScissorOptimizationDisabled(false)
Tom Hudson107843d2014-09-08 11:26:26 -040075 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -070076 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -070077 , mLightRadius(FLT_MIN)
78 , mAmbientShadowAlpha(0)
79 , mSpotShadowAlpha(0) {
Romain Guye4d01122010-06-16 18:44:05 -070080}
81
Romain Guy85bf02f2010-06-22 13:11:24 -070082OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -070083 // The context has already been destroyed at this point, do not call
84 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -070085}
86
Romain Guy87e2f7572012-09-24 11:37:12 -070087void OpenGLRenderer::initProperties() {
88 char property[PROPERTY_VALUE_MAX];
89 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
90 mScissorOptimizationDisabled = !strcasecmp(property, "true");
91 INIT_LOGD(" Scissor optimization %s",
92 mScissorOptimizationDisabled ? "disabled" : "enabled");
93 } else {
94 INIT_LOGD(" Scissor optimization enabled");
95 }
Romain Guy13631f32012-01-30 17:41:55 -080096}
97
Alan Viverette50210d92015-05-14 18:05:36 -070098void OpenGLRenderer::initLight(float lightRadius, uint8_t ambientShadowAlpha,
99 uint8_t spotShadowAlpha) {
Chris Craik058fc642014-07-23 18:19:28 -0700100 mLightRadius = lightRadius;
101 mAmbientShadowAlpha = ambientShadowAlpha;
102 mSpotShadowAlpha = spotShadowAlpha;
103}
104
Alan Viverette50210d92015-05-14 18:05:36 -0700105void OpenGLRenderer::setLightCenter(const Vector3& lightCenter) {
106 mLightCenter = lightCenter;
107}
108
Romain Guy13631f32012-01-30 17:41:55 -0800109///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700110// Setup
111///////////////////////////////////////////////////////////////////////////////
112
Chris Craik797b95b22014-05-20 18:10:25 -0700113void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700114 glDisable(GL_DITHER);
115 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy35643dd2012-09-18 15:40:58 -0700116}
117
Chris Craik64e445b2015-09-02 14:23:49 -0700118void OpenGLRenderer::setupFrameState(int viewportWidth, int viewportHeight,
119 float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800120 mCaches.clearGarbage();
Chris Craik64e445b2015-09-02 14:23:49 -0700121 mState.initializeSaveStack(viewportWidth, viewportHeight,
122 left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700123 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700124 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700125}
126
Tom Hudson107843d2014-09-08 11:26:26 -0400127void OpenGLRenderer::startFrame() {
128 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700129 mFrameStarted = true;
130
Tom Hudson984162f2014-10-10 13:38:16 -0400131 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700132
Romain Guy96885eb2013-03-26 15:05:58 -0700133 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700134
Tom Hudson984162f2014-10-10 13:38:16 -0400135 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800136
Romain Guy7c450aa2012-09-21 19:15:00 -0700137 debugOverdraw(true, true);
138
Tom Hudson107843d2014-09-08 11:26:26 -0400139 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700140 mTilingClip.right, mTilingClip.bottom, mOpaque);
141}
142
Chris Craik64e445b2015-09-02 14:23:49 -0700143void OpenGLRenderer::prepareDirty(int viewportWidth, int viewportHeight,
144 float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700145
Chris Craik64e445b2015-09-02 14:23:49 -0700146 setupFrameState(viewportWidth, viewportHeight, left, top, right, bottom, opaque);
Romain Guy96885eb2013-03-26 15:05:58 -0700147
148 // Layer renderers will start the frame immediately
149 // The framebuffer renderer will first defer the display list
150 // for each layer and wait until the first drawing command
151 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800152 if (currentSnapshot()->fbo == 0) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800153 mRenderState.blend().syncEnabled();
Romain Guy96885eb2013-03-26 15:05:58 -0700154 updateLayers();
155 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400156 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700157 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700158}
159
Romain Guydcfc8362013-01-03 13:08:57 -0800160void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
161 // If we know that we are going to redraw the entire framebuffer,
162 // perform a discard to let the driver know we don't need to preserve
163 // the back buffer for this frame.
Chris Craik117bdbc2015-02-05 10:12:38 -0800164 if (mCaches.extensions().hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400165 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
Chris Craik6b109c72015-02-27 10:55:28 -0800166 const bool isFbo = getTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800167 const GLenum attachments[] = {
168 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
169 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800170 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
171 }
172}
173
Tom Hudson107843d2014-09-08 11:26:26 -0400174void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700175 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800176 mRenderState.scissor().setEnabled(true);
177 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700178 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400179 mDirty = true;
180 return;
Romain Guyddf74372012-05-22 14:07:07 -0700181 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700182
Chris Craik65fe5ee2015-01-26 18:06:29 -0800183 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700184}
185
Tom Hudson107843d2014-09-08 11:26:26 -0400186bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700187 renderOverdraw();
Chris Craik4ac36f82014-12-09 16:54:03 -0800188 mTempPaths.clear();
189
Romain Guyca89e2a2013-03-08 17:44:20 -0800190 // When finish() is invoked on FBO 0 we've reached the end
191 // of the current frame
Chris Craik6b109c72015-02-27 10:55:28 -0800192 if (getTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800193 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700194 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800195 }
196
Romain Guy11cb6422012-09-21 00:39:43 -0700197 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700198#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700199 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700200#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700201
Romain Guyc15008e2010-11-10 11:59:15 -0800202#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800203 mCaches.dumpMemoryUsage();
John Reck38e0c322015-11-10 12:19:17 -0800204 GPUMemoryTracker::dump();
Romain Guy11cb6422012-09-21 00:39:43 -0700205#else
Chris Craik2507c342015-05-04 14:36:49 -0700206 if (Properties::debugLevel & kDebugMemory) {
Romain Guy11cb6422012-09-21 00:39:43 -0700207 mCaches.dumpMemoryUsage();
208 }
Romain Guyc15008e2010-11-10 11:59:15 -0800209#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700210 }
Romain Guy96885eb2013-03-26 15:05:58 -0700211
212 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400213
214 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700215}
216
Romain Guy35643dd2012-09-18 15:40:58 -0700217void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700218 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
219 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700220 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700221
Chris Craik65fe5ee2015-01-26 18:06:29 -0800222 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700223 dirtyClip();
224}
225
Andreas Gampe64bb4132014-11-22 00:35:09 +0000226void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400227 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700228
Chris Craik6fe991e52015-10-20 09:39:42 -0700229 Rect clip(mState.currentRenderTargetClip());
Romain Guy80911b82011-03-16 15:30:12 -0700230 clip.snapToPixelBoundaries();
231
Romain Guyd643bb52011-03-01 14:55:21 -0800232 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800233 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800234 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800235 dirtyLayerUnchecked(clip, getRegion());
236 }
Romain Guyd643bb52011-03-01 14:55:21 -0800237
Romain Guy08aa2cb2011-03-17 11:06:57 -0700238 DrawGlInfo info;
239 info.clipLeft = clip.left;
240 info.clipTop = clip.top;
241 info.clipRight = clip.right;
242 info.clipBottom = clip.bottom;
243 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700244 info.width = getViewportWidth();
245 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800246 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700247
Tom Hudson984162f2014-10-10 13:38:16 -0400248 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700249 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400250 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700251 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
252 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800253 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700254 setScissorFromClip();
255 }
Chris Craik54f574a2013-08-26 11:23:46 -0700256
John Reck3b202512014-06-23 13:13:08 -0700257 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
258 // Scissor may have been modified, reset dirty clip
259 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800260
Tom Hudson107843d2014-09-08 11:26:26 -0400261 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800262}
263
Romain Guyf6a11b82010-06-23 17:47:49 -0700264///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700265// Debug
266///////////////////////////////////////////////////////////////////////////////
267
Chris Craik62d307c2014-07-29 10:35:13 -0700268void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
269#if DEBUG_DETAILED_EVENTS
270 const int BUFFER_SIZE = 256;
271 va_list ap;
272 char buf[BUFFER_SIZE];
273
274 va_start(ap, fmt);
275 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
276 va_end(ap);
277
278 eventMark(buf);
279#endif
280}
281
282
Romain Guy0f6675332013-03-01 14:31:04 -0800283void OpenGLRenderer::eventMark(const char* name) const {
284 mCaches.eventMark(0, name);
285}
286
Romain Guy87e2f7572012-09-24 11:37:12 -0700287void OpenGLRenderer::startMark(const char* name) const {
288 mCaches.startMark(0, name);
289}
290
291void OpenGLRenderer::endMark() const {
292 mCaches.endMark();
293}
294
295void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700296 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700297}
298
299void OpenGLRenderer::renderOverdraw() {
Chris Craik2507c342015-05-04 14:36:49 -0700300 if (Properties::debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700301 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700302
Chris Craik65fe5ee2015-01-26 18:06:29 -0800303 mRenderState.scissor().setEnabled(true);
304 mRenderState.scissor().set(clip->left,
305 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
306 clip->right - clip->left,
307 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700308
Romain Guy627c6fd2013-08-21 11:53:18 -0700309 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800310 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700311 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
312
313 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800314 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700315 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
316
317 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800318 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700319 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
320
321 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800322 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700323 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
324
Chris Craik96a5c4c2015-01-27 15:46:35 -0800325 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700326 }
327}
328
329///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700330// Layers
331///////////////////////////////////////////////////////////////////////////////
332
333bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700334 if (layer->deferredUpdateScheduled && layer->renderer
335 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700336
Romain Guy7c450aa2012-09-21 19:15:00 -0700337 if (inFrame) {
Romain Guy7c450aa2012-09-21 19:15:00 -0700338 debugOverdraw(false, false);
339 }
Romain Guy11cb6422012-09-21 00:39:43 -0700340
Chris Craik2507c342015-05-04 14:36:49 -0700341 if (CC_UNLIKELY(inFrame || Properties::drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700342 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700343 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700344 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700345 }
Romain Guy11cb6422012-09-21 00:39:43 -0700346
347 if (inFrame) {
348 resumeAfterLayer();
Romain Guy11cb6422012-09-21 00:39:43 -0700349 }
350
Chris Craik2507c342015-05-04 14:36:49 -0700351 layer->debugDrawUpdate = Properties::debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700352 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700353
354 return true;
355 }
356
357 return false;
358}
359
360void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700361 // If draw deferring is enabled this method will simply defer
362 // the display list of each individual layer. The layers remain
363 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700364 int count = mLayerUpdates.size();
365 if (count > 0) {
Chris Craik2507c342015-05-04 14:36:49 -0700366 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -0700367 startMark("Layer Updates");
368 } else {
369 startMark("Defer Layer Updates");
370 }
Romain Guy11cb6422012-09-21 00:39:43 -0700371
Chris Craik1206b9b2013-04-04 14:46:24 -0700372 // Note: it is very important to update the layers in order
373 for (int i = 0; i < count; i++) {
John Reck272a6852015-07-29 16:48:58 -0700374 Layer* layer = mLayerUpdates[i].get();
Romain Guy11cb6422012-09-21 00:39:43 -0700375 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700376 }
Romain Guy11cb6422012-09-21 00:39:43 -0700377
Chris Craik2507c342015-05-04 14:36:49 -0700378 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -0700379 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800380 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700381 }
382 endMark();
383 }
384}
385
386void OpenGLRenderer::flushLayers() {
387 int count = mLayerUpdates.size();
388 if (count > 0) {
389 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700390
Chris Craik1206b9b2013-04-04 14:46:24 -0700391 // Note: it is very important to update the layers in order
392 for (int i = 0; i < count; i++) {
John Reck272a6852015-07-29 16:48:58 -0700393 mLayerUpdates[i]->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700394 }
395
396 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800397 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700398
Romain Guy11cb6422012-09-21 00:39:43 -0700399 endMark();
400 }
401}
402
403void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
404 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700405 // Make sure we don't introduce duplicates.
406 // SortedVector would do this automatically but we need to respect
407 // the insertion order. The linear search is not an issue since
408 // this list is usually very short (typically one item, at most a few)
409 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
John Reck272a6852015-07-29 16:48:58 -0700410 if (mLayerUpdates[i] == layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700411 return;
412 }
413 }
Romain Guy11cb6422012-09-21 00:39:43 -0700414 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700415 }
416}
417
Romain Guye93482f2013-06-17 13:14:51 -0700418void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
419 if (layer) {
420 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
John Reck272a6852015-07-29 16:48:58 -0700421 if (mLayerUpdates[i] == layer) {
422 mLayerUpdates.erase(mLayerUpdates.begin() + i);
Romain Guye93482f2013-06-17 13:14:51 -0700423 break;
424 }
425 }
426 }
427}
428
Romain Guy40543602013-06-12 15:31:28 -0700429void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800430 ATRACE_NAME("Update HW Layers");
Chris Craik44eb2c02015-01-29 09:45:09 -0800431 mRenderState.blend().syncEnabled();
Romain Guy40543602013-06-12 15:31:28 -0700432 updateLayers();
433 flushLayers();
434 // Wait for all the layer updates to be executed
John Reck55156372015-01-21 07:46:37 -0800435 glFinish();
Romain Guy40543602013-06-12 15:31:28 -0700436}
437
John Reck443a7142014-09-04 17:40:05 -0700438void OpenGLRenderer::markLayersAsBuildLayers() {
439 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
440 mLayerUpdates[i]->wasBuildLayered = true;
441 }
442}
443
Romain Guy11cb6422012-09-21 00:39:43 -0700444///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700445// State management
446///////////////////////////////////////////////////////////////////////////////
447
Chris Craik14e51302013-12-30 15:32:54 -0800448void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700449 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800450 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
451 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700452
Chris Craika64a2be2014-05-14 14:17:01 -0700453 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700454 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700455 }
456
Romain Guy2542d192010-08-18 11:47:12 -0700457 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700458 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700459 }
Romain Guy2542d192010-08-18 11:47:12 -0700460
Romain Guy5ec99242010-11-03 16:19:08 -0700461 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700462 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700463 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700464 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800465 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700466 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700467 }
Romain Guybb9524b2010-06-22 18:56:38 -0700468}
469
Romain Guyf6a11b82010-06-23 17:47:49 -0700470///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700471// Layers
472///////////////////////////////////////////////////////////////////////////////
473
474int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700475 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700476 // force matrix/clip isolation for layer
477 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
478
Tom Hudson984162f2014-10-10 13:38:16 -0400479 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700480
Tom Hudson984162f2014-10-10 13:38:16 -0400481 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700482 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700483 }
Romain Guyd55a8612010-06-28 17:42:46 -0700484
485 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700486}
487
Chris Craikd90144d2013-03-19 15:03:48 -0700488void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
489 const Rect untransformedBounds(bounds);
490
Chris Craikd6b65f62014-01-01 14:45:21 -0800491 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700492
493 // Layers only make sense if they are in the framebuffer's bounds
Chris Craik6fe991e52015-10-20 09:39:42 -0700494 bounds.doIntersect(mState.currentRenderTargetClip());
Chris Craikac02eb92015-10-05 12:23:46 -0700495 if (!bounds.isEmpty()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700496 // We cannot work with sub-pixels in this case
497 bounds.snapToPixelBoundaries();
498
499 // When the layer is not an FBO, we may use glCopyTexImage so we
500 // need to make sure the layer does not extend outside the bounds
501 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700502 const Snapshot& previous = *(currentSnapshot()->previous);
503 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
Chris Craikac02eb92015-10-05 12:23:46 -0700504
505 bounds.doIntersect(previousViewport);
506 if (!bounds.isEmpty() && fboLayer) {
Chris Craikd90144d2013-03-19 15:03:48 -0700507 clip.set(bounds);
508 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800509 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700510 inverse.mapRect(clip);
511 clip.snapToPixelBoundaries();
Chris Craikac02eb92015-10-05 12:23:46 -0700512 clip.doIntersect(untransformedBounds);
513 if (!clip.isEmpty()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700514 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
515 bounds.set(untransformedBounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700516 }
517 }
Chris Craikd90144d2013-03-19 15:03:48 -0700518 }
519}
520
Chris Craik408eb122013-03-26 18:55:15 -0700521void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
522 bool fboLayer, int alpha) {
523 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
524 bounds.getHeight() > mCaches.maxTextureSize ||
525 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400526 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700527 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400528 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700529 }
530}
531
Chris Craikd90144d2013-03-19 15:03:48 -0700532int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500533 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400534 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700535
Tom Hudson984162f2014-10-10 13:38:16 -0400536 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700537 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
538 // operations will be able to store and restore the current clip and transform info, and
539 // quick rejection will be correct (for display lists)
540
541 Rect bounds(left, top, right, bottom);
542 Rect clip;
543 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craikbf6f0f22015-10-01 12:36:07 -0700544 updateSnapshotIgnoreForLayer(bounds, clip, true, PaintUtils::getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700545
Tom Hudson984162f2014-10-10 13:38:16 -0400546 if (!mState.currentlyIgnored()) {
547 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
548 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
549 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800550 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700551 }
552 }
553
554 return count;
555}
556
Romain Guy1c740bc2010-09-13 18:00:09 -0700557/**
558 * Layers are viewed by Skia are slightly different than layers in image editing
559 * programs (for instance.) When a layer is created, previously created layers
560 * and the frame buffer still receive every drawing command. For instance, if a
561 * layer is created and a shape intersecting the bounds of the layers and the
562 * framebuffer is draw, the shape will be drawn on both (unless the layer was
563 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
564 *
565 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
566 * texture. Unfortunately, this is inefficient as it requires every primitive to
567 * be drawn n + 1 times, where n is the number of active layers. In practice this
568 * means, for every primitive:
569 * - Switch active frame buffer
570 * - Change viewport, clip and projection matrix
571 * - Issue the drawing
572 *
573 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700574 * To avoid this, layers are implemented in a different way here, at least in the
575 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
576 * is set. When this flag is set we can redirect all drawing operations into a
577 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700578 *
579 * This implementation relies on the frame buffer being at least RGBA 8888. When
580 * a layer is created, only a texture is created, not an FBO. The content of the
581 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700582 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700583 * buffer and drawing continues as normal. This technique therefore treats the
584 * frame buffer as a scratch buffer for the layers.
585 *
586 * To compose the layers back onto the frame buffer, each layer texture
587 * (containing the original frame buffer data) is drawn as a simple quad over
588 * the frame buffer. The trick is that the quad is set as the composition
589 * destination in the blending equation, and the frame buffer becomes the source
590 * of the composition.
591 *
592 * Drawing layers with an alpha value requires an extra step before composition.
593 * An empty quad is drawn over the layer's region in the frame buffer. This quad
594 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
595 * quad is used to multiply the colors in the frame buffer. This is achieved by
596 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
597 * GL_ZERO, GL_SRC_ALPHA.
598 *
599 * Because glCopyTexImage2D() can be slow, an alternative implementation might
600 * be use to draw a single clipped layer. The implementation described above
601 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700602 *
603 * (1) The frame buffer is actually not cleared right away. To allow the GPU
604 * to potentially optimize series of calls to glCopyTexImage2D, the frame
605 * buffer is left untouched until the first drawing operation. Only when
606 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700607 */
Chet Haased48885a2012-08-28 17:43:28 -0700608bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700609 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800610 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
611 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700612
Romain Guyeb993562010-10-05 18:14:38 -0700613 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
614
Romain Guyf607bdc2010-09-10 19:20:06 -0700615 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700616 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700617 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700618 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craikbf6f0f22015-10-01 12:36:07 -0700619 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, PaintUtils::getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700620
621 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400622 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700623 return false;
624 }
Romain Guyf18fd992010-07-08 11:45:51 -0700625
Chris Craik44eb2c02015-01-29 09:45:09 -0800626 mCaches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700627 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700628 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700629 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700630 }
631
Derek Sollenberger674554f2014-02-19 16:47:32 +0000632 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700633 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700634 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
635 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500636
Chet Haasea23eed82012-04-12 15:19:04 -0700637 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700638 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700639 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700640
Romain Guy8fb95422010-08-17 18:38:51 -0700641 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400642 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
643 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700644
Chris Craika8bea8e2014-09-24 11:29:43 -0700645 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
646 fboLayer ? "" : "unclipped ",
647 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700648 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700649 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700650 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700651 } else {
652 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700653 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800654 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700655 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700656 // Workaround for some GL drivers. When reading pixels lying outside
657 // of the window we should get undefined values for those pixels.
658 // Unfortunately some drivers will turn the entire target texture black
659 // when reading outside of the window.
660 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800661 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700662 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800663 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700664
Chris Craika64a2be2014-05-14 14:17:01 -0700665 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
666 bounds.left, getViewportHeight() - bounds.bottom,
667 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700668
Romain Guy54be1cd2011-06-13 19:04:27 -0700669 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800670 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700671 }
Romain Guyeb993562010-10-05 18:14:38 -0700672 }
Romain Guyf86ef572010-07-01 11:05:42 -0700673
Romain Guyd55a8612010-06-28 17:42:46 -0700674 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700675}
676
Chris Craike63f7c622013-10-17 10:30:55 -0700677bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800678 layer->clipRect.set(clip);
Chris Craik818c9fb2015-10-23 14:33:42 -0700679 layer->setFbo(mRenderState.genFramebuffer());
Romain Guy5b3b3522010-10-27 18:57:51 -0700680
Tom Hudson984162f2014-10-10 13:38:16 -0400681 writableSnapshot()->region = &writableSnapshot()->layer->region;
682 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
683 writableSnapshot()->fbo = layer->getFbo();
684 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
685 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
686 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800687 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700688
Romain Guy7c450aa2012-09-21 19:15:00 -0700689 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700690 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700691 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700692 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700693
694 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700695 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700696 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700697 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700698 }
699
700 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Chris Craikf27133d2015-02-19 09:51:53 -0800701 layer->getTextureId(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700702
Romain Guy5b3b3522010-10-27 18:57:51 -0700703 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800704 mRenderState.scissor().setEnabled(true);
705 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700706 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700707 glClear(GL_COLOR_BUFFER_BIT);
708
709 dirtyClip();
710
711 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700712 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700713 return true;
714}
715
Romain Guy1c740bc2010-09-13 18:00:09 -0700716/**
717 * Read the documentation of createLayer() before doing anything in this method.
718 */
Chris Craik14e51302013-12-30 15:32:54 -0800719void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
720 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000721 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700722 return;
723 }
724
Chris Craik14e51302013-12-30 15:32:54 -0800725 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800726 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800727 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700728
Chris Craik39a908c2013-06-13 14:39:01 -0700729 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400730 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800731 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800732 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700733
Romain Guyeb993562010-10-05 18:14:38 -0700734 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700735 // Detach the texture from the FBO
736 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800737
738 layer->removeFbo(false);
739
Romain Guyeb993562010-10-05 18:14:38 -0700740 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700741 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700742 debugOverdraw(true, false);
Romain Guyeb993562010-10-05 18:14:38 -0700743 }
744
Romain Guy9ace8f52011-07-07 20:50:11 -0700745 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500746 SkPaint layerPaint;
747 layerPaint.setAlpha(layer->getAlpha());
748 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
749 layerPaint.setColorFilter(layer->getColorFilter());
750
751 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700752 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700753 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700754 }
755
Chris Craik96a5c4c2015-01-27 15:46:35 -0800756 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700757
Chris Craik44eb2c02015-01-29 09:45:09 -0800758 mCaches.textureState().activateTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700759
Romain Guy5b3b3522010-10-27 18:57:51 -0700760 // When the layer is stored in an FBO, we can save a bit of fillrate by
761 // drawing only the dirty region
762 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800763 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700764 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700765 } else if (!rect.isEmpty()) {
766 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530767
768 save(0);
769 // the layer contains screen buffer content that shouldn't be alpha modulated
770 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400771 writableSnapshot()->alpha = 1.0f;
Chris Craik53e51e42015-06-01 10:35:35 -0700772 composeLayerRectSwapped(layer, rect);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530773 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700774 }
Romain Guy8b55f372010-08-18 17:10:07 -0700775
Romain Guy746b7402010-10-26 16:27:31 -0700776 dirtyClip();
777
Romain Guyeb993562010-10-05 18:14:38 -0700778 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800779 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700780 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800781 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800782 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700783 }
784}
785
Romain Guyaa6c24c2011-04-28 18:40:04 -0700786void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craik1e4c8072015-05-26 14:25:02 -0700787 const bool tryToSnap = !layer->getForceFilter()
Chris Craik26bf3422015-02-26 16:28:17 -0800788 && layer->getWidth() == (uint32_t) rect.getWidth()
Chris Craik82840732015-04-03 09:37:49 -0700789 && layer->getHeight() == (uint32_t) rect.getHeight();
790 Glop glop;
791 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700792 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -0700793 .setMeshTexturedUvQuad(nullptr, Rect(0, 1, 1, 0)) // TODO: simplify with VBO
794 .setFillTextureLayer(*layer, getLayerAlpha(layer))
Chris Craik53e51e42015-06-01 10:35:35 -0700795 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik1e4c8072015-05-26 14:25:02 -0700796 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, rect)
Chris Craik82840732015-04-03 09:37:49 -0700797 .build();
798 renderGlop(glop);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700799}
800
Chris Craik53e51e42015-06-01 10:35:35 -0700801void OpenGLRenderer::composeLayerRectSwapped(Layer* layer, const Rect& rect) {
802 Glop glop;
803 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700804 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik53e51e42015-06-01 10:35:35 -0700805 .setMeshTexturedUvQuad(nullptr, layer->texCoords)
806 .setFillLayer(layer->getTexture(), layer->getColorFilter(),
807 getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::Swap)
808 .setTransform(*currentSnapshot(), TransformFlags::MeshIgnoresCanvasTransform)
809 .setModelViewMapUnitToRect(rect)
Chris Craik53e51e42015-06-01 10:35:35 -0700810 .build();
811 renderGlop(glop);
812}
813
814void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect) {
Chris Craik62d307c2014-07-29 10:35:13 -0700815 if (layer->isTextureLayer()) {
816 EVENT_LOGD("composeTextureLayerRect");
Chris Craik62d307c2014-07-29 10:35:13 -0700817 drawTextureLayer(layer, rect);
Chris Craik62d307c2014-07-29 10:35:13 -0700818 } else {
819 EVENT_LOGD("composeHardwareLayerRect");
Chris Craik182952f2015-03-09 14:17:29 -0700820
Chris Craik53e51e42015-06-01 10:35:35 -0700821 const bool tryToSnap = layer->getWidth() == static_cast<uint32_t>(rect.getWidth())
Chris Craik82840732015-04-03 09:37:49 -0700822 && layer->getHeight() == static_cast<uint32_t>(rect.getHeight());
823 Glop glop;
824 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700825 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -0700826 .setMeshTexturedUvQuad(nullptr, layer->texCoords)
Chris Craik53e51e42015-06-01 10:35:35 -0700827 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
828 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik1e4c8072015-05-26 14:25:02 -0700829 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, rect)
Chris Craik82840732015-04-03 09:37:49 -0700830 .build();
831 renderGlop(glop);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700832 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700833}
834
Chris Craik34416ea2013-04-15 16:08:28 -0700835/**
836 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
837 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
838 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
839 * by saveLayer's restore
840 */
Chris Craik2507c342015-05-04 14:36:49 -0700841#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
842 DRAW_COMMAND; \
843 if (CC_UNLIKELY(Properties::debugOverdraw && getTargetFbo() == 0 && COND)) { \
844 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
845 DRAW_COMMAND; \
846 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
847 } \
Chris Craik34416ea2013-04-15 16:08:28 -0700848 }
849
850#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
851
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400852// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
853// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
854class LayerShader : public SkShader {
855public:
856 LayerShader(Layer* layer, const SkMatrix* localMatrix)
857 : INHERITED(localMatrix)
858 , mLayer(layer) {
859 }
860
Chris Craike84a2082014-12-22 14:28:49 -0800861 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400862 if (data) {
863 *data = static_cast<void*>(mLayer);
864 }
865 return true;
866 }
867
Chris Craike84a2082014-12-22 14:28:49 -0800868 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400869 return !mLayer->isBlend();
870 }
871
872protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000873 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400874 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
875 }
876
Chris Craike84a2082014-12-22 14:28:49 -0800877 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400878 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
879 }
880
Chris Craike84a2082014-12-22 14:28:49 -0800881 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400882 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -0800883 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400884 }
885private:
886 // Unowned.
887 Layer* mLayer;
888 typedef SkShader INHERITED;
889};
890
Romain Guy5b3b3522010-10-27 18:57:51 -0700891void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -0700892 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
893
894 if (layer->getConvexMask()) {
895 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
896
897 // clip to the area of the layer the mask can be larger
898 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
899
900 SkPaint paint;
901 paint.setAntiAlias(true);
902 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
903
Chris Craik3f0854292014-04-15 16:18:08 -0700904 // create LayerShader to map SaveLayer content into subsequent draw
905 SkMatrix shaderMatrix;
906 shaderMatrix.setTranslate(rect.left, rect.bottom);
907 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400908 LayerShader layerShader(layer, &shaderMatrix);
909 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -0700910
911 // Since the drawing primitive is defined in local drawing space,
912 // we don't need to modify the draw matrix
913 const SkPath* maskPath = layer->getConvexMask();
914 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
915
Chris Craike84a2082014-12-22 14:28:49 -0800916 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -0700917 restore();
918
919 return;
920 }
921
Romain Guy5b3b3522010-10-27 18:57:51 -0700922 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700923 layer->setRegionAsRect();
924
Chris Craik34416ea2013-04-15 16:08:28 -0700925 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -0700926
Romain Guy5b3b3522010-10-27 18:57:51 -0700927 layer->region.clear();
928 return;
929 }
930
Chris Craik62d307c2014-07-29 10:35:13 -0700931 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -0700932 // standard Region based draw
933 size_t count;
934 const android::Rect* rects;
935 Region safeRegion;
936 if (CC_LIKELY(hasRectToRectTransform())) {
937 rects = layer->region.getArray(&count);
938 } else {
939 safeRegion = Region::createTJunctionFreeRegion(layer->region);
940 rects = safeRegion.getArray(&count);
941 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700942
Chris Craik3f0854292014-04-15 16:18:08 -0700943 const float texX = 1.0f / float(layer->getWidth());
944 const float texY = 1.0f / float(layer->getHeight());
945 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700946
Chris Craik82840732015-04-03 09:37:49 -0700947 TextureVertex quadVertices[count * 4];
948 TextureVertex* mesh = &quadVertices[0];
Chris Craik3f0854292014-04-15 16:18:08 -0700949 for (size_t i = 0; i < count; i++) {
950 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -0700951
Chris Craik3f0854292014-04-15 16:18:08 -0700952 const float u1 = r->left * texX;
953 const float v1 = (height - r->top) * texY;
954 const float u2 = r->right * texX;
955 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700956
Chris Craik3f0854292014-04-15 16:18:08 -0700957 // TODO: Reject quads outside of the clip
958 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
959 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
960 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
961 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Chris Craik3f0854292014-04-15 16:18:08 -0700962 }
Tom Hudson040b6d82015-04-16 10:50:53 -0400963 Rect modelRect = Rect(rect.getWidth(), rect.getHeight());
Chris Craik82840732015-04-03 09:37:49 -0700964 Glop glop;
965 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700966 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -0700967 .setMeshTexturedIndexedQuads(&quadVertices[0], count * 6)
968 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
Chris Craik53e51e42015-06-01 10:35:35 -0700969 .setTransform(*currentSnapshot(), TransformFlags::None)
Tom Hudson040b6d82015-04-16 10:50:53 -0400970 .setModelViewOffsetRectSnap(rect.left, rect.top, modelRect)
Chris Craik82840732015-04-03 09:37:49 -0700971 .build();
972 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy5b3b3522010-10-27 18:57:51 -0700973
Romain Guy5b3b3522010-10-27 18:57:51 -0700974#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -0700975 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700976#endif
977
Chris Craik3f0854292014-04-15 16:18:08 -0700978 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -0700979}
980
Romain Guy3a3133d2011-02-01 22:59:58 -0800981#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -0700982void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -0800983 size_t count;
984 const android::Rect* rects = region.getArray(&count);
985
986 uint32_t colors[] = {
987 0x7fff0000, 0x7f00ff00,
988 0x7f0000ff, 0x7fff00ff,
989 };
990
991 int offset = 0;
992 int32_t top = rects[0].top;
993
994 for (size_t i = 0; i < count; i++) {
995 if (top != rects[i].top) {
996 offset ^= 0x2;
997 top = rects[i].top;
998 }
999
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001000 SkPaint paint;
1001 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001002 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001003 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001004 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001005}
Chris Craike63f7c622013-10-17 10:30:55 -07001006#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001007
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001008void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001009 Vector<float> rects;
1010
1011 SkRegion::Iterator it(region);
1012 while (!it.done()) {
1013 const SkIRect& r = it.rect();
1014 rects.push(r.fLeft);
1015 rects.push(r.fTop);
1016 rects.push(r.fRight);
1017 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001018 it.next();
1019 }
1020
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001021 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001022}
1023
Romain Guy5b3b3522010-10-27 18:57:51 -07001024void OpenGLRenderer::dirtyLayer(const float left, const float top,
Chris Craik083e7332015-02-27 17:04:20 -08001025 const float right, const float bottom, const Matrix4& transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001026 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001027 Rect bounds(left, top, right, bottom);
1028 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001029 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001030 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001031}
1032
1033void OpenGLRenderer::dirtyLayer(const float left, const float top,
1034 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001035 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001036 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001037 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001038 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001039}
1040
1041void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Chris Craik6fe991e52015-10-20 09:39:42 -07001042 bounds.doIntersect(mState.currentRenderTargetClip());
Chris Craikac02eb92015-10-05 12:23:46 -07001043 if (!bounds.isEmpty()) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001044 bounds.snapToPixelBoundaries();
1045 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1046 if (!dirty.isEmpty()) {
1047 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001048 }
1049 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001050}
1051
Romain Guy54be1cd2011-06-13 19:04:27 -07001052void OpenGLRenderer::clearLayerRegions() {
Chris Craik2bb8f562015-02-17 16:42:02 -08001053 const size_t quadCount = mLayers.size();
1054 if (quadCount == 0) return;
Romain Guy54be1cd2011-06-13 19:04:27 -07001055
Tom Hudson984162f2014-10-10 13:38:16 -04001056 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001057 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001058 // Doing several glScissor/glClear here can negatively impact
1059 // GPUs with a tiler architecture, instead we draw quads with
1060 // the Clear blending mode
1061
1062 // The list contains bounds that have already been clipped
1063 // against their initial clip rect, and the current clip
1064 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001065 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001066
Chris Craik2bb8f562015-02-17 16:42:02 -08001067 Vertex mesh[quadCount * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001068 Vertex* vertex = mesh;
1069
Chris Craik2bb8f562015-02-17 16:42:02 -08001070 for (uint32_t i = 0; i < quadCount; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001071 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001072
Chris Craik51d6a3d2014-12-22 17:16:56 -08001073 Vertex::set(vertex++, bounds.left, bounds.top);
1074 Vertex::set(vertex++, bounds.right, bounds.top);
1075 Vertex::set(vertex++, bounds.left, bounds.bottom);
1076 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001077 }
Romain Guye67307c2013-02-11 18:01:20 -08001078 // We must clear the list of dirty rects before we
Chris Craik82840732015-04-03 09:37:49 -07001079 // call clearLayerRegions() in renderGlop to prevent
1080 // stencil setup from doing the same thing again
Romain Guye67307c2013-02-11 18:01:20 -08001081 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001082
Chris Craik53e51e42015-06-01 10:35:35 -07001083 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001084 Glop glop;
1085 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001086 .setRoundRectClipState(nullptr) // clear ignores clip state
Chris Craik82840732015-04-03 09:37:49 -07001087 .setMeshIndexedQuads(&mesh[0], quadCount)
1088 .setFillClear()
Chris Craik53e51e42015-06-01 10:35:35 -07001089 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik6fe991e52015-10-20 09:39:42 -07001090 .setModelViewOffsetRect(0, 0, Rect(currentSnapshot()->getRenderTargetClip()))
Chris Craik82840732015-04-03 09:37:49 -07001091 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001092 renderGlop(glop, GlopRenderType::LayerClear);
Romain Guy8a4ac612012-07-17 17:32:48 -07001093
Chris Craik65fe5ee2015-01-26 18:06:29 -08001094 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001095 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001096 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001097 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001098}
1099
Romain Guybd6b79b2010-06-26 00:13:53 -07001100///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001101// State Deferral
1102///////////////////////////////////////////////////////////////////////////////
1103
Chris Craikff785832013-03-08 13:12:16 -08001104bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craik6fe991e52015-10-20 09:39:42 -07001105 const Rect& currentClip = mState.currentRenderTargetClip();
Chris Craikd6b65f62014-01-01 14:45:21 -08001106 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001107
Chris Craikff785832013-03-08 13:12:16 -08001108 if (stateDeferFlags & kStateDeferFlag_Draw) {
1109 // state has bounds initialized in local coordinates
1110 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001111 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001112 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001113 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1114 // is used, it should more closely duplicate the quickReject logic (in how it uses
1115 // snapToPixelBoundaries)
1116
Chris Craikac02eb92015-10-05 12:23:46 -07001117 clippedBounds.doIntersect(currentClip);
1118 if (clippedBounds.isEmpty()) {
Chris Craikff785832013-03-08 13:12:16 -08001119 // quick rejected
1120 return true;
1121 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001122
Chris Craika02c4ed2013-06-14 13:43:58 -07001123 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001124 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001125 int& flags = state.mClipSideFlags;
1126 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001127 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1128 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1129 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1130 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001131 }
1132 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001133 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001134 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1135 // overdraw avoidance (since we don't know what it overlaps)
1136 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001137 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001138 }
1139 }
1140
Chris Craik527a3aa2013-03-04 10:19:31 -08001141 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1142 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001143 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001144 }
1145
Chris Craik8df5ffa2015-04-28 17:47:20 -07001146 // Transform and alpha always deferred, since they are used by state operations
Chris Craik7273daa2013-03-28 11:25:24 -07001147 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craik7c85c542015-08-19 15:10:24 -07001148 state.mMatrix = *currentMatrix;
Chris Craikd6b65f62014-01-01 14:45:21 -08001149 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001150
Chris Craikfca52b752015-04-28 11:45:59 -07001151 // always store/restore, since these are just pointers
Chris Craikdeeda3d2014-05-05 19:09:33 -07001152 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikfca52b752015-04-28 11:45:59 -07001153 state.mProjectionPathMask = currentSnapshot()->projectionPathMask;
Chris Craikc3566d02013-02-04 16:16:33 -08001154 return false;
1155}
1156
Chris Craik527a3aa2013-03-04 10:19:31 -08001157void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik6daa13c2015-08-19 13:32:12 -07001158 setGlobalMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001159 writableSnapshot()->alpha = state.mAlpha;
Tom Hudson984162f2014-10-10 13:38:16 -04001160 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikfca52b752015-04-28 11:45:59 -07001161 writableSnapshot()->projectionPathMask = state.mProjectionPathMask;
Chris Craikff785832013-03-08 13:12:16 -08001162
Chris Craik527a3aa2013-03-04 10:19:31 -08001163 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001164 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001165 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001166 dirtyClip();
1167 }
Chris Craikc3566d02013-02-04 16:16:33 -08001168}
1169
Chris Craik28ce94a2013-05-31 11:38:03 -07001170/**
1171 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1172 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1173 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1174 *
1175 * This method should be called when restoreDisplayState() won't be restoring the clip
1176 */
1177void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001178 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001179 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001180 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001181 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001182 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001183 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001184 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1185 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001186}
1187
Chris Craikc3566d02013-02-04 16:16:33 -08001188///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001189// Clipping
1190///////////////////////////////////////////////////////////////////////////////
1191
Romain Guybb9524b2010-06-22 18:56:38 -07001192void OpenGLRenderer::setScissorFromClip() {
Chris Craik6fe991e52015-10-20 09:39:42 -07001193 Rect clip(mState.currentRenderTargetClip());
Romain Guye5ebcb02010-10-15 13:57:28 -07001194 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001195
Chris Craik65fe5ee2015-01-26 18:06:29 -08001196 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001197 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001198 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001199 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001200}
1201
Romain Guy8ce00302013-01-15 18:51:42 -08001202void OpenGLRenderer::ensureStencilBuffer() {
1203 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1204 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1205 // just hope we have one when hasLayer() returns false.
1206 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001207 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001208 }
1209}
1210
1211void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1212 // The layer's FBO is already bound when we reach this stage
1213 if (!layer->getStencilRenderBuffer()) {
Romain Guy8d4aeb72013-02-12 16:08:55 -08001214 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Chris Craike1450132015-04-28 17:55:50 -07001215 Stencil::getLayerStencilFormat(),
Chris Craik117bdbc2015-02-05 10:12:38 -08001216 layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001217 layer->setStencilRenderBuffer(buffer);
1218 }
1219}
1220
Rob Tsuk487a92c2015-01-06 13:22:54 -08001221static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1222 float x, float y) {
1223 Vertex v;
1224 v.x = x;
1225 v.y = y;
1226 transform.mapPoint(v.x, v.y);
1227 rectangleVertices.push_back(v);
1228}
1229
1230static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1231 Vertex v;
1232 v.x = x;
1233 v.y = y;
1234 rectangleVertices.push_back(v);
1235}
1236
1237void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
Chris Craik2bb8f562015-02-17 16:42:02 -08001238 int quadCount = rectangleList.getTransformedRectanglesCount();
1239 std::vector<Vertex> rectangleVertices(quadCount * 4);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001240 Rect scissorBox = rectangleList.calculateBounds();
1241 scissorBox.snapToPixelBoundaries();
Chris Craik2bb8f562015-02-17 16:42:02 -08001242 for (int i = 0; i < quadCount; ++i) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001243 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1244 const Matrix4& transform = tr.getTransform();
1245 Rect bounds = tr.getBounds();
1246 if (transform.rectToRect()) {
1247 transform.mapRect(bounds);
Chris Craikac02eb92015-10-05 12:23:46 -07001248 bounds.doIntersect(scissorBox);
1249 if (!bounds.isEmpty()) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001250 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1251 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1252 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1253 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1254 }
1255 } else {
1256 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1257 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1258 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1259 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1260 }
1261 }
1262
Chris Craik65fe5ee2015-01-26 18:06:29 -08001263 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001264 scissorBox.getWidth(), scissorBox.getHeight());
Chris Craik53e51e42015-06-01 10:35:35 -07001265 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001266 Glop glop;
1267 Vertex* vertices = &rectangleVertices[0];
1268 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001269 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001270 .setMeshIndexedQuads(vertices, rectangleVertices.size() / 4)
1271 .setFillBlack()
Chris Craik53e51e42015-06-01 10:35:35 -07001272 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001273 .setModelViewOffsetRect(0, 0, scissorBox)
Chris Craik82840732015-04-03 09:37:49 -07001274 .build();
1275 renderGlop(glop);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001276}
1277
Romain Guy8ce00302013-01-15 18:51:42 -08001278void OpenGLRenderer::setStencilFromClip() {
Chris Craik2507c342015-05-04 14:36:49 -07001279 if (!Properties::debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001280 if (!currentSnapshot()->clipIsSimple()) {
1281 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001282 EVENT_LOGD("setStencilFromClip - enabling");
1283
Romain Guy8ce00302013-01-15 18:51:42 -08001284 // NOTE: The order here is important, we must set dirtyClip to false
1285 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001286 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001287
1288 ensureStencilBuffer();
1289
Rob Tsuk487a92c2015-01-06 13:22:54 -08001290 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001291
Rob Tsuk487a92c2015-01-06 13:22:54 -08001292 bool isRectangleList = clipArea.isRectangleList();
1293 if (isRectangleList) {
1294 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1295 } else {
1296 incrementThreshold = 0;
1297 }
1298
Chris Craik96a5c4c2015-01-27 15:46:35 -08001299 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001300
1301 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001302 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001303 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001304 if (resetScissor) {
1305 // The scissor was not set so we now need to update it
1306 setScissorFromClip();
1307 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001308
Chris Craik96a5c4c2015-01-27 15:46:35 -08001309 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001310
1311 // stash and disable the outline clip state, since stencil doesn't account for outline
1312 bool storedSkipOutlineClip = mSkipOutlineClip;
1313 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001314
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001315 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001316 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001317 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1318
Rob Tsuk487a92c2015-01-06 13:22:54 -08001319 if (isRectangleList) {
1320 drawRectangleList(clipArea.getRectangleList());
1321 } else {
1322 // NOTE: We could use the region contour path to generate a smaller mesh
1323 // Since we are using the stencil we could use the red book path
1324 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001325
Rob Tsuk487a92c2015-01-06 13:22:54 -08001326 // The last parameter is important: we are not drawing in the color buffer
1327 // so we don't want to dirty the current layer, if any
1328 drawRegionRects(clipArea.getClipRegion(), paint, false);
1329 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001330 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001331 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001332
Chris Craik96a5c4c2015-01-27 15:46:35 -08001333 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001334
1335 // Draw the region used to generate the stencil if the appropriate debug
1336 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001337 // TODO: Implement for rectangle list clip areas
Chris Craik2507c342015-05-04 14:36:49 -07001338 if (Properties::debugStencilClip == StencilClipDebug::ShowRegion
1339 && !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001340 paint.setColor(0x7f0000ff);
1341 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001342 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001343 }
Romain Guy8ce00302013-01-15 18:51:42 -08001344 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001345 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001346 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001347 }
1348 }
1349}
1350
Chris Craikf0a59072013-11-19 18:00:46 -08001351/**
1352 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1353 *
1354 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1355 * style, and tessellated AA ramp
1356 */
1357bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001358 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001359 bool snapOut = paint && paint->isAntiAlias();
1360
1361 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1362 float outset = paint->getStrokeWidth() * 0.5f;
1363 left -= outset;
1364 top -= outset;
1365 right += outset;
1366 bottom += outset;
1367 }
1368
Chris Craikdeeda3d2014-05-05 19:09:33 -07001369 bool clipRequired = false;
1370 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001371 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001372 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001373 return true;
1374 }
1375
Chris Craikf23b25a2014-06-26 15:46:20 -07001376 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001377 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001378 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001379 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001380}
1381
Romain Guy8ce00302013-01-15 18:51:42 -08001382void OpenGLRenderer::debugClip() {
1383#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001384 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001385 SkPaint paint;
1386 paint.setColor(0x7f00ff00);
1387 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1388
Romain Guy8ce00302013-01-15 18:51:42 -08001389 }
1390#endif
1391}
1392
Chris Craik3375f8a2015-06-23 17:33:06 -07001393void OpenGLRenderer::renderGlop(const Glop& glop, GlopRenderType type) {
Chris Craik6b109c72015-02-27 10:55:28 -08001394 // TODO: It would be best if we could do this before quickRejectSetupScissor()
1395 // changes the scissor test state
Chris Craik3375f8a2015-06-23 17:33:06 -07001396 if (type != GlopRenderType::LayerClear) {
1397 // Regular draws need to clear the dirty area on the layer before they start drawing on top
1398 // of it. If this draw *is* a layer clear, it skips the clear step (since it would
1399 // infinitely recurse)
1400 clearLayerRegions();
1401 }
Chris Craik6b109c72015-02-27 10:55:28 -08001402
Chris Craik117bdbc2015-02-05 10:12:38 -08001403 if (mState.getDirtyClip()) {
1404 if (mRenderState.scissor().isEnabled()) {
1405 setScissorFromClip();
1406 }
1407
1408 setStencilFromClip();
1409 }
Chris Craik12efe642015-09-28 15:52:12 -07001410 mRenderState.render(glop, currentSnapshot()->getOrthoMatrix());
Chris Craik3375f8a2015-06-23 17:33:06 -07001411 if (type == GlopRenderType::Standard && !mRenderState.stencil().isWriteEnabled()) {
Chris Craik2ab95d72015-02-06 15:25:51 -08001412 // TODO: specify more clearly when a draw should dirty the layer.
1413 // is writing to the stencil the only time we should ignore this?
1414 dirtyLayer(glop.bounds.left, glop.bounds.top, glop.bounds.right, glop.bounds.bottom);
Chris Craik0519c8102015-02-11 13:17:06 -08001415 mDirty = true;
Chris Craik2ab95d72015-02-06 15:25:51 -08001416 }
Chris Craik117bdbc2015-02-05 10:12:38 -08001417}
1418
Romain Guyf6a11b82010-06-23 17:47:49 -07001419///////////////////////////////////////////////////////////////////////////////
1420// Drawing
1421///////////////////////////////////////////////////////////////////////////////
1422
Tom Hudson107843d2014-09-08 11:26:26 -04001423void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001424 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1425 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001426 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001427 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001428 renderNode->computeOrdering();
Chris Craik2507c342015-05-04 14:36:49 -07001429 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001430 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001431 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001432 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001433 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001434 }
1435
Chris Craik6fe991e52015-10-20 09:39:42 -07001436 DeferredDisplayList deferredList(mState.currentRenderTargetClip());
Chris Craikff785832013-03-08 13:12:16 -08001437 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001438 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001439
1440 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001441 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001442
Tom Hudson107843d2014-09-08 11:26:26 -04001443 deferredList.flush(*this, dirty);
1444 } else {
1445 // Even if there is no drawing command(Ex: invisible),
1446 // it still needs startFrame to clear buffer and start tiling.
1447 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001448 }
1449}
1450
Romain Guy03c00b52013-06-20 18:30:28 -07001451/**
1452 * Important note: this method is intended to draw batches of bitmaps and
1453 * will not set the scissor enable or dirty the current layer, if any.
1454 * The caller is responsible for properly dirtying the current layer.
1455 */
Tom Hudson107843d2014-09-08 11:26:26 -04001456void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001457 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1458 const Rect& bounds, const SkPaint* paint) {
Romain Guy55b6f952013-06-27 15:27:09 -07001459 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001460 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001461
Chris Craik527a3aa2013-03-04 10:19:31 -08001462 const AutoTexture autoCleanup(texture);
1463
Chris Craik82840732015-04-03 09:37:49 -07001464 // TODO: remove layer dirty in multi-draw callers
1465 // TODO: snap doesn't need to touch transform, only texture filter.
1466 bool snap = pureTranslate;
Chris Craika6b52192015-02-27 17:43:02 -08001467 const float x = floorf(bounds.left + 0.5f);
1468 const float y = floorf(bounds.top + 0.5f);
Chris Craik53e51e42015-06-01 10:35:35 -07001469
1470 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1471 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
1472 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001473 Glop glop;
1474 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001475 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001476 .setMeshTexturedMesh(vertices, bitmapCount * 6)
1477 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001478 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik5430ab22015-12-10 16:28:16 -08001479 .setModelViewOffsetRectOptionalSnap(snap, x, y, Rect(bounds.getWidth(), bounds.getHeight()))
Chris Craik82840732015-04-03 09:37:49 -07001480 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001481 renderGlop(glop, GlopRenderType::Multi);
Chris Craik527a3aa2013-03-04 10:19:31 -08001482}
1483
Tom Hudson107843d2014-09-08 11:26:26 -04001484void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07001485 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04001486 return;
Romain Guy6926c722010-07-12 20:20:03 -07001487 }
1488
Chris Craik44eb2c02015-01-29 09:45:09 -08001489 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07001490 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001491 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001492 const AutoTexture autoCleanup(texture);
1493
Chris Craik53e51e42015-06-01 10:35:35 -07001494 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1495 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07001496 Glop glop;
1497 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001498 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001499 .setMeshTexturedUnitQuad(texture->uvMapper)
1500 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001501 .setTransform(*currentSnapshot(), TransformFlags::None)
John Reck38e0c322015-11-10 12:19:17 -08001502 .setModelViewMapUnitToRectSnap(Rect(texture->width(), texture->height()))
Chris Craik82840732015-04-03 09:37:49 -07001503 .build();
1504 renderGlop(glop);
Romain Guyce0537b2010-06-29 21:05:21 -07001505}
1506
Tom Hudson107843d2014-09-08 11:26:26 -04001507void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08001508 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001509 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04001510 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08001511 }
1512
Romain Guyb18d2d02011-02-10 15:52:54 -08001513 float left = FLT_MAX;
1514 float top = FLT_MAX;
1515 float right = FLT_MIN;
1516 float bottom = FLT_MIN;
1517
Chris Craikef250742015-02-25 17:16:16 -08001518 const uint32_t elementCount = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001519
Chris Craikef250742015-02-25 17:16:16 -08001520 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[elementCount]);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001521 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08001522
Chris Craik51d6a3d2014-12-22 17:16:56 -08001523 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08001524 if (!colors) {
1525 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001526 tempColors.reset(new int[colorsCount]);
1527 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
1528 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08001529 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001530
Chris Craik15c3f192015-12-03 12:16:56 -08001531 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap->pixelRef());
Romain Guy3b748a42013-04-17 18:54:38 -07001532 const UvMapper& mapper(getMapper(texture));
1533
Romain Guy5a7b4662011-01-20 19:09:30 -08001534 for (int32_t y = 0; y < meshHeight; y++) {
1535 for (int32_t x = 0; x < meshWidth; x++) {
1536 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1537
1538 float u1 = float(x) / meshWidth;
1539 float u2 = float(x + 1) / meshWidth;
1540 float v1 = float(y) / meshHeight;
1541 float v2 = float(y + 1) / meshHeight;
1542
Romain Guy3b748a42013-04-17 18:54:38 -07001543 mapper.map(u1, v1, u2, v2);
1544
Romain Guy5a7b4662011-01-20 19:09:30 -08001545 int ax = i + (meshWidth + 1) * 2;
1546 int ay = ax + 1;
1547 int bx = i;
1548 int by = bx + 1;
1549 int cx = i + 2;
1550 int cy = cx + 1;
1551 int dx = i + (meshWidth + 1) * 2 + 2;
1552 int dy = dx + 1;
1553
Romain Guyff316ec2013-02-13 18:39:43 -08001554 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1555 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
1556 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08001557
Romain Guyff316ec2013-02-13 18:39:43 -08001558 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1559 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
1560 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08001561
Chris Craikdf72b632015-06-30 17:56:13 -07001562 left = std::min(left, std::min(vertices[ax], std::min(vertices[bx], vertices[cx])));
1563 top = std::min(top, std::min(vertices[ay], std::min(vertices[by], vertices[cy])));
1564 right = std::max(right, std::max(vertices[ax], std::max(vertices[bx], vertices[cx])));
1565 bottom = std::max(bottom, std::max(vertices[ay], std::max(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001566 }
1567 }
1568
Chris Craikf0a59072013-11-19 18:00:46 -08001569 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001570 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07001571 }
1572
Romain Guyff316ec2013-02-13 18:39:43 -08001573 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07001574 texture = mCaches.textureCache.get(bitmap);
1575 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04001576 return;
Romain Guy3b748a42013-04-17 18:54:38 -07001577 }
Romain Guyff316ec2013-02-13 18:39:43 -08001578 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001579 const AutoTexture autoCleanup(texture);
1580
Chris Craik82840732015-04-03 09:37:49 -07001581 /*
1582 * TODO: handle alpha_8 textures correctly by applying paint color, but *not*
1583 * shader in that case to mimic the behavior in SkiaCanvas::drawBitmapMesh.
1584 */
Chris Craik53e51e42015-06-01 10:35:35 -07001585 const int textureFillFlags = TextureFillFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07001586 Glop glop;
1587 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001588 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001589 .setMeshColoredTexturedMesh(mesh.get(), elementCount)
Chris Craik53e51e42015-06-01 10:35:35 -07001590 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1591 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001592 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07001593 .build();
1594 renderGlop(glop);
Romain Guy5a7b4662011-01-20 19:09:30 -08001595}
1596
Chris Craik14100ac2015-02-24 13:46:29 -08001597void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, Rect src, Rect dst, const SkPaint* paint) {
1598 if (quickRejectSetupScissor(dst)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001599 return;
Romain Guy6926c722010-07-12 20:20:03 -07001600 }
1601
Romain Guy3b748a42013-04-17 18:54:38 -07001602 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001603 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001604 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001605
John Reck38e0c322015-11-10 12:19:17 -08001606 Rect uv(std::max(0.0f, src.left / texture->width()),
1607 std::max(0.0f, src.top / texture->height()),
1608 std::min(1.0f, src.right / texture->width()),
1609 std::min(1.0f, src.bottom / texture->height()));
Chris Craik14100ac2015-02-24 13:46:29 -08001610
Chris Craik53e51e42015-06-01 10:35:35 -07001611 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1612 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
Chris Craik1e4c8072015-05-26 14:25:02 -07001613 const bool tryToSnap = MathUtils::areEqual(src.getWidth(), dst.getWidth())
1614 && MathUtils::areEqual(src.getHeight(), dst.getHeight());
Chris Craik82840732015-04-03 09:37:49 -07001615 Glop glop;
1616 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001617 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001618 .setMeshTexturedUvQuad(texture->uvMapper, uv)
1619 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001620 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik1e4c8072015-05-26 14:25:02 -07001621 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, dst)
Chris Craik82840732015-04-03 09:37:49 -07001622 .build();
1623 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07001624}
1625
Tom Hudson107843d2014-09-08 11:26:26 -04001626void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08001627 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
1628 const SkPaint* paint) {
Chris Craika6b52192015-02-27 17:43:02 -08001629 if (!mesh || !mesh->verticesCount || quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001630 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07001631 }
1632
Chris Craika6b52192015-02-27 17:43:02 -08001633 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
1634 if (!texture) return;
Chris Craik0556d902015-03-03 09:54:14 -08001635
Chris Craik82840732015-04-03 09:37:49 -07001636 // 9 patches are built for stretching - always filter
Chris Craik53e51e42015-06-01 10:35:35 -07001637 int textureFillFlags = TextureFillFlags::ForceFilter;
Chris Craik82840732015-04-03 09:37:49 -07001638 if (bitmap->colorType() == kAlpha_8_SkColorType) {
Chris Craik53e51e42015-06-01 10:35:35 -07001639 textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture;
Chris Craik0556d902015-03-03 09:54:14 -08001640 }
Chris Craik82840732015-04-03 09:37:49 -07001641 Glop glop;
1642 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001643 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001644 .setMeshPatchQuads(*mesh)
1645 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001646 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik5430ab22015-12-10 16:28:16 -08001647 .setModelViewOffsetRectSnap(left, top, Rect(right - left, bottom - top)) // TODO: get minimal bounds from patch
Chris Craik82840732015-04-03 09:37:49 -07001648 .build();
1649 renderGlop(glop);
Romain Guyf7f93552010-07-08 19:17:03 -07001650}
1651
Romain Guy03c00b52013-06-20 18:30:28 -07001652/**
1653 * Important note: this method is intended to draw batches of 9-patch objects and
1654 * will not set the scissor enable or dirty the current layer, if any.
1655 * The caller is responsible for properly dirtying the current layer.
1656 */
Tom Hudson107843d2014-09-08 11:26:26 -04001657void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craika6b52192015-02-27 17:43:02 -08001658 TextureVertex* vertices, uint32_t elementCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001659 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07001660 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001661 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07001662 const AutoTexture autoCleanup(texture);
1663
Chris Craik82840732015-04-03 09:37:49 -07001664 // TODO: get correct bounds from caller
Chris Craik53e51e42015-06-01 10:35:35 -07001665 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001666 // 9 patches are built for stretching - always filter
Chris Craik53e51e42015-06-01 10:35:35 -07001667 int textureFillFlags = TextureFillFlags::ForceFilter;
Chris Craik82840732015-04-03 09:37:49 -07001668 if (bitmap->colorType() == kAlpha_8_SkColorType) {
Chris Craik53e51e42015-06-01 10:35:35 -07001669 textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture;
Chris Craika6b52192015-02-27 17:43:02 -08001670 }
Chris Craik82840732015-04-03 09:37:49 -07001671 Glop glop;
1672 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001673 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001674 .setMeshTexturedIndexedQuads(vertices, elementCount)
1675 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001676 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik5430ab22015-12-10 16:28:16 -08001677 .setModelViewOffsetRect(0, 0, Rect())
Chris Craik82840732015-04-03 09:37:49 -07001678 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001679 renderGlop(glop, GlopRenderType::Multi);
Romain Guy03c00b52013-06-20 18:30:28 -07001680}
1681
Tom Hudson107843d2014-09-08 11:26:26 -04001682void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07001683 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001684 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07001685 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08001686 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04001687 return;
Chris Craik65cd6122012-12-10 17:56:27 -08001688 }
1689
Chris Craik82840732015-04-03 09:37:49 -07001690 bool shadowInterp = displayFlags & kVertexBuffer_ShadowInterp;
Chris Craik53e51e42015-06-01 10:35:35 -07001691 const int transformFlags = TransformFlags::OffsetByFudgeFactor;
Chris Craik82840732015-04-03 09:37:49 -07001692 Glop glop;
1693 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001694 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001695 .setMeshVertexBuffer(vertexBuffer, shadowInterp)
1696 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001697 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001698 .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds())
Chris Craik82840732015-04-03 09:37:49 -07001699 .build();
1700 renderGlop(glop);
Chet Haase858aa932011-05-12 09:06:00 -07001701}
1702
1703/**
Chris Craik65cd6122012-12-10 17:56:27 -08001704 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
1705 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
1706 * screen space in all directions. However, instead of using a fragment shader to compute the
1707 * translucency of the color from its position, we simply use a varying parameter to define how far
1708 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
1709 *
1710 * Doesn't yet support joins, caps, or path effects.
1711 */
Tom Hudson107843d2014-09-08 11:26:26 -04001712void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08001713 VertexBuffer vertexBuffer;
1714 // TODO: try clipping large paths to viewport
Chris Craikfca52b752015-04-28 11:45:59 -07001715
Chris Craikd6b65f62014-01-01 14:45:21 -08001716 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04001717 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08001718}
1719
1720/**
1721 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
1722 * and additional geometry for defining an alpha slope perimeter.
1723 *
1724 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
1725 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
1726 * in-shader alpha region, but found it to be taxing on some GPUs.
1727 *
1728 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
1729 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07001730 */
Tom Hudson107843d2014-09-08 11:26:26 -04001731void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001732 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07001733
Chris Craik65cd6122012-12-10 17:56:27 -08001734 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07001735
Chris Craik65cd6122012-12-10 17:56:27 -08001736 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001737 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
1738 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08001739
Chris Craik05f3d6e2014-06-02 16:27:04 -07001740 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001741 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08001742 }
1743
Chris Craikbf759452014-08-11 16:00:44 -07001744 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001745 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07001746}
1747
Tom Hudson107843d2014-09-08 11:26:26 -04001748void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001749 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001750
Chris Craik6d29c8d2013-05-08 18:35:44 -07001751 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07001752
Chris Craik6d29c8d2013-05-08 18:35:44 -07001753 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001754 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08001755
Chris Craik05f3d6e2014-06-02 16:27:04 -07001756 const Rect& bounds = buffer.getBounds();
1757 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001758 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001759 }
1760
Chris Craikbf759452014-08-11 16:00:44 -07001761 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001762 drawVertexBuffer(buffer, paint, displayFlags);
1763
1764 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07001765}
1766
Tom Hudson107843d2014-09-08 11:26:26 -04001767void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001768 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04001769 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001770
Chris Craik6fe991e52015-10-20 09:39:42 -07001771 Rect clip(mState.currentRenderTargetClip());
Romain Guyae88e5e2010-10-22 17:49:18 -07001772 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001773
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001774 SkPaint paint;
1775 paint.setColor(color);
1776 paint.setXfermodeMode(mode);
1777
1778 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07001779
Tom Hudson107843d2014-09-08 11:26:26 -04001780 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07001781}
Romain Guy9d5316e2010-06-24 19:30:36 -07001782
Chris Craik30036092015-02-12 10:41:39 -08001783void OpenGLRenderer::drawShape(float left, float top, PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08001784 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04001785 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001786 const AutoTexture autoCleanup(texture);
1787
1788 const float x = left + texture->left - texture->offset;
1789 const float y = top + texture->top - texture->offset;
1790
1791 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07001792
Tom Hudson107843d2014-09-08 11:26:26 -04001793 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08001794}
1795
Tom Hudson107843d2014-09-08 11:26:26 -04001796void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001797 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001798 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001799 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001800 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001801 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001802 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001803
Chris Craike84a2082014-12-22 14:28:49 -08001804 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001805 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001806 PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07001807 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001808 drawShape(left, top, texture, p);
1809 } else {
1810 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
1811 *currentTransform(), *p, right - left, bottom - top, rx, ry);
1812 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001813 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001814}
1815
Tom Hudson107843d2014-09-08 11:26:26 -04001816void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001817 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001818 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001819 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001820 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001821 }
Chris Craikfca52b752015-04-28 11:45:59 -07001822
Chris Craike84a2082014-12-22 14:28:49 -08001823 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001824 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001825 PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001826 drawShape(x - radius, y - radius, texture, p);
Chris Craikfca52b752015-04-28 11:45:59 -07001827 return;
Chris Craikcb4d6002012-09-25 12:00:29 -07001828 }
Chris Craikfca52b752015-04-28 11:45:59 -07001829
1830 SkPath path;
1831 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1832 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
1833 } else {
1834 path.addCircle(x, y, radius);
1835 }
1836
1837 if (CC_UNLIKELY(currentSnapshot()->projectionPathMask != nullptr)) {
1838 // mask ripples with projection mask
1839 SkPath maskPath = *(currentSnapshot()->projectionPathMask->projectionMask);
1840
1841 Matrix4 screenSpaceTransform;
1842 currentSnapshot()->buildScreenSpaceTransform(&screenSpaceTransform);
1843
1844 Matrix4 totalTransform;
1845 totalTransform.loadInverse(screenSpaceTransform);
1846 totalTransform.multiply(currentSnapshot()->projectionPathMask->projectionMaskTransform);
1847
1848 SkMatrix skTotalTransform;
1849 totalTransform.copyTo(skTotalTransform);
1850 maskPath.transform(skTotalTransform);
1851
1852 // Mask the ripple path by the projection mask, now that it's
1853 // in local space. Note that this can create CCW paths.
Tom Hudson02a26302015-06-24 11:32:42 -04001854 Op(path, maskPath, kIntersect_SkPathOp, &path);
Chris Craikfca52b752015-04-28 11:45:59 -07001855 }
1856 drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001857}
Romain Guy01d58e42011-01-19 21:54:02 -08001858
Tom Hudson107843d2014-09-08 11:26:26 -04001859void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001860 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001861 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001862 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001863 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001864 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001865 }
Romain Guy01d58e42011-01-19 21:54:02 -08001866
Chris Craike84a2082014-12-22 14:28:49 -08001867 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001868 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001869 PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001870 drawShape(left, top, texture, p);
1871 } else {
1872 SkPath path;
1873 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1874 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1875 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1876 }
1877 path.addOval(rect);
1878 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001879 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001880}
1881
Tom Hudson107843d2014-09-08 11:26:26 -04001882void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001883 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001884 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001885 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001886 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001887 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08001888 }
1889
Chris Craik780c1282012-10-04 14:10:49 -07001890 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08001891 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001892 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001893 PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07001894 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001895 drawShape(left, top, texture, p);
1896 return;
Chris Craik780c1282012-10-04 14:10:49 -07001897 }
Chris Craik780c1282012-10-04 14:10:49 -07001898 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1899 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1900 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1901 }
1902
1903 SkPath path;
1904 if (useCenter) {
1905 path.moveTo(rect.centerX(), rect.centerY());
1906 }
1907 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
1908 if (useCenter) {
1909 path.close();
1910 }
Tom Hudson107843d2014-09-08 11:26:26 -04001911 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08001912}
1913
Tom Hudson107843d2014-09-08 11:26:26 -04001914void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001915 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001916 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001917 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001918 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001919 return;
Romain Guy6926c722010-07-12 20:20:03 -07001920 }
1921
Chris Craik710f46d2012-09-17 17:25:49 -07001922 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07001923 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craik386aa032015-12-07 17:08:25 -08001924 static_assert(SkPaintDefaults_MiterLimit == 4.0f, "Miter limit has changed");
Chris Craike84a2082014-12-22 14:28:49 -08001925 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07001926 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001927 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001928 PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07001929 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001930 drawShape(left, top, texture, p);
1931 } else {
1932 SkPath path;
1933 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1934 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1935 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1936 }
1937 path.addRect(rect);
1938 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07001939 }
Chet Haase858aa932011-05-12 09:06:00 -07001940 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04001941 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
1942 SkPath path;
1943 path.addRect(left, top, right, bottom);
1944 drawConvexPath(path, p);
1945 } else {
1946 drawColorRect(left, top, right, bottom, p);
1947
1948 mDirty = true;
1949 }
Chet Haase858aa932011-05-12 09:06:00 -07001950 }
Romain Guyc7d53492010-06-25 13:41:57 -07001951}
Romain Guy9d5316e2010-06-24 19:30:36 -07001952
Chris Craikd218a922014-01-02 17:13:34 -08001953void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
Chris Craika1717272015-11-19 13:02:43 -08001954 int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001955 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001956 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07001957
Chris Craikbf6f0f22015-10-01 12:36:07 -07001958 PaintUtils::TextShadow textShadow;
1959 if (!PaintUtils::getTextShadow(paint, &textShadow)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04001960 LOG_ALWAYS_FATAL("failed to query shadow attributes");
1961 }
1962
Raph Levien416a8472012-07-19 22:48:17 -07001963 // NOTE: The drop shadow will not perform gamma correction
1964 // if shader-based correction is enabled
1965 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Chris Craik2bb8f562015-02-17 16:42:02 -08001966 ShadowTexture* texture = mCaches.dropShadowCache.get(
Chris Craika1717272015-11-19 13:02:43 -08001967 paint, text, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07001968 // If the drop shadow exceeds the max texture size or couldn't be
1969 // allocated, skip drawing
Chris Craik2bb8f562015-02-17 16:42:02 -08001970 if (!texture) return;
1971 const AutoTexture autoCleanup(texture);
Raph Levien416a8472012-07-19 22:48:17 -07001972
Chris Craik2bb8f562015-02-17 16:42:02 -08001973 const float sx = x - texture->left + textShadow.dx;
1974 const float sy = y - texture->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07001975
Chris Craik82840732015-04-03 09:37:49 -07001976 Glop glop;
1977 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001978 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001979 .setMeshTexturedUnitQuad(nullptr)
1980 .setFillShadowTexturePaint(*texture, textShadow.color, *paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001981 .setTransform(*currentSnapshot(), TransformFlags::None)
John Reck38e0c322015-11-10 12:19:17 -08001982 .setModelViewMapUnitToRect(Rect(sx, sy, sx + texture->width(), sy + texture->height()))
Chris Craik82840732015-04-03 09:37:49 -07001983 .build();
1984 renderGlop(glop);
Raph Levien416a8472012-07-19 22:48:17 -07001985}
1986
Chris Craikbf6f0f22015-10-01 12:36:07 -07001987// TODO: remove this, once mState.currentlyIgnored captures snapshot alpha
Romain Guy768bffc2013-02-27 13:50:45 -08001988bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Chris Craikbf6f0f22015-10-01 12:36:07 -07001989 float alpha = (PaintUtils::hasTextShadow(paint)
1990 ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05001991 return MathUtils::isZero(alpha)
1992 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08001993}
1994
Chris Craik59744b72014-07-01 17:56:52 -07001995bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08001996 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07001997 outMatrix->setIdentity();
1998 return false;
1999 } else if (CC_UNLIKELY(transform.isPerspective())) {
2000 outMatrix->setIdentity();
2001 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002002 }
Chris Craik59744b72014-07-01 17:56:52 -07002003
2004 /**
Chris Craika736cd92014-08-04 13:18:38 -07002005 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2006 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002007 */
2008 float sx, sy;
2009 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002010 outMatrix->setScale(
Chris Craikdf72b632015-06-30 17:56:13 -07002011 roundf(std::max(1.0f, sx)),
2012 roundf(std::max(1.0f, sy)));
Chris Craika736cd92014-08-04 13:18:38 -07002013 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002014}
2015
Tom Hudson984162f2014-10-10 13:38:16 -04002016int OpenGLRenderer::getSaveCount() const {
2017 return mState.getSaveCount();
2018}
2019
2020int OpenGLRenderer::save(int flags) {
2021 return mState.save(flags);
2022}
2023
2024void OpenGLRenderer::restore() {
Chris Craike2bb3802015-03-13 15:07:52 -07002025 mState.restore();
Tom Hudson984162f2014-10-10 13:38:16 -04002026}
2027
2028void OpenGLRenderer::restoreToCount(int saveCount) {
Chris Craike2bb3802015-03-13 15:07:52 -07002029 mState.restoreToCount(saveCount);
Tom Hudson984162f2014-10-10 13:38:16 -04002030}
2031
Tom Hudsonac7b6d32015-06-30 11:26:13 -04002032
Tom Hudson984162f2014-10-10 13:38:16 -04002033void OpenGLRenderer::translate(float dx, float dy, float dz) {
Chris Craike2bb3802015-03-13 15:07:52 -07002034 mState.translate(dx, dy, dz);
Tom Hudson984162f2014-10-10 13:38:16 -04002035}
2036
2037void OpenGLRenderer::rotate(float degrees) {
Chris Craike2bb3802015-03-13 15:07:52 -07002038 mState.rotate(degrees);
Tom Hudson984162f2014-10-10 13:38:16 -04002039}
2040
2041void OpenGLRenderer::scale(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002042 mState.scale(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002043}
2044
2045void OpenGLRenderer::skew(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002046 mState.skew(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002047}
2048
Chris Craik6daa13c2015-08-19 13:32:12 -07002049void OpenGLRenderer::setLocalMatrix(const Matrix4& matrix) {
2050 mState.setMatrix(mBaseTransform);
2051 mState.concatMatrix(matrix);
Tom Hudson984162f2014-10-10 13:38:16 -04002052}
2053
Tom Hudsonac7b6d32015-06-30 11:26:13 -04002054void OpenGLRenderer::setLocalMatrix(const SkMatrix& matrix) {
2055 mState.setMatrix(mBaseTransform);
2056 mState.concatMatrix(matrix);
2057}
2058
Tom Hudson984162f2014-10-10 13:38:16 -04002059void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2060 mState.concatMatrix(matrix);
2061}
2062
2063bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2064 return mState.clipRect(left, top, right, bottom, op);
2065}
2066
2067bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2068 return mState.clipPath(path, op);
2069}
2070
2071bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2072 return mState.clipRegion(region, op);
2073}
2074
2075void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2076 mState.setClippingOutline(allocator, outline);
2077}
2078
2079void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2080 const Rect& rect, float radius, bool highPriority) {
2081 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2082}
2083
Chris Craikfca52b752015-04-28 11:45:59 -07002084void OpenGLRenderer::setProjectionPathMask(LinearAllocator& allocator, const SkPath* path) {
2085 mState.setProjectionPathMask(allocator, path);
2086}
2087
Tom Hudson107843d2014-09-08 11:26:26 -04002088void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002089 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002090 DrawOpMode drawOpMode) {
2091
Chris Craik03ae2722015-02-25 11:01:09 -08002092 if (drawOpMode == DrawOpMode::kImmediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002093 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2094 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002095 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002096 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002097 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002098 }
Romain Guycac5fd32011-12-01 20:08:50 -08002099 }
2100
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002101 const float oldX = x;
2102 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002103
Chris Craikd6b65f62014-01-01 14:45:21 -08002104 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002105 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002106
Romain Guy211370f2012-02-01 16:10:55 -08002107 if (CC_LIKELY(pureTranslate)) {
Chris Craika6b52192015-02-27 17:43:02 -08002108 x = floorf(x + transform.getTranslateX() + 0.5f);
2109 y = floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002110 }
2111
Chris Craikbf6f0f22015-10-01 12:36:07 -07002112 int alpha = PaintUtils::getAlphaDirect(paint) * currentSnapshot()->alpha;
2113 SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(paint);
Romain Guy9d13fe252010-10-15 16:06:03 -07002114
Chris Craikc08820f2015-09-22 14:22:29 -07002115 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
Romain Guyc74f45a2013-02-26 19:10:14 -08002116
Chris Craikbf6f0f22015-10-01 12:36:07 -07002117 if (CC_UNLIKELY(PaintUtils::hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002118 fontRenderer.setFont(paint, SkMatrix::I());
Chris Craika1717272015-11-19 13:02:43 -08002119 drawTextShadow(paint, text, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002120 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002121 }
2122
Romain Guy19d4dd82013-03-04 11:14:26 -08002123 const bool hasActiveLayer = hasLayer();
2124
Romain Guy624234f2013-03-05 16:43:31 -08002125 // We only pass a partial transform to the font renderer. That partial
2126 // matrix defines how glyphs are rasterized. Typically we want glyphs
2127 // to be rasterized at their final size on screen, which means the partial
2128 // matrix needs to take the scale factor into account.
2129 // When a partial matrix is used to transform glyphs during rasterization,
2130 // the mesh is generated with the inverse transform (in the case of scale,
2131 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2132 // apply the full transform matrix at draw time in the vertex shader.
2133 // Applying the full matrix in the shader is the easiest way to handle
2134 // rotation and perspective and allows us to always generated quads in the
2135 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002136 SkMatrix fontTransform;
2137 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2138 || fabs(y - (int) y) > 0.0f
2139 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002140 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002141 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002142
Romain Guy624234f2013-03-05 16:43:31 -08002143 // TODO: Implement better clipping for scaled/rotated text
Chris Craik6fe991e52015-10-20 09:39:42 -07002144 const Rect* clip = !pureTranslate ? nullptr : &mState.currentRenderTargetClip();
Chris Craik41541822013-05-03 16:35:54 -07002145 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 -07002146
Raph Levien996e57c2012-07-23 15:22:52 -07002147 bool status;
Chris Craika1717272015-11-19 13:02:43 -08002148#if HWUI_NEW_OPS
2149 LOG_ALWAYS_FATAL("unsupported");
Chris Craik15c3f192015-12-03 12:16:56 -08002150 TextDrawFunctor functor(nullptr, nullptr, nullptr, x, y, pureTranslate, alpha, mode, paint);
Chris Craika1717272015-11-19 13:02:43 -08002151#else
Chris Craik82840732015-04-03 09:37:49 -07002152 TextDrawFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craika1717272015-11-19 13:02:43 -08002153#endif
Chris Craik527a3aa2013-03-04 10:19:31 -08002154
2155 // don't call issuedrawcommand, do it at end of batch
Chris Craik03ae2722015-02-25 11:01:09 -08002156 bool forceFinish = (drawOpMode != DrawOpMode::kDefer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002157 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002158 SkPaint paintCopy(*paint);
2159 paintCopy.setTextAlign(SkPaint::kLeft_Align);
Chris Craika1717272015-11-19 13:02:43 -08002160 status = fontRenderer.renderPosText(&paintCopy, clip, text, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002161 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002162 } else {
Chris Craika1717272015-11-19 13:02:43 -08002163 status = fontRenderer.renderPosText(paint, clip, text, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002164 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002165 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002166
Chris Craik03ae2722015-02-25 11:01:09 -08002167 if ((status || drawOpMode != DrawOpMode::kImmediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002168 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002169 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002170 }
Chris Craik41541822013-05-03 16:35:54 -07002171 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002172 }
Romain Guy694b5192010-07-21 21:33:20 -07002173
Tom Hudson107843d2014-09-08 11:26:26 -04002174 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002175}
2176
Tom Hudson107843d2014-09-08 11:26:26 -04002177void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002178 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002179 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002180 return;
Romain Guy03d58522012-02-24 17:54:07 -08002181 }
2182
Chris Craik39a908c2013-06-13 14:39:01 -07002183 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002184 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002185
Chris Craikc08820f2015-09-22 14:22:29 -07002186 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
Chris Craik59744b72014-07-01 17:56:52 -07002187 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002188 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002189
Chris Craikbf6f0f22015-10-01 12:36:07 -07002190 int alpha = PaintUtils::getAlphaDirect(paint) * currentSnapshot()->alpha;
2191 SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(paint);
Chris Craika1717272015-11-19 13:02:43 -08002192#if HWUI_NEW_OPS
2193 LOG_ALWAYS_FATAL("unsupported");
Chris Craik15c3f192015-12-03 12:16:56 -08002194 TextDrawFunctor functor(nullptr, nullptr, nullptr, 0.0f, 0.0f, false, alpha, mode, paint);
Chris Craika1717272015-11-19 13:02:43 -08002195#else
Chris Craik82840732015-04-03 09:37:49 -07002196 TextDrawFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Chris Craika1717272015-11-19 13:02:43 -08002197#endif
Romain Guy03d58522012-02-24 17:54:07 -08002198
Tom Hudson984162f2014-10-10 13:38:16 -04002199 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002200 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 -08002201
Chris Craika1717272015-11-19 13:02:43 -08002202 if (fontRenderer.renderTextOnPath(paint, clip, text, count, path,
Chih-Hung Hsieh9ad6ac02015-02-27 16:11:36 -08002203 hOffset, vOffset, hasLayer() ? &bounds : nullptr, &functor)) {
Chris Craik083e7332015-02-27 17:04:20 -08002204 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2205 mDirty = true;
Romain Guy97771732012-02-28 18:17:02 -08002206 }
Romain Guy325740f2012-02-24 16:48:34 -08002207}
2208
Tom Hudson107843d2014-09-08 11:26:26 -04002209void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002210 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002211
Chris Craik44eb2c02015-01-29 09:45:09 -08002212 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002213
Chris Craik30036092015-02-12 10:41:39 -08002214 PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002215 if (!texture) return;
Romain Guy7fbcc042010-08-04 15:40:07 -07002216
Romain Guy8b55f372010-08-18 17:10:07 -07002217 const float x = texture->left - texture->offset;
2218 const float y = texture->top - texture->offset;
2219
Romain Guy01d58e42011-01-19 21:54:02 -08002220 drawPathTexture(texture, x, y, paint);
Digish Pandya2e4f67c2015-11-04 11:00:28 +05302221
2222 if (texture->cleanup) {
2223 mCaches.pathCache.remove(path, paint);
2224 }
Tom Hudson107843d2014-09-08 11:26:26 -04002225 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002226}
2227
Chris Craik3aadd602015-08-20 12:41:40 -07002228void OpenGLRenderer::drawLayer(Layer* layer) {
Romain Guy35643dd2012-09-18 15:40:58 -07002229 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002230 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002231 }
2232
Chris Craike84a2082014-12-22 14:28:49 -08002233 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002234 if (layer->isTextureLayer()) {
2235 transform = &layer->getTransform();
2236 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002237 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002238 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002239 }
2240 }
2241
Chris Craik39a908c2013-06-13 14:39:01 -07002242 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08002243 const bool rejected = mState.calculateQuickRejectForScissor(
Chris Craik3aadd602015-08-20 12:41:40 -07002244 0, 0, layer->layer.getWidth(), layer->layer.getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -08002245 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002246
2247 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002248 if (transform && !transform->isIdentity()) {
2249 restore();
2250 }
Tom Hudson107843d2014-09-08 11:26:26 -04002251 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002252 }
2253
Chris Craik62d307c2014-07-29 10:35:13 -07002254 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2255 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2256
Romain Guy5bb3c732012-11-29 17:52:58 -08002257 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002258
Chris Craik65fe5ee2015-01-26 18:06:29 -08002259 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08002260 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002261
Romain Guy211370f2012-02-01 16:10:55 -08002262 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002263 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002264 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2265 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002266 } else if (layer->mesh) {
Chris Craik82840732015-04-03 09:37:49 -07002267 Glop glop;
2268 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002269 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002270 .setMeshTexturedIndexedQuads(layer->mesh, layer->meshElementCount)
2271 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
Chris Craik53e51e42015-06-01 10:35:35 -07002272 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik5430ab22015-12-10 16:28:16 -08002273 .setModelViewOffsetRectSnap(0, 0, Rect(layer->layer.getWidth(), layer->layer.getHeight()))
Chris Craik82840732015-04-03 09:37:49 -07002274 .build();
2275 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy3a3133d2011-02-01 22:59:58 -08002276#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07002277 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08002278#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002279 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002280
Romain Guy5bb3c732012-11-29 17:52:58 -08002281 if (layer->debugDrawUpdate) {
2282 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002283
2284 SkPaint paint;
2285 paint.setColor(0x7f00ff00);
Chris Craik3aadd602015-08-20 12:41:40 -07002286 drawColorRect(0, 0, layer->layer.getWidth(), layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002287 }
Romain Guyf219da52011-01-16 12:54:25 -08002288 }
Chris Craik34416ea2013-04-15 16:08:28 -07002289 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07002290
Romain Guyb2e2f242012-10-17 18:18:35 -07002291 if (transform && !transform->isIdentity()) {
2292 restore();
2293 }
2294
Tom Hudson107843d2014-09-08 11:26:26 -04002295 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08002296}
2297
Romain Guy6926c722010-07-12 20:20:03 -07002298///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002299// Draw filters
2300///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04002301void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
2302 // We should never get here since we apply the draw filter when stashing
2303 // the paints in the DisplayList.
2304 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08002305}
2306
2307///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002308// Drawing implementation
2309///////////////////////////////////////////////////////////////////////////////
2310
Chris Craikd218a922014-01-02 17:13:34 -08002311Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
Chris Craik15c3f192015-12-03 12:16:56 -08002312 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap->pixelRef());
Romain Guy3b748a42013-04-17 18:54:38 -07002313 if (!texture) {
2314 return mCaches.textureCache.get(bitmap);
2315 }
2316 return texture;
2317}
2318
Chris Craik2bb8f562015-02-17 16:42:02 -08002319void OpenGLRenderer::drawPathTexture(PathTexture* texture, float x, float y,
2320 const SkPaint* paint) {
John Reck38e0c322015-11-10 12:19:17 -08002321 if (quickRejectSetupScissor(x, y, x + texture->width(), y + texture->height())) {
Romain Guy01d58e42011-01-19 21:54:02 -08002322 return;
2323 }
2324
Chris Craik82840732015-04-03 09:37:49 -07002325 Glop glop;
2326 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002327 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002328 .setMeshTexturedUnitQuad(nullptr)
2329 .setFillPathTexturePaint(*texture, *paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002330 .setTransform(*currentSnapshot(), TransformFlags::None)
John Reck38e0c322015-11-10 12:19:17 -08002331 .setModelViewMapUnitToRect(Rect(x, y, x + texture->width(), y + texture->height()))
Chris Craik82840732015-04-03 09:37:49 -07002332 .build();
2333 renderGlop(glop);
Romain Guy01d58e42011-01-19 21:54:02 -08002334}
2335
Tom Hudson107843d2014-09-08 11:26:26 -04002336void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002337 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002338 return;
Romain Guy672433d2013-01-04 19:05:13 -08002339 }
2340
Tom Hudson107843d2014-09-08 11:26:26 -04002341 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08002342}
2343
Tom Hudson107843d2014-09-08 11:26:26 -04002344void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07002345 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04002346 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07002347
Chris Craik15a07a22014-01-26 13:43:53 -08002348 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08002349 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07002350
2351 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08002352 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07002353
ztenghui14a4e352014-08-13 10:44:39 -07002354 // The caller has made sure casterAlpha > 0.
2355 float ambientShadowAlpha = mAmbientShadowAlpha;
Chris Craik2507c342015-05-04 14:36:49 -07002356 if (CC_UNLIKELY(Properties::overrideAmbientShadowStrength >= 0)) {
2357 ambientShadowAlpha = Properties::overrideAmbientShadowStrength;
ztenghui14a4e352014-08-13 10:44:39 -07002358 }
2359 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
2360 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002361 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002362 }
ztenghui7b4516e2014-01-07 10:42:55 -08002363
ztenghui14a4e352014-08-13 10:44:39 -07002364 float spotShadowAlpha = mSpotShadowAlpha;
Chris Craik2507c342015-05-04 14:36:49 -07002365 if (CC_UNLIKELY(Properties::overrideSpotShadowStrength >= 0)) {
2366 spotShadowAlpha = Properties::overrideSpotShadowStrength;
ztenghui14a4e352014-08-13 10:44:39 -07002367 }
2368 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
2369 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002370 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002371 }
ztenghui7b4516e2014-01-07 10:42:55 -08002372
Tom Hudson107843d2014-09-08 11:26:26 -04002373 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07002374}
2375
Tom Hudson107843d2014-09-08 11:26:26 -04002376void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002377 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08002378 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04002379 return;
Romain Guy3b753822013-03-05 10:27:35 -08002380 }
Romain Guy735738c2012-12-03 12:34:51 -08002381
Romain Guy672433d2013-01-04 19:05:13 -08002382 float left = FLT_MAX;
2383 float top = FLT_MAX;
2384 float right = FLT_MIN;
2385 float bottom = FLT_MIN;
2386
Romain Guy448455f2013-07-22 13:57:50 -07002387 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08002388 Vertex* vertex = mesh;
2389
Chris Craik2af46352012-11-26 18:30:17 -08002390 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08002391 float l = rects[index + 0];
2392 float t = rects[index + 1];
2393 float r = rects[index + 2];
2394 float b = rects[index + 3];
2395
Romain Guy3b753822013-03-05 10:27:35 -08002396 Vertex::set(vertex++, l, t);
2397 Vertex::set(vertex++, r, t);
2398 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08002399 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08002400
Chris Craikdf72b632015-06-30 17:56:13 -07002401 left = std::min(left, l);
2402 top = std::min(top, t);
2403 right = std::max(right, r);
2404 bottom = std::max(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08002405 }
2406
Chris Craikf0a59072013-11-19 18:00:46 -08002407 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002408 return;
Romain Guya362c692013-02-04 13:50:16 -08002409 }
Romain Guy672433d2013-01-04 19:05:13 -08002410
Chris Craik53e51e42015-06-01 10:35:35 -07002411 const int transformFlags = ignoreTransform
2412 ? TransformFlags::MeshIgnoresCanvasTransform : TransformFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07002413 Glop glop;
2414 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002415 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002416 .setMeshIndexedQuads(&mesh[0], count / 4)
2417 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002418 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07002419 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07002420 .build();
2421 renderGlop(glop);
Romain Guy672433d2013-01-04 19:05:13 -08002422}
2423
Romain Guy026c5e162010-06-28 17:12:22 -07002424void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002425 const SkPaint* paint, bool ignoreTransform) {
Chris Craik53e51e42015-06-01 10:35:35 -07002426 const int transformFlags = ignoreTransform
2427 ? TransformFlags::MeshIgnoresCanvasTransform : TransformFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07002428 Glop glop;
2429 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002430 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002431 .setMeshUnitQuad()
2432 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002433 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07002434 .setModelViewMapUnitToRect(Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07002435 .build();
2436 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07002437}
2438
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002439float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik8df5ffa2015-04-28 17:47:20 -07002440 return (layer->getAlpha() / 255.0f) * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07002441}
2442
Romain Guy9d5316e2010-06-24 19:30:36 -07002443}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002444}; // namespace android