blob: b62af3b9d40218d79db3e82d6780d8850c57e4f9 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chris Craik65fe5ee2015-01-26 18:06:29 -080017#include "OpenGLRenderer.h"
18
19#include "DeferredDisplayList.h"
20#include "DisplayListRenderer.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080021#include "GammaFontRenderer.h"
Chris Craik03188872015-02-02 18:39:33 -080022#include "Glop.h"
23#include "GlopBuilder.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080024#include "Patch.h"
25#include "PathTessellator.h"
26#include "Properties.h"
27#include "RenderNode.h"
Chris Craik03188872015-02-02 18:39:33 -080028#include "renderstate/MeshState.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080029#include "renderstate/RenderState.h"
30#include "ShadowTessellator.h"
31#include "SkiaShader.h"
32#include "Vector.h"
33#include "VertexBuffer.h"
34#include "utils/GLUtils.h"
35#include "utils/PaintUtils.h"
36#include "utils/TraceUtils.h"
37
Romain Guye4d01122010-06-16 18:44:05 -070038#include <stdlib.h>
39#include <stdint.h>
40#include <sys/types.h>
41
Romain Guy5cbbce52010-06-27 22:59:20 -070042#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070043#include <SkColor.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040044#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070045#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070046
Romain Guye4d01122010-06-16 18:44:05 -070047#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070048#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070049
Romain Guy08aa2cb2011-03-17 11:06:57 -070050#include <private/hwui/DrawGlInfo.h>
51
Romain Guy5b3b3522010-10-27 18:57:51 -070052#include <ui/Rect.h>
53
Chris Craik62d307c2014-07-29 10:35:13 -070054#if DEBUG_DETAILED_EVENTS
55 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
56#else
57 #define EVENT_LOGD(...)
58#endif
59
Chris Craik0519c812015-02-11 13:17:06 -080060#define USE_GLOPS true
61
Romain Guye4d01122010-06-16 18:44:05 -070062namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070063namespace uirenderer {
64
Romain Guy9d5316e2010-06-24 19:30:36 -070065///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -070066// Constructors/destructor
67///////////////////////////////////////////////////////////////////////////////
68
John Reck3b202512014-06-23 13:13:08 -070069OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -040070 : mState(*this)
Chris Craik058fc642014-07-23 18:19:28 -070071 , mCaches(Caches::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -070072 , mRenderState(renderState)
Chris Craik65fe5ee2015-01-26 18:06:29 -080073 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -070074 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -070075 , mSuppressTiling(false)
76 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -040077 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -070078 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -070079 , mLightRadius(FLT_MIN)
80 , mAmbientShadowAlpha(0)
81 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -080082 // *set* draw modifiers to be 0
83 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -070084 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -070085
Chris Craik03188872015-02-02 18:39:33 -080086 memcpy(mMeshVertices, kUnitQuadVertices, sizeof(kUnitQuadVertices));
Romain Guye4d01122010-06-16 18:44:05 -070087}
88
Romain Guy85bf02f2010-06-22 13:11:24 -070089OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -070090 // The context has already been destroyed at this point, do not call
91 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -070092}
93
Romain Guy87e2f7572012-09-24 11:37:12 -070094void OpenGLRenderer::initProperties() {
95 char property[PROPERTY_VALUE_MAX];
96 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
97 mScissorOptimizationDisabled = !strcasecmp(property, "true");
98 INIT_LOGD(" Scissor optimization %s",
99 mScissorOptimizationDisabled ? "disabled" : "enabled");
100 } else {
101 INIT_LOGD(" Scissor optimization enabled");
102 }
Romain Guy13631f32012-01-30 17:41:55 -0800103}
104
Chris Craik058fc642014-07-23 18:19:28 -0700105void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
106 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
107 mLightCenter = lightCenter;
108 mLightRadius = lightRadius;
109 mAmbientShadowAlpha = ambientShadowAlpha;
110 mSpotShadowAlpha = spotShadowAlpha;
111}
112
Romain Guy13631f32012-01-30 17:41:55 -0800113///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700114// Setup
115///////////////////////////////////////////////////////////////////////////////
116
Chris Craik797b95b2014-05-20 18:10:25 -0700117void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700118 glDisable(GL_DITHER);
119 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Chris Craik284b2432014-09-18 16:05:35 -0700120 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700121}
122
Romain Guy96885eb2013-03-26 15:05:58 -0700123void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800124 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800125 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400126 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700127 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700128 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700129}
130
Tom Hudson107843d2014-09-08 11:26:26 -0400131void OpenGLRenderer::startFrame() {
132 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700133 mFrameStarted = true;
134
Tom Hudson984162f2014-10-10 13:38:16 -0400135 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700136
Romain Guy96885eb2013-03-26 15:05:58 -0700137 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700138
Tom Hudson984162f2014-10-10 13:38:16 -0400139 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800140
Romain Guy54c1a642012-09-27 17:55:46 -0700141 // Functors break the tiling extension in pretty spectacular ways
142 // This ensures we don't use tiling when a functor is going to be
143 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700144 mSuppressTiling = mCaches.hasRegisteredFunctors()
145 || mFirstFrameAfterResize;
146 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700147
Chris Craikd6b65f62014-01-01 14:45:21 -0800148 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700149
Romain Guy7c450aa2012-09-21 19:15:00 -0700150 debugOverdraw(true, true);
151
Tom Hudson107843d2014-09-08 11:26:26 -0400152 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700153 mTilingClip.right, mTilingClip.bottom, mOpaque);
154}
155
Tom Hudson107843d2014-09-08 11:26:26 -0400156void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700157 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700158
Romain Guy96885eb2013-03-26 15:05:58 -0700159 setupFrameState(left, top, right, bottom, opaque);
160
161 // Layer renderers will start the frame immediately
162 // The framebuffer renderer will first defer the display list
163 // for each layer and wait until the first drawing command
164 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800165 if (currentSnapshot()->fbo == 0) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800166 mRenderState.blend().syncEnabled();
Romain Guy96885eb2013-03-26 15:05:58 -0700167 updateLayers();
168 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400169 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700170 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700171}
172
Romain Guydcfc8362013-01-03 13:08:57 -0800173void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
174 // If we know that we are going to redraw the entire framebuffer,
175 // perform a discard to let the driver know we don't need to preserve
176 // the back buffer for this frame.
Chris Craik117bdbc2015-02-05 10:12:38 -0800177 if (mCaches.extensions().hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400178 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
179 const bool isFbo = onGetTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800180 const GLenum attachments[] = {
181 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
182 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800183 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
184 }
185}
186
Tom Hudson107843d2014-09-08 11:26:26 -0400187void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700188 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800189 mRenderState.scissor().setEnabled(true);
190 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700191 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400192 mDirty = true;
193 return;
Romain Guyddf74372012-05-22 14:07:07 -0700194 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700195
Chris Craik65fe5ee2015-01-26 18:06:29 -0800196 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700197}
198
henry.uh_chen33f5a592014-07-02 19:36:56 +0800199void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700200 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800201 const Snapshot* snapshot = currentSnapshot();
202
Chris Craik14e51302013-12-30 15:32:54 -0800203 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800204 if (snapshot->flags & Snapshot::kFlagFboTarget) {
205 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700206 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700207
henry.uh_chen33f5a592014-07-02 19:36:56 +0800208 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800209 }
210}
211
henry.uh_chen33f5a592014-07-02 19:36:56 +0800212void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800213 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800214 if(expand) {
215 // Expand the startTiling region by 1
216 int leftNotZero = (clip.left > 0) ? 1 : 0;
217 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
218
219 mCaches.startTiling(
220 clip.left - leftNotZero,
221 windowHeight - clip.bottom - topNotZero,
222 clip.right - clip.left + leftNotZero + 1,
223 clip.bottom - clip.top + topNotZero + 1,
224 opaque);
225 } else {
226 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800227 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800228 }
Romain Guy54c1a642012-09-27 17:55:46 -0700229 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700230}
231
232void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700233 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700234}
235
Tom Hudson107843d2014-09-08 11:26:26 -0400236bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700237 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700238 endTiling();
Chris Craik4ac36f82014-12-09 16:54:03 -0800239 mTempPaths.clear();
240
Romain Guyca89e2a2013-03-08 17:44:20 -0800241 // When finish() is invoked on FBO 0 we've reached the end
242 // of the current frame
Tom Hudson984162f2014-10-10 13:38:16 -0400243 if (onGetTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800244 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700245 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800246 }
247
Romain Guy11cb6422012-09-21 00:39:43 -0700248 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700249#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700250 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700251#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700252
Romain Guyc15008e2010-11-10 11:59:15 -0800253#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800254 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700255#else
256 if (mCaches.getDebugLevel() & kDebugMemory) {
257 mCaches.dumpMemoryUsage();
258 }
Romain Guyc15008e2010-11-10 11:59:15 -0800259#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700260 }
Romain Guy96885eb2013-03-26 15:05:58 -0700261
262 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400263
264 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700265}
266
Romain Guy35643dd2012-09-18 15:40:58 -0700267void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700268 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
269 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700270 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700271
Chris Craik65fe5ee2015-01-26 18:06:29 -0800272 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700273 dirtyClip();
274}
275
Andreas Gampe64bb4132014-11-22 00:35:09 +0000276void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400277 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700278
Rob Tsuk487a92c2015-01-06 13:22:54 -0800279 Rect clip(mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700280 clip.snapToPixelBoundaries();
281
Romain Guyd643bb52011-03-01 14:55:21 -0800282 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800283 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800284 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800285 dirtyLayerUnchecked(clip, getRegion());
286 }
Romain Guyd643bb52011-03-01 14:55:21 -0800287
Romain Guy08aa2cb2011-03-17 11:06:57 -0700288 DrawGlInfo info;
289 info.clipLeft = clip.left;
290 info.clipTop = clip.top;
291 info.clipRight = clip.right;
292 info.clipBottom = clip.bottom;
293 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700294 info.width = getViewportWidth();
295 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800296 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700297
Tom Hudson984162f2014-10-10 13:38:16 -0400298 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700299 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400300 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700301 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
302 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800303 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700304 setScissorFromClip();
305 }
Chris Craik54f574a2013-08-26 11:23:46 -0700306
John Reck3b202512014-06-23 13:13:08 -0700307 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
308 // Scissor may have been modified, reset dirty clip
309 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800310
Tom Hudson107843d2014-09-08 11:26:26 -0400311 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800312}
313
Romain Guyf6a11b82010-06-23 17:47:49 -0700314///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700315// Debug
316///////////////////////////////////////////////////////////////////////////////
317
Chris Craik62d307c2014-07-29 10:35:13 -0700318void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
319#if DEBUG_DETAILED_EVENTS
320 const int BUFFER_SIZE = 256;
321 va_list ap;
322 char buf[BUFFER_SIZE];
323
324 va_start(ap, fmt);
325 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
326 va_end(ap);
327
328 eventMark(buf);
329#endif
330}
331
332
Romain Guy0f667532013-03-01 14:31:04 -0800333void OpenGLRenderer::eventMark(const char* name) const {
334 mCaches.eventMark(0, name);
335}
336
Romain Guy87e2f7572012-09-24 11:37:12 -0700337void OpenGLRenderer::startMark(const char* name) const {
338 mCaches.startMark(0, name);
339}
340
341void OpenGLRenderer::endMark() const {
342 mCaches.endMark();
343}
344
345void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700346 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700347}
348
349void OpenGLRenderer::renderOverdraw() {
Tom Hudson984162f2014-10-10 13:38:16 -0400350 if (mCaches.debugOverdraw && onGetTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700351 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700352
Chris Craik65fe5ee2015-01-26 18:06:29 -0800353 mRenderState.scissor().setEnabled(true);
354 mRenderState.scissor().set(clip->left,
355 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
356 clip->right - clip->left,
357 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700358
Romain Guy627c6fd2013-08-21 11:53:18 -0700359 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800360 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700361 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
362
363 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800364 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700365 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
366
367 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800368 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700369 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
370
371 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800372 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700373 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
374
Chris Craik96a5c4c2015-01-27 15:46:35 -0800375 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700376 }
377}
378
379///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700380// Layers
381///////////////////////////////////////////////////////////////////////////////
382
383bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700384 if (layer->deferredUpdateScheduled && layer->renderer
385 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700386
Romain Guy7c450aa2012-09-21 19:15:00 -0700387 if (inFrame) {
388 endTiling();
389 debugOverdraw(false, false);
390 }
Romain Guy11cb6422012-09-21 00:39:43 -0700391
Romain Guy96885eb2013-03-26 15:05:58 -0700392 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700393 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700394 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700395 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700396 }
Romain Guy11cb6422012-09-21 00:39:43 -0700397
398 if (inFrame) {
399 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800400 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700401 }
402
Romain Guy5bb3c732012-11-29 17:52:58 -0800403 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700404 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700405
406 return true;
407 }
408
409 return false;
410}
411
412void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700413 // If draw deferring is enabled this method will simply defer
414 // the display list of each individual layer. The layers remain
415 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700416 int count = mLayerUpdates.size();
417 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700418 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
419 startMark("Layer Updates");
420 } else {
421 startMark("Defer Layer Updates");
422 }
Romain Guy11cb6422012-09-21 00:39:43 -0700423
Chris Craik1206b9b2013-04-04 14:46:24 -0700424 // Note: it is very important to update the layers in order
425 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700426 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700427 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700428 }
Romain Guy11cb6422012-09-21 00:39:43 -0700429
Romain Guy96885eb2013-03-26 15:05:58 -0700430 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
431 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400432 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700433 }
434 endMark();
435 }
436}
437
438void OpenGLRenderer::flushLayers() {
439 int count = mLayerUpdates.size();
440 if (count > 0) {
441 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700442
Chris Craik1206b9b2013-04-04 14:46:24 -0700443 // Note: it is very important to update the layers in order
444 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800445 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700446 }
447
448 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400449 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700450
Romain Guy11cb6422012-09-21 00:39:43 -0700451 endMark();
452 }
453}
454
455void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
456 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700457 // Make sure we don't introduce duplicates.
458 // SortedVector would do this automatically but we need to respect
459 // the insertion order. The linear search is not an issue since
460 // this list is usually very short (typically one item, at most a few)
461 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
462 if (mLayerUpdates.itemAt(i) == layer) {
463 return;
464 }
465 }
Romain Guy11cb6422012-09-21 00:39:43 -0700466 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700467 }
468}
469
Romain Guye93482f2013-06-17 13:14:51 -0700470void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
471 if (layer) {
472 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
473 if (mLayerUpdates.itemAt(i) == layer) {
474 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700475 break;
476 }
477 }
478 }
479}
480
Romain Guy40543602013-06-12 15:31:28 -0700481void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800482 ATRACE_NAME("Update HW Layers");
Chris Craik44eb2c02015-01-29 09:45:09 -0800483 mRenderState.blend().syncEnabled();
Romain Guy40543602013-06-12 15:31:28 -0700484 updateLayers();
485 flushLayers();
486 // Wait for all the layer updates to be executed
John Reck55156372015-01-21 07:46:37 -0800487 glFinish();
Romain Guy40543602013-06-12 15:31:28 -0700488}
489
John Reck443a7142014-09-04 17:40:05 -0700490void OpenGLRenderer::markLayersAsBuildLayers() {
491 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
492 mLayerUpdates[i]->wasBuildLayered = true;
493 }
494}
495
Romain Guy11cb6422012-09-21 00:39:43 -0700496///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700497// State management
498///////////////////////////////////////////////////////////////////////////////
499
Chris Craik14e51302013-12-30 15:32:54 -0800500void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700501 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800502 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
503 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700504
Chris Craika64a2be2014-05-14 14:17:01 -0700505 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700506 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700507 }
508
Romain Guy2542d192010-08-18 11:47:12 -0700509 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700510 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700511 }
Romain Guy2542d192010-08-18 11:47:12 -0700512
Romain Guy5ec99242010-11-03 16:19:08 -0700513 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700514 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700515 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700516 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800517 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700518 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700519 }
Romain Guybb9524b2010-06-22 18:56:38 -0700520}
521
Romain Guyf6a11b82010-06-23 17:47:49 -0700522///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700523// Layers
524///////////////////////////////////////////////////////////////////////////////
525
526int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700527 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700528 // force matrix/clip isolation for layer
529 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
530
Tom Hudson984162f2014-10-10 13:38:16 -0400531 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700532
Tom Hudson984162f2014-10-10 13:38:16 -0400533 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700534 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700535 }
Romain Guyd55a8612010-06-28 17:42:46 -0700536
537 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700538}
539
Chris Craikd90144d2013-03-19 15:03:48 -0700540void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
541 const Rect untransformedBounds(bounds);
542
Chris Craikd6b65f62014-01-01 14:45:21 -0800543 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700544
545 // Layers only make sense if they are in the framebuffer's bounds
Rob Tsuk487a92c2015-01-06 13:22:54 -0800546 if (bounds.intersect(mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700547 // We cannot work with sub-pixels in this case
548 bounds.snapToPixelBoundaries();
549
550 // When the layer is not an FBO, we may use glCopyTexImage so we
551 // need to make sure the layer does not extend outside the bounds
552 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700553 const Snapshot& previous = *(currentSnapshot()->previous);
554 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
555 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700556 bounds.setEmpty();
557 } else if (fboLayer) {
558 clip.set(bounds);
559 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800560 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700561 inverse.mapRect(clip);
562 clip.snapToPixelBoundaries();
563 if (clip.intersect(untransformedBounds)) {
564 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
565 bounds.set(untransformedBounds);
566 } else {
567 clip.setEmpty();
568 }
569 }
570 } else {
571 bounds.setEmpty();
572 }
573}
574
Chris Craik408eb122013-03-26 18:55:15 -0700575void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
576 bool fboLayer, int alpha) {
577 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
578 bounds.getHeight() > mCaches.maxTextureSize ||
579 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400580 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700581 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400582 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700583 }
584}
585
Chris Craikd90144d2013-03-19 15:03:48 -0700586int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500587 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400588 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700589
Tom Hudson984162f2014-10-10 13:38:16 -0400590 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700591 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
592 // operations will be able to store and restore the current clip and transform info, and
593 // quick rejection will be correct (for display lists)
594
595 Rect bounds(left, top, right, bottom);
596 Rect clip;
597 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500598 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700599
Tom Hudson984162f2014-10-10 13:38:16 -0400600 if (!mState.currentlyIgnored()) {
601 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
602 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
603 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800604 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700605 }
606 }
607
608 return count;
609}
610
Romain Guy1c740bc2010-09-13 18:00:09 -0700611/**
612 * Layers are viewed by Skia are slightly different than layers in image editing
613 * programs (for instance.) When a layer is created, previously created layers
614 * and the frame buffer still receive every drawing command. For instance, if a
615 * layer is created and a shape intersecting the bounds of the layers and the
616 * framebuffer is draw, the shape will be drawn on both (unless the layer was
617 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
618 *
619 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
620 * texture. Unfortunately, this is inefficient as it requires every primitive to
621 * be drawn n + 1 times, where n is the number of active layers. In practice this
622 * means, for every primitive:
623 * - Switch active frame buffer
624 * - Change viewport, clip and projection matrix
625 * - Issue the drawing
626 *
627 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700628 * To avoid this, layers are implemented in a different way here, at least in the
629 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
630 * is set. When this flag is set we can redirect all drawing operations into a
631 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700632 *
633 * This implementation relies on the frame buffer being at least RGBA 8888. When
634 * a layer is created, only a texture is created, not an FBO. The content of the
635 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700636 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700637 * buffer and drawing continues as normal. This technique therefore treats the
638 * frame buffer as a scratch buffer for the layers.
639 *
640 * To compose the layers back onto the frame buffer, each layer texture
641 * (containing the original frame buffer data) is drawn as a simple quad over
642 * the frame buffer. The trick is that the quad is set as the composition
643 * destination in the blending equation, and the frame buffer becomes the source
644 * of the composition.
645 *
646 * Drawing layers with an alpha value requires an extra step before composition.
647 * An empty quad is drawn over the layer's region in the frame buffer. This quad
648 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
649 * quad is used to multiply the colors in the frame buffer. This is achieved by
650 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
651 * GL_ZERO, GL_SRC_ALPHA.
652 *
653 * Because glCopyTexImage2D() can be slow, an alternative implementation might
654 * be use to draw a single clipped layer. The implementation described above
655 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700656 *
657 * (1) The frame buffer is actually not cleared right away. To allow the GPU
658 * to potentially optimize series of calls to glCopyTexImage2D, the frame
659 * buffer is left untouched until the first drawing operation. Only when
660 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700661 */
Chet Haased48885a2012-08-28 17:43:28 -0700662bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700663 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800664 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
665 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700666
Romain Guyeb993562010-10-05 18:14:38 -0700667 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
668
Romain Guyf607bdc2010-09-10 19:20:06 -0700669 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700670 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700671 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700672 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000673 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700674
675 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400676 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700677 return false;
678 }
Romain Guyf18fd992010-07-08 11:45:51 -0700679
Chris Craik44eb2c02015-01-29 09:45:09 -0800680 mCaches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700681 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700682 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700683 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700684 }
685
Derek Sollenberger674554f2014-02-19 16:47:32 +0000686 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700687 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700688 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
689 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500690
Chet Haasea23eed82012-04-12 15:19:04 -0700691 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700692 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700693 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda57022010-07-06 11:39:32 -0700694
Romain Guy8fb95422010-08-17 18:38:51 -0700695 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400696 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
697 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700698
Chris Craika8bea8e2014-09-24 11:29:43 -0700699 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
700 fboLayer ? "" : "unclipped ",
701 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700702 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700703 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700704 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700705 } else {
706 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700707 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800708 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700709 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700710 // Workaround for some GL drivers. When reading pixels lying outside
711 // of the window we should get undefined values for those pixels.
712 // Unfortunately some drivers will turn the entire target texture black
713 // when reading outside of the window.
714 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800715 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700716 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800717 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700718
Chris Craika64a2be2014-05-14 14:17:01 -0700719 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
720 bounds.left, getViewportHeight() - bounds.bottom,
721 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700722
Romain Guy54be1cd2011-06-13 19:04:27 -0700723 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800724 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700725 }
Romain Guyeb993562010-10-05 18:14:38 -0700726 }
Romain Guyf86ef572010-07-01 11:05:42 -0700727
Romain Guyd55a8612010-06-28 17:42:46 -0700728 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700729}
730
Chris Craike63f7c622013-10-17 10:30:55 -0700731bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800732 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700733 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700734
Tom Hudson984162f2014-10-10 13:38:16 -0400735 writableSnapshot()->region = &writableSnapshot()->layer->region;
736 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
737 writableSnapshot()->fbo = layer->getFbo();
738 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
739 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
740 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800741 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700742
Romain Guy2b7028e2012-09-19 17:25:38 -0700743 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700744 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700745 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700746 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700747 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700748
749 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700750 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700751 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700752 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700753 }
754
755 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Chris Craikf27133d2015-02-19 09:51:53 -0800756 layer->getTextureId(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700757
henry.uh_chen33f5a592014-07-02 19:36:56 +0800758 // Expand the startTiling region by 1
759 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700760
761 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800762 mRenderState.scissor().setEnabled(true);
763 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700764 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700765 glClear(GL_COLOR_BUFFER_BIT);
766
767 dirtyClip();
768
769 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700770 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700771 return true;
772}
773
Romain Guy1c740bc2010-09-13 18:00:09 -0700774/**
775 * Read the documentation of createLayer() before doing anything in this method.
776 */
Chris Craik14e51302013-12-30 15:32:54 -0800777void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
778 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000779 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700780 return;
781 }
782
Chris Craik14e51302013-12-30 15:32:54 -0800783 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800784 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800785 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700786
Chris Craik39a908c2013-06-13 14:39:01 -0700787 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400788 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800789 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800790 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700791
Romain Guyeb993562010-10-05 18:14:38 -0700792 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700793 endTiling();
794
Romain Guye0aa84b2012-04-03 19:30:26 -0700795 // Detach the texture from the FBO
796 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800797
798 layer->removeFbo(false);
799
Romain Guyeb993562010-10-05 18:14:38 -0700800 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700801 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700802 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700803
Chris Craikd6b65f62014-01-01 14:45:21 -0800804 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700805 }
806
Romain Guy9ace8f52011-07-07 20:50:11 -0700807 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500808 SkPaint layerPaint;
809 layerPaint.setAlpha(layer->getAlpha());
810 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
811 layerPaint.setColorFilter(layer->getColorFilter());
812
813 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700814 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700815 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700816 }
817
Chris Craik96a5c4c2015-01-27 15:46:35 -0800818 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700819
Chris Craik44eb2c02015-01-29 09:45:09 -0800820 mCaches.textureState().activateTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700821
Romain Guy5b3b3522010-10-27 18:57:51 -0700822 // When the layer is stored in an FBO, we can save a bit of fillrate by
823 // drawing only the dirty region
824 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800825 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700826 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700827 } else if (!rect.isEmpty()) {
828 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530829
830 save(0);
831 // the layer contains screen buffer content that shouldn't be alpha modulated
832 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400833 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700834 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530835 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700836 }
Romain Guy8b55f372010-08-18 17:10:07 -0700837
Romain Guy746b7402010-10-26 16:27:31 -0700838 dirtyClip();
839
Romain Guyeb993562010-10-05 18:14:38 -0700840 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800841 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700842 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800843 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800844 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700845 }
846}
847
Romain Guyaa6c24c2011-04-28 18:40:04 -0700848void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700849 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700850
851 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700852 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700853 setupDrawWithTexture();
854 } else {
855 setupDrawWithExternalTexture();
856 }
857 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700858 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500859 setupDrawColorFilter(layer->getColorFilter());
860 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700861 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700862 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500863 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700864 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Chris Craikf27133d2015-02-19 09:51:53 -0800865 setupDrawTexture(layer->getTextureId());
Romain Guy8f0095c2011-05-02 17:24:22 -0700866 } else {
Chris Craikf27133d2015-02-19 09:51:53 -0800867 setupDrawExternalTexture(layer->getTextureId());
Romain Guy8f0095c2011-05-02 17:24:22 -0700868 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800869 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800870 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700871 layer->getWidth() == (uint32_t) rect.getWidth() &&
872 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800873 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
874 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700875
Romain Guyd21b6e12011-11-30 20:21:23 -0800876 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800877 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
878 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700879 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800880 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800881 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
882 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700883 }
884 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700885 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700886
Chris Craik117bdbc2015-02-05 10:12:38 -0800887 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700888}
889
Romain Guy5b3b3522010-10-27 18:57:51 -0700890void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700891 if (layer->isTextureLayer()) {
892 EVENT_LOGD("composeTextureLayerRect");
893 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
894 drawTextureLayer(layer, rect);
895 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
896 } else {
897 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700898 const Rect& texCoords = layer->texCoords;
899 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
900 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700901
Romain Guy9ace8f52011-07-07 20:50:11 -0700902 float x = rect.left;
903 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -0800904 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700905 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700906 layer->getHeight() == (uint32_t) rect.getHeight();
907
908 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700909 // When we're swapping, the layer is already in screen coordinates
910 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800911 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
912 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700913 }
914
Romain Guyd21b6e12011-11-30 20:21:23 -0800915 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700916 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800917 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700918 }
919
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500920 SkPaint layerPaint;
921 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
922 layerPaint.setXfermodeMode(layer->getMode());
923 layerPaint.setColorFilter(layer->getColorFilter());
924
925 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700926 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craikf27133d2015-02-19 09:51:53 -0800927 layer->getTextureId(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -0700928 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik117bdbc2015-02-05 10:12:38 -0800929 GL_TRIANGLE_STRIP, kUnitQuadCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700930
Romain Guyaa6c24c2011-04-28 18:40:04 -0700931 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700932 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700933}
934
Chris Craik34416ea2013-04-15 16:08:28 -0700935/**
936 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
937 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
938 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
939 * by saveLayer's restore
940 */
Tom Hudson984162f2014-10-10 13:38:16 -0400941#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
942 DRAW_COMMAND; \
943 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
944 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
945 DRAW_COMMAND; \
946 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
947 } \
Chris Craik34416ea2013-04-15 16:08:28 -0700948 }
949
950#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
951
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400952// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
953// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
954class LayerShader : public SkShader {
955public:
956 LayerShader(Layer* layer, const SkMatrix* localMatrix)
957 : INHERITED(localMatrix)
958 , mLayer(layer) {
959 }
960
Chris Craike84a2082014-12-22 14:28:49 -0800961 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400962 if (data) {
963 *data = static_cast<void*>(mLayer);
964 }
965 return true;
966 }
967
Chris Craike84a2082014-12-22 14:28:49 -0800968 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400969 return !mLayer->isBlend();
970 }
971
972protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000973 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400974 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
975 }
976
Chris Craike84a2082014-12-22 14:28:49 -0800977 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400978 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
979 }
980
Chris Craike84a2082014-12-22 14:28:49 -0800981 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400982 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -0800983 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400984 }
985private:
986 // Unowned.
987 Layer* mLayer;
988 typedef SkShader INHERITED;
989};
990
Romain Guy5b3b3522010-10-27 18:57:51 -0700991void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -0700992 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
993
994 if (layer->getConvexMask()) {
995 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
996
997 // clip to the area of the layer the mask can be larger
998 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
999
1000 SkPaint paint;
1001 paint.setAntiAlias(true);
1002 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1003
Chris Craik3f0854292014-04-15 16:18:08 -07001004 // create LayerShader to map SaveLayer content into subsequent draw
1005 SkMatrix shaderMatrix;
1006 shaderMatrix.setTranslate(rect.left, rect.bottom);
1007 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001008 LayerShader layerShader(layer, &shaderMatrix);
1009 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001010
1011 // Since the drawing primitive is defined in local drawing space,
1012 // we don't need to modify the draw matrix
1013 const SkPath* maskPath = layer->getConvexMask();
1014 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1015
Chris Craike84a2082014-12-22 14:28:49 -08001016 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -07001017 restore();
1018
1019 return;
1020 }
1021
Romain Guy5b3b3522010-10-27 18:57:51 -07001022 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001023 layer->setRegionAsRect();
1024
Chris Craik34416ea2013-04-15 16:08:28 -07001025 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001026
Romain Guy5b3b3522010-10-27 18:57:51 -07001027 layer->region.clear();
1028 return;
1029 }
1030
Chris Craik62d307c2014-07-29 10:35:13 -07001031 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001032 // standard Region based draw
1033 size_t count;
1034 const android::Rect* rects;
1035 Region safeRegion;
1036 if (CC_LIKELY(hasRectToRectTransform())) {
1037 rects = layer->region.getArray(&count);
1038 } else {
1039 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1040 rects = safeRegion.getArray(&count);
1041 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001042
Chris Craik3f0854292014-04-15 16:18:08 -07001043 const float alpha = getLayerAlpha(layer);
1044 const float texX = 1.0f / float(layer->getWidth());
1045 const float texY = 1.0f / float(layer->getHeight());
1046 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001047
Chris Craik3f0854292014-04-15 16:18:08 -07001048 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001049
Chris Craik3f0854292014-04-15 16:18:08 -07001050 // We must get (and therefore bind) the region mesh buffer
1051 // after we setup drawing in case we need to mess with the
1052 // stencil buffer in setupDraw()
1053 TextureVertex* mesh = mCaches.getRegionMesh();
1054 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001055
Chris Craik3f0854292014-04-15 16:18:08 -07001056 setupDrawWithTexture();
1057 setupDrawColor(alpha, alpha, alpha, alpha);
1058 setupDrawColorFilter(layer->getColorFilter());
1059 setupDrawBlending(layer);
1060 setupDrawProgram();
1061 setupDrawDirtyRegionsDisabled();
1062 setupDrawPureColorUniforms();
1063 setupDrawColorFilterUniforms(layer->getColorFilter());
Chris Craikf27133d2015-02-19 09:51:53 -08001064 setupDrawTexture(layer->getTextureId());
Chris Craik3f0854292014-04-15 16:18:08 -07001065 if (currentTransform()->isPureTranslate()) {
1066 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1067 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001068
Chris Craik3f0854292014-04-15 16:18:08 -07001069 layer->setFilter(GL_NEAREST);
1070 setupDrawModelView(kModelViewMode_Translate, false,
1071 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1072 } else {
1073 layer->setFilter(GL_LINEAR);
1074 setupDrawModelView(kModelViewMode_Translate, false,
1075 rect.left, rect.top, rect.right, rect.bottom);
1076 }
1077 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001078
Chris Craik3f0854292014-04-15 16:18:08 -07001079 for (size_t i = 0; i < count; i++) {
1080 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001081
Chris Craik3f0854292014-04-15 16:18:08 -07001082 const float u1 = r->left * texX;
1083 const float v1 = (height - r->top) * texY;
1084 const float u2 = r->right * texX;
1085 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001086
Chris Craik3f0854292014-04-15 16:18:08 -07001087 // TODO: Reject quads outside of the clip
1088 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1089 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1090 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1091 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001092
Chris Craik3f0854292014-04-15 16:18:08 -07001093 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001094
Chris Craik96a5c4c2015-01-27 15:46:35 -08001095 if (numQuads >= kMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001096 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001097 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001098 numQuads = 0;
1099 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001100 }
Chris Craik3f0854292014-04-15 16:18:08 -07001101 }
1102
1103 if (numQuads > 0) {
1104 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001105 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001106 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001107
Romain Guy5b3b3522010-10-27 18:57:51 -07001108#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001109 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001110#endif
1111
Chris Craik3f0854292014-04-15 16:18:08 -07001112 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001113}
1114
Romain Guy3a3133d2011-02-01 22:59:58 -08001115#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001116void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001117 size_t count;
1118 const android::Rect* rects = region.getArray(&count);
1119
1120 uint32_t colors[] = {
1121 0x7fff0000, 0x7f00ff00,
1122 0x7f0000ff, 0x7fff00ff,
1123 };
1124
1125 int offset = 0;
1126 int32_t top = rects[0].top;
1127
1128 for (size_t i = 0; i < count; i++) {
1129 if (top != rects[i].top) {
1130 offset ^= 0x2;
1131 top = rects[i].top;
1132 }
1133
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001134 SkPaint paint;
1135 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001136 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001137 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001138 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001139}
Chris Craike63f7c622013-10-17 10:30:55 -07001140#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001141
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001142void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001143 Vector<float> rects;
1144
1145 SkRegion::Iterator it(region);
1146 while (!it.done()) {
1147 const SkIRect& r = it.rect();
1148 rects.push(r.fLeft);
1149 rects.push(r.fTop);
1150 rects.push(r.fRight);
1151 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001152 it.next();
1153 }
1154
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001155 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001156}
1157
Romain Guy5b3b3522010-10-27 18:57:51 -07001158void OpenGLRenderer::dirtyLayer(const float left, const float top,
1159 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001160 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001161 Rect bounds(left, top, right, bottom);
1162 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001163 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001164 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001165}
1166
1167void OpenGLRenderer::dirtyLayer(const float left, const float top,
1168 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001169 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001170 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001171 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001172 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001173}
1174
1175void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001176 if (bounds.intersect(mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001177 bounds.snapToPixelBoundaries();
1178 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1179 if (!dirty.isEmpty()) {
1180 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001181 }
1182 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001183}
1184
Chris Craik4063a0e2013-11-15 16:06:56 -08001185void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001186 GLsizei elementsCount = quadsCount * 6;
1187 while (elementsCount > 0) {
Chris Craik922d3a72015-02-13 17:47:21 -08001188 GLsizei drawCount = MathUtils::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Romain Guy448455f2013-07-22 13:57:50 -07001189
Romain Guy3380cfd2013-08-15 16:57:57 -07001190 setupDrawIndexedVertices(&mesh[0].x);
Chris Craike84a2082014-12-22 14:28:49 -08001191 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy448455f2013-07-22 13:57:50 -07001192
1193 elementsCount -= drawCount;
1194 // Though there are 4 vertices in a quad, we use 6 indices per
1195 // quad to draw with GL_TRIANGLES
1196 mesh += (drawCount / 6) * 4;
1197 }
1198}
1199
Romain Guy54be1cd2011-06-13 19:04:27 -07001200void OpenGLRenderer::clearLayerRegions() {
Chris Craik2bb8f562015-02-17 16:42:02 -08001201 const size_t quadCount = mLayers.size();
1202 if (quadCount == 0) return;
Romain Guy54be1cd2011-06-13 19:04:27 -07001203
Tom Hudson984162f2014-10-10 13:38:16 -04001204 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001205 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001206 // Doing several glScissor/glClear here can negatively impact
1207 // GPUs with a tiler architecture, instead we draw quads with
1208 // the Clear blending mode
1209
1210 // The list contains bounds that have already been clipped
1211 // against their initial clip rect, and the current clip
1212 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001213 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001214
Chris Craik2bb8f562015-02-17 16:42:02 -08001215 Vertex mesh[quadCount * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001216 Vertex* vertex = mesh;
1217
Chris Craik2bb8f562015-02-17 16:42:02 -08001218 for (uint32_t i = 0; i < quadCount; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001219 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001220
Chris Craik51d6a3d2014-12-22 17:16:56 -08001221 Vertex::set(vertex++, bounds.left, bounds.top);
1222 Vertex::set(vertex++, bounds.right, bounds.top);
1223 Vertex::set(vertex++, bounds.left, bounds.bottom);
1224 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001225 }
Romain Guye67307c2013-02-11 18:01:20 -08001226 // We must clear the list of dirty rects before we
1227 // call setupDraw() to prevent stencil setup to do
1228 // the same thing again
1229 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001230
Chris Craik2bb8f562015-02-17 16:42:02 -08001231 if (USE_GLOPS) {
1232 Glop glop;
1233 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
1234 aBuilder.setMeshIndexedQuads(&mesh[0], quadCount)
1235 .setFillClear()
Chris Craikf27133d2015-02-19 09:51:53 -08001236 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
Chris Craik2bb8f562015-02-17 16:42:02 -08001237 .setModelViewOffsetRect(0, 0, currentSnapshot()->getClipRect())
1238 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1239 .build();
1240 renderGlop(glop);
1241 } else {
1242 SkPaint clearPaint;
1243 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001244
Chris Craik2bb8f562015-02-17 16:42:02 -08001245 setupDraw(false);
1246 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1247 setupDrawBlending(&clearPaint, true);
1248 setupDrawProgram();
1249 setupDrawPureColorUniforms();
1250 setupDrawModelView(kModelViewMode_Translate, false,
1251 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001252
Chris Craik2bb8f562015-02-17 16:42:02 -08001253 issueIndexedQuadDraw(&mesh[0], quadCount);
1254 }
Romain Guy8a4ac612012-07-17 17:32:48 -07001255
Chris Craik65fe5ee2015-01-26 18:06:29 -08001256 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001257 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001258 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001259 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001260}
1261
Romain Guybd6b79b2010-06-26 00:13:53 -07001262///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001263// State Deferral
1264///////////////////////////////////////////////////////////////////////////////
1265
Chris Craikff785832013-03-08 13:12:16 -08001266bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001267 const Rect& currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001268 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001269
Chris Craikff785832013-03-08 13:12:16 -08001270 if (stateDeferFlags & kStateDeferFlag_Draw) {
1271 // state has bounds initialized in local coordinates
1272 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001273 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001274 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001275 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1276 // is used, it should more closely duplicate the quickReject logic (in how it uses
1277 // snapToPixelBoundaries)
1278
Rob Tsuk487a92c2015-01-06 13:22:54 -08001279 if (!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001280 // quick rejected
1281 return true;
1282 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001283
Chris Craika02c4ed2013-06-14 13:43:58 -07001284 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001285 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001286 int& flags = state.mClipSideFlags;
1287 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001288 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1289 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1290 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1291 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001292 }
1293 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001294 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001295 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1296 // overdraw avoidance (since we don't know what it overlaps)
1297 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001298 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001299 }
1300 }
1301
Chris Craik527a3aa2013-03-04 10:19:31 -08001302 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1303 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001304 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001305 }
1306
Chris Craik7273daa2013-03-28 11:25:24 -07001307 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1308 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001309 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001310 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001311 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001312
1313 // always store/restore, since it's just a pointer
1314 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001315 return false;
1316}
1317
Chris Craik527a3aa2013-03-04 10:19:31 -08001318void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001319 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001320 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001321 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001322 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001323
Chris Craik527a3aa2013-03-04 10:19:31 -08001324 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001325 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001326 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001327 dirtyClip();
1328 }
Chris Craikc3566d02013-02-04 16:16:33 -08001329}
1330
Chris Craik28ce94a2013-05-31 11:38:03 -07001331/**
1332 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1333 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1334 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1335 *
1336 * This method should be called when restoreDisplayState() won't be restoring the clip
1337 */
1338void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001339 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001340 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001341 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001342 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001343 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001344 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001345 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1346 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001347}
1348
Chris Craikc3566d02013-02-04 16:16:33 -08001349///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001350// Clipping
1351///////////////////////////////////////////////////////////////////////////////
1352
Romain Guybb9524b2010-06-22 18:56:38 -07001353void OpenGLRenderer::setScissorFromClip() {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001354 Rect clip(mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001355 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001356
Chris Craik65fe5ee2015-01-26 18:06:29 -08001357 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001358 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001359 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001360 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001361}
1362
Romain Guy8ce00302013-01-15 18:51:42 -08001363void OpenGLRenderer::ensureStencilBuffer() {
1364 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1365 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1366 // just hope we have one when hasLayer() returns false.
1367 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001368 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001369 }
1370}
1371
1372void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1373 // The layer's FBO is already bound when we reach this stage
1374 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001375 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1376 // is attached after we initiated tiling. We must turn it off,
1377 // attach the new render buffer then turn tiling back on
1378 endTiling();
1379
Romain Guy8d4aeb72013-02-12 16:08:55 -08001380 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Chris Craik117bdbc2015-02-05 10:12:38 -08001381 Stencil::getSmallestStencilFormat(),
1382 layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001383 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001384
Romain Guyf735c8e2013-01-31 17:45:55 -08001385 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001386 }
1387}
1388
Rob Tsuk487a92c2015-01-06 13:22:54 -08001389static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1390 float x, float y) {
1391 Vertex v;
1392 v.x = x;
1393 v.y = y;
1394 transform.mapPoint(v.x, v.y);
1395 rectangleVertices.push_back(v);
1396}
1397
1398static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1399 Vertex v;
1400 v.x = x;
1401 v.y = y;
1402 rectangleVertices.push_back(v);
1403}
1404
1405void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
Chris Craik2bb8f562015-02-17 16:42:02 -08001406 int quadCount = rectangleList.getTransformedRectanglesCount();
1407 std::vector<Vertex> rectangleVertices(quadCount * 4);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001408 Rect scissorBox = rectangleList.calculateBounds();
1409 scissorBox.snapToPixelBoundaries();
Chris Craik2bb8f562015-02-17 16:42:02 -08001410 for (int i = 0; i < quadCount; ++i) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001411 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1412 const Matrix4& transform = tr.getTransform();
1413 Rect bounds = tr.getBounds();
1414 if (transform.rectToRect()) {
1415 transform.mapRect(bounds);
1416 if (!bounds.intersect(scissorBox)) {
1417 bounds.setEmpty();
1418 } else {
1419 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1420 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1421 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1422 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1423 }
1424 } else {
1425 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1426 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1427 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1428 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1429 }
1430 }
1431
Chris Craik65fe5ee2015-01-26 18:06:29 -08001432 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001433 scissorBox.getWidth(), scissorBox.getHeight());
1434
Chris Craik2bb8f562015-02-17 16:42:02 -08001435 if (USE_GLOPS) {
1436 Glop glop;
1437 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
1438 aBuilder.setMeshIndexedQuads(&rectangleVertices[0], rectangleVertices.size() / 4)
1439 .setFillBlack()
Chris Craikf27133d2015-02-19 09:51:53 -08001440 .setTransform(currentSnapshot()->getOrthoMatrix(), Matrix4::identity(), false)
Chris Craik2bb8f562015-02-17 16:42:02 -08001441 .setModelViewOffsetRect(0, 0, scissorBox)
1442 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
1443 .build();
1444 renderGlop(glop);
1445 return;
1446 }
1447
Rob Tsuk487a92c2015-01-06 13:22:54 -08001448 const SkPaint* paint = nullptr;
1449 setupDraw();
1450 setupDrawNoTexture();
1451 setupDrawColor(0, 0xff * currentSnapshot()->alpha);
1452 setupDrawShader(getShader(paint));
1453 setupDrawColorFilter(getColorFilter(paint));
1454 setupDrawBlending(paint);
1455 setupDrawProgram();
1456 setupDrawDirtyRegionsDisabled();
1457 setupDrawModelView(kModelViewMode_Translate, false,
1458 0.0f, 0.0f, 0.0f, 0.0f, true);
1459 setupDrawColorUniforms(getShader(paint));
1460 setupDrawShaderUniforms(getShader(paint));
1461 setupDrawColorFilterUniforms(getColorFilter(paint));
1462
1463 issueIndexedQuadDraw(&rectangleVertices[0], rectangleVertices.size() / 4);
1464}
1465
Romain Guy8ce00302013-01-15 18:51:42 -08001466void OpenGLRenderer::setStencilFromClip() {
1467 if (!mCaches.debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001468 if (!currentSnapshot()->clipIsSimple()) {
1469 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001470 EVENT_LOGD("setStencilFromClip - enabling");
1471
Romain Guy8ce00302013-01-15 18:51:42 -08001472 // NOTE: The order here is important, we must set dirtyClip to false
1473 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001474 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001475
1476 ensureStencilBuffer();
1477
Rob Tsuk487a92c2015-01-06 13:22:54 -08001478 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001479
Rob Tsuk487a92c2015-01-06 13:22:54 -08001480 bool isRectangleList = clipArea.isRectangleList();
1481 if (isRectangleList) {
1482 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1483 } else {
1484 incrementThreshold = 0;
1485 }
1486
Chris Craik96a5c4c2015-01-27 15:46:35 -08001487 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001488
1489 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001490 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001491 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001492 if (resetScissor) {
1493 // The scissor was not set so we now need to update it
1494 setScissorFromClip();
1495 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001496
Chris Craik96a5c4c2015-01-27 15:46:35 -08001497 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001498
1499 // stash and disable the outline clip state, since stencil doesn't account for outline
1500 bool storedSkipOutlineClip = mSkipOutlineClip;
1501 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001502
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001503 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001504 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001505 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1506
Rob Tsuk487a92c2015-01-06 13:22:54 -08001507 if (isRectangleList) {
1508 drawRectangleList(clipArea.getRectangleList());
1509 } else {
1510 // NOTE: We could use the region contour path to generate a smaller mesh
1511 // Since we are using the stencil we could use the red book path
1512 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001513
Rob Tsuk487a92c2015-01-06 13:22:54 -08001514 // The last parameter is important: we are not drawing in the color buffer
1515 // so we don't want to dirty the current layer, if any
1516 drawRegionRects(clipArea.getClipRegion(), paint, false);
1517 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001518 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001519 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001520
Chris Craik96a5c4c2015-01-27 15:46:35 -08001521 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001522
1523 // Draw the region used to generate the stencil if the appropriate debug
1524 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001525 // TODO: Implement for rectangle list clip areas
1526 if (mCaches.debugStencilClip == Caches::kStencilShowRegion &&
1527 !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001528 paint.setColor(0x7f0000ff);
1529 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001530 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001531 }
Romain Guy8ce00302013-01-15 18:51:42 -08001532 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001533 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001534 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001535 }
1536 }
1537}
1538
Chris Craikf0a59072013-11-19 18:00:46 -08001539/**
1540 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1541 *
1542 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1543 * style, and tessellated AA ramp
1544 */
1545bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001546 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001547 bool snapOut = paint && paint->isAntiAlias();
1548
1549 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1550 float outset = paint->getStrokeWidth() * 0.5f;
1551 left -= outset;
1552 top -= outset;
1553 right += outset;
1554 bottom += outset;
1555 }
1556
Chris Craikdeeda3d2014-05-05 19:09:33 -07001557 bool clipRequired = false;
1558 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001559 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001560 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001561 return true;
1562 }
1563
Chris Craikf23b25a2014-06-26 15:46:20 -07001564 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001565 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001566 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001567 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001568}
1569
Romain Guy8ce00302013-01-15 18:51:42 -08001570void OpenGLRenderer::debugClip() {
1571#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001572 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001573 SkPaint paint;
1574 paint.setColor(0x7f00ff00);
1575 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1576
Romain Guy8ce00302013-01-15 18:51:42 -08001577 }
1578#endif
1579}
1580
Chris Craik117bdbc2015-02-05 10:12:38 -08001581void OpenGLRenderer::renderGlop(const Glop& glop) {
1582 if (mState.getDirtyClip()) {
1583 if (mRenderState.scissor().isEnabled()) {
1584 setScissorFromClip();
1585 }
1586
1587 setStencilFromClip();
1588 }
1589 mRenderState.render(glop);
Chris Craik2ab95d72015-02-06 15:25:51 -08001590
1591 if (!mRenderState.stencil().isWriteEnabled()) {
1592 // TODO: specify more clearly when a draw should dirty the layer.
1593 // is writing to the stencil the only time we should ignore this?
1594 dirtyLayer(glop.bounds.left, glop.bounds.top, glop.bounds.right, glop.bounds.bottom);
Chris Craik0519c812015-02-11 13:17:06 -08001595 mDirty = true;
Chris Craik2ab95d72015-02-06 15:25:51 -08001596 }
Chris Craik117bdbc2015-02-05 10:12:38 -08001597}
1598
Romain Guyf6a11b82010-06-23 17:47:49 -07001599///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001600// Drawing commands
1601///////////////////////////////////////////////////////////////////////////////
1602
Chris Craik62d307c2014-07-29 10:35:13 -07001603void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001604 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001605 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001606 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001607 // Make sure setScissor & setStencil happen at the beginning of
1608 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001609 if (mState.getDirtyClip()) {
Chris Craik65fe5ee2015-01-26 18:06:29 -08001610 if (mRenderState.scissor().isEnabled()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001611 setScissorFromClip();
1612 }
Chris Craik62d307c2014-07-29 10:35:13 -07001613
Dohyun Leeadc0d9d2014-11-24 21:08:15 +09001614 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001615 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001616
Romain Guy70ca14e2010-12-13 18:24:33 -08001617 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001618
Romain Guy70ca14e2010-12-13 18:24:33 -08001619 mSetShaderColor = false;
1620 mColorSet = false;
Chris Craik0519c812015-02-11 13:17:06 -08001621 mColor.a = mColor.r = mColor.g = mColor.b = 0.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001622 mTextureUnit = 0;
1623 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001624
1625 // Enable debug highlight when what we're about to draw is tested against
1626 // the stencil buffer and if stencil highlight debugging is on
Chris Craik117bdbc2015-02-05 10:12:38 -08001627 mDescription.hasDebugHighlight = !mCaches.debugOverdraw
1628 && mCaches.debugStencilClip == Caches::kStencilShowHighlight
1629 && mRenderState.stencil().isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001630}
1631
1632void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1633 mDescription.hasTexture = true;
1634 mDescription.hasAlpha8Texture = isAlpha8;
1635}
1636
Romain Guyff316ec2013-02-13 18:39:43 -08001637void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1638 mDescription.hasTexture = true;
1639 mDescription.hasColors = true;
1640 mDescription.hasAlpha8Texture = isAlpha8;
1641}
1642
Romain Guyaa6c24c2011-04-28 18:40:04 -07001643void OpenGLRenderer::setupDrawWithExternalTexture() {
1644 mDescription.hasExternalTexture = true;
1645}
1646
Romain Guy15bc6432011-12-13 13:11:32 -08001647void OpenGLRenderer::setupDrawNoTexture() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001648 mRenderState.meshState().disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001649}
1650
Chris Craik91a8c7c2014-08-12 14:31:35 -07001651void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1652 mDescription.hasVertexAlpha = true;
1653 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001654}
1655
Romain Guy8d0d4782010-12-14 20:13:35 -08001656void OpenGLRenderer::setupDrawColor(int color, int alpha) {
Chris Craik0519c812015-02-11 13:17:06 -08001657 mColor.a = alpha / 255.0f;
1658 mColor.r = mColor.a * ((color >> 16) & 0xFF) / 255.0f;
1659 mColor.g = mColor.a * ((color >> 8) & 0xFF) / 255.0f;
1660 mColor.b = mColor.a * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001661 mColorSet = true;
Chris Craik0519c812015-02-11 13:17:06 -08001662 mSetShaderColor = mDescription.setColorModulate(mColor.a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001663}
1664
Romain Guy86568192010-12-14 15:55:39 -08001665void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
Chris Craik0519c812015-02-11 13:17:06 -08001666 mColor.a = alpha / 255.0f;
1667 mColor.r = mColor.a * ((color >> 16) & 0xFF) / 255.0f;
1668 mColor.g = mColor.a * ((color >> 8) & 0xFF) / 255.0f;
1669 mColor.b = mColor.a * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001670 mColorSet = true;
Chris Craik0519c812015-02-11 13:17:06 -08001671 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColor.r, mColor.g, mColor.b, mColor.a);
Romain Guy86568192010-12-14 15:55:39 -08001672}
1673
Romain Guy41210632012-07-16 17:04:24 -07001674void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1675 mCaches.fontRenderer->describe(mDescription, paint);
1676}
1677
Romain Guy70ca14e2010-12-13 18:24:33 -08001678void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
Chris Craik0519c812015-02-11 13:17:06 -08001679 mColor.a = a;
1680 mColor.r = r;
1681 mColor.g = g;
1682 mColor.b = b;
Romain Guy70ca14e2010-12-13 18:24:33 -08001683 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001684 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001685}
1686
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001687void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
Chris Craike84a2082014-12-22 14:28:49 -08001688 if (shader != nullptr) {
Chris Craik117bdbc2015-02-05 10:12:38 -08001689 SkiaShader::describe(&mCaches, mDescription, mCaches.extensions(), *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001690 }
1691}
1692
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001693void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001694 if (filter == nullptr) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001695 return;
1696 }
1697
1698 SkXfermode::Mode mode;
Chris Craike84a2082014-12-22 14:28:49 -08001699 if (filter->asColorMode(nullptr, &mode)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001700 mDescription.colorOp = ProgramDescription::kColorBlend;
1701 mDescription.colorMode = mode;
Chris Craike84a2082014-12-22 14:28:49 -08001702 } else if (filter->asColorMatrix(nullptr)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001703 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001704 }
1705}
1706
Romain Guyf09ef512011-05-27 11:43:46 -07001707void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1708 if (mColorSet && mode == SkXfermode::kClear_Mode) {
Chris Craik0519c812015-02-11 13:17:06 -08001709 mColor.a = 1.0f;
1710 mColor.r = mColor.g = mColor.b = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001711 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001712 }
1713}
1714
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001715void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1716 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001717 // When the blending mode is kClear_Mode, we need to use a modulate color
1718 // argb=1,0,0,0
1719 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001720 // TODO: check shader blending, once we have shader drawing support for layers.
Tom Hudson8dfaa492014-12-09 15:03:44 -05001721 bool blend = layer->isBlend()
1722 || getLayerAlpha(layer) < 1.0f
Chris Craik0519c812015-02-11 13:17:06 -08001723 || (mColorSet && mColor.a < 1.0f)
Tom Hudson8dfaa492014-12-09 15:03:44 -05001724 || PaintUtils::isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001725 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001726}
1727
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001728void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1729 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001730 // When the blending mode is kClear_Mode, we need to use a modulate color
1731 // argb=1,0,0,0
1732 accountForClear(mode);
Chris Craik0519c812015-02-11 13:17:06 -08001733 blend |= (mColorSet && mColor.a < 1.0f)
Chris Craik03188872015-02-02 18:39:33 -08001734 || (getShader(paint) && !getShader(paint)->isOpaque())
1735 || PaintUtils::isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001736 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001737}
1738
1739void OpenGLRenderer::setupDrawProgram() {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001740 mCaches.setProgram(mDescription);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001741 if (mDescription.hasRoundRectClip) {
1742 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001743 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001744 const Rect& innerRect = state->innerRect;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001745 glUniform4f(mCaches.program().getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001746 innerRect.left, innerRect.top,
1747 innerRect.right, innerRect.bottom);
Chris Craik6c15ffa2015-02-02 13:50:55 -08001748 glUniformMatrix4fv(mCaches.program().getUniform("roundRectInvTransform"),
Chris Craikdeeda3d2014-05-05 19:09:33 -07001749 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001750
1751 // add half pixel to round out integer rect space to cover pixel centers
1752 float roundedOutRadius = state->radius + 0.5f;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001753 glUniform1f(mCaches.program().getUniform("roundRectRadius"),
Chris Craik4340c262014-09-11 18:58:45 -07001754 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001755 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001756}
1757
1758void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1759 mTrackDirtyRegions = false;
1760}
1761
Chris Craik4063a0e2013-11-15 16:06:56 -08001762void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1763 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001764 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001765 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001766 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001767 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001768
Romain Guy86568192010-12-14 15:55:39 -08001769 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001770 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001771
1772 mCaches.program().set(currentSnapshot()->getOrthoMatrix(),
Chris Craike84a2082014-12-22 14:28:49 -08001773 mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001774 if (dirty && mTrackDirtyRegions) {
1775 if (!ignoreTransform) {
1776 dirtyLayer(left, top, right, bottom, *currentTransform());
1777 } else {
1778 dirtyLayer(left, top, right, bottom);
1779 }
Romain Guy86568192010-12-14 15:55:39 -08001780 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001781}
1782
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001783void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1784 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Chris Craik0519c812015-02-11 13:17:06 -08001785 mCaches.program().setColor(mColor);
Romain Guy70ca14e2010-12-13 18:24:33 -08001786 }
1787}
1788
Romain Guy86568192010-12-14 15:55:39 -08001789void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001790 if (mSetShaderColor) {
Chris Craik0519c812015-02-11 13:17:06 -08001791 mCaches.program().setColor(mColor);
Romain Guy55368412010-12-14 10:59:41 -08001792 }
1793}
1794
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001795void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
Chris Craike84a2082014-12-22 14:28:49 -08001796 if (shader == nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001797 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001798 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001799
1800 if (ignoreTransform) {
1801 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1802 // because it was built into modelView / the geometry, and the description needs to
1803 // compensate.
1804 mat4 modelViewWithoutTransform;
1805 modelViewWithoutTransform.loadInverse(*currentTransform());
1806 modelViewWithoutTransform.multiply(mModelViewMatrix);
1807 mModelViewMatrix.load(modelViewWithoutTransform);
1808 }
1809
Chris Craik117bdbc2015-02-05 10:12:38 -08001810 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit,
1811 mCaches.extensions(), *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001812}
1813
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001814void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001815 if (nullptr == filter) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001816 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001817 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001818
1819 SkColor color;
1820 SkXfermode::Mode mode;
1821 if (filter->asColorMode(&color, &mode)) {
1822 const int alpha = SkColorGetA(color);
1823 const GLfloat a = alpha / 255.0f;
1824 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1825 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1826 const GLfloat b = a * SkColorGetB(color) / 255.0f;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001827 glUniform4f(mCaches.program().getUniform("colorBlend"), r, g, b, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001828 return;
1829 }
1830
1831 SkScalar srcColorMatrix[20];
1832 if (filter->asColorMatrix(srcColorMatrix)) {
1833
1834 float colorMatrix[16];
1835 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1836 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1837 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1838 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1839
1840 // Skia uses the range [0..255] for the addition vector, but we need
1841 // the [0..1] range to apply the vector in GLSL
1842 float colorVector[4];
1843 colorVector[0] = srcColorMatrix[4] / 255.0f;
1844 colorVector[1] = srcColorMatrix[9] / 255.0f;
1845 colorVector[2] = srcColorMatrix[14] / 255.0f;
1846 colorVector[3] = srcColorMatrix[19] / 255.0f;
1847
Chris Craik6c15ffa2015-02-02 13:50:55 -08001848 glUniformMatrix4fv(mCaches.program().getUniform("colorMatrix"), 1,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001849 GL_FALSE, colorMatrix);
Chris Craik6c15ffa2015-02-02 13:50:55 -08001850 glUniform4fv(mCaches.program().getUniform("colorMatrixVector"), 1, colorVector);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001851 return;
1852 }
1853
1854 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001855}
1856
Romain Guy41210632012-07-16 17:04:24 -07001857void OpenGLRenderer::setupDrawTextGammaUniforms() {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001858 mCaches.fontRenderer->setupProgram(mDescription, mCaches.program());
Romain Guy41210632012-07-16 17:04:24 -07001859}
1860
Romain Guy70ca14e2010-12-13 18:24:33 -08001861void OpenGLRenderer::setupDrawSimpleMesh() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001862 bool force = mRenderState.meshState().bindMeshBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001863 mRenderState.meshState().bindPositionVertexPointer(force, nullptr);
Chris Craik96a5c4c2015-01-27 15:46:35 -08001864 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001865}
1866
1867void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001868 if (texture) mCaches.textureState().bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001869 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001870 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001871}
1872
Romain Guyaa6c24c2011-04-28 18:40:04 -07001873void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001874 mCaches.textureState().bindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001875 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001876 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001877}
1878
Romain Guy8f0095c2011-05-02 17:24:22 -07001879void OpenGLRenderer::setupDrawTextureTransform() {
1880 mDescription.hasTextureTransform = true;
1881}
1882
1883void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001884 glUniformMatrix4fv(mCaches.program().getUniform("mainTextureTransform"), 1,
Romain Guyaa6c24c2011-04-28 18:40:04 -07001885 GL_FALSE, &transform.data[0]);
1886}
1887
Chris Craik564acf72014-01-02 16:46:18 -08001888void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1889 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001890 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001891 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001892 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001893 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001894 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001895 }
Romain Guyd71dd362011-12-12 19:03:35 -08001896
Chris Craik6c15ffa2015-02-02 13:50:55 -08001897 mRenderState.meshState().bindPositionVertexPointer(force, vertices);
1898 if (mCaches.program().texCoords >= 0) {
1899 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001900 }
1901
Chris Craik96a5c4c2015-01-27 15:46:35 -08001902 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -08001903}
1904
Chris Craik564acf72014-01-02 16:46:18 -08001905void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1906 const GLvoid* texCoords, const GLvoid* colors) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001907 bool force = mRenderState.meshState().unbindMeshBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001908 GLsizei stride = sizeof(ColorTextureVertex);
1909
Chris Craik6c15ffa2015-02-02 13:50:55 -08001910 mRenderState.meshState().bindPositionVertexPointer(force, vertices, stride);
1911 if (mCaches.program().texCoords >= 0) {
1912 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords, stride);
Romain Guyff316ec2013-02-13 18:39:43 -08001913 }
Chris Craik6c15ffa2015-02-02 13:50:55 -08001914 int slot = mCaches.program().getAttrib("colors");
Romain Guyff316ec2013-02-13 18:39:43 -08001915 if (slot >= 0) {
1916 glEnableVertexAttribArray(slot);
1917 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1918 }
1919
Chris Craik96a5c4c2015-01-27 15:46:35 -08001920 mRenderState.meshState().unbindIndicesBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001921}
1922
Chris Craik564acf72014-01-02 16:46:18 -08001923void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1924 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001925 bool force = false;
1926 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1927 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
Chris Craik96a5c4c2015-01-27 15:46:35 -08001928 // use the default VBO found in RenderState
Romain Guy3b748a42013-04-17 18:54:38 -07001929 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001930 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy3b748a42013-04-17 18:54:38 -07001931 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001932 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001933 }
Chris Craik96a5c4c2015-01-27 15:46:35 -08001934 mRenderState.meshState().bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001935
Chris Craik6c15ffa2015-02-02 13:50:55 -08001936 mRenderState.meshState().bindPositionVertexPointer(force, vertices);
1937 if (mCaches.program().texCoords >= 0) {
1938 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001939 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001940}
1941
Romain Guy448455f2013-07-22 13:57:50 -07001942void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001943 bool force = mRenderState.meshState().unbindMeshBuffer();
1944 mRenderState.meshState().bindQuadIndicesBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001945 mRenderState.meshState().bindPositionVertexPointer(force, vertices, kVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001946}
1947
Romain Guy70ca14e2010-12-13 18:24:33 -08001948///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001949// Drawing
1950///////////////////////////////////////////////////////////////////////////////
1951
Tom Hudson107843d2014-09-08 11:26:26 -04001952void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001953 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1954 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001955 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001956 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001957 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001958 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001959 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001960 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001961 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001962 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001963 }
1964
Chris Craikef8d6f22014-12-17 11:10:28 -08001965 // Don't avoid overdraw when visualizing, since that makes it harder to
1966 // debug where it's coming from, and when the problem occurs.
1967 bool avoidOverdraw = !mCaches.debugOverdraw;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001968 DeferredDisplayList deferredList(mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001969 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001970 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001971
1972 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001973 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001974
Tom Hudson107843d2014-09-08 11:26:26 -04001975 deferredList.flush(*this, dirty);
1976 } else {
1977 // Even if there is no drawing command(Ex: invisible),
1978 // it still needs startFrame to clear buffer and start tiling.
1979 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001980 }
1981}
1982
Chris Craik0519c812015-02-11 13:17:06 -08001983void OpenGLRenderer::drawAlphaBitmap(Texture* texture, const SkPaint* paint) {
Chris Craik0519c812015-02-11 13:17:06 -08001984 float x = 0;
1985 float y = 0;
Romain Guya168d732011-03-18 16:50:13 -07001986
Romain Guy886b2752013-01-04 12:26:18 -08001987 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1988
Romain Guya168d732011-03-18 16:50:13 -07001989 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001990 if (currentTransform()->isPureTranslate()) {
Chris Craik0519c812015-02-11 13:17:06 -08001991 x = (int) floorf(currentTransform()->getTranslateX() + 0.5f);
1992 y = (int) floorf(currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001993 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001994
1995 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001996 } else {
Chris Craik0519c812015-02-11 13:17:06 -08001997 texture->setFilter(PaintUtils::getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001998 }
1999
Romain Guy3b748a42013-04-17 18:54:38 -07002000 // No need to check for a UV mapper on the texture object, only ARGB_8888
2001 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002002 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Chris Craik96a5c4c2015-01-27 15:46:35 -08002003 paint, (GLvoid*) nullptr, (GLvoid*) kMeshTextureOffset,
Chris Craik117bdbc2015-02-05 10:12:38 -08002004 GL_TRIANGLE_STRIP, kUnitQuadCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002005}
2006
Romain Guy03c00b52013-06-20 18:30:28 -07002007/**
2008 * Important note: this method is intended to draw batches of bitmaps and
2009 * will not set the scissor enable or dirty the current layer, if any.
2010 * The caller is responsible for properly dirtying the current layer.
2011 */
Tom Hudson107843d2014-09-08 11:26:26 -04002012void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002013 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
2014 const Rect& bounds, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002015 mCaches.textureState().activateTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002016 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002017 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07002018
Chris Craik527a3aa2013-03-04 10:19:31 -08002019 const AutoTexture autoCleanup(texture);
2020
Chris Craik527a3aa2013-03-04 10:19:31 -08002021 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik0519c812015-02-11 13:17:06 -08002022 texture->setFilter(pureTranslate ? GL_NEAREST : PaintUtils::getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002023
2024 const float x = (int) floorf(bounds.left + 0.5f);
2025 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04002026 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002027 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002028 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002029 GL_TRIANGLES, bitmapCount * 6, true,
2030 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002031 } else {
2032 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002033 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002034 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2035 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002036 }
2037
Tom Hudson107843d2014-09-08 11:26:26 -04002038 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08002039}
2040
Tom Hudson107843d2014-09-08 11:26:26 -04002041void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002042 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002043 return;
Romain Guy6926c722010-07-12 20:20:03 -07002044 }
2045
Chris Craik44eb2c02015-01-29 09:45:09 -08002046 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002047 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002048 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002049 const AutoTexture autoCleanup(texture);
2050
Chris Craikf27133d2015-02-19 09:51:53 -08002051 if (USE_GLOPS) {
2052 bool isAlpha8Texture = bitmap->colorType() == kAlpha_8_SkColorType;
2053 Glop glop;
2054 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
2055 aBuilder.setMeshTexturedUnitQuad(texture->uvMapper)
2056 .setFillTexturePaint(*texture, isAlpha8Texture, paint, currentSnapshot()->alpha)
2057 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
2058 .setModelViewMapUnitToRectSnap(Rect(0, 0, texture->width, texture->height))
2059 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2060 .build();
2061 renderGlop(glop);
2062 return;
2063 }
2064
Mike Reed1103b322014-07-08 12:36:44 -04002065 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik0519c812015-02-11 13:17:06 -08002066 drawAlphaBitmap(texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002067 } else {
Chris Craik0519c812015-02-11 13:17:06 -08002068 drawTextureRect(texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002069 }
Chet Haase48659092012-05-31 15:21:51 -07002070
Tom Hudson107843d2014-09-08 11:26:26 -04002071 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002072}
2073
Tom Hudson107843d2014-09-08 11:26:26 -04002074void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002075 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002076 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002077 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002078 }
2079
Chris Craik39a908c2013-06-13 14:39:01 -07002080 // TODO: use quickReject on bounds from vertices
Chris Craik65fe5ee2015-01-26 18:06:29 -08002081 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002082
Romain Guyb18d2d02011-02-10 15:52:54 -08002083 float left = FLT_MAX;
2084 float top = FLT_MAX;
2085 float right = FLT_MIN;
2086 float bottom = FLT_MIN;
2087
Romain Guya92bb4d2012-10-16 11:08:44 -07002088 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002089
Chris Craik51d6a3d2014-12-22 17:16:56 -08002090 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[count]);
2091 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08002092
Chris Craik51d6a3d2014-12-22 17:16:56 -08002093 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002094 if (!colors) {
2095 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08002096 tempColors.reset(new int[colorsCount]);
2097 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
2098 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08002099 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002100
Chris Craik44eb2c02015-01-29 09:45:09 -08002101 mCaches.textureState().activateTexture(0);
John Reckebd52612014-12-10 16:47:36 -08002102 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002103 const UvMapper& mapper(getMapper(texture));
2104
Romain Guy5a7b4662011-01-20 19:09:30 -08002105 for (int32_t y = 0; y < meshHeight; y++) {
2106 for (int32_t x = 0; x < meshWidth; x++) {
2107 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2108
2109 float u1 = float(x) / meshWidth;
2110 float u2 = float(x + 1) / meshWidth;
2111 float v1 = float(y) / meshHeight;
2112 float v2 = float(y + 1) / meshHeight;
2113
Romain Guy3b748a42013-04-17 18:54:38 -07002114 mapper.map(u1, v1, u2, v2);
2115
Romain Guy5a7b4662011-01-20 19:09:30 -08002116 int ax = i + (meshWidth + 1) * 2;
2117 int ay = ax + 1;
2118 int bx = i;
2119 int by = bx + 1;
2120 int cx = i + 2;
2121 int cy = cx + 1;
2122 int dx = i + (meshWidth + 1) * 2 + 2;
2123 int dy = dx + 1;
2124
Romain Guyff316ec2013-02-13 18:39:43 -08002125 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2126 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2127 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002128
Romain Guyff316ec2013-02-13 18:39:43 -08002129 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2130 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2131 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002132
Romain Guya92bb4d2012-10-16 11:08:44 -07002133 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2134 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2135 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2136 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002137 }
2138 }
2139
Chris Craikf0a59072013-11-19 18:00:46 -08002140 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002141 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002142 }
2143
Romain Guyff316ec2013-02-13 18:39:43 -08002144 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002145 texture = mCaches.textureCache.get(bitmap);
2146 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04002147 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002148 }
Romain Guyff316ec2013-02-13 18:39:43 -08002149 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002150 const AutoTexture autoCleanup(texture);
2151
2152 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik0519c812015-02-11 13:17:06 -08002153 texture->setFilter(PaintUtils::getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002154
2155 int alpha;
2156 SkXfermode::Mode mode;
2157 getAlphaAndMode(paint, &alpha, &mode);
2158
Romain Guyff316ec2013-02-13 18:39:43 -08002159 float a = alpha / 255.0f;
2160
Romain Guya92bb4d2012-10-16 11:08:44 -07002161 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002162 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002163 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002164
Romain Guyff316ec2013-02-13 18:39:43 -08002165 setupDraw();
2166 setupDrawWithTextureAndColor();
2167 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002168 setupDrawColorFilter(getColorFilter(paint));
2169 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002170 setupDrawProgram();
2171 setupDrawDirtyRegionsDisabled();
Chris Craik117bdbc2015-02-05 10:12:38 -08002172 setupDrawModelView(kModelViewMode_Translate, false, 0, 0, 0, 0);
Romain Guyff316ec2013-02-13 18:39:43 -08002173 setupDrawTexture(texture->id);
2174 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002175 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002176 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002177
2178 glDrawArrays(GL_TRIANGLES, 0, count);
2179
Chris Craik6c15ffa2015-02-02 13:50:55 -08002180 int slot = mCaches.program().getAttrib("colors");
Romain Guyff316ec2013-02-13 18:39:43 -08002181 if (slot >= 0) {
2182 glDisableVertexAttribArray(slot);
2183 }
2184
Tom Hudson107843d2014-09-08 11:26:26 -04002185 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002186}
2187
Tom Hudson107843d2014-09-08 11:26:26 -04002188void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002189 float srcLeft, float srcTop, float srcRight, float srcBottom,
2190 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002191 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002192 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002193 return;
Romain Guy6926c722010-07-12 20:20:03 -07002194 }
2195
Chris Craik44eb2c02015-01-29 09:45:09 -08002196 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002197 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002198 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002199 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002200
Romain Guy8ba548f2010-06-30 19:21:21 -07002201 const float width = texture->width;
2202 const float height = texture->height;
2203
Romain Guy3b748a42013-04-17 18:54:38 -07002204 float u1 = fmax(0.0f, srcLeft / width);
2205 float v1 = fmax(0.0f, srcTop / height);
2206 float u2 = fmin(1.0f, srcRight / width);
2207 float v2 = fmin(1.0f, srcBottom / height);
2208
2209 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002210
Chris Craik96a5c4c2015-01-27 15:46:35 -08002211 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002212 resetDrawTextureTexCoords(u1, v1, u2, v2);
2213
Romain Guyd21b6e12011-11-30 20:21:23 -08002214 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2215
Romain Guy886b2752013-01-04 12:26:18 -08002216 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2217 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002218
Romain Guy886b2752013-01-04 12:26:18 -08002219 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2220 // Apply a scale transform on the canvas only when a shader is in use
2221 // Skia handles the ratio between the dst and src rects as a scale factor
2222 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002223 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002224 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002225
Chris Craikd6b65f62014-01-01 14:45:21 -08002226 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2227 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2228 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002229
2230 dstRight = x + (dstRight - dstLeft);
2231 dstBottom = y + (dstBottom - dstTop);
2232
2233 dstLeft = x;
2234 dstTop = y;
2235
Chris Craik0519c812015-02-11 13:17:06 -08002236 texture->setFilter(scaled ? PaintUtils::getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002237 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002238 } else {
Chris Craik0519c812015-02-11 13:17:06 -08002239 texture->setFilter(PaintUtils::getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002240 }
2241
2242 if (CC_UNLIKELY(useScaleTransform)) {
2243 save(SkCanvas::kMatrix_SaveFlag);
2244 translate(dstLeft, dstTop);
2245 scale(scaleX, scaleY);
2246
2247 dstLeft = 0.0f;
2248 dstTop = 0.0f;
2249
2250 dstRight = srcRight - srcLeft;
2251 dstBottom = srcBottom - srcTop;
2252 }
2253
Mike Reed1103b322014-07-08 12:36:44 -04002254 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002255 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002256 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002257 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik117bdbc2015-02-05 10:12:38 -08002258 GL_TRIANGLE_STRIP, kUnitQuadCount, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002259 } else {
2260 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002261 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002262 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik117bdbc2015-02-05 10:12:38 -08002263 GL_TRIANGLE_STRIP, kUnitQuadCount, false, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002264 }
2265
2266 if (CC_UNLIKELY(useScaleTransform)) {
2267 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002268 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002269
2270 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002271
Tom Hudson107843d2014-09-08 11:26:26 -04002272 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002273}
2274
Tom Hudson107843d2014-09-08 11:26:26 -04002275void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002276 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002277 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002278 return;
Romain Guy6926c722010-07-12 20:20:03 -07002279 }
2280
John Reckebd52612014-12-10 16:47:36 -08002281 AssetAtlas::Entry* entry = mRenderState.assetAtlas().getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002282 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2283 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002284
Tom Hudson107843d2014-09-08 11:26:26 -04002285 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002286}
2287
Tom Hudson107843d2014-09-08 11:26:26 -04002288void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002289 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2290 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002291 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002292 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002293 }
2294
Romain Guy211370f2012-02-01 16:10:55 -08002295 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002296 mCaches.textureState().activateTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002297 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002298 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002299 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002300
Romain Guya4adcf02013-02-28 12:15:35 -08002301 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2302 texture->setFilter(GL_LINEAR, true);
2303
Chris Craikd6b65f62014-01-01 14:45:21 -08002304 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002305 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002306 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002307 const float offsetX = left + currentTransform()->getTranslateX();
2308 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002309 const size_t count = mesh->quads.size();
2310 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002311 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002312 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002313 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2314 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2315 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002316 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002317 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002318 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002319 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002320 }
2321 }
2322
Chris Craik4063a0e2013-11-15 16:06:56 -08002323 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002324 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002325 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2326 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002327
Romain Guy3b748a42013-04-17 18:54:38 -07002328 right = x + right - left;
2329 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002330 left = x;
2331 top = y;
2332 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002333 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002334 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2335 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002336 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2337 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002338 }
Chet Haase48659092012-05-31 15:21:51 -07002339
Tom Hudson107843d2014-09-08 11:26:26 -04002340 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002341}
2342
Romain Guy03c00b52013-06-20 18:30:28 -07002343/**
2344 * Important note: this method is intended to draw batches of 9-patch objects and
2345 * will not set the scissor enable or dirty the current layer, if any.
2346 * The caller is responsible for properly dirtying the current layer.
2347 */
Tom Hudson107843d2014-09-08 11:26:26 -04002348void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002349 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002350 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07002351 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002352 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002353 const AutoTexture autoCleanup(texture);
2354
2355 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2356 texture->setFilter(GL_LINEAR, true);
2357
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002358 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2359 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002360 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002361
Tom Hudson107843d2014-09-08 11:26:26 -04002362 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002363}
2364
Tom Hudson107843d2014-09-08 11:26:26 -04002365void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002366 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002367 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002368 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002369 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002370 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002371 }
2372
Chris Craik922d3a72015-02-13 17:47:21 -08002373 if (USE_GLOPS) {
Chris Craik117bdbc2015-02-05 10:12:38 -08002374 Glop glop;
2375 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
2376 bool fudgeOffset = displayFlags & kVertexBuffer_Offset;
2377 bool shadowInterp = displayFlags & kVertexBuffer_ShadowInterp;
2378 aBuilder.setMeshVertexBuffer(vertexBuffer, shadowInterp)
Chris Craik0519c812015-02-11 13:17:06 -08002379 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craikf27133d2015-02-19 09:51:53 -08002380 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), fudgeOffset)
Chris Craik117bdbc2015-02-05 10:12:38 -08002381 .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds())
Chris Craik0519c812015-02-11 13:17:06 -08002382 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik117bdbc2015-02-05 10:12:38 -08002383 .build();
2384 renderGlop(glop);
2385 return;
2386 }
2387
Chris Craik117bdbc2015-02-05 10:12:38 -08002388 const VertexBuffer::MeshFeatureFlags meshFeatureFlags = vertexBuffer.getMeshFeatureFlags();
Chris Craik0d5ac952014-07-15 13:01:02 -07002389 Rect bounds(vertexBuffer.getBounds());
2390 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002391 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2392
Chris Craikcb4d6002012-09-25 12:00:29 -07002393 int color = paint->getColor();
Chris Craik117bdbc2015-02-05 10:12:38 -08002394 bool isAA = meshFeatureFlags & VertexBuffer::kAlpha;
Chris Craikcb4d6002012-09-25 12:00:29 -07002395
Chris Craik710f46d2012-09-17 17:25:49 -07002396 setupDraw();
2397 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002398 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Chris Craik117bdbc2015-02-05 10:12:38 -08002399 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002400 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002401 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002402 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002403 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002404 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2405 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002406 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002407 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002408 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002409
ztenghui55bfb4e2013-12-03 10:38:55 -08002410 const void* vertices = vertexBuffer.getBuffer();
Chris Craik96a5c4c2015-01-27 15:46:35 -08002411 mRenderState.meshState().unbindMeshBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08002412 mRenderState.meshState().bindPositionVertexPointer(true, vertices,
2413 isAA ? kAlphaVertexStride : kVertexStride);
Chris Craik96a5c4c2015-01-27 15:46:35 -08002414 mRenderState.meshState().resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002415
Chris Craik710f46d2012-09-17 17:25:49 -07002416 int alphaSlot = -1;
2417 if (isAA) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002418 void* alphaCoords = ((GLbyte*) vertices) + kVertexAlphaOffset;
Chris Craik6c15ffa2015-02-02 13:50:55 -08002419 alphaSlot = mCaches.program().getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002420 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002421 glEnableVertexAttribArray(alphaSlot);
Chris Craik96a5c4c2015-01-27 15:46:35 -08002422 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, kAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002423 }
Romain Guy04299382012-07-18 17:15:41 -07002424
Chris Craik117bdbc2015-02-05 10:12:38 -08002425 if (meshFeatureFlags & VertexBuffer::kIndices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002426 mRenderState.meshState().unbindIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002427 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(),
2428 GL_UNSIGNED_SHORT, vertexBuffer.getIndices());
Chris Craik117bdbc2015-02-05 10:12:38 -08002429 } else {
2430 mRenderState.meshState().unbindIndicesBuffer();
2431 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
ztenghui63d41ab2014-02-14 13:13:41 -08002432 }
Romain Guy04299382012-07-18 17:15:41 -07002433
Chris Craik710f46d2012-09-17 17:25:49 -07002434 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002435 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002436 }
Chris Craik65cd6122012-12-10 17:56:27 -08002437
Tom Hudson107843d2014-09-08 11:26:26 -04002438 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002439}
2440
2441/**
Chris Craik65cd6122012-12-10 17:56:27 -08002442 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2443 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2444 * screen space in all directions. However, instead of using a fragment shader to compute the
2445 * translucency of the color from its position, we simply use a varying parameter to define how far
2446 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2447 *
2448 * Doesn't yet support joins, caps, or path effects.
2449 */
Tom Hudson107843d2014-09-08 11:26:26 -04002450void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002451 VertexBuffer vertexBuffer;
2452 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002453 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002454 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002455}
2456
2457/**
2458 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2459 * and additional geometry for defining an alpha slope perimeter.
2460 *
2461 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2462 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2463 * in-shader alpha region, but found it to be taxing on some GPUs.
2464 *
2465 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2466 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002467 */
Tom Hudson107843d2014-09-08 11:26:26 -04002468void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002469 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002470
Chris Craik65cd6122012-12-10 17:56:27 -08002471 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002472
Chris Craik65cd6122012-12-10 17:56:27 -08002473 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002474 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2475 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002476
Chris Craik05f3d6e2014-06-02 16:27:04 -07002477 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002478 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002479 }
2480
Chris Craikbf759452014-08-11 16:00:44 -07002481 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002482 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002483}
2484
Tom Hudson107843d2014-09-08 11:26:26 -04002485void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002486 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002487
Chris Craik6d29c8d2013-05-08 18:35:44 -07002488 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002489
Chris Craik6d29c8d2013-05-08 18:35:44 -07002490 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002491 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002492
Chris Craik05f3d6e2014-06-02 16:27:04 -07002493 const Rect& bounds = buffer.getBounds();
2494 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002495 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002496 }
2497
Chris Craikbf759452014-08-11 16:00:44 -07002498 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002499 drawVertexBuffer(buffer, paint, displayFlags);
2500
2501 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002502}
2503
Tom Hudson107843d2014-09-08 11:26:26 -04002504void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002505 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002506 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002507
Rob Tsuk487a92c2015-01-06 13:22:54 -08002508 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002509 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002510
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002511 SkPaint paint;
2512 paint.setColor(color);
2513 paint.setXfermodeMode(mode);
2514
2515 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002516
Tom Hudson107843d2014-09-08 11:26:26 -04002517 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002518}
Romain Guy9d5316e2010-06-24 19:30:36 -07002519
Chris Craik30036092015-02-12 10:41:39 -08002520void OpenGLRenderer::drawShape(float left, float top, PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002521 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002522 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002523 const AutoTexture autoCleanup(texture);
2524
2525 const float x = left + texture->left - texture->offset;
2526 const float y = top + texture->top - texture->offset;
2527
2528 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002529
Tom Hudson107843d2014-09-08 11:26:26 -04002530 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002531}
2532
Tom Hudson107843d2014-09-08 11:26:26 -04002533void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002534 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002535 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002536 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002537 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002538 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002539 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002540
Chris Craike84a2082014-12-22 14:28:49 -08002541 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002542 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08002543 PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002544 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002545 drawShape(left, top, texture, p);
2546 } else {
2547 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2548 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2549 drawVertexBuffer(left, top, *vertexBuffer, 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::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002554 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002555 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002556 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002557 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002558 }
Chris Craike84a2082014-12-22 14:28:49 -08002559 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002560 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08002561 PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002562 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002563 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002564 SkPath path;
2565 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2566 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2567 } else {
2568 path.addCircle(x, y, radius);
2569 }
2570 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002571 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002572}
Romain Guy01d58e42011-01-19 21:54:02 -08002573
Tom Hudson107843d2014-09-08 11:26:26 -04002574void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002575 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002576 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002577 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002578 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002579 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002580 }
Romain Guy01d58e42011-01-19 21:54:02 -08002581
Chris Craike84a2082014-12-22 14:28:49 -08002582 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002583 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08002584 PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002585 drawShape(left, top, texture, p);
2586 } else {
2587 SkPath path;
2588 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2589 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2590 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2591 }
2592 path.addOval(rect);
2593 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002594 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002595}
2596
Tom Hudson107843d2014-09-08 11:26:26 -04002597void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002598 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002599 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002600 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002601 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002602 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002603 }
2604
Chris Craik780c1282012-10-04 14:10:49 -07002605 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08002606 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002607 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08002608 PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002609 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002610 drawShape(left, top, texture, p);
2611 return;
Chris Craik780c1282012-10-04 14:10:49 -07002612 }
Chris Craik780c1282012-10-04 14:10:49 -07002613 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2614 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2615 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2616 }
2617
2618 SkPath path;
2619 if (useCenter) {
2620 path.moveTo(rect.centerX(), rect.centerY());
2621 }
2622 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2623 if (useCenter) {
2624 path.close();
2625 }
Tom Hudson107843d2014-09-08 11:26:26 -04002626 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002627}
2628
Romain Guycf8675e2012-10-02 12:32:25 -07002629// See SkPaintDefaults.h
2630#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2631
Tom Hudson107843d2014-09-08 11:26:26 -04002632void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002633 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002634 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002635 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002636 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002637 return;
Romain Guy6926c722010-07-12 20:20:03 -07002638 }
2639
Chris Craik710f46d2012-09-17 17:25:49 -07002640 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002641 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08002642 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07002643 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002644 mCaches.textureState().activateTexture(0);
Chris Craik30036092015-02-12 10:41:39 -08002645 PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002646 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002647 drawShape(left, top, texture, p);
2648 } else {
2649 SkPath path;
2650 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2651 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2652 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2653 }
2654 path.addRect(rect);
2655 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002656 }
Chet Haase858aa932011-05-12 09:06:00 -07002657 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002658 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2659 SkPath path;
2660 path.addRect(left, top, right, bottom);
2661 drawConvexPath(path, p);
2662 } else {
2663 drawColorRect(left, top, right, bottom, p);
2664
2665 mDirty = true;
2666 }
Chet Haase858aa932011-05-12 09:06:00 -07002667 }
Romain Guyc7d53492010-06-25 13:41:57 -07002668}
Romain Guy9d5316e2010-06-24 19:30:36 -07002669
Chris Craikd218a922014-01-02 17:13:34 -08002670void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2671 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002672 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002673 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07002674
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002675 TextShadow textShadow;
2676 if (!getTextShadow(paint, &textShadow)) {
2677 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2678 }
2679
Raph Levien416a8472012-07-19 22:48:17 -07002680 // NOTE: The drop shadow will not perform gamma correction
2681 // if shader-based correction is enabled
2682 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Chris Craik2bb8f562015-02-17 16:42:02 -08002683 ShadowTexture* texture = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002684 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002685 // If the drop shadow exceeds the max texture size or couldn't be
2686 // allocated, skip drawing
Chris Craik2bb8f562015-02-17 16:42:02 -08002687 if (!texture) return;
2688 const AutoTexture autoCleanup(texture);
Raph Levien416a8472012-07-19 22:48:17 -07002689
Chris Craik2bb8f562015-02-17 16:42:02 -08002690 const float sx = x - texture->left + textShadow.dx;
2691 const float sy = y - texture->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002692
Chris Craik2bb8f562015-02-17 16:42:02 -08002693 if (USE_GLOPS) {
2694 Glop glop;
2695 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
Chris Craikf27133d2015-02-19 09:51:53 -08002696 aBuilder.setMeshTexturedUnitQuad(nullptr)
Chris Craik2bb8f562015-02-17 16:42:02 -08002697 .setFillShadowTexturePaint(*texture, textShadow.color, *paint, currentSnapshot()->alpha)
Chris Craikf27133d2015-02-19 09:51:53 -08002698 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
Chris Craik2bb8f562015-02-17 16:42:02 -08002699 .setModelViewMapUnitToRect(Rect(sx, sy, sx + texture->width, sy + texture->height))
2700 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
2701 .build();
2702 renderGlop(glop);
2703 return;
2704 }
2705
2706 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * currentSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002707 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002708 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002709 }
2710
2711 setupDraw();
2712 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002713 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002714 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002715 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002716 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002717 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002718 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
Chris Craik2bb8f562015-02-17 16:42:02 -08002719 sx, sy, sx + texture->width, sy + texture->height);
2720 setupDrawTexture(texture->id);
Raph Levien416a8472012-07-19 22:48:17 -07002721 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002722 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002723 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08002724 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Raph Levien416a8472012-07-19 22:48:17 -07002725
Chris Craik117bdbc2015-02-05 10:12:38 -08002726 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Raph Levien416a8472012-07-19 22:48:17 -07002727}
2728
Romain Guy768bffc2013-02-27 13:50:45 -08002729bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002730 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05002731 return MathUtils::isZero(alpha)
2732 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002733}
2734
Tom Hudson107843d2014-09-08 11:26:26 -04002735void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002736 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002737 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002738 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002739 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002740
Romain Guy671d6cf2012-01-18 12:39:17 -08002741 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002742 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002743 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002744 }
2745
Chris Craik65fe5ee2015-01-26 18:06:29 -08002746 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002747
Romain Guy671d6cf2012-01-18 12:39:17 -08002748 float x = 0.0f;
2749 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002750 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002751 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002752 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2753 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002754 }
2755
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002756 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002757 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002758
2759 int alpha;
2760 SkXfermode::Mode mode;
2761 getAlphaAndMode(paint, &alpha, &mode);
2762
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002763 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002764 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002765 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002766 }
2767
Romain Guy671d6cf2012-01-18 12:39:17 -08002768 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002769 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002770 if (pureTranslate && !linearFilter) {
2771 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2772 }
Romain Guy257ae352013-03-20 16:31:12 -07002773 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002774
Rob Tsuk487a92c2015-01-06 13:22:54 -08002775 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002776 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2777
Romain Guy211370f2012-02-01 16:10:55 -08002778 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002779
Victoria Lease1e546812013-06-25 14:25:17 -07002780 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002781 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002782 positions, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002783 if (hasActiveLayer) {
2784 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002785 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002786 }
2787 dirtyLayerUnchecked(bounds, getRegion());
2788 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002789 }
Chet Haase48659092012-05-31 15:21:51 -07002790
Tom Hudson107843d2014-09-08 11:26:26 -04002791 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002792}
2793
Chris Craik59744b72014-07-01 17:56:52 -07002794bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002795 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002796 outMatrix->setIdentity();
2797 return false;
2798 } else if (CC_UNLIKELY(transform.isPerspective())) {
2799 outMatrix->setIdentity();
2800 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002801 }
Chris Craik59744b72014-07-01 17:56:52 -07002802
2803 /**
Chris Craika736cd92014-08-04 13:18:38 -07002804 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2805 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002806 */
2807 float sx, sy;
2808 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002809 outMatrix->setScale(
2810 roundf(fmaxf(1.0f, sx)),
2811 roundf(fmaxf(1.0f, sy)));
2812 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002813}
2814
Tom Hudson984162f2014-10-10 13:38:16 -04002815int OpenGLRenderer::getSaveCount() const {
2816 return mState.getSaveCount();
2817}
2818
2819int OpenGLRenderer::save(int flags) {
2820 return mState.save(flags);
2821}
2822
2823void OpenGLRenderer::restore() {
2824 return mState.restore();
2825}
2826
2827void OpenGLRenderer::restoreToCount(int saveCount) {
2828 return mState.restoreToCount(saveCount);
2829}
2830
2831void OpenGLRenderer::translate(float dx, float dy, float dz) {
2832 return mState.translate(dx, dy, dz);
2833}
2834
2835void OpenGLRenderer::rotate(float degrees) {
2836 return mState.rotate(degrees);
2837}
2838
2839void OpenGLRenderer::scale(float sx, float sy) {
2840 return mState.scale(sx, sy);
2841}
2842
2843void OpenGLRenderer::skew(float sx, float sy) {
2844 return mState.skew(sx, sy);
2845}
2846
2847void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2848 mState.setMatrix(matrix);
2849}
2850
2851void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2852 mState.concatMatrix(matrix);
2853}
2854
2855bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2856 return mState.clipRect(left, top, right, bottom, op);
2857}
2858
2859bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2860 return mState.clipPath(path, op);
2861}
2862
2863bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2864 return mState.clipRegion(region, op);
2865}
2866
2867void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2868 mState.setClippingOutline(allocator, outline);
2869}
2870
2871void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2872 const Rect& rect, float radius, bool highPriority) {
2873 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2874}
2875
Tom Hudson107843d2014-09-08 11:26:26 -04002876void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002877 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002878 DrawOpMode drawOpMode) {
2879
Chris Craik527a3aa2013-03-04 10:19:31 -08002880 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002881 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2882 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002883 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002884 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002885 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002886 }
Romain Guycac5fd32011-12-01 20:08:50 -08002887 }
2888
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002889 const float oldX = x;
2890 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002891
Chris Craikd6b65f62014-01-01 14:45:21 -08002892 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002893 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002894
Romain Guy211370f2012-02-01 16:10:55 -08002895 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002896 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2897 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002898 }
2899
Romain Guy86568192010-12-14 15:55:39 -08002900 int alpha;
2901 SkXfermode::Mode mode;
2902 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002903
Romain Guyc74f45a2013-02-26 19:10:14 -08002904 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2905
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002906 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002907 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002908 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002909 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002910 }
2911
Romain Guy19d4dd82013-03-04 11:14:26 -08002912 const bool hasActiveLayer = hasLayer();
2913
Romain Guy624234f2013-03-05 16:43:31 -08002914 // We only pass a partial transform to the font renderer. That partial
2915 // matrix defines how glyphs are rasterized. Typically we want glyphs
2916 // to be rasterized at their final size on screen, which means the partial
2917 // matrix needs to take the scale factor into account.
2918 // When a partial matrix is used to transform glyphs during rasterization,
2919 // the mesh is generated with the inverse transform (in the case of scale,
2920 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2921 // apply the full transform matrix at draw time in the vertex shader.
2922 // Applying the full matrix in the shader is the easiest way to handle
2923 // rotation and perspective and allows us to always generated quads in the
2924 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002925 SkMatrix fontTransform;
2926 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2927 || fabs(y - (int) y) > 0.0f
2928 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002929 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002930 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002931
Romain Guy624234f2013-03-05 16:43:31 -08002932 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08002933 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002934 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 -07002935
Raph Levien996e57c2012-07-23 15:22:52 -07002936 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002937 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002938
2939 // don't call issuedrawcommand, do it at end of batch
2940 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002941 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002942 SkPaint paintCopy(*paint);
2943 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2944 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002945 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002946 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002947 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002948 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002949 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002950
Chris Craik527a3aa2013-03-04 10:19:31 -08002951 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002952 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002953 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002954 }
Chris Craik41541822013-05-03 16:35:54 -07002955 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002956 }
Romain Guy694b5192010-07-21 21:33:20 -07002957
Chris Craike63f7c622013-10-17 10:30:55 -07002958 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002959
Tom Hudson107843d2014-09-08 11:26:26 -04002960 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002961}
2962
Tom Hudson107843d2014-09-08 11:26:26 -04002963void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002964 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002965 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002966 return;
Romain Guy03d58522012-02-24 17:54:07 -08002967 }
2968
Chris Craik39a908c2013-06-13 14:39:01 -07002969 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002970 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002971
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002972 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002973 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002974 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002975
2976 int alpha;
2977 SkXfermode::Mode mode;
2978 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002979 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002980
Tom Hudson984162f2014-10-10 13:38:16 -04002981 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002982 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 -08002983
Romain Guy97771732012-02-28 18:17:02 -08002984 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002985
2986 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chris Craike84a2082014-12-22 14:28:49 -08002987 hOffset, vOffset, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002988 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002989 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002990 dirtyLayerUnchecked(bounds, getRegion());
2991 }
Romain Guy97771732012-02-28 18:17:02 -08002992 }
Chet Haase48659092012-05-31 15:21:51 -07002993
Tom Hudson107843d2014-09-08 11:26:26 -04002994 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002995}
2996
Tom Hudson107843d2014-09-08 11:26:26 -04002997void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002998 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002999
Chris Craik44eb2c02015-01-29 09:45:09 -08003000 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003001
Chris Craik30036092015-02-12 10:41:39 -08003002 PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04003003 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07003004 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003005
Romain Guy8b55f372010-08-18 17:10:07 -07003006 const float x = texture->left - texture->offset;
3007 const float y = texture->top - texture->offset;
3008
Romain Guy01d58e42011-01-19 21:54:02 -08003009 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04003010 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07003011}
3012
Tom Hudson107843d2014-09-08 11:26:26 -04003013void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003014 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04003015 return;
Romain Guy35643dd2012-09-18 15:40:58 -07003016 }
3017
Chris Craike84a2082014-12-22 14:28:49 -08003018 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07003019 if (layer->isTextureLayer()) {
3020 transform = &layer->getTransform();
3021 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07003022 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08003023 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003024 }
3025 }
3026
Chris Craik39a908c2013-06-13 14:39:01 -07003027 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08003028 const bool rejected = mState.calculateQuickRejectForScissor(
3029 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3030 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07003031
3032 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003033 if (transform && !transform->isIdentity()) {
3034 restore();
3035 }
Tom Hudson107843d2014-09-08 11:26:26 -04003036 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08003037 }
3038
Chris Craik62d307c2014-07-29 10:35:13 -07003039 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
3040 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
3041
Romain Guy5bb3c732012-11-29 17:52:58 -08003042 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003043
Chris Craik65fe5ee2015-01-26 18:06:29 -08003044 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08003045 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003046
Romain Guy211370f2012-02-01 16:10:55 -08003047 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08003048 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003049 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3050 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003051 } else if (layer->mesh) {
Chris Craikf27133d2015-02-19 09:51:53 -08003052 if (USE_GLOPS) {
3053 Glop glop;
3054 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
3055 aBuilder.setMeshTexturedIndexedQuads(layer->mesh, layer->meshElementCount)
3056 .setFillLayer(layer->getTexture(), layer->getColorFilter(), getLayerAlpha(layer), layer->getMode())
3057 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
3058 .setModelViewOffsetRectSnap(x, y, Rect(0, 0, layer->layer.getWidth(), layer->layer.getHeight()))
3059 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
3060 .build();
3061 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, renderGlop(glop));
Romain Guy9ace8f52011-07-07 20:50:11 -07003062 } else {
Chris Craikf27133d2015-02-19 09:51:53 -08003063 const float a = getLayerAlpha(layer);
3064 setupDraw();
3065 setupDrawWithTexture();
3066 setupDrawColor(a, a, a, a);
3067 setupDrawColorFilter(layer->getColorFilter());
3068 setupDrawBlending(layer);
3069 setupDrawProgram();
3070 setupDrawPureColorUniforms();
3071 setupDrawColorFilterUniforms(layer->getColorFilter());
3072 setupDrawTexture(layer->getTextureId());
3073 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3074 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3075 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
3076
3077 layer->setFilter(GL_NEAREST);
3078 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
3079 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
3080 } else {
3081 layer->setFilter(GL_LINEAR);
3082 setupDrawModelView(kModelViewMode_Translate, false, x, y,
3083 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3084 }
3085
3086
3087 TextureVertex* mesh = &layer->mesh[0];
3088 GLsizei elementsCount = layer->meshElementCount;
3089
3090
3091 while (elementsCount > 0) {
3092 GLsizei drawCount = MathUtils::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
3093
3094 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
3095 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3096 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr));
3097
3098 elementsCount -= drawCount;
3099 // Though there are 4 vertices in a quad, we use 6 indices per
3100 // quad to draw with GL_TRIANGLES
3101 mesh += (drawCount / 6) * 4;
3102 }
Romain Guy9ace8f52011-07-07 20:50:11 -07003103 }
Romain Guy3a3133d2011-02-01 22:59:58 -08003104#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003105 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003106#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003107 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003108
Romain Guy5bb3c732012-11-29 17:52:58 -08003109 if (layer->debugDrawUpdate) {
3110 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003111
3112 SkPaint paint;
3113 paint.setColor(0x7f00ff00);
3114 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003115 }
Romain Guyf219da52011-01-16 12:54:25 -08003116 }
Chris Craik34416ea2013-04-15 16:08:28 -07003117 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003118
Romain Guyb2e2f242012-10-17 18:18:35 -07003119 if (transform && !transform->isIdentity()) {
3120 restore();
3121 }
3122
Tom Hudson107843d2014-09-08 11:26:26 -04003123 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003124}
3125
Romain Guy6926c722010-07-12 20:20:03 -07003126///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003127// Draw filters
3128///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003129void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3130 // We should never get here since we apply the draw filter when stashing
3131 // the paints in the DisplayList.
3132 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003133}
3134
3135///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003136// Drawing implementation
3137///////////////////////////////////////////////////////////////////////////////
3138
Chris Craikd218a922014-01-02 17:13:34 -08003139Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003140 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003141 if (!texture) {
3142 return mCaches.textureCache.get(bitmap);
3143 }
3144 return texture;
3145}
3146
Chris Craik2bb8f562015-02-17 16:42:02 -08003147void OpenGLRenderer::drawPathTexture(PathTexture* texture, float x, float y,
3148 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003149 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003150 return;
3151 }
3152
Chris Craik922d3a72015-02-13 17:47:21 -08003153 if (USE_GLOPS) {
Chris Craik30036092015-02-12 10:41:39 -08003154 Glop glop;
3155 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
Chris Craikf27133d2015-02-19 09:51:53 -08003156 aBuilder.setMeshTexturedUnitQuad(nullptr)
Chris Craik30036092015-02-12 10:41:39 -08003157 .setFillPathTexturePaint(*texture, *paint, currentSnapshot()->alpha)
Chris Craikf27133d2015-02-19 09:51:53 -08003158 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), false)
Chris Craik30036092015-02-12 10:41:39 -08003159 .setModelViewMapUnitToRect(Rect(x, y, x + texture->width, y + texture->height))
3160 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
3161 .build();
3162 renderGlop(glop);
3163 return;
3164 }
3165
3166
Romain Guy01d58e42011-01-19 21:54:02 -08003167 int alpha;
3168 SkXfermode::Mode mode;
3169 getAlphaAndMode(paint, &alpha, &mode);
3170
3171 setupDraw();
3172 setupDrawWithTexture(true);
3173 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003174 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003175 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003176 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003177 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003178 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3179 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003180 setupDrawTexture(texture->id);
3181 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003182 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003183 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08003184 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Romain Guy01d58e42011-01-19 21:54:02 -08003185
Chris Craik117bdbc2015-02-05 10:12:38 -08003186 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003187}
3188
Romain Guyf607bdc2010-09-10 19:20:06 -07003189// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003190#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3191#define kStdUnderline_Offset (1.0f / 9.0f)
3192#define kStdUnderline_Thickness (1.0f / 18.0f)
3193
Chris Craikd218a922014-01-02 17:13:34 -08003194void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3195 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003196 // Handle underline and strike-through
3197 uint32_t flags = paint->getFlags();
3198 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003199 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003200
Romain Guy211370f2012-02-01 16:10:55 -08003201 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003202 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003203 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003204
Raph Levien8b4072d2012-07-30 15:50:00 -07003205 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003206 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003207
Romain Guyf6834472011-01-23 13:32:12 -08003208 int linesCount = 0;
3209 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3210 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3211
3212 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003213 float points[pointsCount];
3214 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003215
3216 if (flags & SkPaint::kUnderlineText_Flag) {
3217 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003218 points[currentPoint++] = left;
3219 points[currentPoint++] = top;
3220 points[currentPoint++] = left + underlineWidth;
3221 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003222 }
3223
3224 if (flags & SkPaint::kStrikeThruText_Flag) {
3225 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003226 points[currentPoint++] = left;
3227 points[currentPoint++] = top;
3228 points[currentPoint++] = left + underlineWidth;
3229 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003230 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003231
Romain Guy726aeba2011-06-01 14:52:00 -07003232 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003233
Romain Guy726aeba2011-06-01 14:52:00 -07003234 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003235 }
3236 }
3237}
3238
Tom Hudson107843d2014-09-08 11:26:26 -04003239void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003240 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003241 return;
Romain Guy672433d2013-01-04 19:05:13 -08003242 }
3243
Tom Hudson107843d2014-09-08 11:26:26 -04003244 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003245}
3246
Tom Hudson107843d2014-09-08 11:26:26 -04003247void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003248 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003249 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003250
Chris Craik15a07a22014-01-26 13:43:53 -08003251 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08003252 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07003253
3254 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003255 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003256
ztenghui14a4e352014-08-13 10:44:39 -07003257 // The caller has made sure casterAlpha > 0.
3258 float ambientShadowAlpha = mAmbientShadowAlpha;
3259 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3260 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3261 }
3262 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3263 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003264 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003265 }
ztenghui7b4516e2014-01-07 10:42:55 -08003266
ztenghui14a4e352014-08-13 10:44:39 -07003267 float spotShadowAlpha = mSpotShadowAlpha;
3268 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3269 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3270 }
3271 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3272 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003273 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003274 }
ztenghui7b4516e2014-01-07 10:42:55 -08003275
Tom Hudson107843d2014-09-08 11:26:26 -04003276 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003277}
3278
Tom Hudson107843d2014-09-08 11:26:26 -04003279void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003280 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003281 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003282 return;
Romain Guy3b753822013-03-05 10:27:35 -08003283 }
Romain Guy735738c2012-12-03 12:34:51 -08003284
Romain Guy672433d2013-01-04 19:05:13 -08003285 float left = FLT_MAX;
3286 float top = FLT_MAX;
3287 float right = FLT_MIN;
3288 float bottom = FLT_MIN;
3289
Romain Guy448455f2013-07-22 13:57:50 -07003290 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003291 Vertex* vertex = mesh;
3292
Chris Craik2af46352012-11-26 18:30:17 -08003293 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003294 float l = rects[index + 0];
3295 float t = rects[index + 1];
3296 float r = rects[index + 2];
3297 float b = rects[index + 3];
3298
Romain Guy3b753822013-03-05 10:27:35 -08003299 Vertex::set(vertex++, l, t);
3300 Vertex::set(vertex++, r, t);
3301 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003302 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003303
Romain Guy3b753822013-03-05 10:27:35 -08003304 left = fminf(left, l);
3305 top = fminf(top, t);
3306 right = fmaxf(right, r);
3307 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003308 }
3309
Chris Craikf0a59072013-11-19 18:00:46 -08003310 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003311 return;
Romain Guya362c692013-02-04 13:50:16 -08003312 }
Romain Guy672433d2013-01-04 19:05:13 -08003313
Chris Craik922d3a72015-02-13 17:47:21 -08003314 if (USE_GLOPS) {
Chris Craik2ab95d72015-02-06 15:25:51 -08003315 const Matrix4& transform = ignoreTransform ? Matrix4::identity() : *currentTransform();
3316 Glop glop;
3317 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
3318 aBuilder.setMeshIndexedQuads(&mesh[0], count / 4)
Chris Craik0519c812015-02-11 13:17:06 -08003319 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craikf27133d2015-02-19 09:51:53 -08003320 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
Chris Craik2ab95d72015-02-06 15:25:51 -08003321 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
Chris Craik0519c812015-02-11 13:17:06 -08003322 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik2ab95d72015-02-06 15:25:51 -08003323 .build();
3324 renderGlop(glop);
3325 return;
3326 }
3327
3328 int color = paint->getColor();
3329 // If a shader is set, preserve only the alpha
3330 if (getShader(paint)) {
3331 color |= 0x00ffffff;
3332 }
3333
Romain Guy672433d2013-01-04 19:05:13 -08003334 setupDraw();
3335 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003336 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003337 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003338 setupDrawColorFilter(getColorFilter(paint));
3339 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003340 setupDrawProgram();
3341 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003342 setupDrawModelView(kModelViewMode_Translate, false,
3343 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003344 setupDrawColorUniforms(getShader(paint));
3345 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003346 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003347
Romain Guy8ce00302013-01-15 18:51:42 -08003348 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003349 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003350 }
3351
Chris Craik4063a0e2013-11-15 16:06:56 -08003352 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003353
Tom Hudson107843d2014-09-08 11:26:26 -04003354 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003355}
3356
Romain Guy026c5e162010-06-28 17:12:22 -07003357void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003358 const SkPaint* paint, bool ignoreTransform) {
Chris Craik117bdbc2015-02-05 10:12:38 -08003359
Chris Craik922d3a72015-02-13 17:47:21 -08003360 if (USE_GLOPS) {
Chris Craik117bdbc2015-02-05 10:12:38 -08003361 const Matrix4& transform = ignoreTransform ? Matrix4::identity() : *currentTransform();
3362 Glop glop;
3363 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
3364 aBuilder.setMeshUnitQuad()
Chris Craik0519c812015-02-11 13:17:06 -08003365 .setFillPaint(*paint, currentSnapshot()->alpha)
Chris Craikf27133d2015-02-19 09:51:53 -08003366 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
Chris Craik117bdbc2015-02-05 10:12:38 -08003367 .setModelViewMapUnitToRect(Rect(left, top, right, bottom))
Chris Craik0519c812015-02-11 13:17:06 -08003368 .setRoundRectClipState(currentSnapshot()->roundRectClipState)
Chris Craik117bdbc2015-02-05 10:12:38 -08003369 .build();
3370 renderGlop(glop);
3371 return;
3372 }
3373
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003374 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003375 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003376 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003377 color |= 0x00ffffff;
3378 }
3379
Romain Guy70ca14e2010-12-13 18:24:33 -08003380 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003381 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003382 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003383 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003384 setupDrawColorFilter(getColorFilter(paint));
3385 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003386 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003387 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3388 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003389 setupDrawColorUniforms(getShader(paint));
3390 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003391 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003392 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003393
Chris Craik117bdbc2015-02-05 10:12:38 -08003394 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Romain Guyc95c8d62010-09-17 15:31:32 -07003395}
3396
Chris Craik0519c812015-02-11 13:17:06 -08003397void OpenGLRenderer::drawTextureRect(Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003398 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003399
Chris Craike84a2082014-12-22 14:28:49 -08003400 GLvoid* vertices = (GLvoid*) nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -08003401 GLvoid* texCoords = (GLvoid*) kMeshTextureOffset;
Romain Guy3b748a42013-04-17 18:54:38 -07003402
3403 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003404 vertices = &mMeshVertices[0].x;
3405 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003406
3407 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3408 texture->uvMapper->map(uvs);
3409
3410 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3411 }
3412
Chris Craikd6b65f62014-01-01 14:45:21 -08003413 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
Chris Craik0519c812015-02-11 13:17:06 -08003414 const float x = (int) floorf(currentTransform()->getTranslateX() + 0.5f);
3415 const float y = (int) floorf(currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003416
Romain Guyd21b6e12011-11-30 20:21:23 -08003417 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003418 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003419 paint, texture->blend, vertices, texCoords,
Chris Craik117bdbc2015-02-05 10:12:38 -08003420 GL_TRIANGLE_STRIP, kUnitQuadCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003421 } else {
Chris Craik0519c812015-02-11 13:17:06 -08003422 texture->setFilter(PaintUtils::getFilter(paint), true);
3423 drawTextureMesh(0, 0, texture->width, texture->height, texture->id, paint,
Chris Craik117bdbc2015-02-05 10:12:38 -08003424 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, kUnitQuadCount);
Romain Guy3b748a42013-04-17 18:54:38 -07003425 }
3426
3427 if (texture->uvMapper) {
3428 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003429 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003430}
3431
Romain Guyf7f93552010-07-08 19:17:03 -07003432void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003433 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003434 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003435 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3436 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003437
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003438 int a;
3439 SkXfermode::Mode mode;
3440 getAlphaAndMode(paint, &a, &mode);
3441 const float alpha = a / 255.0f;
3442
Romain Guy746b7402010-10-26 16:27:31 -07003443 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003444 setupDrawWithTexture();
3445 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003446 setupDrawColorFilter(getColorFilter(paint));
3447 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003448 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003449 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003450 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003451 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003452 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003453 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003454 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003455
Romain Guy6820ac82010-09-15 18:11:50 -07003456 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003457}
3458
Romain Guy3b748a42013-04-17 18:54:38 -07003459void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003460 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003461 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003462 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3463 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003464
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003465 int a;
3466 SkXfermode::Mode mode;
3467 getAlphaAndMode(paint, &a, &mode);
3468 const float alpha = a / 255.0f;
3469
Romain Guy3b748a42013-04-17 18:54:38 -07003470 setupDraw();
3471 setupDrawWithTexture();
3472 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003473 setupDrawColorFilter(getColorFilter(paint));
3474 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003475 setupDrawProgram();
3476 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003477 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003478 setupDrawTexture(texture);
3479 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003480 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003481 setupDrawMeshIndices(vertices, texCoords, vbo);
3482
Chris Craike84a2082014-12-22 14:28:49 -08003483 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy3b748a42013-04-17 18:54:38 -07003484}
3485
Romain Guy886b2752013-01-04 12:26:18 -08003486void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003487 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003488 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003489 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003490
Chris Craike84a2082014-12-22 14:28:49 -08003491 int color = paint != nullptr ? paint->getColor() : 0;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003492 int alpha;
3493 SkXfermode::Mode mode;
3494 getAlphaAndMode(paint, &alpha, &mode);
3495
Romain Guy886b2752013-01-04 12:26:18 -08003496 setupDraw();
3497 setupDrawWithTexture(true);
Chris Craike84a2082014-12-22 14:28:49 -08003498 if (paint != nullptr) {
Romain Guy886b2752013-01-04 12:26:18 -08003499 setupDrawAlpha8Color(color, alpha);
3500 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003501 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003502 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003503 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003504 setupDrawProgram();
3505 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003506 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003507 setupDrawTexture(texture);
3508 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003509 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003510 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003511 setupDrawMesh(vertices, texCoords);
3512
3513 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003514}
3515
Romain Guya5aed0d2010-09-09 14:42:43 -07003516void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003517 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003518
Chris Craik117bdbc2015-02-05 10:12:38 -08003519 if (currentSnapshot()->roundRectClipState != nullptr /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003520 blend = true;
3521 mDescription.hasRoundRectClip = true;
3522 }
3523 mSkipOutlineClip = true;
3524
Romain Guy82ba8142010-07-09 13:25:56 -07003525 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003526
Romain Guy82ba8142010-07-09 13:25:56 -07003527 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003528 // These blend modes are not supported by OpenGL directly and have
3529 // to be implemented using shaders. Since the shader will perform
3530 // the blending, turn blending off here
3531 // If the blend mode cannot be implemented using shaders, fall
3532 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003533 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Chris Craik117bdbc2015-02-05 10:12:38 -08003534 if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003535 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003536 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003537
Chris Craik44eb2c02015-01-29 09:45:09 -08003538 mRenderState.blend().disable();
Romain Guy82bc7a72012-01-03 14:13:39 -08003539 return;
3540 } else {
3541 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003542 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003543 }
Chris Craik44eb2c02015-01-29 09:45:09 -08003544 mRenderState.blend().enable(mode, swapSrcDst);
3545 } else {
3546 mRenderState.blend().disable();
Romain Guy82ba8142010-07-09 13:25:56 -07003547 }
Romain Guybd6b79b2010-06-26 00:13:53 -07003548}
3549
Romain Guy026c5e162010-06-28 17:12:22 -07003550void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003551 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003552 TextureVertex::setUV(v++, u1, v1);
3553 TextureVertex::setUV(v++, u2, v1);
3554 TextureVertex::setUV(v++, u1, v2);
3555 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003556}
3557
Chris Craike84a2082014-12-22 14:28:49 -08003558void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
3559 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003560 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003561 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3562 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003563 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003564 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003565 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003566}
3567
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003568float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003569 float alpha;
3570 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3571 alpha = mDrawModifiers.mOverrideLayerAlpha;
3572 } else {
3573 alpha = layer->getAlpha() / 255.0f;
3574 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003575 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003576}
3577
Romain Guy9d5316e2010-06-24 19:30:36 -07003578}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003579}; // namespace android