blob: 9d821f38fab6e49979c3cda7b4f7666f767c65c5 [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"
sergeyvdccca442016-03-21 15:38:21 -070034#include "hwui/Canvas.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080035#include "utils/GLUtils.h"
36#include "utils/PaintUtils.h"
37#include "utils/TraceUtils.h"
38
Romain Guye4d01122010-06-16 18:44:05 -070039#include <stdlib.h>
40#include <stdint.h>
41#include <sys/types.h>
42
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 Craik797b95b2014-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()) {
John Reck975591a2016-01-22 16:28:07 -0800198 GL_CHECKPOINT(MODERATE);
Romain Guy11cb6422012-09-21 00:39:43 -0700199
Romain Guyc15008e2010-11-10 11:59:15 -0800200#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800201 mCaches.dumpMemoryUsage();
John Reck38e0c322015-11-10 12:19:17 -0800202 GPUMemoryTracker::dump();
Romain Guy11cb6422012-09-21 00:39:43 -0700203#else
Chris Craik2507c342015-05-04 14:36:49 -0700204 if (Properties::debugLevel & kDebugMemory) {
Romain Guy11cb6422012-09-21 00:39:43 -0700205 mCaches.dumpMemoryUsage();
206 }
Romain Guyc15008e2010-11-10 11:59:15 -0800207#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700208 }
Romain Guy96885eb2013-03-26 15:05:58 -0700209
210 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400211
212 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700213}
214
Romain Guy35643dd2012-09-18 15:40:58 -0700215void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700216 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
217 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700218 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700219
Chris Craik65fe5ee2015-01-26 18:06:29 -0800220 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700221 dirtyClip();
222}
223
Andreas Gampe64bb4132014-11-22 00:35:09 +0000224void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400225 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700226
Chris Craik6fe991e52015-10-20 09:39:42 -0700227 Rect clip(mState.currentRenderTargetClip());
Romain Guy80911b82011-03-16 15:30:12 -0700228 clip.snapToPixelBoundaries();
229
Romain Guyd643bb52011-03-01 14:55:21 -0800230 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800231 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800232 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800233 dirtyLayerUnchecked(clip, getRegion());
234 }
Romain Guyd643bb52011-03-01 14:55:21 -0800235
Romain Guy08aa2cb2011-03-17 11:06:57 -0700236 DrawGlInfo info;
237 info.clipLeft = clip.left;
238 info.clipTop = clip.top;
239 info.clipRight = clip.right;
240 info.clipBottom = clip.bottom;
241 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700242 info.width = getViewportWidth();
243 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800244 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700245
Tom Hudson984162f2014-10-10 13:38:16 -0400246 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700247 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400248 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700249 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
250 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800251 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700252 setScissorFromClip();
253 }
Chris Craik54f574a2013-08-26 11:23:46 -0700254
John Reck3b202512014-06-23 13:13:08 -0700255 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
256 // Scissor may have been modified, reset dirty clip
257 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800258
Tom Hudson107843d2014-09-08 11:26:26 -0400259 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800260}
261
Romain Guyf6a11b82010-06-23 17:47:49 -0700262///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700263// Debug
264///////////////////////////////////////////////////////////////////////////////
265
Chris Craik62d307c2014-07-29 10:35:13 -0700266void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
267#if DEBUG_DETAILED_EVENTS
268 const int BUFFER_SIZE = 256;
269 va_list ap;
270 char buf[BUFFER_SIZE];
271
272 va_start(ap, fmt);
273 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
274 va_end(ap);
275
276 eventMark(buf);
277#endif
278}
279
280
Romain Guy0f667532013-03-01 14:31:04 -0800281void OpenGLRenderer::eventMark(const char* name) const {
282 mCaches.eventMark(0, name);
283}
284
Romain Guy87e2f7572012-09-24 11:37:12 -0700285void OpenGLRenderer::startMark(const char* name) const {
286 mCaches.startMark(0, name);
287}
288
289void OpenGLRenderer::endMark() const {
290 mCaches.endMark();
291}
292
293void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700294 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700295}
296
297void OpenGLRenderer::renderOverdraw() {
Chris Craik2507c342015-05-04 14:36:49 -0700298 if (Properties::debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700299 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700300
Chris Craik65fe5ee2015-01-26 18:06:29 -0800301 mRenderState.scissor().setEnabled(true);
302 mRenderState.scissor().set(clip->left,
303 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
304 clip->right - clip->left,
305 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700306
Romain Guy627c6fd2013-08-21 11:53:18 -0700307 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800308 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700309 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
310
311 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800312 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700313 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
314
315 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800316 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700317 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
318
319 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800320 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700321 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
322
Chris Craik96a5c4c2015-01-27 15:46:35 -0800323 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700324 }
325}
326
327///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700328// Layers
329///////////////////////////////////////////////////////////////////////////////
330
331bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700332 if (layer->deferredUpdateScheduled && layer->renderer
333 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700334
Romain Guy7c450aa2012-09-21 19:15:00 -0700335 if (inFrame) {
Romain Guy7c450aa2012-09-21 19:15:00 -0700336 debugOverdraw(false, false);
337 }
Romain Guy11cb6422012-09-21 00:39:43 -0700338
Chris Craik2507c342015-05-04 14:36:49 -0700339 if (CC_UNLIKELY(inFrame || Properties::drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700340 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700341 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700342 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700343 }
Romain Guy11cb6422012-09-21 00:39:43 -0700344
345 if (inFrame) {
346 resumeAfterLayer();
Romain Guy11cb6422012-09-21 00:39:43 -0700347 }
348
Chris Craik2507c342015-05-04 14:36:49 -0700349 layer->debugDrawUpdate = Properties::debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700350 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700351
352 return true;
353 }
354
355 return false;
356}
357
358void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700359 // If draw deferring is enabled this method will simply defer
360 // the display list of each individual layer. The layers remain
361 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700362 int count = mLayerUpdates.size();
363 if (count > 0) {
Chris Craik2507c342015-05-04 14:36:49 -0700364 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -0700365 startMark("Layer Updates");
366 } else {
367 startMark("Defer Layer Updates");
368 }
Romain Guy11cb6422012-09-21 00:39:43 -0700369
Chris Craik1206b9b2013-04-04 14:46:24 -0700370 // Note: it is very important to update the layers in order
371 for (int i = 0; i < count; i++) {
John Reck272a6852015-07-29 16:48:58 -0700372 Layer* layer = mLayerUpdates[i].get();
Romain Guy11cb6422012-09-21 00:39:43 -0700373 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700374 }
Romain Guy11cb6422012-09-21 00:39:43 -0700375
Chris Craik2507c342015-05-04 14:36:49 -0700376 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -0700377 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800378 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700379 }
380 endMark();
381 }
382}
383
384void OpenGLRenderer::flushLayers() {
385 int count = mLayerUpdates.size();
386 if (count > 0) {
387 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700388
Chris Craik1206b9b2013-04-04 14:46:24 -0700389 // Note: it is very important to update the layers in order
390 for (int i = 0; i < count; i++) {
John Reck272a6852015-07-29 16:48:58 -0700391 mLayerUpdates[i]->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700392 }
393
394 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800395 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700396
Romain Guy11cb6422012-09-21 00:39:43 -0700397 endMark();
398 }
399}
400
401void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
402 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700403 // Make sure we don't introduce duplicates.
404 // SortedVector would do this automatically but we need to respect
405 // the insertion order. The linear search is not an issue since
406 // this list is usually very short (typically one item, at most a few)
407 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
John Reck272a6852015-07-29 16:48:58 -0700408 if (mLayerUpdates[i] == layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700409 return;
410 }
411 }
Romain Guy11cb6422012-09-21 00:39:43 -0700412 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700413 }
414}
415
Romain Guye93482f2013-06-17 13:14:51 -0700416void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
417 if (layer) {
418 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
John Reck272a6852015-07-29 16:48:58 -0700419 if (mLayerUpdates[i] == layer) {
420 mLayerUpdates.erase(mLayerUpdates.begin() + i);
Romain Guye93482f2013-06-17 13:14:51 -0700421 break;
422 }
423 }
424 }
425}
426
Romain Guy40543602013-06-12 15:31:28 -0700427void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800428 ATRACE_NAME("Update HW Layers");
Chris Craik44eb2c02015-01-29 09:45:09 -0800429 mRenderState.blend().syncEnabled();
Romain Guy40543602013-06-12 15:31:28 -0700430 updateLayers();
431 flushLayers();
432 // Wait for all the layer updates to be executed
John Reck55156372015-01-21 07:46:37 -0800433 glFinish();
Romain Guy40543602013-06-12 15:31:28 -0700434}
435
John Reck443a7142014-09-04 17:40:05 -0700436void OpenGLRenderer::markLayersAsBuildLayers() {
437 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
438 mLayerUpdates[i]->wasBuildLayered = true;
439 }
440}
441
Romain Guy11cb6422012-09-21 00:39:43 -0700442///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700443// State management
444///////////////////////////////////////////////////////////////////////////////
445
Chris Craik14e51302013-12-30 15:32:54 -0800446void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700447 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800448 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
449 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700450
Chris Craika64a2be2014-05-14 14:17:01 -0700451 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700452 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700453 }
454
Romain Guy2542d192010-08-18 11:47:12 -0700455 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700456 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700457 }
Romain Guy2542d192010-08-18 11:47:12 -0700458
Romain Guy5ec99242010-11-03 16:19:08 -0700459 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700460 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700461 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700462 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800463 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700464 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700465 }
Romain Guybb9524b2010-06-22 18:56:38 -0700466}
467
Romain Guyf6a11b82010-06-23 17:47:49 -0700468///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700469// Layers
470///////////////////////////////////////////////////////////////////////////////
471
472int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700473 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700474 // force matrix/clip isolation for layer
Florin Malitaeecff562015-12-21 10:43:01 -0500475 flags |= SaveFlags::MatrixClip;
Chris Craik4ace7302014-09-14 15:49:54 -0700476
Tom Hudson984162f2014-10-10 13:38:16 -0400477 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700478
Tom Hudson984162f2014-10-10 13:38:16 -0400479 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700480 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700481 }
Romain Guyd55a8612010-06-28 17:42:46 -0700482
483 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700484}
485
Chris Craikd90144d2013-03-19 15:03:48 -0700486void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
487 const Rect untransformedBounds(bounds);
488
Chris Craikd6b65f62014-01-01 14:45:21 -0800489 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700490
491 // Layers only make sense if they are in the framebuffer's bounds
Chris Craik6fe991e52015-10-20 09:39:42 -0700492 bounds.doIntersect(mState.currentRenderTargetClip());
Chris Craikac02eb92015-10-05 12:23:46 -0700493 if (!bounds.isEmpty()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700494 // We cannot work with sub-pixels in this case
495 bounds.snapToPixelBoundaries();
496
497 // When the layer is not an FBO, we may use glCopyTexImage so we
498 // need to make sure the layer does not extend outside the bounds
499 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700500 const Snapshot& previous = *(currentSnapshot()->previous);
501 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
Chris Craikac02eb92015-10-05 12:23:46 -0700502
503 bounds.doIntersect(previousViewport);
504 if (!bounds.isEmpty() && fboLayer) {
Chris Craikd90144d2013-03-19 15:03:48 -0700505 clip.set(bounds);
506 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800507 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700508 inverse.mapRect(clip);
509 clip.snapToPixelBoundaries();
Chris Craikac02eb92015-10-05 12:23:46 -0700510 clip.doIntersect(untransformedBounds);
511 if (!clip.isEmpty()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700512 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
513 bounds.set(untransformedBounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700514 }
515 }
Chris Craikd90144d2013-03-19 15:03:48 -0700516 }
517}
518
Chris Craik408eb122013-03-26 18:55:15 -0700519void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
520 bool fboLayer, int alpha) {
521 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
522 bounds.getHeight() > mCaches.maxTextureSize ||
523 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400524 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700525 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400526 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700527 }
528}
529
Chris Craikd90144d2013-03-19 15:03:48 -0700530int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500531 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400532 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700533
Florin Malitaeecff562015-12-21 10:43:01 -0500534 if (!mState.currentlyIgnored() && (flags & SaveFlags::ClipToLayer)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700535 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
536 // operations will be able to store and restore the current clip and transform info, and
537 // quick rejection will be correct (for display lists)
538
539 Rect bounds(left, top, right, bottom);
540 Rect clip;
541 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craikbf6f0f22015-10-01 12:36:07 -0700542 updateSnapshotIgnoreForLayer(bounds, clip, true, PaintUtils::getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700543
Tom Hudson984162f2014-10-10 13:38:16 -0400544 if (!mState.currentlyIgnored()) {
545 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
546 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
547 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800548 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700549 }
550 }
551
552 return count;
553}
554
Romain Guy1c740bc2010-09-13 18:00:09 -0700555/**
556 * Layers are viewed by Skia are slightly different than layers in image editing
557 * programs (for instance.) When a layer is created, previously created layers
558 * and the frame buffer still receive every drawing command. For instance, if a
559 * layer is created and a shape intersecting the bounds of the layers and the
560 * framebuffer is draw, the shape will be drawn on both (unless the layer was
Florin Malitaeecff562015-12-21 10:43:01 -0500561 * created with the SaveFlags::ClipToLayer flag.)
Romain Guy1c740bc2010-09-13 18:00:09 -0700562 *
563 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
564 * texture. Unfortunately, this is inefficient as it requires every primitive to
565 * be drawn n + 1 times, where n is the number of active layers. In practice this
566 * means, for every primitive:
567 * - Switch active frame buffer
568 * - Change viewport, clip and projection matrix
569 * - Issue the drawing
570 *
571 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700572 * To avoid this, layers are implemented in a different way here, at least in the
573 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
574 * is set. When this flag is set we can redirect all drawing operations into a
575 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700576 *
577 * This implementation relies on the frame buffer being at least RGBA 8888. When
578 * a layer is created, only a texture is created, not an FBO. The content of the
579 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700580 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700581 * buffer and drawing continues as normal. This technique therefore treats the
582 * frame buffer as a scratch buffer for the layers.
583 *
584 * To compose the layers back onto the frame buffer, each layer texture
585 * (containing the original frame buffer data) is drawn as a simple quad over
586 * the frame buffer. The trick is that the quad is set as the composition
587 * destination in the blending equation, and the frame buffer becomes the source
588 * of the composition.
589 *
590 * Drawing layers with an alpha value requires an extra step before composition.
591 * An empty quad is drawn over the layer's region in the frame buffer. This quad
592 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
593 * quad is used to multiply the colors in the frame buffer. This is achieved by
594 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
595 * GL_ZERO, GL_SRC_ALPHA.
596 *
597 * Because glCopyTexImage2D() can be slow, an alternative implementation might
598 * be use to draw a single clipped layer. The implementation described above
599 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700600 *
601 * (1) The frame buffer is actually not cleared right away. To allow the GPU
602 * to potentially optimize series of calls to glCopyTexImage2D, the frame
603 * buffer is left untouched until the first drawing operation. Only when
604 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700605 */
Chet Haased48885a2012-08-28 17:43:28 -0700606bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700607 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800608 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
609 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700610
Florin Malitaeecff562015-12-21 10:43:01 -0500611 const bool fboLayer = flags & SaveFlags::ClipToLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700612
Romain Guyf607bdc2010-09-10 19:20:06 -0700613 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700614 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700615 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700616 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craikbf6f0f22015-10-01 12:36:07 -0700617 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, PaintUtils::getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700618
619 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400620 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700621 return false;
622 }
Romain Guyf18fd992010-07-08 11:45:51 -0700623
Chris Craik44eb2c02015-01-29 09:45:09 -0800624 mCaches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700625 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700626 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700627 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700628 }
629
Derek Sollenberger674554f2014-02-19 16:47:32 +0000630 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700631 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700632 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
633 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500634
Chet Haasea23eed82012-04-12 15:19:04 -0700635 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700636 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700637 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda57022010-07-06 11:39:32 -0700638
Romain Guy8fb95422010-08-17 18:38:51 -0700639 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400640 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
641 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700642
Chris Craika8bea8e2014-09-24 11:29:43 -0700643 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
644 fboLayer ? "" : "unclipped ",
645 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700646 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700647 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700648 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700649 } else {
650 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700651 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800652 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700653 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700654 // Workaround for some GL drivers. When reading pixels lying outside
655 // of the window we should get undefined values for those pixels.
656 // Unfortunately some drivers will turn the entire target texture black
657 // when reading outside of the window.
658 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800659 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700660 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800661 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700662
Chris Craika64a2be2014-05-14 14:17:01 -0700663 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
664 bounds.left, getViewportHeight() - bounds.bottom,
665 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700666
Romain Guy54be1cd2011-06-13 19:04:27 -0700667 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800668 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700669 }
Romain Guyeb993562010-10-05 18:14:38 -0700670 }
Romain Guyf86ef572010-07-01 11:05:42 -0700671
Romain Guyd55a8612010-06-28 17:42:46 -0700672 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700673}
674
Chris Craike63f7c622013-10-17 10:30:55 -0700675bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800676 layer->clipRect.set(clip);
John Reck0b8d0672016-01-29 14:18:22 -0800677 layer->setFbo(mRenderState.createFramebuffer());
Romain Guy5b3b3522010-10-27 18:57:51 -0700678
Tom Hudson984162f2014-10-10 13:38:16 -0400679 writableSnapshot()->region = &writableSnapshot()->layer->region;
680 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
681 writableSnapshot()->fbo = layer->getFbo();
682 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
683 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
684 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800685 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700686
Romain Guy7c450aa2012-09-21 19:15:00 -0700687 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700688 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700689 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700690 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700691
692 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700693 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700694 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700695 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700696 }
697
698 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Chris Craikf27133d2015-02-19 09:51:53 -0800699 layer->getTextureId(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700700
Romain Guy5b3b3522010-10-27 18:57:51 -0700701 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800702 mRenderState.scissor().setEnabled(true);
703 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700704 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700705 glClear(GL_COLOR_BUFFER_BIT);
706
707 dirtyClip();
708
709 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700710 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700711 return true;
712}
713
Romain Guy1c740bc2010-09-13 18:00:09 -0700714/**
715 * Read the documentation of createLayer() before doing anything in this method.
716 */
Chris Craik14e51302013-12-30 15:32:54 -0800717void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
718 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000719 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700720 return;
721 }
722
Chris Craik14e51302013-12-30 15:32:54 -0800723 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800724 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800725 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700726
Chris Craik39a908c2013-06-13 14:39:01 -0700727 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400728 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800729 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800730 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700731
Romain Guyeb993562010-10-05 18:14:38 -0700732 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700733 // Detach the texture from the FBO
734 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800735
736 layer->removeFbo(false);
737
Romain Guyeb993562010-10-05 18:14:38 -0700738 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700739 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700740 debugOverdraw(true, false);
Romain Guyeb993562010-10-05 18:14:38 -0700741 }
742
Romain Guy9ace8f52011-07-07 20:50:11 -0700743 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500744 SkPaint layerPaint;
745 layerPaint.setAlpha(layer->getAlpha());
746 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
747 layerPaint.setColorFilter(layer->getColorFilter());
748
749 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700750 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700751 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700752 }
753
Chris Craik96a5c4c2015-01-27 15:46:35 -0800754 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700755
Chris Craik44eb2c02015-01-29 09:45:09 -0800756 mCaches.textureState().activateTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700757
Romain Guy5b3b3522010-10-27 18:57:51 -0700758 // When the layer is stored in an FBO, we can save a bit of fillrate by
759 // drawing only the dirty region
760 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800761 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700762 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700763 } else if (!rect.isEmpty()) {
764 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530765
766 save(0);
767 // the layer contains screen buffer content that shouldn't be alpha modulated
768 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400769 writableSnapshot()->alpha = 1.0f;
Chris Craik53e51e42015-06-01 10:35:35 -0700770 composeLayerRectSwapped(layer, rect);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530771 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700772 }
Romain Guy8b55f372010-08-18 17:10:07 -0700773
Romain Guy746b7402010-10-26 16:27:31 -0700774 dirtyClip();
775
Romain Guyeb993562010-10-05 18:14:38 -0700776 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800777 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700778 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800779 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800780 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700781 }
782}
783
Romain Guyaa6c24c2011-04-28 18:40:04 -0700784void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craik1e4c8072015-05-26 14:25:02 -0700785 const bool tryToSnap = !layer->getForceFilter()
Chris Craik26bf3422015-02-26 16:28:17 -0800786 && layer->getWidth() == (uint32_t) rect.getWidth()
Chris Craik82840732015-04-03 09:37:49 -0700787 && layer->getHeight() == (uint32_t) rect.getHeight();
788 Glop glop;
789 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700790 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -0700791 .setMeshTexturedUvQuad(nullptr, Rect(0, 1, 1, 0)) // TODO: simplify with VBO
792 .setFillTextureLayer(*layer, getLayerAlpha(layer))
Chris Craik53e51e42015-06-01 10:35:35 -0700793 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik1e4c8072015-05-26 14:25:02 -0700794 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, rect)
Chris Craik82840732015-04-03 09:37:49 -0700795 .build();
796 renderGlop(glop);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700797}
798
Chris Craik53e51e42015-06-01 10:35:35 -0700799void OpenGLRenderer::composeLayerRectSwapped(Layer* layer, const Rect& rect) {
800 Glop glop;
801 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700802 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik53e51e42015-06-01 10:35:35 -0700803 .setMeshTexturedUvQuad(nullptr, layer->texCoords)
804 .setFillLayer(layer->getTexture(), layer->getColorFilter(),
805 getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::Swap)
806 .setTransform(*currentSnapshot(), TransformFlags::MeshIgnoresCanvasTransform)
807 .setModelViewMapUnitToRect(rect)
Chris Craik53e51e42015-06-01 10:35:35 -0700808 .build();
809 renderGlop(glop);
810}
811
812void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect) {
Chris Craik62d307c2014-07-29 10:35:13 -0700813 if (layer->isTextureLayer()) {
814 EVENT_LOGD("composeTextureLayerRect");
Chris Craik62d307c2014-07-29 10:35:13 -0700815 drawTextureLayer(layer, rect);
Chris Craik62d307c2014-07-29 10:35:13 -0700816 } else {
817 EVENT_LOGD("composeHardwareLayerRect");
Chris Craik182952f2015-03-09 14:17:29 -0700818
Chris Craik53e51e42015-06-01 10:35:35 -0700819 const bool tryToSnap = layer->getWidth() == static_cast<uint32_t>(rect.getWidth())
Chris Craik82840732015-04-03 09:37:49 -0700820 && layer->getHeight() == static_cast<uint32_t>(rect.getHeight());
821 Glop glop;
822 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700823 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -0700824 .setMeshTexturedUvQuad(nullptr, layer->texCoords)
Chris Craik53e51e42015-06-01 10:35:35 -0700825 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
826 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik1e4c8072015-05-26 14:25:02 -0700827 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, rect)
Chris Craik82840732015-04-03 09:37:49 -0700828 .build();
829 renderGlop(glop);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700830 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700831}
832
Chris Craik34416ea2013-04-15 16:08:28 -0700833/**
834 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
835 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
836 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
837 * by saveLayer's restore
838 */
Chris Craik2507c342015-05-04 14:36:49 -0700839#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
840 DRAW_COMMAND; \
Chih-Hung Hsiehcef190d2016-05-19 15:25:50 -0700841 if (CC_UNLIKELY(Properties::debugOverdraw && getTargetFbo() == 0 && (COND))) { \
Chris Craik2507c342015-05-04 14:36:49 -0700842 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
843 DRAW_COMMAND; \
844 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
845 } \
Chris Craik34416ea2013-04-15 16:08:28 -0700846 }
847
848#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
849
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400850// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
851// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
852class LayerShader : public SkShader {
853public:
854 LayerShader(Layer* layer, const SkMatrix* localMatrix)
855 : INHERITED(localMatrix)
856 , mLayer(layer) {
857 }
858
Chris Craike84a2082014-12-22 14:28:49 -0800859 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400860 if (data) {
861 *data = static_cast<void*>(mLayer);
862 }
863 return true;
864 }
865
Chris Craike84a2082014-12-22 14:28:49 -0800866 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400867 return !mLayer->isBlend();
868 }
869
870protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000871 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400872 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
873 }
874
Chris Craike84a2082014-12-22 14:28:49 -0800875 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400876 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
877 }
878
Chris Craike84a2082014-12-22 14:28:49 -0800879 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400880 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -0800881 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400882 }
883private:
884 // Unowned.
885 Layer* mLayer;
886 typedef SkShader INHERITED;
887};
888
Romain Guy5b3b3522010-10-27 18:57:51 -0700889void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -0700890 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
891
892 if (layer->getConvexMask()) {
Florin Malitaeecff562015-12-21 10:43:01 -0500893 save(SaveFlags::MatrixClip);
Chris Craik3f0854292014-04-15 16:18:08 -0700894
895 // clip to the area of the layer the mask can be larger
896 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
897
898 SkPaint paint;
899 paint.setAntiAlias(true);
900 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
901
Chris Craik3f0854292014-04-15 16:18:08 -0700902 // create LayerShader to map SaveLayer content into subsequent draw
903 SkMatrix shaderMatrix;
904 shaderMatrix.setTranslate(rect.left, rect.bottom);
905 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400906 LayerShader layerShader(layer, &shaderMatrix);
907 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -0700908
909 // Since the drawing primitive is defined in local drawing space,
910 // we don't need to modify the draw matrix
911 const SkPath* maskPath = layer->getConvexMask();
912 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
913
Chris Craike84a2082014-12-22 14:28:49 -0800914 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -0700915 restore();
916
917 return;
918 }
919
Romain Guy5b3b3522010-10-27 18:57:51 -0700920 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700921 layer->setRegionAsRect();
922
Chris Craik34416ea2013-04-15 16:08:28 -0700923 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -0700924
Romain Guy5b3b3522010-10-27 18:57:51 -0700925 layer->region.clear();
926 return;
927 }
928
Chris Craik62d307c2014-07-29 10:35:13 -0700929 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -0700930 // standard Region based draw
931 size_t count;
932 const android::Rect* rects;
933 Region safeRegion;
934 if (CC_LIKELY(hasRectToRectTransform())) {
935 rects = layer->region.getArray(&count);
936 } else {
937 safeRegion = Region::createTJunctionFreeRegion(layer->region);
938 rects = safeRegion.getArray(&count);
939 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700940
Chris Craik3f0854292014-04-15 16:18:08 -0700941 const float texX = 1.0f / float(layer->getWidth());
942 const float texY = 1.0f / float(layer->getHeight());
943 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700944
Chris Craik82840732015-04-03 09:37:49 -0700945 TextureVertex quadVertices[count * 4];
946 TextureVertex* mesh = &quadVertices[0];
Chris Craik3f0854292014-04-15 16:18:08 -0700947 for (size_t i = 0; i < count; i++) {
948 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -0700949
Chris Craik3f0854292014-04-15 16:18:08 -0700950 const float u1 = r->left * texX;
951 const float v1 = (height - r->top) * texY;
952 const float u2 = r->right * texX;
953 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700954
Chris Craik3f0854292014-04-15 16:18:08 -0700955 // TODO: Reject quads outside of the clip
956 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
957 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
958 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
959 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Chris Craik3f0854292014-04-15 16:18:08 -0700960 }
Tom Hudson040b6d82015-04-16 10:50:53 -0400961 Rect modelRect = Rect(rect.getWidth(), rect.getHeight());
Chris Craik82840732015-04-03 09:37:49 -0700962 Glop glop;
963 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -0700964 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -0700965 .setMeshTexturedIndexedQuads(&quadVertices[0], count * 6)
966 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
Chris Craik53e51e42015-06-01 10:35:35 -0700967 .setTransform(*currentSnapshot(), TransformFlags::None)
Tom Hudson040b6d82015-04-16 10:50:53 -0400968 .setModelViewOffsetRectSnap(rect.left, rect.top, modelRect)
Chris Craik82840732015-04-03 09:37:49 -0700969 .build();
970 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy5b3b3522010-10-27 18:57:51 -0700971
Romain Guy5b3b3522010-10-27 18:57:51 -0700972#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -0700973 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700974#endif
975
Chris Craik3f0854292014-04-15 16:18:08 -0700976 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -0700977}
978
Romain Guy3a3133d2011-02-01 22:59:58 -0800979#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -0700980void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -0800981 size_t count;
982 const android::Rect* rects = region.getArray(&count);
983
984 uint32_t colors[] = {
985 0x7fff0000, 0x7f00ff00,
986 0x7f0000ff, 0x7fff00ff,
987 };
988
989 int offset = 0;
990 int32_t top = rects[0].top;
991
992 for (size_t i = 0; i < count; i++) {
993 if (top != rects[i].top) {
994 offset ^= 0x2;
995 top = rects[i].top;
996 }
997
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500998 SkPaint paint;
999 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001000 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001001 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001002 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001003}
Chris Craike63f7c622013-10-17 10:30:55 -07001004#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001005
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001006void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001007 Vector<float> rects;
1008
1009 SkRegion::Iterator it(region);
1010 while (!it.done()) {
1011 const SkIRect& r = it.rect();
1012 rects.push(r.fLeft);
1013 rects.push(r.fTop);
1014 rects.push(r.fRight);
1015 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001016 it.next();
1017 }
1018
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001019 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001020}
1021
Romain Guy5b3b3522010-10-27 18:57:51 -07001022void OpenGLRenderer::dirtyLayer(const float left, const float top,
Chris Craik083e7332015-02-27 17:04:20 -08001023 const float right, const float bottom, const Matrix4& transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001024 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001025 Rect bounds(left, top, right, bottom);
1026 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001027 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001028 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001029}
1030
1031void OpenGLRenderer::dirtyLayer(const float left, const float top,
1032 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001033 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001034 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001035 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001036 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001037}
1038
1039void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Chris Craik6fe991e52015-10-20 09:39:42 -07001040 bounds.doIntersect(mState.currentRenderTargetClip());
Chris Craikac02eb92015-10-05 12:23:46 -07001041 if (!bounds.isEmpty()) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001042 bounds.snapToPixelBoundaries();
1043 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1044 if (!dirty.isEmpty()) {
1045 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001046 }
1047 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001048}
1049
Romain Guy54be1cd2011-06-13 19:04:27 -07001050void OpenGLRenderer::clearLayerRegions() {
Chris Craik2bb8f562015-02-17 16:42:02 -08001051 const size_t quadCount = mLayers.size();
1052 if (quadCount == 0) return;
Romain Guy54be1cd2011-06-13 19:04:27 -07001053
Tom Hudson984162f2014-10-10 13:38:16 -04001054 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001055 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001056 // Doing several glScissor/glClear here can negatively impact
1057 // GPUs with a tiler architecture, instead we draw quads with
1058 // the Clear blending mode
1059
1060 // The list contains bounds that have already been clipped
1061 // against their initial clip rect, and the current clip
1062 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001063 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001064
Chris Craik2bb8f562015-02-17 16:42:02 -08001065 Vertex mesh[quadCount * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001066 Vertex* vertex = mesh;
1067
Chris Craik2bb8f562015-02-17 16:42:02 -08001068 for (uint32_t i = 0; i < quadCount; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001069 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001070
Chris Craik51d6a3d2014-12-22 17:16:56 -08001071 Vertex::set(vertex++, bounds.left, bounds.top);
1072 Vertex::set(vertex++, bounds.right, bounds.top);
1073 Vertex::set(vertex++, bounds.left, bounds.bottom);
1074 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001075 }
Romain Guye67307c2013-02-11 18:01:20 -08001076 // We must clear the list of dirty rects before we
Chris Craik82840732015-04-03 09:37:49 -07001077 // call clearLayerRegions() in renderGlop to prevent
1078 // stencil setup from doing the same thing again
Romain Guye67307c2013-02-11 18:01:20 -08001079 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001080
Chris Craik53e51e42015-06-01 10:35:35 -07001081 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001082 Glop glop;
1083 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001084 .setRoundRectClipState(nullptr) // clear ignores clip state
Chris Craik82840732015-04-03 09:37:49 -07001085 .setMeshIndexedQuads(&mesh[0], quadCount)
1086 .setFillClear()
Chris Craik53e51e42015-06-01 10:35:35 -07001087 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik6fe991e52015-10-20 09:39:42 -07001088 .setModelViewOffsetRect(0, 0, Rect(currentSnapshot()->getRenderTargetClip()))
Chris Craik82840732015-04-03 09:37:49 -07001089 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001090 renderGlop(glop, GlopRenderType::LayerClear);
Romain Guy8a4ac612012-07-17 17:32:48 -07001091
Chris Craik65fe5ee2015-01-26 18:06:29 -08001092 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001093 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001094 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001095 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001096}
1097
Romain Guybd6b79b2010-06-26 00:13:53 -07001098///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001099// State Deferral
1100///////////////////////////////////////////////////////////////////////////////
1101
Chris Craikff785832013-03-08 13:12:16 -08001102bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craik6fe991e52015-10-20 09:39:42 -07001103 const Rect& currentClip = mState.currentRenderTargetClip();
Chris Craikd6b65f62014-01-01 14:45:21 -08001104 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001105
Chris Craikff785832013-03-08 13:12:16 -08001106 if (stateDeferFlags & kStateDeferFlag_Draw) {
1107 // state has bounds initialized in local coordinates
1108 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001109 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001110 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001111 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1112 // is used, it should more closely duplicate the quickReject logic (in how it uses
1113 // snapToPixelBoundaries)
1114
Chris Craikac02eb92015-10-05 12:23:46 -07001115 clippedBounds.doIntersect(currentClip);
1116 if (clippedBounds.isEmpty()) {
Chris Craikff785832013-03-08 13:12:16 -08001117 // quick rejected
1118 return true;
1119 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001120
Chris Craika02c4ed2013-06-14 13:43:58 -07001121 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001122 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001123 int& flags = state.mClipSideFlags;
1124 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001125 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1126 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1127 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1128 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001129 }
1130 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001131 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001132 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1133 // overdraw avoidance (since we don't know what it overlaps)
1134 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001135 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001136 }
1137 }
1138
Chris Craik527a3aa2013-03-04 10:19:31 -08001139 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1140 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001141 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001142 }
1143
Chris Craik8df5ffa2015-04-28 17:47:20 -07001144 // Transform and alpha always deferred, since they are used by state operations
Chris Craik7273daa2013-03-28 11:25:24 -07001145 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craik7c85c542015-08-19 15:10:24 -07001146 state.mMatrix = *currentMatrix;
Chris Craikd6b65f62014-01-01 14:45:21 -08001147 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001148
Chris Craikfca52b752015-04-28 11:45:59 -07001149 // always store/restore, since these are just pointers
Chris Craikdeeda3d2014-05-05 19:09:33 -07001150 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craik678ff812016-03-01 13:27:54 -08001151#if !HWUI_NEW_OPS
Chris Craikfca52b752015-04-28 11:45:59 -07001152 state.mProjectionPathMask = currentSnapshot()->projectionPathMask;
Chris Craik678ff812016-03-01 13:27:54 -08001153#endif
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 Craik678ff812016-03-01 13:27:54 -08001161#if !HWUI_NEW_OPS
Chris Craikfca52b752015-04-28 11:45:59 -07001162 writableSnapshot()->projectionPathMask = state.mProjectionPathMask;
Chris Craik678ff812016-03-01 13:27:54 -08001163#endif
Chris Craikff785832013-03-08 13:12:16 -08001164
Chris Craik527a3aa2013-03-04 10:19:31 -08001165 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001166 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001167 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001168 dirtyClip();
1169 }
Chris Craikc3566d02013-02-04 16:16:33 -08001170}
1171
Chris Craik28ce94a2013-05-31 11:38:03 -07001172/**
1173 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1174 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1175 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1176 *
1177 * This method should be called when restoreDisplayState() won't be restoring the clip
1178 */
1179void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001180 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001181 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001182 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001183 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001184 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001185 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001186 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1187 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001188}
1189
Chris Craikc3566d02013-02-04 16:16:33 -08001190///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001191// Clipping
1192///////////////////////////////////////////////////////////////////////////////
1193
Romain Guybb9524b2010-06-22 18:56:38 -07001194void OpenGLRenderer::setScissorFromClip() {
Chris Craik6fe991e52015-10-20 09:39:42 -07001195 Rect clip(mState.currentRenderTargetClip());
Romain Guye5ebcb02010-10-15 13:57:28 -07001196 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001197
Chris Craik65fe5ee2015-01-26 18:06:29 -08001198 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001199 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001200 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001201 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001202}
1203
Romain Guy8ce00302013-01-15 18:51:42 -08001204void OpenGLRenderer::ensureStencilBuffer() {
1205 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1206 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1207 // just hope we have one when hasLayer() returns false.
1208 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001209 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001210 }
1211}
1212
1213void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1214 // The layer's FBO is already bound when we reach this stage
1215 if (!layer->getStencilRenderBuffer()) {
Romain Guy8d4aeb72013-02-12 16:08:55 -08001216 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Chris Craike1450132015-04-28 17:55:50 -07001217 Stencil::getLayerStencilFormat(),
Chris Craik117bdbc2015-02-05 10:12:38 -08001218 layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001219 layer->setStencilRenderBuffer(buffer);
1220 }
1221}
1222
Rob Tsuk487a92c2015-01-06 13:22:54 -08001223static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1224 float x, float y) {
1225 Vertex v;
1226 v.x = x;
1227 v.y = y;
1228 transform.mapPoint(v.x, v.y);
1229 rectangleVertices.push_back(v);
1230}
1231
1232static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1233 Vertex v;
1234 v.x = x;
1235 v.y = y;
1236 rectangleVertices.push_back(v);
1237}
1238
1239void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
Chris Craik2bb8f562015-02-17 16:42:02 -08001240 int quadCount = rectangleList.getTransformedRectanglesCount();
1241 std::vector<Vertex> rectangleVertices(quadCount * 4);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001242 Rect scissorBox = rectangleList.calculateBounds();
1243 scissorBox.snapToPixelBoundaries();
Chris Craik2bb8f562015-02-17 16:42:02 -08001244 for (int i = 0; i < quadCount; ++i) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001245 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1246 const Matrix4& transform = tr.getTransform();
1247 Rect bounds = tr.getBounds();
1248 if (transform.rectToRect()) {
1249 transform.mapRect(bounds);
Chris Craikac02eb92015-10-05 12:23:46 -07001250 bounds.doIntersect(scissorBox);
1251 if (!bounds.isEmpty()) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001252 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1253 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1254 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1255 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1256 }
1257 } else {
1258 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1259 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1260 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1261 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1262 }
1263 }
1264
Chris Craik65fe5ee2015-01-26 18:06:29 -08001265 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001266 scissorBox.getWidth(), scissorBox.getHeight());
Chris Craik53e51e42015-06-01 10:35:35 -07001267 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001268 Glop glop;
1269 Vertex* vertices = &rectangleVertices[0];
1270 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001271 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001272 .setMeshIndexedQuads(vertices, rectangleVertices.size() / 4)
1273 .setFillBlack()
Chris Craik53e51e42015-06-01 10:35:35 -07001274 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001275 .setModelViewOffsetRect(0, 0, scissorBox)
Chris Craik82840732015-04-03 09:37:49 -07001276 .build();
1277 renderGlop(glop);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001278}
1279
Romain Guy8ce00302013-01-15 18:51:42 -08001280void OpenGLRenderer::setStencilFromClip() {
Chris Craik2507c342015-05-04 14:36:49 -07001281 if (!Properties::debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001282 if (!currentSnapshot()->clipIsSimple()) {
1283 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001284 EVENT_LOGD("setStencilFromClip - enabling");
1285
Romain Guy8ce00302013-01-15 18:51:42 -08001286 // NOTE: The order here is important, we must set dirtyClip to false
1287 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001288 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001289
1290 ensureStencilBuffer();
1291
Rob Tsuk487a92c2015-01-06 13:22:54 -08001292 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001293
Rob Tsuk487a92c2015-01-06 13:22:54 -08001294 bool isRectangleList = clipArea.isRectangleList();
1295 if (isRectangleList) {
1296 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1297 } else {
1298 incrementThreshold = 0;
1299 }
1300
Chris Craik96a5c4c2015-01-27 15:46:35 -08001301 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001302
1303 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001304 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001305 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001306 if (resetScissor) {
1307 // The scissor was not set so we now need to update it
1308 setScissorFromClip();
1309 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001310
Chris Craik96a5c4c2015-01-27 15:46:35 -08001311 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001312
1313 // stash and disable the outline clip state, since stencil doesn't account for outline
1314 bool storedSkipOutlineClip = mSkipOutlineClip;
1315 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001316
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001317 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001318 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001319 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1320
Rob Tsuk487a92c2015-01-06 13:22:54 -08001321 if (isRectangleList) {
1322 drawRectangleList(clipArea.getRectangleList());
1323 } else {
1324 // NOTE: We could use the region contour path to generate a smaller mesh
1325 // Since we are using the stencil we could use the red book path
1326 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001327
Rob Tsuk487a92c2015-01-06 13:22:54 -08001328 // The last parameter is important: we are not drawing in the color buffer
1329 // so we don't want to dirty the current layer, if any
1330 drawRegionRects(clipArea.getClipRegion(), paint, false);
1331 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001332 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001333 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001334
Chris Craik96a5c4c2015-01-27 15:46:35 -08001335 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001336
1337 // Draw the region used to generate the stencil if the appropriate debug
1338 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001339 // TODO: Implement for rectangle list clip areas
Chris Craik2507c342015-05-04 14:36:49 -07001340 if (Properties::debugStencilClip == StencilClipDebug::ShowRegion
1341 && !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001342 paint.setColor(0x7f0000ff);
1343 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001344 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001345 }
Romain Guy8ce00302013-01-15 18:51:42 -08001346 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001347 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001348 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001349 }
1350 }
1351}
1352
Chris Craikf0a59072013-11-19 18:00:46 -08001353/**
1354 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1355 *
1356 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1357 * style, and tessellated AA ramp
1358 */
1359bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001360 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001361 bool snapOut = paint && paint->isAntiAlias();
1362
1363 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1364 float outset = paint->getStrokeWidth() * 0.5f;
1365 left -= outset;
1366 top -= outset;
1367 right += outset;
1368 bottom += outset;
1369 }
1370
Chris Craikdeeda3d2014-05-05 19:09:33 -07001371 bool clipRequired = false;
1372 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001373 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001374 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001375 return true;
1376 }
1377
Chris Craikf23b25a2014-06-26 15:46:20 -07001378 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001379 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001380 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001381 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001382}
1383
Romain Guy8ce00302013-01-15 18:51:42 -08001384void OpenGLRenderer::debugClip() {
1385#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001386 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001387 SkPaint paint;
1388 paint.setColor(0x7f00ff00);
1389 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1390
Romain Guy8ce00302013-01-15 18:51:42 -08001391 }
1392#endif
1393}
1394
Chris Craik3375f8a2015-06-23 17:33:06 -07001395void OpenGLRenderer::renderGlop(const Glop& glop, GlopRenderType type) {
Chris Craik6b109c72015-02-27 10:55:28 -08001396 // TODO: It would be best if we could do this before quickRejectSetupScissor()
1397 // changes the scissor test state
Chris Craik3375f8a2015-06-23 17:33:06 -07001398 if (type != GlopRenderType::LayerClear) {
1399 // Regular draws need to clear the dirty area on the layer before they start drawing on top
1400 // of it. If this draw *is* a layer clear, it skips the clear step (since it would
1401 // infinitely recurse)
1402 clearLayerRegions();
1403 }
Chris Craik6b109c72015-02-27 10:55:28 -08001404
Chris Craik117bdbc2015-02-05 10:12:38 -08001405 if (mState.getDirtyClip()) {
1406 if (mRenderState.scissor().isEnabled()) {
1407 setScissorFromClip();
1408 }
1409
1410 setStencilFromClip();
1411 }
Chris Craik12efe642015-09-28 15:52:12 -07001412 mRenderState.render(glop, currentSnapshot()->getOrthoMatrix());
Chris Craik3375f8a2015-06-23 17:33:06 -07001413 if (type == GlopRenderType::Standard && !mRenderState.stencil().isWriteEnabled()) {
Chris Craik2ab95d72015-02-06 15:25:51 -08001414 // TODO: specify more clearly when a draw should dirty the layer.
1415 // is writing to the stencil the only time we should ignore this?
Chris Craik70969cc2016-03-30 18:09:17 -07001416#if !HWUI_NEW_OPS
Chris Craik2ab95d72015-02-06 15:25:51 -08001417 dirtyLayer(glop.bounds.left, glop.bounds.top, glop.bounds.right, glop.bounds.bottom);
Chris Craik70969cc2016-03-30 18:09:17 -07001418#endif
Chris Craik0519c812015-02-11 13:17:06 -08001419 mDirty = true;
Chris Craik2ab95d72015-02-06 15:25:51 -08001420 }
Chris Craik117bdbc2015-02-05 10:12:38 -08001421}
1422
Romain Guyf6a11b82010-06-23 17:47:49 -07001423///////////////////////////////////////////////////////////////////////////////
1424// Drawing
1425///////////////////////////////////////////////////////////////////////////////
1426
Tom Hudson107843d2014-09-08 11:26:26 -04001427void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001428 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1429 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001430 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001431 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001432 renderNode->computeOrdering();
Chris Craik2507c342015-05-04 14:36:49 -07001433 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001434 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001435 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001436 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001437 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001438 }
1439
Chris Craik6fe991e52015-10-20 09:39:42 -07001440 DeferredDisplayList deferredList(mState.currentRenderTargetClip());
Chris Craikff785832013-03-08 13:12:16 -08001441 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001442 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001443
1444 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001445 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001446
Tom Hudson107843d2014-09-08 11:26:26 -04001447 deferredList.flush(*this, dirty);
1448 } else {
1449 // Even if there is no drawing command(Ex: invisible),
1450 // it still needs startFrame to clear buffer and start tiling.
1451 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001452 }
1453}
1454
Romain Guy03c00b52013-06-20 18:30:28 -07001455/**
1456 * Important note: this method is intended to draw batches of bitmaps and
1457 * will not set the scissor enable or dirty the current layer, if any.
1458 * The caller is responsible for properly dirtying the current layer.
1459 */
Tom Hudson107843d2014-09-08 11:26:26 -04001460void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001461 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1462 const Rect& bounds, const SkPaint* paint) {
Romain Guy55b6f952013-06-27 15:27:09 -07001463 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001464 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001465
Chris Craik527a3aa2013-03-04 10:19:31 -08001466 const AutoTexture autoCleanup(texture);
1467
Chris Craik82840732015-04-03 09:37:49 -07001468 // TODO: remove layer dirty in multi-draw callers
1469 // TODO: snap doesn't need to touch transform, only texture filter.
1470 bool snap = pureTranslate;
Chris Craika6b52192015-02-27 17:43:02 -08001471 const float x = floorf(bounds.left + 0.5f);
1472 const float y = floorf(bounds.top + 0.5f);
Chris Craik53e51e42015-06-01 10:35:35 -07001473
1474 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1475 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
1476 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001477 Glop glop;
1478 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001479 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001480 .setMeshTexturedMesh(vertices, bitmapCount * 6)
1481 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001482 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik5430ab22015-12-10 16:28:16 -08001483 .setModelViewOffsetRectOptionalSnap(snap, x, y, Rect(bounds.getWidth(), bounds.getHeight()))
Chris Craik82840732015-04-03 09:37:49 -07001484 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001485 renderGlop(glop, GlopRenderType::Multi);
Chris Craik527a3aa2013-03-04 10:19:31 -08001486}
1487
Tom Hudson107843d2014-09-08 11:26:26 -04001488void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07001489 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04001490 return;
Romain Guy6926c722010-07-12 20:20:03 -07001491 }
1492
Chris Craik44eb2c02015-01-29 09:45:09 -08001493 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07001494 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001495 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001496 const AutoTexture autoCleanup(texture);
1497
Chris Craik53e51e42015-06-01 10:35:35 -07001498 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1499 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07001500 Glop glop;
1501 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001502 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001503 .setMeshTexturedUnitQuad(texture->uvMapper)
1504 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001505 .setTransform(*currentSnapshot(), TransformFlags::None)
John Reck38e0c322015-11-10 12:19:17 -08001506 .setModelViewMapUnitToRectSnap(Rect(texture->width(), texture->height()))
Chris Craik82840732015-04-03 09:37:49 -07001507 .build();
1508 renderGlop(glop);
Romain Guyce0537b2010-06-29 21:05:21 -07001509}
1510
Tom Hudson107843d2014-09-08 11:26:26 -04001511void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08001512 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001513 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04001514 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08001515 }
1516
Romain Guyb18d2d02011-02-10 15:52:54 -08001517 float left = FLT_MAX;
1518 float top = FLT_MAX;
1519 float right = FLT_MIN;
1520 float bottom = FLT_MIN;
1521
Chris Craikef250742015-02-25 17:16:16 -08001522 const uint32_t elementCount = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001523
Chris Craikef250742015-02-25 17:16:16 -08001524 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[elementCount]);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001525 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08001526
Chris Craik51d6a3d2014-12-22 17:16:56 -08001527 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08001528 if (!colors) {
1529 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001530 tempColors.reset(new int[colorsCount]);
1531 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
1532 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08001533 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001534
Chris Craik15c3f192015-12-03 12:16:56 -08001535 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap->pixelRef());
Romain Guy3b748a42013-04-17 18:54:38 -07001536 const UvMapper& mapper(getMapper(texture));
1537
Romain Guy5a7b4662011-01-20 19:09:30 -08001538 for (int32_t y = 0; y < meshHeight; y++) {
1539 for (int32_t x = 0; x < meshWidth; x++) {
1540 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1541
1542 float u1 = float(x) / meshWidth;
1543 float u2 = float(x + 1) / meshWidth;
1544 float v1 = float(y) / meshHeight;
1545 float v2 = float(y + 1) / meshHeight;
1546
Romain Guy3b748a42013-04-17 18:54:38 -07001547 mapper.map(u1, v1, u2, v2);
1548
Romain Guy5a7b4662011-01-20 19:09:30 -08001549 int ax = i + (meshWidth + 1) * 2;
1550 int ay = ax + 1;
1551 int bx = i;
1552 int by = bx + 1;
1553 int cx = i + 2;
1554 int cy = cx + 1;
1555 int dx = i + (meshWidth + 1) * 2 + 2;
1556 int dy = dx + 1;
1557
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[ax], vertices[ay], u1, v2, colors[ax / 2]);
1560 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08001561
Romain Guyff316ec2013-02-13 18:39:43 -08001562 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1563 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
1564 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08001565
Chris Craikdf72b632015-06-30 17:56:13 -07001566 left = std::min(left, std::min(vertices[ax], std::min(vertices[bx], vertices[cx])));
1567 top = std::min(top, std::min(vertices[ay], std::min(vertices[by], vertices[cy])));
1568 right = std::max(right, std::max(vertices[ax], std::max(vertices[bx], vertices[cx])));
1569 bottom = std::max(bottom, std::max(vertices[ay], std::max(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001570 }
1571 }
1572
Chris Craikf0a59072013-11-19 18:00:46 -08001573 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001574 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07001575 }
1576
Romain Guyff316ec2013-02-13 18:39:43 -08001577 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07001578 texture = mCaches.textureCache.get(bitmap);
1579 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04001580 return;
Romain Guy3b748a42013-04-17 18:54:38 -07001581 }
Romain Guyff316ec2013-02-13 18:39:43 -08001582 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001583 const AutoTexture autoCleanup(texture);
1584
Chris Craik82840732015-04-03 09:37:49 -07001585 /*
1586 * TODO: handle alpha_8 textures correctly by applying paint color, but *not*
1587 * shader in that case to mimic the behavior in SkiaCanvas::drawBitmapMesh.
1588 */
Chris Craik53e51e42015-06-01 10:35:35 -07001589 const int textureFillFlags = TextureFillFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07001590 Glop glop;
1591 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001592 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001593 .setMeshColoredTexturedMesh(mesh.get(), elementCount)
Chris Craik53e51e42015-06-01 10:35:35 -07001594 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1595 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik82840732015-04-03 09:37:49 -07001596 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07001597 .build();
1598 renderGlop(glop);
Romain Guy5a7b4662011-01-20 19:09:30 -08001599}
1600
Chris Craik14100ac2015-02-24 13:46:29 -08001601void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, Rect src, Rect dst, const SkPaint* paint) {
1602 if (quickRejectSetupScissor(dst)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001603 return;
Romain Guy6926c722010-07-12 20:20:03 -07001604 }
1605
Romain Guy3b748a42013-04-17 18:54:38 -07001606 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001607 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001608 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001609
John Reck38e0c322015-11-10 12:19:17 -08001610 Rect uv(std::max(0.0f, src.left / texture->width()),
1611 std::max(0.0f, src.top / texture->height()),
1612 std::min(1.0f, src.right / texture->width()),
1613 std::min(1.0f, src.bottom / texture->height()));
Chris Craik14100ac2015-02-24 13:46:29 -08001614
Chris Craik53e51e42015-06-01 10:35:35 -07001615 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
1616 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
Chris Craik1e4c8072015-05-26 14:25:02 -07001617 const bool tryToSnap = MathUtils::areEqual(src.getWidth(), dst.getWidth())
1618 && MathUtils::areEqual(src.getHeight(), dst.getHeight());
Chris Craik82840732015-04-03 09:37:49 -07001619 Glop glop;
1620 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001621 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001622 .setMeshTexturedUvQuad(texture->uvMapper, uv)
1623 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001624 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik1e4c8072015-05-26 14:25:02 -07001625 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, dst)
Chris Craik82840732015-04-03 09:37:49 -07001626 .build();
1627 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07001628}
1629
Tom Hudson107843d2014-09-08 11:26:26 -04001630void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08001631 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
1632 const SkPaint* paint) {
Chris Craika6b52192015-02-27 17:43:02 -08001633 if (!mesh || !mesh->verticesCount || quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001634 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07001635 }
1636
Chris Craika6b52192015-02-27 17:43:02 -08001637 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
1638 if (!texture) return;
Dohyun Leec5a3efd2016-01-21 13:46:21 +09001639 const AutoTexture autoCleanup(texture);
Chris Craik0556d902015-03-03 09:54:14 -08001640
Chris Craik82840732015-04-03 09:37:49 -07001641 // 9 patches are built for stretching - always filter
Chris Craik53e51e42015-06-01 10:35:35 -07001642 int textureFillFlags = TextureFillFlags::ForceFilter;
Chris Craik82840732015-04-03 09:37:49 -07001643 if (bitmap->colorType() == kAlpha_8_SkColorType) {
Chris Craik53e51e42015-06-01 10:35:35 -07001644 textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture;
Chris Craik0556d902015-03-03 09:54:14 -08001645 }
Chris Craik82840732015-04-03 09:37:49 -07001646 Glop glop;
1647 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001648 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001649 .setMeshPatchQuads(*mesh)
1650 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001651 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik5430ab22015-12-10 16:28:16 -08001652 .setModelViewOffsetRectSnap(left, top, Rect(right - left, bottom - top)) // TODO: get minimal bounds from patch
Chris Craik82840732015-04-03 09:37:49 -07001653 .build();
1654 renderGlop(glop);
Romain Guyf7f93552010-07-08 19:17:03 -07001655}
1656
Romain Guy03c00b52013-06-20 18:30:28 -07001657/**
1658 * Important note: this method is intended to draw batches of 9-patch objects and
1659 * will not set the scissor enable or dirty the current layer, if any.
1660 * The caller is responsible for properly dirtying the current layer.
1661 */
Tom Hudson107843d2014-09-08 11:26:26 -04001662void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craika6b52192015-02-27 17:43:02 -08001663 TextureVertex* vertices, uint32_t elementCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001664 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07001665 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001666 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07001667 const AutoTexture autoCleanup(texture);
1668
Chris Craik82840732015-04-03 09:37:49 -07001669 // TODO: get correct bounds from caller
Chris Craik53e51e42015-06-01 10:35:35 -07001670 const int transformFlags = TransformFlags::MeshIgnoresCanvasTransform;
Chris Craik82840732015-04-03 09:37:49 -07001671 // 9 patches are built for stretching - always filter
Chris Craik53e51e42015-06-01 10:35:35 -07001672 int textureFillFlags = TextureFillFlags::ForceFilter;
Chris Craik82840732015-04-03 09:37:49 -07001673 if (bitmap->colorType() == kAlpha_8_SkColorType) {
Chris Craik53e51e42015-06-01 10:35:35 -07001674 textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture;
Chris Craika6b52192015-02-27 17:43:02 -08001675 }
Chris Craik82840732015-04-03 09:37:49 -07001676 Glop glop;
1677 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001678 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001679 .setMeshTexturedIndexedQuads(vertices, elementCount)
1680 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001681 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik5430ab22015-12-10 16:28:16 -08001682 .setModelViewOffsetRect(0, 0, Rect())
Chris Craik82840732015-04-03 09:37:49 -07001683 .build();
Chris Craik3375f8a2015-06-23 17:33:06 -07001684 renderGlop(glop, GlopRenderType::Multi);
Romain Guy03c00b52013-06-20 18:30:28 -07001685}
1686
Tom Hudson107843d2014-09-08 11:26:26 -04001687void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07001688 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001689 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07001690 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08001691 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04001692 return;
Chris Craik65cd6122012-12-10 17:56:27 -08001693 }
1694
Chris Craik82840732015-04-03 09:37:49 -07001695 bool shadowInterp = displayFlags & kVertexBuffer_ShadowInterp;
Chris Craik53e51e42015-06-01 10:35:35 -07001696 const int transformFlags = TransformFlags::OffsetByFudgeFactor;
Chris Craik82840732015-04-03 09:37:49 -07001697 Glop glop;
1698 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001699 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik138c21f2016-04-28 16:59:42 -07001700 .setMeshVertexBuffer(vertexBuffer)
1701 .setFillPaint(*paint, currentSnapshot()->alpha, shadowInterp)
Chris Craik53e51e42015-06-01 10:35:35 -07001702 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07001703 .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds())
Chris Craik82840732015-04-03 09:37:49 -07001704 .build();
1705 renderGlop(glop);
Chet Haase858aa932011-05-12 09:06:00 -07001706}
1707
1708/**
Chris Craik65cd6122012-12-10 17:56:27 -08001709 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
1710 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
1711 * screen space in all directions. However, instead of using a fragment shader to compute the
1712 * translucency of the color from its position, we simply use a varying parameter to define how far
1713 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
1714 *
1715 * Doesn't yet support joins, caps, or path effects.
1716 */
Tom Hudson107843d2014-09-08 11:26:26 -04001717void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08001718 VertexBuffer vertexBuffer;
1719 // TODO: try clipping large paths to viewport
Chris Craikfca52b752015-04-28 11:45:59 -07001720
Chris Craikd6b65f62014-01-01 14:45:21 -08001721 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04001722 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08001723}
1724
1725/**
1726 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
1727 * and additional geometry for defining an alpha slope perimeter.
1728 *
1729 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
1730 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
1731 * in-shader alpha region, but found it to be taxing on some GPUs.
1732 *
1733 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
1734 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07001735 */
Tom Hudson107843d2014-09-08 11:26:26 -04001736void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001737 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07001738
Chris Craik65cd6122012-12-10 17:56:27 -08001739 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07001740
Chris Craik65cd6122012-12-10 17:56:27 -08001741 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001742 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
1743 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08001744
Chris Craik05f3d6e2014-06-02 16:27:04 -07001745 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001746 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08001747 }
1748
Chris Craikbf759452014-08-11 16:00:44 -07001749 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001750 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07001751}
1752
Tom Hudson107843d2014-09-08 11:26:26 -04001753void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001754 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001755
Chris Craik6d29c8d2013-05-08 18:35:44 -07001756 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07001757
Chris Craik6d29c8d2013-05-08 18:35:44 -07001758 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001759 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08001760
Chris Craik05f3d6e2014-06-02 16:27:04 -07001761 const Rect& bounds = buffer.getBounds();
1762 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001763 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001764 }
1765
Chris Craikbf759452014-08-11 16:00:44 -07001766 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001767 drawVertexBuffer(buffer, paint, displayFlags);
1768
1769 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07001770}
1771
Tom Hudson107843d2014-09-08 11:26:26 -04001772void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001773 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04001774 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001775
Chris Craik6fe991e52015-10-20 09:39:42 -07001776 Rect clip(mState.currentRenderTargetClip());
Romain Guyae88e5e2010-10-22 17:49:18 -07001777 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001778
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001779 SkPaint paint;
1780 paint.setColor(color);
1781 paint.setXfermodeMode(mode);
1782
1783 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07001784
Tom Hudson107843d2014-09-08 11:26:26 -04001785 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07001786}
Romain Guy9d5316e2010-06-24 19:30:36 -07001787
Chris Craik30036092015-02-12 10:41:39 -08001788void OpenGLRenderer::drawShape(float left, float top, PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08001789 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04001790 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001791 const AutoTexture autoCleanup(texture);
1792
1793 const float x = left + texture->left - texture->offset;
1794 const float y = top + texture->top - texture->offset;
1795
1796 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07001797
Tom Hudson107843d2014-09-08 11:26:26 -04001798 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08001799}
1800
Tom Hudson107843d2014-09-08 11:26:26 -04001801void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001802 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001803 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001804 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001805 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001806 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001807 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001808
Chris Craike84a2082014-12-22 14:28:49 -08001809 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001810 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001811 PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07001812 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001813 drawShape(left, top, texture, p);
1814 } else {
1815 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
1816 *currentTransform(), *p, right - left, bottom - top, rx, ry);
1817 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001818 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001819}
1820
Tom Hudson107843d2014-09-08 11:26:26 -04001821void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001822 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001823 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001824 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001825 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001826 }
Chris Craikfca52b752015-04-28 11:45:59 -07001827
Chris Craike84a2082014-12-22 14:28:49 -08001828 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001829 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001830 PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001831 drawShape(x - radius, y - radius, texture, p);
Chris Craikfca52b752015-04-28 11:45:59 -07001832 return;
Chris Craikcb4d6002012-09-25 12:00:29 -07001833 }
Chris Craikfca52b752015-04-28 11:45:59 -07001834
1835 SkPath path;
1836 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1837 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
1838 } else {
1839 path.addCircle(x, y, radius);
1840 }
1841
Chris Craik678ff812016-03-01 13:27:54 -08001842#if !HWUI_NEW_OPS
Chris Craikfca52b752015-04-28 11:45:59 -07001843 if (CC_UNLIKELY(currentSnapshot()->projectionPathMask != nullptr)) {
1844 // mask ripples with projection mask
1845 SkPath maskPath = *(currentSnapshot()->projectionPathMask->projectionMask);
1846
1847 Matrix4 screenSpaceTransform;
1848 currentSnapshot()->buildScreenSpaceTransform(&screenSpaceTransform);
1849
1850 Matrix4 totalTransform;
1851 totalTransform.loadInverse(screenSpaceTransform);
1852 totalTransform.multiply(currentSnapshot()->projectionPathMask->projectionMaskTransform);
1853
1854 SkMatrix skTotalTransform;
1855 totalTransform.copyTo(skTotalTransform);
1856 maskPath.transform(skTotalTransform);
1857
1858 // Mask the ripple path by the projection mask, now that it's
1859 // in local space. Note that this can create CCW paths.
Tom Hudson02a26302015-06-24 11:32:42 -04001860 Op(path, maskPath, kIntersect_SkPathOp, &path);
Chris Craikfca52b752015-04-28 11:45:59 -07001861 }
Chris Craik678ff812016-03-01 13:27:54 -08001862#endif
Chris Craikfca52b752015-04-28 11:45:59 -07001863 drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001864}
Romain Guy01d58e42011-01-19 21:54:02 -08001865
Tom Hudson107843d2014-09-08 11:26:26 -04001866void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001867 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001868 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001869 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001870 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001871 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001872 }
Romain Guy01d58e42011-01-19 21:54:02 -08001873
Chris Craike84a2082014-12-22 14:28:49 -08001874 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001875 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001876 PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001877 drawShape(left, top, texture, p);
1878 } else {
1879 SkPath path;
1880 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1881 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1882 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1883 }
1884 path.addOval(rect);
1885 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001886 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001887}
1888
Tom Hudson107843d2014-09-08 11:26:26 -04001889void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001890 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001891 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001892 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001893 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001894 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08001895 }
1896
Chris Craik780c1282012-10-04 14:10:49 -07001897 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08001898 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001899 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001900 PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07001901 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001902 drawShape(left, top, texture, p);
1903 return;
Chris Craik780c1282012-10-04 14:10:49 -07001904 }
Chris Craik780c1282012-10-04 14:10:49 -07001905 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1906 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1907 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1908 }
1909
1910 SkPath path;
1911 if (useCenter) {
1912 path.moveTo(rect.centerX(), rect.centerY());
1913 }
1914 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
1915 if (useCenter) {
1916 path.close();
1917 }
Tom Hudson107843d2014-09-08 11:26:26 -04001918 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08001919}
1920
Tom Hudson107843d2014-09-08 11:26:26 -04001921void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001922 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001923 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001924 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001925 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001926 return;
Romain Guy6926c722010-07-12 20:20:03 -07001927 }
1928
Chris Craik710f46d2012-09-17 17:25:49 -07001929 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07001930 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craik386aa032015-12-07 17:08:25 -08001931 static_assert(SkPaintDefaults_MiterLimit == 4.0f, "Miter limit has changed");
Chris Craike84a2082014-12-22 14:28:49 -08001932 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07001933 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001934 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001935 PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07001936 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001937 drawShape(left, top, texture, p);
1938 } else {
1939 SkPath path;
1940 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1941 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1942 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1943 }
1944 path.addRect(rect);
1945 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07001946 }
Chet Haase858aa932011-05-12 09:06:00 -07001947 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04001948 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
1949 SkPath path;
1950 path.addRect(left, top, right, bottom);
1951 drawConvexPath(path, p);
1952 } else {
1953 drawColorRect(left, top, right, bottom, p);
1954
1955 mDirty = true;
1956 }
Chet Haase858aa932011-05-12 09:06:00 -07001957 }
Romain Guyc7d53492010-06-25 13:41:57 -07001958}
Romain Guy9d5316e2010-06-24 19:30:36 -07001959
Chris Craike8c3c812016-02-05 20:10:50 -08001960void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const glyph_t* glyphs,
Chris Craika1717272015-11-19 13:02:43 -08001961 int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001962 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001963 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07001964
Chris Craikbf6f0f22015-10-01 12:36:07 -07001965 PaintUtils::TextShadow textShadow;
1966 if (!PaintUtils::getTextShadow(paint, &textShadow)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04001967 LOG_ALWAYS_FATAL("failed to query shadow attributes");
1968 }
1969
Raph Levien416a8472012-07-19 22:48:17 -07001970 // NOTE: The drop shadow will not perform gamma correction
1971 // if shader-based correction is enabled
1972 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Chris Craik2bb8f562015-02-17 16:42:02 -08001973 ShadowTexture* texture = mCaches.dropShadowCache.get(
Chris Craike8c3c812016-02-05 20:10:50 -08001974 paint, glyphs, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07001975 // If the drop shadow exceeds the max texture size or couldn't be
1976 // allocated, skip drawing
Chris Craik2bb8f562015-02-17 16:42:02 -08001977 if (!texture) return;
1978 const AutoTexture autoCleanup(texture);
Raph Levien416a8472012-07-19 22:48:17 -07001979
Chris Craik2bb8f562015-02-17 16:42:02 -08001980 const float sx = x - texture->left + textShadow.dx;
1981 const float sy = y - texture->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07001982
Chris Craik82840732015-04-03 09:37:49 -07001983 Glop glop;
1984 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07001985 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07001986 .setMeshTexturedUnitQuad(nullptr)
1987 .setFillShadowTexturePaint(*texture, textShadow.color, *paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07001988 .setTransform(*currentSnapshot(), TransformFlags::None)
John Reck38e0c322015-11-10 12:19:17 -08001989 .setModelViewMapUnitToRect(Rect(sx, sy, sx + texture->width(), sy + texture->height()))
Chris Craik82840732015-04-03 09:37:49 -07001990 .build();
1991 renderGlop(glop);
Raph Levien416a8472012-07-19 22:48:17 -07001992}
1993
Chris Craikbf6f0f22015-10-01 12:36:07 -07001994// TODO: remove this, once mState.currentlyIgnored captures snapshot alpha
Romain Guy768bffc2013-02-27 13:50:45 -08001995bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Chris Craikbf6f0f22015-10-01 12:36:07 -07001996 float alpha = (PaintUtils::hasTextShadow(paint)
1997 ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05001998 return MathUtils::isZero(alpha)
1999 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002000}
2001
Chris Craik59744b72014-07-01 17:56:52 -07002002bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002003 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002004 outMatrix->setIdentity();
2005 return false;
2006 } else if (CC_UNLIKELY(transform.isPerspective())) {
2007 outMatrix->setIdentity();
2008 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002009 }
Chris Craik59744b72014-07-01 17:56:52 -07002010
2011 /**
Chris Craika736cd92014-08-04 13:18:38 -07002012 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2013 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002014 */
2015 float sx, sy;
2016 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002017 outMatrix->setScale(
Chris Craikdf72b632015-06-30 17:56:13 -07002018 roundf(std::max(1.0f, sx)),
2019 roundf(std::max(1.0f, sy)));
Chris Craika736cd92014-08-04 13:18:38 -07002020 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002021}
2022
Tom Hudson984162f2014-10-10 13:38:16 -04002023int OpenGLRenderer::getSaveCount() const {
2024 return mState.getSaveCount();
2025}
2026
2027int OpenGLRenderer::save(int flags) {
2028 return mState.save(flags);
2029}
2030
2031void OpenGLRenderer::restore() {
Chris Craike2bb3802015-03-13 15:07:52 -07002032 mState.restore();
Tom Hudson984162f2014-10-10 13:38:16 -04002033}
2034
2035void OpenGLRenderer::restoreToCount(int saveCount) {
Chris Craike2bb3802015-03-13 15:07:52 -07002036 mState.restoreToCount(saveCount);
Tom Hudson984162f2014-10-10 13:38:16 -04002037}
2038
Tom Hudsonac7b6d32015-06-30 11:26:13 -04002039
Tom Hudson984162f2014-10-10 13:38:16 -04002040void OpenGLRenderer::translate(float dx, float dy, float dz) {
Chris Craike2bb3802015-03-13 15:07:52 -07002041 mState.translate(dx, dy, dz);
Tom Hudson984162f2014-10-10 13:38:16 -04002042}
2043
2044void OpenGLRenderer::rotate(float degrees) {
Chris Craike2bb3802015-03-13 15:07:52 -07002045 mState.rotate(degrees);
Tom Hudson984162f2014-10-10 13:38:16 -04002046}
2047
2048void OpenGLRenderer::scale(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002049 mState.scale(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002050}
2051
2052void OpenGLRenderer::skew(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002053 mState.skew(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002054}
2055
Chris Craik6daa13c2015-08-19 13:32:12 -07002056void OpenGLRenderer::setLocalMatrix(const Matrix4& matrix) {
2057 mState.setMatrix(mBaseTransform);
2058 mState.concatMatrix(matrix);
Tom Hudson984162f2014-10-10 13:38:16 -04002059}
2060
Tom Hudsonac7b6d32015-06-30 11:26:13 -04002061void OpenGLRenderer::setLocalMatrix(const SkMatrix& matrix) {
2062 mState.setMatrix(mBaseTransform);
2063 mState.concatMatrix(matrix);
2064}
2065
Tom Hudson984162f2014-10-10 13:38:16 -04002066void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2067 mState.concatMatrix(matrix);
2068}
2069
2070bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2071 return mState.clipRect(left, top, right, bottom, op);
2072}
2073
2074bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2075 return mState.clipPath(path, op);
2076}
2077
2078bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2079 return mState.clipRegion(region, op);
2080}
2081
2082void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2083 mState.setClippingOutline(allocator, outline);
2084}
2085
2086void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2087 const Rect& rect, float radius, bool highPriority) {
2088 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2089}
2090
Chris Craikfca52b752015-04-28 11:45:59 -07002091void OpenGLRenderer::setProjectionPathMask(LinearAllocator& allocator, const SkPath* path) {
2092 mState.setProjectionPathMask(allocator, path);
2093}
2094
Chris Craike8c3c812016-02-05 20:10:50 -08002095void OpenGLRenderer::drawText(const glyph_t* glyphs, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002096 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002097 DrawOpMode drawOpMode) {
2098
Chris Craik03ae2722015-02-25 11:01:09 -08002099 if (drawOpMode == DrawOpMode::kImmediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002100 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2101 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike8c3c812016-02-05 20:10:50 -08002102 if (glyphs == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002103 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002104 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002105 }
Romain Guycac5fd32011-12-01 20:08:50 -08002106 }
2107
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002108 const float oldX = x;
2109 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002110
Chris Craikd6b65f62014-01-01 14:45:21 -08002111 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002112 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002113
Romain Guy211370f2012-02-01 16:10:55 -08002114 if (CC_LIKELY(pureTranslate)) {
Chris Craika6b52192015-02-27 17:43:02 -08002115 x = floorf(x + transform.getTranslateX() + 0.5f);
2116 y = floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002117 }
2118
Chris Craikbf6f0f22015-10-01 12:36:07 -07002119 int alpha = PaintUtils::getAlphaDirect(paint) * currentSnapshot()->alpha;
2120 SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(paint);
Romain Guy9d13fe252010-10-15 16:06:03 -07002121
Chris Craikc08820f2015-09-22 14:22:29 -07002122 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
Romain Guyc74f45a2013-02-26 19:10:14 -08002123
Chris Craikbf6f0f22015-10-01 12:36:07 -07002124 if (CC_UNLIKELY(PaintUtils::hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002125 fontRenderer.setFont(paint, SkMatrix::I());
Chris Craike8c3c812016-02-05 20:10:50 -08002126 drawTextShadow(paint, glyphs, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002127 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002128 }
2129
Romain Guy19d4dd82013-03-04 11:14:26 -08002130 const bool hasActiveLayer = hasLayer();
2131
Romain Guy624234f2013-03-05 16:43:31 -08002132 // We only pass a partial transform to the font renderer. That partial
2133 // matrix defines how glyphs are rasterized. Typically we want glyphs
2134 // to be rasterized at their final size on screen, which means the partial
2135 // matrix needs to take the scale factor into account.
2136 // When a partial matrix is used to transform glyphs during rasterization,
2137 // the mesh is generated with the inverse transform (in the case of scale,
2138 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2139 // apply the full transform matrix at draw time in the vertex shader.
2140 // Applying the full matrix in the shader is the easiest way to handle
2141 // rotation and perspective and allows us to always generated quads in the
2142 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002143 SkMatrix fontTransform;
2144 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2145 || fabs(y - (int) y) > 0.0f
2146 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002147 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002148 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002149
Romain Guy624234f2013-03-05 16:43:31 -08002150 // TODO: Implement better clipping for scaled/rotated text
Chris Craik6fe991e52015-10-20 09:39:42 -07002151 const Rect* clip = !pureTranslate ? nullptr : &mState.currentRenderTargetClip();
Chris Craik41541822013-05-03 16:35:54 -07002152 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 -07002153
Raph Levien996e57c2012-07-23 15:22:52 -07002154 bool status;
Chris Craika1717272015-11-19 13:02:43 -08002155#if HWUI_NEW_OPS
2156 LOG_ALWAYS_FATAL("unsupported");
Chris Craik15c3f192015-12-03 12:16:56 -08002157 TextDrawFunctor functor(nullptr, nullptr, nullptr, x, y, pureTranslate, alpha, mode, paint);
Chris Craika1717272015-11-19 13:02:43 -08002158#else
Chris Craik82840732015-04-03 09:37:49 -07002159 TextDrawFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craika1717272015-11-19 13:02:43 -08002160#endif
Chris Craik527a3aa2013-03-04 10:19:31 -08002161
2162 // don't call issuedrawcommand, do it at end of batch
Chris Craik03ae2722015-02-25 11:01:09 -08002163 bool forceFinish = (drawOpMode != DrawOpMode::kDefer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002164 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002165 SkPaint paintCopy(*paint);
2166 paintCopy.setTextAlign(SkPaint::kLeft_Align);
Chris Craike8c3c812016-02-05 20:10:50 -08002167 status = fontRenderer.renderPosText(&paintCopy, clip, glyphs, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002168 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002169 } else {
Chris Craike8c3c812016-02-05 20:10:50 -08002170 status = fontRenderer.renderPosText(paint, clip, glyphs, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002171 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002172 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002173
Chris Craik03ae2722015-02-25 11:01:09 -08002174 if ((status || drawOpMode != DrawOpMode::kImmediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002175 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002176 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002177 }
Chris Craik41541822013-05-03 16:35:54 -07002178 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002179 }
Romain Guy694b5192010-07-21 21:33:20 -07002180
Tom Hudson107843d2014-09-08 11:26:26 -04002181 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002182}
2183
Chris Craike8c3c812016-02-05 20:10:50 -08002184void OpenGLRenderer::drawTextOnPath(const glyph_t* glyphs, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002185 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike8c3c812016-02-05 20:10:50 -08002186 if (glyphs == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002187 return;
Romain Guy03d58522012-02-24 17:54:07 -08002188 }
2189
Chris Craik39a908c2013-06-13 14:39:01 -07002190 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002191 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002192
Chris Craikc08820f2015-09-22 14:22:29 -07002193 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
Chris Craik59744b72014-07-01 17:56:52 -07002194 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002195 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002196
Chris Craikbf6f0f22015-10-01 12:36:07 -07002197 int alpha = PaintUtils::getAlphaDirect(paint) * currentSnapshot()->alpha;
2198 SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(paint);
Chris Craika1717272015-11-19 13:02:43 -08002199#if HWUI_NEW_OPS
2200 LOG_ALWAYS_FATAL("unsupported");
Chris Craik15c3f192015-12-03 12:16:56 -08002201 TextDrawFunctor functor(nullptr, nullptr, nullptr, 0.0f, 0.0f, false, alpha, mode, paint);
Chris Craika1717272015-11-19 13:02:43 -08002202#else
Chris Craik82840732015-04-03 09:37:49 -07002203 TextDrawFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Chris Craika1717272015-11-19 13:02:43 -08002204#endif
Romain Guy03d58522012-02-24 17:54:07 -08002205
Tom Hudson984162f2014-10-10 13:38:16 -04002206 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002207 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 -08002208
Chris Craike8c3c812016-02-05 20:10:50 -08002209 if (fontRenderer.renderTextOnPath(paint, clip, glyphs, count, path,
Chih-Hung Hsieh9ad6ac02015-02-27 16:11:36 -08002210 hOffset, vOffset, hasLayer() ? &bounds : nullptr, &functor)) {
Chris Craik083e7332015-02-27 17:04:20 -08002211 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2212 mDirty = true;
Romain Guy97771732012-02-28 18:17:02 -08002213 }
Romain Guy325740f2012-02-24 16:48:34 -08002214}
2215
Tom Hudson107843d2014-09-08 11:26:26 -04002216void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002217 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002218
Chris Craik44eb2c02015-01-29 09:45:09 -08002219 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002220
Chris Craik30036092015-02-12 10:41:39 -08002221 PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002222 if (!texture) return;
Romain Guy7fbcc042010-08-04 15:40:07 -07002223
Romain Guy8b55f372010-08-18 17:10:07 -07002224 const float x = texture->left - texture->offset;
2225 const float y = texture->top - texture->offset;
2226
Romain Guy01d58e42011-01-19 21:54:02 -08002227 drawPathTexture(texture, x, y, paint);
Digish Pandya2e4f67c2015-11-04 11:00:28 +05302228
2229 if (texture->cleanup) {
2230 mCaches.pathCache.remove(path, paint);
2231 }
Tom Hudson107843d2014-09-08 11:26:26 -04002232 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002233}
2234
Chris Craik3aadd602015-08-20 12:41:40 -07002235void OpenGLRenderer::drawLayer(Layer* layer) {
Romain Guy35643dd2012-09-18 15:40:58 -07002236 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002237 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002238 }
2239
Chris Craike84a2082014-12-22 14:28:49 -08002240 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002241 if (layer->isTextureLayer()) {
2242 transform = &layer->getTransform();
2243 if (!transform->isIdentity()) {
Florin Malitaeecff562015-12-21 10:43:01 -05002244 save(SaveFlags::Matrix);
Chris Craik14e51302013-12-30 15:32:54 -08002245 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002246 }
2247 }
2248
Chris Craik39a908c2013-06-13 14:39:01 -07002249 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08002250 const bool rejected = mState.calculateQuickRejectForScissor(
Chris Craik3aadd602015-08-20 12:41:40 -07002251 0, 0, layer->layer.getWidth(), layer->layer.getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -08002252 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002253
2254 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002255 if (transform && !transform->isIdentity()) {
2256 restore();
2257 }
Tom Hudson107843d2014-09-08 11:26:26 -04002258 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002259 }
2260
Chris Craik62d307c2014-07-29 10:35:13 -07002261 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2262 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2263
Romain Guy5bb3c732012-11-29 17:52:58 -08002264 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002265
Chris Craik65fe5ee2015-01-26 18:06:29 -08002266 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08002267 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002268
Romain Guy211370f2012-02-01 16:10:55 -08002269 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002270 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002271 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2272 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002273 } else if (layer->mesh) {
Chris Craik82840732015-04-03 09:37:49 -07002274 Glop glop;
2275 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002276 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002277 .setMeshTexturedIndexedQuads(layer->mesh, layer->meshElementCount)
2278 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
Chris Craik53e51e42015-06-01 10:35:35 -07002279 .setTransform(*currentSnapshot(), TransformFlags::None)
Chris Craik5430ab22015-12-10 16:28:16 -08002280 .setModelViewOffsetRectSnap(0, 0, Rect(layer->layer.getWidth(), layer->layer.getHeight()))
Chris Craik82840732015-04-03 09:37:49 -07002281 .build();
2282 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy3a3133d2011-02-01 22:59:58 -08002283#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07002284 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08002285#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002286 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002287
Romain Guy5bb3c732012-11-29 17:52:58 -08002288 if (layer->debugDrawUpdate) {
2289 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002290
2291 SkPaint paint;
2292 paint.setColor(0x7f00ff00);
Chris Craik3aadd602015-08-20 12:41:40 -07002293 drawColorRect(0, 0, layer->layer.getWidth(), layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002294 }
Romain Guyf219da52011-01-16 12:54:25 -08002295 }
Chris Craik34416ea2013-04-15 16:08:28 -07002296 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07002297
Romain Guyb2e2f242012-10-17 18:18:35 -07002298 if (transform && !transform->isIdentity()) {
2299 restore();
2300 }
2301
Tom Hudson107843d2014-09-08 11:26:26 -04002302 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08002303}
2304
Romain Guy6926c722010-07-12 20:20:03 -07002305///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002306// Draw filters
2307///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04002308void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
2309 // We should never get here since we apply the draw filter when stashing
2310 // the paints in the DisplayList.
2311 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08002312}
2313
2314///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002315// Drawing implementation
2316///////////////////////////////////////////////////////////////////////////////
2317
Chris Craikd218a922014-01-02 17:13:34 -08002318Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
Chris Craik15c3f192015-12-03 12:16:56 -08002319 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap->pixelRef());
Romain Guy3b748a42013-04-17 18:54:38 -07002320 if (!texture) {
2321 return mCaches.textureCache.get(bitmap);
2322 }
2323 return texture;
2324}
2325
Chris Craik2bb8f562015-02-17 16:42:02 -08002326void OpenGLRenderer::drawPathTexture(PathTexture* texture, float x, float y,
2327 const SkPaint* paint) {
John Reck38e0c322015-11-10 12:19:17 -08002328 if (quickRejectSetupScissor(x, y, x + texture->width(), y + texture->height())) {
Romain Guy01d58e42011-01-19 21:54:02 -08002329 return;
2330 }
2331
Chris Craik82840732015-04-03 09:37:49 -07002332 Glop glop;
2333 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002334 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002335 .setMeshTexturedUnitQuad(nullptr)
2336 .setFillPathTexturePaint(*texture, *paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002337 .setTransform(*currentSnapshot(), TransformFlags::None)
John Reck38e0c322015-11-10 12:19:17 -08002338 .setModelViewMapUnitToRect(Rect(x, y, x + texture->width(), y + texture->height()))
Chris Craik82840732015-04-03 09:37:49 -07002339 .build();
2340 renderGlop(glop);
Romain Guy01d58e42011-01-19 21:54:02 -08002341}
2342
Tom Hudson107843d2014-09-08 11:26:26 -04002343void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002344 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002345 return;
Romain Guy672433d2013-01-04 19:05:13 -08002346 }
2347
Tom Hudson107843d2014-09-08 11:26:26 -04002348 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08002349}
2350
Tom Hudson107843d2014-09-08 11:26:26 -04002351void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07002352 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04002353 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07002354
Chris Craik15a07a22014-01-26 13:43:53 -08002355 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08002356 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07002357
2358 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08002359 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07002360
ztenghui14a4e352014-08-13 10:44:39 -07002361 // The caller has made sure casterAlpha > 0.
2362 float ambientShadowAlpha = mAmbientShadowAlpha;
Chris Craik2507c342015-05-04 14:36:49 -07002363 if (CC_UNLIKELY(Properties::overrideAmbientShadowStrength >= 0)) {
2364 ambientShadowAlpha = Properties::overrideAmbientShadowStrength;
ztenghui14a4e352014-08-13 10:44:39 -07002365 }
2366 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
2367 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002368 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002369 }
ztenghui7b4516e2014-01-07 10:42:55 -08002370
ztenghui14a4e352014-08-13 10:44:39 -07002371 float spotShadowAlpha = mSpotShadowAlpha;
Chris Craik2507c342015-05-04 14:36:49 -07002372 if (CC_UNLIKELY(Properties::overrideSpotShadowStrength >= 0)) {
2373 spotShadowAlpha = Properties::overrideSpotShadowStrength;
ztenghui14a4e352014-08-13 10:44:39 -07002374 }
2375 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
2376 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002377 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002378 }
ztenghui7b4516e2014-01-07 10:42:55 -08002379
Tom Hudson107843d2014-09-08 11:26:26 -04002380 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07002381}
2382
Tom Hudson107843d2014-09-08 11:26:26 -04002383void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002384 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08002385 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04002386 return;
Romain Guy3b753822013-03-05 10:27:35 -08002387 }
Romain Guy735738c2012-12-03 12:34:51 -08002388
Romain Guy672433d2013-01-04 19:05:13 -08002389 float left = FLT_MAX;
2390 float top = FLT_MAX;
2391 float right = FLT_MIN;
2392 float bottom = FLT_MIN;
2393
Romain Guy448455f2013-07-22 13:57:50 -07002394 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08002395 Vertex* vertex = mesh;
2396
Chris Craik2af46352012-11-26 18:30:17 -08002397 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08002398 float l = rects[index + 0];
2399 float t = rects[index + 1];
2400 float r = rects[index + 2];
2401 float b = rects[index + 3];
2402
Romain Guy3b753822013-03-05 10:27:35 -08002403 Vertex::set(vertex++, l, t);
2404 Vertex::set(vertex++, r, t);
2405 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08002406 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08002407
Chris Craikdf72b632015-06-30 17:56:13 -07002408 left = std::min(left, l);
2409 top = std::min(top, t);
2410 right = std::max(right, r);
2411 bottom = std::max(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08002412 }
2413
Chris Craikf0a59072013-11-19 18:00:46 -08002414 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002415 return;
Romain Guya362c692013-02-04 13:50:16 -08002416 }
Romain Guy672433d2013-01-04 19:05:13 -08002417
Chris Craik53e51e42015-06-01 10:35:35 -07002418 const int transformFlags = ignoreTransform
2419 ? TransformFlags::MeshIgnoresCanvasTransform : TransformFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07002420 Glop glop;
2421 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002422 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002423 .setMeshIndexedQuads(&mesh[0], count / 4)
2424 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002425 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07002426 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07002427 .build();
2428 renderGlop(glop);
Romain Guy672433d2013-01-04 19:05:13 -08002429}
2430
Romain Guy026c5e162010-06-28 17:12:22 -07002431void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002432 const SkPaint* paint, bool ignoreTransform) {
Chris Craik53e51e42015-06-01 10:35:35 -07002433 const int transformFlags = ignoreTransform
2434 ? TransformFlags::MeshIgnoresCanvasTransform : TransformFlags::None;
Chris Craik82840732015-04-03 09:37:49 -07002435 Glop glop;
2436 GlopBuilder(mRenderState, mCaches, &glop)
Chris Craikb1f990d2015-06-12 11:28:52 -07002437 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik82840732015-04-03 09:37:49 -07002438 .setMeshUnitQuad()
2439 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craik53e51e42015-06-01 10:35:35 -07002440 .setTransform(*currentSnapshot(), transformFlags)
Chris Craik82840732015-04-03 09:37:49 -07002441 .setModelViewMapUnitToRect(Rect(left, top, right, bottom))
Chris Craik82840732015-04-03 09:37:49 -07002442 .build();
2443 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07002444}
2445
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002446float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik8df5ffa2015-04-28 17:47:20 -07002447 return (layer->getAlpha() / 255.0f) * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07002448}
2449
Romain Guy9d5316e2010-06-24 19:30:36 -07002450}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002451}; // namespace android