blob: 54bcd7e43d1d5f207f23615f560376536694db79 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chris Craik65fe5ee2015-01-26 18:06:29 -080017#include "OpenGLRenderer.h"
18
19#include "DeferredDisplayList.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080020#include "GammaFontRenderer.h"
Chris Craik03188872015-02-02 18:39:33 -080021#include "Glop.h"
22#include "GlopBuilder.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080023#include "Patch.h"
24#include "PathTessellator.h"
25#include "Properties.h"
26#include "RenderNode.h"
Chris Craik03188872015-02-02 18:39:33 -080027#include "renderstate/MeshState.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080028#include "renderstate/RenderState.h"
29#include "ShadowTessellator.h"
30#include "SkiaShader.h"
31#include "Vector.h"
32#include "VertexBuffer.h"
33#include "utils/GLUtils.h"
34#include "utils/PaintUtils.h"
35#include "utils/TraceUtils.h"
36
Romain Guye4d01122010-06-16 18:44:05 -070037#include <stdlib.h>
38#include <stdint.h>
39#include <sys/types.h>
40
Romain Guy5cbbce52010-06-27 22:59:20 -070041#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070042#include <SkColor.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040043#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070044#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070045
Romain Guye4d01122010-06-16 18:44:05 -070046#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070047#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070048
Romain Guy08aa2cb2011-03-17 11:06:57 -070049#include <private/hwui/DrawGlInfo.h>
50
Romain Guy5b3b3522010-10-27 18:57:51 -070051#include <ui/Rect.h>
52
Chris Craik62d307c2014-07-29 10:35:13 -070053#if DEBUG_DETAILED_EVENTS
54 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
55#else
56 #define EVENT_LOGD(...)
57#endif
58
Romain Guye4d01122010-06-16 18:44:05 -070059namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070060namespace uirenderer {
61
Romain Guy9d5316e2010-06-24 19:30:36 -070062///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -070063// Constructors/destructor
64///////////////////////////////////////////////////////////////////////////////
65
John Reck3b202512014-06-23 13:13:08 -070066OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -040067 : mState(*this)
Chris Craik058fc642014-07-23 18:19:28 -070068 , mCaches(Caches::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -070069 , mRenderState(renderState)
Chris Craik65fe5ee2015-01-26 18:06:29 -080070 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -070071 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -070072 , mSuppressTiling(false)
73 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -040074 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -070075 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -070076 , mLightRadius(FLT_MIN)
77 , mAmbientShadowAlpha(0)
78 , mSpotShadowAlpha(0) {
Romain Guye4d01122010-06-16 18:44:05 -070079}
80
Romain Guy85bf02f2010-06-22 13:11:24 -070081OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -070082 // The context has already been destroyed at this point, do not call
83 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -070084}
85
Romain Guy87e2f7572012-09-24 11:37:12 -070086void OpenGLRenderer::initProperties() {
87 char property[PROPERTY_VALUE_MAX];
88 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
89 mScissorOptimizationDisabled = !strcasecmp(property, "true");
90 INIT_LOGD(" Scissor optimization %s",
91 mScissorOptimizationDisabled ? "disabled" : "enabled");
92 } else {
93 INIT_LOGD(" Scissor optimization enabled");
94 }
Romain Guy13631f32012-01-30 17:41:55 -080095}
96
Alan Viverette50210d92015-05-14 18:05:36 -070097void OpenGLRenderer::initLight(float lightRadius, uint8_t ambientShadowAlpha,
98 uint8_t spotShadowAlpha) {
Chris Craik058fc642014-07-23 18:19:28 -070099 mLightRadius = lightRadius;
100 mAmbientShadowAlpha = ambientShadowAlpha;
101 mSpotShadowAlpha = spotShadowAlpha;
102}
103
Alan Viverette50210d92015-05-14 18:05:36 -0700104void OpenGLRenderer::setLightCenter(const Vector3& lightCenter) {
105 mLightCenter = lightCenter;
106}
107
Romain Guy13631f32012-01-30 17:41:55 -0800108///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700109// Setup
110///////////////////////////////////////////////////////////////////////////////
111
Chris Craik797b95b2014-05-20 18:10:25 -0700112void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700113 glDisable(GL_DITHER);
114 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Chris Craik284b2432014-09-18 16:05:35 -0700115 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700116}
117
Romain Guy96885eb2013-03-26 15:05:58 -0700118void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800119 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800120 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400121 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700122 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700123 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700124}
125
Tom Hudson107843d2014-09-08 11:26:26 -0400126void OpenGLRenderer::startFrame() {
127 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700128 mFrameStarted = true;
129
Tom Hudson984162f2014-10-10 13:38:16 -0400130 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700131
Romain Guy96885eb2013-03-26 15:05:58 -0700132 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700133
Tom Hudson984162f2014-10-10 13:38:16 -0400134 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800135
Romain Guy54c1a642012-09-27 17:55:46 -0700136 // Functors break the tiling extension in pretty spectacular ways
137 // This ensures we don't use tiling when a functor is going to be
138 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700139 mSuppressTiling = mCaches.hasRegisteredFunctors()
140 || mFirstFrameAfterResize;
141 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700142
Chris Craikd6b65f62014-01-01 14:45:21 -0800143 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700144
Romain Guy7c450aa2012-09-21 19:15:00 -0700145 debugOverdraw(true, true);
146
Tom Hudson107843d2014-09-08 11:26:26 -0400147 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700148 mTilingClip.right, mTilingClip.bottom, mOpaque);
149}
150
Tom Hudson107843d2014-09-08 11:26:26 -0400151void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700152 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700153
Romain Guy96885eb2013-03-26 15:05:58 -0700154 setupFrameState(left, top, right, bottom, opaque);
155
156 // Layer renderers will start the frame immediately
157 // The framebuffer renderer will first defer the display list
158 // for each layer and wait until the first drawing command
159 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800160 if (currentSnapshot()->fbo == 0) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800161 mRenderState.blend().syncEnabled();
Romain Guy96885eb2013-03-26 15:05:58 -0700162 updateLayers();
163 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400164 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700165 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700166}
167
Romain Guydcfc8362013-01-03 13:08:57 -0800168void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
169 // If we know that we are going to redraw the entire framebuffer,
170 // perform a discard to let the driver know we don't need to preserve
171 // the back buffer for this frame.
Chris Craik117bdbc2015-02-05 10:12:38 -0800172 if (mCaches.extensions().hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400173 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
Chris Craik6b109c72015-02-27 10:55:28 -0800174 const bool isFbo = getTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800175 const GLenum attachments[] = {
176 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
177 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800178 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
179 }
180}
181
Tom Hudson107843d2014-09-08 11:26:26 -0400182void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700183 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800184 mRenderState.scissor().setEnabled(true);
185 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700186 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400187 mDirty = true;
188 return;
Romain Guyddf74372012-05-22 14:07:07 -0700189 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700190
Chris Craik65fe5ee2015-01-26 18:06:29 -0800191 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700192}
193
henry.uh_chen33f5a592014-07-02 19:36:56 +0800194void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700195 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800196 const Snapshot* snapshot = currentSnapshot();
197
Chris Craik14e51302013-12-30 15:32:54 -0800198 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800199 if (snapshot->flags & Snapshot::kFlagFboTarget) {
200 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700201 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700202
henry.uh_chen33f5a592014-07-02 19:36:56 +0800203 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800204 }
205}
206
henry.uh_chen33f5a592014-07-02 19:36:56 +0800207void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800208 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800209 if(expand) {
210 // Expand the startTiling region by 1
211 int leftNotZero = (clip.left > 0) ? 1 : 0;
212 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
213
214 mCaches.startTiling(
215 clip.left - leftNotZero,
216 windowHeight - clip.bottom - topNotZero,
217 clip.right - clip.left + leftNotZero + 1,
218 clip.bottom - clip.top + topNotZero + 1,
219 opaque);
220 } else {
221 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800222 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800223 }
Romain Guy54c1a642012-09-27 17:55:46 -0700224 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700225}
226
227void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700228 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700229}
230
Tom Hudson107843d2014-09-08 11:26:26 -0400231bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700232 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700233 endTiling();
Chris Craik4ac36f82014-12-09 16:54:03 -0800234 mTempPaths.clear();
235
Romain Guyca89e2a2013-03-08 17:44:20 -0800236 // When finish() is invoked on FBO 0 we've reached the end
237 // of the current frame
Chris Craik6b109c72015-02-27 10:55:28 -0800238 if (getTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800239 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700240 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800241 }
242
Romain Guy11cb6422012-09-21 00:39:43 -0700243 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700244#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700245 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700246#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700247
Romain Guyc15008e2010-11-10 11:59:15 -0800248#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800249 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700250#else
Chris Craik2507c342015-05-04 14:36:49 -0700251 if (Properties::debugLevel & kDebugMemory) {
Romain Guy11cb6422012-09-21 00:39:43 -0700252 mCaches.dumpMemoryUsage();
253 }
Romain Guyc15008e2010-11-10 11:59:15 -0800254#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700255 }
Romain Guy96885eb2013-03-26 15:05:58 -0700256
257 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400258
259 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700260}
261
Romain Guy35643dd2012-09-18 15:40:58 -0700262void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700263 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
264 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700265 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700266
Chris Craik65fe5ee2015-01-26 18:06:29 -0800267 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700268 dirtyClip();
269}
270
Andreas Gampe64bb4132014-11-22 00:35:09 +0000271void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400272 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700273
Rob Tsuk487a92c2015-01-06 13:22:54 -0800274 Rect clip(mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700275 clip.snapToPixelBoundaries();
276
Romain Guyd643bb52011-03-01 14:55:21 -0800277 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800278 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800279 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800280 dirtyLayerUnchecked(clip, getRegion());
281 }
Romain Guyd643bb52011-03-01 14:55:21 -0800282
Romain Guy08aa2cb2011-03-17 11:06:57 -0700283 DrawGlInfo info;
284 info.clipLeft = clip.left;
285 info.clipTop = clip.top;
286 info.clipRight = clip.right;
287 info.clipBottom = clip.bottom;
288 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700289 info.width = getViewportWidth();
290 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800291 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700292
Tom Hudson984162f2014-10-10 13:38:16 -0400293 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700294 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400295 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700296 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
297 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800298 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700299 setScissorFromClip();
300 }
Chris Craik54f574a2013-08-26 11:23:46 -0700301
John Reck3b202512014-06-23 13:13:08 -0700302 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
303 // Scissor may have been modified, reset dirty clip
304 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800305
Tom Hudson107843d2014-09-08 11:26:26 -0400306 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800307}
308
Romain Guyf6a11b82010-06-23 17:47:49 -0700309///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700310// Debug
311///////////////////////////////////////////////////////////////////////////////
312
Chris Craik62d307c2014-07-29 10:35:13 -0700313void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
314#if DEBUG_DETAILED_EVENTS
315 const int BUFFER_SIZE = 256;
316 va_list ap;
317 char buf[BUFFER_SIZE];
318
319 va_start(ap, fmt);
320 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
321 va_end(ap);
322
323 eventMark(buf);
324#endif
325}
326
327
Romain Guy0f6675332013-03-01 14:31:04 -0800328void OpenGLRenderer::eventMark(const char* name) const {
329 mCaches.eventMark(0, name);
330}
331
Romain Guy87e2f7572012-09-24 11:37:12 -0700332void OpenGLRenderer::startMark(const char* name) const {
333 mCaches.startMark(0, name);
334}
335
336void OpenGLRenderer::endMark() const {
337 mCaches.endMark();
338}
339
340void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700341 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700342}
343
344void OpenGLRenderer::renderOverdraw() {
Chris Craik2507c342015-05-04 14:36:49 -0700345 if (Properties::debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700346 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700347
Chris Craik65fe5ee2015-01-26 18:06:29 -0800348 mRenderState.scissor().setEnabled(true);
349 mRenderState.scissor().set(clip->left,
350 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
351 clip->right - clip->left,
352 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700353
Romain Guy627c6fd2013-08-21 11:53:18 -0700354 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800355 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700356 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
357
358 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800359 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700360 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
361
362 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800363 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700364 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
365
366 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800367 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700368 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
369
Chris Craik96a5c4c2015-01-27 15:46:35 -0800370 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700371 }
372}
373
374///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700375// Layers
376///////////////////////////////////////////////////////////////////////////////
377
378bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700379 if (layer->deferredUpdateScheduled && layer->renderer
380 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700381
Romain Guy7c450aa2012-09-21 19:15:00 -0700382 if (inFrame) {
383 endTiling();
384 debugOverdraw(false, false);
385 }
Romain Guy11cb6422012-09-21 00:39:43 -0700386
Chris Craik2507c342015-05-04 14:36:49 -0700387 if (CC_UNLIKELY(inFrame || Properties::drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700388 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700389 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700390 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700391 }
Romain Guy11cb6422012-09-21 00:39:43 -0700392
393 if (inFrame) {
394 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800395 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700396 }
397
Chris Craik2507c342015-05-04 14:36:49 -0700398 layer->debugDrawUpdate = Properties::debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700399 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700400
401 return true;
402 }
403
404 return false;
405}
406
407void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700408 // If draw deferring is enabled this method will simply defer
409 // the display list of each individual layer. The layers remain
410 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700411 int count = mLayerUpdates.size();
412 if (count > 0) {
Chris Craik2507c342015-05-04 14:36:49 -0700413 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -0700414 startMark("Layer Updates");
415 } else {
416 startMark("Defer Layer Updates");
417 }
Romain Guy11cb6422012-09-21 00:39:43 -0700418
Chris Craik1206b9b2013-04-04 14:46:24 -0700419 // Note: it is very important to update the layers in order
420 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700421 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700422 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700423 }
Romain Guy11cb6422012-09-21 00:39:43 -0700424
Chris Craik2507c342015-05-04 14:36:49 -0700425 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -0700426 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800427 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700428 }
429 endMark();
430 }
431}
432
433void OpenGLRenderer::flushLayers() {
434 int count = mLayerUpdates.size();
435 if (count > 0) {
436 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700437
Chris Craik1206b9b2013-04-04 14:46:24 -0700438 // Note: it is very important to update the layers in order
439 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800440 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700441 }
442
443 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800444 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700445
Romain Guy11cb6422012-09-21 00:39:43 -0700446 endMark();
447 }
448}
449
450void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
451 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700452 // Make sure we don't introduce duplicates.
453 // SortedVector would do this automatically but we need to respect
454 // the insertion order. The linear search is not an issue since
455 // this list is usually very short (typically one item, at most a few)
456 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
457 if (mLayerUpdates.itemAt(i) == layer) {
458 return;
459 }
460 }
Romain Guy11cb6422012-09-21 00:39:43 -0700461 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700462 }
463}
464
Romain Guye93482f2013-06-17 13:14:51 -0700465void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
466 if (layer) {
467 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
468 if (mLayerUpdates.itemAt(i) == layer) {
469 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700470 break;
471 }
472 }
473 }
474}
475
Romain Guy40543602013-06-12 15:31:28 -0700476void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800477 ATRACE_NAME("Update HW Layers");
Chris Craik44eb2c02015-01-29 09:45:09 -0800478 mRenderState.blend().syncEnabled();
Romain Guy40543602013-06-12 15:31:28 -0700479 updateLayers();
480 flushLayers();
481 // Wait for all the layer updates to be executed
John Reck55156372015-01-21 07:46:37 -0800482 glFinish();
Romain Guy40543602013-06-12 15:31:28 -0700483}
484
John Reck443a7142014-09-04 17:40:05 -0700485void OpenGLRenderer::markLayersAsBuildLayers() {
486 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
487 mLayerUpdates[i]->wasBuildLayered = true;
488 }
489}
490
Romain Guy11cb6422012-09-21 00:39:43 -0700491///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700492// State management
493///////////////////////////////////////////////////////////////////////////////
494
Chris Craik14e51302013-12-30 15:32:54 -0800495void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700496 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800497 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
498 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700499
Chris Craika64a2be2014-05-14 14:17:01 -0700500 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700501 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700502 }
503
Romain Guy2542d192010-08-18 11:47:12 -0700504 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700505 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700506 }
Romain Guy2542d192010-08-18 11:47:12 -0700507
Romain Guy5ec99242010-11-03 16:19:08 -0700508 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700509 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700510 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700511 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800512 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700513 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700514 }
Romain Guybb9524b2010-06-22 18:56:38 -0700515}
516
Romain Guyf6a11b82010-06-23 17:47:49 -0700517///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700518// Layers
519///////////////////////////////////////////////////////////////////////////////
520
521int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700522 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700523 // force matrix/clip isolation for layer
524 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
525
Tom Hudson984162f2014-10-10 13:38:16 -0400526 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700527
Tom Hudson984162f2014-10-10 13:38:16 -0400528 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700529 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700530 }
Romain Guyd55a8612010-06-28 17:42:46 -0700531
532 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700533}
534
Chris Craikd90144d2013-03-19 15:03:48 -0700535void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
536 const Rect untransformedBounds(bounds);
537
Chris Craikd6b65f62014-01-01 14:45:21 -0800538 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700539
540 // Layers only make sense if they are in the framebuffer's bounds
Rob Tsuk487a92c2015-01-06 13:22:54 -0800541 if (bounds.intersect(mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700542 // We cannot work with sub-pixels in this case
543 bounds.snapToPixelBoundaries();
544
545 // When the layer is not an FBO, we may use glCopyTexImage so we
546 // need to make sure the layer does not extend outside the bounds
547 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700548 const Snapshot& previous = *(currentSnapshot()->previous);
549 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
550 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700551 bounds.setEmpty();
552 } else if (fboLayer) {
553 clip.set(bounds);
554 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800555 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700556 inverse.mapRect(clip);
557 clip.snapToPixelBoundaries();
558 if (clip.intersect(untransformedBounds)) {
559 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
560 bounds.set(untransformedBounds);
561 } else {
562 clip.setEmpty();
563 }
564 }
565 } else {
566 bounds.setEmpty();
567 }
568}
569
Chris Craik408eb122013-03-26 18:55:15 -0700570void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
571 bool fboLayer, int alpha) {
572 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
573 bounds.getHeight() > mCaches.maxTextureSize ||
574 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400575 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700576 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400577 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700578 }
579}
580
Chris Craikd90144d2013-03-19 15:03:48 -0700581int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500582 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400583 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700584
Tom Hudson984162f2014-10-10 13:38:16 -0400585 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700586 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
587 // operations will be able to store and restore the current clip and transform info, and
588 // quick rejection will be correct (for display lists)
589
590 Rect bounds(left, top, right, bottom);
591 Rect clip;
592 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500593 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700594
Tom Hudson984162f2014-10-10 13:38:16 -0400595 if (!mState.currentlyIgnored()) {
596 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
597 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
598 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800599 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700600 }
601 }
602
603 return count;
604}
605
Romain Guy1c740bc2010-09-13 18:00:09 -0700606/**
607 * Layers are viewed by Skia are slightly different than layers in image editing
608 * programs (for instance.) When a layer is created, previously created layers
609 * and the frame buffer still receive every drawing command. For instance, if a
610 * layer is created and a shape intersecting the bounds of the layers and the
611 * framebuffer is draw, the shape will be drawn on both (unless the layer was
612 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
613 *
614 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
615 * texture. Unfortunately, this is inefficient as it requires every primitive to
616 * be drawn n + 1 times, where n is the number of active layers. In practice this
617 * means, for every primitive:
618 * - Switch active frame buffer
619 * - Change viewport, clip and projection matrix
620 * - Issue the drawing
621 *
622 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700623 * To avoid this, layers are implemented in a different way here, at least in the
624 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
625 * is set. When this flag is set we can redirect all drawing operations into a
626 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700627 *
628 * This implementation relies on the frame buffer being at least RGBA 8888. When
629 * a layer is created, only a texture is created, not an FBO. The content of the
630 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700631 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700632 * buffer and drawing continues as normal. This technique therefore treats the
633 * frame buffer as a scratch buffer for the layers.
634 *
635 * To compose the layers back onto the frame buffer, each layer texture
636 * (containing the original frame buffer data) is drawn as a simple quad over
637 * the frame buffer. The trick is that the quad is set as the composition
638 * destination in the blending equation, and the frame buffer becomes the source
639 * of the composition.
640 *
641 * Drawing layers with an alpha value requires an extra step before composition.
642 * An empty quad is drawn over the layer's region in the frame buffer. This quad
643 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
644 * quad is used to multiply the colors in the frame buffer. This is achieved by
645 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
646 * GL_ZERO, GL_SRC_ALPHA.
647 *
648 * Because glCopyTexImage2D() can be slow, an alternative implementation might
649 * be use to draw a single clipped layer. The implementation described above
650 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700651 *
652 * (1) The frame buffer is actually not cleared right away. To allow the GPU
653 * to potentially optimize series of calls to glCopyTexImage2D, the frame
654 * buffer is left untouched until the first drawing operation. Only when
655 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700656 */
Chet Haased48885a2012-08-28 17:43:28 -0700657bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700658 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800659 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
660 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700661
Romain Guyeb993562010-10-05 18:14:38 -0700662 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
663
Romain Guyf607bdc2010-09-10 19:20:06 -0700664 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700665 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700666 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700667 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000668 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700669
670 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400671 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700672 return false;
673 }
Romain Guyf18fd992010-07-08 11:45:51 -0700674
Chris Craik44eb2c02015-01-29 09:45:09 -0800675 mCaches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700676 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700677 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700678 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700679 }
680
Derek Sollenberger674554f2014-02-19 16:47:32 +0000681 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700682 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700683 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
684 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500685
Chet Haasea23eed82012-04-12 15:19:04 -0700686 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700687 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700688 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700689
Romain Guy8fb95422010-08-17 18:38:51 -0700690 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400691 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
692 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700693
Chris Craika8bea8e2014-09-24 11:29:43 -0700694 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
695 fboLayer ? "" : "unclipped ",
696 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700697 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700698 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700699 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700700 } else {
701 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700702 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800703 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700704 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700705 // Workaround for some GL drivers. When reading pixels lying outside
706 // of the window we should get undefined values for those pixels.
707 // Unfortunately some drivers will turn the entire target texture black
708 // when reading outside of the window.
709 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800710 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700711 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800712 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700713
Chris Craika64a2be2014-05-14 14:17:01 -0700714 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
715 bounds.left, getViewportHeight() - bounds.bottom,
716 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700717
Romain Guy54be1cd2011-06-13 19:04:27 -0700718 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800719 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700720 }
Romain Guyeb993562010-10-05 18:14:38 -0700721 }
Romain Guyf86ef572010-07-01 11:05:42 -0700722
Romain Guyd55a8612010-06-28 17:42:46 -0700723 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700724}
725
Chris Craike63f7c622013-10-17 10:30:55 -0700726bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800727 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700728 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700729
Tom Hudson984162f2014-10-10 13:38:16 -0400730 writableSnapshot()->region = &writableSnapshot()->layer->region;
731 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
732 writableSnapshot()->fbo = layer->getFbo();
733 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
734 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
735 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800736 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700737
Romain Guy2b7028e2012-09-19 17:25:38 -0700738 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700739 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700740 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700741 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700742 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700743
744 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700745 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700746 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700747 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700748 }
749
750 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Chris Craikf27133d2015-02-19 09:51:53 -0800751 layer->getTextureId(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700752
henry.uh_chen33f5a592014-07-02 19:36:56 +0800753 // Expand the startTiling region by 1
754 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700755
756 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800757 mRenderState.scissor().setEnabled(true);
758 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700759 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700760 glClear(GL_COLOR_BUFFER_BIT);
761
762 dirtyClip();
763
764 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700765 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700766 return true;
767}
768
Romain Guy1c740bc2010-09-13 18:00:09 -0700769/**
770 * Read the documentation of createLayer() before doing anything in this method.
771 */
Chris Craik14e51302013-12-30 15:32:54 -0800772void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
773 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000774 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700775 return;
776 }
777
Chris Craik14e51302013-12-30 15:32:54 -0800778 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800779 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800780 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700781
Chris Craik39a908c2013-06-13 14:39:01 -0700782 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400783 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800784 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800785 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700786
Romain Guyeb993562010-10-05 18:14:38 -0700787 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700788 endTiling();
789
Romain Guye0aa84b2012-04-03 19:30:26 -0700790 // Detach the texture from the FBO
791 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800792
793 layer->removeFbo(false);
794
Romain Guyeb993562010-10-05 18:14:38 -0700795 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700796 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700797 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700798
Chris Craikd6b65f62014-01-01 14:45:21 -0800799 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700800 }
801
Romain Guy9ace8f52011-07-07 20:50:11 -0700802 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500803 SkPaint layerPaint;
804 layerPaint.setAlpha(layer->getAlpha());
805 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
806 layerPaint.setColorFilter(layer->getColorFilter());
807
808 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700809 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700810 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700811 }
812
Chris Craik96a5c4c2015-01-27 15:46:35 -0800813 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700814
Chris Craik44eb2c02015-01-29 09:45:09 -0800815 mCaches.textureState().activateTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700816
Romain Guy5b3b3522010-10-27 18:57:51 -0700817 // When the layer is stored in an FBO, we can save a bit of fillrate by
818 // drawing only the dirty region
819 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800820 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700821 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700822 } else if (!rect.isEmpty()) {
823 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530824
825 save(0);
826 // the layer contains screen buffer content that shouldn't be alpha modulated
827 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400828 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700829 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530830 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700831 }
Romain Guy8b55f372010-08-18 17:10:07 -0700832
Romain Guy746b7402010-10-26 16:27:31 -0700833 dirtyClip();
834
Romain Guyeb993562010-10-05 18:14:38 -0700835 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800836 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700837 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800838 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800839 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700840 }
841}
842
Romain Guyaa6c24c2011-04-28 18:40:04 -0700843void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craik82840732015-04-03 09:37:49 -0700844 bool snap = !layer->getForceFilter()
Chris Craik26bf3422015-02-26 16:28:17 -0800845 && layer->getWidth() == (uint32_t) rect.getWidth()
Chris Craik82840732015-04-03 09:37:49 -0700846 && layer->getHeight() == (uint32_t) rect.getHeight();
847 Glop glop;
848 GlopBuilder(mRenderState, mCaches, &glop)
849 .setMeshTexturedUvQuad(nullptr, Rect(0, 1, 1, 0)) // TODO: simplify with VBO
850 .setFillTextureLayer(*layer, getLayerAlpha(layer))
851 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
852 .setModelViewMapUnitToRectOptionalSnap(snap, rect)
853 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
854 .build();
855 renderGlop(glop);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700856}
857
Romain Guy5b3b3522010-10-27 18:57:51 -0700858void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700859 if (layer->isTextureLayer()) {
860 EVENT_LOGD("composeTextureLayerRect");
Chris Craik62d307c2014-07-29 10:35:13 -0700861 drawTextureLayer(layer, rect);
Chris Craik62d307c2014-07-29 10:35:13 -0700862 } else {
863 EVENT_LOGD("composeHardwareLayerRect");
Chris Craik182952f2015-03-09 14:17:29 -0700864
Chris Craik82840732015-04-03 09:37:49 -0700865 Blend::ModeOrderSwap modeUsage = swap ?
866 Blend::ModeOrderSwap::Swap : Blend::ModeOrderSwap::NoSwap;
867 const Matrix4& transform = swap ? Matrix4::identity() : *currentTransform();
868 bool snap = !swap
869 && layer->getWidth() == static_cast<uint32_t>(rect.getWidth())
870 && layer->getHeight() == static_cast<uint32_t>(rect.getHeight());
871 Glop glop;
872 GlopBuilder(mRenderState, mCaches, &glop)
873 .setMeshTexturedUvQuad(nullptr, layer->texCoords)
874 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), modeUsage)
875 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
876 .setModelViewMapUnitToRectOptionalSnap(snap, rect)
877 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
878 .build();
879 renderGlop(glop);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700880 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700881}
882
Chris Craik34416ea2013-04-15 16:08:28 -0700883/**
884 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
885 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
886 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
887 * by saveLayer's restore
888 */
Chris Craik2507c342015-05-04 14:36:49 -0700889#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
890 DRAW_COMMAND; \
891 if (CC_UNLIKELY(Properties::debugOverdraw && getTargetFbo() == 0 && COND)) { \
892 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
893 DRAW_COMMAND; \
894 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
895 } \
Chris Craik34416ea2013-04-15 16:08:28 -0700896 }
897
898#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
899
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400900// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
901// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
902class LayerShader : public SkShader {
903public:
904 LayerShader(Layer* layer, const SkMatrix* localMatrix)
905 : INHERITED(localMatrix)
906 , mLayer(layer) {
907 }
908
Chris Craike84a2082014-12-22 14:28:49 -0800909 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400910 if (data) {
911 *data = static_cast<void*>(mLayer);
912 }
913 return true;
914 }
915
Chris Craike84a2082014-12-22 14:28:49 -0800916 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400917 return !mLayer->isBlend();
918 }
919
920protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000921 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400922 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
923 }
924
Chris Craike84a2082014-12-22 14:28:49 -0800925 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400926 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
927 }
928
Chris Craike84a2082014-12-22 14:28:49 -0800929 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400930 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -0800931 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400932 }
933private:
934 // Unowned.
935 Layer* mLayer;
936 typedef SkShader INHERITED;
937};
938
Romain Guy5b3b3522010-10-27 18:57:51 -0700939void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -0700940 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
941
942 if (layer->getConvexMask()) {
943 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
944
945 // clip to the area of the layer the mask can be larger
946 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
947
948 SkPaint paint;
949 paint.setAntiAlias(true);
950 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
951
Chris Craik3f0854292014-04-15 16:18:08 -0700952 // create LayerShader to map SaveLayer content into subsequent draw
953 SkMatrix shaderMatrix;
954 shaderMatrix.setTranslate(rect.left, rect.bottom);
955 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400956 LayerShader layerShader(layer, &shaderMatrix);
957 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -0700958
959 // Since the drawing primitive is defined in local drawing space,
960 // we don't need to modify the draw matrix
961 const SkPath* maskPath = layer->getConvexMask();
962 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
963
Chris Craike84a2082014-12-22 14:28:49 -0800964 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -0700965 restore();
966
967 return;
968 }
969
Romain Guy5b3b3522010-10-27 18:57:51 -0700970 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700971 layer->setRegionAsRect();
972
Chris Craik34416ea2013-04-15 16:08:28 -0700973 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -0700974
Romain Guy5b3b3522010-10-27 18:57:51 -0700975 layer->region.clear();
976 return;
977 }
978
Chris Craik62d307c2014-07-29 10:35:13 -0700979 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -0700980 // standard Region based draw
981 size_t count;
982 const android::Rect* rects;
983 Region safeRegion;
984 if (CC_LIKELY(hasRectToRectTransform())) {
985 rects = layer->region.getArray(&count);
986 } else {
987 safeRegion = Region::createTJunctionFreeRegion(layer->region);
988 rects = safeRegion.getArray(&count);
989 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700990
Chris Craik3f0854292014-04-15 16:18:08 -0700991 const float texX = 1.0f / float(layer->getWidth());
992 const float texY = 1.0f / float(layer->getHeight());
993 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700994
Chris Craik82840732015-04-03 09:37:49 -0700995 TextureVertex quadVertices[count * 4];
996 TextureVertex* mesh = &quadVertices[0];
Chris Craik3f0854292014-04-15 16:18:08 -0700997 for (size_t i = 0; i < count; i++) {
998 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -0700999
Chris Craik3f0854292014-04-15 16:18:08 -07001000 const float u1 = r->left * texX;
1001 const float v1 = (height - r->top) * texY;
1002 const float u2 = r->right * texX;
1003 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001004
Chris Craik3f0854292014-04-15 16:18:08 -07001005 // TODO: Reject quads outside of the clip
1006 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1007 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1008 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1009 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Chris Craik3f0854292014-04-15 16:18:08 -07001010 }
Tom Hudson040b6d82015-04-16 10:50:53 -04001011 Rect modelRect = Rect(rect.getWidth(), rect.getHeight());
Chris Craik82840732015-04-03 09:37:49 -07001012 Glop glop;
1013 GlopBuilder(mRenderState, mCaches, &glop)
1014 .setMeshTexturedIndexedQuads(&quadVertices[0], count * 6)
1015 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
1016 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
Tom Hudson040b6d82015-04-16 10:50:53 -04001017 .setModelViewOffsetRectSnap(rect.left, rect.top, modelRect)
Chris Craik82840732015-04-03 09:37:49 -07001018 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1019 .build();
1020 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy5b3b3522010-10-27 18:57:51 -07001021
Romain Guy5b3b3522010-10-27 18:57:51 -07001022#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001023 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001024#endif
1025
Chris Craik3f0854292014-04-15 16:18:08 -07001026 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001027}
1028
Romain Guy3a3133d2011-02-01 22:59:58 -08001029#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001030void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001031 size_t count;
1032 const android::Rect* rects = region.getArray(&count);
1033
1034 uint32_t colors[] = {
1035 0x7fff0000, 0x7f00ff00,
1036 0x7f0000ff, 0x7fff00ff,
1037 };
1038
1039 int offset = 0;
1040 int32_t top = rects[0].top;
1041
1042 for (size_t i = 0; i < count; i++) {
1043 if (top != rects[i].top) {
1044 offset ^= 0x2;
1045 top = rects[i].top;
1046 }
1047
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001048 SkPaint paint;
1049 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001050 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001051 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001052 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001053}
Chris Craike63f7c622013-10-17 10:30:55 -07001054#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001055
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001056void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001057 Vector<float> rects;
1058
1059 SkRegion::Iterator it(region);
1060 while (!it.done()) {
1061 const SkIRect& r = it.rect();
1062 rects.push(r.fLeft);
1063 rects.push(r.fTop);
1064 rects.push(r.fRight);
1065 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001066 it.next();
1067 }
1068
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001069 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001070}
1071
Romain Guy5b3b3522010-10-27 18:57:51 -07001072void OpenGLRenderer::dirtyLayer(const float left, const float top,
Chris Craik083e7332015-02-27 17:04:20 -08001073 const float right, const float bottom, const Matrix4& transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001074 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001075 Rect bounds(left, top, right, bottom);
1076 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001077 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001078 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001079}
1080
1081void OpenGLRenderer::dirtyLayer(const float left, const float top,
1082 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001083 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001084 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001085 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001086 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001087}
1088
1089void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001090 if (bounds.intersect(mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001091 bounds.snapToPixelBoundaries();
1092 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1093 if (!dirty.isEmpty()) {
1094 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001095 }
1096 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001097}
1098
Romain Guy54be1cd2011-06-13 19:04:27 -07001099void OpenGLRenderer::clearLayerRegions() {
Chris Craik2bb8f562015-02-17 16:42:02 -08001100 const size_t quadCount = mLayers.size();
1101 if (quadCount == 0) return;
Romain Guy54be1cd2011-06-13 19:04:27 -07001102
Tom Hudson984162f2014-10-10 13:38:16 -04001103 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001104 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001105 // Doing several glScissor/glClear here can negatively impact
1106 // GPUs with a tiler architecture, instead we draw quads with
1107 // the Clear blending mode
1108
1109 // The list contains bounds that have already been clipped
1110 // against their initial clip rect, and the current clip
1111 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001112 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001113
Chris Craik2bb8f562015-02-17 16:42:02 -08001114 Vertex mesh[quadCount * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001115 Vertex* vertex = mesh;
1116
Chris Craik2bb8f562015-02-17 16:42:02 -08001117 for (uint32_t i = 0; i < quadCount; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001118 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001119
Chris Craik51d6a3d2014-12-22 17:16:56 -08001120 Vertex::set(vertex++, bounds.left, bounds.top);
1121 Vertex::set(vertex++, bounds.right, bounds.top);
1122 Vertex::set(vertex++, bounds.left, bounds.bottom);
1123 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001124 }
Romain Guye67307c2013-02-11 18:01:20 -08001125 // We must clear the list of dirty rects before we
Chris Craik82840732015-04-03 09:37:49 -07001126 // call clearLayerRegions() in renderGlop to prevent
1127 // stencil setup from doing the same thing again
Romain Guye67307c2013-02-11 18:01:20 -08001128 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001129
Chris Craik82840732015-04-03 09:37:49 -07001130 Glop glop;
1131 GlopBuilder(mRenderState, mCaches, &glop)
1132 .setMeshIndexedQuads(&mesh[0], quadCount)
1133 .setFillClear()
1134 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
1135 .setModelViewOffsetRect(0, 0, Rect(currentSnapshot()->getClipRect()))
1136 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1137 .build();
1138 renderGlop(glop, false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001139
Chris Craik65fe5ee2015-01-26 18:06:29 -08001140 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001141 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001142 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001143 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001144}
1145
Romain Guybd6b79b2010-06-26 00:13:53 -07001146///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001147// State Deferral
1148///////////////////////////////////////////////////////////////////////////////
1149
Chris Craikff785832013-03-08 13:12:16 -08001150bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001151 const Rect& currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001152 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001153
Chris Craikff785832013-03-08 13:12:16 -08001154 if (stateDeferFlags & kStateDeferFlag_Draw) {
1155 // state has bounds initialized in local coordinates
1156 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001157 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001158 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001159 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1160 // is used, it should more closely duplicate the quickReject logic (in how it uses
1161 // snapToPixelBoundaries)
1162
Rob Tsuk487a92c2015-01-06 13:22:54 -08001163 if (!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001164 // quick rejected
1165 return true;
1166 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001167
Chris Craika02c4ed2013-06-14 13:43:58 -07001168 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001169 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001170 int& flags = state.mClipSideFlags;
1171 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001172 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1173 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1174 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1175 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001176 }
1177 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001178 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001179 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1180 // overdraw avoidance (since we don't know what it overlaps)
1181 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001182 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001183 }
1184 }
1185
Chris Craik527a3aa2013-03-04 10:19:31 -08001186 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1187 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001188 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001189 }
1190
Chris Craik8df5ffa2015-04-28 17:47:20 -07001191 // Transform and alpha always deferred, since they are used by state operations
Chris Craik7273daa2013-03-28 11:25:24 -07001192 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001193 state.mMatrix.load(*currentMatrix);
Chris Craikd6b65f62014-01-01 14:45:21 -08001194 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001195
1196 // always store/restore, since it's just a pointer
1197 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001198 return false;
1199}
1200
Chris Craik527a3aa2013-03-04 10:19:31 -08001201void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001202 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001203 writableSnapshot()->alpha = state.mAlpha;
Tom Hudson984162f2014-10-10 13:38:16 -04001204 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001205
Chris Craik527a3aa2013-03-04 10:19:31 -08001206 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001207 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001208 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001209 dirtyClip();
1210 }
Chris Craikc3566d02013-02-04 16:16:33 -08001211}
1212
Chris Craik28ce94a2013-05-31 11:38:03 -07001213/**
1214 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1215 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1216 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1217 *
1218 * This method should be called when restoreDisplayState() won't be restoring the clip
1219 */
1220void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001221 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001222 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001223 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001224 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001225 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001226 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001227 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1228 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001229}
1230
Chris Craikc3566d02013-02-04 16:16:33 -08001231///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001232// Clipping
1233///////////////////////////////////////////////////////////////////////////////
1234
Romain Guybb9524b2010-06-22 18:56:38 -07001235void OpenGLRenderer::setScissorFromClip() {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001236 Rect clip(mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001237 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001238
Chris Craik65fe5ee2015-01-26 18:06:29 -08001239 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001240 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001241 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001242 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001243}
1244
Romain Guy8ce00302013-01-15 18:51:42 -08001245void OpenGLRenderer::ensureStencilBuffer() {
1246 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1247 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1248 // just hope we have one when hasLayer() returns false.
1249 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001250 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001251 }
1252}
1253
1254void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1255 // The layer's FBO is already bound when we reach this stage
1256 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001257 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1258 // is attached after we initiated tiling. We must turn it off,
1259 // attach the new render buffer then turn tiling back on
1260 endTiling();
1261
Romain Guy8d4aeb72013-02-12 16:08:55 -08001262 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Chris Craike1450132015-04-28 17:55:50 -07001263 Stencil::getLayerStencilFormat(),
Chris Craik117bdbc2015-02-05 10:12:38 -08001264 layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001265 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001266
Romain Guyf735c8e2013-01-31 17:45:55 -08001267 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001268 }
1269}
1270
Rob Tsuk487a92c2015-01-06 13:22:54 -08001271static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1272 float x, float y) {
1273 Vertex v;
1274 v.x = x;
1275 v.y = y;
1276 transform.mapPoint(v.x, v.y);
1277 rectangleVertices.push_back(v);
1278}
1279
1280static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1281 Vertex v;
1282 v.x = x;
1283 v.y = y;
1284 rectangleVertices.push_back(v);
1285}
1286
1287void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
Chris Craik2bb8f562015-02-17 16:42:02 -08001288 int quadCount = rectangleList.getTransformedRectanglesCount();
1289 std::vector<Vertex> rectangleVertices(quadCount * 4);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001290 Rect scissorBox = rectangleList.calculateBounds();
1291 scissorBox.snapToPixelBoundaries();
Chris Craik2bb8f562015-02-17 16:42:02 -08001292 for (int i = 0; i < quadCount; ++i) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001293 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1294 const Matrix4& transform = tr.getTransform();
1295 Rect bounds = tr.getBounds();
1296 if (transform.rectToRect()) {
1297 transform.mapRect(bounds);
1298 if (!bounds.intersect(scissorBox)) {
1299 bounds.setEmpty();
1300 } else {
1301 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1302 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1303 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1304 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1305 }
1306 } else {
1307 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1308 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1309 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1310 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1311 }
1312 }
1313
Chris Craik65fe5ee2015-01-26 18:06:29 -08001314 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001315 scissorBox.getWidth(), scissorBox.getHeight());
1316
Chris Craik82840732015-04-03 09:37:49 -07001317 Glop glop;
1318 Vertex* vertices = &rectangleVertices[0];
1319 GlopBuilder(mRenderState, mCaches, &glop)
1320 .setMeshIndexedQuads(vertices, rectangleVertices.size() / 4)
1321 .setFillBlack()
1322 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
1323 .setModelViewOffsetRect(0, 0, scissorBox)
1324 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1325 .build();
1326 renderGlop(glop);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001327}
1328
Romain Guy8ce00302013-01-15 18:51:42 -08001329void OpenGLRenderer::setStencilFromClip() {
Chris Craik2507c342015-05-04 14:36:49 -07001330 if (!Properties::debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001331 if (!currentSnapshot()->clipIsSimple()) {
1332 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001333 EVENT_LOGD("setStencilFromClip - enabling");
1334
Romain Guy8ce00302013-01-15 18:51:42 -08001335 // NOTE: The order here is important, we must set dirtyClip to false
1336 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001337 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001338
1339 ensureStencilBuffer();
1340
Rob Tsuk487a92c2015-01-06 13:22:54 -08001341 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001342
Rob Tsuk487a92c2015-01-06 13:22:54 -08001343 bool isRectangleList = clipArea.isRectangleList();
1344 if (isRectangleList) {
1345 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1346 } else {
1347 incrementThreshold = 0;
1348 }
1349
Chris Craik96a5c4c2015-01-27 15:46:35 -08001350 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001351
1352 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001353 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001354 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001355 if (resetScissor) {
1356 // The scissor was not set so we now need to update it
1357 setScissorFromClip();
1358 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001359
Chris Craik96a5c4c2015-01-27 15:46:35 -08001360 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001361
1362 // stash and disable the outline clip state, since stencil doesn't account for outline
1363 bool storedSkipOutlineClip = mSkipOutlineClip;
1364 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001365
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001366 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001367 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001368 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1369
Rob Tsuk487a92c2015-01-06 13:22:54 -08001370 if (isRectangleList) {
1371 drawRectangleList(clipArea.getRectangleList());
1372 } else {
1373 // NOTE: We could use the region contour path to generate a smaller mesh
1374 // Since we are using the stencil we could use the red book path
1375 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001376
Rob Tsuk487a92c2015-01-06 13:22:54 -08001377 // The last parameter is important: we are not drawing in the color buffer
1378 // so we don't want to dirty the current layer, if any
1379 drawRegionRects(clipArea.getClipRegion(), paint, false);
1380 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001381 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001382 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001383
Chris Craik96a5c4c2015-01-27 15:46:35 -08001384 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001385
1386 // Draw the region used to generate the stencil if the appropriate debug
1387 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001388 // TODO: Implement for rectangle list clip areas
Chris Craik2507c342015-05-04 14:36:49 -07001389 if (Properties::debugStencilClip == StencilClipDebug::ShowRegion
1390 && !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001391 paint.setColor(0x7f0000ff);
1392 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001393 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001394 }
Romain Guy8ce00302013-01-15 18:51:42 -08001395 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001396 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001397 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001398 }
1399 }
1400}
1401
Chris Craikf0a59072013-11-19 18:00:46 -08001402/**
1403 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1404 *
1405 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1406 * style, and tessellated AA ramp
1407 */
1408bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001409 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001410 bool snapOut = paint && paint->isAntiAlias();
1411
1412 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1413 float outset = paint->getStrokeWidth() * 0.5f;
1414 left -= outset;
1415 top -= outset;
1416 right += outset;
1417 bottom += outset;
1418 }
1419
Chris Craikdeeda3d2014-05-05 19:09:33 -07001420 bool clipRequired = false;
1421 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001422 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001423 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001424 return true;
1425 }
1426
Chris Craikf23b25a2014-06-26 15:46:20 -07001427 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001428 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001429 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001430 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001431}
1432
Romain Guy8ce00302013-01-15 18:51:42 -08001433void OpenGLRenderer::debugClip() {
1434#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001435 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001436 SkPaint paint;
1437 paint.setColor(0x7f00ff00);
1438 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1439
Romain Guy8ce00302013-01-15 18:51:42 -08001440 }
1441#endif
1442}
1443
Chris Craik6b109c72015-02-27 10:55:28 -08001444void OpenGLRenderer::renderGlop(const Glop& glop, bool clearLayer) {
1445 // TODO: It would be best if we could do this before quickRejectSetupScissor()
1446 // changes the scissor test state
1447 if (clearLayer) clearLayerRegions();
1448
Chris Craik117bdbc2015-02-05 10:12:38 -08001449 if (mState.getDirtyClip()) {
1450 if (mRenderState.scissor().isEnabled()) {
1451 setScissorFromClip();
1452 }
1453
1454 setStencilFromClip();
1455 }
1456 mRenderState.render(glop);
Chris Craik2ab95d72015-02-06 15:25:51 -08001457 if (!mRenderState.stencil().isWriteEnabled()) {
1458 // TODO: specify more clearly when a draw should dirty the layer.
1459 // is writing to the stencil the only time we should ignore this?
1460 dirtyLayer(glop.bounds.left, glop.bounds.top, glop.bounds.right, glop.bounds.bottom);
Chris Craik0519c812015-02-11 13:17:06 -08001461 mDirty = true;
Chris Craik2ab95d72015-02-06 15:25:51 -08001462 }
Chris Craik117bdbc2015-02-05 10:12:38 -08001463}
1464
Romain Guyf6a11b82010-06-23 17:47:49 -07001465///////////////////////////////////////////////////////////////////////////////
1466// Drawing
1467///////////////////////////////////////////////////////////////////////////////
1468
Tom Hudson107843d2014-09-08 11:26:26 -04001469void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001470 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1471 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001472 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001473 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001474 renderNode->computeOrdering();
Chris Craik2507c342015-05-04 14:36:49 -07001475 if (CC_UNLIKELY(Properties::drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001476 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001477 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001478 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001479 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001480 }
1481
Chris Craikef8d6f22014-12-17 11:10:28 -08001482 // Don't avoid overdraw when visualizing, since that makes it harder to
1483 // debug where it's coming from, and when the problem occurs.
Chris Craik2507c342015-05-04 14:36:49 -07001484 bool avoidOverdraw = !Properties::debugOverdraw;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001485 DeferredDisplayList deferredList(mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001486 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001487 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001488
1489 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001490 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001491
Tom Hudson107843d2014-09-08 11:26:26 -04001492 deferredList.flush(*this, dirty);
1493 } else {
1494 // Even if there is no drawing command(Ex: invisible),
1495 // it still needs startFrame to clear buffer and start tiling.
1496 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001497 }
1498}
1499
Romain Guy03c00b52013-06-20 18:30:28 -07001500/**
1501 * Important note: this method is intended to draw batches of bitmaps and
1502 * will not set the scissor enable or dirty the current layer, if any.
1503 * The caller is responsible for properly dirtying the current layer.
1504 */
Tom Hudson107843d2014-09-08 11:26:26 -04001505void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001506 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1507 const Rect& bounds, const SkPaint* paint) {
Romain Guy55b6f952013-06-27 15:27:09 -07001508 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001509 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001510
Chris Craik527a3aa2013-03-04 10:19:31 -08001511 const AutoTexture autoCleanup(texture);
1512
Chris Craik82840732015-04-03 09:37:49 -07001513 // TODO: remove layer dirty in multi-draw callers
1514 // TODO: snap doesn't need to touch transform, only texture filter.
1515 bool snap = pureTranslate;
Chris Craika6b52192015-02-27 17:43:02 -08001516 const float x = floorf(bounds.left + 0.5f);
1517 const float y = floorf(bounds.top + 0.5f);
Chris Craik82840732015-04-03 09:37:49 -07001518 int textureFillFlags = static_cast<int>((bitmap->colorType() == kAlpha_8_SkColorType)
1519 ? TextureFillFlags::kIsAlphaMaskTexture : TextureFillFlags::kNone);
1520 Glop glop;
1521 GlopBuilder(mRenderState, mCaches, &glop)
1522 .setMeshTexturedMesh(vertices, bitmapCount * 6)
1523 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1524 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
1525 .setModelViewOffsetRectOptionalSnap(snap, x, y, Rect(0, 0, bounds.getWidth(), bounds.getHeight()))
1526 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1527 .build();
1528 renderGlop(glop);
Chris Craik527a3aa2013-03-04 10:19:31 -08001529}
1530
Tom Hudson107843d2014-09-08 11:26:26 -04001531void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07001532 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04001533 return;
Romain Guy6926c722010-07-12 20:20:03 -07001534 }
1535
Chris Craik44eb2c02015-01-29 09:45:09 -08001536 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07001537 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001538 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001539 const AutoTexture autoCleanup(texture);
1540
Chris Craik82840732015-04-03 09:37:49 -07001541 int textureFillFlags = static_cast<int>((bitmap->colorType() == kAlpha_8_SkColorType)
1542 ? TextureFillFlags::kIsAlphaMaskTexture : TextureFillFlags::kNone);
1543 Glop glop;
1544 GlopBuilder(mRenderState, mCaches, &glop)
1545 .setMeshTexturedUnitQuad(texture->uvMapper)
1546 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1547 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
1548 .setModelViewMapUnitToRectSnap(Rect(0, 0, texture->width, texture->height))
1549 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1550 .build();
1551 renderGlop(glop);
Romain Guyce0537b2010-06-29 21:05:21 -07001552}
1553
Tom Hudson107843d2014-09-08 11:26:26 -04001554void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08001555 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001556 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04001557 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08001558 }
1559
Romain Guyb18d2d02011-02-10 15:52:54 -08001560 float left = FLT_MAX;
1561 float top = FLT_MAX;
1562 float right = FLT_MIN;
1563 float bottom = FLT_MIN;
1564
Chris Craikef250742015-02-25 17:16:16 -08001565 const uint32_t elementCount = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001566
Chris Craikef250742015-02-25 17:16:16 -08001567 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[elementCount]);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001568 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08001569
Chris Craik51d6a3d2014-12-22 17:16:56 -08001570 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08001571 if (!colors) {
1572 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001573 tempColors.reset(new int[colorsCount]);
1574 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
1575 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08001576 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001577
John Reckebd52612014-12-10 16:47:36 -08001578 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07001579 const UvMapper& mapper(getMapper(texture));
1580
Romain Guy5a7b4662011-01-20 19:09:30 -08001581 for (int32_t y = 0; y < meshHeight; y++) {
1582 for (int32_t x = 0; x < meshWidth; x++) {
1583 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1584
1585 float u1 = float(x) / meshWidth;
1586 float u2 = float(x + 1) / meshWidth;
1587 float v1 = float(y) / meshHeight;
1588 float v2 = float(y + 1) / meshHeight;
1589
Romain Guy3b748a42013-04-17 18:54:38 -07001590 mapper.map(u1, v1, u2, v2);
1591
Romain Guy5a7b4662011-01-20 19:09:30 -08001592 int ax = i + (meshWidth + 1) * 2;
1593 int ay = ax + 1;
1594 int bx = i;
1595 int by = bx + 1;
1596 int cx = i + 2;
1597 int cy = cx + 1;
1598 int dx = i + (meshWidth + 1) * 2 + 2;
1599 int dy = dx + 1;
1600
Romain Guyff316ec2013-02-13 18:39:43 -08001601 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1602 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
1603 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08001604
Romain Guyff316ec2013-02-13 18:39:43 -08001605 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1606 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
1607 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08001608
Romain Guya92bb4d2012-10-16 11:08:44 -07001609 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1610 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1611 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1612 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001613 }
1614 }
1615
Chris Craikf0a59072013-11-19 18:00:46 -08001616 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001617 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07001618 }
1619
Romain Guyff316ec2013-02-13 18:39:43 -08001620 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07001621 texture = mCaches.textureCache.get(bitmap);
1622 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04001623 return;
Romain Guy3b748a42013-04-17 18:54:38 -07001624 }
Romain Guyff316ec2013-02-13 18:39:43 -08001625 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001626 const AutoTexture autoCleanup(texture);
1627
Chris Craik82840732015-04-03 09:37:49 -07001628 /*
1629 * TODO: handle alpha_8 textures correctly by applying paint color, but *not*
1630 * shader in that case to mimic the behavior in SkiaCanvas::drawBitmapMesh.
1631 */
1632 Glop glop;
1633 GlopBuilder(mRenderState, mCaches, &glop)
1634 .setMeshColoredTexturedMesh(mesh.get(), elementCount)
1635 .setFillTexturePaint(*texture, static_cast<int>(TextureFillFlags::kNone), paint, currentSnapshot()->alpha)
1636 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
1637 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
1638 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1639 .build();
1640 renderGlop(glop);
Romain Guy5a7b4662011-01-20 19:09:30 -08001641}
1642
Chris Craik14100ac2015-02-24 13:46:29 -08001643void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, Rect src, Rect dst, const SkPaint* paint) {
1644 if (quickRejectSetupScissor(dst)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001645 return;
Romain Guy6926c722010-07-12 20:20:03 -07001646 }
1647
Romain Guy3b748a42013-04-17 18:54:38 -07001648 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001649 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001650 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001651
Chris Craik82840732015-04-03 09:37:49 -07001652 Rect uv(fmax(0.0f, src.left / texture->width),
1653 fmax(0.0f, src.top / texture->height),
1654 fmin(1.0f, src.right / texture->width),
1655 fmin(1.0f, src.bottom / texture->height));
Chris Craik14100ac2015-02-24 13:46:29 -08001656
Chris Craik82840732015-04-03 09:37:49 -07001657 int textureFillFlags = static_cast<int>((bitmap->colorType() == kAlpha_8_SkColorType)
1658 ? TextureFillFlags::kIsAlphaMaskTexture : TextureFillFlags::kNone);
1659 Glop glop;
1660 GlopBuilder(mRenderState, mCaches, &glop)
1661 .setMeshTexturedUvQuad(texture->uvMapper, uv)
1662 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1663 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
1664 .setModelViewMapUnitToRectSnap(dst)
1665 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1666 .build();
1667 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07001668}
1669
Tom Hudson107843d2014-09-08 11:26:26 -04001670void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08001671 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
1672 const SkPaint* paint) {
Chris Craika6b52192015-02-27 17:43:02 -08001673 if (!mesh || !mesh->verticesCount || quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001674 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07001675 }
1676
Chris Craika6b52192015-02-27 17:43:02 -08001677 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
1678 if (!texture) return;
Chris Craik0556d902015-03-03 09:54:14 -08001679
Chris Craik82840732015-04-03 09:37:49 -07001680 // 9 patches are built for stretching - always filter
1681 int textureFillFlags = static_cast<int>(TextureFillFlags::kForceFilter);
1682 if (bitmap->colorType() == kAlpha_8_SkColorType) {
1683 textureFillFlags |= TextureFillFlags::kIsAlphaMaskTexture;
Chris Craik0556d902015-03-03 09:54:14 -08001684 }
Chris Craik82840732015-04-03 09:37:49 -07001685 Glop glop;
1686 GlopBuilder(mRenderState, mCaches, &glop)
1687 .setMeshPatchQuads(*mesh)
1688 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1689 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
1690 .setModelViewOffsetRectSnap(left, top, Rect(0, 0, right - left, bottom - top)) // TODO: get minimal bounds from patch
1691 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1692 .build();
1693 renderGlop(glop);
Romain Guyf7f93552010-07-08 19:17:03 -07001694}
1695
Romain Guy03c00b52013-06-20 18:30:28 -07001696/**
1697 * Important note: this method is intended to draw batches of 9-patch objects and
1698 * will not set the scissor enable or dirty the current layer, if any.
1699 * The caller is responsible for properly dirtying the current layer.
1700 */
Tom Hudson107843d2014-09-08 11:26:26 -04001701void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craika6b52192015-02-27 17:43:02 -08001702 TextureVertex* vertices, uint32_t elementCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001703 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07001704 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001705 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07001706 const AutoTexture autoCleanup(texture);
1707
Chris Craik82840732015-04-03 09:37:49 -07001708 // TODO: get correct bounds from caller
1709 // 9 patches are built for stretching - always filter
1710 int textureFillFlags = static_cast<int>(TextureFillFlags::kForceFilter);
1711 if (bitmap->colorType() == kAlpha_8_SkColorType) {
1712 textureFillFlags |= TextureFillFlags::kIsAlphaMaskTexture;
Chris Craika6b52192015-02-27 17:43:02 -08001713 }
Chris Craik82840732015-04-03 09:37:49 -07001714 Glop glop;
1715 GlopBuilder(mRenderState, mCaches, &glop)
1716 .setMeshTexturedIndexedQuads(vertices, elementCount)
1717 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1718 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
1719 .setModelViewOffsetRect(0, 0, Rect(0, 0, 0, 0))
1720 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1721 .build();
1722 renderGlop(glop);
Romain Guy03c00b52013-06-20 18:30:28 -07001723}
1724
Tom Hudson107843d2014-09-08 11:26:26 -04001725void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07001726 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001727 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07001728 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08001729 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04001730 return;
Chris Craik65cd6122012-12-10 17:56:27 -08001731 }
1732
Chris Craik82840732015-04-03 09:37:49 -07001733 bool fudgeOffset = displayFlags & kVertexBuffer_Offset;
1734 bool shadowInterp = displayFlags & kVertexBuffer_ShadowInterp;
1735 Glop glop;
1736 GlopBuilder(mRenderState, mCaches, &glop)
1737 .setMeshVertexBuffer(vertexBuffer, shadowInterp)
1738 .setFillPaint(*paint, currentSnapshot()->alpha)
1739 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), fudgeOffset)
1740 .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds())
1741 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1742 .build();
1743 renderGlop(glop);
Chet Haase858aa932011-05-12 09:06:00 -07001744}
1745
1746/**
Chris Craik65cd6122012-12-10 17:56:27 -08001747 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
1748 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
1749 * screen space in all directions. However, instead of using a fragment shader to compute the
1750 * translucency of the color from its position, we simply use a varying parameter to define how far
1751 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
1752 *
1753 * Doesn't yet support joins, caps, or path effects.
1754 */
Tom Hudson107843d2014-09-08 11:26:26 -04001755void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08001756 VertexBuffer vertexBuffer;
1757 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08001758 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04001759 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08001760}
1761
1762/**
1763 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
1764 * and additional geometry for defining an alpha slope perimeter.
1765 *
1766 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
1767 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
1768 * in-shader alpha region, but found it to be taxing on some GPUs.
1769 *
1770 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
1771 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07001772 */
Tom Hudson107843d2014-09-08 11:26:26 -04001773void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001774 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07001775
Chris Craik65cd6122012-12-10 17:56:27 -08001776 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07001777
Chris Craik65cd6122012-12-10 17:56:27 -08001778 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001779 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
1780 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08001781
Chris Craik05f3d6e2014-06-02 16:27:04 -07001782 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001783 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08001784 }
1785
Chris Craikbf759452014-08-11 16:00:44 -07001786 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001787 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07001788}
1789
Tom Hudson107843d2014-09-08 11:26:26 -04001790void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001791 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001792
Chris Craik6d29c8d2013-05-08 18:35:44 -07001793 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07001794
Chris Craik6d29c8d2013-05-08 18:35:44 -07001795 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001796 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08001797
Chris Craik05f3d6e2014-06-02 16:27:04 -07001798 const Rect& bounds = buffer.getBounds();
1799 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001800 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001801 }
1802
Chris Craikbf759452014-08-11 16:00:44 -07001803 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001804 drawVertexBuffer(buffer, paint, displayFlags);
1805
1806 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07001807}
1808
Tom Hudson107843d2014-09-08 11:26:26 -04001809void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001810 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04001811 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001812
Rob Tsuk487a92c2015-01-06 13:22:54 -08001813 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07001814 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001815
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001816 SkPaint paint;
1817 paint.setColor(color);
1818 paint.setXfermodeMode(mode);
1819
1820 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07001821
Tom Hudson107843d2014-09-08 11:26:26 -04001822 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07001823}
Romain Guy9d5316e2010-06-24 19:30:36 -07001824
Chris Craik30036092015-02-12 10:41:39 -08001825void OpenGLRenderer::drawShape(float left, float top, PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08001826 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04001827 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001828 const AutoTexture autoCleanup(texture);
1829
1830 const float x = left + texture->left - texture->offset;
1831 const float y = top + texture->top - texture->offset;
1832
1833 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07001834
Tom Hudson107843d2014-09-08 11:26:26 -04001835 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08001836}
1837
Tom Hudson107843d2014-09-08 11:26:26 -04001838void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001839 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001840 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001841 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001842 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001843 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001844 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001845
Chris Craike84a2082014-12-22 14:28:49 -08001846 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001847 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001848 PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07001849 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001850 drawShape(left, top, texture, p);
1851 } else {
1852 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
1853 *currentTransform(), *p, right - left, bottom - top, rx, ry);
1854 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001855 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001856}
1857
Tom Hudson107843d2014-09-08 11:26:26 -04001858void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001859 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001860 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001861 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001862 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001863 }
Chris Craike84a2082014-12-22 14:28:49 -08001864 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001865 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001866 PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001867 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07001868 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04001869 SkPath path;
1870 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1871 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
1872 } else {
1873 path.addCircle(x, y, radius);
1874 }
1875 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07001876 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001877}
Romain Guy01d58e42011-01-19 21:54:02 -08001878
Tom Hudson107843d2014-09-08 11:26:26 -04001879void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001880 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001881 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001882 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001883 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001884 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001885 }
Romain Guy01d58e42011-01-19 21:54:02 -08001886
Chris Craike84a2082014-12-22 14:28:49 -08001887 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001888 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001889 PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001890 drawShape(left, top, texture, p);
1891 } else {
1892 SkPath path;
1893 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1894 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1895 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1896 }
1897 path.addOval(rect);
1898 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001899 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001900}
1901
Tom Hudson107843d2014-09-08 11:26:26 -04001902void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001903 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001904 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001905 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001906 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001907 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08001908 }
1909
Chris Craik780c1282012-10-04 14:10:49 -07001910 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08001911 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001912 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001913 PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07001914 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001915 drawShape(left, top, texture, p);
1916 return;
Chris Craik780c1282012-10-04 14:10:49 -07001917 }
Chris Craik780c1282012-10-04 14:10:49 -07001918 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1919 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1920 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1921 }
1922
1923 SkPath path;
1924 if (useCenter) {
1925 path.moveTo(rect.centerX(), rect.centerY());
1926 }
1927 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
1928 if (useCenter) {
1929 path.close();
1930 }
Tom Hudson107843d2014-09-08 11:26:26 -04001931 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08001932}
1933
Romain Guycf8675e2012-10-02 12:32:25 -07001934// See SkPaintDefaults.h
1935#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
1936
Tom Hudson107843d2014-09-08 11:26:26 -04001937void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001938 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001939 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001940 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001941 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001942 return;
Romain Guy6926c722010-07-12 20:20:03 -07001943 }
1944
Chris Craik710f46d2012-09-17 17:25:49 -07001945 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07001946 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08001947 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07001948 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001949 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001950 PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07001951 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001952 drawShape(left, top, texture, p);
1953 } else {
1954 SkPath path;
1955 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1956 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1957 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1958 }
1959 path.addRect(rect);
1960 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07001961 }
Chet Haase858aa932011-05-12 09:06:00 -07001962 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04001963 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
1964 SkPath path;
1965 path.addRect(left, top, right, bottom);
1966 drawConvexPath(path, p);
1967 } else {
1968 drawColorRect(left, top, right, bottom, p);
1969
1970 mDirty = true;
1971 }
Chet Haase858aa932011-05-12 09:06:00 -07001972 }
Romain Guyc7d53492010-06-25 13:41:57 -07001973}
Romain Guy9d5316e2010-06-24 19:30:36 -07001974
Chris Craikd218a922014-01-02 17:13:34 -08001975void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
1976 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001977 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001978 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07001979
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04001980 TextShadow textShadow;
1981 if (!getTextShadow(paint, &textShadow)) {
1982 LOG_ALWAYS_FATAL("failed to query shadow attributes");
1983 }
1984
Raph Levien416a8472012-07-19 22:48:17 -07001985 // NOTE: The drop shadow will not perform gamma correction
1986 // if shader-based correction is enabled
1987 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Chris Craik2bb8f562015-02-17 16:42:02 -08001988 ShadowTexture* texture = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04001989 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07001990 // If the drop shadow exceeds the max texture size or couldn't be
1991 // allocated, skip drawing
Chris Craik2bb8f562015-02-17 16:42:02 -08001992 if (!texture) return;
1993 const AutoTexture autoCleanup(texture);
Raph Levien416a8472012-07-19 22:48:17 -07001994
Chris Craik2bb8f562015-02-17 16:42:02 -08001995 const float sx = x - texture->left + textShadow.dx;
1996 const float sy = y - texture->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07001997
Chris Craik82840732015-04-03 09:37:49 -07001998 Glop glop;
1999 GlopBuilder(mRenderState, mCaches, &glop)
2000 .setMeshTexturedUnitQuad(nullptr)
2001 .setFillShadowTexturePaint(*texture, textShadow.color, *paint, currentSnapshot()->alpha)
2002 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
2003 .setModelViewMapUnitToRect(Rect(sx, sy, sx + texture->width, sy + texture->height))
2004 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2005 .build();
2006 renderGlop(glop);
Raph Levien416a8472012-07-19 22:48:17 -07002007}
2008
Romain Guy768bffc2013-02-27 13:50:45 -08002009bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002010 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05002011 return MathUtils::isZero(alpha)
2012 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002013}
2014
Tom Hudson107843d2014-09-08 11:26:26 -04002015void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002016 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002017 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002018 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002019 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002020
Romain Guy671d6cf2012-01-18 12:39:17 -08002021 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002022 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002023 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002024 }
2025
Chris Craik65fe5ee2015-01-26 18:06:29 -08002026 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002027
Romain Guy671d6cf2012-01-18 12:39:17 -08002028 float x = 0.0f;
2029 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002030 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002031 if (pureTranslate) {
Chris Craika6b52192015-02-27 17:43:02 -08002032 x = floorf(x + currentTransform()->getTranslateX() + 0.5f);
2033 y = floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002034 }
2035
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002036 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002037 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002038
2039 int alpha;
2040 SkXfermode::Mode mode;
2041 getAlphaAndMode(paint, &alpha, &mode);
2042
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002043 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002044 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002045 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002046 }
2047
Romain Guy671d6cf2012-01-18 12:39:17 -08002048 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002049 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002050 if (pureTranslate && !linearFilter) {
2051 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2052 }
Romain Guy257ae352013-03-20 16:31:12 -07002053 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002054
Rob Tsuk487a92c2015-01-06 13:22:54 -08002055 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002056 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2057
Chris Craik82840732015-04-03 09:37:49 -07002058 TextDrawFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002059 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craik083e7332015-02-27 17:04:20 -08002060 positions, hasLayer() ? &bounds : nullptr, &functor)) {
2061 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2062 mDirty = true;
Romain Guy671d6cf2012-01-18 12:39:17 -08002063 }
Chet Haase48659092012-05-31 15:21:51 -07002064
Romain Guyeb9a5362012-01-17 17:39:26 -08002065}
2066
Chris Craik59744b72014-07-01 17:56:52 -07002067bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002068 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002069 outMatrix->setIdentity();
2070 return false;
2071 } else if (CC_UNLIKELY(transform.isPerspective())) {
2072 outMatrix->setIdentity();
2073 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002074 }
Chris Craik59744b72014-07-01 17:56:52 -07002075
2076 /**
Chris Craika736cd92014-08-04 13:18:38 -07002077 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2078 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002079 */
2080 float sx, sy;
2081 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002082 outMatrix->setScale(
2083 roundf(fmaxf(1.0f, sx)),
2084 roundf(fmaxf(1.0f, sy)));
2085 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002086}
2087
Tom Hudson984162f2014-10-10 13:38:16 -04002088int OpenGLRenderer::getSaveCount() const {
2089 return mState.getSaveCount();
2090}
2091
2092int OpenGLRenderer::save(int flags) {
2093 return mState.save(flags);
2094}
2095
2096void OpenGLRenderer::restore() {
Chris Craike2bb3802015-03-13 15:07:52 -07002097 mState.restore();
Tom Hudson984162f2014-10-10 13:38:16 -04002098}
2099
2100void OpenGLRenderer::restoreToCount(int saveCount) {
Chris Craike2bb3802015-03-13 15:07:52 -07002101 mState.restoreToCount(saveCount);
Tom Hudson984162f2014-10-10 13:38:16 -04002102}
2103
2104void OpenGLRenderer::translate(float dx, float dy, float dz) {
Chris Craike2bb3802015-03-13 15:07:52 -07002105 mState.translate(dx, dy, dz);
Tom Hudson984162f2014-10-10 13:38:16 -04002106}
2107
2108void OpenGLRenderer::rotate(float degrees) {
Chris Craike2bb3802015-03-13 15:07:52 -07002109 mState.rotate(degrees);
Tom Hudson984162f2014-10-10 13:38:16 -04002110}
2111
2112void OpenGLRenderer::scale(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002113 mState.scale(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002114}
2115
2116void OpenGLRenderer::skew(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002117 mState.skew(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002118}
2119
2120void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2121 mState.setMatrix(matrix);
2122}
2123
2124void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2125 mState.concatMatrix(matrix);
2126}
2127
2128bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2129 return mState.clipRect(left, top, right, bottom, op);
2130}
2131
2132bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2133 return mState.clipPath(path, op);
2134}
2135
2136bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2137 return mState.clipRegion(region, op);
2138}
2139
2140void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2141 mState.setClippingOutline(allocator, outline);
2142}
2143
2144void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2145 const Rect& rect, float radius, bool highPriority) {
2146 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2147}
2148
Tom Hudson107843d2014-09-08 11:26:26 -04002149void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002150 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002151 DrawOpMode drawOpMode) {
2152
Chris Craik03ae2722015-02-25 11:01:09 -08002153 if (drawOpMode == DrawOpMode::kImmediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002154 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2155 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002156 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002157 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002158 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002159 }
Romain Guycac5fd32011-12-01 20:08:50 -08002160 }
2161
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002162 const float oldX = x;
2163 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002164
Chris Craikd6b65f62014-01-01 14:45:21 -08002165 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002166 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002167
Romain Guy211370f2012-02-01 16:10:55 -08002168 if (CC_LIKELY(pureTranslate)) {
Chris Craika6b52192015-02-27 17:43:02 -08002169 x = floorf(x + transform.getTranslateX() + 0.5f);
2170 y = floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002171 }
2172
Romain Guy86568192010-12-14 15:55:39 -08002173 int alpha;
2174 SkXfermode::Mode mode;
2175 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002176
Romain Guyc74f45a2013-02-26 19:10:14 -08002177 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2178
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002179 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002180 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002181 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002182 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002183 }
2184
Romain Guy19d4dd82013-03-04 11:14:26 -08002185 const bool hasActiveLayer = hasLayer();
2186
Romain Guy624234f2013-03-05 16:43:31 -08002187 // We only pass a partial transform to the font renderer. That partial
2188 // matrix defines how glyphs are rasterized. Typically we want glyphs
2189 // to be rasterized at their final size on screen, which means the partial
2190 // matrix needs to take the scale factor into account.
2191 // When a partial matrix is used to transform glyphs during rasterization,
2192 // the mesh is generated with the inverse transform (in the case of scale,
2193 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2194 // apply the full transform matrix at draw time in the vertex shader.
2195 // Applying the full matrix in the shader is the easiest way to handle
2196 // rotation and perspective and allows us to always generated quads in the
2197 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002198 SkMatrix fontTransform;
2199 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2200 || fabs(y - (int) y) > 0.0f
2201 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002202 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002203 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002204
Romain Guy624234f2013-03-05 16:43:31 -08002205 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08002206 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002207 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 -07002208
Raph Levien996e57c2012-07-23 15:22:52 -07002209 bool status;
Chris Craik82840732015-04-03 09:37:49 -07002210 TextDrawFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002211
2212 // don't call issuedrawcommand, do it at end of batch
Chris Craik03ae2722015-02-25 11:01:09 -08002213 bool forceFinish = (drawOpMode != DrawOpMode::kDefer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002214 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002215 SkPaint paintCopy(*paint);
2216 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2217 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002218 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002219 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002220 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002221 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002222 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002223
Chris Craik03ae2722015-02-25 11:01:09 -08002224 if ((status || drawOpMode != DrawOpMode::kImmediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002225 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002226 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002227 }
Chris Craik41541822013-05-03 16:35:54 -07002228 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002229 }
Romain Guy694b5192010-07-21 21:33:20 -07002230
Chris Craike63f7c622013-10-17 10:30:55 -07002231 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002232
Tom Hudson107843d2014-09-08 11:26:26 -04002233 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002234}
2235
Tom Hudson107843d2014-09-08 11:26:26 -04002236void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002237 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002238 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002239 return;
Romain Guy03d58522012-02-24 17:54:07 -08002240 }
2241
Chris Craik39a908c2013-06-13 14:39:01 -07002242 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002243 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002244
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002245 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002246 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002247 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002248
2249 int alpha;
2250 SkXfermode::Mode mode;
2251 getAlphaAndMode(paint, &alpha, &mode);
Chris Craik82840732015-04-03 09:37:49 -07002252 TextDrawFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002253
Tom Hudson984162f2014-10-10 13:38:16 -04002254 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002255 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 -08002256
Romain Guy97771732012-02-28 18:17:02 -08002257 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chih-Hung Hsieh9ad6ac02015-02-27 16:11:36 -08002258 hOffset, vOffset, hasLayer() ? &bounds : nullptr, &functor)) {
Chris Craik083e7332015-02-27 17:04:20 -08002259 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2260 mDirty = true;
Romain Guy97771732012-02-28 18:17:02 -08002261 }
Romain Guy325740f2012-02-24 16:48:34 -08002262}
2263
Tom Hudson107843d2014-09-08 11:26:26 -04002264void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002265 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002266
Chris Craik44eb2c02015-01-29 09:45:09 -08002267 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002268
Chris Craik30036092015-02-12 10:41:39 -08002269 PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002270 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002271 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002272
Romain Guy8b55f372010-08-18 17:10:07 -07002273 const float x = texture->left - texture->offset;
2274 const float y = texture->top - texture->offset;
2275
Romain Guy01d58e42011-01-19 21:54:02 -08002276 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002277 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002278}
2279
Tom Hudson107843d2014-09-08 11:26:26 -04002280void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002281 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002282 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002283 }
2284
Chris Craike84a2082014-12-22 14:28:49 -08002285 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002286 if (layer->isTextureLayer()) {
2287 transform = &layer->getTransform();
2288 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002289 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002290 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002291 }
2292 }
2293
Chris Craik39a908c2013-06-13 14:39:01 -07002294 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08002295 const bool rejected = mState.calculateQuickRejectForScissor(
2296 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2297 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002298
2299 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002300 if (transform && !transform->isIdentity()) {
2301 restore();
2302 }
Tom Hudson107843d2014-09-08 11:26:26 -04002303 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002304 }
2305
Chris Craik62d307c2014-07-29 10:35:13 -07002306 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2307 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2308
Romain Guy5bb3c732012-11-29 17:52:58 -08002309 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002310
Chris Craik65fe5ee2015-01-26 18:06:29 -08002311 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08002312 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002313
Romain Guy211370f2012-02-01 16:10:55 -08002314 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002315 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002316 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2317 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002318 } else if (layer->mesh) {
Chris Craik82840732015-04-03 09:37:49 -07002319 Glop glop;
2320 GlopBuilder(mRenderState, mCaches, &glop)
2321 .setMeshTexturedIndexedQuads(layer->mesh, layer->meshElementCount)
2322 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
2323 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
2324 .setModelViewOffsetRectSnap(x, y, Rect(0, 0, layer->layer.getWidth(), layer->layer.getHeight()))
2325 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2326 .build();
2327 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy3a3133d2011-02-01 22:59:58 -08002328#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07002329 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08002330#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002331 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002332
Romain Guy5bb3c732012-11-29 17:52:58 -08002333 if (layer->debugDrawUpdate) {
2334 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002335
2336 SkPaint paint;
2337 paint.setColor(0x7f00ff00);
2338 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002339 }
Romain Guyf219da52011-01-16 12:54:25 -08002340 }
Chris Craik34416ea2013-04-15 16:08:28 -07002341 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07002342
Romain Guyb2e2f242012-10-17 18:18:35 -07002343 if (transform && !transform->isIdentity()) {
2344 restore();
2345 }
2346
Tom Hudson107843d2014-09-08 11:26:26 -04002347 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08002348}
2349
Romain Guy6926c722010-07-12 20:20:03 -07002350///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002351// Draw filters
2352///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04002353void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
2354 // We should never get here since we apply the draw filter when stashing
2355 // the paints in the DisplayList.
2356 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08002357}
2358
2359///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002360// Drawing implementation
2361///////////////////////////////////////////////////////////////////////////////
2362
Chris Craikd218a922014-01-02 17:13:34 -08002363Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08002364 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002365 if (!texture) {
2366 return mCaches.textureCache.get(bitmap);
2367 }
2368 return texture;
2369}
2370
Chris Craik2bb8f562015-02-17 16:42:02 -08002371void OpenGLRenderer::drawPathTexture(PathTexture* texture, float x, float y,
2372 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002373 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08002374 return;
2375 }
2376
Chris Craik82840732015-04-03 09:37:49 -07002377 Glop glop;
2378 GlopBuilder(mRenderState, mCaches, &glop)
2379 .setMeshTexturedUnitQuad(nullptr)
2380 .setFillPathTexturePaint(*texture, *paint, currentSnapshot()->alpha)
2381 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
2382 .setModelViewMapUnitToRect(Rect(x, y, x + texture->width, y + texture->height))
2383 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2384 .build();
2385 renderGlop(glop);
Romain Guy01d58e42011-01-19 21:54:02 -08002386}
2387
Romain Guyf607bdc2010-09-10 19:20:06 -07002388// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002389#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2390#define kStdUnderline_Offset (1.0f / 9.0f)
2391#define kStdUnderline_Thickness (1.0f / 18.0f)
2392
Chris Craikd218a922014-01-02 17:13:34 -08002393void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
2394 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07002395 // Handle underline and strike-through
2396 uint32_t flags = paint->getFlags();
2397 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002398 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002399
Romain Guy211370f2012-02-01 16:10:55 -08002400 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002401 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002402 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002403
Raph Levien8b4072d2012-07-30 15:50:00 -07002404 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002405 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002406
Romain Guyf6834472011-01-23 13:32:12 -08002407 int linesCount = 0;
2408 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2409 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2410
2411 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002412 float points[pointsCount];
2413 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002414
2415 if (flags & SkPaint::kUnderlineText_Flag) {
2416 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002417 points[currentPoint++] = left;
2418 points[currentPoint++] = top;
2419 points[currentPoint++] = left + underlineWidth;
2420 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002421 }
2422
2423 if (flags & SkPaint::kStrikeThruText_Flag) {
2424 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002425 points[currentPoint++] = left;
2426 points[currentPoint++] = top;
2427 points[currentPoint++] = left + underlineWidth;
2428 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002429 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002430
Romain Guy726aeba2011-06-01 14:52:00 -07002431 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002432
Romain Guy726aeba2011-06-01 14:52:00 -07002433 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002434 }
2435 }
2436}
2437
Tom Hudson107843d2014-09-08 11:26:26 -04002438void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002439 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002440 return;
Romain Guy672433d2013-01-04 19:05:13 -08002441 }
2442
Tom Hudson107843d2014-09-08 11:26:26 -04002443 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08002444}
2445
Tom Hudson107843d2014-09-08 11:26:26 -04002446void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07002447 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04002448 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07002449
Chris Craik15a07a22014-01-26 13:43:53 -08002450 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08002451 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07002452
2453 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08002454 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07002455
ztenghui14a4e352014-08-13 10:44:39 -07002456 // The caller has made sure casterAlpha > 0.
2457 float ambientShadowAlpha = mAmbientShadowAlpha;
Chris Craik2507c342015-05-04 14:36:49 -07002458 if (CC_UNLIKELY(Properties::overrideAmbientShadowStrength >= 0)) {
2459 ambientShadowAlpha = Properties::overrideAmbientShadowStrength;
ztenghui14a4e352014-08-13 10:44:39 -07002460 }
2461 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
2462 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002463 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002464 }
ztenghui7b4516e2014-01-07 10:42:55 -08002465
ztenghui14a4e352014-08-13 10:44:39 -07002466 float spotShadowAlpha = mSpotShadowAlpha;
Chris Craik2507c342015-05-04 14:36:49 -07002467 if (CC_UNLIKELY(Properties::overrideSpotShadowStrength >= 0)) {
2468 spotShadowAlpha = Properties::overrideSpotShadowStrength;
ztenghui14a4e352014-08-13 10:44:39 -07002469 }
2470 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
2471 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002472 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002473 }
ztenghui7b4516e2014-01-07 10:42:55 -08002474
Tom Hudson107843d2014-09-08 11:26:26 -04002475 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07002476}
2477
Tom Hudson107843d2014-09-08 11:26:26 -04002478void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002479 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08002480 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04002481 return;
Romain Guy3b753822013-03-05 10:27:35 -08002482 }
Romain Guy735738c2012-12-03 12:34:51 -08002483
Romain Guy672433d2013-01-04 19:05:13 -08002484 float left = FLT_MAX;
2485 float top = FLT_MAX;
2486 float right = FLT_MIN;
2487 float bottom = FLT_MIN;
2488
Romain Guy448455f2013-07-22 13:57:50 -07002489 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08002490 Vertex* vertex = mesh;
2491
Chris Craik2af46352012-11-26 18:30:17 -08002492 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08002493 float l = rects[index + 0];
2494 float t = rects[index + 1];
2495 float r = rects[index + 2];
2496 float b = rects[index + 3];
2497
Romain Guy3b753822013-03-05 10:27:35 -08002498 Vertex::set(vertex++, l, t);
2499 Vertex::set(vertex++, r, t);
2500 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08002501 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08002502
Romain Guy3b753822013-03-05 10:27:35 -08002503 left = fminf(left, l);
2504 top = fminf(top, t);
2505 right = fmaxf(right, r);
2506 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08002507 }
2508
Chris Craikf0a59072013-11-19 18:00:46 -08002509 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002510 return;
Romain Guya362c692013-02-04 13:50:16 -08002511 }
Romain Guy672433d2013-01-04 19:05:13 -08002512
Chris Craik82840732015-04-03 09:37:49 -07002513 const Matrix4& transform = ignoreTransform ? Matrix4::identity() : *currentTransform();
2514 Glop glop;
2515 GlopBuilder(mRenderState, mCaches, &glop)
2516 .setMeshIndexedQuads(&mesh[0], count / 4)
2517 .setFillPaint(*paint, currentSnapshot()->alpha)
2518 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
2519 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
2520 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2521 .build();
2522 renderGlop(glop);
Romain Guy672433d2013-01-04 19:05:13 -08002523}
2524
Romain Guy026c5e162010-06-28 17:12:22 -07002525void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002526 const SkPaint* paint, bool ignoreTransform) {
Chris Craik82840732015-04-03 09:37:49 -07002527 const Matrix4& transform = ignoreTransform ? Matrix4::identity() : *currentTransform();
2528 Glop glop;
2529 GlopBuilder(mRenderState, mCaches, &glop)
2530 .setMeshUnitQuad()
2531 .setFillPaint(*paint, currentSnapshot()->alpha)
2532 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
2533 .setModelViewMapUnitToRect(Rect(left, top, right, bottom))
2534 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2535 .build();
2536 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07002537}
2538
Chris Craike84a2082014-12-22 14:28:49 -08002539void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
2540 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002541 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craikd6b65f62014-01-01 14:45:21 -08002542 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002543}
2544
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002545float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik8df5ffa2015-04-28 17:47:20 -07002546 return (layer->getAlpha() / 255.0f) * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07002547}
2548
Romain Guy9d5316e2010-06-24 19:30:36 -07002549}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002550}; // namespace android