blob: b10aea31772760ff4fbea670dda85dcb706935f5 [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"
21#include "Fence.h"
22#include "GammaFontRenderer.h"
Chris Craik03188872015-02-02 18:39:33 -080023#include "Glop.h"
24#include "GlopBuilder.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080025#include "Patch.h"
26#include "PathTessellator.h"
27#include "Properties.h"
28#include "RenderNode.h"
Chris Craik03188872015-02-02 18:39:33 -080029#include "renderstate/MeshState.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080030#include "renderstate/RenderState.h"
31#include "ShadowTessellator.h"
32#include "SkiaShader.h"
33#include "Vector.h"
34#include "VertexBuffer.h"
35#include "utils/GLUtils.h"
36#include "utils/PaintUtils.h"
37#include "utils/TraceUtils.h"
38
Romain Guye4d01122010-06-16 18:44:05 -070039#include <stdlib.h>
40#include <stdint.h>
41#include <sys/types.h>
42
Romain Guy5cbbce52010-06-27 22:59:20 -070043#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070044#include <SkColor.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040045#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070046#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070047
Romain Guye4d01122010-06-16 18:44:05 -070048#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070049#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070050
Romain Guy08aa2cb2011-03-17 11:06:57 -070051#include <private/hwui/DrawGlInfo.h>
52
Romain Guy5b3b3522010-10-27 18:57:51 -070053#include <ui/Rect.h>
54
Chris Craik62d307c2014-07-29 10:35:13 -070055#if DEBUG_DETAILED_EVENTS
56 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
57#else
58 #define EVENT_LOGD(...)
59#endif
60
Romain Guye4d01122010-06-16 18:44:05 -070061namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070062namespace uirenderer {
63
Chris Craik678625242014-02-28 12:26:34 -080064static GLenum getFilter(const SkPaint* paint) {
65 if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) {
66 return GL_LINEAR;
67 }
68 return GL_NEAREST;
69}
Romain Guyd21b6e12011-11-30 20:21:23 -080070
Romain Guy9d5316e2010-06-24 19:30:36 -070071///////////////////////////////////////////////////////////////////////////////
72// Globals
73///////////////////////////////////////////////////////////////////////////////
74
Romain Guyf607bdc2010-09-10 19:20:06 -070075
Romain Guyf6a11b82010-06-23 17:47:49 -070076///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -070077// Functions
78///////////////////////////////////////////////////////////////////////////////
79
80template<typename T>
81static inline T min(T a, T b) {
82 return a < b ? a : b;
83}
84
85///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -070086// Constructors/destructor
87///////////////////////////////////////////////////////////////////////////////
88
John Reck3b202512014-06-23 13:13:08 -070089OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -040090 : mState(*this)
Chris Craik058fc642014-07-23 18:19:28 -070091 , mCaches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -070092 , mExtensions(Extensions::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -070093 , mRenderState(renderState)
Chris Craik65fe5ee2015-01-26 18:06:29 -080094 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -070095 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -070096 , mSuppressTiling(false)
97 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -040098 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -070099 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -0700100 , mLightRadius(FLT_MIN)
101 , mAmbientShadowAlpha(0)
102 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800103 // *set* draw modifiers to be 0
104 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700105 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700106
Chris Craik03188872015-02-02 18:39:33 -0800107 memcpy(mMeshVertices, kUnitQuadVertices, sizeof(kUnitQuadVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700108}
109
Romain Guy85bf02f2010-06-22 13:11:24 -0700110OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700111 // The context has already been destroyed at this point, do not call
112 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700113}
114
Romain Guy87e2f7572012-09-24 11:37:12 -0700115void OpenGLRenderer::initProperties() {
116 char property[PROPERTY_VALUE_MAX];
117 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
118 mScissorOptimizationDisabled = !strcasecmp(property, "true");
119 INIT_LOGD(" Scissor optimization %s",
120 mScissorOptimizationDisabled ? "disabled" : "enabled");
121 } else {
122 INIT_LOGD(" Scissor optimization enabled");
123 }
Romain Guy13631f32012-01-30 17:41:55 -0800124}
125
Chris Craik058fc642014-07-23 18:19:28 -0700126void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
127 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
128 mLightCenter = lightCenter;
129 mLightRadius = lightRadius;
130 mAmbientShadowAlpha = ambientShadowAlpha;
131 mSpotShadowAlpha = spotShadowAlpha;
132}
133
Romain Guy13631f32012-01-30 17:41:55 -0800134///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700135// Setup
136///////////////////////////////////////////////////////////////////////////////
137
Chris Craik797b95b22014-05-20 18:10:25 -0700138void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700139 glDisable(GL_DITHER);
140 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Chris Craik284b2432014-09-18 16:05:35 -0700141 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700142}
143
Romain Guy96885eb2013-03-26 15:05:58 -0700144void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800145 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800146 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400147 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700148 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700149 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700150}
151
Tom Hudson107843d2014-09-08 11:26:26 -0400152void OpenGLRenderer::startFrame() {
153 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700154 mFrameStarted = true;
155
Tom Hudson984162f2014-10-10 13:38:16 -0400156 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700157
Romain Guy96885eb2013-03-26 15:05:58 -0700158 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700159
Tom Hudson984162f2014-10-10 13:38:16 -0400160 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800161
Romain Guy54c1a642012-09-27 17:55:46 -0700162 // Functors break the tiling extension in pretty spectacular ways
163 // This ensures we don't use tiling when a functor is going to be
164 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700165 mSuppressTiling = mCaches.hasRegisteredFunctors()
166 || mFirstFrameAfterResize;
167 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700168
Chris Craikd6b65f62014-01-01 14:45:21 -0800169 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700170
Romain Guy7c450aa2012-09-21 19:15:00 -0700171 debugOverdraw(true, true);
172
Tom Hudson107843d2014-09-08 11:26:26 -0400173 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700174 mTilingClip.right, mTilingClip.bottom, mOpaque);
175}
176
Tom Hudson107843d2014-09-08 11:26:26 -0400177void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700178 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700179
Romain Guy96885eb2013-03-26 15:05:58 -0700180 setupFrameState(left, top, right, bottom, opaque);
181
182 // Layer renderers will start the frame immediately
183 // The framebuffer renderer will first defer the display list
184 // for each layer and wait until the first drawing command
185 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800186 if (currentSnapshot()->fbo == 0) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800187 mRenderState.blend().syncEnabled();
Romain Guy96885eb2013-03-26 15:05:58 -0700188 updateLayers();
189 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400190 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700191 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700192}
193
Romain Guydcfc8362013-01-03 13:08:57 -0800194void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
195 // If we know that we are going to redraw the entire framebuffer,
196 // perform a discard to let the driver know we don't need to preserve
197 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800198 if (mExtensions.hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400199 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
200 const bool isFbo = onGetTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800201 const GLenum attachments[] = {
202 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
203 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800204 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
205 }
206}
207
Tom Hudson107843d2014-09-08 11:26:26 -0400208void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700209 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800210 mRenderState.scissor().setEnabled(true);
211 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700212 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400213 mDirty = true;
214 return;
Romain Guyddf74372012-05-22 14:07:07 -0700215 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700216
Chris Craik65fe5ee2015-01-26 18:06:29 -0800217 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700218}
219
henry.uh_chen33f5a592014-07-02 19:36:56 +0800220void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700221 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800222 const Snapshot* snapshot = currentSnapshot();
223
Chris Craik14e51302013-12-30 15:32:54 -0800224 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800225 if (snapshot->flags & Snapshot::kFlagFboTarget) {
226 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700227 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700228
henry.uh_chen33f5a592014-07-02 19:36:56 +0800229 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800230 }
231}
232
henry.uh_chen33f5a592014-07-02 19:36:56 +0800233void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800234 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800235 if(expand) {
236 // Expand the startTiling region by 1
237 int leftNotZero = (clip.left > 0) ? 1 : 0;
238 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
239
240 mCaches.startTiling(
241 clip.left - leftNotZero,
242 windowHeight - clip.bottom - topNotZero,
243 clip.right - clip.left + leftNotZero + 1,
244 clip.bottom - clip.top + topNotZero + 1,
245 opaque);
246 } else {
247 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800248 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800249 }
Romain Guy54c1a642012-09-27 17:55:46 -0700250 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700251}
252
253void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700254 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700255}
256
Tom Hudson107843d2014-09-08 11:26:26 -0400257bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700258 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700259 endTiling();
Chris Craik4ac36f82014-12-09 16:54:03 -0800260 mTempPaths.clear();
261
Romain Guyca89e2a2013-03-08 17:44:20 -0800262 // When finish() is invoked on FBO 0 we've reached the end
263 // of the current frame
Tom Hudson984162f2014-10-10 13:38:16 -0400264 if (onGetTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800265 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700266 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800267 }
268
Romain Guy11cb6422012-09-21 00:39:43 -0700269 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700270#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700271 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700272#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700273
Romain Guyc15008e2010-11-10 11:59:15 -0800274#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800275 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700276#else
277 if (mCaches.getDebugLevel() & kDebugMemory) {
278 mCaches.dumpMemoryUsage();
279 }
Romain Guyc15008e2010-11-10 11:59:15 -0800280#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700281 }
Romain Guy96885eb2013-03-26 15:05:58 -0700282
283 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400284
285 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700286}
287
Romain Guy35643dd2012-09-18 15:40:58 -0700288void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700289 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
290 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700291 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700292
Chris Craik65fe5ee2015-01-26 18:06:29 -0800293 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700294 dirtyClip();
295}
296
Andreas Gampe64bb4132014-11-22 00:35:09 +0000297void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400298 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700299
Rob Tsuk487a92c2015-01-06 13:22:54 -0800300 Rect clip(mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700301 clip.snapToPixelBoundaries();
302
Romain Guyd643bb52011-03-01 14:55:21 -0800303 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800304 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800305 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800306 dirtyLayerUnchecked(clip, getRegion());
307 }
Romain Guyd643bb52011-03-01 14:55:21 -0800308
Romain Guy08aa2cb2011-03-17 11:06:57 -0700309 DrawGlInfo info;
310 info.clipLeft = clip.left;
311 info.clipTop = clip.top;
312 info.clipRight = clip.right;
313 info.clipBottom = clip.bottom;
314 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700315 info.width = getViewportWidth();
316 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800317 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700318
Tom Hudson984162f2014-10-10 13:38:16 -0400319 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700320 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400321 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700322 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
323 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800324 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700325 setScissorFromClip();
326 }
Chris Craik54f574a2013-08-26 11:23:46 -0700327
John Reck3b202512014-06-23 13:13:08 -0700328 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
329 // Scissor may have been modified, reset dirty clip
330 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800331
Tom Hudson107843d2014-09-08 11:26:26 -0400332 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800333}
334
Romain Guyf6a11b82010-06-23 17:47:49 -0700335///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700336// Debug
337///////////////////////////////////////////////////////////////////////////////
338
Chris Craik62d307c2014-07-29 10:35:13 -0700339void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
340#if DEBUG_DETAILED_EVENTS
341 const int BUFFER_SIZE = 256;
342 va_list ap;
343 char buf[BUFFER_SIZE];
344
345 va_start(ap, fmt);
346 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
347 va_end(ap);
348
349 eventMark(buf);
350#endif
351}
352
353
Romain Guy0f6675332013-03-01 14:31:04 -0800354void OpenGLRenderer::eventMark(const char* name) const {
355 mCaches.eventMark(0, name);
356}
357
Romain Guy87e2f7572012-09-24 11:37:12 -0700358void OpenGLRenderer::startMark(const char* name) const {
359 mCaches.startMark(0, name);
360}
361
362void OpenGLRenderer::endMark() const {
363 mCaches.endMark();
364}
365
366void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700367 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700368}
369
370void OpenGLRenderer::renderOverdraw() {
Tom Hudson984162f2014-10-10 13:38:16 -0400371 if (mCaches.debugOverdraw && onGetTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700372 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700373
Chris Craik65fe5ee2015-01-26 18:06:29 -0800374 mRenderState.scissor().setEnabled(true);
375 mRenderState.scissor().set(clip->left,
376 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
377 clip->right - clip->left,
378 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700379
Romain Guy627c6fd2013-08-21 11:53:18 -0700380 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800381 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700382 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
383
384 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800385 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700386 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
387
388 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800389 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700390 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
391
392 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800393 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700394 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
395
Chris Craik96a5c4c2015-01-27 15:46:35 -0800396 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700397 }
398}
399
400///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700401// Layers
402///////////////////////////////////////////////////////////////////////////////
403
404bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700405 if (layer->deferredUpdateScheduled && layer->renderer
406 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700407
Romain Guy7c450aa2012-09-21 19:15:00 -0700408 if (inFrame) {
409 endTiling();
410 debugOverdraw(false, false);
411 }
Romain Guy11cb6422012-09-21 00:39:43 -0700412
Romain Guy96885eb2013-03-26 15:05:58 -0700413 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700414 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700415 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700416 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700417 }
Romain Guy11cb6422012-09-21 00:39:43 -0700418
419 if (inFrame) {
420 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800421 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700422 }
423
Romain Guy5bb3c732012-11-29 17:52:58 -0800424 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700425 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700426
427 return true;
428 }
429
430 return false;
431}
432
433void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700434 // If draw deferring is enabled this method will simply defer
435 // the display list of each individual layer. The layers remain
436 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700437 int count = mLayerUpdates.size();
438 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700439 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
440 startMark("Layer Updates");
441 } else {
442 startMark("Defer Layer Updates");
443 }
Romain Guy11cb6422012-09-21 00:39:43 -0700444
Chris Craik1206b9b2013-04-04 14:46:24 -0700445 // Note: it is very important to update the layers in order
446 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700447 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700448 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700449 }
Romain Guy11cb6422012-09-21 00:39:43 -0700450
Romain Guy96885eb2013-03-26 15:05:58 -0700451 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
452 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400453 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700454 }
455 endMark();
456 }
457}
458
459void OpenGLRenderer::flushLayers() {
460 int count = mLayerUpdates.size();
461 if (count > 0) {
462 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700463
Chris Craik1206b9b2013-04-04 14:46:24 -0700464 // Note: it is very important to update the layers in order
465 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800466 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700467 }
468
469 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400470 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700471
Romain Guy11cb6422012-09-21 00:39:43 -0700472 endMark();
473 }
474}
475
476void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
477 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700478 // Make sure we don't introduce duplicates.
479 // SortedVector would do this automatically but we need to respect
480 // the insertion order. The linear search is not an issue since
481 // this list is usually very short (typically one item, at most a few)
482 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
483 if (mLayerUpdates.itemAt(i) == layer) {
484 return;
485 }
486 }
Romain Guy11cb6422012-09-21 00:39:43 -0700487 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700488 }
489}
490
Romain Guye93482f2013-06-17 13:14:51 -0700491void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
492 if (layer) {
493 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
494 if (mLayerUpdates.itemAt(i) == layer) {
495 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700496 break;
497 }
498 }
499 }
500}
501
Romain Guy40543602013-06-12 15:31:28 -0700502void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800503 ATRACE_NAME("Update HW Layers");
Chris Craik44eb2c02015-01-29 09:45:09 -0800504 mRenderState.blend().syncEnabled();
Romain Guy40543602013-06-12 15:31:28 -0700505 updateLayers();
506 flushLayers();
507 // Wait for all the layer updates to be executed
508 AutoFence fence;
509}
510
John Reck443a7142014-09-04 17:40:05 -0700511void OpenGLRenderer::markLayersAsBuildLayers() {
512 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
513 mLayerUpdates[i]->wasBuildLayered = true;
514 }
515}
516
Romain Guy11cb6422012-09-21 00:39:43 -0700517///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700518// State management
519///////////////////////////////////////////////////////////////////////////////
520
Chris Craik14e51302013-12-30 15:32:54 -0800521void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700522 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800523 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
524 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700525
Chris Craika64a2be2014-05-14 14:17:01 -0700526 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700527 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700528 }
529
Romain Guy2542d192010-08-18 11:47:12 -0700530 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700531 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700532 }
Romain Guy2542d192010-08-18 11:47:12 -0700533
Romain Guy5ec99242010-11-03 16:19:08 -0700534 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700535 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700536 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700537 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800538 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700539 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700540 }
Romain Guybb9524b2010-06-22 18:56:38 -0700541}
542
Romain Guyf6a11b82010-06-23 17:47:49 -0700543///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700544// Layers
545///////////////////////////////////////////////////////////////////////////////
546
547int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700548 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700549 // force matrix/clip isolation for layer
550 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
551
Tom Hudson984162f2014-10-10 13:38:16 -0400552 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700553
Tom Hudson984162f2014-10-10 13:38:16 -0400554 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700555 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700556 }
Romain Guyd55a8612010-06-28 17:42:46 -0700557
558 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700559}
560
Chris Craikd90144d2013-03-19 15:03:48 -0700561void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
562 const Rect untransformedBounds(bounds);
563
Chris Craikd6b65f62014-01-01 14:45:21 -0800564 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700565
566 // Layers only make sense if they are in the framebuffer's bounds
Rob Tsuk487a92c2015-01-06 13:22:54 -0800567 if (bounds.intersect(mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700568 // We cannot work with sub-pixels in this case
569 bounds.snapToPixelBoundaries();
570
571 // When the layer is not an FBO, we may use glCopyTexImage so we
572 // need to make sure the layer does not extend outside the bounds
573 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700574 const Snapshot& previous = *(currentSnapshot()->previous);
575 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
576 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700577 bounds.setEmpty();
578 } else if (fboLayer) {
579 clip.set(bounds);
580 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800581 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700582 inverse.mapRect(clip);
583 clip.snapToPixelBoundaries();
584 if (clip.intersect(untransformedBounds)) {
585 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
586 bounds.set(untransformedBounds);
587 } else {
588 clip.setEmpty();
589 }
590 }
591 } else {
592 bounds.setEmpty();
593 }
594}
595
Chris Craik408eb122013-03-26 18:55:15 -0700596void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
597 bool fboLayer, int alpha) {
598 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
599 bounds.getHeight() > mCaches.maxTextureSize ||
600 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400601 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700602 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400603 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700604 }
605}
606
Chris Craikd90144d2013-03-19 15:03:48 -0700607int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500608 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400609 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700610
Tom Hudson984162f2014-10-10 13:38:16 -0400611 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700612 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
613 // operations will be able to store and restore the current clip and transform info, and
614 // quick rejection will be correct (for display lists)
615
616 Rect bounds(left, top, right, bottom);
617 Rect clip;
618 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500619 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700620
Tom Hudson984162f2014-10-10 13:38:16 -0400621 if (!mState.currentlyIgnored()) {
622 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
623 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
624 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800625 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700626 }
627 }
628
629 return count;
630}
631
Romain Guy1c740bc2010-09-13 18:00:09 -0700632/**
633 * Layers are viewed by Skia are slightly different than layers in image editing
634 * programs (for instance.) When a layer is created, previously created layers
635 * and the frame buffer still receive every drawing command. For instance, if a
636 * layer is created and a shape intersecting the bounds of the layers and the
637 * framebuffer is draw, the shape will be drawn on both (unless the layer was
638 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
639 *
640 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
641 * texture. Unfortunately, this is inefficient as it requires every primitive to
642 * be drawn n + 1 times, where n is the number of active layers. In practice this
643 * means, for every primitive:
644 * - Switch active frame buffer
645 * - Change viewport, clip and projection matrix
646 * - Issue the drawing
647 *
648 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700649 * To avoid this, layers are implemented in a different way here, at least in the
650 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
651 * is set. When this flag is set we can redirect all drawing operations into a
652 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700653 *
654 * This implementation relies on the frame buffer being at least RGBA 8888. When
655 * a layer is created, only a texture is created, not an FBO. The content of the
656 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700657 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700658 * buffer and drawing continues as normal. This technique therefore treats the
659 * frame buffer as a scratch buffer for the layers.
660 *
661 * To compose the layers back onto the frame buffer, each layer texture
662 * (containing the original frame buffer data) is drawn as a simple quad over
663 * the frame buffer. The trick is that the quad is set as the composition
664 * destination in the blending equation, and the frame buffer becomes the source
665 * of the composition.
666 *
667 * Drawing layers with an alpha value requires an extra step before composition.
668 * An empty quad is drawn over the layer's region in the frame buffer. This quad
669 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
670 * quad is used to multiply the colors in the frame buffer. This is achieved by
671 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
672 * GL_ZERO, GL_SRC_ALPHA.
673 *
674 * Because glCopyTexImage2D() can be slow, an alternative implementation might
675 * be use to draw a single clipped layer. The implementation described above
676 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700677 *
678 * (1) The frame buffer is actually not cleared right away. To allow the GPU
679 * to potentially optimize series of calls to glCopyTexImage2D, the frame
680 * buffer is left untouched until the first drawing operation. Only when
681 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700682 */
Chet Haased48885a2012-08-28 17:43:28 -0700683bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700684 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800685 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
686 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700687
Romain Guyeb993562010-10-05 18:14:38 -0700688 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
689
Romain Guyf607bdc2010-09-10 19:20:06 -0700690 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700691 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700692 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700693 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000694 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700695
696 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400697 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700698 return false;
699 }
Romain Guyf18fd992010-07-08 11:45:51 -0700700
Chris Craik44eb2c02015-01-29 09:45:09 -0800701 mCaches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700702 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700703 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700704 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700705 }
706
Derek Sollenberger674554f2014-02-19 16:47:32 +0000707 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700708 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700709 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
710 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500711
Chet Haasea23eed82012-04-12 15:19:04 -0700712 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700713 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700714 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700715
Romain Guy8fb95422010-08-17 18:38:51 -0700716 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400717 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
718 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700719
Chris Craika8bea8e2014-09-24 11:29:43 -0700720 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
721 fboLayer ? "" : "unclipped ",
722 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700723 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700724 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700725 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700726 } else {
727 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700728 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800729 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700730 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700731 // Workaround for some GL drivers. When reading pixels lying outside
732 // of the window we should get undefined values for those pixels.
733 // Unfortunately some drivers will turn the entire target texture black
734 // when reading outside of the window.
735 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800736 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700737 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800738 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700739
Chris Craika64a2be2014-05-14 14:17:01 -0700740 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
741 bounds.left, getViewportHeight() - bounds.bottom,
742 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700743
Romain Guy54be1cd2011-06-13 19:04:27 -0700744 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800745 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700746 }
Romain Guyeb993562010-10-05 18:14:38 -0700747 }
Romain Guyf86ef572010-07-01 11:05:42 -0700748
Romain Guyd55a8612010-06-28 17:42:46 -0700749 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700750}
751
Chris Craike63f7c622013-10-17 10:30:55 -0700752bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800753 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700754 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700755
Tom Hudson984162f2014-10-10 13:38:16 -0400756 writableSnapshot()->region = &writableSnapshot()->layer->region;
757 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
758 writableSnapshot()->fbo = layer->getFbo();
759 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
760 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
761 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800762 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700763
Romain Guy2b7028e2012-09-19 17:25:38 -0700764 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700765 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700766 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700767 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700768 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700769
770 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700771 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700772 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700773 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700774 }
775
776 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700777 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700778
henry.uh_chen33f5a592014-07-02 19:36:56 +0800779 // Expand the startTiling region by 1
780 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700781
782 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800783 mRenderState.scissor().setEnabled(true);
784 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700785 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700786 glClear(GL_COLOR_BUFFER_BIT);
787
788 dirtyClip();
789
790 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700791 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700792 return true;
793}
794
Romain Guy1c740bc2010-09-13 18:00:09 -0700795/**
796 * Read the documentation of createLayer() before doing anything in this method.
797 */
Chris Craik14e51302013-12-30 15:32:54 -0800798void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
799 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000800 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700801 return;
802 }
803
Chris Craik14e51302013-12-30 15:32:54 -0800804 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800805 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800806 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700807
Chris Craik39a908c2013-06-13 14:39:01 -0700808 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400809 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800810 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800811 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700812
Romain Guyeb993562010-10-05 18:14:38 -0700813 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700814 endTiling();
815
Romain Guye0aa84b2012-04-03 19:30:26 -0700816 // Detach the texture from the FBO
817 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800818
819 layer->removeFbo(false);
820
Romain Guyeb993562010-10-05 18:14:38 -0700821 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700822 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700823 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700824
Chris Craikd6b65f62014-01-01 14:45:21 -0800825 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700826 }
827
Romain Guy9ace8f52011-07-07 20:50:11 -0700828 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500829 SkPaint layerPaint;
830 layerPaint.setAlpha(layer->getAlpha());
831 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
832 layerPaint.setColorFilter(layer->getColorFilter());
833
834 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700835 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700836 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700837 }
838
Chris Craik96a5c4c2015-01-27 15:46:35 -0800839 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700840
Chris Craik44eb2c02015-01-29 09:45:09 -0800841 mCaches.textureState().activateTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700842
Romain Guy5b3b3522010-10-27 18:57:51 -0700843 // When the layer is stored in an FBO, we can save a bit of fillrate by
844 // drawing only the dirty region
845 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800846 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700847 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700848 } else if (!rect.isEmpty()) {
849 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530850
851 save(0);
852 // the layer contains screen buffer content that shouldn't be alpha modulated
853 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400854 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700855 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530856 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700857 }
Romain Guy8b55f372010-08-18 17:10:07 -0700858
Romain Guy746b7402010-10-26 16:27:31 -0700859 dirtyClip();
860
Romain Guyeb993562010-10-05 18:14:38 -0700861 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800862 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700863 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800864 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800865 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700866 }
867}
868
Romain Guyaa6c24c2011-04-28 18:40:04 -0700869void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700870 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700871
872 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700873 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700874 setupDrawWithTexture();
875 } else {
876 setupDrawWithExternalTexture();
877 }
878 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700879 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500880 setupDrawColorFilter(layer->getColorFilter());
881 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700882 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700883 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500884 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700885 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
886 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700887 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700888 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700889 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800890 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800891 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700892 layer->getWidth() == (uint32_t) rect.getWidth() &&
893 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800894 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
895 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700896
Romain Guyd21b6e12011-11-30 20:21:23 -0800897 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800898 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
899 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700900 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800901 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800902 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
903 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700904 }
905 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700906 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700907
Chris Craik96a5c4c2015-01-27 15:46:35 -0800908 glDrawArrays(GL_TRIANGLE_STRIP, 0, kMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700909}
910
Romain Guy5b3b3522010-10-27 18:57:51 -0700911void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700912 if (layer->isTextureLayer()) {
913 EVENT_LOGD("composeTextureLayerRect");
914 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
915 drawTextureLayer(layer, rect);
916 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
917 } else {
918 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700919 const Rect& texCoords = layer->texCoords;
920 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
921 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700922
Romain Guy9ace8f52011-07-07 20:50:11 -0700923 float x = rect.left;
924 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -0800925 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700926 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700927 layer->getHeight() == (uint32_t) rect.getHeight();
928
929 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700930 // When we're swapping, the layer is already in screen coordinates
931 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800932 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
933 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700934 }
935
Romain Guyd21b6e12011-11-30 20:21:23 -0800936 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700937 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800938 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700939 }
940
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500941 SkPaint layerPaint;
942 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
943 layerPaint.setXfermodeMode(layer->getMode());
944 layerPaint.setColorFilter(layer->getColorFilter());
945
946 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700947 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500948 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -0700949 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik96a5c4c2015-01-27 15:46:35 -0800950 GL_TRIANGLE_STRIP, kMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700951
Romain Guyaa6c24c2011-04-28 18:40:04 -0700952 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700953 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700954}
955
Chris Craik34416ea2013-04-15 16:08:28 -0700956/**
957 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
958 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
959 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
960 * by saveLayer's restore
961 */
Tom Hudson984162f2014-10-10 13:38:16 -0400962#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
963 DRAW_COMMAND; \
964 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
965 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
966 DRAW_COMMAND; \
967 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
968 } \
Chris Craik34416ea2013-04-15 16:08:28 -0700969 }
970
971#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
972
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400973// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
974// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
975class LayerShader : public SkShader {
976public:
977 LayerShader(Layer* layer, const SkMatrix* localMatrix)
978 : INHERITED(localMatrix)
979 , mLayer(layer) {
980 }
981
Chris Craike84a2082014-12-22 14:28:49 -0800982 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400983 if (data) {
984 *data = static_cast<void*>(mLayer);
985 }
986 return true;
987 }
988
Chris Craike84a2082014-12-22 14:28:49 -0800989 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400990 return !mLayer->isBlend();
991 }
992
993protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000994 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400995 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
996 }
997
Chris Craike84a2082014-12-22 14:28:49 -0800998 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400999 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1000 }
1001
Chris Craike84a2082014-12-22 14:28:49 -08001002 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001003 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -08001004 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001005 }
1006private:
1007 // Unowned.
1008 Layer* mLayer;
1009 typedef SkShader INHERITED;
1010};
1011
Romain Guy5b3b3522010-10-27 18:57:51 -07001012void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001013 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1014
1015 if (layer->getConvexMask()) {
1016 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1017
1018 // clip to the area of the layer the mask can be larger
1019 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1020
1021 SkPaint paint;
1022 paint.setAntiAlias(true);
1023 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1024
Chris Craik3f0854292014-04-15 16:18:08 -07001025 // create LayerShader to map SaveLayer content into subsequent draw
1026 SkMatrix shaderMatrix;
1027 shaderMatrix.setTranslate(rect.left, rect.bottom);
1028 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001029 LayerShader layerShader(layer, &shaderMatrix);
1030 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001031
1032 // Since the drawing primitive is defined in local drawing space,
1033 // we don't need to modify the draw matrix
1034 const SkPath* maskPath = layer->getConvexMask();
1035 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1036
Chris Craike84a2082014-12-22 14:28:49 -08001037 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -07001038 restore();
1039
1040 return;
1041 }
1042
Romain Guy5b3b3522010-10-27 18:57:51 -07001043 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001044 layer->setRegionAsRect();
1045
Chris Craik34416ea2013-04-15 16:08:28 -07001046 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001047
Romain Guy5b3b3522010-10-27 18:57:51 -07001048 layer->region.clear();
1049 return;
1050 }
1051
Chris Craik62d307c2014-07-29 10:35:13 -07001052 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001053 // standard Region based draw
1054 size_t count;
1055 const android::Rect* rects;
1056 Region safeRegion;
1057 if (CC_LIKELY(hasRectToRectTransform())) {
1058 rects = layer->region.getArray(&count);
1059 } else {
1060 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1061 rects = safeRegion.getArray(&count);
1062 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001063
Chris Craik3f0854292014-04-15 16:18:08 -07001064 const float alpha = getLayerAlpha(layer);
1065 const float texX = 1.0f / float(layer->getWidth());
1066 const float texY = 1.0f / float(layer->getHeight());
1067 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001068
Chris Craik3f0854292014-04-15 16:18:08 -07001069 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001070
Chris Craik3f0854292014-04-15 16:18:08 -07001071 // We must get (and therefore bind) the region mesh buffer
1072 // after we setup drawing in case we need to mess with the
1073 // stencil buffer in setupDraw()
1074 TextureVertex* mesh = mCaches.getRegionMesh();
1075 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001076
Chris Craik3f0854292014-04-15 16:18:08 -07001077 setupDrawWithTexture();
1078 setupDrawColor(alpha, alpha, alpha, alpha);
1079 setupDrawColorFilter(layer->getColorFilter());
1080 setupDrawBlending(layer);
1081 setupDrawProgram();
1082 setupDrawDirtyRegionsDisabled();
1083 setupDrawPureColorUniforms();
1084 setupDrawColorFilterUniforms(layer->getColorFilter());
1085 setupDrawTexture(layer->getTexture());
1086 if (currentTransform()->isPureTranslate()) {
1087 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1088 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001089
Chris Craik3f0854292014-04-15 16:18:08 -07001090 layer->setFilter(GL_NEAREST);
1091 setupDrawModelView(kModelViewMode_Translate, false,
1092 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1093 } else {
1094 layer->setFilter(GL_LINEAR);
1095 setupDrawModelView(kModelViewMode_Translate, false,
1096 rect.left, rect.top, rect.right, rect.bottom);
1097 }
1098 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001099
Chris Craik3f0854292014-04-15 16:18:08 -07001100 for (size_t i = 0; i < count; i++) {
1101 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001102
Chris Craik3f0854292014-04-15 16:18:08 -07001103 const float u1 = r->left * texX;
1104 const float v1 = (height - r->top) * texY;
1105 const float u2 = r->right * texX;
1106 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001107
Chris Craik3f0854292014-04-15 16:18:08 -07001108 // TODO: Reject quads outside of the clip
1109 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1110 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1111 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1112 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001113
Chris Craik3f0854292014-04-15 16:18:08 -07001114 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001115
Chris Craik96a5c4c2015-01-27 15:46:35 -08001116 if (numQuads >= kMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001117 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001118 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001119 numQuads = 0;
1120 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001121 }
Chris Craik3f0854292014-04-15 16:18:08 -07001122 }
1123
1124 if (numQuads > 0) {
1125 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001126 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001127 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001128
Romain Guy5b3b3522010-10-27 18:57:51 -07001129#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001130 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001131#endif
1132
Chris Craik3f0854292014-04-15 16:18:08 -07001133 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001134}
1135
Romain Guy3a3133d2011-02-01 22:59:58 -08001136#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001137void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001138 size_t count;
1139 const android::Rect* rects = region.getArray(&count);
1140
1141 uint32_t colors[] = {
1142 0x7fff0000, 0x7f00ff00,
1143 0x7f0000ff, 0x7fff00ff,
1144 };
1145
1146 int offset = 0;
1147 int32_t top = rects[0].top;
1148
1149 for (size_t i = 0; i < count; i++) {
1150 if (top != rects[i].top) {
1151 offset ^= 0x2;
1152 top = rects[i].top;
1153 }
1154
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001155 SkPaint paint;
1156 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001157 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001158 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001159 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001160}
Chris Craike63f7c622013-10-17 10:30:55 -07001161#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001162
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001163void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001164 Vector<float> rects;
1165
1166 SkRegion::Iterator it(region);
1167 while (!it.done()) {
1168 const SkIRect& r = it.rect();
1169 rects.push(r.fLeft);
1170 rects.push(r.fTop);
1171 rects.push(r.fRight);
1172 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001173 it.next();
1174 }
1175
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001176 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001177}
1178
Romain Guy5b3b3522010-10-27 18:57:51 -07001179void OpenGLRenderer::dirtyLayer(const float left, const float top,
1180 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001181 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001182 Rect bounds(left, top, right, bottom);
1183 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001184 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001185 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001186}
1187
1188void OpenGLRenderer::dirtyLayer(const float left, const float top,
1189 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001190 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001191 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001192 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001193 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001194}
1195
1196void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001197 if (bounds.intersect(mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001198 bounds.snapToPixelBoundaries();
1199 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1200 if (!dirty.isEmpty()) {
1201 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001202 }
1203 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001204}
1205
Chris Craik4063a0e2013-11-15 16:06:56 -08001206void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001207 GLsizei elementsCount = quadsCount * 6;
1208 while (elementsCount > 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001209 GLsizei drawCount = min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Romain Guy448455f2013-07-22 13:57:50 -07001210
Romain Guy3380cfd2013-08-15 16:57:57 -07001211 setupDrawIndexedVertices(&mesh[0].x);
Chris Craike84a2082014-12-22 14:28:49 -08001212 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy448455f2013-07-22 13:57:50 -07001213
1214 elementsCount -= drawCount;
1215 // Though there are 4 vertices in a quad, we use 6 indices per
1216 // quad to draw with GL_TRIANGLES
1217 mesh += (drawCount / 6) * 4;
1218 }
1219}
1220
Romain Guy54be1cd2011-06-13 19:04:27 -07001221void OpenGLRenderer::clearLayerRegions() {
1222 const size_t count = mLayers.size();
1223 if (count == 0) return;
1224
Tom Hudson984162f2014-10-10 13:38:16 -04001225 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001226 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001227 // Doing several glScissor/glClear here can negatively impact
1228 // GPUs with a tiler architecture, instead we draw quads with
1229 // the Clear blending mode
1230
1231 // The list contains bounds that have already been clipped
1232 // against their initial clip rect, and the current clip
1233 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001234 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001235
Romain Guy448455f2013-07-22 13:57:50 -07001236 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001237 Vertex* vertex = mesh;
1238
1239 for (uint32_t i = 0; i < count; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001240 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001241
Chris Craik51d6a3d2014-12-22 17:16:56 -08001242 Vertex::set(vertex++, bounds.left, bounds.top);
1243 Vertex::set(vertex++, bounds.right, bounds.top);
1244 Vertex::set(vertex++, bounds.left, bounds.bottom);
1245 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001246 }
Romain Guye67307c2013-02-11 18:01:20 -08001247 // We must clear the list of dirty rects before we
1248 // call setupDraw() to prevent stencil setup to do
1249 // the same thing again
1250 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001251
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001252 SkPaint clearPaint;
1253 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1254
Romain Guy54be1cd2011-06-13 19:04:27 -07001255 setupDraw(false);
1256 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001257 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001258 setupDrawProgram();
1259 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001260 setupDrawModelView(kModelViewMode_Translate, false,
1261 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001262
Chris Craik4063a0e2013-11-15 16:06:56 -08001263 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001264
Chris Craik65fe5ee2015-01-26 18:06:29 -08001265 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001266 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001267 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001268 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001269}
1270
Romain Guybd6b79b2010-06-26 00:13:53 -07001271///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001272// State Deferral
1273///////////////////////////////////////////////////////////////////////////////
1274
Chris Craikff785832013-03-08 13:12:16 -08001275bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001276 const Rect& currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001277 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001278
Chris Craikff785832013-03-08 13:12:16 -08001279 if (stateDeferFlags & kStateDeferFlag_Draw) {
1280 // state has bounds initialized in local coordinates
1281 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001282 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001283 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001284 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1285 // is used, it should more closely duplicate the quickReject logic (in how it uses
1286 // snapToPixelBoundaries)
1287
Rob Tsuk487a92c2015-01-06 13:22:54 -08001288 if (!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001289 // quick rejected
1290 return true;
1291 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001292
Chris Craika02c4ed2013-06-14 13:43:58 -07001293 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001294 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001295 int& flags = state.mClipSideFlags;
1296 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001297 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1298 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1299 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1300 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001301 }
1302 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001303 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001304 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1305 // overdraw avoidance (since we don't know what it overlaps)
1306 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001307 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001308 }
1309 }
1310
Chris Craik527a3aa2013-03-04 10:19:31 -08001311 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1312 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001313 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001314 }
1315
Chris Craik7273daa2013-03-28 11:25:24 -07001316 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1317 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001318 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001319 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001320 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001321
1322 // always store/restore, since it's just a pointer
1323 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001324 return false;
1325}
1326
Chris Craik527a3aa2013-03-04 10:19:31 -08001327void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001328 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001329 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001330 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001331 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001332
Chris Craik527a3aa2013-03-04 10:19:31 -08001333 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001334 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001335 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001336 dirtyClip();
1337 }
Chris Craikc3566d02013-02-04 16:16:33 -08001338}
1339
Chris Craik28ce94a2013-05-31 11:38:03 -07001340/**
1341 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1342 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1343 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1344 *
1345 * This method should be called when restoreDisplayState() won't be restoring the clip
1346 */
1347void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001348 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001349 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001350 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001351 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001352 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001353 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001354 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1355 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001356}
1357
Chris Craikc3566d02013-02-04 16:16:33 -08001358///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001359// Clipping
1360///////////////////////////////////////////////////////////////////////////////
1361
Romain Guybb9524b2010-06-22 18:56:38 -07001362void OpenGLRenderer::setScissorFromClip() {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001363 Rect clip(mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001364 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001365
Chris Craik65fe5ee2015-01-26 18:06:29 -08001366 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001367 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001368 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001369 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001370}
1371
Romain Guy8ce00302013-01-15 18:51:42 -08001372void OpenGLRenderer::ensureStencilBuffer() {
1373 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1374 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1375 // just hope we have one when hasLayer() returns false.
1376 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001377 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001378 }
1379}
1380
1381void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1382 // The layer's FBO is already bound when we reach this stage
1383 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001384 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1385 // is attached after we initiated tiling. We must turn it off,
1386 // attach the new render buffer then turn tiling back on
1387 endTiling();
1388
Romain Guy8d4aeb72013-02-12 16:08:55 -08001389 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001390 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001391 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001392
Romain Guyf735c8e2013-01-31 17:45:55 -08001393 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001394 }
1395}
1396
Rob Tsuk487a92c2015-01-06 13:22:54 -08001397static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1398 float x, float y) {
1399 Vertex v;
1400 v.x = x;
1401 v.y = y;
1402 transform.mapPoint(v.x, v.y);
1403 rectangleVertices.push_back(v);
1404}
1405
1406static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1407 Vertex v;
1408 v.x = x;
1409 v.y = y;
1410 rectangleVertices.push_back(v);
1411}
1412
1413void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
1414 int count = rectangleList.getTransformedRectanglesCount();
1415 std::vector<Vertex> rectangleVertices(count * 4);
1416 Rect scissorBox = rectangleList.calculateBounds();
1417 scissorBox.snapToPixelBoundaries();
1418 for (int i = 0; i < count; ++i) {
1419 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1420 const Matrix4& transform = tr.getTransform();
1421 Rect bounds = tr.getBounds();
1422 if (transform.rectToRect()) {
1423 transform.mapRect(bounds);
1424 if (!bounds.intersect(scissorBox)) {
1425 bounds.setEmpty();
1426 } else {
1427 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1428 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1429 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1430 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1431 }
1432 } else {
1433 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1434 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1435 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1436 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1437 }
1438 }
1439
Chris Craik65fe5ee2015-01-26 18:06:29 -08001440 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001441 scissorBox.getWidth(), scissorBox.getHeight());
1442
1443 const SkPaint* paint = nullptr;
1444 setupDraw();
1445 setupDrawNoTexture();
1446 setupDrawColor(0, 0xff * currentSnapshot()->alpha);
1447 setupDrawShader(getShader(paint));
1448 setupDrawColorFilter(getColorFilter(paint));
1449 setupDrawBlending(paint);
1450 setupDrawProgram();
1451 setupDrawDirtyRegionsDisabled();
1452 setupDrawModelView(kModelViewMode_Translate, false,
1453 0.0f, 0.0f, 0.0f, 0.0f, true);
1454 setupDrawColorUniforms(getShader(paint));
1455 setupDrawShaderUniforms(getShader(paint));
1456 setupDrawColorFilterUniforms(getColorFilter(paint));
1457
1458 issueIndexedQuadDraw(&rectangleVertices[0], rectangleVertices.size() / 4);
1459}
1460
Romain Guy8ce00302013-01-15 18:51:42 -08001461void OpenGLRenderer::setStencilFromClip() {
1462 if (!mCaches.debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001463 if (!currentSnapshot()->clipIsSimple()) {
1464 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001465 EVENT_LOGD("setStencilFromClip - enabling");
1466
Romain Guy8ce00302013-01-15 18:51:42 -08001467 // NOTE: The order here is important, we must set dirtyClip to false
1468 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001469 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001470
1471 ensureStencilBuffer();
1472
Rob Tsuk487a92c2015-01-06 13:22:54 -08001473 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001474
Rob Tsuk487a92c2015-01-06 13:22:54 -08001475 bool isRectangleList = clipArea.isRectangleList();
1476 if (isRectangleList) {
1477 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1478 } else {
1479 incrementThreshold = 0;
1480 }
1481
Chris Craik96a5c4c2015-01-27 15:46:35 -08001482 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001483
1484 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001485 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001486 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001487 if (resetScissor) {
1488 // The scissor was not set so we now need to update it
1489 setScissorFromClip();
1490 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001491
Chris Craik96a5c4c2015-01-27 15:46:35 -08001492 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001493
1494 // stash and disable the outline clip state, since stencil doesn't account for outline
1495 bool storedSkipOutlineClip = mSkipOutlineClip;
1496 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001497
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001498 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001499 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001500 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1501
Rob Tsuk487a92c2015-01-06 13:22:54 -08001502 if (isRectangleList) {
1503 drawRectangleList(clipArea.getRectangleList());
1504 } else {
1505 // NOTE: We could use the region contour path to generate a smaller mesh
1506 // Since we are using the stencil we could use the red book path
1507 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001508
Rob Tsuk487a92c2015-01-06 13:22:54 -08001509 // The last parameter is important: we are not drawing in the color buffer
1510 // so we don't want to dirty the current layer, if any
1511 drawRegionRects(clipArea.getClipRegion(), paint, false);
1512 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001513 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001514 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001515
Chris Craik96a5c4c2015-01-27 15:46:35 -08001516 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001517
1518 // Draw the region used to generate the stencil if the appropriate debug
1519 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001520 // TODO: Implement for rectangle list clip areas
1521 if (mCaches.debugStencilClip == Caches::kStencilShowRegion &&
1522 !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001523 paint.setColor(0x7f0000ff);
1524 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001525 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001526 }
Romain Guy8ce00302013-01-15 18:51:42 -08001527 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001528 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001529 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001530 }
1531 }
1532}
1533
Chris Craikf0a59072013-11-19 18:00:46 -08001534/**
1535 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1536 *
1537 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1538 * style, and tessellated AA ramp
1539 */
1540bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001541 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001542 bool snapOut = paint && paint->isAntiAlias();
1543
1544 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1545 float outset = paint->getStrokeWidth() * 0.5f;
1546 left -= outset;
1547 top -= outset;
1548 right += outset;
1549 bottom += outset;
1550 }
1551
Chris Craikdeeda3d2014-05-05 19:09:33 -07001552 bool clipRequired = false;
1553 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001554 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001555 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001556 return true;
1557 }
1558
Chris Craikf23b25a2014-06-26 15:46:20 -07001559 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001560 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001561 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001562 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001563}
1564
Romain Guy8ce00302013-01-15 18:51:42 -08001565void OpenGLRenderer::debugClip() {
1566#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001567 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001568 SkPaint paint;
1569 paint.setColor(0x7f00ff00);
1570 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1571
Romain Guy8ce00302013-01-15 18:51:42 -08001572 }
1573#endif
1574}
1575
Romain Guyf6a11b82010-06-23 17:47:49 -07001576///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001577// Drawing commands
1578///////////////////////////////////////////////////////////////////////////////
1579
Chris Craik62d307c2014-07-29 10:35:13 -07001580void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001581 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001582 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001583 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001584 // Make sure setScissor & setStencil happen at the beginning of
1585 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001586 if (mState.getDirtyClip()) {
Chris Craik65fe5ee2015-01-26 18:06:29 -08001587 if (mRenderState.scissor().isEnabled()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001588 setScissorFromClip();
1589 }
Chris Craik62d307c2014-07-29 10:35:13 -07001590
Dohyun Leeadc0d9d2014-11-24 21:08:15 +09001591 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001592 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001593
Romain Guy70ca14e2010-12-13 18:24:33 -08001594 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001595
Romain Guy70ca14e2010-12-13 18:24:33 -08001596 mSetShaderColor = false;
1597 mColorSet = false;
1598 mColorA = mColorR = mColorG = mColorB = 0.0f;
1599 mTextureUnit = 0;
1600 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001601
1602 // Enable debug highlight when what we're about to draw is tested against
1603 // the stencil buffer and if stencil highlight debugging is on
1604 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1605 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
Chris Craik96a5c4c2015-01-27 15:46:35 -08001606 mRenderState.stencil().isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001607}
1608
1609void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1610 mDescription.hasTexture = true;
1611 mDescription.hasAlpha8Texture = isAlpha8;
1612}
1613
Romain Guyff316ec2013-02-13 18:39:43 -08001614void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1615 mDescription.hasTexture = true;
1616 mDescription.hasColors = true;
1617 mDescription.hasAlpha8Texture = isAlpha8;
1618}
1619
Romain Guyaa6c24c2011-04-28 18:40:04 -07001620void OpenGLRenderer::setupDrawWithExternalTexture() {
1621 mDescription.hasExternalTexture = true;
1622}
1623
Romain Guy15bc6432011-12-13 13:11:32 -08001624void OpenGLRenderer::setupDrawNoTexture() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001625 mRenderState.meshState().disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001626}
1627
Chris Craik91a8c7c2014-08-12 14:31:35 -07001628void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1629 mDescription.hasVertexAlpha = true;
1630 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001631}
1632
Romain Guy8d0d4782010-12-14 20:13:35 -08001633void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1634 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001635 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1636 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1637 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001638 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001639 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001640}
1641
Romain Guy86568192010-12-14 15:55:39 -08001642void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1643 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001644 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1645 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1646 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001647 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001648 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001649}
1650
Romain Guy41210632012-07-16 17:04:24 -07001651void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1652 mCaches.fontRenderer->describe(mDescription, paint);
1653}
1654
Romain Guy70ca14e2010-12-13 18:24:33 -08001655void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1656 mColorA = a;
1657 mColorR = r;
1658 mColorG = g;
1659 mColorB = b;
1660 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001661 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001662}
1663
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001664void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
Chris Craike84a2082014-12-22 14:28:49 -08001665 if (shader != nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001666 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001667 }
1668}
1669
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001670void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001671 if (filter == nullptr) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001672 return;
1673 }
1674
1675 SkXfermode::Mode mode;
Chris Craike84a2082014-12-22 14:28:49 -08001676 if (filter->asColorMode(nullptr, &mode)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001677 mDescription.colorOp = ProgramDescription::kColorBlend;
1678 mDescription.colorMode = mode;
Chris Craike84a2082014-12-22 14:28:49 -08001679 } else if (filter->asColorMatrix(nullptr)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001680 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001681 }
1682}
1683
Romain Guyf09ef512011-05-27 11:43:46 -07001684void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1685 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1686 mColorA = 1.0f;
1687 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001688 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001689 }
1690}
1691
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001692void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1693 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001694 // When the blending mode is kClear_Mode, we need to use a modulate color
1695 // argb=1,0,0,0
1696 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001697 // TODO: check shader blending, once we have shader drawing support for layers.
Tom Hudson8dfaa492014-12-09 15:03:44 -05001698 bool blend = layer->isBlend()
1699 || getLayerAlpha(layer) < 1.0f
1700 || (mColorSet && mColorA < 1.0f)
1701 || PaintUtils::isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001702 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001703}
1704
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001705void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1706 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001707 // When the blending mode is kClear_Mode, we need to use a modulate color
1708 // argb=1,0,0,0
1709 accountForClear(mode);
Chris Craik03188872015-02-02 18:39:33 -08001710 blend |= (mColorSet && mColorA < 1.0f)
1711 || (getShader(paint) && !getShader(paint)->isOpaque())
1712 || PaintUtils::isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001713 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001714}
1715
1716void OpenGLRenderer::setupDrawProgram() {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001717 mCaches.setProgram(mDescription);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001718 if (mDescription.hasRoundRectClip) {
1719 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001720 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001721 const Rect& innerRect = state->innerRect;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001722 glUniform4f(mCaches.program().getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001723 innerRect.left, innerRect.top,
1724 innerRect.right, innerRect.bottom);
Chris Craik6c15ffa2015-02-02 13:50:55 -08001725 glUniformMatrix4fv(mCaches.program().getUniform("roundRectInvTransform"),
Chris Craikdeeda3d2014-05-05 19:09:33 -07001726 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001727
1728 // add half pixel to round out integer rect space to cover pixel centers
1729 float roundedOutRadius = state->radius + 0.5f;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001730 glUniform1f(mCaches.program().getUniform("roundRectRadius"),
Chris Craik4340c262014-09-11 18:58:45 -07001731 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001732 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001733}
1734
1735void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1736 mTrackDirtyRegions = false;
1737}
1738
Chris Craik4063a0e2013-11-15 16:06:56 -08001739void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1740 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001741 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001742 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001743 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001744 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001745
Romain Guy86568192010-12-14 15:55:39 -08001746 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001747 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001748
1749 mCaches.program().set(currentSnapshot()->getOrthoMatrix(),
Chris Craike84a2082014-12-22 14:28:49 -08001750 mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001751 if (dirty && mTrackDirtyRegions) {
1752 if (!ignoreTransform) {
1753 dirtyLayer(left, top, right, bottom, *currentTransform());
1754 } else {
1755 dirtyLayer(left, top, right, bottom);
1756 }
Romain Guy86568192010-12-14 15:55:39 -08001757 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001758}
1759
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001760void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1761 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001762 mCaches.program().setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001763 }
1764}
1765
Romain Guy86568192010-12-14 15:55:39 -08001766void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001767 if (mSetShaderColor) {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001768 mCaches.program().setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001769 }
1770}
1771
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001772void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
Chris Craike84a2082014-12-22 14:28:49 -08001773 if (shader == nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001774 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001775 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001776
1777 if (ignoreTransform) {
1778 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1779 // because it was built into modelView / the geometry, and the description needs to
1780 // compensate.
1781 mat4 modelViewWithoutTransform;
1782 modelViewWithoutTransform.loadInverse(*currentTransform());
1783 modelViewWithoutTransform.multiply(mModelViewMatrix);
1784 mModelViewMatrix.load(modelViewWithoutTransform);
1785 }
1786
1787 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001788}
1789
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001790void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001791 if (nullptr == filter) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001792 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001793 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001794
1795 SkColor color;
1796 SkXfermode::Mode mode;
1797 if (filter->asColorMode(&color, &mode)) {
1798 const int alpha = SkColorGetA(color);
1799 const GLfloat a = alpha / 255.0f;
1800 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1801 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1802 const GLfloat b = a * SkColorGetB(color) / 255.0f;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001803 glUniform4f(mCaches.program().getUniform("colorBlend"), r, g, b, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001804 return;
1805 }
1806
1807 SkScalar srcColorMatrix[20];
1808 if (filter->asColorMatrix(srcColorMatrix)) {
1809
1810 float colorMatrix[16];
1811 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1812 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1813 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1814 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1815
1816 // Skia uses the range [0..255] for the addition vector, but we need
1817 // the [0..1] range to apply the vector in GLSL
1818 float colorVector[4];
1819 colorVector[0] = srcColorMatrix[4] / 255.0f;
1820 colorVector[1] = srcColorMatrix[9] / 255.0f;
1821 colorVector[2] = srcColorMatrix[14] / 255.0f;
1822 colorVector[3] = srcColorMatrix[19] / 255.0f;
1823
Chris Craik6c15ffa2015-02-02 13:50:55 -08001824 glUniformMatrix4fv(mCaches.program().getUniform("colorMatrix"), 1,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001825 GL_FALSE, colorMatrix);
Chris Craik6c15ffa2015-02-02 13:50:55 -08001826 glUniform4fv(mCaches.program().getUniform("colorMatrixVector"), 1, colorVector);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001827 return;
1828 }
1829
1830 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001831}
1832
Romain Guy41210632012-07-16 17:04:24 -07001833void OpenGLRenderer::setupDrawTextGammaUniforms() {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001834 mCaches.fontRenderer->setupProgram(mDescription, mCaches.program());
Romain Guy41210632012-07-16 17:04:24 -07001835}
1836
Romain Guy70ca14e2010-12-13 18:24:33 -08001837void OpenGLRenderer::setupDrawSimpleMesh() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001838 bool force = mRenderState.meshState().bindMeshBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001839 mRenderState.meshState().bindPositionVertexPointer(force, nullptr);
Chris Craik96a5c4c2015-01-27 15:46:35 -08001840 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001841}
1842
1843void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001844 if (texture) mCaches.textureState().bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001845 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001846 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001847}
1848
Romain Guyaa6c24c2011-04-28 18:40:04 -07001849void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001850 mCaches.textureState().bindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001851 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001852 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001853}
1854
Romain Guy8f0095c2011-05-02 17:24:22 -07001855void OpenGLRenderer::setupDrawTextureTransform() {
1856 mDescription.hasTextureTransform = true;
1857}
1858
1859void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001860 glUniformMatrix4fv(mCaches.program().getUniform("mainTextureTransform"), 1,
Romain Guyaa6c24c2011-04-28 18:40:04 -07001861 GL_FALSE, &transform.data[0]);
1862}
1863
Chris Craik564acf72014-01-02 16:46:18 -08001864void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1865 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001866 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001867 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001868 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001869 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001870 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001871 }
Romain Guyd71dd362011-12-12 19:03:35 -08001872
Chris Craik6c15ffa2015-02-02 13:50:55 -08001873 mRenderState.meshState().bindPositionVertexPointer(force, vertices);
1874 if (mCaches.program().texCoords >= 0) {
1875 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001876 }
1877
Chris Craik96a5c4c2015-01-27 15:46:35 -08001878 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -08001879}
1880
Chris Craik564acf72014-01-02 16:46:18 -08001881void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1882 const GLvoid* texCoords, const GLvoid* colors) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001883 bool force = mRenderState.meshState().unbindMeshBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001884 GLsizei stride = sizeof(ColorTextureVertex);
1885
Chris Craik6c15ffa2015-02-02 13:50:55 -08001886 mRenderState.meshState().bindPositionVertexPointer(force, vertices, stride);
1887 if (mCaches.program().texCoords >= 0) {
1888 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords, stride);
Romain Guyff316ec2013-02-13 18:39:43 -08001889 }
Chris Craik6c15ffa2015-02-02 13:50:55 -08001890 int slot = mCaches.program().getAttrib("colors");
Romain Guyff316ec2013-02-13 18:39:43 -08001891 if (slot >= 0) {
1892 glEnableVertexAttribArray(slot);
1893 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1894 }
1895
Chris Craik96a5c4c2015-01-27 15:46:35 -08001896 mRenderState.meshState().unbindIndicesBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001897}
1898
Chris Craik564acf72014-01-02 16:46:18 -08001899void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1900 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001901 bool force = false;
1902 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1903 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
Chris Craik96a5c4c2015-01-27 15:46:35 -08001904 // use the default VBO found in RenderState
Romain Guy3b748a42013-04-17 18:54:38 -07001905 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001906 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy3b748a42013-04-17 18:54:38 -07001907 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001908 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001909 }
Chris Craik96a5c4c2015-01-27 15:46:35 -08001910 mRenderState.meshState().bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001911
Chris Craik6c15ffa2015-02-02 13:50:55 -08001912 mRenderState.meshState().bindPositionVertexPointer(force, vertices);
1913 if (mCaches.program().texCoords >= 0) {
1914 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001915 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001916}
1917
Romain Guy448455f2013-07-22 13:57:50 -07001918void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001919 bool force = mRenderState.meshState().unbindMeshBuffer();
1920 mRenderState.meshState().bindQuadIndicesBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001921 mRenderState.meshState().bindPositionVertexPointer(force, vertices, kVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001922}
1923
Romain Guy70ca14e2010-12-13 18:24:33 -08001924///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001925// Drawing
1926///////////////////////////////////////////////////////////////////////////////
1927
Tom Hudson107843d2014-09-08 11:26:26 -04001928void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001929 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1930 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001931 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001932 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001933 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001934 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001935 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001936 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001937 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001938 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001939 }
1940
Chris Craikef8d6f22014-12-17 11:10:28 -08001941 // Don't avoid overdraw when visualizing, since that makes it harder to
1942 // debug where it's coming from, and when the problem occurs.
1943 bool avoidOverdraw = !mCaches.debugOverdraw;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001944 DeferredDisplayList deferredList(mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001945 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001946 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001947
1948 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001949 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001950
Tom Hudson107843d2014-09-08 11:26:26 -04001951 deferredList.flush(*this, dirty);
1952 } else {
1953 // Even if there is no drawing command(Ex: invisible),
1954 // it still needs startFrame to clear buffer and start tiling.
1955 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001956 }
1957}
1958
Chris Craike84a2082014-12-22 14:28:49 -08001959void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top,
1960 const SkPaint* paint) {
Romain Guya168d732011-03-18 16:50:13 -07001961 float x = left;
1962 float y = top;
1963
Romain Guy886b2752013-01-04 12:26:18 -08001964 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1965
Romain Guya168d732011-03-18 16:50:13 -07001966 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001967 if (currentTransform()->isPureTranslate()) {
1968 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1969 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001970 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001971
1972 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001973 } else {
Chris Craik678625242014-02-28 12:26:34 -08001974 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001975 }
1976
Romain Guy3b748a42013-04-17 18:54:38 -07001977 // No need to check for a UV mapper on the texture object, only ARGB_8888
1978 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001979 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Chris Craik96a5c4c2015-01-27 15:46:35 -08001980 paint, (GLvoid*) nullptr, (GLvoid*) kMeshTextureOffset,
1981 GL_TRIANGLE_STRIP, kMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001982}
1983
Romain Guy03c00b52013-06-20 18:30:28 -07001984/**
1985 * Important note: this method is intended to draw batches of bitmaps and
1986 * will not set the scissor enable or dirty the current layer, if any.
1987 * The caller is responsible for properly dirtying the current layer.
1988 */
Tom Hudson107843d2014-09-08 11:26:26 -04001989void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001990 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1991 const Rect& bounds, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001992 mCaches.textureState().activateTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001993 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001994 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001995
Chris Craik527a3aa2013-03-04 10:19:31 -08001996 const AutoTexture autoCleanup(texture);
1997
Chris Craik527a3aa2013-03-04 10:19:31 -08001998 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08001999 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002000
2001 const float x = (int) floorf(bounds.left + 0.5f);
2002 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04002003 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002004 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002005 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002006 GL_TRIANGLES, bitmapCount * 6, true,
2007 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002008 } else {
2009 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002010 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002011 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2012 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002013 }
2014
Tom Hudson107843d2014-09-08 11:26:26 -04002015 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08002016}
2017
Tom Hudson107843d2014-09-08 11:26:26 -04002018void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002019 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002020 return;
Romain Guy6926c722010-07-12 20:20:03 -07002021 }
2022
Chris Craik44eb2c02015-01-29 09:45:09 -08002023 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002024 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002025 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002026 const AutoTexture autoCleanup(texture);
2027
Mike Reed1103b322014-07-08 12:36:44 -04002028 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002029 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002030 } else {
Chris Craik79647502014-08-06 13:42:24 -07002031 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002032 }
Chet Haase48659092012-05-31 15:21:51 -07002033
Tom Hudson107843d2014-09-08 11:26:26 -04002034 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002035}
2036
Tom Hudson107843d2014-09-08 11:26:26 -04002037void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002038 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002039 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002040 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002041 }
2042
Chris Craik39a908c2013-06-13 14:39:01 -07002043 // TODO: use quickReject on bounds from vertices
Chris Craik65fe5ee2015-01-26 18:06:29 -08002044 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002045
Romain Guyb18d2d02011-02-10 15:52:54 -08002046 float left = FLT_MAX;
2047 float top = FLT_MAX;
2048 float right = FLT_MIN;
2049 float bottom = FLT_MIN;
2050
Romain Guya92bb4d2012-10-16 11:08:44 -07002051 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002052
Chris Craik51d6a3d2014-12-22 17:16:56 -08002053 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[count]);
2054 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08002055
Chris Craik51d6a3d2014-12-22 17:16:56 -08002056 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002057 if (!colors) {
2058 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08002059 tempColors.reset(new int[colorsCount]);
2060 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
2061 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08002062 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002063
Chris Craik44eb2c02015-01-29 09:45:09 -08002064 mCaches.textureState().activateTexture(0);
John Reckebd52612014-12-10 16:47:36 -08002065 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002066 const UvMapper& mapper(getMapper(texture));
2067
Romain Guy5a7b4662011-01-20 19:09:30 -08002068 for (int32_t y = 0; y < meshHeight; y++) {
2069 for (int32_t x = 0; x < meshWidth; x++) {
2070 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2071
2072 float u1 = float(x) / meshWidth;
2073 float u2 = float(x + 1) / meshWidth;
2074 float v1 = float(y) / meshHeight;
2075 float v2 = float(y + 1) / meshHeight;
2076
Romain Guy3b748a42013-04-17 18:54:38 -07002077 mapper.map(u1, v1, u2, v2);
2078
Romain Guy5a7b4662011-01-20 19:09:30 -08002079 int ax = i + (meshWidth + 1) * 2;
2080 int ay = ax + 1;
2081 int bx = i;
2082 int by = bx + 1;
2083 int cx = i + 2;
2084 int cy = cx + 1;
2085 int dx = i + (meshWidth + 1) * 2 + 2;
2086 int dy = dx + 1;
2087
Romain Guyff316ec2013-02-13 18:39:43 -08002088 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2089 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2090 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002091
Romain Guyff316ec2013-02-13 18:39:43 -08002092 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2093 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2094 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002095
Romain Guya92bb4d2012-10-16 11:08:44 -07002096 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2097 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2098 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2099 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002100 }
2101 }
2102
Chris Craikf0a59072013-11-19 18:00:46 -08002103 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002104 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002105 }
2106
Romain Guyff316ec2013-02-13 18:39:43 -08002107 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002108 texture = mCaches.textureCache.get(bitmap);
2109 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04002110 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002111 }
Romain Guyff316ec2013-02-13 18:39:43 -08002112 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002113 const AutoTexture autoCleanup(texture);
2114
2115 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002116 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002117
2118 int alpha;
2119 SkXfermode::Mode mode;
2120 getAlphaAndMode(paint, &alpha, &mode);
2121
Romain Guyff316ec2013-02-13 18:39:43 -08002122 float a = alpha / 255.0f;
2123
Romain Guya92bb4d2012-10-16 11:08:44 -07002124 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002125 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002126 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002127
Romain Guyff316ec2013-02-13 18:39:43 -08002128 setupDraw();
2129 setupDrawWithTextureAndColor();
2130 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002131 setupDrawColorFilter(getColorFilter(paint));
2132 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002133 setupDrawProgram();
2134 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002135 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002136 setupDrawTexture(texture->id);
2137 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002138 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002139 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002140
2141 glDrawArrays(GL_TRIANGLES, 0, count);
2142
Chris Craik6c15ffa2015-02-02 13:50:55 -08002143 int slot = mCaches.program().getAttrib("colors");
Romain Guyff316ec2013-02-13 18:39:43 -08002144 if (slot >= 0) {
2145 glDisableVertexAttribArray(slot);
2146 }
2147
Tom Hudson107843d2014-09-08 11:26:26 -04002148 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002149}
2150
Tom Hudson107843d2014-09-08 11:26:26 -04002151void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002152 float srcLeft, float srcTop, float srcRight, float srcBottom,
2153 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002154 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002155 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002156 return;
Romain Guy6926c722010-07-12 20:20:03 -07002157 }
2158
Chris Craik44eb2c02015-01-29 09:45:09 -08002159 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002160 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002161 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002162 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002163
Romain Guy8ba548f2010-06-30 19:21:21 -07002164 const float width = texture->width;
2165 const float height = texture->height;
2166
Romain Guy3b748a42013-04-17 18:54:38 -07002167 float u1 = fmax(0.0f, srcLeft / width);
2168 float v1 = fmax(0.0f, srcTop / height);
2169 float u2 = fmin(1.0f, srcRight / width);
2170 float v2 = fmin(1.0f, srcBottom / height);
2171
2172 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002173
Chris Craik96a5c4c2015-01-27 15:46:35 -08002174 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002175 resetDrawTextureTexCoords(u1, v1, u2, v2);
2176
Romain Guyd21b6e12011-11-30 20:21:23 -08002177 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2178
Romain Guy886b2752013-01-04 12:26:18 -08002179 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2180 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002181
Romain Guy886b2752013-01-04 12:26:18 -08002182 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2183 // Apply a scale transform on the canvas only when a shader is in use
2184 // Skia handles the ratio between the dst and src rects as a scale factor
2185 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002186 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002187 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002188
Chris Craikd6b65f62014-01-01 14:45:21 -08002189 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2190 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2191 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002192
2193 dstRight = x + (dstRight - dstLeft);
2194 dstBottom = y + (dstBottom - dstTop);
2195
2196 dstLeft = x;
2197 dstTop = y;
2198
Chris Craik678625242014-02-28 12:26:34 -08002199 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002200 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002201 } else {
Chris Craik678625242014-02-28 12:26:34 -08002202 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002203 }
2204
2205 if (CC_UNLIKELY(useScaleTransform)) {
2206 save(SkCanvas::kMatrix_SaveFlag);
2207 translate(dstLeft, dstTop);
2208 scale(scaleX, scaleY);
2209
2210 dstLeft = 0.0f;
2211 dstTop = 0.0f;
2212
2213 dstRight = srcRight - srcLeft;
2214 dstBottom = srcBottom - srcTop;
2215 }
2216
Mike Reed1103b322014-07-08 12:36:44 -04002217 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002218 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002219 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002220 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik96a5c4c2015-01-27 15:46:35 -08002221 GL_TRIANGLE_STRIP, kMeshCount, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002222 } else {
2223 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002224 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002225 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik96a5c4c2015-01-27 15:46:35 -08002226 GL_TRIANGLE_STRIP, kMeshCount, false, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002227 }
2228
2229 if (CC_UNLIKELY(useScaleTransform)) {
2230 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002231 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002232
2233 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002234
Tom Hudson107843d2014-09-08 11:26:26 -04002235 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002236}
2237
Tom Hudson107843d2014-09-08 11:26:26 -04002238void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002239 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002240 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002241 return;
Romain Guy6926c722010-07-12 20:20:03 -07002242 }
2243
John Reckebd52612014-12-10 16:47:36 -08002244 AssetAtlas::Entry* entry = mRenderState.assetAtlas().getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002245 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2246 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002247
Tom Hudson107843d2014-09-08 11:26:26 -04002248 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002249}
2250
Tom Hudson107843d2014-09-08 11:26:26 -04002251void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002252 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2253 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002254 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002255 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002256 }
2257
Romain Guy211370f2012-02-01 16:10:55 -08002258 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002259 mCaches.textureState().activateTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002260 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002261 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002262 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002263
Romain Guya4adcf02013-02-28 12:15:35 -08002264 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2265 texture->setFilter(GL_LINEAR, true);
2266
Chris Craikd6b65f62014-01-01 14:45:21 -08002267 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002268 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002269 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002270 const float offsetX = left + currentTransform()->getTranslateX();
2271 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002272 const size_t count = mesh->quads.size();
2273 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002274 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002275 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002276 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2277 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2278 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002279 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002280 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002281 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002282 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002283 }
2284 }
2285
Chris Craik4063a0e2013-11-15 16:06:56 -08002286 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002287 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002288 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2289 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002290
Romain Guy3b748a42013-04-17 18:54:38 -07002291 right = x + right - left;
2292 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002293 left = x;
2294 top = y;
2295 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002296 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002297 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2298 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002299 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2300 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002301 }
Chet Haase48659092012-05-31 15:21:51 -07002302
Tom Hudson107843d2014-09-08 11:26:26 -04002303 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002304}
2305
Romain Guy03c00b52013-06-20 18:30:28 -07002306/**
2307 * Important note: this method is intended to draw batches of 9-patch objects and
2308 * will not set the scissor enable or dirty the current layer, if any.
2309 * The caller is responsible for properly dirtying the current layer.
2310 */
Tom Hudson107843d2014-09-08 11:26:26 -04002311void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002312 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002313 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07002314 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002315 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002316 const AutoTexture autoCleanup(texture);
2317
2318 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2319 texture->setFilter(GL_LINEAR, true);
2320
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002321 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2322 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002323 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002324
Tom Hudson107843d2014-09-08 11:26:26 -04002325 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002326}
2327
Tom Hudson107843d2014-09-08 11:26:26 -04002328void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002329 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002330 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002331 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002332 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002333 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002334 }
2335
Chris Craik0d5ac952014-07-15 13:01:02 -07002336 Rect bounds(vertexBuffer.getBounds());
2337 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002338 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2339
Chris Craikcb4d6002012-09-25 12:00:29 -07002340 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002341 bool isAA = paint->isAntiAlias();
2342
Chris Craik710f46d2012-09-17 17:25:49 -07002343 setupDraw();
2344 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002345 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Tom Hudson984162f2014-10-10 13:38:16 -04002346 setupDrawColor(color, ((color >> 24) & 0xFF) * writableSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002347 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002348 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002349 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002350 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002351 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2352 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002353 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002354 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002355 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002356
ztenghui55bfb4e2013-12-03 10:38:55 -08002357 const void* vertices = vertexBuffer.getBuffer();
Chris Craik96a5c4c2015-01-27 15:46:35 -08002358 mRenderState.meshState().unbindMeshBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08002359 mRenderState.meshState().bindPositionVertexPointer(true, vertices,
2360 isAA ? kAlphaVertexStride : kVertexStride);
Chris Craik96a5c4c2015-01-27 15:46:35 -08002361 mRenderState.meshState().resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002362
Chris Craik710f46d2012-09-17 17:25:49 -07002363 int alphaSlot = -1;
2364 if (isAA) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002365 void* alphaCoords = ((GLbyte*) vertices) + kVertexAlphaOffset;
Chris Craik6c15ffa2015-02-02 13:50:55 -08002366 alphaSlot = mCaches.program().getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002367 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002368 glEnableVertexAttribArray(alphaSlot);
Chris Craik96a5c4c2015-01-27 15:46:35 -08002369 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, kAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002370 }
Romain Guy04299382012-07-18 17:15:41 -07002371
Chris Craik05f3d6e2014-06-02 16:27:04 -07002372 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2373 if (mode == VertexBuffer::kStandard) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002374 mRenderState.meshState().unbindIndicesBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08002375 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002376 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002377 mRenderState.meshState().bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002378 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT,
2379 GL_UNSIGNED_SHORT, nullptr);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002380 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002381 mRenderState.meshState().bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002382 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT,
2383 GL_UNSIGNED_SHORT, nullptr);
ztenghuid5e8ade2014-08-13 15:48:02 -07002384 } else if (mode == VertexBuffer::kIndices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002385 mRenderState.meshState().unbindIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002386 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(),
2387 GL_UNSIGNED_SHORT, vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002388 }
Romain Guy04299382012-07-18 17:15:41 -07002389
Chris Craik710f46d2012-09-17 17:25:49 -07002390 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002391 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002392 }
Chris Craik65cd6122012-12-10 17:56:27 -08002393
Tom Hudson107843d2014-09-08 11:26:26 -04002394 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002395}
2396
2397/**
Chris Craik65cd6122012-12-10 17:56:27 -08002398 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2399 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2400 * screen space in all directions. However, instead of using a fragment shader to compute the
2401 * translucency of the color from its position, we simply use a varying parameter to define how far
2402 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2403 *
2404 * Doesn't yet support joins, caps, or path effects.
2405 */
Tom Hudson107843d2014-09-08 11:26:26 -04002406void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002407 VertexBuffer vertexBuffer;
2408 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002409 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002410 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002411}
2412
2413/**
2414 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2415 * and additional geometry for defining an alpha slope perimeter.
2416 *
2417 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2418 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2419 * in-shader alpha region, but found it to be taxing on some GPUs.
2420 *
2421 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2422 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002423 */
Tom Hudson107843d2014-09-08 11:26:26 -04002424void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002425 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002426
Chris Craik65cd6122012-12-10 17:56:27 -08002427 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002428
Chris Craik65cd6122012-12-10 17:56:27 -08002429 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002430 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2431 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002432
Chris Craik05f3d6e2014-06-02 16:27:04 -07002433 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002434 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002435 }
2436
Chris Craikbf759452014-08-11 16:00:44 -07002437 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002438 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002439}
2440
Tom Hudson107843d2014-09-08 11:26:26 -04002441void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002442 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002443
Chris Craik6d29c8d2013-05-08 18:35:44 -07002444 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002445
Chris Craik6d29c8d2013-05-08 18:35:44 -07002446 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002447 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002448
Chris Craik05f3d6e2014-06-02 16:27:04 -07002449 const Rect& bounds = buffer.getBounds();
2450 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002451 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002452 }
2453
Chris Craikbf759452014-08-11 16:00:44 -07002454 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002455 drawVertexBuffer(buffer, paint, displayFlags);
2456
2457 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002458}
2459
Tom Hudson107843d2014-09-08 11:26:26 -04002460void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002461 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002462 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002463
Rob Tsuk487a92c2015-01-06 13:22:54 -08002464 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002465 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002466
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002467 SkPaint paint;
2468 paint.setColor(color);
2469 paint.setXfermodeMode(mode);
2470
2471 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002472
Tom Hudson107843d2014-09-08 11:26:26 -04002473 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002474}
Romain Guy9d5316e2010-06-24 19:30:36 -07002475
Tom Hudson107843d2014-09-08 11:26:26 -04002476void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002477 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002478 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002479 const AutoTexture autoCleanup(texture);
2480
2481 const float x = left + texture->left - texture->offset;
2482 const float y = top + texture->top - texture->offset;
2483
2484 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002485
Tom Hudson107843d2014-09-08 11:26:26 -04002486 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002487}
2488
Tom Hudson107843d2014-09-08 11:26:26 -04002489void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002490 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002491 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002492 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002493 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002494 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002495 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002496
Chris Craike84a2082014-12-22 14:28:49 -08002497 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002498 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002499 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002500 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002501 drawShape(left, top, texture, p);
2502 } else {
2503 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2504 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2505 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002506 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002507}
2508
Tom Hudson107843d2014-09-08 11:26:26 -04002509void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002510 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002511 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002512 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002513 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002514 }
Chris Craike84a2082014-12-22 14:28:49 -08002515 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002516 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002517 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002518 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002519 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002520 SkPath path;
2521 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2522 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2523 } else {
2524 path.addCircle(x, y, radius);
2525 }
2526 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002527 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002528}
Romain Guy01d58e42011-01-19 21:54:02 -08002529
Tom Hudson107843d2014-09-08 11:26:26 -04002530void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002531 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002532 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002533 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002534 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002535 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002536 }
Romain Guy01d58e42011-01-19 21:54:02 -08002537
Chris Craike84a2082014-12-22 14:28:49 -08002538 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002539 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002540 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002541 drawShape(left, top, texture, p);
2542 } else {
2543 SkPath path;
2544 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2545 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2546 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2547 }
2548 path.addOval(rect);
2549 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002550 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002551}
2552
Tom Hudson107843d2014-09-08 11:26:26 -04002553void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002554 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002555 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002556 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002557 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002558 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002559 }
2560
Chris Craik780c1282012-10-04 14:10:49 -07002561 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08002562 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002563 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002564 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002565 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002566 drawShape(left, top, texture, p);
2567 return;
Chris Craik780c1282012-10-04 14:10:49 -07002568 }
Chris Craik780c1282012-10-04 14:10:49 -07002569 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2570 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2571 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2572 }
2573
2574 SkPath path;
2575 if (useCenter) {
2576 path.moveTo(rect.centerX(), rect.centerY());
2577 }
2578 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2579 if (useCenter) {
2580 path.close();
2581 }
Tom Hudson107843d2014-09-08 11:26:26 -04002582 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002583}
2584
Romain Guycf8675e2012-10-02 12:32:25 -07002585// See SkPaintDefaults.h
2586#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2587
Tom Hudson107843d2014-09-08 11:26:26 -04002588void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002589 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002590 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002591 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002592 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002593 return;
Romain Guy6926c722010-07-12 20:20:03 -07002594 }
2595
Chris Craik710f46d2012-09-17 17:25:49 -07002596 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002597 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08002598 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07002599 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002600 mCaches.textureState().activateTexture(0);
Romain Guycf8675e2012-10-02 12:32:25 -07002601 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002602 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002603 drawShape(left, top, texture, p);
2604 } else {
2605 SkPath path;
2606 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2607 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2608 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2609 }
2610 path.addRect(rect);
2611 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002612 }
Chet Haase858aa932011-05-12 09:06:00 -07002613 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002614 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2615 SkPath path;
2616 path.addRect(left, top, right, bottom);
2617 drawConvexPath(path, p);
2618 } else {
2619 drawColorRect(left, top, right, bottom, p);
2620
2621 mDirty = true;
2622 }
Chet Haase858aa932011-05-12 09:06:00 -07002623 }
Romain Guyc7d53492010-06-25 13:41:57 -07002624}
Romain Guy9d5316e2010-06-24 19:30:36 -07002625
Chris Craikd218a922014-01-02 17:13:34 -08002626void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2627 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002628 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002629 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07002630
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002631 TextShadow textShadow;
2632 if (!getTextShadow(paint, &textShadow)) {
2633 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2634 }
2635
Raph Levien416a8472012-07-19 22:48:17 -07002636 // NOTE: The drop shadow will not perform gamma correction
2637 // if shader-based correction is enabled
2638 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2639 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002640 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002641 // If the drop shadow exceeds the max texture size or couldn't be
2642 // allocated, skip drawing
2643 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002644 const AutoTexture autoCleanup(shadow);
2645
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002646 const float sx = x - shadow->left + textShadow.dx;
2647 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002648
Tom Hudson984162f2014-10-10 13:38:16 -04002649 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002650 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002651 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002652 }
2653
2654 setupDraw();
2655 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002656 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002657 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002658 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002659 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002660 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002661 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2662 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002663 setupDrawTexture(shadow->id);
2664 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002665 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002666 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08002667 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Raph Levien416a8472012-07-19 22:48:17 -07002668
Chris Craik96a5c4c2015-01-27 15:46:35 -08002669 glDrawArrays(GL_TRIANGLE_STRIP, 0, kMeshCount);
Raph Levien416a8472012-07-19 22:48:17 -07002670}
2671
Romain Guy768bffc2013-02-27 13:50:45 -08002672bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002673 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05002674 return MathUtils::isZero(alpha)
2675 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002676}
2677
Tom Hudson107843d2014-09-08 11:26:26 -04002678void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002679 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002680 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002681 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002682 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002683
Romain Guy671d6cf2012-01-18 12:39:17 -08002684 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002685 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002686 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002687 }
2688
Chris Craik65fe5ee2015-01-26 18:06:29 -08002689 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002690
Romain Guy671d6cf2012-01-18 12:39:17 -08002691 float x = 0.0f;
2692 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002693 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002694 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002695 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2696 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002697 }
2698
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002699 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002700 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002701
2702 int alpha;
2703 SkXfermode::Mode mode;
2704 getAlphaAndMode(paint, &alpha, &mode);
2705
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002706 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002707 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002708 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002709 }
2710
Romain Guy671d6cf2012-01-18 12:39:17 -08002711 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002712 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002713 if (pureTranslate && !linearFilter) {
2714 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2715 }
Romain Guy257ae352013-03-20 16:31:12 -07002716 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002717
Rob Tsuk487a92c2015-01-06 13:22:54 -08002718 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002719 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2720
Romain Guy211370f2012-02-01 16:10:55 -08002721 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002722
Victoria Lease1e546812013-06-25 14:25:17 -07002723 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002724 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002725 positions, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002726 if (hasActiveLayer) {
2727 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002728 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002729 }
2730 dirtyLayerUnchecked(bounds, getRegion());
2731 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002732 }
Chet Haase48659092012-05-31 15:21:51 -07002733
Tom Hudson107843d2014-09-08 11:26:26 -04002734 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002735}
2736
Chris Craik59744b72014-07-01 17:56:52 -07002737bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002738 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002739 outMatrix->setIdentity();
2740 return false;
2741 } else if (CC_UNLIKELY(transform.isPerspective())) {
2742 outMatrix->setIdentity();
2743 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002744 }
Chris Craik59744b72014-07-01 17:56:52 -07002745
2746 /**
Chris Craika736cd92014-08-04 13:18:38 -07002747 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2748 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002749 */
2750 float sx, sy;
2751 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002752 outMatrix->setScale(
2753 roundf(fmaxf(1.0f, sx)),
2754 roundf(fmaxf(1.0f, sy)));
2755 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002756}
2757
Tom Hudson984162f2014-10-10 13:38:16 -04002758int OpenGLRenderer::getSaveCount() const {
2759 return mState.getSaveCount();
2760}
2761
2762int OpenGLRenderer::save(int flags) {
2763 return mState.save(flags);
2764}
2765
2766void OpenGLRenderer::restore() {
2767 return mState.restore();
2768}
2769
2770void OpenGLRenderer::restoreToCount(int saveCount) {
2771 return mState.restoreToCount(saveCount);
2772}
2773
2774void OpenGLRenderer::translate(float dx, float dy, float dz) {
2775 return mState.translate(dx, dy, dz);
2776}
2777
2778void OpenGLRenderer::rotate(float degrees) {
2779 return mState.rotate(degrees);
2780}
2781
2782void OpenGLRenderer::scale(float sx, float sy) {
2783 return mState.scale(sx, sy);
2784}
2785
2786void OpenGLRenderer::skew(float sx, float sy) {
2787 return mState.skew(sx, sy);
2788}
2789
2790void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2791 mState.setMatrix(matrix);
2792}
2793
2794void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2795 mState.concatMatrix(matrix);
2796}
2797
2798bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2799 return mState.clipRect(left, top, right, bottom, op);
2800}
2801
2802bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2803 return mState.clipPath(path, op);
2804}
2805
2806bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2807 return mState.clipRegion(region, op);
2808}
2809
2810void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2811 mState.setClippingOutline(allocator, outline);
2812}
2813
2814void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2815 const Rect& rect, float radius, bool highPriority) {
2816 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2817}
2818
Tom Hudson107843d2014-09-08 11:26:26 -04002819void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002820 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002821 DrawOpMode drawOpMode) {
2822
Chris Craik527a3aa2013-03-04 10:19:31 -08002823 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002824 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2825 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002826 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002827 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002828 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002829 }
Romain Guycac5fd32011-12-01 20:08:50 -08002830 }
2831
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002832 const float oldX = x;
2833 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002834
Chris Craikd6b65f62014-01-01 14:45:21 -08002835 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002836 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002837
Romain Guy211370f2012-02-01 16:10:55 -08002838 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002839 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2840 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002841 }
2842
Romain Guy86568192010-12-14 15:55:39 -08002843 int alpha;
2844 SkXfermode::Mode mode;
2845 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002846
Romain Guyc74f45a2013-02-26 19:10:14 -08002847 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2848
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002849 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002850 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002851 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002852 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002853 }
2854
Romain Guy19d4dd82013-03-04 11:14:26 -08002855 const bool hasActiveLayer = hasLayer();
2856
Romain Guy624234f2013-03-05 16:43:31 -08002857 // We only pass a partial transform to the font renderer. That partial
2858 // matrix defines how glyphs are rasterized. Typically we want glyphs
2859 // to be rasterized at their final size on screen, which means the partial
2860 // matrix needs to take the scale factor into account.
2861 // When a partial matrix is used to transform glyphs during rasterization,
2862 // the mesh is generated with the inverse transform (in the case of scale,
2863 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2864 // apply the full transform matrix at draw time in the vertex shader.
2865 // Applying the full matrix in the shader is the easiest way to handle
2866 // rotation and perspective and allows us to always generated quads in the
2867 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002868 SkMatrix fontTransform;
2869 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2870 || fabs(y - (int) y) > 0.0f
2871 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002872 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002873 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002874
Romain Guy624234f2013-03-05 16:43:31 -08002875 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08002876 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002877 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 -07002878
Raph Levien996e57c2012-07-23 15:22:52 -07002879 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002880 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002881
2882 // don't call issuedrawcommand, do it at end of batch
2883 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002884 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002885 SkPaint paintCopy(*paint);
2886 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2887 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002888 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002889 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002890 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002891 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002892 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002893
Chris Craik527a3aa2013-03-04 10:19:31 -08002894 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002895 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002896 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002897 }
Chris Craik41541822013-05-03 16:35:54 -07002898 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002899 }
Romain Guy694b5192010-07-21 21:33:20 -07002900
Chris Craike63f7c622013-10-17 10:30:55 -07002901 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002902
Tom Hudson107843d2014-09-08 11:26:26 -04002903 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002904}
2905
Tom Hudson107843d2014-09-08 11:26:26 -04002906void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002907 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002908 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002909 return;
Romain Guy03d58522012-02-24 17:54:07 -08002910 }
2911
Chris Craik39a908c2013-06-13 14:39:01 -07002912 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002913 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002914
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002915 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002916 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002917 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002918
2919 int alpha;
2920 SkXfermode::Mode mode;
2921 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002922 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002923
Tom Hudson984162f2014-10-10 13:38:16 -04002924 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002925 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 -08002926
Romain Guy97771732012-02-28 18:17:02 -08002927 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002928
2929 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chris Craike84a2082014-12-22 14:28:49 -08002930 hOffset, vOffset, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002931 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002932 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002933 dirtyLayerUnchecked(bounds, getRegion());
2934 }
Romain Guy97771732012-02-28 18:17:02 -08002935 }
Chet Haase48659092012-05-31 15:21:51 -07002936
Tom Hudson107843d2014-09-08 11:26:26 -04002937 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002938}
2939
Tom Hudson107843d2014-09-08 11:26:26 -04002940void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002941 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002942
Chris Craik44eb2c02015-01-29 09:45:09 -08002943 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002944
Romain Guyfb8b7632010-08-23 21:05:08 -07002945 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002946 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002947 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002948
Romain Guy8b55f372010-08-18 17:10:07 -07002949 const float x = texture->left - texture->offset;
2950 const float y = texture->top - texture->offset;
2951
Romain Guy01d58e42011-01-19 21:54:02 -08002952 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002953 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002954}
2955
Tom Hudson107843d2014-09-08 11:26:26 -04002956void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002957 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002958 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002959 }
2960
Chris Craike84a2082014-12-22 14:28:49 -08002961 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002962 if (layer->isTextureLayer()) {
2963 transform = &layer->getTransform();
2964 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002965 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002966 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002967 }
2968 }
2969
Chris Craik39a908c2013-06-13 14:39:01 -07002970 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08002971 const bool rejected = mState.calculateQuickRejectForScissor(
2972 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2973 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002974
2975 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002976 if (transform && !transform->isIdentity()) {
2977 restore();
2978 }
Tom Hudson107843d2014-09-08 11:26:26 -04002979 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002980 }
2981
Chris Craik62d307c2014-07-29 10:35:13 -07002982 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2983 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2984
Romain Guy5bb3c732012-11-29 17:52:58 -08002985 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002986
Chris Craik65fe5ee2015-01-26 18:06:29 -08002987 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08002988 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002989
Romain Guy211370f2012-02-01 16:10:55 -08002990 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002991 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002992 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2993 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002994 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002995
Chris Craik16ecda52013-03-29 10:59:59 -07002996 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002997 setupDraw();
2998 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002999 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003000 setupDrawColorFilter(layer->getColorFilter());
3001 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003002 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003003 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003004 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07003005 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08003006 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3007 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3008 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003009
Romain Guyd21b6e12011-11-30 20:21:23 -08003010 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003011 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003012 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003013 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003014 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003015 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003016 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3017 }
Romain Guyf219da52011-01-16 12:54:25 -08003018
Romain Guy448455f2013-07-22 13:57:50 -07003019 TextureVertex* mesh = &layer->mesh[0];
3020 GLsizei elementsCount = layer->meshElementCount;
3021
3022 while (elementsCount > 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08003023 GLsizei drawCount = min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Romain Guy448455f2013-07-22 13:57:50 -07003024
Romain Guy3380cfd2013-08-15 16:57:57 -07003025 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003026 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
Chris Craike84a2082014-12-22 14:28:49 -08003027 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr));
Romain Guy448455f2013-07-22 13:57:50 -07003028
3029 elementsCount -= drawCount;
3030 // Though there are 4 vertices in a quad, we use 6 indices per
3031 // quad to draw with GL_TRIANGLES
3032 mesh += (drawCount / 6) * 4;
3033 }
Romain Guyf219da52011-01-16 12:54:25 -08003034
Romain Guy3a3133d2011-02-01 22:59:58 -08003035#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003036 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003037#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003038 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003039
Romain Guy5bb3c732012-11-29 17:52:58 -08003040 if (layer->debugDrawUpdate) {
3041 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003042
3043 SkPaint paint;
3044 paint.setColor(0x7f00ff00);
3045 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003046 }
Romain Guyf219da52011-01-16 12:54:25 -08003047 }
Chris Craik34416ea2013-04-15 16:08:28 -07003048 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003049
Romain Guyb2e2f242012-10-17 18:18:35 -07003050 if (transform && !transform->isIdentity()) {
3051 restore();
3052 }
3053
Tom Hudson107843d2014-09-08 11:26:26 -04003054 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003055}
3056
Romain Guy6926c722010-07-12 20:20:03 -07003057///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003058// Draw filters
3059///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003060void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3061 // We should never get here since we apply the draw filter when stashing
3062 // the paints in the DisplayList.
3063 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003064}
3065
3066///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003067// Drawing implementation
3068///////////////////////////////////////////////////////////////////////////////
3069
Chris Craikd218a922014-01-02 17:13:34 -08003070Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003071 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003072 if (!texture) {
3073 return mCaches.textureCache.get(bitmap);
3074 }
3075 return texture;
3076}
3077
Romain Guy01d58e42011-01-19 21:54:02 -08003078void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003079 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003080 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003081 return;
3082 }
3083
3084 int alpha;
3085 SkXfermode::Mode mode;
3086 getAlphaAndMode(paint, &alpha, &mode);
3087
3088 setupDraw();
3089 setupDrawWithTexture(true);
3090 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003091 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003092 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003093 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003094 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003095 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3096 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003097 setupDrawTexture(texture->id);
3098 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003099 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003100 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08003101 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Romain Guy01d58e42011-01-19 21:54:02 -08003102
Chris Craik96a5c4c2015-01-27 15:46:35 -08003103 glDrawArrays(GL_TRIANGLE_STRIP, 0, kMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003104}
3105
Romain Guyf607bdc2010-09-10 19:20:06 -07003106// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003107#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3108#define kStdUnderline_Offset (1.0f / 9.0f)
3109#define kStdUnderline_Thickness (1.0f / 18.0f)
3110
Chris Craikd218a922014-01-02 17:13:34 -08003111void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3112 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003113 // Handle underline and strike-through
3114 uint32_t flags = paint->getFlags();
3115 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003116 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003117
Romain Guy211370f2012-02-01 16:10:55 -08003118 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003119 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003120 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003121
Raph Levien8b4072d2012-07-30 15:50:00 -07003122 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003123 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003124
Romain Guyf6834472011-01-23 13:32:12 -08003125 int linesCount = 0;
3126 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3127 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3128
3129 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003130 float points[pointsCount];
3131 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003132
3133 if (flags & SkPaint::kUnderlineText_Flag) {
3134 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003135 points[currentPoint++] = left;
3136 points[currentPoint++] = top;
3137 points[currentPoint++] = left + underlineWidth;
3138 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003139 }
3140
3141 if (flags & SkPaint::kStrikeThruText_Flag) {
3142 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003143 points[currentPoint++] = left;
3144 points[currentPoint++] = top;
3145 points[currentPoint++] = left + underlineWidth;
3146 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003147 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003148
Romain Guy726aeba2011-06-01 14:52:00 -07003149 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003150
Romain Guy726aeba2011-06-01 14:52:00 -07003151 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003152 }
3153 }
3154}
3155
Tom Hudson107843d2014-09-08 11:26:26 -04003156void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003157 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003158 return;
Romain Guy672433d2013-01-04 19:05:13 -08003159 }
3160
Tom Hudson107843d2014-09-08 11:26:26 -04003161 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003162}
3163
Tom Hudson107843d2014-09-08 11:26:26 -04003164void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003165 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003166 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003167
Chris Craik15a07a22014-01-26 13:43:53 -08003168 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08003169 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07003170
3171 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003172 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003173
ztenghui14a4e352014-08-13 10:44:39 -07003174 // The caller has made sure casterAlpha > 0.
3175 float ambientShadowAlpha = mAmbientShadowAlpha;
3176 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3177 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3178 }
3179 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3180 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003181 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003182 }
ztenghui7b4516e2014-01-07 10:42:55 -08003183
ztenghui14a4e352014-08-13 10:44:39 -07003184 float spotShadowAlpha = mSpotShadowAlpha;
3185 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3186 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3187 }
3188 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3189 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003190 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003191 }
ztenghui7b4516e2014-01-07 10:42:55 -08003192
Tom Hudson107843d2014-09-08 11:26:26 -04003193 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003194}
3195
Tom Hudson107843d2014-09-08 11:26:26 -04003196void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003197 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003198 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003199 return;
Romain Guy3b753822013-03-05 10:27:35 -08003200 }
Romain Guy735738c2012-12-03 12:34:51 -08003201
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003202 int color = paint->getColor();
3203 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003204 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003205 color |= 0x00ffffff;
3206 }
3207
Romain Guy672433d2013-01-04 19:05:13 -08003208 float left = FLT_MAX;
3209 float top = FLT_MAX;
3210 float right = FLT_MIN;
3211 float bottom = FLT_MIN;
3212
Romain Guy448455f2013-07-22 13:57:50 -07003213 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003214 Vertex* vertex = mesh;
3215
Chris Craik2af46352012-11-26 18:30:17 -08003216 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003217 float l = rects[index + 0];
3218 float t = rects[index + 1];
3219 float r = rects[index + 2];
3220 float b = rects[index + 3];
3221
Romain Guy3b753822013-03-05 10:27:35 -08003222 Vertex::set(vertex++, l, t);
3223 Vertex::set(vertex++, r, t);
3224 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003225 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003226
Romain Guy3b753822013-03-05 10:27:35 -08003227 left = fminf(left, l);
3228 top = fminf(top, t);
3229 right = fmaxf(right, r);
3230 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003231 }
3232
Chris Craikf0a59072013-11-19 18:00:46 -08003233 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003234 return;
Romain Guya362c692013-02-04 13:50:16 -08003235 }
Romain Guy672433d2013-01-04 19:05:13 -08003236
Romain Guy672433d2013-01-04 19:05:13 -08003237 setupDraw();
3238 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003239 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003240 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003241 setupDrawColorFilter(getColorFilter(paint));
3242 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003243 setupDrawProgram();
3244 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003245 setupDrawModelView(kModelViewMode_Translate, false,
3246 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003247 setupDrawColorUniforms(getShader(paint));
3248 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003249 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003250
Romain Guy8ce00302013-01-15 18:51:42 -08003251 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003252 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003253 }
3254
Chris Craik4063a0e2013-11-15 16:06:56 -08003255 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003256
Tom Hudson107843d2014-09-08 11:26:26 -04003257 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003258}
3259
Romain Guy026c5e162010-06-28 17:12:22 -07003260void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003261 const SkPaint* paint, bool ignoreTransform) {
3262 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003263 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003264 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003265 color |= 0x00ffffff;
3266 }
3267
Romain Guy70ca14e2010-12-13 18:24:33 -08003268 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003269 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003270 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003271 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003272 setupDrawColorFilter(getColorFilter(paint));
3273 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003274 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003275 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3276 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003277 setupDrawColorUniforms(getShader(paint));
3278 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003279 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003280 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003281
Chris Craik96a5c4c2015-01-27 15:46:35 -08003282 glDrawArrays(GL_TRIANGLE_STRIP, 0, kMeshCount);
Romain Guyc95c8d62010-09-17 15:31:32 -07003283}
3284
Romain Guy82ba8142010-07-09 13:25:56 -07003285void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003286 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003287 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003288
Chris Craike84a2082014-12-22 14:28:49 -08003289 GLvoid* vertices = (GLvoid*) nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -08003290 GLvoid* texCoords = (GLvoid*) kMeshTextureOffset;
Romain Guy3b748a42013-04-17 18:54:38 -07003291
3292 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003293 vertices = &mMeshVertices[0].x;
3294 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003295
3296 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3297 texture->uvMapper->map(uvs);
3298
3299 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3300 }
3301
Chris Craikd6b65f62014-01-01 14:45:21 -08003302 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3303 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3304 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003305
Romain Guyd21b6e12011-11-30 20:21:23 -08003306 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003307 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003308 paint, texture->blend, vertices, texCoords,
Chris Craik96a5c4c2015-01-27 15:46:35 -08003309 GL_TRIANGLE_STRIP, kMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003310 } else {
Chris Craik678625242014-02-28 12:26:34 -08003311 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003312 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Chris Craik96a5c4c2015-01-27 15:46:35 -08003313 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, kMeshCount);
Romain Guy3b748a42013-04-17 18:54:38 -07003314 }
3315
3316 if (texture->uvMapper) {
3317 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003318 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003319}
3320
Romain Guyf7f93552010-07-08 19:17:03 -07003321void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003322 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003323 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003324 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3325 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003326
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003327 int a;
3328 SkXfermode::Mode mode;
3329 getAlphaAndMode(paint, &a, &mode);
3330 const float alpha = a / 255.0f;
3331
Romain Guy746b7402010-10-26 16:27:31 -07003332 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003333 setupDrawWithTexture();
3334 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003335 setupDrawColorFilter(getColorFilter(paint));
3336 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003337 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003338 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003339 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003340 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003341 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003342 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003343 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003344
Romain Guy6820ac82010-09-15 18:11:50 -07003345 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003346}
3347
Romain Guy3b748a42013-04-17 18:54:38 -07003348void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003349 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003350 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003351 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3352 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003353
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003354 int a;
3355 SkXfermode::Mode mode;
3356 getAlphaAndMode(paint, &a, &mode);
3357 const float alpha = a / 255.0f;
3358
Romain Guy3b748a42013-04-17 18:54:38 -07003359 setupDraw();
3360 setupDrawWithTexture();
3361 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003362 setupDrawColorFilter(getColorFilter(paint));
3363 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003364 setupDrawProgram();
3365 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003366 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003367 setupDrawTexture(texture);
3368 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003369 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003370 setupDrawMeshIndices(vertices, texCoords, vbo);
3371
Chris Craike84a2082014-12-22 14:28:49 -08003372 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy3b748a42013-04-17 18:54:38 -07003373}
3374
Romain Guy886b2752013-01-04 12:26:18 -08003375void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003376 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003377 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003378 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003379
Chris Craike84a2082014-12-22 14:28:49 -08003380 int color = paint != nullptr ? paint->getColor() : 0;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003381 int alpha;
3382 SkXfermode::Mode mode;
3383 getAlphaAndMode(paint, &alpha, &mode);
3384
Romain Guy886b2752013-01-04 12:26:18 -08003385 setupDraw();
3386 setupDrawWithTexture(true);
Chris Craike84a2082014-12-22 14:28:49 -08003387 if (paint != nullptr) {
Romain Guy886b2752013-01-04 12:26:18 -08003388 setupDrawAlpha8Color(color, alpha);
3389 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003390 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003391 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003392 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003393 setupDrawProgram();
3394 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003395 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003396 setupDrawTexture(texture);
3397 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003398 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003399 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003400 setupDrawMesh(vertices, texCoords);
3401
3402 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003403}
3404
Romain Guya5aed0d2010-09-09 14:42:43 -07003405void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003406 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003407
Chris Craike84a2082014-12-22 14:28:49 -08003408 if (writableSnapshot()->roundRectClipState != nullptr /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003409 blend = true;
3410 mDescription.hasRoundRectClip = true;
3411 }
3412 mSkipOutlineClip = true;
3413
Romain Guy82ba8142010-07-09 13:25:56 -07003414 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003415
Romain Guy82ba8142010-07-09 13:25:56 -07003416 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003417 // These blend modes are not supported by OpenGL directly and have
3418 // to be implemented using shaders. Since the shader will perform
3419 // the blending, turn blending off here
3420 // If the blend mode cannot be implemented using shaders, fall
3421 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003422 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003423 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003424 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003425 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003426
Chris Craik44eb2c02015-01-29 09:45:09 -08003427 mRenderState.blend().disable();
Romain Guy82bc7a72012-01-03 14:13:39 -08003428 return;
3429 } else {
3430 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003431 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003432 }
Chris Craik44eb2c02015-01-29 09:45:09 -08003433 mRenderState.blend().enable(mode, swapSrcDst);
3434 } else {
3435 mRenderState.blend().disable();
Romain Guy82ba8142010-07-09 13:25:56 -07003436 }
Romain Guybd6b79b2010-06-26 00:13:53 -07003437}
3438
Romain Guy026c5e162010-06-28 17:12:22 -07003439void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003440 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003441 TextureVertex::setUV(v++, u1, v1);
3442 TextureVertex::setUV(v++, u2, v1);
3443 TextureVertex::setUV(v++, u1, v2);
3444 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003445}
3446
Chris Craike84a2082014-12-22 14:28:49 -08003447void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
3448 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003449 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003450 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3451 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003452 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003453 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003454 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003455}
3456
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003457float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003458 float alpha;
3459 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3460 alpha = mDrawModifiers.mOverrideLayerAlpha;
3461 } else {
3462 alpha = layer->getAlpha() / 255.0f;
3463 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003464 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003465}
3466
Romain Guy9d5316e2010-06-24 19:30:36 -07003467}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003468}; // namespace android