blob: aa722d025d6c3e00689bcc9655ddccfa34d9d0cd [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"
20#include "DisplayListRenderer.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080021#include "GammaFontRenderer.h"
Chris Craik03188872015-02-02 18:39:33 -080022#include "Glop.h"
23#include "GlopBuilder.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080024#include "Patch.h"
25#include "PathTessellator.h"
26#include "Properties.h"
27#include "RenderNode.h"
Chris Craik03188872015-02-02 18:39:33 -080028#include "renderstate/MeshState.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080029#include "renderstate/RenderState.h"
30#include "ShadowTessellator.h"
31#include "SkiaShader.h"
32#include "Vector.h"
33#include "VertexBuffer.h"
34#include "utils/GLUtils.h"
35#include "utils/PaintUtils.h"
36#include "utils/TraceUtils.h"
37
Romain Guye4d01122010-06-16 18:44:05 -070038#include <stdlib.h>
39#include <stdint.h>
40#include <sys/types.h>
41
Romain Guy5cbbce52010-06-27 22:59:20 -070042#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070043#include <SkColor.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040044#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070045#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070046
Romain Guye4d01122010-06-16 18:44:05 -070047#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070048#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070049
Romain Guy08aa2cb2011-03-17 11:06:57 -070050#include <private/hwui/DrawGlInfo.h>
51
Romain Guy5b3b3522010-10-27 18:57:51 -070052#include <ui/Rect.h>
53
Chris Craik62d307c2014-07-29 10:35:13 -070054#if DEBUG_DETAILED_EVENTS
55 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
56#else
57 #define EVENT_LOGD(...)
58#endif
59
Romain Guye4d01122010-06-16 18:44:05 -070060namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070061namespace uirenderer {
62
Romain Guy9d5316e2010-06-24 19:30:36 -070063///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -070064// Constructors/destructor
65///////////////////////////////////////////////////////////////////////////////
66
John Reck3b202512014-06-23 13:13:08 -070067OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -040068 : mState(*this)
Chris Craik058fc642014-07-23 18:19:28 -070069 , mCaches(Caches::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -070070 , mRenderState(renderState)
Chris Craik65fe5ee2015-01-26 18:06:29 -080071 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -070072 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -070073 , mSuppressTiling(false)
74 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -040075 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -070076 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -070077 , mLightRadius(FLT_MIN)
78 , mAmbientShadowAlpha(0)
79 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -080080 // *set* draw modifiers to be 0
81 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -070082 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guye4d01122010-06-16 18:44:05 -070083}
84
Romain Guy85bf02f2010-06-22 13:11:24 -070085OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -070086 // The context has already been destroyed at this point, do not call
87 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -070088}
89
Romain Guy87e2f7572012-09-24 11:37:12 -070090void OpenGLRenderer::initProperties() {
91 char property[PROPERTY_VALUE_MAX];
92 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
93 mScissorOptimizationDisabled = !strcasecmp(property, "true");
94 INIT_LOGD(" Scissor optimization %s",
95 mScissorOptimizationDisabled ? "disabled" : "enabled");
96 } else {
97 INIT_LOGD(" Scissor optimization enabled");
98 }
Romain Guy13631f32012-01-30 17:41:55 -080099}
100
Chris Craik058fc642014-07-23 18:19:28 -0700101void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
102 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
103 mLightCenter = lightCenter;
104 mLightRadius = lightRadius;
105 mAmbientShadowAlpha = ambientShadowAlpha;
106 mSpotShadowAlpha = spotShadowAlpha;
107}
108
Romain Guy13631f32012-01-30 17:41:55 -0800109///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700110// Setup
111///////////////////////////////////////////////////////////////////////////////
112
Chris Craik797b95b2014-05-20 18:10:25 -0700113void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700114 glDisable(GL_DITHER);
115 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Chris Craik284b2432014-09-18 16:05:35 -0700116 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700117}
118
Romain Guy96885eb2013-03-26 15:05:58 -0700119void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800120 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800121 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400122 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700123 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700124 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700125}
126
Tom Hudson107843d2014-09-08 11:26:26 -0400127void OpenGLRenderer::startFrame() {
128 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700129 mFrameStarted = true;
130
Tom Hudson984162f2014-10-10 13:38:16 -0400131 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700132
Romain Guy96885eb2013-03-26 15:05:58 -0700133 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700134
Tom Hudson984162f2014-10-10 13:38:16 -0400135 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800136
Romain Guy54c1a642012-09-27 17:55:46 -0700137 // Functors break the tiling extension in pretty spectacular ways
138 // This ensures we don't use tiling when a functor is going to be
139 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700140 mSuppressTiling = mCaches.hasRegisteredFunctors()
141 || mFirstFrameAfterResize;
142 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700143
Chris Craikd6b65f62014-01-01 14:45:21 -0800144 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700145
Romain Guy7c450aa2012-09-21 19:15:00 -0700146 debugOverdraw(true, true);
147
Tom Hudson107843d2014-09-08 11:26:26 -0400148 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700149 mTilingClip.right, mTilingClip.bottom, mOpaque);
150}
151
Tom Hudson107843d2014-09-08 11:26:26 -0400152void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700153 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700154
Romain Guy96885eb2013-03-26 15:05:58 -0700155 setupFrameState(left, top, right, bottom, opaque);
156
157 // Layer renderers will start the frame immediately
158 // The framebuffer renderer will first defer the display list
159 // for each layer and wait until the first drawing command
160 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800161 if (currentSnapshot()->fbo == 0) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800162 mRenderState.blend().syncEnabled();
Romain Guy96885eb2013-03-26 15:05:58 -0700163 updateLayers();
164 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400165 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700166 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700167}
168
Romain Guydcfc8362013-01-03 13:08:57 -0800169void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
170 // If we know that we are going to redraw the entire framebuffer,
171 // perform a discard to let the driver know we don't need to preserve
172 // the back buffer for this frame.
Chris Craik117bdbc2015-02-05 10:12:38 -0800173 if (mCaches.extensions().hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400174 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
Chris Craik6b109c72015-02-27 10:55:28 -0800175 const bool isFbo = getTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800176 const GLenum attachments[] = {
177 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
178 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800179 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
180 }
181}
182
Tom Hudson107843d2014-09-08 11:26:26 -0400183void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700184 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800185 mRenderState.scissor().setEnabled(true);
186 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700187 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400188 mDirty = true;
189 return;
Romain Guyddf74372012-05-22 14:07:07 -0700190 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700191
Chris Craik65fe5ee2015-01-26 18:06:29 -0800192 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700193}
194
henry.uh_chen33f5a592014-07-02 19:36:56 +0800195void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700196 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800197 const Snapshot* snapshot = currentSnapshot();
198
Chris Craik14e51302013-12-30 15:32:54 -0800199 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800200 if (snapshot->flags & Snapshot::kFlagFboTarget) {
201 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700202 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700203
henry.uh_chen33f5a592014-07-02 19:36:56 +0800204 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800205 }
206}
207
henry.uh_chen33f5a592014-07-02 19:36:56 +0800208void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800209 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800210 if(expand) {
211 // Expand the startTiling region by 1
212 int leftNotZero = (clip.left > 0) ? 1 : 0;
213 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
214
215 mCaches.startTiling(
216 clip.left - leftNotZero,
217 windowHeight - clip.bottom - topNotZero,
218 clip.right - clip.left + leftNotZero + 1,
219 clip.bottom - clip.top + topNotZero + 1,
220 opaque);
221 } else {
222 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800223 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800224 }
Romain Guy54c1a642012-09-27 17:55:46 -0700225 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700226}
227
228void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700229 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700230}
231
Tom Hudson107843d2014-09-08 11:26:26 -0400232bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700233 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700234 endTiling();
Chris Craik4ac36f82014-12-09 16:54:03 -0800235 mTempPaths.clear();
236
Romain Guyca89e2a2013-03-08 17:44:20 -0800237 // When finish() is invoked on FBO 0 we've reached the end
238 // of the current frame
Chris Craik6b109c72015-02-27 10:55:28 -0800239 if (getTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800240 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700241 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800242 }
243
Romain Guy11cb6422012-09-21 00:39:43 -0700244 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700245#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700246 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700247#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700248
Romain Guyc15008e2010-11-10 11:59:15 -0800249#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800250 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700251#else
252 if (mCaches.getDebugLevel() & kDebugMemory) {
253 mCaches.dumpMemoryUsage();
254 }
Romain Guyc15008e2010-11-10 11:59:15 -0800255#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700256 }
Romain Guy96885eb2013-03-26 15:05:58 -0700257
258 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400259
260 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700261}
262
Romain Guy35643dd2012-09-18 15:40:58 -0700263void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700264 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
265 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700266 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700267
Chris Craik65fe5ee2015-01-26 18:06:29 -0800268 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700269 dirtyClip();
270}
271
Andreas Gampe64bb4132014-11-22 00:35:09 +0000272void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400273 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700274
Rob Tsuk487a92c2015-01-06 13:22:54 -0800275 Rect clip(mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700276 clip.snapToPixelBoundaries();
277
Romain Guyd643bb52011-03-01 14:55:21 -0800278 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800279 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800280 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800281 dirtyLayerUnchecked(clip, getRegion());
282 }
Romain Guyd643bb52011-03-01 14:55:21 -0800283
Romain Guy08aa2cb2011-03-17 11:06:57 -0700284 DrawGlInfo info;
285 info.clipLeft = clip.left;
286 info.clipTop = clip.top;
287 info.clipRight = clip.right;
288 info.clipBottom = clip.bottom;
289 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700290 info.width = getViewportWidth();
291 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800292 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700293
Tom Hudson984162f2014-10-10 13:38:16 -0400294 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700295 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400296 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700297 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
298 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800299 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700300 setScissorFromClip();
301 }
Chris Craik54f574a2013-08-26 11:23:46 -0700302
John Reck3b202512014-06-23 13:13:08 -0700303 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
304 // Scissor may have been modified, reset dirty clip
305 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800306
Tom Hudson107843d2014-09-08 11:26:26 -0400307 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800308}
309
Romain Guyf6a11b82010-06-23 17:47:49 -0700310///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700311// Debug
312///////////////////////////////////////////////////////////////////////////////
313
Chris Craik62d307c2014-07-29 10:35:13 -0700314void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
315#if DEBUG_DETAILED_EVENTS
316 const int BUFFER_SIZE = 256;
317 va_list ap;
318 char buf[BUFFER_SIZE];
319
320 va_start(ap, fmt);
321 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
322 va_end(ap);
323
324 eventMark(buf);
325#endif
326}
327
328
Romain Guy0f667532013-03-01 14:31:04 -0800329void OpenGLRenderer::eventMark(const char* name) const {
330 mCaches.eventMark(0, name);
331}
332
Romain Guy87e2f7572012-09-24 11:37:12 -0700333void OpenGLRenderer::startMark(const char* name) const {
334 mCaches.startMark(0, name);
335}
336
337void OpenGLRenderer::endMark() const {
338 mCaches.endMark();
339}
340
341void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700342 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700343}
344
345void OpenGLRenderer::renderOverdraw() {
Chris Craik6b109c72015-02-27 10:55:28 -0800346 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700347 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700348
Chris Craik65fe5ee2015-01-26 18:06:29 -0800349 mRenderState.scissor().setEnabled(true);
350 mRenderState.scissor().set(clip->left,
351 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
352 clip->right - clip->left,
353 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700354
Romain Guy627c6fd2013-08-21 11:53:18 -0700355 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800356 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700357 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
358
359 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800360 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700361 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
362
363 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800364 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700365 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
366
367 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800368 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700369 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
370
Chris Craik96a5c4c2015-01-27 15:46:35 -0800371 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700372 }
373}
374
375///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700376// Layers
377///////////////////////////////////////////////////////////////////////////////
378
379bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700380 if (layer->deferredUpdateScheduled && layer->renderer
381 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700382
Romain Guy7c450aa2012-09-21 19:15:00 -0700383 if (inFrame) {
384 endTiling();
385 debugOverdraw(false, false);
386 }
Romain Guy11cb6422012-09-21 00:39:43 -0700387
Romain Guy96885eb2013-03-26 15:05:58 -0700388 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700389 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700390 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700391 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700392 }
Romain Guy11cb6422012-09-21 00:39:43 -0700393
394 if (inFrame) {
395 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800396 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700397 }
398
Romain Guy5bb3c732012-11-29 17:52:58 -0800399 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700400 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700401
402 return true;
403 }
404
405 return false;
406}
407
408void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700409 // If draw deferring is enabled this method will simply defer
410 // the display list of each individual layer. The layers remain
411 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700412 int count = mLayerUpdates.size();
413 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700414 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
415 startMark("Layer Updates");
416 } else {
417 startMark("Defer Layer Updates");
418 }
Romain Guy11cb6422012-09-21 00:39:43 -0700419
Chris Craik1206b9b2013-04-04 14:46:24 -0700420 // Note: it is very important to update the layers in order
421 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700422 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700423 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700424 }
Romain Guy11cb6422012-09-21 00:39:43 -0700425
Romain Guy96885eb2013-03-26 15:05:58 -0700426 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
427 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800428 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700429 }
430 endMark();
431 }
432}
433
434void OpenGLRenderer::flushLayers() {
435 int count = mLayerUpdates.size();
436 if (count > 0) {
437 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700438
Chris Craik1206b9b2013-04-04 14:46:24 -0700439 // Note: it is very important to update the layers in order
440 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800441 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700442 }
443
444 mLayerUpdates.clear();
Chris Craik6b109c72015-02-27 10:55:28 -0800445 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700446
Romain Guy11cb6422012-09-21 00:39:43 -0700447 endMark();
448 }
449}
450
451void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
452 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700453 // Make sure we don't introduce duplicates.
454 // SortedVector would do this automatically but we need to respect
455 // the insertion order. The linear search is not an issue since
456 // this list is usually very short (typically one item, at most a few)
457 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
458 if (mLayerUpdates.itemAt(i) == layer) {
459 return;
460 }
461 }
Romain Guy11cb6422012-09-21 00:39:43 -0700462 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700463 }
464}
465
Romain Guye93482f2013-06-17 13:14:51 -0700466void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
467 if (layer) {
468 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
469 if (mLayerUpdates.itemAt(i) == layer) {
470 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700471 break;
472 }
473 }
474 }
475}
476
Romain Guy40543602013-06-12 15:31:28 -0700477void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800478 ATRACE_NAME("Update HW Layers");
Chris Craik44eb2c02015-01-29 09:45:09 -0800479 mRenderState.blend().syncEnabled();
Romain Guy40543602013-06-12 15:31:28 -0700480 updateLayers();
481 flushLayers();
482 // Wait for all the layer updates to be executed
John Reck55156372015-01-21 07:46:37 -0800483 glFinish();
Romain Guy40543602013-06-12 15:31:28 -0700484}
485
John Reck443a7142014-09-04 17:40:05 -0700486void OpenGLRenderer::markLayersAsBuildLayers() {
487 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
488 mLayerUpdates[i]->wasBuildLayered = true;
489 }
490}
491
Romain Guy11cb6422012-09-21 00:39:43 -0700492///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700493// State management
494///////////////////////////////////////////////////////////////////////////////
495
Chris Craik14e51302013-12-30 15:32:54 -0800496void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700497 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800498 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
499 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700500
Chris Craika64a2be2014-05-14 14:17:01 -0700501 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700502 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700503 }
504
Romain Guy2542d192010-08-18 11:47:12 -0700505 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700506 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700507 }
Romain Guy2542d192010-08-18 11:47:12 -0700508
Romain Guy5ec99242010-11-03 16:19:08 -0700509 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700510 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700511 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700512 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800513 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700514 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700515 }
Romain Guybb9524b2010-06-22 18:56:38 -0700516}
517
Romain Guyf6a11b82010-06-23 17:47:49 -0700518///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700519// Layers
520///////////////////////////////////////////////////////////////////////////////
521
522int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700523 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700524 // force matrix/clip isolation for layer
525 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
526
Tom Hudson984162f2014-10-10 13:38:16 -0400527 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700528
Tom Hudson984162f2014-10-10 13:38:16 -0400529 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700530 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700531 }
Romain Guyd55a8612010-06-28 17:42:46 -0700532
533 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700534}
535
Chris Craikd90144d2013-03-19 15:03:48 -0700536void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
537 const Rect untransformedBounds(bounds);
538
Chris Craikd6b65f62014-01-01 14:45:21 -0800539 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700540
541 // Layers only make sense if they are in the framebuffer's bounds
Rob Tsuk487a92c2015-01-06 13:22:54 -0800542 if (bounds.intersect(mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700543 // We cannot work with sub-pixels in this case
544 bounds.snapToPixelBoundaries();
545
546 // When the layer is not an FBO, we may use glCopyTexImage so we
547 // need to make sure the layer does not extend outside the bounds
548 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700549 const Snapshot& previous = *(currentSnapshot()->previous);
550 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
551 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700552 bounds.setEmpty();
553 } else if (fboLayer) {
554 clip.set(bounds);
555 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800556 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700557 inverse.mapRect(clip);
558 clip.snapToPixelBoundaries();
559 if (clip.intersect(untransformedBounds)) {
560 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
561 bounds.set(untransformedBounds);
562 } else {
563 clip.setEmpty();
564 }
565 }
566 } else {
567 bounds.setEmpty();
568 }
569}
570
Chris Craik408eb122013-03-26 18:55:15 -0700571void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
572 bool fboLayer, int alpha) {
573 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
574 bounds.getHeight() > mCaches.maxTextureSize ||
575 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400576 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700577 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400578 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700579 }
580}
581
Chris Craikd90144d2013-03-19 15:03:48 -0700582int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500583 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400584 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700585
Tom Hudson984162f2014-10-10 13:38:16 -0400586 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700587 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
588 // operations will be able to store and restore the current clip and transform info, and
589 // quick rejection will be correct (for display lists)
590
591 Rect bounds(left, top, right, bottom);
592 Rect clip;
593 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500594 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700595
Tom Hudson984162f2014-10-10 13:38:16 -0400596 if (!mState.currentlyIgnored()) {
597 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
598 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
599 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800600 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700601 }
602 }
603
604 return count;
605}
606
Romain Guy1c740bc2010-09-13 18:00:09 -0700607/**
608 * Layers are viewed by Skia are slightly different than layers in image editing
609 * programs (for instance.) When a layer is created, previously created layers
610 * and the frame buffer still receive every drawing command. For instance, if a
611 * layer is created and a shape intersecting the bounds of the layers and the
612 * framebuffer is draw, the shape will be drawn on both (unless the layer was
613 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
614 *
615 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
616 * texture. Unfortunately, this is inefficient as it requires every primitive to
617 * be drawn n + 1 times, where n is the number of active layers. In practice this
618 * means, for every primitive:
619 * - Switch active frame buffer
620 * - Change viewport, clip and projection matrix
621 * - Issue the drawing
622 *
623 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700624 * To avoid this, layers are implemented in a different way here, at least in the
625 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
626 * is set. When this flag is set we can redirect all drawing operations into a
627 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700628 *
629 * This implementation relies on the frame buffer being at least RGBA 8888. When
630 * a layer is created, only a texture is created, not an FBO. The content of the
631 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700632 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700633 * buffer and drawing continues as normal. This technique therefore treats the
634 * frame buffer as a scratch buffer for the layers.
635 *
636 * To compose the layers back onto the frame buffer, each layer texture
637 * (containing the original frame buffer data) is drawn as a simple quad over
638 * the frame buffer. The trick is that the quad is set as the composition
639 * destination in the blending equation, and the frame buffer becomes the source
640 * of the composition.
641 *
642 * Drawing layers with an alpha value requires an extra step before composition.
643 * An empty quad is drawn over the layer's region in the frame buffer. This quad
644 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
645 * quad is used to multiply the colors in the frame buffer. This is achieved by
646 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
647 * GL_ZERO, GL_SRC_ALPHA.
648 *
649 * Because glCopyTexImage2D() can be slow, an alternative implementation might
650 * be use to draw a single clipped layer. The implementation described above
651 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700652 *
653 * (1) The frame buffer is actually not cleared right away. To allow the GPU
654 * to potentially optimize series of calls to glCopyTexImage2D, the frame
655 * buffer is left untouched until the first drawing operation. Only when
656 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700657 */
Chet Haased48885a2012-08-28 17:43:28 -0700658bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700659 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800660 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
661 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700662
Romain Guyeb993562010-10-05 18:14:38 -0700663 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
664
Romain Guyf607bdc2010-09-10 19:20:06 -0700665 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700666 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700667 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700668 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000669 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700670
671 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400672 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700673 return false;
674 }
Romain Guyf18fd992010-07-08 11:45:51 -0700675
Chris Craik44eb2c02015-01-29 09:45:09 -0800676 mCaches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700677 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700678 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700679 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700680 }
681
Derek Sollenberger674554f2014-02-19 16:47:32 +0000682 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700683 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700684 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
685 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500686
Chet Haasea23eed82012-04-12 15:19:04 -0700687 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700688 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700689 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda57022010-07-06 11:39:32 -0700690
Romain Guy8fb95422010-08-17 18:38:51 -0700691 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400692 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
693 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700694
Chris Craika8bea8e2014-09-24 11:29:43 -0700695 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
696 fboLayer ? "" : "unclipped ",
697 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700698 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700699 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700700 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700701 } else {
702 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700703 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800704 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700705 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700706 // Workaround for some GL drivers. When reading pixels lying outside
707 // of the window we should get undefined values for those pixels.
708 // Unfortunately some drivers will turn the entire target texture black
709 // when reading outside of the window.
710 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800711 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700712 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800713 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700714
Chris Craika64a2be2014-05-14 14:17:01 -0700715 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
716 bounds.left, getViewportHeight() - bounds.bottom,
717 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700718
Romain Guy54be1cd2011-06-13 19:04:27 -0700719 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800720 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700721 }
Romain Guyeb993562010-10-05 18:14:38 -0700722 }
Romain Guyf86ef572010-07-01 11:05:42 -0700723
Romain Guyd55a8612010-06-28 17:42:46 -0700724 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700725}
726
Chris Craike63f7c622013-10-17 10:30:55 -0700727bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800728 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700729 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700730
Tom Hudson984162f2014-10-10 13:38:16 -0400731 writableSnapshot()->region = &writableSnapshot()->layer->region;
732 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
733 writableSnapshot()->fbo = layer->getFbo();
734 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
735 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
736 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800737 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700738
Romain Guy2b7028e2012-09-19 17:25:38 -0700739 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700740 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700741 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700742 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700743 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700744
745 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700746 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700747 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700748 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700749 }
750
751 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Chris Craikf27133d2015-02-19 09:51:53 -0800752 layer->getTextureId(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700753
henry.uh_chen33f5a592014-07-02 19:36:56 +0800754 // Expand the startTiling region by 1
755 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700756
757 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800758 mRenderState.scissor().setEnabled(true);
759 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700760 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700761 glClear(GL_COLOR_BUFFER_BIT);
762
763 dirtyClip();
764
765 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700766 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700767 return true;
768}
769
Romain Guy1c740bc2010-09-13 18:00:09 -0700770/**
771 * Read the documentation of createLayer() before doing anything in this method.
772 */
Chris Craik14e51302013-12-30 15:32:54 -0800773void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
774 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000775 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700776 return;
777 }
778
Chris Craik14e51302013-12-30 15:32:54 -0800779 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800780 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800781 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700782
Chris Craik39a908c2013-06-13 14:39:01 -0700783 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400784 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800785 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800786 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700787
Romain Guyeb993562010-10-05 18:14:38 -0700788 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700789 endTiling();
790
Romain Guye0aa84b2012-04-03 19:30:26 -0700791 // Detach the texture from the FBO
792 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800793
794 layer->removeFbo(false);
795
Romain Guyeb993562010-10-05 18:14:38 -0700796 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700797 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700798 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700799
Chris Craikd6b65f62014-01-01 14:45:21 -0800800 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700801 }
802
Romain Guy9ace8f52011-07-07 20:50:11 -0700803 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500804 SkPaint layerPaint;
805 layerPaint.setAlpha(layer->getAlpha());
806 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
807 layerPaint.setColorFilter(layer->getColorFilter());
808
809 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700810 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700811 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700812 }
813
Chris Craik96a5c4c2015-01-27 15:46:35 -0800814 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700815
Chris Craik44eb2c02015-01-29 09:45:09 -0800816 mCaches.textureState().activateTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700817
Romain Guy5b3b3522010-10-27 18:57:51 -0700818 // When the layer is stored in an FBO, we can save a bit of fillrate by
819 // drawing only the dirty region
820 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800821 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700822 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700823 } else if (!rect.isEmpty()) {
824 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530825
826 save(0);
827 // the layer contains screen buffer content that shouldn't be alpha modulated
828 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400829 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700830 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530831 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700832 }
Romain Guy8b55f372010-08-18 17:10:07 -0700833
Romain Guy746b7402010-10-26 16:27:31 -0700834 dirtyClip();
835
Romain Guyeb993562010-10-05 18:14:38 -0700836 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800837 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700838 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800839 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800840 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700841 }
842}
843
Romain Guyaa6c24c2011-04-28 18:40:04 -0700844void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craik82840732015-04-03 09:37:49 -0700845 bool snap = !layer->getForceFilter()
Chris Craik26bf3422015-02-26 16:28:17 -0800846 && layer->getWidth() == (uint32_t) rect.getWidth()
Chris Craik82840732015-04-03 09:37:49 -0700847 && layer->getHeight() == (uint32_t) rect.getHeight();
848 Glop glop;
849 GlopBuilder(mRenderState, mCaches, &glop)
850 .setMeshTexturedUvQuad(nullptr, Rect(0, 1, 1, 0)) // TODO: simplify with VBO
851 .setFillTextureLayer(*layer, getLayerAlpha(layer))
852 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
853 .setModelViewMapUnitToRectOptionalSnap(snap, rect)
854 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
855 .build();
856 renderGlop(glop);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700857}
858
Romain Guy5b3b3522010-10-27 18:57:51 -0700859void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700860 if (layer->isTextureLayer()) {
861 EVENT_LOGD("composeTextureLayerRect");
Chris Craik62d307c2014-07-29 10:35:13 -0700862 drawTextureLayer(layer, rect);
Chris Craik62d307c2014-07-29 10:35:13 -0700863 } else {
864 EVENT_LOGD("composeHardwareLayerRect");
Chris Craik182952f2015-03-09 14:17:29 -0700865
Chris Craik82840732015-04-03 09:37:49 -0700866 Blend::ModeOrderSwap modeUsage = swap ?
867 Blend::ModeOrderSwap::Swap : Blend::ModeOrderSwap::NoSwap;
868 const Matrix4& transform = swap ? Matrix4::identity() : *currentTransform();
869 bool snap = !swap
870 && layer->getWidth() == static_cast<uint32_t>(rect.getWidth())
871 && layer->getHeight() == static_cast<uint32_t>(rect.getHeight());
872 Glop glop;
873 GlopBuilder(mRenderState, mCaches, &glop)
874 .setMeshTexturedUvQuad(nullptr, layer->texCoords)
875 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), modeUsage)
876 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
877 .setModelViewMapUnitToRectOptionalSnap(snap, rect)
878 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
879 .build();
880 renderGlop(glop);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700881 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700882}
883
Chris Craik34416ea2013-04-15 16:08:28 -0700884/**
885 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
886 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
887 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
888 * by saveLayer's restore
889 */
Chris Craik6b109c72015-02-27 10:55:28 -0800890#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
891 DRAW_COMMAND; \
892 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
893 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
894 DRAW_COMMAND; \
895 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
896 } \
Chris Craik34416ea2013-04-15 16:08:28 -0700897 }
898
899#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
900
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400901// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
902// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
903class LayerShader : public SkShader {
904public:
905 LayerShader(Layer* layer, const SkMatrix* localMatrix)
906 : INHERITED(localMatrix)
907 , mLayer(layer) {
908 }
909
Chris Craike84a2082014-12-22 14:28:49 -0800910 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400911 if (data) {
912 *data = static_cast<void*>(mLayer);
913 }
914 return true;
915 }
916
Chris Craike84a2082014-12-22 14:28:49 -0800917 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400918 return !mLayer->isBlend();
919 }
920
921protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000922 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400923 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
924 }
925
Chris Craike84a2082014-12-22 14:28:49 -0800926 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400927 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
928 }
929
Chris Craike84a2082014-12-22 14:28:49 -0800930 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400931 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -0800932 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400933 }
934private:
935 // Unowned.
936 Layer* mLayer;
937 typedef SkShader INHERITED;
938};
939
Romain Guy5b3b3522010-10-27 18:57:51 -0700940void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -0700941 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
942
943 if (layer->getConvexMask()) {
944 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
945
946 // clip to the area of the layer the mask can be larger
947 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
948
949 SkPaint paint;
950 paint.setAntiAlias(true);
951 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
952
Chris Craik3f0854292014-04-15 16:18:08 -0700953 // create LayerShader to map SaveLayer content into subsequent draw
954 SkMatrix shaderMatrix;
955 shaderMatrix.setTranslate(rect.left, rect.bottom);
956 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400957 LayerShader layerShader(layer, &shaderMatrix);
958 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -0700959
960 // Since the drawing primitive is defined in local drawing space,
961 // we don't need to modify the draw matrix
962 const SkPath* maskPath = layer->getConvexMask();
963 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
964
Chris Craike84a2082014-12-22 14:28:49 -0800965 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -0700966 restore();
967
968 return;
969 }
970
Romain Guy5b3b3522010-10-27 18:57:51 -0700971 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700972 layer->setRegionAsRect();
973
Chris Craik34416ea2013-04-15 16:08:28 -0700974 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -0700975
Romain Guy5b3b3522010-10-27 18:57:51 -0700976 layer->region.clear();
977 return;
978 }
979
Chris Craik62d307c2014-07-29 10:35:13 -0700980 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -0700981 // standard Region based draw
982 size_t count;
983 const android::Rect* rects;
984 Region safeRegion;
985 if (CC_LIKELY(hasRectToRectTransform())) {
986 rects = layer->region.getArray(&count);
987 } else {
988 safeRegion = Region::createTJunctionFreeRegion(layer->region);
989 rects = safeRegion.getArray(&count);
990 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700991
Chris Craik3f0854292014-04-15 16:18:08 -0700992 const float texX = 1.0f / float(layer->getWidth());
993 const float texY = 1.0f / float(layer->getHeight());
994 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700995
Chris Craik82840732015-04-03 09:37:49 -0700996 TextureVertex quadVertices[count * 4];
997 TextureVertex* mesh = &quadVertices[0];
Chris Craik3f0854292014-04-15 16:18:08 -0700998 for (size_t i = 0; i < count; i++) {
999 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001000
Chris Craik3f0854292014-04-15 16:18:08 -07001001 const float u1 = r->left * texX;
1002 const float v1 = (height - r->top) * texY;
1003 const float u2 = r->right * texX;
1004 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001005
Chris Craik3f0854292014-04-15 16:18:08 -07001006 // TODO: Reject quads outside of the clip
1007 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1008 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1009 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1010 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Chris Craik3f0854292014-04-15 16:18:08 -07001011 }
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)
1017 .setModelViewOffsetRectSnap(0, 0, rect)
1018 .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 Craik7273daa2013-03-28 11:25:24 -07001191 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1192 // (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 Craik7273daa2013-03-28 11:25:24 -07001194 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001195 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001196
1197 // always store/restore, since it's just a pointer
1198 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001199 return false;
1200}
1201
Chris Craik527a3aa2013-03-04 10:19:31 -08001202void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001203 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001204 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001205 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001206 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001207
Chris Craik527a3aa2013-03-04 10:19:31 -08001208 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001209 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001210 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001211 dirtyClip();
1212 }
Chris Craikc3566d02013-02-04 16:16:33 -08001213}
1214
Chris Craik28ce94a2013-05-31 11:38:03 -07001215/**
1216 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1217 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1218 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1219 *
1220 * This method should be called when restoreDisplayState() won't be restoring the clip
1221 */
1222void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001223 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001224 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001225 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001226 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001227 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001228 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001229 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1230 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001231}
1232
Chris Craikc3566d02013-02-04 16:16:33 -08001233///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001234// Clipping
1235///////////////////////////////////////////////////////////////////////////////
1236
Romain Guybb9524b2010-06-22 18:56:38 -07001237void OpenGLRenderer::setScissorFromClip() {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001238 Rect clip(mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001239 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001240
Chris Craik65fe5ee2015-01-26 18:06:29 -08001241 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001242 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001243 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001244 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001245}
1246
Romain Guy8ce00302013-01-15 18:51:42 -08001247void OpenGLRenderer::ensureStencilBuffer() {
1248 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1249 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1250 // just hope we have one when hasLayer() returns false.
1251 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001252 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001253 }
1254}
1255
1256void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1257 // The layer's FBO is already bound when we reach this stage
1258 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001259 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1260 // is attached after we initiated tiling. We must turn it off,
1261 // attach the new render buffer then turn tiling back on
1262 endTiling();
1263
Romain Guy8d4aeb72013-02-12 16:08:55 -08001264 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Chris Craik117bdbc2015-02-05 10:12:38 -08001265 Stencil::getSmallestStencilFormat(),
1266 layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001267 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001268
Romain Guyf735c8e2013-01-31 17:45:55 -08001269 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001270 }
1271}
1272
Rob Tsuk487a92c2015-01-06 13:22:54 -08001273static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1274 float x, float y) {
1275 Vertex v;
1276 v.x = x;
1277 v.y = y;
1278 transform.mapPoint(v.x, v.y);
1279 rectangleVertices.push_back(v);
1280}
1281
1282static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1283 Vertex v;
1284 v.x = x;
1285 v.y = y;
1286 rectangleVertices.push_back(v);
1287}
1288
1289void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
Chris Craik2bb8f562015-02-17 16:42:02 -08001290 int quadCount = rectangleList.getTransformedRectanglesCount();
1291 std::vector<Vertex> rectangleVertices(quadCount * 4);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001292 Rect scissorBox = rectangleList.calculateBounds();
1293 scissorBox.snapToPixelBoundaries();
Chris Craik2bb8f562015-02-17 16:42:02 -08001294 for (int i = 0; i < quadCount; ++i) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001295 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1296 const Matrix4& transform = tr.getTransform();
1297 Rect bounds = tr.getBounds();
1298 if (transform.rectToRect()) {
1299 transform.mapRect(bounds);
1300 if (!bounds.intersect(scissorBox)) {
1301 bounds.setEmpty();
1302 } else {
1303 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1304 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1305 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1306 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1307 }
1308 } else {
1309 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1310 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1311 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1312 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1313 }
1314 }
1315
Chris Craik65fe5ee2015-01-26 18:06:29 -08001316 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001317 scissorBox.getWidth(), scissorBox.getHeight());
1318
Chris Craik82840732015-04-03 09:37:49 -07001319 Glop glop;
1320 Vertex* vertices = &rectangleVertices[0];
1321 GlopBuilder(mRenderState, mCaches, &glop)
1322 .setMeshIndexedQuads(vertices, rectangleVertices.size() / 4)
1323 .setFillBlack()
1324 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
1325 .setModelViewOffsetRect(0, 0, scissorBox)
1326 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1327 .build();
1328 renderGlop(glop);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001329}
1330
Romain Guy8ce00302013-01-15 18:51:42 -08001331void OpenGLRenderer::setStencilFromClip() {
1332 if (!mCaches.debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001333 if (!currentSnapshot()->clipIsSimple()) {
1334 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001335 EVENT_LOGD("setStencilFromClip - enabling");
1336
Romain Guy8ce00302013-01-15 18:51:42 -08001337 // NOTE: The order here is important, we must set dirtyClip to false
1338 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001339 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001340
1341 ensureStencilBuffer();
1342
Rob Tsuk487a92c2015-01-06 13:22:54 -08001343 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001344
Rob Tsuk487a92c2015-01-06 13:22:54 -08001345 bool isRectangleList = clipArea.isRectangleList();
1346 if (isRectangleList) {
1347 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1348 } else {
1349 incrementThreshold = 0;
1350 }
1351
Chris Craik96a5c4c2015-01-27 15:46:35 -08001352 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001353
1354 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001355 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001356 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001357 if (resetScissor) {
1358 // The scissor was not set so we now need to update it
1359 setScissorFromClip();
1360 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001361
Chris Craik96a5c4c2015-01-27 15:46:35 -08001362 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001363
1364 // stash and disable the outline clip state, since stencil doesn't account for outline
1365 bool storedSkipOutlineClip = mSkipOutlineClip;
1366 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001367
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001368 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001369 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001370 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1371
Rob Tsuk487a92c2015-01-06 13:22:54 -08001372 if (isRectangleList) {
1373 drawRectangleList(clipArea.getRectangleList());
1374 } else {
1375 // NOTE: We could use the region contour path to generate a smaller mesh
1376 // Since we are using the stencil we could use the red book path
1377 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001378
Rob Tsuk487a92c2015-01-06 13:22:54 -08001379 // The last parameter is important: we are not drawing in the color buffer
1380 // so we don't want to dirty the current layer, if any
1381 drawRegionRects(clipArea.getClipRegion(), paint, false);
1382 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001383 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001384 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001385
Chris Craik96a5c4c2015-01-27 15:46:35 -08001386 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001387
1388 // Draw the region used to generate the stencil if the appropriate debug
1389 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001390 // TODO: Implement for rectangle list clip areas
1391 if (mCaches.debugStencilClip == Caches::kStencilShowRegion &&
1392 !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001393 paint.setColor(0x7f0000ff);
1394 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001395 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001396 }
Romain Guy8ce00302013-01-15 18:51:42 -08001397 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001398 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001399 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001400 }
1401 }
1402}
1403
Chris Craikf0a59072013-11-19 18:00:46 -08001404/**
1405 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1406 *
1407 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1408 * style, and tessellated AA ramp
1409 */
1410bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001411 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001412 bool snapOut = paint && paint->isAntiAlias();
1413
1414 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1415 float outset = paint->getStrokeWidth() * 0.5f;
1416 left -= outset;
1417 top -= outset;
1418 right += outset;
1419 bottom += outset;
1420 }
1421
Chris Craikdeeda3d2014-05-05 19:09:33 -07001422 bool clipRequired = false;
1423 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001424 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001425 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001426 return true;
1427 }
1428
Chris Craikf23b25a2014-06-26 15:46:20 -07001429 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001430 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001431 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001432 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001433}
1434
Romain Guy8ce00302013-01-15 18:51:42 -08001435void OpenGLRenderer::debugClip() {
1436#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001437 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001438 SkPaint paint;
1439 paint.setColor(0x7f00ff00);
1440 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1441
Romain Guy8ce00302013-01-15 18:51:42 -08001442 }
1443#endif
1444}
1445
Chris Craik6b109c72015-02-27 10:55:28 -08001446void OpenGLRenderer::renderGlop(const Glop& glop, bool clearLayer) {
1447 // TODO: It would be best if we could do this before quickRejectSetupScissor()
1448 // changes the scissor test state
1449 if (clearLayer) clearLayerRegions();
1450
Chris Craik117bdbc2015-02-05 10:12:38 -08001451 if (mState.getDirtyClip()) {
1452 if (mRenderState.scissor().isEnabled()) {
1453 setScissorFromClip();
1454 }
1455
1456 setStencilFromClip();
1457 }
1458 mRenderState.render(glop);
Chris Craik2ab95d72015-02-06 15:25:51 -08001459 if (!mRenderState.stencil().isWriteEnabled()) {
1460 // TODO: specify more clearly when a draw should dirty the layer.
1461 // is writing to the stencil the only time we should ignore this?
1462 dirtyLayer(glop.bounds.left, glop.bounds.top, glop.bounds.right, glop.bounds.bottom);
Chris Craik0519c812015-02-11 13:17:06 -08001463 mDirty = true;
Chris Craik2ab95d72015-02-06 15:25:51 -08001464 }
Chris Craik117bdbc2015-02-05 10:12:38 -08001465}
1466
Romain Guyf6a11b82010-06-23 17:47:49 -07001467///////////////////////////////////////////////////////////////////////////////
1468// Drawing
1469///////////////////////////////////////////////////////////////////////////////
1470
Tom Hudson107843d2014-09-08 11:26:26 -04001471void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001472 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1473 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001474 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001475 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001476 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001477 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001478 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001479 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001480 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001481 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001482 }
1483
Chris Craikef8d6f22014-12-17 11:10:28 -08001484 // Don't avoid overdraw when visualizing, since that makes it harder to
1485 // debug where it's coming from, and when the problem occurs.
1486 bool avoidOverdraw = !mCaches.debugOverdraw;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001487 DeferredDisplayList deferredList(mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001488 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001489 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001490
1491 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001492 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001493
Tom Hudson107843d2014-09-08 11:26:26 -04001494 deferredList.flush(*this, dirty);
1495 } else {
1496 // Even if there is no drawing command(Ex: invisible),
1497 // it still needs startFrame to clear buffer and start tiling.
1498 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001499 }
1500}
1501
Romain Guy03c00b52013-06-20 18:30:28 -07001502/**
1503 * Important note: this method is intended to draw batches of bitmaps and
1504 * will not set the scissor enable or dirty the current layer, if any.
1505 * The caller is responsible for properly dirtying the current layer.
1506 */
Tom Hudson107843d2014-09-08 11:26:26 -04001507void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001508 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1509 const Rect& bounds, const SkPaint* paint) {
Romain Guy55b6f952013-06-27 15:27:09 -07001510 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001511 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001512
Chris Craik527a3aa2013-03-04 10:19:31 -08001513 const AutoTexture autoCleanup(texture);
1514
Chris Craik82840732015-04-03 09:37:49 -07001515 // TODO: remove layer dirty in multi-draw callers
1516 // TODO: snap doesn't need to touch transform, only texture filter.
1517 bool snap = pureTranslate;
Chris Craika6b52192015-02-27 17:43:02 -08001518 const float x = floorf(bounds.left + 0.5f);
1519 const float y = floorf(bounds.top + 0.5f);
Chris Craik82840732015-04-03 09:37:49 -07001520 int textureFillFlags = static_cast<int>((bitmap->colorType() == kAlpha_8_SkColorType)
1521 ? TextureFillFlags::kIsAlphaMaskTexture : TextureFillFlags::kNone);
1522 Glop glop;
1523 GlopBuilder(mRenderState, mCaches, &glop)
1524 .setMeshTexturedMesh(vertices, bitmapCount * 6)
1525 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1526 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
1527 .setModelViewOffsetRectOptionalSnap(snap, x, y, Rect(0, 0, bounds.getWidth(), bounds.getHeight()))
1528 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1529 .build();
1530 renderGlop(glop);
Chris Craik527a3aa2013-03-04 10:19:31 -08001531}
1532
Tom Hudson107843d2014-09-08 11:26:26 -04001533void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07001534 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04001535 return;
Romain Guy6926c722010-07-12 20:20:03 -07001536 }
1537
Chris Craik44eb2c02015-01-29 09:45:09 -08001538 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07001539 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001540 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001541 const AutoTexture autoCleanup(texture);
1542
Chris Craik82840732015-04-03 09:37:49 -07001543 int textureFillFlags = static_cast<int>((bitmap->colorType() == kAlpha_8_SkColorType)
1544 ? TextureFillFlags::kIsAlphaMaskTexture : TextureFillFlags::kNone);
1545 Glop glop;
1546 GlopBuilder(mRenderState, mCaches, &glop)
1547 .setMeshTexturedUnitQuad(texture->uvMapper)
1548 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1549 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
1550 .setModelViewMapUnitToRectSnap(Rect(0, 0, texture->width, texture->height))
1551 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1552 .build();
1553 renderGlop(glop);
Romain Guyce0537b2010-06-29 21:05:21 -07001554}
1555
Tom Hudson107843d2014-09-08 11:26:26 -04001556void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08001557 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001558 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04001559 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08001560 }
1561
Romain Guyb18d2d02011-02-10 15:52:54 -08001562 float left = FLT_MAX;
1563 float top = FLT_MAX;
1564 float right = FLT_MIN;
1565 float bottom = FLT_MIN;
1566
Chris Craikef250742015-02-25 17:16:16 -08001567 const uint32_t elementCount = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001568
Chris Craikef250742015-02-25 17:16:16 -08001569 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[elementCount]);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001570 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08001571
Chris Craik51d6a3d2014-12-22 17:16:56 -08001572 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08001573 if (!colors) {
1574 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08001575 tempColors.reset(new int[colorsCount]);
1576 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
1577 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08001578 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001579
John Reckebd52612014-12-10 16:47:36 -08001580 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07001581 const UvMapper& mapper(getMapper(texture));
1582
Romain Guy5a7b4662011-01-20 19:09:30 -08001583 for (int32_t y = 0; y < meshHeight; y++) {
1584 for (int32_t x = 0; x < meshWidth; x++) {
1585 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1586
1587 float u1 = float(x) / meshWidth;
1588 float u2 = float(x + 1) / meshWidth;
1589 float v1 = float(y) / meshHeight;
1590 float v2 = float(y + 1) / meshHeight;
1591
Romain Guy3b748a42013-04-17 18:54:38 -07001592 mapper.map(u1, v1, u2, v2);
1593
Romain Guy5a7b4662011-01-20 19:09:30 -08001594 int ax = i + (meshWidth + 1) * 2;
1595 int ay = ax + 1;
1596 int bx = i;
1597 int by = bx + 1;
1598 int cx = i + 2;
1599 int cy = cx + 1;
1600 int dx = i + (meshWidth + 1) * 2 + 2;
1601 int dy = dx + 1;
1602
Romain Guyff316ec2013-02-13 18:39:43 -08001603 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1604 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
1605 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08001606
Romain Guyff316ec2013-02-13 18:39:43 -08001607 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1608 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
1609 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08001610
Romain Guya92bb4d2012-10-16 11:08:44 -07001611 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1612 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1613 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1614 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001615 }
1616 }
1617
Chris Craikf0a59072013-11-19 18:00:46 -08001618 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001619 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07001620 }
1621
Romain Guyff316ec2013-02-13 18:39:43 -08001622 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07001623 texture = mCaches.textureCache.get(bitmap);
1624 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04001625 return;
Romain Guy3b748a42013-04-17 18:54:38 -07001626 }
Romain Guyff316ec2013-02-13 18:39:43 -08001627 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001628 const AutoTexture autoCleanup(texture);
1629
Chris Craik82840732015-04-03 09:37:49 -07001630 /*
1631 * TODO: handle alpha_8 textures correctly by applying paint color, but *not*
1632 * shader in that case to mimic the behavior in SkiaCanvas::drawBitmapMesh.
1633 */
1634 Glop glop;
1635 GlopBuilder(mRenderState, mCaches, &glop)
1636 .setMeshColoredTexturedMesh(mesh.get(), elementCount)
1637 .setFillTexturePaint(*texture, static_cast<int>(TextureFillFlags::kNone), paint, currentSnapshot()->alpha)
1638 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
1639 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
1640 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1641 .build();
1642 renderGlop(glop);
Romain Guy5a7b4662011-01-20 19:09:30 -08001643}
1644
Chris Craik14100ac2015-02-24 13:46:29 -08001645void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, Rect src, Rect dst, const SkPaint* paint) {
1646 if (quickRejectSetupScissor(dst)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001647 return;
Romain Guy6926c722010-07-12 20:20:03 -07001648 }
1649
Romain Guy3b748a42013-04-17 18:54:38 -07001650 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001651 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001652 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001653
Chris Craik82840732015-04-03 09:37:49 -07001654 Rect uv(fmax(0.0f, src.left / texture->width),
1655 fmax(0.0f, src.top / texture->height),
1656 fmin(1.0f, src.right / texture->width),
1657 fmin(1.0f, src.bottom / texture->height));
Chris Craik14100ac2015-02-24 13:46:29 -08001658
Chris Craik82840732015-04-03 09:37:49 -07001659 int textureFillFlags = static_cast<int>((bitmap->colorType() == kAlpha_8_SkColorType)
1660 ? TextureFillFlags::kIsAlphaMaskTexture : TextureFillFlags::kNone);
1661 Glop glop;
1662 GlopBuilder(mRenderState, mCaches, &glop)
1663 .setMeshTexturedUvQuad(texture->uvMapper, uv)
1664 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1665 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
1666 .setModelViewMapUnitToRectSnap(dst)
1667 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1668 .build();
1669 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07001670}
1671
Tom Hudson107843d2014-09-08 11:26:26 -04001672void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08001673 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
1674 const SkPaint* paint) {
Chris Craika6b52192015-02-27 17:43:02 -08001675 if (!mesh || !mesh->verticesCount || quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001676 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07001677 }
1678
Chris Craika6b52192015-02-27 17:43:02 -08001679 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
1680 if (!texture) return;
Chris Craik0556d902015-03-03 09:54:14 -08001681
Chris Craik82840732015-04-03 09:37:49 -07001682 // 9 patches are built for stretching - always filter
1683 int textureFillFlags = static_cast<int>(TextureFillFlags::kForceFilter);
1684 if (bitmap->colorType() == kAlpha_8_SkColorType) {
1685 textureFillFlags |= TextureFillFlags::kIsAlphaMaskTexture;
Chris Craik0556d902015-03-03 09:54:14 -08001686 }
Chris Craik82840732015-04-03 09:37:49 -07001687 Glop glop;
1688 GlopBuilder(mRenderState, mCaches, &glop)
1689 .setMeshPatchQuads(*mesh)
1690 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1691 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
1692 .setModelViewOffsetRectSnap(left, top, Rect(0, 0, right - left, bottom - top)) // TODO: get minimal bounds from patch
1693 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1694 .build();
1695 renderGlop(glop);
Romain Guyf7f93552010-07-08 19:17:03 -07001696}
1697
Romain Guy03c00b52013-06-20 18:30:28 -07001698/**
1699 * Important note: this method is intended to draw batches of 9-patch objects and
1700 * will not set the scissor enable or dirty the current layer, if any.
1701 * The caller is responsible for properly dirtying the current layer.
1702 */
Tom Hudson107843d2014-09-08 11:26:26 -04001703void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craika6b52192015-02-27 17:43:02 -08001704 TextureVertex* vertices, uint32_t elementCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001705 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07001706 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001707 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07001708 const AutoTexture autoCleanup(texture);
1709
Chris Craik82840732015-04-03 09:37:49 -07001710 // TODO: get correct bounds from caller
1711 // 9 patches are built for stretching - always filter
1712 int textureFillFlags = static_cast<int>(TextureFillFlags::kForceFilter);
1713 if (bitmap->colorType() == kAlpha_8_SkColorType) {
1714 textureFillFlags |= TextureFillFlags::kIsAlphaMaskTexture;
Chris Craika6b52192015-02-27 17:43:02 -08001715 }
Chris Craik82840732015-04-03 09:37:49 -07001716 Glop glop;
1717 GlopBuilder(mRenderState, mCaches, &glop)
1718 .setMeshTexturedIndexedQuads(vertices, elementCount)
1719 .setFillTexturePaint(*texture, textureFillFlags, paint, currentSnapshot()->alpha)
1720 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
1721 .setModelViewOffsetRect(0, 0, Rect(0, 0, 0, 0))
1722 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1723 .build();
1724 renderGlop(glop);
Romain Guy03c00b52013-06-20 18:30:28 -07001725}
1726
Tom Hudson107843d2014-09-08 11:26:26 -04001727void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07001728 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001729 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07001730 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08001731 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04001732 return;
Chris Craik65cd6122012-12-10 17:56:27 -08001733 }
1734
Chris Craik82840732015-04-03 09:37:49 -07001735 bool fudgeOffset = displayFlags & kVertexBuffer_Offset;
1736 bool shadowInterp = displayFlags & kVertexBuffer_ShadowInterp;
1737 Glop glop;
1738 GlopBuilder(mRenderState, mCaches, &glop)
1739 .setMeshVertexBuffer(vertexBuffer, shadowInterp)
1740 .setFillPaint(*paint, currentSnapshot()->alpha)
1741 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), fudgeOffset)
1742 .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds())
1743 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1744 .build();
1745 renderGlop(glop);
Chet Haase858aa932011-05-12 09:06:00 -07001746}
1747
1748/**
Chris Craik65cd6122012-12-10 17:56:27 -08001749 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
1750 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
1751 * screen space in all directions. However, instead of using a fragment shader to compute the
1752 * translucency of the color from its position, we simply use a varying parameter to define how far
1753 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
1754 *
1755 * Doesn't yet support joins, caps, or path effects.
1756 */
Tom Hudson107843d2014-09-08 11:26:26 -04001757void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08001758 VertexBuffer vertexBuffer;
1759 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08001760 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04001761 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08001762}
1763
1764/**
1765 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
1766 * and additional geometry for defining an alpha slope perimeter.
1767 *
1768 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
1769 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
1770 * in-shader alpha region, but found it to be taxing on some GPUs.
1771 *
1772 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
1773 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07001774 */
Tom Hudson107843d2014-09-08 11:26:26 -04001775void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001776 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07001777
Chris Craik65cd6122012-12-10 17:56:27 -08001778 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07001779
Chris Craik65cd6122012-12-10 17:56:27 -08001780 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001781 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
1782 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08001783
Chris Craik05f3d6e2014-06-02 16:27:04 -07001784 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001785 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08001786 }
1787
Chris Craikbf759452014-08-11 16:00:44 -07001788 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001789 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07001790}
1791
Tom Hudson107843d2014-09-08 11:26:26 -04001792void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04001793 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001794
Chris Craik6d29c8d2013-05-08 18:35:44 -07001795 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07001796
Chris Craik6d29c8d2013-05-08 18:35:44 -07001797 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07001798 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08001799
Chris Craik05f3d6e2014-06-02 16:27:04 -07001800 const Rect& bounds = buffer.getBounds();
1801 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001802 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07001803 }
1804
Chris Craikbf759452014-08-11 16:00:44 -07001805 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04001806 drawVertexBuffer(buffer, paint, displayFlags);
1807
1808 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07001809}
1810
Tom Hudson107843d2014-09-08 11:26:26 -04001811void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001812 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04001813 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001814
Rob Tsuk487a92c2015-01-06 13:22:54 -08001815 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07001816 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001817
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001818 SkPaint paint;
1819 paint.setColor(color);
1820 paint.setXfermodeMode(mode);
1821
1822 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07001823
Tom Hudson107843d2014-09-08 11:26:26 -04001824 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07001825}
Romain Guy9d5316e2010-06-24 19:30:36 -07001826
Chris Craik30036092015-02-12 10:41:39 -08001827void OpenGLRenderer::drawShape(float left, float top, PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08001828 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04001829 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001830 const AutoTexture autoCleanup(texture);
1831
1832 const float x = left + texture->left - texture->offset;
1833 const float y = top + texture->top - texture->offset;
1834
1835 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07001836
Tom Hudson107843d2014-09-08 11:26:26 -04001837 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08001838}
1839
Tom Hudson107843d2014-09-08 11:26:26 -04001840void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001841 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001842 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001843 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001844 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001845 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001846 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001847
Chris Craike84a2082014-12-22 14:28:49 -08001848 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001849 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001850 PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07001851 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001852 drawShape(left, top, texture, p);
1853 } else {
1854 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
1855 *currentTransform(), *p, right - left, bottom - top, rx, ry);
1856 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001857 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001858}
1859
Tom Hudson107843d2014-09-08 11:26:26 -04001860void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001861 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001862 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001863 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001864 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001865 }
Chris Craike84a2082014-12-22 14:28:49 -08001866 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001867 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001868 PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001869 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07001870 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04001871 SkPath path;
1872 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1873 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
1874 } else {
1875 path.addCircle(x, y, radius);
1876 }
1877 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07001878 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001879}
Romain Guy01d58e42011-01-19 21:54:02 -08001880
Tom Hudson107843d2014-09-08 11:26:26 -04001881void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001882 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001883 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001884 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001885 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001886 return;
Chris Craik710f46d2012-09-17 17:25:49 -07001887 }
Romain Guy01d58e42011-01-19 21:54:02 -08001888
Chris Craike84a2082014-12-22 14:28:49 -08001889 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001890 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001891 PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001892 drawShape(left, top, texture, p);
1893 } else {
1894 SkPath path;
1895 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1896 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1897 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1898 }
1899 path.addOval(rect);
1900 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07001901 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001902}
1903
Tom Hudson107843d2014-09-08 11:26:26 -04001904void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001905 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001906 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001907 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001908 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001909 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08001910 }
1911
Chris Craik780c1282012-10-04 14:10:49 -07001912 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08001913 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001914 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001915 PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07001916 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001917 drawShape(left, top, texture, p);
1918 return;
Chris Craik780c1282012-10-04 14:10:49 -07001919 }
Chris Craik780c1282012-10-04 14:10:49 -07001920 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1921 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1922 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1923 }
1924
1925 SkPath path;
1926 if (useCenter) {
1927 path.moveTo(rect.centerX(), rect.centerY());
1928 }
1929 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
1930 if (useCenter) {
1931 path.close();
1932 }
Tom Hudson107843d2014-09-08 11:26:26 -04001933 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08001934}
1935
Romain Guycf8675e2012-10-02 12:32:25 -07001936// See SkPaintDefaults.h
1937#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
1938
Tom Hudson107843d2014-09-08 11:26:26 -04001939void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001940 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04001941 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07001942 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001943 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001944 return;
Romain Guy6926c722010-07-12 20:20:03 -07001945 }
1946
Chris Craik710f46d2012-09-17 17:25:49 -07001947 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07001948 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08001949 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07001950 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001951 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08001952 PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07001953 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04001954 drawShape(left, top, texture, p);
1955 } else {
1956 SkPath path;
1957 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
1958 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
1959 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
1960 }
1961 path.addRect(rect);
1962 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07001963 }
Chet Haase858aa932011-05-12 09:06:00 -07001964 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04001965 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
1966 SkPath path;
1967 path.addRect(left, top, right, bottom);
1968 drawConvexPath(path, p);
1969 } else {
1970 drawColorRect(left, top, right, bottom, p);
1971
1972 mDirty = true;
1973 }
Chet Haase858aa932011-05-12 09:06:00 -07001974 }
Romain Guyc7d53492010-06-25 13:41:57 -07001975}
Romain Guy9d5316e2010-06-24 19:30:36 -07001976
Chris Craikd218a922014-01-02 17:13:34 -08001977void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
1978 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001979 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001980 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07001981
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04001982 TextShadow textShadow;
1983 if (!getTextShadow(paint, &textShadow)) {
1984 LOG_ALWAYS_FATAL("failed to query shadow attributes");
1985 }
1986
Raph Levien416a8472012-07-19 22:48:17 -07001987 // NOTE: The drop shadow will not perform gamma correction
1988 // if shader-based correction is enabled
1989 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Chris Craik2bb8f562015-02-17 16:42:02 -08001990 ShadowTexture* texture = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04001991 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07001992 // If the drop shadow exceeds the max texture size or couldn't be
1993 // allocated, skip drawing
Chris Craik2bb8f562015-02-17 16:42:02 -08001994 if (!texture) return;
1995 const AutoTexture autoCleanup(texture);
Raph Levien416a8472012-07-19 22:48:17 -07001996
Chris Craik2bb8f562015-02-17 16:42:02 -08001997 const float sx = x - texture->left + textShadow.dx;
1998 const float sy = y - texture->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07001999
Chris Craik82840732015-04-03 09:37:49 -07002000 Glop glop;
2001 GlopBuilder(mRenderState, mCaches, &glop)
2002 .setMeshTexturedUnitQuad(nullptr)
2003 .setFillShadowTexturePaint(*texture, textShadow.color, *paint, currentSnapshot()->alpha)
2004 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
2005 .setModelViewMapUnitToRect(Rect(sx, sy, sx + texture->width, sy + texture->height))
2006 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2007 .build();
2008 renderGlop(glop);
Raph Levien416a8472012-07-19 22:48:17 -07002009}
2010
Romain Guy768bffc2013-02-27 13:50:45 -08002011bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002012 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05002013 return MathUtils::isZero(alpha)
2014 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002015}
2016
Tom Hudson107843d2014-09-08 11:26:26 -04002017void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002018 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002019 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002020 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002021 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002022
Romain Guy671d6cf2012-01-18 12:39:17 -08002023 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002024 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002025 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002026 }
2027
Chris Craik65fe5ee2015-01-26 18:06:29 -08002028 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002029
Romain Guy671d6cf2012-01-18 12:39:17 -08002030 float x = 0.0f;
2031 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002032 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002033 if (pureTranslate) {
Chris Craika6b52192015-02-27 17:43:02 -08002034 x = floorf(x + currentTransform()->getTranslateX() + 0.5f);
2035 y = floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002036 }
2037
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002038 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002039 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002040
2041 int alpha;
2042 SkXfermode::Mode mode;
2043 getAlphaAndMode(paint, &alpha, &mode);
2044
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002045 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002046 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002047 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002048 }
2049
Romain Guy671d6cf2012-01-18 12:39:17 -08002050 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002051 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002052 if (pureTranslate && !linearFilter) {
2053 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2054 }
Romain Guy257ae352013-03-20 16:31:12 -07002055 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002056
Rob Tsuk487a92c2015-01-06 13:22:54 -08002057 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002058 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2059
Chris Craik82840732015-04-03 09:37:49 -07002060 TextDrawFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002061 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craik083e7332015-02-27 17:04:20 -08002062 positions, hasLayer() ? &bounds : nullptr, &functor)) {
2063 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2064 mDirty = true;
Romain Guy671d6cf2012-01-18 12:39:17 -08002065 }
Chet Haase48659092012-05-31 15:21:51 -07002066
Romain Guyeb9a5362012-01-17 17:39:26 -08002067}
2068
Chris Craik59744b72014-07-01 17:56:52 -07002069bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002070 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002071 outMatrix->setIdentity();
2072 return false;
2073 } else if (CC_UNLIKELY(transform.isPerspective())) {
2074 outMatrix->setIdentity();
2075 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002076 }
Chris Craik59744b72014-07-01 17:56:52 -07002077
2078 /**
Chris Craika736cd92014-08-04 13:18:38 -07002079 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2080 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002081 */
2082 float sx, sy;
2083 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002084 outMatrix->setScale(
2085 roundf(fmaxf(1.0f, sx)),
2086 roundf(fmaxf(1.0f, sy)));
2087 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002088}
2089
Tom Hudson984162f2014-10-10 13:38:16 -04002090int OpenGLRenderer::getSaveCount() const {
2091 return mState.getSaveCount();
2092}
2093
2094int OpenGLRenderer::save(int flags) {
2095 return mState.save(flags);
2096}
2097
2098void OpenGLRenderer::restore() {
Chris Craike2bb3802015-03-13 15:07:52 -07002099 mState.restore();
Tom Hudson984162f2014-10-10 13:38:16 -04002100}
2101
2102void OpenGLRenderer::restoreToCount(int saveCount) {
Chris Craike2bb3802015-03-13 15:07:52 -07002103 mState.restoreToCount(saveCount);
Tom Hudson984162f2014-10-10 13:38:16 -04002104}
2105
2106void OpenGLRenderer::translate(float dx, float dy, float dz) {
Chris Craike2bb3802015-03-13 15:07:52 -07002107 mState.translate(dx, dy, dz);
Tom Hudson984162f2014-10-10 13:38:16 -04002108}
2109
2110void OpenGLRenderer::rotate(float degrees) {
Chris Craike2bb3802015-03-13 15:07:52 -07002111 mState.rotate(degrees);
Tom Hudson984162f2014-10-10 13:38:16 -04002112}
2113
2114void OpenGLRenderer::scale(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002115 mState.scale(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002116}
2117
2118void OpenGLRenderer::skew(float sx, float sy) {
Chris Craike2bb3802015-03-13 15:07:52 -07002119 mState.skew(sx, sy);
Tom Hudson984162f2014-10-10 13:38:16 -04002120}
2121
2122void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2123 mState.setMatrix(matrix);
2124}
2125
2126void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2127 mState.concatMatrix(matrix);
2128}
2129
2130bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2131 return mState.clipRect(left, top, right, bottom, op);
2132}
2133
2134bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2135 return mState.clipPath(path, op);
2136}
2137
2138bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2139 return mState.clipRegion(region, op);
2140}
2141
2142void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2143 mState.setClippingOutline(allocator, outline);
2144}
2145
2146void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2147 const Rect& rect, float radius, bool highPriority) {
2148 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2149}
2150
Tom Hudson107843d2014-09-08 11:26:26 -04002151void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002152 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002153 DrawOpMode drawOpMode) {
2154
Chris Craik03ae2722015-02-25 11:01:09 -08002155 if (drawOpMode == DrawOpMode::kImmediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002156 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2157 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002158 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002159 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002160 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002161 }
Romain Guycac5fd32011-12-01 20:08:50 -08002162 }
2163
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002164 const float oldX = x;
2165 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002166
Chris Craikd6b65f62014-01-01 14:45:21 -08002167 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002168 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002169
Romain Guy211370f2012-02-01 16:10:55 -08002170 if (CC_LIKELY(pureTranslate)) {
Chris Craika6b52192015-02-27 17:43:02 -08002171 x = floorf(x + transform.getTranslateX() + 0.5f);
2172 y = floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002173 }
2174
Romain Guy86568192010-12-14 15:55:39 -08002175 int alpha;
2176 SkXfermode::Mode mode;
2177 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002178
Romain Guyc74f45a2013-02-26 19:10:14 -08002179 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2180
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002181 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002182 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002183 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002184 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002185 }
2186
Romain Guy19d4dd82013-03-04 11:14:26 -08002187 const bool hasActiveLayer = hasLayer();
2188
Romain Guy624234f2013-03-05 16:43:31 -08002189 // We only pass a partial transform to the font renderer. That partial
2190 // matrix defines how glyphs are rasterized. Typically we want glyphs
2191 // to be rasterized at their final size on screen, which means the partial
2192 // matrix needs to take the scale factor into account.
2193 // When a partial matrix is used to transform glyphs during rasterization,
2194 // the mesh is generated with the inverse transform (in the case of scale,
2195 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2196 // apply the full transform matrix at draw time in the vertex shader.
2197 // Applying the full matrix in the shader is the easiest way to handle
2198 // rotation and perspective and allows us to always generated quads in the
2199 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002200 SkMatrix fontTransform;
2201 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2202 || fabs(y - (int) y) > 0.0f
2203 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002204 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002205 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002206
Romain Guy624234f2013-03-05 16:43:31 -08002207 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08002208 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002209 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 -07002210
Raph Levien996e57c2012-07-23 15:22:52 -07002211 bool status;
Chris Craik82840732015-04-03 09:37:49 -07002212 TextDrawFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002213
2214 // don't call issuedrawcommand, do it at end of batch
Chris Craik03ae2722015-02-25 11:01:09 -08002215 bool forceFinish = (drawOpMode != DrawOpMode::kDefer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002216 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002217 SkPaint paintCopy(*paint);
2218 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2219 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002220 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002221 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002222 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002223 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002224 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002225
Chris Craik03ae2722015-02-25 11:01:09 -08002226 if ((status || drawOpMode != DrawOpMode::kImmediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002227 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002228 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002229 }
Chris Craik41541822013-05-03 16:35:54 -07002230 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002231 }
Romain Guy694b5192010-07-21 21:33:20 -07002232
Chris Craike63f7c622013-10-17 10:30:55 -07002233 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002234
Tom Hudson107843d2014-09-08 11:26:26 -04002235 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002236}
2237
Tom Hudson107843d2014-09-08 11:26:26 -04002238void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002239 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002240 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002241 return;
Romain Guy03d58522012-02-24 17:54:07 -08002242 }
2243
Chris Craik39a908c2013-06-13 14:39:01 -07002244 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002245 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002246
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002247 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002248 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002249 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002250
2251 int alpha;
2252 SkXfermode::Mode mode;
2253 getAlphaAndMode(paint, &alpha, &mode);
Chris Craik82840732015-04-03 09:37:49 -07002254 TextDrawFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002255
Tom Hudson984162f2014-10-10 13:38:16 -04002256 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002257 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 -08002258
Romain Guy97771732012-02-28 18:17:02 -08002259 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chih-Hung Hsieh9ad6ac02015-02-27 16:11:36 -08002260 hOffset, vOffset, hasLayer() ? &bounds : nullptr, &functor)) {
Chris Craik083e7332015-02-27 17:04:20 -08002261 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2262 mDirty = true;
Romain Guy97771732012-02-28 18:17:02 -08002263 }
Romain Guy325740f2012-02-24 16:48:34 -08002264}
2265
Tom Hudson107843d2014-09-08 11:26:26 -04002266void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002267 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002268
Chris Craik44eb2c02015-01-29 09:45:09 -08002269 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002270
Chris Craik30036092015-02-12 10:41:39 -08002271 PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002272 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002273 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002274
Romain Guy8b55f372010-08-18 17:10:07 -07002275 const float x = texture->left - texture->offset;
2276 const float y = texture->top - texture->offset;
2277
Romain Guy01d58e42011-01-19 21:54:02 -08002278 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002279 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002280}
2281
Tom Hudson107843d2014-09-08 11:26:26 -04002282void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002283 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002284 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002285 }
2286
Chris Craike84a2082014-12-22 14:28:49 -08002287 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002288 if (layer->isTextureLayer()) {
2289 transform = &layer->getTransform();
2290 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002291 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002292 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002293 }
2294 }
2295
Chris Craik39a908c2013-06-13 14:39:01 -07002296 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08002297 const bool rejected = mState.calculateQuickRejectForScissor(
2298 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2299 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002300
2301 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002302 if (transform && !transform->isIdentity()) {
2303 restore();
2304 }
Tom Hudson107843d2014-09-08 11:26:26 -04002305 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002306 }
2307
Chris Craik62d307c2014-07-29 10:35:13 -07002308 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2309 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2310
Romain Guy5bb3c732012-11-29 17:52:58 -08002311 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002312
Chris Craik65fe5ee2015-01-26 18:06:29 -08002313 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08002314 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002315
Romain Guy211370f2012-02-01 16:10:55 -08002316 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002317 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002318 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2319 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002320 } else if (layer->mesh) {
Chris Craik82840732015-04-03 09:37:49 -07002321 Glop glop;
2322 GlopBuilder(mRenderState, mCaches, &glop)
2323 .setMeshTexturedIndexedQuads(layer->mesh, layer->meshElementCount)
2324 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode(), Blend::ModeOrderSwap::NoSwap)
2325 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
2326 .setModelViewOffsetRectSnap(x, y, Rect(0, 0, layer->layer.getWidth(), layer->layer.getHeight()))
2327 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2328 .build();
2329 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy3a3133d2011-02-01 22:59:58 -08002330#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07002331 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08002332#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002333 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002334
Romain Guy5bb3c732012-11-29 17:52:58 -08002335 if (layer->debugDrawUpdate) {
2336 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002337
2338 SkPaint paint;
2339 paint.setColor(0x7f00ff00);
2340 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002341 }
Romain Guyf219da52011-01-16 12:54:25 -08002342 }
Chris Craik34416ea2013-04-15 16:08:28 -07002343 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07002344
Romain Guyb2e2f242012-10-17 18:18:35 -07002345 if (transform && !transform->isIdentity()) {
2346 restore();
2347 }
2348
Tom Hudson107843d2014-09-08 11:26:26 -04002349 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08002350}
2351
Romain Guy6926c722010-07-12 20:20:03 -07002352///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002353// Draw filters
2354///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04002355void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
2356 // We should never get here since we apply the draw filter when stashing
2357 // the paints in the DisplayList.
2358 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08002359}
2360
2361///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002362// Drawing implementation
2363///////////////////////////////////////////////////////////////////////////////
2364
Chris Craikd218a922014-01-02 17:13:34 -08002365Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08002366 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002367 if (!texture) {
2368 return mCaches.textureCache.get(bitmap);
2369 }
2370 return texture;
2371}
2372
Chris Craik2bb8f562015-02-17 16:42:02 -08002373void OpenGLRenderer::drawPathTexture(PathTexture* texture, float x, float y,
2374 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002375 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08002376 return;
2377 }
2378
Chris Craik82840732015-04-03 09:37:49 -07002379 Glop glop;
2380 GlopBuilder(mRenderState, mCaches, &glop)
2381 .setMeshTexturedUnitQuad(nullptr)
2382 .setFillPathTexturePaint(*texture, *paint, currentSnapshot()->alpha)
2383 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
2384 .setModelViewMapUnitToRect(Rect(x, y, x + texture->width, y + texture->height))
2385 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2386 .build();
2387 renderGlop(glop);
Romain Guy01d58e42011-01-19 21:54:02 -08002388}
2389
Romain Guyf607bdc2010-09-10 19:20:06 -07002390// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002391#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2392#define kStdUnderline_Offset (1.0f / 9.0f)
2393#define kStdUnderline_Thickness (1.0f / 18.0f)
2394
Chris Craikd218a922014-01-02 17:13:34 -08002395void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
2396 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07002397 // Handle underline and strike-through
2398 uint32_t flags = paint->getFlags();
2399 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002400 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002401
Romain Guy211370f2012-02-01 16:10:55 -08002402 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002403 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002404 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002405
Raph Levien8b4072d2012-07-30 15:50:00 -07002406 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002407 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002408
Romain Guyf6834472011-01-23 13:32:12 -08002409 int linesCount = 0;
2410 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2411 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2412
2413 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002414 float points[pointsCount];
2415 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002416
2417 if (flags & SkPaint::kUnderlineText_Flag) {
2418 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002419 points[currentPoint++] = left;
2420 points[currentPoint++] = top;
2421 points[currentPoint++] = left + underlineWidth;
2422 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002423 }
2424
2425 if (flags & SkPaint::kStrikeThruText_Flag) {
2426 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002427 points[currentPoint++] = left;
2428 points[currentPoint++] = top;
2429 points[currentPoint++] = left + underlineWidth;
2430 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002431 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002432
Romain Guy726aeba2011-06-01 14:52:00 -07002433 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002434
Romain Guy726aeba2011-06-01 14:52:00 -07002435 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002436 }
2437 }
2438}
2439
Tom Hudson107843d2014-09-08 11:26:26 -04002440void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002441 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002442 return;
Romain Guy672433d2013-01-04 19:05:13 -08002443 }
2444
Tom Hudson107843d2014-09-08 11:26:26 -04002445 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08002446}
2447
Tom Hudson107843d2014-09-08 11:26:26 -04002448void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07002449 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04002450 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07002451
Chris Craik15a07a22014-01-26 13:43:53 -08002452 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08002453 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07002454
2455 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08002456 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07002457
ztenghui14a4e352014-08-13 10:44:39 -07002458 // The caller has made sure casterAlpha > 0.
2459 float ambientShadowAlpha = mAmbientShadowAlpha;
2460 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
2461 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
2462 }
2463 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
2464 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002465 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002466 }
ztenghui7b4516e2014-01-07 10:42:55 -08002467
ztenghui14a4e352014-08-13 10:44:39 -07002468 float spotShadowAlpha = mSpotShadowAlpha;
2469 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
2470 spotShadowAlpha = mCaches.propertySpotShadowStrength;
2471 }
2472 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
2473 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07002474 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08002475 }
ztenghui7b4516e2014-01-07 10:42:55 -08002476
Tom Hudson107843d2014-09-08 11:26:26 -04002477 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07002478}
2479
Tom Hudson107843d2014-09-08 11:26:26 -04002480void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002481 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08002482 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04002483 return;
Romain Guy3b753822013-03-05 10:27:35 -08002484 }
Romain Guy735738c2012-12-03 12:34:51 -08002485
Romain Guy672433d2013-01-04 19:05:13 -08002486 float left = FLT_MAX;
2487 float top = FLT_MAX;
2488 float right = FLT_MIN;
2489 float bottom = FLT_MIN;
2490
Romain Guy448455f2013-07-22 13:57:50 -07002491 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08002492 Vertex* vertex = mesh;
2493
Chris Craik2af46352012-11-26 18:30:17 -08002494 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08002495 float l = rects[index + 0];
2496 float t = rects[index + 1];
2497 float r = rects[index + 2];
2498 float b = rects[index + 3];
2499
Romain Guy3b753822013-03-05 10:27:35 -08002500 Vertex::set(vertex++, l, t);
2501 Vertex::set(vertex++, r, t);
2502 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08002503 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08002504
Romain Guy3b753822013-03-05 10:27:35 -08002505 left = fminf(left, l);
2506 top = fminf(top, t);
2507 right = fmaxf(right, r);
2508 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08002509 }
2510
Chris Craikf0a59072013-11-19 18:00:46 -08002511 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002512 return;
Romain Guya362c692013-02-04 13:50:16 -08002513 }
Romain Guy672433d2013-01-04 19:05:13 -08002514
Chris Craik82840732015-04-03 09:37:49 -07002515 const Matrix4& transform = ignoreTransform ? Matrix4::identity() : *currentTransform();
2516 Glop glop;
2517 GlopBuilder(mRenderState, mCaches, &glop)
2518 .setMeshIndexedQuads(&mesh[0], count / 4)
2519 .setFillPaint(*paint, currentSnapshot()->alpha)
2520 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
2521 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
2522 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2523 .build();
2524 renderGlop(glop);
Romain Guy672433d2013-01-04 19:05:13 -08002525}
2526
Romain Guy026c5e162010-06-28 17:12:22 -07002527void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002528 const SkPaint* paint, bool ignoreTransform) {
Chris Craik82840732015-04-03 09:37:49 -07002529 const Matrix4& transform = ignoreTransform ? Matrix4::identity() : *currentTransform();
2530 Glop glop;
2531 GlopBuilder(mRenderState, mCaches, &glop)
2532 .setMeshUnitQuad()
2533 .setFillPaint(*paint, currentSnapshot()->alpha)
2534 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
2535 .setModelViewMapUnitToRect(Rect(left, top, right, bottom))
2536 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2537 .build();
2538 renderGlop(glop);
Romain Guy8ba548f2010-06-30 19:21:21 -07002539}
2540
Chris Craike84a2082014-12-22 14:28:49 -08002541void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
2542 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002543 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07002544 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
2545 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07002546 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07002547 }
Chris Craikd6b65f62014-01-01 14:45:21 -08002548 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002549}
2550
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002551float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07002552 float alpha;
2553 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
2554 alpha = mDrawModifiers.mOverrideLayerAlpha;
2555 } else {
2556 alpha = layer->getAlpha() / 255.0f;
2557 }
Chris Craikd6b65f62014-01-01 14:45:21 -08002558 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07002559}
2560
Romain Guy9d5316e2010-06-24 19:30:36 -07002561}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002562}; // namespace android