blob: c4622f6613b7fc73612547c82a9a828ac51a4ca7 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chris Craik65fe5ee2015-01-26 18:06:29 -080017#include "OpenGLRenderer.h"
18
19#include "DeferredDisplayList.h"
20#include "DisplayListRenderer.h"
21#include "Fence.h"
22#include "GammaFontRenderer.h"
Chris Craik03188872015-02-02 18:39:33 -080023#include "Glop.h"
24#include "GlopBuilder.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080025#include "Patch.h"
26#include "PathTessellator.h"
27#include "Properties.h"
28#include "RenderNode.h"
Chris Craik03188872015-02-02 18:39:33 -080029#include "renderstate/MeshState.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080030#include "renderstate/RenderState.h"
31#include "ShadowTessellator.h"
32#include "SkiaShader.h"
33#include "Vector.h"
34#include "VertexBuffer.h"
35#include "utils/GLUtils.h"
36#include "utils/PaintUtils.h"
37#include "utils/TraceUtils.h"
38
Romain Guye4d01122010-06-16 18:44:05 -070039#include <stdlib.h>
40#include <stdint.h>
41#include <sys/types.h>
42
Romain Guy5cbbce52010-06-27 22:59:20 -070043#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070044#include <SkColor.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040045#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070046#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070047
Romain Guye4d01122010-06-16 18:44:05 -070048#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070049#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070050
Romain Guy08aa2cb2011-03-17 11:06:57 -070051#include <private/hwui/DrawGlInfo.h>
52
Romain Guy5b3b3522010-10-27 18:57:51 -070053#include <ui/Rect.h>
54
Chris Craik62d307c2014-07-29 10:35:13 -070055#if DEBUG_DETAILED_EVENTS
56 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
57#else
58 #define EVENT_LOGD(...)
59#endif
60
Romain Guye4d01122010-06-16 18:44:05 -070061namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070062namespace uirenderer {
63
Chris Craik678625242014-02-28 12:26:34 -080064static GLenum getFilter(const SkPaint* paint) {
65 if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) {
66 return GL_LINEAR;
67 }
68 return GL_NEAREST;
69}
Romain Guyd21b6e12011-11-30 20:21:23 -080070
Romain Guy9d5316e2010-06-24 19:30:36 -070071///////////////////////////////////////////////////////////////////////////////
72// Globals
73///////////////////////////////////////////////////////////////////////////////
74
Romain Guyf607bdc2010-09-10 19:20:06 -070075
Romain Guyf6a11b82010-06-23 17:47:49 -070076///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -070077// Functions
78///////////////////////////////////////////////////////////////////////////////
79
80template<typename T>
81static inline T min(T a, T b) {
82 return a < b ? a : b;
83}
84
85///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -070086// Constructors/destructor
87///////////////////////////////////////////////////////////////////////////////
88
John Reck3b202512014-06-23 13:13:08 -070089OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -040090 : mState(*this)
Chris Craik058fc642014-07-23 18:19:28 -070091 , mCaches(Caches::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -070092 , mRenderState(renderState)
Chris Craik65fe5ee2015-01-26 18:06:29 -080093 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -070094 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -070095 , mSuppressTiling(false)
96 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -040097 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -070098 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -070099 , mLightRadius(FLT_MIN)
100 , mAmbientShadowAlpha(0)
101 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800102 // *set* draw modifiers to be 0
103 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700104 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700105
Chris Craik03188872015-02-02 18:39:33 -0800106 memcpy(mMeshVertices, kUnitQuadVertices, sizeof(kUnitQuadVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700107}
108
Romain Guy85bf02f2010-06-22 13:11:24 -0700109OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700110 // The context has already been destroyed at this point, do not call
111 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700112}
113
Romain Guy87e2f7572012-09-24 11:37:12 -0700114void OpenGLRenderer::initProperties() {
115 char property[PROPERTY_VALUE_MAX];
116 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
117 mScissorOptimizationDisabled = !strcasecmp(property, "true");
118 INIT_LOGD(" Scissor optimization %s",
119 mScissorOptimizationDisabled ? "disabled" : "enabled");
120 } else {
121 INIT_LOGD(" Scissor optimization enabled");
122 }
Romain Guy13631f32012-01-30 17:41:55 -0800123}
124
Chris Craik058fc642014-07-23 18:19:28 -0700125void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
126 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
127 mLightCenter = lightCenter;
128 mLightRadius = lightRadius;
129 mAmbientShadowAlpha = ambientShadowAlpha;
130 mSpotShadowAlpha = spotShadowAlpha;
131}
132
Romain Guy13631f32012-01-30 17:41:55 -0800133///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700134// Setup
135///////////////////////////////////////////////////////////////////////////////
136
Chris Craik797b95b2014-05-20 18:10:25 -0700137void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700138 glDisable(GL_DITHER);
139 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Chris Craik284b2432014-09-18 16:05:35 -0700140 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700141}
142
Romain Guy96885eb2013-03-26 15:05:58 -0700143void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800144 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800145 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400146 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700147 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700148 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700149}
150
Tom Hudson107843d2014-09-08 11:26:26 -0400151void OpenGLRenderer::startFrame() {
152 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700153 mFrameStarted = true;
154
Tom Hudson984162f2014-10-10 13:38:16 -0400155 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700156
Romain Guy96885eb2013-03-26 15:05:58 -0700157 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700158
Tom Hudson984162f2014-10-10 13:38:16 -0400159 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800160
Romain Guy54c1a642012-09-27 17:55:46 -0700161 // Functors break the tiling extension in pretty spectacular ways
162 // This ensures we don't use tiling when a functor is going to be
163 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700164 mSuppressTiling = mCaches.hasRegisteredFunctors()
165 || mFirstFrameAfterResize;
166 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700167
Chris Craikd6b65f62014-01-01 14:45:21 -0800168 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700169
Romain Guy7c450aa2012-09-21 19:15:00 -0700170 debugOverdraw(true, true);
171
Tom Hudson107843d2014-09-08 11:26:26 -0400172 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700173 mTilingClip.right, mTilingClip.bottom, mOpaque);
174}
175
Tom Hudson107843d2014-09-08 11:26:26 -0400176void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700177 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700178
Romain Guy96885eb2013-03-26 15:05:58 -0700179 setupFrameState(left, top, right, bottom, opaque);
180
181 // Layer renderers will start the frame immediately
182 // The framebuffer renderer will first defer the display list
183 // for each layer and wait until the first drawing command
184 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800185 if (currentSnapshot()->fbo == 0) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800186 mRenderState.blend().syncEnabled();
Romain Guy96885eb2013-03-26 15:05:58 -0700187 updateLayers();
188 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400189 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700190 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700191}
192
Romain Guydcfc8362013-01-03 13:08:57 -0800193void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
194 // If we know that we are going to redraw the entire framebuffer,
195 // perform a discard to let the driver know we don't need to preserve
196 // the back buffer for this frame.
Chris Craik117bdbc2015-02-05 10:12:38 -0800197 if (mCaches.extensions().hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400198 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
199 const bool isFbo = onGetTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800200 const GLenum attachments[] = {
201 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
202 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800203 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
204 }
205}
206
Tom Hudson107843d2014-09-08 11:26:26 -0400207void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700208 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800209 mRenderState.scissor().setEnabled(true);
210 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700211 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400212 mDirty = true;
213 return;
Romain Guyddf74372012-05-22 14:07:07 -0700214 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700215
Chris Craik65fe5ee2015-01-26 18:06:29 -0800216 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700217}
218
henry.uh_chen33f5a592014-07-02 19:36:56 +0800219void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700220 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800221 const Snapshot* snapshot = currentSnapshot();
222
Chris Craik14e51302013-12-30 15:32:54 -0800223 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800224 if (snapshot->flags & Snapshot::kFlagFboTarget) {
225 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700226 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700227
henry.uh_chen33f5a592014-07-02 19:36:56 +0800228 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800229 }
230}
231
henry.uh_chen33f5a592014-07-02 19:36:56 +0800232void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800233 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800234 if(expand) {
235 // Expand the startTiling region by 1
236 int leftNotZero = (clip.left > 0) ? 1 : 0;
237 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
238
239 mCaches.startTiling(
240 clip.left - leftNotZero,
241 windowHeight - clip.bottom - topNotZero,
242 clip.right - clip.left + leftNotZero + 1,
243 clip.bottom - clip.top + topNotZero + 1,
244 opaque);
245 } else {
246 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800247 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800248 }
Romain Guy54c1a642012-09-27 17:55:46 -0700249 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700250}
251
252void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700253 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700254}
255
Tom Hudson107843d2014-09-08 11:26:26 -0400256bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700257 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700258 endTiling();
Chris Craik4ac36f82014-12-09 16:54:03 -0800259 mTempPaths.clear();
260
Romain Guyca89e2a2013-03-08 17:44:20 -0800261 // When finish() is invoked on FBO 0 we've reached the end
262 // of the current frame
Tom Hudson984162f2014-10-10 13:38:16 -0400263 if (onGetTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800264 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700265 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800266 }
267
Romain Guy11cb6422012-09-21 00:39:43 -0700268 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700269#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700270 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700271#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700272
Romain Guyc15008e2010-11-10 11:59:15 -0800273#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800274 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700275#else
276 if (mCaches.getDebugLevel() & kDebugMemory) {
277 mCaches.dumpMemoryUsage();
278 }
Romain Guyc15008e2010-11-10 11:59:15 -0800279#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700280 }
Romain Guy96885eb2013-03-26 15:05:58 -0700281
282 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400283
284 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700285}
286
Romain Guy35643dd2012-09-18 15:40:58 -0700287void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700288 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
289 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700290 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700291
Chris Craik65fe5ee2015-01-26 18:06:29 -0800292 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700293 dirtyClip();
294}
295
Andreas Gampe64bb4132014-11-22 00:35:09 +0000296void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400297 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700298
Rob Tsuk487a92c2015-01-06 13:22:54 -0800299 Rect clip(mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700300 clip.snapToPixelBoundaries();
301
Romain Guyd643bb52011-03-01 14:55:21 -0800302 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800303 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800304 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800305 dirtyLayerUnchecked(clip, getRegion());
306 }
Romain Guyd643bb52011-03-01 14:55:21 -0800307
Romain Guy08aa2cb2011-03-17 11:06:57 -0700308 DrawGlInfo info;
309 info.clipLeft = clip.left;
310 info.clipTop = clip.top;
311 info.clipRight = clip.right;
312 info.clipBottom = clip.bottom;
313 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700314 info.width = getViewportWidth();
315 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800316 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700317
Tom Hudson984162f2014-10-10 13:38:16 -0400318 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700319 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400320 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700321 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
322 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800323 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700324 setScissorFromClip();
325 }
Chris Craik54f574a2013-08-26 11:23:46 -0700326
John Reck3b202512014-06-23 13:13:08 -0700327 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
328 // Scissor may have been modified, reset dirty clip
329 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800330
Tom Hudson107843d2014-09-08 11:26:26 -0400331 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800332}
333
Romain Guyf6a11b82010-06-23 17:47:49 -0700334///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700335// Debug
336///////////////////////////////////////////////////////////////////////////////
337
Chris Craik62d307c2014-07-29 10:35:13 -0700338void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
339#if DEBUG_DETAILED_EVENTS
340 const int BUFFER_SIZE = 256;
341 va_list ap;
342 char buf[BUFFER_SIZE];
343
344 va_start(ap, fmt);
345 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
346 va_end(ap);
347
348 eventMark(buf);
349#endif
350}
351
352
Romain Guy0f667532013-03-01 14:31:04 -0800353void OpenGLRenderer::eventMark(const char* name) const {
354 mCaches.eventMark(0, name);
355}
356
Romain Guy87e2f7572012-09-24 11:37:12 -0700357void OpenGLRenderer::startMark(const char* name) const {
358 mCaches.startMark(0, name);
359}
360
361void OpenGLRenderer::endMark() const {
362 mCaches.endMark();
363}
364
365void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700366 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700367}
368
369void OpenGLRenderer::renderOverdraw() {
Tom Hudson984162f2014-10-10 13:38:16 -0400370 if (mCaches.debugOverdraw && onGetTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700371 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700372
Chris Craik65fe5ee2015-01-26 18:06:29 -0800373 mRenderState.scissor().setEnabled(true);
374 mRenderState.scissor().set(clip->left,
375 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
376 clip->right - clip->left,
377 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700378
Romain Guy627c6fd2013-08-21 11:53:18 -0700379 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800380 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700381 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
382
383 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800384 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700385 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
386
387 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800388 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700389 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
390
391 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800392 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700393 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
394
Chris Craik96a5c4c2015-01-27 15:46:35 -0800395 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700396 }
397}
398
399///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700400// Layers
401///////////////////////////////////////////////////////////////////////////////
402
403bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700404 if (layer->deferredUpdateScheduled && layer->renderer
405 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700406
Romain Guy7c450aa2012-09-21 19:15:00 -0700407 if (inFrame) {
408 endTiling();
409 debugOverdraw(false, false);
410 }
Romain Guy11cb6422012-09-21 00:39:43 -0700411
Romain Guy96885eb2013-03-26 15:05:58 -0700412 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700413 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700414 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700415 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700416 }
Romain Guy11cb6422012-09-21 00:39:43 -0700417
418 if (inFrame) {
419 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800420 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700421 }
422
Romain Guy5bb3c732012-11-29 17:52:58 -0800423 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700424 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700425
426 return true;
427 }
428
429 return false;
430}
431
432void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700433 // If draw deferring is enabled this method will simply defer
434 // the display list of each individual layer. The layers remain
435 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700436 int count = mLayerUpdates.size();
437 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700438 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
439 startMark("Layer Updates");
440 } else {
441 startMark("Defer Layer Updates");
442 }
Romain Guy11cb6422012-09-21 00:39:43 -0700443
Chris Craik1206b9b2013-04-04 14:46:24 -0700444 // Note: it is very important to update the layers in order
445 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700446 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700447 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700448 }
Romain Guy11cb6422012-09-21 00:39:43 -0700449
Romain Guy96885eb2013-03-26 15:05:58 -0700450 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
451 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400452 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700453 }
454 endMark();
455 }
456}
457
458void OpenGLRenderer::flushLayers() {
459 int count = mLayerUpdates.size();
460 if (count > 0) {
461 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700462
Chris Craik1206b9b2013-04-04 14:46:24 -0700463 // Note: it is very important to update the layers in order
464 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800465 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700466 }
467
468 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400469 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700470
Romain Guy11cb6422012-09-21 00:39:43 -0700471 endMark();
472 }
473}
474
475void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
476 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700477 // Make sure we don't introduce duplicates.
478 // SortedVector would do this automatically but we need to respect
479 // the insertion order. The linear search is not an issue since
480 // this list is usually very short (typically one item, at most a few)
481 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
482 if (mLayerUpdates.itemAt(i) == layer) {
483 return;
484 }
485 }
Romain Guy11cb6422012-09-21 00:39:43 -0700486 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700487 }
488}
489
Romain Guye93482f2013-06-17 13:14:51 -0700490void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
491 if (layer) {
492 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
493 if (mLayerUpdates.itemAt(i) == layer) {
494 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700495 break;
496 }
497 }
498 }
499}
500
Romain Guy40543602013-06-12 15:31:28 -0700501void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800502 ATRACE_NAME("Update HW Layers");
Chris Craik44eb2c02015-01-29 09:45:09 -0800503 mRenderState.blend().syncEnabled();
Romain Guy40543602013-06-12 15:31:28 -0700504 updateLayers();
505 flushLayers();
506 // Wait for all the layer updates to be executed
507 AutoFence fence;
508}
509
John Reck443a7142014-09-04 17:40:05 -0700510void OpenGLRenderer::markLayersAsBuildLayers() {
511 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
512 mLayerUpdates[i]->wasBuildLayered = true;
513 }
514}
515
Romain Guy11cb6422012-09-21 00:39:43 -0700516///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700517// State management
518///////////////////////////////////////////////////////////////////////////////
519
Chris Craik14e51302013-12-30 15:32:54 -0800520void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700521 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800522 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
523 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700524
Chris Craika64a2be2014-05-14 14:17:01 -0700525 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700526 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700527 }
528
Romain Guy2542d192010-08-18 11:47:12 -0700529 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700530 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700531 }
Romain Guy2542d192010-08-18 11:47:12 -0700532
Romain Guy5ec99242010-11-03 16:19:08 -0700533 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700534 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700535 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700536 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800537 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700538 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700539 }
Romain Guybb9524b2010-06-22 18:56:38 -0700540}
541
Romain Guyf6a11b82010-06-23 17:47:49 -0700542///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700543// Layers
544///////////////////////////////////////////////////////////////////////////////
545
546int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700547 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700548 // force matrix/clip isolation for layer
549 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
550
Tom Hudson984162f2014-10-10 13:38:16 -0400551 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700552
Tom Hudson984162f2014-10-10 13:38:16 -0400553 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700554 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700555 }
Romain Guyd55a8612010-06-28 17:42:46 -0700556
557 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700558}
559
Chris Craikd90144d2013-03-19 15:03:48 -0700560void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
561 const Rect untransformedBounds(bounds);
562
Chris Craikd6b65f62014-01-01 14:45:21 -0800563 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700564
565 // Layers only make sense if they are in the framebuffer's bounds
Rob Tsuk487a92c2015-01-06 13:22:54 -0800566 if (bounds.intersect(mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700567 // We cannot work with sub-pixels in this case
568 bounds.snapToPixelBoundaries();
569
570 // When the layer is not an FBO, we may use glCopyTexImage so we
571 // need to make sure the layer does not extend outside the bounds
572 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700573 const Snapshot& previous = *(currentSnapshot()->previous);
574 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
575 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700576 bounds.setEmpty();
577 } else if (fboLayer) {
578 clip.set(bounds);
579 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800580 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700581 inverse.mapRect(clip);
582 clip.snapToPixelBoundaries();
583 if (clip.intersect(untransformedBounds)) {
584 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
585 bounds.set(untransformedBounds);
586 } else {
587 clip.setEmpty();
588 }
589 }
590 } else {
591 bounds.setEmpty();
592 }
593}
594
Chris Craik408eb122013-03-26 18:55:15 -0700595void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
596 bool fboLayer, int alpha) {
597 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
598 bounds.getHeight() > mCaches.maxTextureSize ||
599 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400600 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700601 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400602 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700603 }
604}
605
Chris Craikd90144d2013-03-19 15:03:48 -0700606int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500607 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400608 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700609
Tom Hudson984162f2014-10-10 13:38:16 -0400610 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700611 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
612 // operations will be able to store and restore the current clip and transform info, and
613 // quick rejection will be correct (for display lists)
614
615 Rect bounds(left, top, right, bottom);
616 Rect clip;
617 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500618 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700619
Tom Hudson984162f2014-10-10 13:38:16 -0400620 if (!mState.currentlyIgnored()) {
621 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
622 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
623 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800624 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700625 }
626 }
627
628 return count;
629}
630
Romain Guy1c740bc2010-09-13 18:00:09 -0700631/**
632 * Layers are viewed by Skia are slightly different than layers in image editing
633 * programs (for instance.) When a layer is created, previously created layers
634 * and the frame buffer still receive every drawing command. For instance, if a
635 * layer is created and a shape intersecting the bounds of the layers and the
636 * framebuffer is draw, the shape will be drawn on both (unless the layer was
637 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
638 *
639 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
640 * texture. Unfortunately, this is inefficient as it requires every primitive to
641 * be drawn n + 1 times, where n is the number of active layers. In practice this
642 * means, for every primitive:
643 * - Switch active frame buffer
644 * - Change viewport, clip and projection matrix
645 * - Issue the drawing
646 *
647 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700648 * To avoid this, layers are implemented in a different way here, at least in the
649 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
650 * is set. When this flag is set we can redirect all drawing operations into a
651 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700652 *
653 * This implementation relies on the frame buffer being at least RGBA 8888. When
654 * a layer is created, only a texture is created, not an FBO. The content of the
655 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700656 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700657 * buffer and drawing continues as normal. This technique therefore treats the
658 * frame buffer as a scratch buffer for the layers.
659 *
660 * To compose the layers back onto the frame buffer, each layer texture
661 * (containing the original frame buffer data) is drawn as a simple quad over
662 * the frame buffer. The trick is that the quad is set as the composition
663 * destination in the blending equation, and the frame buffer becomes the source
664 * of the composition.
665 *
666 * Drawing layers with an alpha value requires an extra step before composition.
667 * An empty quad is drawn over the layer's region in the frame buffer. This quad
668 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
669 * quad is used to multiply the colors in the frame buffer. This is achieved by
670 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
671 * GL_ZERO, GL_SRC_ALPHA.
672 *
673 * Because glCopyTexImage2D() can be slow, an alternative implementation might
674 * be use to draw a single clipped layer. The implementation described above
675 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700676 *
677 * (1) The frame buffer is actually not cleared right away. To allow the GPU
678 * to potentially optimize series of calls to glCopyTexImage2D, the frame
679 * buffer is left untouched until the first drawing operation. Only when
680 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700681 */
Chet Haased48885a2012-08-28 17:43:28 -0700682bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700683 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800684 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
685 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700686
Romain Guyeb993562010-10-05 18:14:38 -0700687 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
688
Romain Guyf607bdc2010-09-10 19:20:06 -0700689 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700690 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700691 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700692 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000693 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700694
695 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400696 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700697 return false;
698 }
Romain Guyf18fd992010-07-08 11:45:51 -0700699
Chris Craik44eb2c02015-01-29 09:45:09 -0800700 mCaches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700701 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700702 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700703 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700704 }
705
Derek Sollenberger674554f2014-02-19 16:47:32 +0000706 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700707 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700708 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
709 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500710
Chet Haasea23eed82012-04-12 15:19:04 -0700711 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700712 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700713 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda57022010-07-06 11:39:32 -0700714
Romain Guy8fb95422010-08-17 18:38:51 -0700715 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400716 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
717 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700718
Chris Craika8bea8e2014-09-24 11:29:43 -0700719 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
720 fboLayer ? "" : "unclipped ",
721 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700722 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700723 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700724 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700725 } else {
726 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700727 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800728 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700729 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700730 // Workaround for some GL drivers. When reading pixels lying outside
731 // of the window we should get undefined values for those pixels.
732 // Unfortunately some drivers will turn the entire target texture black
733 // when reading outside of the window.
734 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800735 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700736 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800737 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700738
Chris Craika64a2be2014-05-14 14:17:01 -0700739 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
740 bounds.left, getViewportHeight() - bounds.bottom,
741 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700742
Romain Guy54be1cd2011-06-13 19:04:27 -0700743 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800744 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700745 }
Romain Guyeb993562010-10-05 18:14:38 -0700746 }
Romain Guyf86ef572010-07-01 11:05:42 -0700747
Romain Guyd55a8612010-06-28 17:42:46 -0700748 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700749}
750
Chris Craike63f7c622013-10-17 10:30:55 -0700751bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800752 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700753 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700754
Tom Hudson984162f2014-10-10 13:38:16 -0400755 writableSnapshot()->region = &writableSnapshot()->layer->region;
756 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
757 writableSnapshot()->fbo = layer->getFbo();
758 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
759 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
760 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800761 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700762
Romain Guy2b7028e2012-09-19 17:25:38 -0700763 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700764 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700765 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700766 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700767 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700768
769 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700770 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700771 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700772 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700773 }
774
775 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700776 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700777
henry.uh_chen33f5a592014-07-02 19:36:56 +0800778 // Expand the startTiling region by 1
779 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700780
781 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800782 mRenderState.scissor().setEnabled(true);
783 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700784 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700785 glClear(GL_COLOR_BUFFER_BIT);
786
787 dirtyClip();
788
789 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700790 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700791 return true;
792}
793
Romain Guy1c740bc2010-09-13 18:00:09 -0700794/**
795 * Read the documentation of createLayer() before doing anything in this method.
796 */
Chris Craik14e51302013-12-30 15:32:54 -0800797void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
798 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000799 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700800 return;
801 }
802
Chris Craik14e51302013-12-30 15:32:54 -0800803 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800804 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800805 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700806
Chris Craik39a908c2013-06-13 14:39:01 -0700807 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400808 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800809 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800810 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700811
Romain Guyeb993562010-10-05 18:14:38 -0700812 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700813 endTiling();
814
Romain Guye0aa84b2012-04-03 19:30:26 -0700815 // Detach the texture from the FBO
816 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800817
818 layer->removeFbo(false);
819
Romain Guyeb993562010-10-05 18:14:38 -0700820 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700821 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700822 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700823
Chris Craikd6b65f62014-01-01 14:45:21 -0800824 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700825 }
826
Romain Guy9ace8f52011-07-07 20:50:11 -0700827 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500828 SkPaint layerPaint;
829 layerPaint.setAlpha(layer->getAlpha());
830 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
831 layerPaint.setColorFilter(layer->getColorFilter());
832
833 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700834 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700835 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700836 }
837
Chris Craik96a5c4c2015-01-27 15:46:35 -0800838 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700839
Chris Craik44eb2c02015-01-29 09:45:09 -0800840 mCaches.textureState().activateTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700841
Romain Guy5b3b3522010-10-27 18:57:51 -0700842 // When the layer is stored in an FBO, we can save a bit of fillrate by
843 // drawing only the dirty region
844 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800845 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700846 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700847 } else if (!rect.isEmpty()) {
848 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530849
850 save(0);
851 // the layer contains screen buffer content that shouldn't be alpha modulated
852 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400853 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700854 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530855 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700856 }
Romain Guy8b55f372010-08-18 17:10:07 -0700857
Romain Guy746b7402010-10-26 16:27:31 -0700858 dirtyClip();
859
Romain Guyeb993562010-10-05 18:14:38 -0700860 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800861 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700862 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800863 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800864 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700865 }
866}
867
Romain Guyaa6c24c2011-04-28 18:40:04 -0700868void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700869 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700870
871 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700872 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700873 setupDrawWithTexture();
874 } else {
875 setupDrawWithExternalTexture();
876 }
877 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700878 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500879 setupDrawColorFilter(layer->getColorFilter());
880 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700881 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700882 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500883 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700884 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
885 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700886 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700887 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700888 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800889 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800890 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700891 layer->getWidth() == (uint32_t) rect.getWidth() &&
892 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800893 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
894 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700895
Romain Guyd21b6e12011-11-30 20:21:23 -0800896 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800897 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
898 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700899 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800900 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800901 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
902 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700903 }
904 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700905 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700906
Chris Craik117bdbc2015-02-05 10:12:38 -0800907 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700908}
909
Romain Guy5b3b3522010-10-27 18:57:51 -0700910void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700911 if (layer->isTextureLayer()) {
912 EVENT_LOGD("composeTextureLayerRect");
913 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
914 drawTextureLayer(layer, rect);
915 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
916 } else {
917 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700918 const Rect& texCoords = layer->texCoords;
919 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
920 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700921
Romain Guy9ace8f52011-07-07 20:50:11 -0700922 float x = rect.left;
923 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -0800924 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700925 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700926 layer->getHeight() == (uint32_t) rect.getHeight();
927
928 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700929 // When we're swapping, the layer is already in screen coordinates
930 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800931 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
932 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700933 }
934
Romain Guyd21b6e12011-11-30 20:21:23 -0800935 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700936 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800937 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700938 }
939
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500940 SkPaint layerPaint;
941 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
942 layerPaint.setXfermodeMode(layer->getMode());
943 layerPaint.setColorFilter(layer->getColorFilter());
944
945 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700946 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500947 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -0700948 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik117bdbc2015-02-05 10:12:38 -0800949 GL_TRIANGLE_STRIP, kUnitQuadCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700950
Romain Guyaa6c24c2011-04-28 18:40:04 -0700951 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700952 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700953}
954
Chris Craik34416ea2013-04-15 16:08:28 -0700955/**
956 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
957 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
958 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
959 * by saveLayer's restore
960 */
Tom Hudson984162f2014-10-10 13:38:16 -0400961#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
962 DRAW_COMMAND; \
963 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
964 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
965 DRAW_COMMAND; \
966 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
967 } \
Chris Craik34416ea2013-04-15 16:08:28 -0700968 }
969
970#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
971
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400972// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
973// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
974class LayerShader : public SkShader {
975public:
976 LayerShader(Layer* layer, const SkMatrix* localMatrix)
977 : INHERITED(localMatrix)
978 , mLayer(layer) {
979 }
980
Chris Craike84a2082014-12-22 14:28:49 -0800981 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400982 if (data) {
983 *data = static_cast<void*>(mLayer);
984 }
985 return true;
986 }
987
Chris Craike84a2082014-12-22 14:28:49 -0800988 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400989 return !mLayer->isBlend();
990 }
991
992protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000993 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400994 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
995 }
996
Chris Craike84a2082014-12-22 14:28:49 -0800997 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400998 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
999 }
1000
Chris Craike84a2082014-12-22 14:28:49 -08001001 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001002 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -08001003 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001004 }
1005private:
1006 // Unowned.
1007 Layer* mLayer;
1008 typedef SkShader INHERITED;
1009};
1010
Romain Guy5b3b3522010-10-27 18:57:51 -07001011void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001012 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1013
1014 if (layer->getConvexMask()) {
1015 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1016
1017 // clip to the area of the layer the mask can be larger
1018 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1019
1020 SkPaint paint;
1021 paint.setAntiAlias(true);
1022 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1023
Chris Craik3f0854292014-04-15 16:18:08 -07001024 // create LayerShader to map SaveLayer content into subsequent draw
1025 SkMatrix shaderMatrix;
1026 shaderMatrix.setTranslate(rect.left, rect.bottom);
1027 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001028 LayerShader layerShader(layer, &shaderMatrix);
1029 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001030
1031 // Since the drawing primitive is defined in local drawing space,
1032 // we don't need to modify the draw matrix
1033 const SkPath* maskPath = layer->getConvexMask();
1034 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1035
Chris Craike84a2082014-12-22 14:28:49 -08001036 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -07001037 restore();
1038
1039 return;
1040 }
1041
Romain Guy5b3b3522010-10-27 18:57:51 -07001042 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001043 layer->setRegionAsRect();
1044
Chris Craik34416ea2013-04-15 16:08:28 -07001045 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001046
Romain Guy5b3b3522010-10-27 18:57:51 -07001047 layer->region.clear();
1048 return;
1049 }
1050
Chris Craik62d307c2014-07-29 10:35:13 -07001051 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001052 // standard Region based draw
1053 size_t count;
1054 const android::Rect* rects;
1055 Region safeRegion;
1056 if (CC_LIKELY(hasRectToRectTransform())) {
1057 rects = layer->region.getArray(&count);
1058 } else {
1059 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1060 rects = safeRegion.getArray(&count);
1061 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001062
Chris Craik3f0854292014-04-15 16:18:08 -07001063 const float alpha = getLayerAlpha(layer);
1064 const float texX = 1.0f / float(layer->getWidth());
1065 const float texY = 1.0f / float(layer->getHeight());
1066 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001067
Chris Craik3f0854292014-04-15 16:18:08 -07001068 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001069
Chris Craik3f0854292014-04-15 16:18:08 -07001070 // We must get (and therefore bind) the region mesh buffer
1071 // after we setup drawing in case we need to mess with the
1072 // stencil buffer in setupDraw()
1073 TextureVertex* mesh = mCaches.getRegionMesh();
1074 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001075
Chris Craik3f0854292014-04-15 16:18:08 -07001076 setupDrawWithTexture();
1077 setupDrawColor(alpha, alpha, alpha, alpha);
1078 setupDrawColorFilter(layer->getColorFilter());
1079 setupDrawBlending(layer);
1080 setupDrawProgram();
1081 setupDrawDirtyRegionsDisabled();
1082 setupDrawPureColorUniforms();
1083 setupDrawColorFilterUniforms(layer->getColorFilter());
1084 setupDrawTexture(layer->getTexture());
1085 if (currentTransform()->isPureTranslate()) {
1086 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1087 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001088
Chris Craik3f0854292014-04-15 16:18:08 -07001089 layer->setFilter(GL_NEAREST);
1090 setupDrawModelView(kModelViewMode_Translate, false,
1091 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1092 } else {
1093 layer->setFilter(GL_LINEAR);
1094 setupDrawModelView(kModelViewMode_Translate, false,
1095 rect.left, rect.top, rect.right, rect.bottom);
1096 }
1097 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001098
Chris Craik3f0854292014-04-15 16:18:08 -07001099 for (size_t i = 0; i < count; i++) {
1100 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001101
Chris Craik3f0854292014-04-15 16:18:08 -07001102 const float u1 = r->left * texX;
1103 const float v1 = (height - r->top) * texY;
1104 const float u2 = r->right * texX;
1105 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001106
Chris Craik3f0854292014-04-15 16:18:08 -07001107 // TODO: Reject quads outside of the clip
1108 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1109 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1110 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1111 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001112
Chris Craik3f0854292014-04-15 16:18:08 -07001113 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001114
Chris Craik96a5c4c2015-01-27 15:46:35 -08001115 if (numQuads >= kMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001116 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001117 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001118 numQuads = 0;
1119 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001120 }
Chris Craik3f0854292014-04-15 16:18:08 -07001121 }
1122
1123 if (numQuads > 0) {
1124 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001125 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001126 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001127
Romain Guy5b3b3522010-10-27 18:57:51 -07001128#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001129 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001130#endif
1131
Chris Craik3f0854292014-04-15 16:18:08 -07001132 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001133}
1134
Romain Guy3a3133d2011-02-01 22:59:58 -08001135#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001136void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001137 size_t count;
1138 const android::Rect* rects = region.getArray(&count);
1139
1140 uint32_t colors[] = {
1141 0x7fff0000, 0x7f00ff00,
1142 0x7f0000ff, 0x7fff00ff,
1143 };
1144
1145 int offset = 0;
1146 int32_t top = rects[0].top;
1147
1148 for (size_t i = 0; i < count; i++) {
1149 if (top != rects[i].top) {
1150 offset ^= 0x2;
1151 top = rects[i].top;
1152 }
1153
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001154 SkPaint paint;
1155 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001156 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001157 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001158 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001159}
Chris Craike63f7c622013-10-17 10:30:55 -07001160#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001161
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001162void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001163 Vector<float> rects;
1164
1165 SkRegion::Iterator it(region);
1166 while (!it.done()) {
1167 const SkIRect& r = it.rect();
1168 rects.push(r.fLeft);
1169 rects.push(r.fTop);
1170 rects.push(r.fRight);
1171 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001172 it.next();
1173 }
1174
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001175 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001176}
1177
Romain Guy5b3b3522010-10-27 18:57:51 -07001178void OpenGLRenderer::dirtyLayer(const float left, const float top,
1179 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001180 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001181 Rect bounds(left, top, right, bottom);
1182 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001183 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001184 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001185}
1186
1187void OpenGLRenderer::dirtyLayer(const float left, const float top,
1188 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001189 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001190 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001191 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001192 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001193}
1194
1195void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001196 if (bounds.intersect(mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001197 bounds.snapToPixelBoundaries();
1198 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1199 if (!dirty.isEmpty()) {
1200 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001201 }
1202 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001203}
1204
Chris Craik4063a0e2013-11-15 16:06:56 -08001205void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001206 GLsizei elementsCount = quadsCount * 6;
1207 while (elementsCount > 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001208 GLsizei drawCount = min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Romain Guy448455f2013-07-22 13:57:50 -07001209
Romain Guy3380cfd2013-08-15 16:57:57 -07001210 setupDrawIndexedVertices(&mesh[0].x);
Chris Craike84a2082014-12-22 14:28:49 -08001211 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy448455f2013-07-22 13:57:50 -07001212
1213 elementsCount -= drawCount;
1214 // Though there are 4 vertices in a quad, we use 6 indices per
1215 // quad to draw with GL_TRIANGLES
1216 mesh += (drawCount / 6) * 4;
1217 }
1218}
1219
Romain Guy54be1cd2011-06-13 19:04:27 -07001220void OpenGLRenderer::clearLayerRegions() {
1221 const size_t count = mLayers.size();
1222 if (count == 0) return;
1223
Tom Hudson984162f2014-10-10 13:38:16 -04001224 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001225 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001226 // Doing several glScissor/glClear here can negatively impact
1227 // GPUs with a tiler architecture, instead we draw quads with
1228 // the Clear blending mode
1229
1230 // The list contains bounds that have already been clipped
1231 // against their initial clip rect, and the current clip
1232 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001233 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001234
Romain Guy448455f2013-07-22 13:57:50 -07001235 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001236 Vertex* vertex = mesh;
1237
1238 for (uint32_t i = 0; i < count; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001239 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001240
Chris Craik51d6a3d2014-12-22 17:16:56 -08001241 Vertex::set(vertex++, bounds.left, bounds.top);
1242 Vertex::set(vertex++, bounds.right, bounds.top);
1243 Vertex::set(vertex++, bounds.left, bounds.bottom);
1244 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001245 }
Romain Guye67307c2013-02-11 18:01:20 -08001246 // We must clear the list of dirty rects before we
1247 // call setupDraw() to prevent stencil setup to do
1248 // the same thing again
1249 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001250
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001251 SkPaint clearPaint;
1252 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1253
Romain Guy54be1cd2011-06-13 19:04:27 -07001254 setupDraw(false);
1255 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001256 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001257 setupDrawProgram();
1258 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001259 setupDrawModelView(kModelViewMode_Translate, false,
1260 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001261
Chris Craik4063a0e2013-11-15 16:06:56 -08001262 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001263
Chris Craik65fe5ee2015-01-26 18:06:29 -08001264 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001265 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001266 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001267 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001268}
1269
Romain Guybd6b79b2010-06-26 00:13:53 -07001270///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001271// State Deferral
1272///////////////////////////////////////////////////////////////////////////////
1273
Chris Craikff785832013-03-08 13:12:16 -08001274bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001275 const Rect& currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001276 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001277
Chris Craikff785832013-03-08 13:12:16 -08001278 if (stateDeferFlags & kStateDeferFlag_Draw) {
1279 // state has bounds initialized in local coordinates
1280 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001281 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001282 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001283 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1284 // is used, it should more closely duplicate the quickReject logic (in how it uses
1285 // snapToPixelBoundaries)
1286
Rob Tsuk487a92c2015-01-06 13:22:54 -08001287 if (!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001288 // quick rejected
1289 return true;
1290 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001291
Chris Craika02c4ed2013-06-14 13:43:58 -07001292 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001293 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001294 int& flags = state.mClipSideFlags;
1295 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001296 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1297 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1298 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1299 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001300 }
1301 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001302 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001303 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1304 // overdraw avoidance (since we don't know what it overlaps)
1305 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001306 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001307 }
1308 }
1309
Chris Craik527a3aa2013-03-04 10:19:31 -08001310 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1311 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001312 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001313 }
1314
Chris Craik7273daa2013-03-28 11:25:24 -07001315 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1316 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001317 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001318 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001319 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001320
1321 // always store/restore, since it's just a pointer
1322 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001323 return false;
1324}
1325
Chris Craik527a3aa2013-03-04 10:19:31 -08001326void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001327 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001328 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001329 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001330 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001331
Chris Craik527a3aa2013-03-04 10:19:31 -08001332 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001333 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001334 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001335 dirtyClip();
1336 }
Chris Craikc3566d02013-02-04 16:16:33 -08001337}
1338
Chris Craik28ce94a2013-05-31 11:38:03 -07001339/**
1340 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1341 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1342 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1343 *
1344 * This method should be called when restoreDisplayState() won't be restoring the clip
1345 */
1346void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001347 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001348 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001349 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001350 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001351 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001352 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001353 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1354 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001355}
1356
Chris Craikc3566d02013-02-04 16:16:33 -08001357///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001358// Clipping
1359///////////////////////////////////////////////////////////////////////////////
1360
Romain Guybb9524b2010-06-22 18:56:38 -07001361void OpenGLRenderer::setScissorFromClip() {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001362 Rect clip(mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001363 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001364
Chris Craik65fe5ee2015-01-26 18:06:29 -08001365 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001366 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001367 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001368 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001369}
1370
Romain Guy8ce00302013-01-15 18:51:42 -08001371void OpenGLRenderer::ensureStencilBuffer() {
1372 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1373 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1374 // just hope we have one when hasLayer() returns false.
1375 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001376 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001377 }
1378}
1379
1380void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1381 // The layer's FBO is already bound when we reach this stage
1382 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001383 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1384 // is attached after we initiated tiling. We must turn it off,
1385 // attach the new render buffer then turn tiling back on
1386 endTiling();
1387
Romain Guy8d4aeb72013-02-12 16:08:55 -08001388 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Chris Craik117bdbc2015-02-05 10:12:38 -08001389 Stencil::getSmallestStencilFormat(),
1390 layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001391 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001392
Romain Guyf735c8e2013-01-31 17:45:55 -08001393 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001394 }
1395}
1396
Rob Tsuk487a92c2015-01-06 13:22:54 -08001397static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1398 float x, float y) {
1399 Vertex v;
1400 v.x = x;
1401 v.y = y;
1402 transform.mapPoint(v.x, v.y);
1403 rectangleVertices.push_back(v);
1404}
1405
1406static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1407 Vertex v;
1408 v.x = x;
1409 v.y = y;
1410 rectangleVertices.push_back(v);
1411}
1412
1413void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
1414 int count = rectangleList.getTransformedRectanglesCount();
1415 std::vector<Vertex> rectangleVertices(count * 4);
1416 Rect scissorBox = rectangleList.calculateBounds();
1417 scissorBox.snapToPixelBoundaries();
1418 for (int i = 0; i < count; ++i) {
1419 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1420 const Matrix4& transform = tr.getTransform();
1421 Rect bounds = tr.getBounds();
1422 if (transform.rectToRect()) {
1423 transform.mapRect(bounds);
1424 if (!bounds.intersect(scissorBox)) {
1425 bounds.setEmpty();
1426 } else {
1427 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1428 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1429 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1430 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1431 }
1432 } else {
1433 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1434 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1435 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1436 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1437 }
1438 }
1439
Chris Craik65fe5ee2015-01-26 18:06:29 -08001440 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001441 scissorBox.getWidth(), scissorBox.getHeight());
1442
1443 const SkPaint* paint = nullptr;
1444 setupDraw();
1445 setupDrawNoTexture();
1446 setupDrawColor(0, 0xff * currentSnapshot()->alpha);
1447 setupDrawShader(getShader(paint));
1448 setupDrawColorFilter(getColorFilter(paint));
1449 setupDrawBlending(paint);
1450 setupDrawProgram();
1451 setupDrawDirtyRegionsDisabled();
1452 setupDrawModelView(kModelViewMode_Translate, false,
1453 0.0f, 0.0f, 0.0f, 0.0f, true);
1454 setupDrawColorUniforms(getShader(paint));
1455 setupDrawShaderUniforms(getShader(paint));
1456 setupDrawColorFilterUniforms(getColorFilter(paint));
1457
1458 issueIndexedQuadDraw(&rectangleVertices[0], rectangleVertices.size() / 4);
1459}
1460
Romain Guy8ce00302013-01-15 18:51:42 -08001461void OpenGLRenderer::setStencilFromClip() {
1462 if (!mCaches.debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001463 if (!currentSnapshot()->clipIsSimple()) {
1464 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001465 EVENT_LOGD("setStencilFromClip - enabling");
1466
Romain Guy8ce00302013-01-15 18:51:42 -08001467 // NOTE: The order here is important, we must set dirtyClip to false
1468 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001469 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001470
1471 ensureStencilBuffer();
1472
Rob Tsuk487a92c2015-01-06 13:22:54 -08001473 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001474
Rob Tsuk487a92c2015-01-06 13:22:54 -08001475 bool isRectangleList = clipArea.isRectangleList();
1476 if (isRectangleList) {
1477 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1478 } else {
1479 incrementThreshold = 0;
1480 }
1481
Chris Craik96a5c4c2015-01-27 15:46:35 -08001482 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001483
1484 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001485 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001486 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001487 if (resetScissor) {
1488 // The scissor was not set so we now need to update it
1489 setScissorFromClip();
1490 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001491
Chris Craik96a5c4c2015-01-27 15:46:35 -08001492 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001493
1494 // stash and disable the outline clip state, since stencil doesn't account for outline
1495 bool storedSkipOutlineClip = mSkipOutlineClip;
1496 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001497
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001498 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001499 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001500 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1501
Rob Tsuk487a92c2015-01-06 13:22:54 -08001502 if (isRectangleList) {
1503 drawRectangleList(clipArea.getRectangleList());
1504 } else {
1505 // NOTE: We could use the region contour path to generate a smaller mesh
1506 // Since we are using the stencil we could use the red book path
1507 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001508
Rob Tsuk487a92c2015-01-06 13:22:54 -08001509 // The last parameter is important: we are not drawing in the color buffer
1510 // so we don't want to dirty the current layer, if any
1511 drawRegionRects(clipArea.getClipRegion(), paint, false);
1512 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001513 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001514 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001515
Chris Craik96a5c4c2015-01-27 15:46:35 -08001516 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001517
1518 // Draw the region used to generate the stencil if the appropriate debug
1519 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001520 // TODO: Implement for rectangle list clip areas
1521 if (mCaches.debugStencilClip == Caches::kStencilShowRegion &&
1522 !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001523 paint.setColor(0x7f0000ff);
1524 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001525 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001526 }
Romain Guy8ce00302013-01-15 18:51:42 -08001527 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001528 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001529 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001530 }
1531 }
1532}
1533
Chris Craikf0a59072013-11-19 18:00:46 -08001534/**
1535 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1536 *
1537 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1538 * style, and tessellated AA ramp
1539 */
1540bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001541 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001542 bool snapOut = paint && paint->isAntiAlias();
1543
1544 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1545 float outset = paint->getStrokeWidth() * 0.5f;
1546 left -= outset;
1547 top -= outset;
1548 right += outset;
1549 bottom += outset;
1550 }
1551
Chris Craikdeeda3d2014-05-05 19:09:33 -07001552 bool clipRequired = false;
1553 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001554 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001555 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001556 return true;
1557 }
1558
Chris Craikf23b25a2014-06-26 15:46:20 -07001559 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001560 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001561 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001562 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001563}
1564
Romain Guy8ce00302013-01-15 18:51:42 -08001565void OpenGLRenderer::debugClip() {
1566#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001567 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001568 SkPaint paint;
1569 paint.setColor(0x7f00ff00);
1570 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1571
Romain Guy8ce00302013-01-15 18:51:42 -08001572 }
1573#endif
1574}
1575
Chris Craik117bdbc2015-02-05 10:12:38 -08001576void OpenGLRenderer::renderGlop(const Glop& glop) {
1577 if (mState.getDirtyClip()) {
1578 if (mRenderState.scissor().isEnabled()) {
1579 setScissorFromClip();
1580 }
1581
1582 setStencilFromClip();
1583 }
1584 mRenderState.render(glop);
1585 dirtyLayer(glop.bounds.left, glop.bounds.top, glop.bounds.right, glop.bounds.bottom);
1586}
1587
Romain Guyf6a11b82010-06-23 17:47:49 -07001588///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001589// Drawing commands
1590///////////////////////////////////////////////////////////////////////////////
1591
Chris Craik62d307c2014-07-29 10:35:13 -07001592void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001593 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001594 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001595 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001596 // Make sure setScissor & setStencil happen at the beginning of
1597 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001598 if (mState.getDirtyClip()) {
Chris Craik65fe5ee2015-01-26 18:06:29 -08001599 if (mRenderState.scissor().isEnabled()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001600 setScissorFromClip();
1601 }
Chris Craik62d307c2014-07-29 10:35:13 -07001602
Dohyun Leeadc0d9d2014-11-24 21:08:15 +09001603 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001604 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001605
Romain Guy70ca14e2010-12-13 18:24:33 -08001606 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001607
Romain Guy70ca14e2010-12-13 18:24:33 -08001608 mSetShaderColor = false;
1609 mColorSet = false;
1610 mColorA = mColorR = mColorG = mColorB = 0.0f;
1611 mTextureUnit = 0;
1612 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001613
1614 // Enable debug highlight when what we're about to draw is tested against
1615 // the stencil buffer and if stencil highlight debugging is on
Chris Craik117bdbc2015-02-05 10:12:38 -08001616 mDescription.hasDebugHighlight = !mCaches.debugOverdraw
1617 && mCaches.debugStencilClip == Caches::kStencilShowHighlight
1618 && mRenderState.stencil().isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001619}
1620
1621void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1622 mDescription.hasTexture = true;
1623 mDescription.hasAlpha8Texture = isAlpha8;
1624}
1625
Romain Guyff316ec2013-02-13 18:39:43 -08001626void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1627 mDescription.hasTexture = true;
1628 mDescription.hasColors = true;
1629 mDescription.hasAlpha8Texture = isAlpha8;
1630}
1631
Romain Guyaa6c24c2011-04-28 18:40:04 -07001632void OpenGLRenderer::setupDrawWithExternalTexture() {
1633 mDescription.hasExternalTexture = true;
1634}
1635
Romain Guy15bc6432011-12-13 13:11:32 -08001636void OpenGLRenderer::setupDrawNoTexture() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001637 mRenderState.meshState().disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001638}
1639
Chris Craik91a8c7c2014-08-12 14:31:35 -07001640void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1641 mDescription.hasVertexAlpha = true;
1642 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001643}
1644
Romain Guy8d0d4782010-12-14 20:13:35 -08001645void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1646 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001647 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1648 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1649 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001650 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001651 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001652}
1653
Romain Guy86568192010-12-14 15:55:39 -08001654void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1655 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001656 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1657 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1658 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001659 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001660 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001661}
1662
Romain Guy41210632012-07-16 17:04:24 -07001663void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1664 mCaches.fontRenderer->describe(mDescription, paint);
1665}
1666
Romain Guy70ca14e2010-12-13 18:24:33 -08001667void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1668 mColorA = a;
1669 mColorR = r;
1670 mColorG = g;
1671 mColorB = b;
1672 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001673 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001674}
1675
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001676void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
Chris Craike84a2082014-12-22 14:28:49 -08001677 if (shader != nullptr) {
Chris Craik117bdbc2015-02-05 10:12:38 -08001678 SkiaShader::describe(&mCaches, mDescription, mCaches.extensions(), *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001679 }
1680}
1681
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001682void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001683 if (filter == nullptr) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001684 return;
1685 }
1686
1687 SkXfermode::Mode mode;
Chris Craike84a2082014-12-22 14:28:49 -08001688 if (filter->asColorMode(nullptr, &mode)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001689 mDescription.colorOp = ProgramDescription::kColorBlend;
1690 mDescription.colorMode = mode;
Chris Craike84a2082014-12-22 14:28:49 -08001691 } else if (filter->asColorMatrix(nullptr)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001692 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001693 }
1694}
1695
Romain Guyf09ef512011-05-27 11:43:46 -07001696void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1697 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1698 mColorA = 1.0f;
1699 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001700 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001701 }
1702}
1703
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001704void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1705 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001706 // When the blending mode is kClear_Mode, we need to use a modulate color
1707 // argb=1,0,0,0
1708 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001709 // TODO: check shader blending, once we have shader drawing support for layers.
Tom Hudson8dfaa492014-12-09 15:03:44 -05001710 bool blend = layer->isBlend()
1711 || getLayerAlpha(layer) < 1.0f
1712 || (mColorSet && mColorA < 1.0f)
1713 || PaintUtils::isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001714 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001715}
1716
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001717void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1718 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001719 // When the blending mode is kClear_Mode, we need to use a modulate color
1720 // argb=1,0,0,0
1721 accountForClear(mode);
Chris Craik03188872015-02-02 18:39:33 -08001722 blend |= (mColorSet && mColorA < 1.0f)
1723 || (getShader(paint) && !getShader(paint)->isOpaque())
1724 || PaintUtils::isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001725 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001726}
1727
1728void OpenGLRenderer::setupDrawProgram() {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001729 mCaches.setProgram(mDescription);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001730 if (mDescription.hasRoundRectClip) {
1731 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001732 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001733 const Rect& innerRect = state->innerRect;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001734 glUniform4f(mCaches.program().getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001735 innerRect.left, innerRect.top,
1736 innerRect.right, innerRect.bottom);
Chris Craik6c15ffa2015-02-02 13:50:55 -08001737 glUniformMatrix4fv(mCaches.program().getUniform("roundRectInvTransform"),
Chris Craikdeeda3d2014-05-05 19:09:33 -07001738 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001739
1740 // add half pixel to round out integer rect space to cover pixel centers
1741 float roundedOutRadius = state->radius + 0.5f;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001742 glUniform1f(mCaches.program().getUniform("roundRectRadius"),
Chris Craik4340c262014-09-11 18:58:45 -07001743 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001744 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001745}
1746
1747void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1748 mTrackDirtyRegions = false;
1749}
1750
Chris Craik4063a0e2013-11-15 16:06:56 -08001751void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1752 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001753 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001754 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001755 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001756 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001757
Romain Guy86568192010-12-14 15:55:39 -08001758 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001759 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001760
1761 mCaches.program().set(currentSnapshot()->getOrthoMatrix(),
Chris Craike84a2082014-12-22 14:28:49 -08001762 mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001763 if (dirty && mTrackDirtyRegions) {
1764 if (!ignoreTransform) {
1765 dirtyLayer(left, top, right, bottom, *currentTransform());
1766 } else {
1767 dirtyLayer(left, top, right, bottom);
1768 }
Romain Guy86568192010-12-14 15:55:39 -08001769 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001770}
1771
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001772void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1773 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001774 mCaches.program().setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001775 }
1776}
1777
Romain Guy86568192010-12-14 15:55:39 -08001778void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001779 if (mSetShaderColor) {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001780 mCaches.program().setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001781 }
1782}
1783
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001784void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
Chris Craike84a2082014-12-22 14:28:49 -08001785 if (shader == nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001786 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001787 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001788
1789 if (ignoreTransform) {
1790 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1791 // because it was built into modelView / the geometry, and the description needs to
1792 // compensate.
1793 mat4 modelViewWithoutTransform;
1794 modelViewWithoutTransform.loadInverse(*currentTransform());
1795 modelViewWithoutTransform.multiply(mModelViewMatrix);
1796 mModelViewMatrix.load(modelViewWithoutTransform);
1797 }
1798
Chris Craik117bdbc2015-02-05 10:12:38 -08001799 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit,
1800 mCaches.extensions(), *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001801}
1802
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001803void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001804 if (nullptr == filter) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001805 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001806 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001807
1808 SkColor color;
1809 SkXfermode::Mode mode;
1810 if (filter->asColorMode(&color, &mode)) {
1811 const int alpha = SkColorGetA(color);
1812 const GLfloat a = alpha / 255.0f;
1813 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1814 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1815 const GLfloat b = a * SkColorGetB(color) / 255.0f;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001816 glUniform4f(mCaches.program().getUniform("colorBlend"), r, g, b, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001817 return;
1818 }
1819
1820 SkScalar srcColorMatrix[20];
1821 if (filter->asColorMatrix(srcColorMatrix)) {
1822
1823 float colorMatrix[16];
1824 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1825 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1826 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1827 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1828
1829 // Skia uses the range [0..255] for the addition vector, but we need
1830 // the [0..1] range to apply the vector in GLSL
1831 float colorVector[4];
1832 colorVector[0] = srcColorMatrix[4] / 255.0f;
1833 colorVector[1] = srcColorMatrix[9] / 255.0f;
1834 colorVector[2] = srcColorMatrix[14] / 255.0f;
1835 colorVector[3] = srcColorMatrix[19] / 255.0f;
1836
Chris Craik6c15ffa2015-02-02 13:50:55 -08001837 glUniformMatrix4fv(mCaches.program().getUniform("colorMatrix"), 1,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001838 GL_FALSE, colorMatrix);
Chris Craik6c15ffa2015-02-02 13:50:55 -08001839 glUniform4fv(mCaches.program().getUniform("colorMatrixVector"), 1, colorVector);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001840 return;
1841 }
1842
1843 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001844}
1845
Romain Guy41210632012-07-16 17:04:24 -07001846void OpenGLRenderer::setupDrawTextGammaUniforms() {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001847 mCaches.fontRenderer->setupProgram(mDescription, mCaches.program());
Romain Guy41210632012-07-16 17:04:24 -07001848}
1849
Romain Guy70ca14e2010-12-13 18:24:33 -08001850void OpenGLRenderer::setupDrawSimpleMesh() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001851 bool force = mRenderState.meshState().bindMeshBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001852 mRenderState.meshState().bindPositionVertexPointer(force, nullptr);
Chris Craik96a5c4c2015-01-27 15:46:35 -08001853 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001854}
1855
1856void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001857 if (texture) mCaches.textureState().bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001858 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001859 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001860}
1861
Romain Guyaa6c24c2011-04-28 18:40:04 -07001862void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001863 mCaches.textureState().bindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001864 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001865 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001866}
1867
Romain Guy8f0095c2011-05-02 17:24:22 -07001868void OpenGLRenderer::setupDrawTextureTransform() {
1869 mDescription.hasTextureTransform = true;
1870}
1871
1872void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001873 glUniformMatrix4fv(mCaches.program().getUniform("mainTextureTransform"), 1,
Romain Guyaa6c24c2011-04-28 18:40:04 -07001874 GL_FALSE, &transform.data[0]);
1875}
1876
Chris Craik564acf72014-01-02 16:46:18 -08001877void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1878 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001879 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001880 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001881 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001882 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001883 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001884 }
Romain Guyd71dd362011-12-12 19:03:35 -08001885
Chris Craik6c15ffa2015-02-02 13:50:55 -08001886 mRenderState.meshState().bindPositionVertexPointer(force, vertices);
1887 if (mCaches.program().texCoords >= 0) {
1888 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001889 }
1890
Chris Craik96a5c4c2015-01-27 15:46:35 -08001891 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -08001892}
1893
Chris Craik564acf72014-01-02 16:46:18 -08001894void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1895 const GLvoid* texCoords, const GLvoid* colors) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001896 bool force = mRenderState.meshState().unbindMeshBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001897 GLsizei stride = sizeof(ColorTextureVertex);
1898
Chris Craik6c15ffa2015-02-02 13:50:55 -08001899 mRenderState.meshState().bindPositionVertexPointer(force, vertices, stride);
1900 if (mCaches.program().texCoords >= 0) {
1901 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords, stride);
Romain Guyff316ec2013-02-13 18:39:43 -08001902 }
Chris Craik6c15ffa2015-02-02 13:50:55 -08001903 int slot = mCaches.program().getAttrib("colors");
Romain Guyff316ec2013-02-13 18:39:43 -08001904 if (slot >= 0) {
1905 glEnableVertexAttribArray(slot);
1906 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1907 }
1908
Chris Craik96a5c4c2015-01-27 15:46:35 -08001909 mRenderState.meshState().unbindIndicesBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001910}
1911
Chris Craik564acf72014-01-02 16:46:18 -08001912void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1913 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001914 bool force = false;
1915 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1916 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
Chris Craik96a5c4c2015-01-27 15:46:35 -08001917 // use the default VBO found in RenderState
Romain Guy3b748a42013-04-17 18:54:38 -07001918 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001919 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy3b748a42013-04-17 18:54:38 -07001920 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001921 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001922 }
Chris Craik96a5c4c2015-01-27 15:46:35 -08001923 mRenderState.meshState().bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001924
Chris Craik6c15ffa2015-02-02 13:50:55 -08001925 mRenderState.meshState().bindPositionVertexPointer(force, vertices);
1926 if (mCaches.program().texCoords >= 0) {
1927 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001928 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001929}
1930
Romain Guy448455f2013-07-22 13:57:50 -07001931void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001932 bool force = mRenderState.meshState().unbindMeshBuffer();
1933 mRenderState.meshState().bindQuadIndicesBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001934 mRenderState.meshState().bindPositionVertexPointer(force, vertices, kVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001935}
1936
Romain Guy70ca14e2010-12-13 18:24:33 -08001937///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001938// Drawing
1939///////////////////////////////////////////////////////////////////////////////
1940
Tom Hudson107843d2014-09-08 11:26:26 -04001941void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001942 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1943 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001944 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001945 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001946 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001947 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001948 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001949 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001950 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001951 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001952 }
1953
Chris Craikef8d6f22014-12-17 11:10:28 -08001954 // Don't avoid overdraw when visualizing, since that makes it harder to
1955 // debug where it's coming from, and when the problem occurs.
1956 bool avoidOverdraw = !mCaches.debugOverdraw;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001957 DeferredDisplayList deferredList(mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001958 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001959 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001960
1961 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001962 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001963
Tom Hudson107843d2014-09-08 11:26:26 -04001964 deferredList.flush(*this, dirty);
1965 } else {
1966 // Even if there is no drawing command(Ex: invisible),
1967 // it still needs startFrame to clear buffer and start tiling.
1968 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001969 }
1970}
1971
Chris Craike84a2082014-12-22 14:28:49 -08001972void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top,
1973 const SkPaint* paint) {
Romain Guya168d732011-03-18 16:50:13 -07001974 float x = left;
1975 float y = top;
1976
Romain Guy886b2752013-01-04 12:26:18 -08001977 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1978
Romain Guya168d732011-03-18 16:50:13 -07001979 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001980 if (currentTransform()->isPureTranslate()) {
1981 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1982 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001983 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001984
1985 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001986 } else {
Chris Craik678625242014-02-28 12:26:34 -08001987 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001988 }
1989
Romain Guy3b748a42013-04-17 18:54:38 -07001990 // No need to check for a UV mapper on the texture object, only ARGB_8888
1991 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001992 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Chris Craik96a5c4c2015-01-27 15:46:35 -08001993 paint, (GLvoid*) nullptr, (GLvoid*) kMeshTextureOffset,
Chris Craik117bdbc2015-02-05 10:12:38 -08001994 GL_TRIANGLE_STRIP, kUnitQuadCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001995}
1996
Romain Guy03c00b52013-06-20 18:30:28 -07001997/**
1998 * Important note: this method is intended to draw batches of bitmaps and
1999 * will not set the scissor enable or dirty the current layer, if any.
2000 * The caller is responsible for properly dirtying the current layer.
2001 */
Tom Hudson107843d2014-09-08 11:26:26 -04002002void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002003 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
2004 const Rect& bounds, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002005 mCaches.textureState().activateTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002006 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002007 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07002008
Chris Craik527a3aa2013-03-04 10:19:31 -08002009 const AutoTexture autoCleanup(texture);
2010
Chris Craik527a3aa2013-03-04 10:19:31 -08002011 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002012 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002013
2014 const float x = (int) floorf(bounds.left + 0.5f);
2015 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04002016 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002017 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002018 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002019 GL_TRIANGLES, bitmapCount * 6, true,
2020 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002021 } else {
2022 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002023 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002024 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2025 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002026 }
2027
Tom Hudson107843d2014-09-08 11:26:26 -04002028 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08002029}
2030
Tom Hudson107843d2014-09-08 11:26:26 -04002031void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002032 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002033 return;
Romain Guy6926c722010-07-12 20:20:03 -07002034 }
2035
Chris Craik44eb2c02015-01-29 09:45:09 -08002036 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002037 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002038 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002039 const AutoTexture autoCleanup(texture);
2040
Mike Reed1103b322014-07-08 12:36:44 -04002041 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002042 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002043 } else {
Chris Craik79647502014-08-06 13:42:24 -07002044 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002045 }
Chet Haase48659092012-05-31 15:21:51 -07002046
Tom Hudson107843d2014-09-08 11:26:26 -04002047 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002048}
2049
Tom Hudson107843d2014-09-08 11:26:26 -04002050void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002051 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002052 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002053 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002054 }
2055
Chris Craik39a908c2013-06-13 14:39:01 -07002056 // TODO: use quickReject on bounds from vertices
Chris Craik65fe5ee2015-01-26 18:06:29 -08002057 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002058
Romain Guyb18d2d02011-02-10 15:52:54 -08002059 float left = FLT_MAX;
2060 float top = FLT_MAX;
2061 float right = FLT_MIN;
2062 float bottom = FLT_MIN;
2063
Romain Guya92bb4d2012-10-16 11:08:44 -07002064 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002065
Chris Craik51d6a3d2014-12-22 17:16:56 -08002066 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[count]);
2067 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08002068
Chris Craik51d6a3d2014-12-22 17:16:56 -08002069 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002070 if (!colors) {
2071 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08002072 tempColors.reset(new int[colorsCount]);
2073 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
2074 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08002075 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002076
Chris Craik44eb2c02015-01-29 09:45:09 -08002077 mCaches.textureState().activateTexture(0);
John Reckebd52612014-12-10 16:47:36 -08002078 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002079 const UvMapper& mapper(getMapper(texture));
2080
Romain Guy5a7b4662011-01-20 19:09:30 -08002081 for (int32_t y = 0; y < meshHeight; y++) {
2082 for (int32_t x = 0; x < meshWidth; x++) {
2083 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2084
2085 float u1 = float(x) / meshWidth;
2086 float u2 = float(x + 1) / meshWidth;
2087 float v1 = float(y) / meshHeight;
2088 float v2 = float(y + 1) / meshHeight;
2089
Romain Guy3b748a42013-04-17 18:54:38 -07002090 mapper.map(u1, v1, u2, v2);
2091
Romain Guy5a7b4662011-01-20 19:09:30 -08002092 int ax = i + (meshWidth + 1) * 2;
2093 int ay = ax + 1;
2094 int bx = i;
2095 int by = bx + 1;
2096 int cx = i + 2;
2097 int cy = cx + 1;
2098 int dx = i + (meshWidth + 1) * 2 + 2;
2099 int dy = dx + 1;
2100
Romain Guyff316ec2013-02-13 18:39:43 -08002101 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2102 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2103 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002104
Romain Guyff316ec2013-02-13 18:39:43 -08002105 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2106 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2107 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002108
Romain Guya92bb4d2012-10-16 11:08:44 -07002109 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2110 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2111 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2112 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002113 }
2114 }
2115
Chris Craikf0a59072013-11-19 18:00:46 -08002116 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002117 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002118 }
2119
Romain Guyff316ec2013-02-13 18:39:43 -08002120 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002121 texture = mCaches.textureCache.get(bitmap);
2122 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04002123 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002124 }
Romain Guyff316ec2013-02-13 18:39:43 -08002125 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002126 const AutoTexture autoCleanup(texture);
2127
2128 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002129 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002130
2131 int alpha;
2132 SkXfermode::Mode mode;
2133 getAlphaAndMode(paint, &alpha, &mode);
2134
Romain Guyff316ec2013-02-13 18:39:43 -08002135 float a = alpha / 255.0f;
2136
Romain Guya92bb4d2012-10-16 11:08:44 -07002137 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002138 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002139 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002140
Romain Guyff316ec2013-02-13 18:39:43 -08002141 setupDraw();
2142 setupDrawWithTextureAndColor();
2143 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002144 setupDrawColorFilter(getColorFilter(paint));
2145 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002146 setupDrawProgram();
2147 setupDrawDirtyRegionsDisabled();
Chris Craik117bdbc2015-02-05 10:12:38 -08002148 setupDrawModelView(kModelViewMode_Translate, false, 0, 0, 0, 0);
Romain Guyff316ec2013-02-13 18:39:43 -08002149 setupDrawTexture(texture->id);
2150 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002151 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002152 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002153
2154 glDrawArrays(GL_TRIANGLES, 0, count);
2155
Chris Craik6c15ffa2015-02-02 13:50:55 -08002156 int slot = mCaches.program().getAttrib("colors");
Romain Guyff316ec2013-02-13 18:39:43 -08002157 if (slot >= 0) {
2158 glDisableVertexAttribArray(slot);
2159 }
2160
Tom Hudson107843d2014-09-08 11:26:26 -04002161 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002162}
2163
Tom Hudson107843d2014-09-08 11:26:26 -04002164void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002165 float srcLeft, float srcTop, float srcRight, float srcBottom,
2166 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002167 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002168 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002169 return;
Romain Guy6926c722010-07-12 20:20:03 -07002170 }
2171
Chris Craik44eb2c02015-01-29 09:45:09 -08002172 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002173 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002174 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002175 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002176
Romain Guy8ba548f2010-06-30 19:21:21 -07002177 const float width = texture->width;
2178 const float height = texture->height;
2179
Romain Guy3b748a42013-04-17 18:54:38 -07002180 float u1 = fmax(0.0f, srcLeft / width);
2181 float v1 = fmax(0.0f, srcTop / height);
2182 float u2 = fmin(1.0f, srcRight / width);
2183 float v2 = fmin(1.0f, srcBottom / height);
2184
2185 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002186
Chris Craik96a5c4c2015-01-27 15:46:35 -08002187 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002188 resetDrawTextureTexCoords(u1, v1, u2, v2);
2189
Romain Guyd21b6e12011-11-30 20:21:23 -08002190 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2191
Romain Guy886b2752013-01-04 12:26:18 -08002192 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2193 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002194
Romain Guy886b2752013-01-04 12:26:18 -08002195 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2196 // Apply a scale transform on the canvas only when a shader is in use
2197 // Skia handles the ratio between the dst and src rects as a scale factor
2198 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002199 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002200 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002201
Chris Craikd6b65f62014-01-01 14:45:21 -08002202 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2203 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2204 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002205
2206 dstRight = x + (dstRight - dstLeft);
2207 dstBottom = y + (dstBottom - dstTop);
2208
2209 dstLeft = x;
2210 dstTop = y;
2211
Chris Craik678625242014-02-28 12:26:34 -08002212 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002213 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002214 } else {
Chris Craik678625242014-02-28 12:26:34 -08002215 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002216 }
2217
2218 if (CC_UNLIKELY(useScaleTransform)) {
2219 save(SkCanvas::kMatrix_SaveFlag);
2220 translate(dstLeft, dstTop);
2221 scale(scaleX, scaleY);
2222
2223 dstLeft = 0.0f;
2224 dstTop = 0.0f;
2225
2226 dstRight = srcRight - srcLeft;
2227 dstBottom = srcBottom - srcTop;
2228 }
2229
Mike Reed1103b322014-07-08 12:36:44 -04002230 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002231 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002232 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002233 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik117bdbc2015-02-05 10:12:38 -08002234 GL_TRIANGLE_STRIP, kUnitQuadCount, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002235 } else {
2236 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002237 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002238 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik117bdbc2015-02-05 10:12:38 -08002239 GL_TRIANGLE_STRIP, kUnitQuadCount, false, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002240 }
2241
2242 if (CC_UNLIKELY(useScaleTransform)) {
2243 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002244 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002245
2246 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002247
Tom Hudson107843d2014-09-08 11:26:26 -04002248 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002249}
2250
Tom Hudson107843d2014-09-08 11:26:26 -04002251void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002252 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002253 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002254 return;
Romain Guy6926c722010-07-12 20:20:03 -07002255 }
2256
John Reckebd52612014-12-10 16:47:36 -08002257 AssetAtlas::Entry* entry = mRenderState.assetAtlas().getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002258 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2259 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002260
Tom Hudson107843d2014-09-08 11:26:26 -04002261 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002262}
2263
Tom Hudson107843d2014-09-08 11:26:26 -04002264void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002265 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2266 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002267 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002268 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002269 }
2270
Romain Guy211370f2012-02-01 16:10:55 -08002271 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002272 mCaches.textureState().activateTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002273 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002274 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002275 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002276
Romain Guya4adcf02013-02-28 12:15:35 -08002277 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2278 texture->setFilter(GL_LINEAR, true);
2279
Chris Craikd6b65f62014-01-01 14:45:21 -08002280 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002281 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002282 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002283 const float offsetX = left + currentTransform()->getTranslateX();
2284 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002285 const size_t count = mesh->quads.size();
2286 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002287 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002288 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002289 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2290 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2291 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002292 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002293 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002294 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002295 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002296 }
2297 }
2298
Chris Craik4063a0e2013-11-15 16:06:56 -08002299 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002300 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002301 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2302 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002303
Romain Guy3b748a42013-04-17 18:54:38 -07002304 right = x + right - left;
2305 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002306 left = x;
2307 top = y;
2308 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002309 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002310 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2311 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002312 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2313 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002314 }
Chet Haase48659092012-05-31 15:21:51 -07002315
Tom Hudson107843d2014-09-08 11:26:26 -04002316 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002317}
2318
Romain Guy03c00b52013-06-20 18:30:28 -07002319/**
2320 * Important note: this method is intended to draw batches of 9-patch objects and
2321 * will not set the scissor enable or dirty the current layer, if any.
2322 * The caller is responsible for properly dirtying the current layer.
2323 */
Tom Hudson107843d2014-09-08 11:26:26 -04002324void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002325 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002326 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07002327 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002328 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002329 const AutoTexture autoCleanup(texture);
2330
2331 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2332 texture->setFilter(GL_LINEAR, true);
2333
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002334 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2335 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002336 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002337
Tom Hudson107843d2014-09-08 11:26:26 -04002338 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002339}
2340
Tom Hudson107843d2014-09-08 11:26:26 -04002341void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002342 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002343 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002344 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002345 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002346 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002347 }
2348
Chris Craik117bdbc2015-02-05 10:12:38 -08002349 if (!paint->getShader() && !currentSnapshot()->roundRectClipState) {
2350 Glop glop;
2351 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
2352 bool fudgeOffset = displayFlags & kVertexBuffer_Offset;
2353 bool shadowInterp = displayFlags & kVertexBuffer_ShadowInterp;
2354 aBuilder.setMeshVertexBuffer(vertexBuffer, shadowInterp)
2355 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), fudgeOffset)
2356 .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds())
2357 .setPaint(paint, currentSnapshot()->alpha)
2358 .build();
2359 renderGlop(glop);
2360 return;
2361 }
2362
2363
2364 const VertexBuffer::MeshFeatureFlags meshFeatureFlags = vertexBuffer.getMeshFeatureFlags();
Chris Craik0d5ac952014-07-15 13:01:02 -07002365 Rect bounds(vertexBuffer.getBounds());
2366 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002367 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2368
Chris Craikcb4d6002012-09-25 12:00:29 -07002369 int color = paint->getColor();
Chris Craik117bdbc2015-02-05 10:12:38 -08002370 bool isAA = meshFeatureFlags & VertexBuffer::kAlpha;
Chris Craikcb4d6002012-09-25 12:00:29 -07002371
Chris Craik710f46d2012-09-17 17:25:49 -07002372 setupDraw();
2373 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002374 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Chris Craik117bdbc2015-02-05 10:12:38 -08002375 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002376 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002377 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002378 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002379 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002380 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2381 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002382 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002383 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002384 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002385
ztenghui55bfb4e2013-12-03 10:38:55 -08002386 const void* vertices = vertexBuffer.getBuffer();
Chris Craik96a5c4c2015-01-27 15:46:35 -08002387 mRenderState.meshState().unbindMeshBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08002388 mRenderState.meshState().bindPositionVertexPointer(true, vertices,
2389 isAA ? kAlphaVertexStride : kVertexStride);
Chris Craik96a5c4c2015-01-27 15:46:35 -08002390 mRenderState.meshState().resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002391
Chris Craik710f46d2012-09-17 17:25:49 -07002392 int alphaSlot = -1;
2393 if (isAA) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002394 void* alphaCoords = ((GLbyte*) vertices) + kVertexAlphaOffset;
Chris Craik6c15ffa2015-02-02 13:50:55 -08002395 alphaSlot = mCaches.program().getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002396 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002397 glEnableVertexAttribArray(alphaSlot);
Chris Craik96a5c4c2015-01-27 15:46:35 -08002398 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, kAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002399 }
Romain Guy04299382012-07-18 17:15:41 -07002400
Chris Craik117bdbc2015-02-05 10:12:38 -08002401 if (meshFeatureFlags & VertexBuffer::kIndices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002402 mRenderState.meshState().unbindIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002403 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(),
2404 GL_UNSIGNED_SHORT, vertexBuffer.getIndices());
Chris Craik117bdbc2015-02-05 10:12:38 -08002405 } else {
2406 mRenderState.meshState().unbindIndicesBuffer();
2407 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
ztenghui63d41ab2014-02-14 13:13:41 -08002408 }
Romain Guy04299382012-07-18 17:15:41 -07002409
Chris Craik710f46d2012-09-17 17:25:49 -07002410 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002411 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002412 }
Chris Craik65cd6122012-12-10 17:56:27 -08002413
Tom Hudson107843d2014-09-08 11:26:26 -04002414 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002415}
2416
2417/**
Chris Craik65cd6122012-12-10 17:56:27 -08002418 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2419 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2420 * screen space in all directions. However, instead of using a fragment shader to compute the
2421 * translucency of the color from its position, we simply use a varying parameter to define how far
2422 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2423 *
2424 * Doesn't yet support joins, caps, or path effects.
2425 */
Tom Hudson107843d2014-09-08 11:26:26 -04002426void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002427 VertexBuffer vertexBuffer;
2428 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002429 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002430 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002431}
2432
2433/**
2434 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2435 * and additional geometry for defining an alpha slope perimeter.
2436 *
2437 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2438 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2439 * in-shader alpha region, but found it to be taxing on some GPUs.
2440 *
2441 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2442 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002443 */
Tom Hudson107843d2014-09-08 11:26:26 -04002444void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002445 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002446
Chris Craik65cd6122012-12-10 17:56:27 -08002447 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002448
Chris Craik65cd6122012-12-10 17:56:27 -08002449 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002450 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2451 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002452
Chris Craik05f3d6e2014-06-02 16:27:04 -07002453 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002454 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002455 }
2456
Chris Craikbf759452014-08-11 16:00:44 -07002457 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002458 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002459}
2460
Tom Hudson107843d2014-09-08 11:26:26 -04002461void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002462 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002463
Chris Craik6d29c8d2013-05-08 18:35:44 -07002464 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002465
Chris Craik6d29c8d2013-05-08 18:35:44 -07002466 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002467 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002468
Chris Craik05f3d6e2014-06-02 16:27:04 -07002469 const Rect& bounds = buffer.getBounds();
2470 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002471 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002472 }
2473
Chris Craikbf759452014-08-11 16:00:44 -07002474 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002475 drawVertexBuffer(buffer, paint, displayFlags);
2476
2477 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002478}
2479
Tom Hudson107843d2014-09-08 11:26:26 -04002480void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002481 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002482 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002483
Rob Tsuk487a92c2015-01-06 13:22:54 -08002484 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002485 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002486
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002487 SkPaint paint;
2488 paint.setColor(color);
2489 paint.setXfermodeMode(mode);
2490
2491 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002492
Tom Hudson107843d2014-09-08 11:26:26 -04002493 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002494}
Romain Guy9d5316e2010-06-24 19:30:36 -07002495
Tom Hudson107843d2014-09-08 11:26:26 -04002496void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002497 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002498 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002499 const AutoTexture autoCleanup(texture);
2500
2501 const float x = left + texture->left - texture->offset;
2502 const float y = top + texture->top - texture->offset;
2503
2504 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002505
Tom Hudson107843d2014-09-08 11:26:26 -04002506 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002507}
2508
Tom Hudson107843d2014-09-08 11:26:26 -04002509void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002510 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002511 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002512 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002513 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002514 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002515 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002516
Chris Craike84a2082014-12-22 14:28:49 -08002517 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002518 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002519 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002520 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002521 drawShape(left, top, texture, p);
2522 } else {
2523 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2524 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2525 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002526 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002527}
2528
Tom Hudson107843d2014-09-08 11:26:26 -04002529void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002530 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002531 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002532 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002533 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002534 }
Chris Craike84a2082014-12-22 14:28:49 -08002535 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002536 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002537 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002538 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002539 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002540 SkPath path;
2541 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2542 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2543 } else {
2544 path.addCircle(x, y, radius);
2545 }
2546 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002547 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002548}
Romain Guy01d58e42011-01-19 21:54:02 -08002549
Tom Hudson107843d2014-09-08 11:26:26 -04002550void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002551 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002552 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002553 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002554 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002555 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002556 }
Romain Guy01d58e42011-01-19 21:54:02 -08002557
Chris Craike84a2082014-12-22 14:28:49 -08002558 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002559 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002560 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002561 drawShape(left, top, texture, p);
2562 } else {
2563 SkPath path;
2564 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2565 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2566 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2567 }
2568 path.addOval(rect);
2569 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002570 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002571}
2572
Tom Hudson107843d2014-09-08 11:26:26 -04002573void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002574 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002575 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002576 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002577 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002578 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002579 }
2580
Chris Craik780c1282012-10-04 14:10:49 -07002581 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08002582 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002583 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002584 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002585 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002586 drawShape(left, top, texture, p);
2587 return;
Chris Craik780c1282012-10-04 14:10:49 -07002588 }
Chris Craik780c1282012-10-04 14:10:49 -07002589 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2590 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2591 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2592 }
2593
2594 SkPath path;
2595 if (useCenter) {
2596 path.moveTo(rect.centerX(), rect.centerY());
2597 }
2598 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2599 if (useCenter) {
2600 path.close();
2601 }
Tom Hudson107843d2014-09-08 11:26:26 -04002602 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002603}
2604
Romain Guycf8675e2012-10-02 12:32:25 -07002605// See SkPaintDefaults.h
2606#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2607
Tom Hudson107843d2014-09-08 11:26:26 -04002608void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002609 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002610 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002611 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002612 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002613 return;
Romain Guy6926c722010-07-12 20:20:03 -07002614 }
2615
Chris Craik710f46d2012-09-17 17:25:49 -07002616 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002617 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08002618 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07002619 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002620 mCaches.textureState().activateTexture(0);
Romain Guycf8675e2012-10-02 12:32:25 -07002621 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002622 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002623 drawShape(left, top, texture, p);
2624 } else {
2625 SkPath path;
2626 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2627 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2628 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2629 }
2630 path.addRect(rect);
2631 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002632 }
Chet Haase858aa932011-05-12 09:06:00 -07002633 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002634 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2635 SkPath path;
2636 path.addRect(left, top, right, bottom);
2637 drawConvexPath(path, p);
2638 } else {
2639 drawColorRect(left, top, right, bottom, p);
2640
2641 mDirty = true;
2642 }
Chet Haase858aa932011-05-12 09:06:00 -07002643 }
Romain Guyc7d53492010-06-25 13:41:57 -07002644}
Romain Guy9d5316e2010-06-24 19:30:36 -07002645
Chris Craikd218a922014-01-02 17:13:34 -08002646void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2647 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002648 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002649 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07002650
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002651 TextShadow textShadow;
2652 if (!getTextShadow(paint, &textShadow)) {
2653 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2654 }
2655
Raph Levien416a8472012-07-19 22:48:17 -07002656 // NOTE: The drop shadow will not perform gamma correction
2657 // if shader-based correction is enabled
2658 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2659 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002660 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002661 // If the drop shadow exceeds the max texture size or couldn't be
2662 // allocated, skip drawing
2663 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002664 const AutoTexture autoCleanup(shadow);
2665
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002666 const float sx = x - shadow->left + textShadow.dx;
2667 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002668
Tom Hudson984162f2014-10-10 13:38:16 -04002669 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002670 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002671 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002672 }
2673
2674 setupDraw();
2675 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002676 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002677 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002678 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002679 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002680 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002681 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2682 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002683 setupDrawTexture(shadow->id);
2684 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002685 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002686 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08002687 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Raph Levien416a8472012-07-19 22:48:17 -07002688
Chris Craik117bdbc2015-02-05 10:12:38 -08002689 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Raph Levien416a8472012-07-19 22:48:17 -07002690}
2691
Romain Guy768bffc2013-02-27 13:50:45 -08002692bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002693 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05002694 return MathUtils::isZero(alpha)
2695 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002696}
2697
Tom Hudson107843d2014-09-08 11:26:26 -04002698void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002699 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002700 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002701 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002702 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002703
Romain Guy671d6cf2012-01-18 12:39:17 -08002704 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002705 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002706 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002707 }
2708
Chris Craik65fe5ee2015-01-26 18:06:29 -08002709 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002710
Romain Guy671d6cf2012-01-18 12:39:17 -08002711 float x = 0.0f;
2712 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002713 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002714 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002715 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2716 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002717 }
2718
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002719 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002720 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002721
2722 int alpha;
2723 SkXfermode::Mode mode;
2724 getAlphaAndMode(paint, &alpha, &mode);
2725
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002726 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002727 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002728 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002729 }
2730
Romain Guy671d6cf2012-01-18 12:39:17 -08002731 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002732 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002733 if (pureTranslate && !linearFilter) {
2734 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2735 }
Romain Guy257ae352013-03-20 16:31:12 -07002736 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002737
Rob Tsuk487a92c2015-01-06 13:22:54 -08002738 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002739 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2740
Romain Guy211370f2012-02-01 16:10:55 -08002741 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002742
Victoria Lease1e546812013-06-25 14:25:17 -07002743 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002744 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002745 positions, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002746 if (hasActiveLayer) {
2747 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002748 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002749 }
2750 dirtyLayerUnchecked(bounds, getRegion());
2751 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002752 }
Chet Haase48659092012-05-31 15:21:51 -07002753
Tom Hudson107843d2014-09-08 11:26:26 -04002754 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002755}
2756
Chris Craik59744b72014-07-01 17:56:52 -07002757bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002758 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002759 outMatrix->setIdentity();
2760 return false;
2761 } else if (CC_UNLIKELY(transform.isPerspective())) {
2762 outMatrix->setIdentity();
2763 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002764 }
Chris Craik59744b72014-07-01 17:56:52 -07002765
2766 /**
Chris Craika736cd92014-08-04 13:18:38 -07002767 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2768 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002769 */
2770 float sx, sy;
2771 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002772 outMatrix->setScale(
2773 roundf(fmaxf(1.0f, sx)),
2774 roundf(fmaxf(1.0f, sy)));
2775 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002776}
2777
Tom Hudson984162f2014-10-10 13:38:16 -04002778int OpenGLRenderer::getSaveCount() const {
2779 return mState.getSaveCount();
2780}
2781
2782int OpenGLRenderer::save(int flags) {
2783 return mState.save(flags);
2784}
2785
2786void OpenGLRenderer::restore() {
2787 return mState.restore();
2788}
2789
2790void OpenGLRenderer::restoreToCount(int saveCount) {
2791 return mState.restoreToCount(saveCount);
2792}
2793
2794void OpenGLRenderer::translate(float dx, float dy, float dz) {
2795 return mState.translate(dx, dy, dz);
2796}
2797
2798void OpenGLRenderer::rotate(float degrees) {
2799 return mState.rotate(degrees);
2800}
2801
2802void OpenGLRenderer::scale(float sx, float sy) {
2803 return mState.scale(sx, sy);
2804}
2805
2806void OpenGLRenderer::skew(float sx, float sy) {
2807 return mState.skew(sx, sy);
2808}
2809
2810void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2811 mState.setMatrix(matrix);
2812}
2813
2814void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2815 mState.concatMatrix(matrix);
2816}
2817
2818bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2819 return mState.clipRect(left, top, right, bottom, op);
2820}
2821
2822bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2823 return mState.clipPath(path, op);
2824}
2825
2826bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2827 return mState.clipRegion(region, op);
2828}
2829
2830void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2831 mState.setClippingOutline(allocator, outline);
2832}
2833
2834void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2835 const Rect& rect, float radius, bool highPriority) {
2836 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2837}
2838
Tom Hudson107843d2014-09-08 11:26:26 -04002839void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002840 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002841 DrawOpMode drawOpMode) {
2842
Chris Craik527a3aa2013-03-04 10:19:31 -08002843 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002844 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2845 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002846 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002847 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002848 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002849 }
Romain Guycac5fd32011-12-01 20:08:50 -08002850 }
2851
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002852 const float oldX = x;
2853 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002854
Chris Craikd6b65f62014-01-01 14:45:21 -08002855 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002856 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002857
Romain Guy211370f2012-02-01 16:10:55 -08002858 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002859 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2860 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002861 }
2862
Romain Guy86568192010-12-14 15:55:39 -08002863 int alpha;
2864 SkXfermode::Mode mode;
2865 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002866
Romain Guyc74f45a2013-02-26 19:10:14 -08002867 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2868
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002869 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002870 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002871 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002872 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002873 }
2874
Romain Guy19d4dd82013-03-04 11:14:26 -08002875 const bool hasActiveLayer = hasLayer();
2876
Romain Guy624234f2013-03-05 16:43:31 -08002877 // We only pass a partial transform to the font renderer. That partial
2878 // matrix defines how glyphs are rasterized. Typically we want glyphs
2879 // to be rasterized at their final size on screen, which means the partial
2880 // matrix needs to take the scale factor into account.
2881 // When a partial matrix is used to transform glyphs during rasterization,
2882 // the mesh is generated with the inverse transform (in the case of scale,
2883 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2884 // apply the full transform matrix at draw time in the vertex shader.
2885 // Applying the full matrix in the shader is the easiest way to handle
2886 // rotation and perspective and allows us to always generated quads in the
2887 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002888 SkMatrix fontTransform;
2889 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2890 || fabs(y - (int) y) > 0.0f
2891 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002892 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002893 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002894
Romain Guy624234f2013-03-05 16:43:31 -08002895 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08002896 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002897 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 -07002898
Raph Levien996e57c2012-07-23 15:22:52 -07002899 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002900 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002901
2902 // don't call issuedrawcommand, do it at end of batch
2903 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002904 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002905 SkPaint paintCopy(*paint);
2906 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2907 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002908 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002909 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002910 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002911 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002912 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002913
Chris Craik527a3aa2013-03-04 10:19:31 -08002914 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002915 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002916 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002917 }
Chris Craik41541822013-05-03 16:35:54 -07002918 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002919 }
Romain Guy694b5192010-07-21 21:33:20 -07002920
Chris Craike63f7c622013-10-17 10:30:55 -07002921 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002922
Tom Hudson107843d2014-09-08 11:26:26 -04002923 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002924}
2925
Tom Hudson107843d2014-09-08 11:26:26 -04002926void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002927 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002928 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002929 return;
Romain Guy03d58522012-02-24 17:54:07 -08002930 }
2931
Chris Craik39a908c2013-06-13 14:39:01 -07002932 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002933 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002934
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002935 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002936 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002937 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002938
2939 int alpha;
2940 SkXfermode::Mode mode;
2941 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002942 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002943
Tom Hudson984162f2014-10-10 13:38:16 -04002944 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002945 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 -08002946
Romain Guy97771732012-02-28 18:17:02 -08002947 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002948
2949 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chris Craike84a2082014-12-22 14:28:49 -08002950 hOffset, vOffset, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002951 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002952 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002953 dirtyLayerUnchecked(bounds, getRegion());
2954 }
Romain Guy97771732012-02-28 18:17:02 -08002955 }
Chet Haase48659092012-05-31 15:21:51 -07002956
Tom Hudson107843d2014-09-08 11:26:26 -04002957 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002958}
2959
Tom Hudson107843d2014-09-08 11:26:26 -04002960void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002961 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002962
Chris Craik44eb2c02015-01-29 09:45:09 -08002963 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002964
Romain Guyfb8b7632010-08-23 21:05:08 -07002965 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002966 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002967 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002968
Romain Guy8b55f372010-08-18 17:10:07 -07002969 const float x = texture->left - texture->offset;
2970 const float y = texture->top - texture->offset;
2971
Romain Guy01d58e42011-01-19 21:54:02 -08002972 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002973 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002974}
2975
Tom Hudson107843d2014-09-08 11:26:26 -04002976void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002977 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002978 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002979 }
2980
Chris Craike84a2082014-12-22 14:28:49 -08002981 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002982 if (layer->isTextureLayer()) {
2983 transform = &layer->getTransform();
2984 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002985 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002986 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002987 }
2988 }
2989
Chris Craik39a908c2013-06-13 14:39:01 -07002990 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08002991 const bool rejected = mState.calculateQuickRejectForScissor(
2992 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2993 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002994
2995 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002996 if (transform && !transform->isIdentity()) {
2997 restore();
2998 }
Tom Hudson107843d2014-09-08 11:26:26 -04002999 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08003000 }
3001
Chris Craik62d307c2014-07-29 10:35:13 -07003002 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
3003 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
3004
Romain Guy5bb3c732012-11-29 17:52:58 -08003005 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003006
Chris Craik65fe5ee2015-01-26 18:06:29 -08003007 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08003008 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003009
Romain Guy211370f2012-02-01 16:10:55 -08003010 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08003011 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003012 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3013 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003014 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003015
Chris Craik16ecda52013-03-29 10:59:59 -07003016 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003017 setupDraw();
3018 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003019 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003020 setupDrawColorFilter(layer->getColorFilter());
3021 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003022 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003023 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003024 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07003025 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08003026 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3027 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3028 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003029
Romain Guyd21b6e12011-11-30 20:21:23 -08003030 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003031 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003032 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003033 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003034 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003035 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003036 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3037 }
Romain Guyf219da52011-01-16 12:54:25 -08003038
Romain Guy448455f2013-07-22 13:57:50 -07003039 TextureVertex* mesh = &layer->mesh[0];
3040 GLsizei elementsCount = layer->meshElementCount;
3041
3042 while (elementsCount > 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08003043 GLsizei drawCount = min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Romain Guy448455f2013-07-22 13:57:50 -07003044
Romain Guy3380cfd2013-08-15 16:57:57 -07003045 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003046 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
Chris Craike84a2082014-12-22 14:28:49 -08003047 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr));
Romain Guy448455f2013-07-22 13:57:50 -07003048
3049 elementsCount -= drawCount;
3050 // Though there are 4 vertices in a quad, we use 6 indices per
3051 // quad to draw with GL_TRIANGLES
3052 mesh += (drawCount / 6) * 4;
3053 }
Romain Guyf219da52011-01-16 12:54:25 -08003054
Romain Guy3a3133d2011-02-01 22:59:58 -08003055#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003056 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003057#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003058 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003059
Romain Guy5bb3c732012-11-29 17:52:58 -08003060 if (layer->debugDrawUpdate) {
3061 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003062
3063 SkPaint paint;
3064 paint.setColor(0x7f00ff00);
3065 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003066 }
Romain Guyf219da52011-01-16 12:54:25 -08003067 }
Chris Craik34416ea2013-04-15 16:08:28 -07003068 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003069
Romain Guyb2e2f242012-10-17 18:18:35 -07003070 if (transform && !transform->isIdentity()) {
3071 restore();
3072 }
3073
Tom Hudson107843d2014-09-08 11:26:26 -04003074 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003075}
3076
Romain Guy6926c722010-07-12 20:20:03 -07003077///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003078// Draw filters
3079///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003080void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3081 // We should never get here since we apply the draw filter when stashing
3082 // the paints in the DisplayList.
3083 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003084}
3085
3086///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003087// Drawing implementation
3088///////////////////////////////////////////////////////////////////////////////
3089
Chris Craikd218a922014-01-02 17:13:34 -08003090Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003091 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003092 if (!texture) {
3093 return mCaches.textureCache.get(bitmap);
3094 }
3095 return texture;
3096}
3097
Romain Guy01d58e42011-01-19 21:54:02 -08003098void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003099 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003100 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003101 return;
3102 }
3103
3104 int alpha;
3105 SkXfermode::Mode mode;
3106 getAlphaAndMode(paint, &alpha, &mode);
3107
3108 setupDraw();
3109 setupDrawWithTexture(true);
3110 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003111 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003112 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003113 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003114 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003115 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3116 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003117 setupDrawTexture(texture->id);
3118 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003119 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003120 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08003121 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Romain Guy01d58e42011-01-19 21:54:02 -08003122
Chris Craik117bdbc2015-02-05 10:12:38 -08003123 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003124}
3125
Romain Guyf607bdc2010-09-10 19:20:06 -07003126// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003127#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3128#define kStdUnderline_Offset (1.0f / 9.0f)
3129#define kStdUnderline_Thickness (1.0f / 18.0f)
3130
Chris Craikd218a922014-01-02 17:13:34 -08003131void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3132 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003133 // Handle underline and strike-through
3134 uint32_t flags = paint->getFlags();
3135 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003136 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003137
Romain Guy211370f2012-02-01 16:10:55 -08003138 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003139 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003140 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003141
Raph Levien8b4072d2012-07-30 15:50:00 -07003142 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003143 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003144
Romain Guyf6834472011-01-23 13:32:12 -08003145 int linesCount = 0;
3146 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3147 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3148
3149 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003150 float points[pointsCount];
3151 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003152
3153 if (flags & SkPaint::kUnderlineText_Flag) {
3154 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003155 points[currentPoint++] = left;
3156 points[currentPoint++] = top;
3157 points[currentPoint++] = left + underlineWidth;
3158 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003159 }
3160
3161 if (flags & SkPaint::kStrikeThruText_Flag) {
3162 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003163 points[currentPoint++] = left;
3164 points[currentPoint++] = top;
3165 points[currentPoint++] = left + underlineWidth;
3166 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003167 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003168
Romain Guy726aeba2011-06-01 14:52:00 -07003169 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003170
Romain Guy726aeba2011-06-01 14:52:00 -07003171 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003172 }
3173 }
3174}
3175
Tom Hudson107843d2014-09-08 11:26:26 -04003176void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003177 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003178 return;
Romain Guy672433d2013-01-04 19:05:13 -08003179 }
3180
Tom Hudson107843d2014-09-08 11:26:26 -04003181 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003182}
3183
Tom Hudson107843d2014-09-08 11:26:26 -04003184void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003185 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003186 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003187
Chris Craik15a07a22014-01-26 13:43:53 -08003188 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08003189 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07003190
3191 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003192 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003193
ztenghui14a4e352014-08-13 10:44:39 -07003194 // The caller has made sure casterAlpha > 0.
3195 float ambientShadowAlpha = mAmbientShadowAlpha;
3196 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3197 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3198 }
3199 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3200 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003201 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003202 }
ztenghui7b4516e2014-01-07 10:42:55 -08003203
ztenghui14a4e352014-08-13 10:44:39 -07003204 float spotShadowAlpha = mSpotShadowAlpha;
3205 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3206 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3207 }
3208 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3209 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003210 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003211 }
ztenghui7b4516e2014-01-07 10:42:55 -08003212
Tom Hudson107843d2014-09-08 11:26:26 -04003213 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003214}
3215
Tom Hudson107843d2014-09-08 11:26:26 -04003216void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003217 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003218 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003219 return;
Romain Guy3b753822013-03-05 10:27:35 -08003220 }
Romain Guy735738c2012-12-03 12:34:51 -08003221
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003222 int color = paint->getColor();
3223 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003224 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003225 color |= 0x00ffffff;
3226 }
3227
Romain Guy672433d2013-01-04 19:05:13 -08003228 float left = FLT_MAX;
3229 float top = FLT_MAX;
3230 float right = FLT_MIN;
3231 float bottom = FLT_MIN;
3232
Romain Guy448455f2013-07-22 13:57:50 -07003233 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003234 Vertex* vertex = mesh;
3235
Chris Craik2af46352012-11-26 18:30:17 -08003236 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003237 float l = rects[index + 0];
3238 float t = rects[index + 1];
3239 float r = rects[index + 2];
3240 float b = rects[index + 3];
3241
Romain Guy3b753822013-03-05 10:27:35 -08003242 Vertex::set(vertex++, l, t);
3243 Vertex::set(vertex++, r, t);
3244 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003245 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003246
Romain Guy3b753822013-03-05 10:27:35 -08003247 left = fminf(left, l);
3248 top = fminf(top, t);
3249 right = fmaxf(right, r);
3250 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003251 }
3252
Chris Craikf0a59072013-11-19 18:00:46 -08003253 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003254 return;
Romain Guya362c692013-02-04 13:50:16 -08003255 }
Romain Guy672433d2013-01-04 19:05:13 -08003256
Romain Guy672433d2013-01-04 19:05:13 -08003257 setupDraw();
3258 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003259 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003260 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003261 setupDrawColorFilter(getColorFilter(paint));
3262 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003263 setupDrawProgram();
3264 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003265 setupDrawModelView(kModelViewMode_Translate, false,
3266 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003267 setupDrawColorUniforms(getShader(paint));
3268 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003269 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003270
Romain Guy8ce00302013-01-15 18:51:42 -08003271 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003272 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003273 }
3274
Chris Craik4063a0e2013-11-15 16:06:56 -08003275 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003276
Tom Hudson107843d2014-09-08 11:26:26 -04003277 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003278}
3279
Romain Guy026c5e162010-06-28 17:12:22 -07003280void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003281 const SkPaint* paint, bool ignoreTransform) {
Chris Craik117bdbc2015-02-05 10:12:38 -08003282
3283 if (!paint->getShader() && !currentSnapshot()->roundRectClipState) {
3284 const Matrix4& transform = ignoreTransform ? Matrix4::identity() : *currentTransform();
3285 Glop glop;
3286 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
3287 aBuilder.setMeshUnitQuad()
3288 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
3289 .setModelViewMapUnitToRect(Rect(left, top, right, bottom))
3290 .setPaint(paint, currentSnapshot()->alpha)
3291 .build();
3292 renderGlop(glop);
3293 return;
3294 }
3295
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003296 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003297 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003298 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003299 color |= 0x00ffffff;
3300 }
3301
Romain Guy70ca14e2010-12-13 18:24:33 -08003302 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003303 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003304 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003305 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003306 setupDrawColorFilter(getColorFilter(paint));
3307 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003308 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003309 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3310 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003311 setupDrawColorUniforms(getShader(paint));
3312 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003313 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003314 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003315
Chris Craik117bdbc2015-02-05 10:12:38 -08003316 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Romain Guyc95c8d62010-09-17 15:31:32 -07003317}
3318
Romain Guy82ba8142010-07-09 13:25:56 -07003319void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003320 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003321 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003322
Chris Craike84a2082014-12-22 14:28:49 -08003323 GLvoid* vertices = (GLvoid*) nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -08003324 GLvoid* texCoords = (GLvoid*) kMeshTextureOffset;
Romain Guy3b748a42013-04-17 18:54:38 -07003325
3326 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003327 vertices = &mMeshVertices[0].x;
3328 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003329
3330 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3331 texture->uvMapper->map(uvs);
3332
3333 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3334 }
3335
Chris Craikd6b65f62014-01-01 14:45:21 -08003336 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3337 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3338 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003339
Romain Guyd21b6e12011-11-30 20:21:23 -08003340 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003341 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003342 paint, texture->blend, vertices, texCoords,
Chris Craik117bdbc2015-02-05 10:12:38 -08003343 GL_TRIANGLE_STRIP, kUnitQuadCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003344 } else {
Chris Craik678625242014-02-28 12:26:34 -08003345 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003346 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Chris Craik117bdbc2015-02-05 10:12:38 -08003347 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, kUnitQuadCount);
Romain Guy3b748a42013-04-17 18:54:38 -07003348 }
3349
3350 if (texture->uvMapper) {
3351 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003352 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003353}
3354
Romain Guyf7f93552010-07-08 19:17:03 -07003355void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003356 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003357 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003358 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3359 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003360
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003361 int a;
3362 SkXfermode::Mode mode;
3363 getAlphaAndMode(paint, &a, &mode);
3364 const float alpha = a / 255.0f;
3365
Romain Guy746b7402010-10-26 16:27:31 -07003366 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003367 setupDrawWithTexture();
3368 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003369 setupDrawColorFilter(getColorFilter(paint));
3370 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003371 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003372 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003373 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003374 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003375 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003376 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003377 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003378
Romain Guy6820ac82010-09-15 18:11:50 -07003379 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003380}
3381
Romain Guy3b748a42013-04-17 18:54:38 -07003382void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003383 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003384 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003385 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3386 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003387
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003388 int a;
3389 SkXfermode::Mode mode;
3390 getAlphaAndMode(paint, &a, &mode);
3391 const float alpha = a / 255.0f;
3392
Romain Guy3b748a42013-04-17 18:54:38 -07003393 setupDraw();
3394 setupDrawWithTexture();
3395 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003396 setupDrawColorFilter(getColorFilter(paint));
3397 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003398 setupDrawProgram();
3399 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003400 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003401 setupDrawTexture(texture);
3402 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003403 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003404 setupDrawMeshIndices(vertices, texCoords, vbo);
3405
Chris Craike84a2082014-12-22 14:28:49 -08003406 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy3b748a42013-04-17 18:54:38 -07003407}
3408
Romain Guy886b2752013-01-04 12:26:18 -08003409void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003410 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003411 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003412 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003413
Chris Craike84a2082014-12-22 14:28:49 -08003414 int color = paint != nullptr ? paint->getColor() : 0;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003415 int alpha;
3416 SkXfermode::Mode mode;
3417 getAlphaAndMode(paint, &alpha, &mode);
3418
Romain Guy886b2752013-01-04 12:26:18 -08003419 setupDraw();
3420 setupDrawWithTexture(true);
Chris Craike84a2082014-12-22 14:28:49 -08003421 if (paint != nullptr) {
Romain Guy886b2752013-01-04 12:26:18 -08003422 setupDrawAlpha8Color(color, alpha);
3423 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003424 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003425 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003426 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003427 setupDrawProgram();
3428 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003429 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003430 setupDrawTexture(texture);
3431 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003432 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003433 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003434 setupDrawMesh(vertices, texCoords);
3435
3436 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003437}
3438
Romain Guya5aed0d2010-09-09 14:42:43 -07003439void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003440 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003441
Chris Craik117bdbc2015-02-05 10:12:38 -08003442 if (currentSnapshot()->roundRectClipState != nullptr /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003443 blend = true;
3444 mDescription.hasRoundRectClip = true;
3445 }
3446 mSkipOutlineClip = true;
3447
Romain Guy82ba8142010-07-09 13:25:56 -07003448 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003449
Romain Guy82ba8142010-07-09 13:25:56 -07003450 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003451 // These blend modes are not supported by OpenGL directly and have
3452 // to be implemented using shaders. Since the shader will perform
3453 // the blending, turn blending off here
3454 // If the blend mode cannot be implemented using shaders, fall
3455 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003456 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Chris Craik117bdbc2015-02-05 10:12:38 -08003457 if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003458 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003459 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003460
Chris Craik44eb2c02015-01-29 09:45:09 -08003461 mRenderState.blend().disable();
Romain Guy82bc7a72012-01-03 14:13:39 -08003462 return;
3463 } else {
3464 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003465 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003466 }
Chris Craik44eb2c02015-01-29 09:45:09 -08003467 mRenderState.blend().enable(mode, swapSrcDst);
3468 } else {
3469 mRenderState.blend().disable();
Romain Guy82ba8142010-07-09 13:25:56 -07003470 }
Romain Guybd6b79b2010-06-26 00:13:53 -07003471}
3472
Romain Guy026c5e162010-06-28 17:12:22 -07003473void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003474 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003475 TextureVertex::setUV(v++, u1, v1);
3476 TextureVertex::setUV(v++, u2, v1);
3477 TextureVertex::setUV(v++, u1, v2);
3478 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003479}
3480
Chris Craike84a2082014-12-22 14:28:49 -08003481void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
3482 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003483 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003484 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3485 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003486 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003487 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003488 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003489}
3490
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003491float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003492 float alpha;
3493 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3494 alpha = mDrawModifiers.mOverrideLayerAlpha;
3495 } else {
3496 alpha = layer->getAlpha() / 255.0f;
3497 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003498 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003499}
3500
Romain Guy9d5316e2010-06-24 19:30:36 -07003501}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003502}; // namespace android