blob: 61cd16f501e4dfede2ba33f8fe878d05341ed1ca [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chris Craik65fe5ee2015-01-26 18:06:29 -080017#include "OpenGLRenderer.h"
18
19#include "DeferredDisplayList.h"
20#include "DisplayListRenderer.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080021#include "GammaFontRenderer.h"
Chris Craik03188872015-02-02 18:39:33 -080022#include "Glop.h"
23#include "GlopBuilder.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080024#include "Patch.h"
25#include "PathTessellator.h"
26#include "Properties.h"
27#include "RenderNode.h"
Chris Craik03188872015-02-02 18:39:33 -080028#include "renderstate/MeshState.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080029#include "renderstate/RenderState.h"
30#include "ShadowTessellator.h"
31#include "SkiaShader.h"
32#include "Vector.h"
33#include "VertexBuffer.h"
34#include "utils/GLUtils.h"
35#include "utils/PaintUtils.h"
36#include "utils/TraceUtils.h"
37
Romain Guye4d01122010-06-16 18:44:05 -070038#include <stdlib.h>
39#include <stdint.h>
40#include <sys/types.h>
41
Romain Guy5cbbce52010-06-27 22:59:20 -070042#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070043#include <SkColor.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040044#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070045#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070046
Romain Guye4d01122010-06-16 18:44:05 -070047#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070048#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070049
Romain Guy08aa2cb2011-03-17 11:06:57 -070050#include <private/hwui/DrawGlInfo.h>
51
Romain Guy5b3b3522010-10-27 18:57:51 -070052#include <ui/Rect.h>
53
Chris Craik62d307c2014-07-29 10:35:13 -070054#if DEBUG_DETAILED_EVENTS
55 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
56#else
57 #define EVENT_LOGD(...)
58#endif
59
Romain Guye4d01122010-06-16 18:44:05 -070060namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070061namespace uirenderer {
62
Chris Craik678625242014-02-28 12:26:34 -080063static GLenum getFilter(const SkPaint* paint) {
64 if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) {
65 return GL_LINEAR;
66 }
67 return GL_NEAREST;
68}
Romain Guyd21b6e12011-11-30 20:21:23 -080069
Romain Guy9d5316e2010-06-24 19:30:36 -070070///////////////////////////////////////////////////////////////////////////////
71// Globals
72///////////////////////////////////////////////////////////////////////////////
73
Romain Guyf607bdc2010-09-10 19:20:06 -070074
Romain Guyf6a11b82010-06-23 17:47:49 -070075///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -070076// Functions
77///////////////////////////////////////////////////////////////////////////////
78
79template<typename T>
80static inline T min(T a, T b) {
81 return a < b ? a : b;
82}
83
84///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -070085// Constructors/destructor
86///////////////////////////////////////////////////////////////////////////////
87
John Reck3b202512014-06-23 13:13:08 -070088OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -040089 : mState(*this)
Chris Craik058fc642014-07-23 18:19:28 -070090 , mCaches(Caches::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -070091 , mRenderState(renderState)
Chris Craik65fe5ee2015-01-26 18:06:29 -080092 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -070093 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -070094 , mSuppressTiling(false)
95 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -040096 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -070097 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -070098 , mLightRadius(FLT_MIN)
99 , mAmbientShadowAlpha(0)
100 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800101 // *set* draw modifiers to be 0
102 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700103 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700104
Chris Craik03188872015-02-02 18:39:33 -0800105 memcpy(mMeshVertices, kUnitQuadVertices, sizeof(kUnitQuadVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700106}
107
Romain Guy85bf02f2010-06-22 13:11:24 -0700108OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700109 // The context has already been destroyed at this point, do not call
110 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700111}
112
Romain Guy87e2f7572012-09-24 11:37:12 -0700113void OpenGLRenderer::initProperties() {
114 char property[PROPERTY_VALUE_MAX];
115 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
116 mScissorOptimizationDisabled = !strcasecmp(property, "true");
117 INIT_LOGD(" Scissor optimization %s",
118 mScissorOptimizationDisabled ? "disabled" : "enabled");
119 } else {
120 INIT_LOGD(" Scissor optimization enabled");
121 }
Romain Guy13631f32012-01-30 17:41:55 -0800122}
123
Chris Craik058fc642014-07-23 18:19:28 -0700124void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
125 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
126 mLightCenter = lightCenter;
127 mLightRadius = lightRadius;
128 mAmbientShadowAlpha = ambientShadowAlpha;
129 mSpotShadowAlpha = spotShadowAlpha;
130}
131
Romain Guy13631f32012-01-30 17:41:55 -0800132///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700133// Setup
134///////////////////////////////////////////////////////////////////////////////
135
Chris Craik797b95b2014-05-20 18:10:25 -0700136void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700137 glDisable(GL_DITHER);
138 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Chris Craik284b2432014-09-18 16:05:35 -0700139 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700140}
141
Romain Guy96885eb2013-03-26 15:05:58 -0700142void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800143 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800144 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400145 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700146 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700147 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700148}
149
Tom Hudson107843d2014-09-08 11:26:26 -0400150void OpenGLRenderer::startFrame() {
151 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700152 mFrameStarted = true;
153
Tom Hudson984162f2014-10-10 13:38:16 -0400154 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700155
Romain Guy96885eb2013-03-26 15:05:58 -0700156 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700157
Tom Hudson984162f2014-10-10 13:38:16 -0400158 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800159
Romain Guy54c1a642012-09-27 17:55:46 -0700160 // Functors break the tiling extension in pretty spectacular ways
161 // This ensures we don't use tiling when a functor is going to be
162 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700163 mSuppressTiling = mCaches.hasRegisteredFunctors()
164 || mFirstFrameAfterResize;
165 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700166
Chris Craikd6b65f62014-01-01 14:45:21 -0800167 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700168
Romain Guy7c450aa2012-09-21 19:15:00 -0700169 debugOverdraw(true, true);
170
Tom Hudson107843d2014-09-08 11:26:26 -0400171 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700172 mTilingClip.right, mTilingClip.bottom, mOpaque);
173}
174
Tom Hudson107843d2014-09-08 11:26:26 -0400175void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700176 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700177
Romain Guy96885eb2013-03-26 15:05:58 -0700178 setupFrameState(left, top, right, bottom, opaque);
179
180 // Layer renderers will start the frame immediately
181 // The framebuffer renderer will first defer the display list
182 // for each layer and wait until the first drawing command
183 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800184 if (currentSnapshot()->fbo == 0) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800185 mRenderState.blend().syncEnabled();
Romain Guy96885eb2013-03-26 15:05:58 -0700186 updateLayers();
187 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400188 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700189 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700190}
191
Romain Guydcfc8362013-01-03 13:08:57 -0800192void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
193 // If we know that we are going to redraw the entire framebuffer,
194 // perform a discard to let the driver know we don't need to preserve
195 // the back buffer for this frame.
Chris Craik117bdbc2015-02-05 10:12:38 -0800196 if (mCaches.extensions().hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400197 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
198 const bool isFbo = onGetTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800199 const GLenum attachments[] = {
200 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
201 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800202 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
203 }
204}
205
Tom Hudson107843d2014-09-08 11:26:26 -0400206void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700207 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800208 mRenderState.scissor().setEnabled(true);
209 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700210 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400211 mDirty = true;
212 return;
Romain Guyddf74372012-05-22 14:07:07 -0700213 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700214
Chris Craik65fe5ee2015-01-26 18:06:29 -0800215 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700216}
217
henry.uh_chen33f5a592014-07-02 19:36:56 +0800218void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700219 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800220 const Snapshot* snapshot = currentSnapshot();
221
Chris Craik14e51302013-12-30 15:32:54 -0800222 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800223 if (snapshot->flags & Snapshot::kFlagFboTarget) {
224 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700225 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700226
henry.uh_chen33f5a592014-07-02 19:36:56 +0800227 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800228 }
229}
230
henry.uh_chen33f5a592014-07-02 19:36:56 +0800231void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800232 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800233 if(expand) {
234 // Expand the startTiling region by 1
235 int leftNotZero = (clip.left > 0) ? 1 : 0;
236 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
237
238 mCaches.startTiling(
239 clip.left - leftNotZero,
240 windowHeight - clip.bottom - topNotZero,
241 clip.right - clip.left + leftNotZero + 1,
242 clip.bottom - clip.top + topNotZero + 1,
243 opaque);
244 } else {
245 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800246 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800247 }
Romain Guy54c1a642012-09-27 17:55:46 -0700248 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700249}
250
251void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700252 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700253}
254
Tom Hudson107843d2014-09-08 11:26:26 -0400255bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700256 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700257 endTiling();
Chris Craik4ac36f82014-12-09 16:54:03 -0800258 mTempPaths.clear();
259
Romain Guyca89e2a2013-03-08 17:44:20 -0800260 // When finish() is invoked on FBO 0 we've reached the end
261 // of the current frame
Tom Hudson984162f2014-10-10 13:38:16 -0400262 if (onGetTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800263 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700264 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800265 }
266
Romain Guy11cb6422012-09-21 00:39:43 -0700267 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700268#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700269 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700270#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700271
Romain Guyc15008e2010-11-10 11:59:15 -0800272#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800273 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700274#else
275 if (mCaches.getDebugLevel() & kDebugMemory) {
276 mCaches.dumpMemoryUsage();
277 }
Romain Guyc15008e2010-11-10 11:59:15 -0800278#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700279 }
Romain Guy96885eb2013-03-26 15:05:58 -0700280
281 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400282
283 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700284}
285
Romain Guy35643dd2012-09-18 15:40:58 -0700286void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700287 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
288 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700289 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700290
Chris Craik65fe5ee2015-01-26 18:06:29 -0800291 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700292 dirtyClip();
293}
294
Andreas Gampe64bb4132014-11-22 00:35:09 +0000295void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400296 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700297
Rob Tsuk487a92c2015-01-06 13:22:54 -0800298 Rect clip(mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700299 clip.snapToPixelBoundaries();
300
Romain Guyd643bb52011-03-01 14:55:21 -0800301 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800302 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800303 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800304 dirtyLayerUnchecked(clip, getRegion());
305 }
Romain Guyd643bb52011-03-01 14:55:21 -0800306
Romain Guy08aa2cb2011-03-17 11:06:57 -0700307 DrawGlInfo info;
308 info.clipLeft = clip.left;
309 info.clipTop = clip.top;
310 info.clipRight = clip.right;
311 info.clipBottom = clip.bottom;
312 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700313 info.width = getViewportWidth();
314 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800315 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700316
Tom Hudson984162f2014-10-10 13:38:16 -0400317 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700318 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400319 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700320 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
321 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800322 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700323 setScissorFromClip();
324 }
Chris Craik54f574a2013-08-26 11:23:46 -0700325
John Reck3b202512014-06-23 13:13:08 -0700326 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
327 // Scissor may have been modified, reset dirty clip
328 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800329
Tom Hudson107843d2014-09-08 11:26:26 -0400330 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800331}
332
Romain Guyf6a11b82010-06-23 17:47:49 -0700333///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700334// Debug
335///////////////////////////////////////////////////////////////////////////////
336
Chris Craik62d307c2014-07-29 10:35:13 -0700337void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
338#if DEBUG_DETAILED_EVENTS
339 const int BUFFER_SIZE = 256;
340 va_list ap;
341 char buf[BUFFER_SIZE];
342
343 va_start(ap, fmt);
344 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
345 va_end(ap);
346
347 eventMark(buf);
348#endif
349}
350
351
Romain Guy0f667532013-03-01 14:31:04 -0800352void OpenGLRenderer::eventMark(const char* name) const {
353 mCaches.eventMark(0, name);
354}
355
Romain Guy87e2f7572012-09-24 11:37:12 -0700356void OpenGLRenderer::startMark(const char* name) const {
357 mCaches.startMark(0, name);
358}
359
360void OpenGLRenderer::endMark() const {
361 mCaches.endMark();
362}
363
364void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700365 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700366}
367
368void OpenGLRenderer::renderOverdraw() {
Tom Hudson984162f2014-10-10 13:38:16 -0400369 if (mCaches.debugOverdraw && onGetTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700370 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700371
Chris Craik65fe5ee2015-01-26 18:06:29 -0800372 mRenderState.scissor().setEnabled(true);
373 mRenderState.scissor().set(clip->left,
374 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
375 clip->right - clip->left,
376 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700377
Romain Guy627c6fd2013-08-21 11:53:18 -0700378 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800379 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700380 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
381
382 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800383 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700384 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
385
386 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800387 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700388 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
389
390 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800391 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700392 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
393
Chris Craik96a5c4c2015-01-27 15:46:35 -0800394 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700395 }
396}
397
398///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700399// Layers
400///////////////////////////////////////////////////////////////////////////////
401
402bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700403 if (layer->deferredUpdateScheduled && layer->renderer
404 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700405
Romain Guy7c450aa2012-09-21 19:15:00 -0700406 if (inFrame) {
407 endTiling();
408 debugOverdraw(false, false);
409 }
Romain Guy11cb6422012-09-21 00:39:43 -0700410
Romain Guy96885eb2013-03-26 15:05:58 -0700411 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700412 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700413 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700414 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700415 }
Romain Guy11cb6422012-09-21 00:39:43 -0700416
417 if (inFrame) {
418 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800419 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700420 }
421
Romain Guy5bb3c732012-11-29 17:52:58 -0800422 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700423 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700424
425 return true;
426 }
427
428 return false;
429}
430
431void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700432 // If draw deferring is enabled this method will simply defer
433 // the display list of each individual layer. The layers remain
434 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700435 int count = mLayerUpdates.size();
436 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700437 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
438 startMark("Layer Updates");
439 } else {
440 startMark("Defer Layer Updates");
441 }
Romain Guy11cb6422012-09-21 00:39:43 -0700442
Chris Craik1206b9b2013-04-04 14:46:24 -0700443 // Note: it is very important to update the layers in order
444 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700445 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700446 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700447 }
Romain Guy11cb6422012-09-21 00:39:43 -0700448
Romain Guy96885eb2013-03-26 15:05:58 -0700449 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
450 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400451 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700452 }
453 endMark();
454 }
455}
456
457void OpenGLRenderer::flushLayers() {
458 int count = mLayerUpdates.size();
459 if (count > 0) {
460 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700461
Chris Craik1206b9b2013-04-04 14:46:24 -0700462 // Note: it is very important to update the layers in order
463 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800464 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700465 }
466
467 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400468 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700469
Romain Guy11cb6422012-09-21 00:39:43 -0700470 endMark();
471 }
472}
473
474void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
475 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700476 // Make sure we don't introduce duplicates.
477 // SortedVector would do this automatically but we need to respect
478 // the insertion order. The linear search is not an issue since
479 // this list is usually very short (typically one item, at most a few)
480 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
481 if (mLayerUpdates.itemAt(i) == layer) {
482 return;
483 }
484 }
Romain Guy11cb6422012-09-21 00:39:43 -0700485 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700486 }
487}
488
Romain Guye93482f2013-06-17 13:14:51 -0700489void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
490 if (layer) {
491 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
492 if (mLayerUpdates.itemAt(i) == layer) {
493 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700494 break;
495 }
496 }
497 }
498}
499
Romain Guy40543602013-06-12 15:31:28 -0700500void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800501 ATRACE_NAME("Update HW Layers");
Chris Craik44eb2c02015-01-29 09:45:09 -0800502 mRenderState.blend().syncEnabled();
Romain Guy40543602013-06-12 15:31:28 -0700503 updateLayers();
504 flushLayers();
505 // Wait for all the layer updates to be executed
John Reck55156372015-01-21 07:46:37 -0800506 glFinish();
Romain Guy40543602013-06-12 15:31:28 -0700507}
508
John Reck443a7142014-09-04 17:40:05 -0700509void OpenGLRenderer::markLayersAsBuildLayers() {
510 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
511 mLayerUpdates[i]->wasBuildLayered = true;
512 }
513}
514
Romain Guy11cb6422012-09-21 00:39:43 -0700515///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700516// State management
517///////////////////////////////////////////////////////////////////////////////
518
Chris Craik14e51302013-12-30 15:32:54 -0800519void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700520 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800521 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
522 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700523
Chris Craika64a2be2014-05-14 14:17:01 -0700524 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700525 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700526 }
527
Romain Guy2542d192010-08-18 11:47:12 -0700528 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700529 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700530 }
Romain Guy2542d192010-08-18 11:47:12 -0700531
Romain Guy5ec99242010-11-03 16:19:08 -0700532 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700533 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700534 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700535 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800536 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700537 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700538 }
Romain Guybb9524b2010-06-22 18:56:38 -0700539}
540
Romain Guyf6a11b82010-06-23 17:47:49 -0700541///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700542// Layers
543///////////////////////////////////////////////////////////////////////////////
544
545int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700546 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700547 // force matrix/clip isolation for layer
548 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
549
Tom Hudson984162f2014-10-10 13:38:16 -0400550 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700551
Tom Hudson984162f2014-10-10 13:38:16 -0400552 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700553 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700554 }
Romain Guyd55a8612010-06-28 17:42:46 -0700555
556 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700557}
558
Chris Craikd90144d2013-03-19 15:03:48 -0700559void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
560 const Rect untransformedBounds(bounds);
561
Chris Craikd6b65f62014-01-01 14:45:21 -0800562 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700563
564 // Layers only make sense if they are in the framebuffer's bounds
Rob Tsuk487a92c2015-01-06 13:22:54 -0800565 if (bounds.intersect(mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700566 // We cannot work with sub-pixels in this case
567 bounds.snapToPixelBoundaries();
568
569 // When the layer is not an FBO, we may use glCopyTexImage so we
570 // need to make sure the layer does not extend outside the bounds
571 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700572 const Snapshot& previous = *(currentSnapshot()->previous);
573 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
574 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700575 bounds.setEmpty();
576 } else if (fboLayer) {
577 clip.set(bounds);
578 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800579 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700580 inverse.mapRect(clip);
581 clip.snapToPixelBoundaries();
582 if (clip.intersect(untransformedBounds)) {
583 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
584 bounds.set(untransformedBounds);
585 } else {
586 clip.setEmpty();
587 }
588 }
589 } else {
590 bounds.setEmpty();
591 }
592}
593
Chris Craik408eb122013-03-26 18:55:15 -0700594void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
595 bool fboLayer, int alpha) {
596 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
597 bounds.getHeight() > mCaches.maxTextureSize ||
598 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400599 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700600 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400601 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700602 }
603}
604
Chris Craikd90144d2013-03-19 15:03:48 -0700605int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500606 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400607 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700608
Tom Hudson984162f2014-10-10 13:38:16 -0400609 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700610 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
611 // operations will be able to store and restore the current clip and transform info, and
612 // quick rejection will be correct (for display lists)
613
614 Rect bounds(left, top, right, bottom);
615 Rect clip;
616 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500617 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700618
Tom Hudson984162f2014-10-10 13:38:16 -0400619 if (!mState.currentlyIgnored()) {
620 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
621 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
622 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800623 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700624 }
625 }
626
627 return count;
628}
629
Romain Guy1c740bc2010-09-13 18:00:09 -0700630/**
631 * Layers are viewed by Skia are slightly different than layers in image editing
632 * programs (for instance.) When a layer is created, previously created layers
633 * and the frame buffer still receive every drawing command. For instance, if a
634 * layer is created and a shape intersecting the bounds of the layers and the
635 * framebuffer is draw, the shape will be drawn on both (unless the layer was
636 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
637 *
638 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
639 * texture. Unfortunately, this is inefficient as it requires every primitive to
640 * be drawn n + 1 times, where n is the number of active layers. In practice this
641 * means, for every primitive:
642 * - Switch active frame buffer
643 * - Change viewport, clip and projection matrix
644 * - Issue the drawing
645 *
646 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700647 * To avoid this, layers are implemented in a different way here, at least in the
648 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
649 * is set. When this flag is set we can redirect all drawing operations into a
650 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700651 *
652 * This implementation relies on the frame buffer being at least RGBA 8888. When
653 * a layer is created, only a texture is created, not an FBO. The content of the
654 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700655 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700656 * buffer and drawing continues as normal. This technique therefore treats the
657 * frame buffer as a scratch buffer for the layers.
658 *
659 * To compose the layers back onto the frame buffer, each layer texture
660 * (containing the original frame buffer data) is drawn as a simple quad over
661 * the frame buffer. The trick is that the quad is set as the composition
662 * destination in the blending equation, and the frame buffer becomes the source
663 * of the composition.
664 *
665 * Drawing layers with an alpha value requires an extra step before composition.
666 * An empty quad is drawn over the layer's region in the frame buffer. This quad
667 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
668 * quad is used to multiply the colors in the frame buffer. This is achieved by
669 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
670 * GL_ZERO, GL_SRC_ALPHA.
671 *
672 * Because glCopyTexImage2D() can be slow, an alternative implementation might
673 * be use to draw a single clipped layer. The implementation described above
674 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700675 *
676 * (1) The frame buffer is actually not cleared right away. To allow the GPU
677 * to potentially optimize series of calls to glCopyTexImage2D, the frame
678 * buffer is left untouched until the first drawing operation. Only when
679 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700680 */
Chet Haased48885a2012-08-28 17:43:28 -0700681bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700682 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800683 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
684 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700685
Romain Guyeb993562010-10-05 18:14:38 -0700686 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
687
Romain Guyf607bdc2010-09-10 19:20:06 -0700688 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700689 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700690 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700691 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000692 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700693
694 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400695 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700696 return false;
697 }
Romain Guyf18fd992010-07-08 11:45:51 -0700698
Chris Craik44eb2c02015-01-29 09:45:09 -0800699 mCaches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700700 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700701 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700702 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700703 }
704
Derek Sollenberger674554f2014-02-19 16:47:32 +0000705 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700706 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700707 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
708 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500709
Chet Haasea23eed82012-04-12 15:19:04 -0700710 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700711 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700712 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda57022010-07-06 11:39:32 -0700713
Romain Guy8fb95422010-08-17 18:38:51 -0700714 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400715 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
716 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700717
Chris Craika8bea8e2014-09-24 11:29:43 -0700718 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
719 fboLayer ? "" : "unclipped ",
720 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700721 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700722 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700723 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700724 } else {
725 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700726 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800727 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700728 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700729 // Workaround for some GL drivers. When reading pixels lying outside
730 // of the window we should get undefined values for those pixels.
731 // Unfortunately some drivers will turn the entire target texture black
732 // when reading outside of the window.
733 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800734 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700735 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800736 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700737
Chris Craika64a2be2014-05-14 14:17:01 -0700738 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
739 bounds.left, getViewportHeight() - bounds.bottom,
740 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700741
Romain Guy54be1cd2011-06-13 19:04:27 -0700742 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800743 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700744 }
Romain Guyeb993562010-10-05 18:14:38 -0700745 }
Romain Guyf86ef572010-07-01 11:05:42 -0700746
Romain Guyd55a8612010-06-28 17:42:46 -0700747 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700748}
749
Chris Craike63f7c622013-10-17 10:30:55 -0700750bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800751 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700752 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700753
Tom Hudson984162f2014-10-10 13:38:16 -0400754 writableSnapshot()->region = &writableSnapshot()->layer->region;
755 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
756 writableSnapshot()->fbo = layer->getFbo();
757 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
758 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
759 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800760 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700761
Romain Guy2b7028e2012-09-19 17:25:38 -0700762 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700763 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700764 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700765 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700766 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700767
768 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700769 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700770 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700771 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700772 }
773
774 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700775 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700776
henry.uh_chen33f5a592014-07-02 19:36:56 +0800777 // Expand the startTiling region by 1
778 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700779
780 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800781 mRenderState.scissor().setEnabled(true);
782 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700783 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700784 glClear(GL_COLOR_BUFFER_BIT);
785
786 dirtyClip();
787
788 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700789 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700790 return true;
791}
792
Romain Guy1c740bc2010-09-13 18:00:09 -0700793/**
794 * Read the documentation of createLayer() before doing anything in this method.
795 */
Chris Craik14e51302013-12-30 15:32:54 -0800796void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
797 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000798 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700799 return;
800 }
801
Chris Craik14e51302013-12-30 15:32:54 -0800802 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800803 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800804 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700805
Chris Craik39a908c2013-06-13 14:39:01 -0700806 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400807 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800808 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800809 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700810
Romain Guyeb993562010-10-05 18:14:38 -0700811 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700812 endTiling();
813
Romain Guye0aa84b2012-04-03 19:30:26 -0700814 // Detach the texture from the FBO
815 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800816
817 layer->removeFbo(false);
818
Romain Guyeb993562010-10-05 18:14:38 -0700819 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700820 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700821 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700822
Chris Craikd6b65f62014-01-01 14:45:21 -0800823 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700824 }
825
Romain Guy9ace8f52011-07-07 20:50:11 -0700826 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500827 SkPaint layerPaint;
828 layerPaint.setAlpha(layer->getAlpha());
829 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
830 layerPaint.setColorFilter(layer->getColorFilter());
831
832 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700833 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700834 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700835 }
836
Chris Craik96a5c4c2015-01-27 15:46:35 -0800837 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700838
Chris Craik44eb2c02015-01-29 09:45:09 -0800839 mCaches.textureState().activateTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700840
Romain Guy5b3b3522010-10-27 18:57:51 -0700841 // When the layer is stored in an FBO, we can save a bit of fillrate by
842 // drawing only the dirty region
843 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800844 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700845 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700846 } else if (!rect.isEmpty()) {
847 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530848
849 save(0);
850 // the layer contains screen buffer content that shouldn't be alpha modulated
851 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400852 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700853 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530854 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700855 }
Romain Guy8b55f372010-08-18 17:10:07 -0700856
Romain Guy746b7402010-10-26 16:27:31 -0700857 dirtyClip();
858
Romain Guyeb993562010-10-05 18:14:38 -0700859 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800860 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700861 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800862 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800863 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700864 }
865}
866
Romain Guyaa6c24c2011-04-28 18:40:04 -0700867void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700868 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700869
870 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700871 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700872 setupDrawWithTexture();
873 } else {
874 setupDrawWithExternalTexture();
875 }
876 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700877 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500878 setupDrawColorFilter(layer->getColorFilter());
879 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700880 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700881 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500882 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700883 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
884 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700885 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700886 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700887 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800888 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800889 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700890 layer->getWidth() == (uint32_t) rect.getWidth() &&
891 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800892 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
893 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700894
Romain Guyd21b6e12011-11-30 20:21:23 -0800895 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800896 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
897 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700898 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800899 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800900 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
901 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700902 }
903 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700904 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700905
Chris Craik117bdbc2015-02-05 10:12:38 -0800906 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700907}
908
Romain Guy5b3b3522010-10-27 18:57:51 -0700909void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700910 if (layer->isTextureLayer()) {
911 EVENT_LOGD("composeTextureLayerRect");
912 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
913 drawTextureLayer(layer, rect);
914 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
915 } else {
916 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700917 const Rect& texCoords = layer->texCoords;
918 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
919 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700920
Romain Guy9ace8f52011-07-07 20:50:11 -0700921 float x = rect.left;
922 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -0800923 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700924 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700925 layer->getHeight() == (uint32_t) rect.getHeight();
926
927 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700928 // When we're swapping, the layer is already in screen coordinates
929 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800930 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
931 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700932 }
933
Romain Guyd21b6e12011-11-30 20:21:23 -0800934 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700935 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800936 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700937 }
938
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500939 SkPaint layerPaint;
940 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
941 layerPaint.setXfermodeMode(layer->getMode());
942 layerPaint.setColorFilter(layer->getColorFilter());
943
944 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700945 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500946 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -0700947 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik117bdbc2015-02-05 10:12:38 -0800948 GL_TRIANGLE_STRIP, kUnitQuadCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700949
Romain Guyaa6c24c2011-04-28 18:40:04 -0700950 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700951 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700952}
953
Chris Craik34416ea2013-04-15 16:08:28 -0700954/**
955 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
956 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
957 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
958 * by saveLayer's restore
959 */
Tom Hudson984162f2014-10-10 13:38:16 -0400960#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
961 DRAW_COMMAND; \
962 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
963 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
964 DRAW_COMMAND; \
965 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
966 } \
Chris Craik34416ea2013-04-15 16:08:28 -0700967 }
968
969#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
970
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400971// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
972// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
973class LayerShader : public SkShader {
974public:
975 LayerShader(Layer* layer, const SkMatrix* localMatrix)
976 : INHERITED(localMatrix)
977 , mLayer(layer) {
978 }
979
Chris Craike84a2082014-12-22 14:28:49 -0800980 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400981 if (data) {
982 *data = static_cast<void*>(mLayer);
983 }
984 return true;
985 }
986
Chris Craike84a2082014-12-22 14:28:49 -0800987 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400988 return !mLayer->isBlend();
989 }
990
991protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000992 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400993 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
994 }
995
Chris Craike84a2082014-12-22 14:28:49 -0800996 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400997 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
998 }
999
Chris Craike84a2082014-12-22 14:28:49 -08001000 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001001 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -08001002 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001003 }
1004private:
1005 // Unowned.
1006 Layer* mLayer;
1007 typedef SkShader INHERITED;
1008};
1009
Romain Guy5b3b3522010-10-27 18:57:51 -07001010void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001011 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1012
1013 if (layer->getConvexMask()) {
1014 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1015
1016 // clip to the area of the layer the mask can be larger
1017 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1018
1019 SkPaint paint;
1020 paint.setAntiAlias(true);
1021 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1022
Chris Craik3f0854292014-04-15 16:18:08 -07001023 // create LayerShader to map SaveLayer content into subsequent draw
1024 SkMatrix shaderMatrix;
1025 shaderMatrix.setTranslate(rect.left, rect.bottom);
1026 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001027 LayerShader layerShader(layer, &shaderMatrix);
1028 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001029
1030 // Since the drawing primitive is defined in local drawing space,
1031 // we don't need to modify the draw matrix
1032 const SkPath* maskPath = layer->getConvexMask();
1033 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1034
Chris Craike84a2082014-12-22 14:28:49 -08001035 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -07001036 restore();
1037
1038 return;
1039 }
1040
Romain Guy5b3b3522010-10-27 18:57:51 -07001041 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001042 layer->setRegionAsRect();
1043
Chris Craik34416ea2013-04-15 16:08:28 -07001044 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001045
Romain Guy5b3b3522010-10-27 18:57:51 -07001046 layer->region.clear();
1047 return;
1048 }
1049
Chris Craik62d307c2014-07-29 10:35:13 -07001050 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001051 // standard Region based draw
1052 size_t count;
1053 const android::Rect* rects;
1054 Region safeRegion;
1055 if (CC_LIKELY(hasRectToRectTransform())) {
1056 rects = layer->region.getArray(&count);
1057 } else {
1058 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1059 rects = safeRegion.getArray(&count);
1060 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001061
Chris Craik3f0854292014-04-15 16:18:08 -07001062 const float alpha = getLayerAlpha(layer);
1063 const float texX = 1.0f / float(layer->getWidth());
1064 const float texY = 1.0f / float(layer->getHeight());
1065 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001066
Chris Craik3f0854292014-04-15 16:18:08 -07001067 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001068
Chris Craik3f0854292014-04-15 16:18:08 -07001069 // We must get (and therefore bind) the region mesh buffer
1070 // after we setup drawing in case we need to mess with the
1071 // stencil buffer in setupDraw()
1072 TextureVertex* mesh = mCaches.getRegionMesh();
1073 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001074
Chris Craik3f0854292014-04-15 16:18:08 -07001075 setupDrawWithTexture();
1076 setupDrawColor(alpha, alpha, alpha, alpha);
1077 setupDrawColorFilter(layer->getColorFilter());
1078 setupDrawBlending(layer);
1079 setupDrawProgram();
1080 setupDrawDirtyRegionsDisabled();
1081 setupDrawPureColorUniforms();
1082 setupDrawColorFilterUniforms(layer->getColorFilter());
1083 setupDrawTexture(layer->getTexture());
1084 if (currentTransform()->isPureTranslate()) {
1085 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1086 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001087
Chris Craik3f0854292014-04-15 16:18:08 -07001088 layer->setFilter(GL_NEAREST);
1089 setupDrawModelView(kModelViewMode_Translate, false,
1090 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1091 } else {
1092 layer->setFilter(GL_LINEAR);
1093 setupDrawModelView(kModelViewMode_Translate, false,
1094 rect.left, rect.top, rect.right, rect.bottom);
1095 }
1096 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001097
Chris Craik3f0854292014-04-15 16:18:08 -07001098 for (size_t i = 0; i < count; i++) {
1099 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001100
Chris Craik3f0854292014-04-15 16:18:08 -07001101 const float u1 = r->left * texX;
1102 const float v1 = (height - r->top) * texY;
1103 const float u2 = r->right * texX;
1104 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001105
Chris Craik3f0854292014-04-15 16:18:08 -07001106 // TODO: Reject quads outside of the clip
1107 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1108 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1109 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1110 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001111
Chris Craik3f0854292014-04-15 16:18:08 -07001112 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001113
Chris Craik96a5c4c2015-01-27 15:46:35 -08001114 if (numQuads >= kMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001115 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001116 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001117 numQuads = 0;
1118 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001119 }
Chris Craik3f0854292014-04-15 16:18:08 -07001120 }
1121
1122 if (numQuads > 0) {
1123 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001124 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001125 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001126
Romain Guy5b3b3522010-10-27 18:57:51 -07001127#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001128 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001129#endif
1130
Chris Craik3f0854292014-04-15 16:18:08 -07001131 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001132}
1133
Romain Guy3a3133d2011-02-01 22:59:58 -08001134#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001135void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001136 size_t count;
1137 const android::Rect* rects = region.getArray(&count);
1138
1139 uint32_t colors[] = {
1140 0x7fff0000, 0x7f00ff00,
1141 0x7f0000ff, 0x7fff00ff,
1142 };
1143
1144 int offset = 0;
1145 int32_t top = rects[0].top;
1146
1147 for (size_t i = 0; i < count; i++) {
1148 if (top != rects[i].top) {
1149 offset ^= 0x2;
1150 top = rects[i].top;
1151 }
1152
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001153 SkPaint paint;
1154 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001155 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001156 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001157 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001158}
Chris Craike63f7c622013-10-17 10:30:55 -07001159#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001160
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001161void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001162 Vector<float> rects;
1163
1164 SkRegion::Iterator it(region);
1165 while (!it.done()) {
1166 const SkIRect& r = it.rect();
1167 rects.push(r.fLeft);
1168 rects.push(r.fTop);
1169 rects.push(r.fRight);
1170 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001171 it.next();
1172 }
1173
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001174 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001175}
1176
Romain Guy5b3b3522010-10-27 18:57:51 -07001177void OpenGLRenderer::dirtyLayer(const float left, const float top,
1178 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001179 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001180 Rect bounds(left, top, right, bottom);
1181 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001182 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001183 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001184}
1185
1186void OpenGLRenderer::dirtyLayer(const float left, const float top,
1187 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001188 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001189 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001190 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001191 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001192}
1193
1194void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001195 if (bounds.intersect(mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001196 bounds.snapToPixelBoundaries();
1197 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1198 if (!dirty.isEmpty()) {
1199 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001200 }
1201 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001202}
1203
Chris Craik4063a0e2013-11-15 16:06:56 -08001204void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001205 GLsizei elementsCount = quadsCount * 6;
1206 while (elementsCount > 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001207 GLsizei drawCount = min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Romain Guy448455f2013-07-22 13:57:50 -07001208
Romain Guy3380cfd2013-08-15 16:57:57 -07001209 setupDrawIndexedVertices(&mesh[0].x);
Chris Craike84a2082014-12-22 14:28:49 -08001210 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy448455f2013-07-22 13:57:50 -07001211
1212 elementsCount -= drawCount;
1213 // Though there are 4 vertices in a quad, we use 6 indices per
1214 // quad to draw with GL_TRIANGLES
1215 mesh += (drawCount / 6) * 4;
1216 }
1217}
1218
Romain Guy54be1cd2011-06-13 19:04:27 -07001219void OpenGLRenderer::clearLayerRegions() {
1220 const size_t count = mLayers.size();
1221 if (count == 0) return;
1222
Tom Hudson984162f2014-10-10 13:38:16 -04001223 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001224 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001225 // Doing several glScissor/glClear here can negatively impact
1226 // GPUs with a tiler architecture, instead we draw quads with
1227 // the Clear blending mode
1228
1229 // The list contains bounds that have already been clipped
1230 // against their initial clip rect, and the current clip
1231 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001232 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001233
Romain Guy448455f2013-07-22 13:57:50 -07001234 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001235 Vertex* vertex = mesh;
1236
1237 for (uint32_t i = 0; i < count; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001238 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001239
Chris Craik51d6a3d2014-12-22 17:16:56 -08001240 Vertex::set(vertex++, bounds.left, bounds.top);
1241 Vertex::set(vertex++, bounds.right, bounds.top);
1242 Vertex::set(vertex++, bounds.left, bounds.bottom);
1243 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001244 }
Romain Guye67307c2013-02-11 18:01:20 -08001245 // We must clear the list of dirty rects before we
1246 // call setupDraw() to prevent stencil setup to do
1247 // the same thing again
1248 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001249
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001250 SkPaint clearPaint;
1251 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1252
Romain Guy54be1cd2011-06-13 19:04:27 -07001253 setupDraw(false);
1254 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001255 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001256 setupDrawProgram();
1257 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001258 setupDrawModelView(kModelViewMode_Translate, false,
1259 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001260
Chris Craik4063a0e2013-11-15 16:06:56 -08001261 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001262
Chris Craik65fe5ee2015-01-26 18:06:29 -08001263 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001264 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001265 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001266 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001267}
1268
Romain Guybd6b79b2010-06-26 00:13:53 -07001269///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001270// State Deferral
1271///////////////////////////////////////////////////////////////////////////////
1272
Chris Craikff785832013-03-08 13:12:16 -08001273bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001274 const Rect& currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001275 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001276
Chris Craikff785832013-03-08 13:12:16 -08001277 if (stateDeferFlags & kStateDeferFlag_Draw) {
1278 // state has bounds initialized in local coordinates
1279 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001280 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001281 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001282 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1283 // is used, it should more closely duplicate the quickReject logic (in how it uses
1284 // snapToPixelBoundaries)
1285
Rob Tsuk487a92c2015-01-06 13:22:54 -08001286 if (!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001287 // quick rejected
1288 return true;
1289 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001290
Chris Craika02c4ed2013-06-14 13:43:58 -07001291 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001292 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001293 int& flags = state.mClipSideFlags;
1294 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001295 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1296 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1297 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1298 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001299 }
1300 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001301 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001302 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1303 // overdraw avoidance (since we don't know what it overlaps)
1304 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001305 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001306 }
1307 }
1308
Chris Craik527a3aa2013-03-04 10:19:31 -08001309 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1310 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001311 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001312 }
1313
Chris Craik7273daa2013-03-28 11:25:24 -07001314 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1315 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001316 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001317 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001318 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001319
1320 // always store/restore, since it's just a pointer
1321 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001322 return false;
1323}
1324
Chris Craik527a3aa2013-03-04 10:19:31 -08001325void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001326 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001327 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001328 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001329 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001330
Chris Craik527a3aa2013-03-04 10:19:31 -08001331 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001332 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001333 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001334 dirtyClip();
1335 }
Chris Craikc3566d02013-02-04 16:16:33 -08001336}
1337
Chris Craik28ce94a2013-05-31 11:38:03 -07001338/**
1339 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1340 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1341 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1342 *
1343 * This method should be called when restoreDisplayState() won't be restoring the clip
1344 */
1345void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001346 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001347 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001348 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001349 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001350 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001351 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001352 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1353 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001354}
1355
Chris Craikc3566d02013-02-04 16:16:33 -08001356///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001357// Clipping
1358///////////////////////////////////////////////////////////////////////////////
1359
Romain Guybb9524b2010-06-22 18:56:38 -07001360void OpenGLRenderer::setScissorFromClip() {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001361 Rect clip(mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001362 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001363
Chris Craik65fe5ee2015-01-26 18:06:29 -08001364 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001365 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001366 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001367 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001368}
1369
Romain Guy8ce00302013-01-15 18:51:42 -08001370void OpenGLRenderer::ensureStencilBuffer() {
1371 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1372 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1373 // just hope we have one when hasLayer() returns false.
1374 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001375 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001376 }
1377}
1378
1379void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1380 // The layer's FBO is already bound when we reach this stage
1381 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001382 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1383 // is attached after we initiated tiling. We must turn it off,
1384 // attach the new render buffer then turn tiling back on
1385 endTiling();
1386
Romain Guy8d4aeb72013-02-12 16:08:55 -08001387 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Chris Craik117bdbc2015-02-05 10:12:38 -08001388 Stencil::getSmallestStencilFormat(),
1389 layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001390 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001391
Romain Guyf735c8e2013-01-31 17:45:55 -08001392 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001393 }
1394}
1395
Rob Tsuk487a92c2015-01-06 13:22:54 -08001396static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1397 float x, float y) {
1398 Vertex v;
1399 v.x = x;
1400 v.y = y;
1401 transform.mapPoint(v.x, v.y);
1402 rectangleVertices.push_back(v);
1403}
1404
1405static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1406 Vertex v;
1407 v.x = x;
1408 v.y = y;
1409 rectangleVertices.push_back(v);
1410}
1411
1412void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
1413 int count = rectangleList.getTransformedRectanglesCount();
1414 std::vector<Vertex> rectangleVertices(count * 4);
1415 Rect scissorBox = rectangleList.calculateBounds();
1416 scissorBox.snapToPixelBoundaries();
1417 for (int i = 0; i < count; ++i) {
1418 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1419 const Matrix4& transform = tr.getTransform();
1420 Rect bounds = tr.getBounds();
1421 if (transform.rectToRect()) {
1422 transform.mapRect(bounds);
1423 if (!bounds.intersect(scissorBox)) {
1424 bounds.setEmpty();
1425 } else {
1426 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1427 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1428 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1429 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1430 }
1431 } else {
1432 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1433 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1434 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1435 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1436 }
1437 }
1438
Chris Craik65fe5ee2015-01-26 18:06:29 -08001439 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001440 scissorBox.getWidth(), scissorBox.getHeight());
1441
1442 const SkPaint* paint = nullptr;
1443 setupDraw();
1444 setupDrawNoTexture();
1445 setupDrawColor(0, 0xff * currentSnapshot()->alpha);
1446 setupDrawShader(getShader(paint));
1447 setupDrawColorFilter(getColorFilter(paint));
1448 setupDrawBlending(paint);
1449 setupDrawProgram();
1450 setupDrawDirtyRegionsDisabled();
1451 setupDrawModelView(kModelViewMode_Translate, false,
1452 0.0f, 0.0f, 0.0f, 0.0f, true);
1453 setupDrawColorUniforms(getShader(paint));
1454 setupDrawShaderUniforms(getShader(paint));
1455 setupDrawColorFilterUniforms(getColorFilter(paint));
1456
1457 issueIndexedQuadDraw(&rectangleVertices[0], rectangleVertices.size() / 4);
1458}
1459
Romain Guy8ce00302013-01-15 18:51:42 -08001460void OpenGLRenderer::setStencilFromClip() {
1461 if (!mCaches.debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001462 if (!currentSnapshot()->clipIsSimple()) {
1463 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001464 EVENT_LOGD("setStencilFromClip - enabling");
1465
Romain Guy8ce00302013-01-15 18:51:42 -08001466 // NOTE: The order here is important, we must set dirtyClip to false
1467 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001468 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001469
1470 ensureStencilBuffer();
1471
Rob Tsuk487a92c2015-01-06 13:22:54 -08001472 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001473
Rob Tsuk487a92c2015-01-06 13:22:54 -08001474 bool isRectangleList = clipArea.isRectangleList();
1475 if (isRectangleList) {
1476 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1477 } else {
1478 incrementThreshold = 0;
1479 }
1480
Chris Craik96a5c4c2015-01-27 15:46:35 -08001481 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001482
1483 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001484 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001485 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001486 if (resetScissor) {
1487 // The scissor was not set so we now need to update it
1488 setScissorFromClip();
1489 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001490
Chris Craik96a5c4c2015-01-27 15:46:35 -08001491 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001492
1493 // stash and disable the outline clip state, since stencil doesn't account for outline
1494 bool storedSkipOutlineClip = mSkipOutlineClip;
1495 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001496
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001497 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001498 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001499 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1500
Rob Tsuk487a92c2015-01-06 13:22:54 -08001501 if (isRectangleList) {
1502 drawRectangleList(clipArea.getRectangleList());
1503 } else {
1504 // NOTE: We could use the region contour path to generate a smaller mesh
1505 // Since we are using the stencil we could use the red book path
1506 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001507
Rob Tsuk487a92c2015-01-06 13:22:54 -08001508 // The last parameter is important: we are not drawing in the color buffer
1509 // so we don't want to dirty the current layer, if any
1510 drawRegionRects(clipArea.getClipRegion(), paint, false);
1511 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001512 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001513 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001514
Chris Craik96a5c4c2015-01-27 15:46:35 -08001515 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001516
1517 // Draw the region used to generate the stencil if the appropriate debug
1518 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001519 // TODO: Implement for rectangle list clip areas
1520 if (mCaches.debugStencilClip == Caches::kStencilShowRegion &&
1521 !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001522 paint.setColor(0x7f0000ff);
1523 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001524 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001525 }
Romain Guy8ce00302013-01-15 18:51:42 -08001526 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001527 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001528 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001529 }
1530 }
1531}
1532
Chris Craikf0a59072013-11-19 18:00:46 -08001533/**
1534 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1535 *
1536 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1537 * style, and tessellated AA ramp
1538 */
1539bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001540 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001541 bool snapOut = paint && paint->isAntiAlias();
1542
1543 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1544 float outset = paint->getStrokeWidth() * 0.5f;
1545 left -= outset;
1546 top -= outset;
1547 right += outset;
1548 bottom += outset;
1549 }
1550
Chris Craikdeeda3d2014-05-05 19:09:33 -07001551 bool clipRequired = false;
1552 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001553 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001554 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001555 return true;
1556 }
1557
Chris Craikf23b25a2014-06-26 15:46:20 -07001558 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001559 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001560 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001561 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001562}
1563
Romain Guy8ce00302013-01-15 18:51:42 -08001564void OpenGLRenderer::debugClip() {
1565#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001566 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001567 SkPaint paint;
1568 paint.setColor(0x7f00ff00);
1569 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1570
Romain Guy8ce00302013-01-15 18:51:42 -08001571 }
1572#endif
1573}
1574
Chris Craik117bdbc2015-02-05 10:12:38 -08001575void OpenGLRenderer::renderGlop(const Glop& glop) {
1576 if (mState.getDirtyClip()) {
1577 if (mRenderState.scissor().isEnabled()) {
1578 setScissorFromClip();
1579 }
1580
1581 setStencilFromClip();
1582 }
1583 mRenderState.render(glop);
Chris Craik2ab95d72015-02-06 15:25:51 -08001584
1585 if (!mRenderState.stencil().isWriteEnabled()) {
1586 // TODO: specify more clearly when a draw should dirty the layer.
1587 // is writing to the stencil the only time we should ignore this?
1588 dirtyLayer(glop.bounds.left, glop.bounds.top, glop.bounds.right, glop.bounds.bottom);
1589 }
Chris Craik117bdbc2015-02-05 10:12:38 -08001590}
1591
Romain Guyf6a11b82010-06-23 17:47:49 -07001592///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001593// Drawing commands
1594///////////////////////////////////////////////////////////////////////////////
1595
Chris Craik62d307c2014-07-29 10:35:13 -07001596void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001597 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001598 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001599 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001600 // Make sure setScissor & setStencil happen at the beginning of
1601 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001602 if (mState.getDirtyClip()) {
Chris Craik65fe5ee2015-01-26 18:06:29 -08001603 if (mRenderState.scissor().isEnabled()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001604 setScissorFromClip();
1605 }
Chris Craik62d307c2014-07-29 10:35:13 -07001606
Dohyun Leeadc0d9d2014-11-24 21:08:15 +09001607 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001608 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001609
Romain Guy70ca14e2010-12-13 18:24:33 -08001610 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001611
Romain Guy70ca14e2010-12-13 18:24:33 -08001612 mSetShaderColor = false;
1613 mColorSet = false;
1614 mColorA = mColorR = mColorG = mColorB = 0.0f;
1615 mTextureUnit = 0;
1616 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001617
1618 // Enable debug highlight when what we're about to draw is tested against
1619 // the stencil buffer and if stencil highlight debugging is on
Chris Craik117bdbc2015-02-05 10:12:38 -08001620 mDescription.hasDebugHighlight = !mCaches.debugOverdraw
1621 && mCaches.debugStencilClip == Caches::kStencilShowHighlight
1622 && mRenderState.stencil().isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001623}
1624
1625void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1626 mDescription.hasTexture = true;
1627 mDescription.hasAlpha8Texture = isAlpha8;
1628}
1629
Romain Guyff316ec2013-02-13 18:39:43 -08001630void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1631 mDescription.hasTexture = true;
1632 mDescription.hasColors = true;
1633 mDescription.hasAlpha8Texture = isAlpha8;
1634}
1635
Romain Guyaa6c24c2011-04-28 18:40:04 -07001636void OpenGLRenderer::setupDrawWithExternalTexture() {
1637 mDescription.hasExternalTexture = true;
1638}
1639
Romain Guy15bc6432011-12-13 13:11:32 -08001640void OpenGLRenderer::setupDrawNoTexture() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001641 mRenderState.meshState().disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001642}
1643
Chris Craik91a8c7c2014-08-12 14:31:35 -07001644void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1645 mDescription.hasVertexAlpha = true;
1646 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001647}
1648
Romain Guy8d0d4782010-12-14 20:13:35 -08001649void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1650 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001651 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1652 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1653 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001654 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001655 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001656}
1657
Romain Guy86568192010-12-14 15:55:39 -08001658void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1659 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001660 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1661 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1662 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001663 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001664 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001665}
1666
Romain Guy41210632012-07-16 17:04:24 -07001667void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1668 mCaches.fontRenderer->describe(mDescription, paint);
1669}
1670
Romain Guy70ca14e2010-12-13 18:24:33 -08001671void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1672 mColorA = a;
1673 mColorR = r;
1674 mColorG = g;
1675 mColorB = b;
1676 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001677 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001678}
1679
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001680void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
Chris Craike84a2082014-12-22 14:28:49 -08001681 if (shader != nullptr) {
Chris Craik117bdbc2015-02-05 10:12:38 -08001682 SkiaShader::describe(&mCaches, mDescription, mCaches.extensions(), *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001683 }
1684}
1685
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001686void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001687 if (filter == nullptr) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001688 return;
1689 }
1690
1691 SkXfermode::Mode mode;
Chris Craike84a2082014-12-22 14:28:49 -08001692 if (filter->asColorMode(nullptr, &mode)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001693 mDescription.colorOp = ProgramDescription::kColorBlend;
1694 mDescription.colorMode = mode;
Chris Craike84a2082014-12-22 14:28:49 -08001695 } else if (filter->asColorMatrix(nullptr)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001696 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001697 }
1698}
1699
Romain Guyf09ef512011-05-27 11:43:46 -07001700void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1701 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1702 mColorA = 1.0f;
1703 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001704 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001705 }
1706}
1707
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001708void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1709 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001710 // When the blending mode is kClear_Mode, we need to use a modulate color
1711 // argb=1,0,0,0
1712 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001713 // TODO: check shader blending, once we have shader drawing support for layers.
Tom Hudson8dfaa492014-12-09 15:03:44 -05001714 bool blend = layer->isBlend()
1715 || getLayerAlpha(layer) < 1.0f
1716 || (mColorSet && mColorA < 1.0f)
1717 || PaintUtils::isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001718 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001719}
1720
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001721void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1722 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001723 // When the blending mode is kClear_Mode, we need to use a modulate color
1724 // argb=1,0,0,0
1725 accountForClear(mode);
Chris Craik03188872015-02-02 18:39:33 -08001726 blend |= (mColorSet && mColorA < 1.0f)
1727 || (getShader(paint) && !getShader(paint)->isOpaque())
1728 || PaintUtils::isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001729 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001730}
1731
1732void OpenGLRenderer::setupDrawProgram() {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001733 mCaches.setProgram(mDescription);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001734 if (mDescription.hasRoundRectClip) {
1735 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001736 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001737 const Rect& innerRect = state->innerRect;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001738 glUniform4f(mCaches.program().getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001739 innerRect.left, innerRect.top,
1740 innerRect.right, innerRect.bottom);
Chris Craik6c15ffa2015-02-02 13:50:55 -08001741 glUniformMatrix4fv(mCaches.program().getUniform("roundRectInvTransform"),
Chris Craikdeeda3d2014-05-05 19:09:33 -07001742 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001743
1744 // add half pixel to round out integer rect space to cover pixel centers
1745 float roundedOutRadius = state->radius + 0.5f;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001746 glUniform1f(mCaches.program().getUniform("roundRectRadius"),
Chris Craik4340c262014-09-11 18:58:45 -07001747 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001748 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001749}
1750
1751void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1752 mTrackDirtyRegions = false;
1753}
1754
Chris Craik4063a0e2013-11-15 16:06:56 -08001755void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1756 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001757 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001758 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001759 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001760 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001761
Romain Guy86568192010-12-14 15:55:39 -08001762 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001763 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001764
1765 mCaches.program().set(currentSnapshot()->getOrthoMatrix(),
Chris Craike84a2082014-12-22 14:28:49 -08001766 mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001767 if (dirty && mTrackDirtyRegions) {
1768 if (!ignoreTransform) {
1769 dirtyLayer(left, top, right, bottom, *currentTransform());
1770 } else {
1771 dirtyLayer(left, top, right, bottom);
1772 }
Romain Guy86568192010-12-14 15:55:39 -08001773 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001774}
1775
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001776void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1777 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001778 mCaches.program().setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001779 }
1780}
1781
Romain Guy86568192010-12-14 15:55:39 -08001782void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001783 if (mSetShaderColor) {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001784 mCaches.program().setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001785 }
1786}
1787
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001788void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
Chris Craike84a2082014-12-22 14:28:49 -08001789 if (shader == nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001790 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001791 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001792
1793 if (ignoreTransform) {
1794 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1795 // because it was built into modelView / the geometry, and the description needs to
1796 // compensate.
1797 mat4 modelViewWithoutTransform;
1798 modelViewWithoutTransform.loadInverse(*currentTransform());
1799 modelViewWithoutTransform.multiply(mModelViewMatrix);
1800 mModelViewMatrix.load(modelViewWithoutTransform);
1801 }
1802
Chris Craik117bdbc2015-02-05 10:12:38 -08001803 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit,
1804 mCaches.extensions(), *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001805}
1806
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001807void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001808 if (nullptr == filter) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001809 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001810 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001811
1812 SkColor color;
1813 SkXfermode::Mode mode;
1814 if (filter->asColorMode(&color, &mode)) {
1815 const int alpha = SkColorGetA(color);
1816 const GLfloat a = alpha / 255.0f;
1817 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1818 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1819 const GLfloat b = a * SkColorGetB(color) / 255.0f;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001820 glUniform4f(mCaches.program().getUniform("colorBlend"), r, g, b, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001821 return;
1822 }
1823
1824 SkScalar srcColorMatrix[20];
1825 if (filter->asColorMatrix(srcColorMatrix)) {
1826
1827 float colorMatrix[16];
1828 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1829 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1830 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1831 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1832
1833 // Skia uses the range [0..255] for the addition vector, but we need
1834 // the [0..1] range to apply the vector in GLSL
1835 float colorVector[4];
1836 colorVector[0] = srcColorMatrix[4] / 255.0f;
1837 colorVector[1] = srcColorMatrix[9] / 255.0f;
1838 colorVector[2] = srcColorMatrix[14] / 255.0f;
1839 colorVector[3] = srcColorMatrix[19] / 255.0f;
1840
Chris Craik6c15ffa2015-02-02 13:50:55 -08001841 glUniformMatrix4fv(mCaches.program().getUniform("colorMatrix"), 1,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001842 GL_FALSE, colorMatrix);
Chris Craik6c15ffa2015-02-02 13:50:55 -08001843 glUniform4fv(mCaches.program().getUniform("colorMatrixVector"), 1, colorVector);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001844 return;
1845 }
1846
1847 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001848}
1849
Romain Guy41210632012-07-16 17:04:24 -07001850void OpenGLRenderer::setupDrawTextGammaUniforms() {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001851 mCaches.fontRenderer->setupProgram(mDescription, mCaches.program());
Romain Guy41210632012-07-16 17:04:24 -07001852}
1853
Romain Guy70ca14e2010-12-13 18:24:33 -08001854void OpenGLRenderer::setupDrawSimpleMesh() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001855 bool force = mRenderState.meshState().bindMeshBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001856 mRenderState.meshState().bindPositionVertexPointer(force, nullptr);
Chris Craik96a5c4c2015-01-27 15:46:35 -08001857 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001858}
1859
1860void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001861 if (texture) mCaches.textureState().bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001862 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001863 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001864}
1865
Romain Guyaa6c24c2011-04-28 18:40:04 -07001866void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001867 mCaches.textureState().bindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001868 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001869 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001870}
1871
Romain Guy8f0095c2011-05-02 17:24:22 -07001872void OpenGLRenderer::setupDrawTextureTransform() {
1873 mDescription.hasTextureTransform = true;
1874}
1875
1876void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001877 glUniformMatrix4fv(mCaches.program().getUniform("mainTextureTransform"), 1,
Romain Guyaa6c24c2011-04-28 18:40:04 -07001878 GL_FALSE, &transform.data[0]);
1879}
1880
Chris Craik564acf72014-01-02 16:46:18 -08001881void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1882 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001883 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001884 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001885 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001886 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001887 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001888 }
Romain Guyd71dd362011-12-12 19:03:35 -08001889
Chris Craik6c15ffa2015-02-02 13:50:55 -08001890 mRenderState.meshState().bindPositionVertexPointer(force, vertices);
1891 if (mCaches.program().texCoords >= 0) {
1892 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001893 }
1894
Chris Craik96a5c4c2015-01-27 15:46:35 -08001895 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -08001896}
1897
Chris Craik564acf72014-01-02 16:46:18 -08001898void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1899 const GLvoid* texCoords, const GLvoid* colors) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001900 bool force = mRenderState.meshState().unbindMeshBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001901 GLsizei stride = sizeof(ColorTextureVertex);
1902
Chris Craik6c15ffa2015-02-02 13:50:55 -08001903 mRenderState.meshState().bindPositionVertexPointer(force, vertices, stride);
1904 if (mCaches.program().texCoords >= 0) {
1905 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords, stride);
Romain Guyff316ec2013-02-13 18:39:43 -08001906 }
Chris Craik6c15ffa2015-02-02 13:50:55 -08001907 int slot = mCaches.program().getAttrib("colors");
Romain Guyff316ec2013-02-13 18:39:43 -08001908 if (slot >= 0) {
1909 glEnableVertexAttribArray(slot);
1910 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1911 }
1912
Chris Craik96a5c4c2015-01-27 15:46:35 -08001913 mRenderState.meshState().unbindIndicesBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001914}
1915
Chris Craik564acf72014-01-02 16:46:18 -08001916void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1917 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001918 bool force = false;
1919 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1920 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
Chris Craik96a5c4c2015-01-27 15:46:35 -08001921 // use the default VBO found in RenderState
Romain Guy3b748a42013-04-17 18:54:38 -07001922 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001923 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy3b748a42013-04-17 18:54:38 -07001924 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001925 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001926 }
Chris Craik96a5c4c2015-01-27 15:46:35 -08001927 mRenderState.meshState().bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001928
Chris Craik6c15ffa2015-02-02 13:50:55 -08001929 mRenderState.meshState().bindPositionVertexPointer(force, vertices);
1930 if (mCaches.program().texCoords >= 0) {
1931 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001932 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001933}
1934
Romain Guy448455f2013-07-22 13:57:50 -07001935void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001936 bool force = mRenderState.meshState().unbindMeshBuffer();
1937 mRenderState.meshState().bindQuadIndicesBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001938 mRenderState.meshState().bindPositionVertexPointer(force, vertices, kVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001939}
1940
Romain Guy70ca14e2010-12-13 18:24:33 -08001941///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001942// Drawing
1943///////////////////////////////////////////////////////////////////////////////
1944
Tom Hudson107843d2014-09-08 11:26:26 -04001945void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001946 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1947 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001948 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001949 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001950 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001951 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001952 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001953 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001954 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001955 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001956 }
1957
Chris Craikef8d6f22014-12-17 11:10:28 -08001958 // Don't avoid overdraw when visualizing, since that makes it harder to
1959 // debug where it's coming from, and when the problem occurs.
1960 bool avoidOverdraw = !mCaches.debugOverdraw;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001961 DeferredDisplayList deferredList(mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001962 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001963 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001964
1965 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001966 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001967
Tom Hudson107843d2014-09-08 11:26:26 -04001968 deferredList.flush(*this, dirty);
1969 } else {
1970 // Even if there is no drawing command(Ex: invisible),
1971 // it still needs startFrame to clear buffer and start tiling.
1972 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001973 }
1974}
1975
Chris Craike84a2082014-12-22 14:28:49 -08001976void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top,
1977 const SkPaint* paint) {
Romain Guya168d732011-03-18 16:50:13 -07001978 float x = left;
1979 float y = top;
1980
Romain Guy886b2752013-01-04 12:26:18 -08001981 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1982
Romain Guya168d732011-03-18 16:50:13 -07001983 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001984 if (currentTransform()->isPureTranslate()) {
1985 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1986 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001987 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001988
1989 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001990 } else {
Chris Craik678625242014-02-28 12:26:34 -08001991 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001992 }
1993
Romain Guy3b748a42013-04-17 18:54:38 -07001994 // No need to check for a UV mapper on the texture object, only ARGB_8888
1995 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001996 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Chris Craik96a5c4c2015-01-27 15:46:35 -08001997 paint, (GLvoid*) nullptr, (GLvoid*) kMeshTextureOffset,
Chris Craik117bdbc2015-02-05 10:12:38 -08001998 GL_TRIANGLE_STRIP, kUnitQuadCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001999}
2000
Romain Guy03c00b52013-06-20 18:30:28 -07002001/**
2002 * Important note: this method is intended to draw batches of bitmaps and
2003 * will not set the scissor enable or dirty the current layer, if any.
2004 * The caller is responsible for properly dirtying the current layer.
2005 */
Tom Hudson107843d2014-09-08 11:26:26 -04002006void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002007 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
2008 const Rect& bounds, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002009 mCaches.textureState().activateTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002010 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002011 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07002012
Chris Craik527a3aa2013-03-04 10:19:31 -08002013 const AutoTexture autoCleanup(texture);
2014
Chris Craik527a3aa2013-03-04 10:19:31 -08002015 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002016 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002017
2018 const float x = (int) floorf(bounds.left + 0.5f);
2019 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04002020 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002021 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002022 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002023 GL_TRIANGLES, bitmapCount * 6, true,
2024 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002025 } else {
2026 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002027 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002028 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2029 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002030 }
2031
Tom Hudson107843d2014-09-08 11:26:26 -04002032 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08002033}
2034
Tom Hudson107843d2014-09-08 11:26:26 -04002035void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002036 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002037 return;
Romain Guy6926c722010-07-12 20:20:03 -07002038 }
2039
Chris Craik44eb2c02015-01-29 09:45:09 -08002040 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002041 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002042 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002043 const AutoTexture autoCleanup(texture);
2044
Mike Reed1103b322014-07-08 12:36:44 -04002045 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002046 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002047 } else {
Chris Craik79647502014-08-06 13:42:24 -07002048 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002049 }
Chet Haase48659092012-05-31 15:21:51 -07002050
Tom Hudson107843d2014-09-08 11:26:26 -04002051 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002052}
2053
Tom Hudson107843d2014-09-08 11:26:26 -04002054void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002055 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002056 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002057 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002058 }
2059
Chris Craik39a908c2013-06-13 14:39:01 -07002060 // TODO: use quickReject on bounds from vertices
Chris Craik65fe5ee2015-01-26 18:06:29 -08002061 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002062
Romain Guyb18d2d02011-02-10 15:52:54 -08002063 float left = FLT_MAX;
2064 float top = FLT_MAX;
2065 float right = FLT_MIN;
2066 float bottom = FLT_MIN;
2067
Romain Guya92bb4d2012-10-16 11:08:44 -07002068 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002069
Chris Craik51d6a3d2014-12-22 17:16:56 -08002070 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[count]);
2071 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08002072
Chris Craik51d6a3d2014-12-22 17:16:56 -08002073 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002074 if (!colors) {
2075 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08002076 tempColors.reset(new int[colorsCount]);
2077 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
2078 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08002079 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002080
Chris Craik44eb2c02015-01-29 09:45:09 -08002081 mCaches.textureState().activateTexture(0);
John Reckebd52612014-12-10 16:47:36 -08002082 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002083 const UvMapper& mapper(getMapper(texture));
2084
Romain Guy5a7b4662011-01-20 19:09:30 -08002085 for (int32_t y = 0; y < meshHeight; y++) {
2086 for (int32_t x = 0; x < meshWidth; x++) {
2087 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2088
2089 float u1 = float(x) / meshWidth;
2090 float u2 = float(x + 1) / meshWidth;
2091 float v1 = float(y) / meshHeight;
2092 float v2 = float(y + 1) / meshHeight;
2093
Romain Guy3b748a42013-04-17 18:54:38 -07002094 mapper.map(u1, v1, u2, v2);
2095
Romain Guy5a7b4662011-01-20 19:09:30 -08002096 int ax = i + (meshWidth + 1) * 2;
2097 int ay = ax + 1;
2098 int bx = i;
2099 int by = bx + 1;
2100 int cx = i + 2;
2101 int cy = cx + 1;
2102 int dx = i + (meshWidth + 1) * 2 + 2;
2103 int dy = dx + 1;
2104
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[ax], vertices[ay], u1, v2, colors[ax / 2]);
2107 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002108
Romain Guyff316ec2013-02-13 18:39:43 -08002109 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2110 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2111 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002112
Romain Guya92bb4d2012-10-16 11:08:44 -07002113 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2114 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2115 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2116 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002117 }
2118 }
2119
Chris Craikf0a59072013-11-19 18:00:46 -08002120 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002121 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002122 }
2123
Romain Guyff316ec2013-02-13 18:39:43 -08002124 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002125 texture = mCaches.textureCache.get(bitmap);
2126 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04002127 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002128 }
Romain Guyff316ec2013-02-13 18:39:43 -08002129 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002130 const AutoTexture autoCleanup(texture);
2131
2132 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002133 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002134
2135 int alpha;
2136 SkXfermode::Mode mode;
2137 getAlphaAndMode(paint, &alpha, &mode);
2138
Romain Guyff316ec2013-02-13 18:39:43 -08002139 float a = alpha / 255.0f;
2140
Romain Guya92bb4d2012-10-16 11:08:44 -07002141 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002142 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002143 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002144
Romain Guyff316ec2013-02-13 18:39:43 -08002145 setupDraw();
2146 setupDrawWithTextureAndColor();
2147 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002148 setupDrawColorFilter(getColorFilter(paint));
2149 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002150 setupDrawProgram();
2151 setupDrawDirtyRegionsDisabled();
Chris Craik117bdbc2015-02-05 10:12:38 -08002152 setupDrawModelView(kModelViewMode_Translate, false, 0, 0, 0, 0);
Romain Guyff316ec2013-02-13 18:39:43 -08002153 setupDrawTexture(texture->id);
2154 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002155 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002156 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002157
2158 glDrawArrays(GL_TRIANGLES, 0, count);
2159
Chris Craik6c15ffa2015-02-02 13:50:55 -08002160 int slot = mCaches.program().getAttrib("colors");
Romain Guyff316ec2013-02-13 18:39:43 -08002161 if (slot >= 0) {
2162 glDisableVertexAttribArray(slot);
2163 }
2164
Tom Hudson107843d2014-09-08 11:26:26 -04002165 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002166}
2167
Tom Hudson107843d2014-09-08 11:26:26 -04002168void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002169 float srcLeft, float srcTop, float srcRight, float srcBottom,
2170 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002171 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002172 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002173 return;
Romain Guy6926c722010-07-12 20:20:03 -07002174 }
2175
Chris Craik44eb2c02015-01-29 09:45:09 -08002176 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002177 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002178 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002179 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002180
Romain Guy8ba548f2010-06-30 19:21:21 -07002181 const float width = texture->width;
2182 const float height = texture->height;
2183
Romain Guy3b748a42013-04-17 18:54:38 -07002184 float u1 = fmax(0.0f, srcLeft / width);
2185 float v1 = fmax(0.0f, srcTop / height);
2186 float u2 = fmin(1.0f, srcRight / width);
2187 float v2 = fmin(1.0f, srcBottom / height);
2188
2189 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002190
Chris Craik96a5c4c2015-01-27 15:46:35 -08002191 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002192 resetDrawTextureTexCoords(u1, v1, u2, v2);
2193
Romain Guyd21b6e12011-11-30 20:21:23 -08002194 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2195
Romain Guy886b2752013-01-04 12:26:18 -08002196 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2197 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002198
Romain Guy886b2752013-01-04 12:26:18 -08002199 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2200 // Apply a scale transform on the canvas only when a shader is in use
2201 // Skia handles the ratio between the dst and src rects as a scale factor
2202 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002203 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002204 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002205
Chris Craikd6b65f62014-01-01 14:45:21 -08002206 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2207 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2208 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002209
2210 dstRight = x + (dstRight - dstLeft);
2211 dstBottom = y + (dstBottom - dstTop);
2212
2213 dstLeft = x;
2214 dstTop = y;
2215
Chris Craik678625242014-02-28 12:26:34 -08002216 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002217 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002218 } else {
Chris Craik678625242014-02-28 12:26:34 -08002219 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002220 }
2221
2222 if (CC_UNLIKELY(useScaleTransform)) {
2223 save(SkCanvas::kMatrix_SaveFlag);
2224 translate(dstLeft, dstTop);
2225 scale(scaleX, scaleY);
2226
2227 dstLeft = 0.0f;
2228 dstTop = 0.0f;
2229
2230 dstRight = srcRight - srcLeft;
2231 dstBottom = srcBottom - srcTop;
2232 }
2233
Mike Reed1103b322014-07-08 12:36:44 -04002234 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002235 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002236 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002237 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik117bdbc2015-02-05 10:12:38 -08002238 GL_TRIANGLE_STRIP, kUnitQuadCount, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002239 } else {
2240 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002241 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002242 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik117bdbc2015-02-05 10:12:38 -08002243 GL_TRIANGLE_STRIP, kUnitQuadCount, false, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002244 }
2245
2246 if (CC_UNLIKELY(useScaleTransform)) {
2247 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002248 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002249
2250 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002251
Tom Hudson107843d2014-09-08 11:26:26 -04002252 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002253}
2254
Tom Hudson107843d2014-09-08 11:26:26 -04002255void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002256 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002257 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002258 return;
Romain Guy6926c722010-07-12 20:20:03 -07002259 }
2260
John Reckebd52612014-12-10 16:47:36 -08002261 AssetAtlas::Entry* entry = mRenderState.assetAtlas().getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002262 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2263 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002264
Tom Hudson107843d2014-09-08 11:26:26 -04002265 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002266}
2267
Tom Hudson107843d2014-09-08 11:26:26 -04002268void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002269 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2270 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002271 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002272 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002273 }
2274
Romain Guy211370f2012-02-01 16:10:55 -08002275 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002276 mCaches.textureState().activateTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002277 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002278 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002279 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002280
Romain Guya4adcf02013-02-28 12:15:35 -08002281 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2282 texture->setFilter(GL_LINEAR, true);
2283
Chris Craikd6b65f62014-01-01 14:45:21 -08002284 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002285 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002286 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002287 const float offsetX = left + currentTransform()->getTranslateX();
2288 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002289 const size_t count = mesh->quads.size();
2290 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002291 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002292 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002293 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2294 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2295 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002296 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002297 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002298 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002299 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002300 }
2301 }
2302
Chris Craik4063a0e2013-11-15 16:06:56 -08002303 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002304 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002305 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2306 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002307
Romain Guy3b748a42013-04-17 18:54:38 -07002308 right = x + right - left;
2309 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002310 left = x;
2311 top = y;
2312 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002313 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002314 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2315 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002316 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2317 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002318 }
Chet Haase48659092012-05-31 15:21:51 -07002319
Tom Hudson107843d2014-09-08 11:26:26 -04002320 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002321}
2322
Romain Guy03c00b52013-06-20 18:30:28 -07002323/**
2324 * Important note: this method is intended to draw batches of 9-patch objects and
2325 * will not set the scissor enable or dirty the current layer, if any.
2326 * The caller is responsible for properly dirtying the current layer.
2327 */
Tom Hudson107843d2014-09-08 11:26:26 -04002328void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002329 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002330 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07002331 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002332 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002333 const AutoTexture autoCleanup(texture);
2334
2335 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2336 texture->setFilter(GL_LINEAR, true);
2337
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002338 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2339 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002340 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002341
Tom Hudson107843d2014-09-08 11:26:26 -04002342 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002343}
2344
Tom Hudson107843d2014-09-08 11:26:26 -04002345void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002346 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002347 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002348 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002349 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002350 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002351 }
2352
Chris Craik117bdbc2015-02-05 10:12:38 -08002353 if (!paint->getShader() && !currentSnapshot()->roundRectClipState) {
2354 Glop glop;
2355 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
2356 bool fudgeOffset = displayFlags & kVertexBuffer_Offset;
2357 bool shadowInterp = displayFlags & kVertexBuffer_ShadowInterp;
2358 aBuilder.setMeshVertexBuffer(vertexBuffer, shadowInterp)
2359 .setTransform(currentSnapshot()->getOrthoMatrix(), *currentTransform(), fudgeOffset)
2360 .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds())
Chris Craik2ab95d72015-02-06 15:25:51 -08002361 .setPaint(*paint, currentSnapshot()->alpha)
Chris Craik117bdbc2015-02-05 10:12:38 -08002362 .build();
2363 renderGlop(glop);
2364 return;
2365 }
2366
Chris Craik117bdbc2015-02-05 10:12:38 -08002367 const VertexBuffer::MeshFeatureFlags meshFeatureFlags = vertexBuffer.getMeshFeatureFlags();
Chris Craik0d5ac952014-07-15 13:01:02 -07002368 Rect bounds(vertexBuffer.getBounds());
2369 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002370 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2371
Chris Craikcb4d6002012-09-25 12:00:29 -07002372 int color = paint->getColor();
Chris Craik117bdbc2015-02-05 10:12:38 -08002373 bool isAA = meshFeatureFlags & VertexBuffer::kAlpha;
Chris Craikcb4d6002012-09-25 12:00:29 -07002374
Chris Craik710f46d2012-09-17 17:25:49 -07002375 setupDraw();
2376 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002377 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Chris Craik117bdbc2015-02-05 10:12:38 -08002378 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002379 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002380 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002381 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002382 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002383 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2384 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002385 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002386 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002387 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002388
ztenghui55bfb4e2013-12-03 10:38:55 -08002389 const void* vertices = vertexBuffer.getBuffer();
Chris Craik96a5c4c2015-01-27 15:46:35 -08002390 mRenderState.meshState().unbindMeshBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08002391 mRenderState.meshState().bindPositionVertexPointer(true, vertices,
2392 isAA ? kAlphaVertexStride : kVertexStride);
Chris Craik96a5c4c2015-01-27 15:46:35 -08002393 mRenderState.meshState().resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002394
Chris Craik710f46d2012-09-17 17:25:49 -07002395 int alphaSlot = -1;
2396 if (isAA) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002397 void* alphaCoords = ((GLbyte*) vertices) + kVertexAlphaOffset;
Chris Craik6c15ffa2015-02-02 13:50:55 -08002398 alphaSlot = mCaches.program().getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002399 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002400 glEnableVertexAttribArray(alphaSlot);
Chris Craik96a5c4c2015-01-27 15:46:35 -08002401 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, kAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002402 }
Romain Guy04299382012-07-18 17:15:41 -07002403
Chris Craik117bdbc2015-02-05 10:12:38 -08002404 if (meshFeatureFlags & VertexBuffer::kIndices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002405 mRenderState.meshState().unbindIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002406 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(),
2407 GL_UNSIGNED_SHORT, vertexBuffer.getIndices());
Chris Craik117bdbc2015-02-05 10:12:38 -08002408 } else {
2409 mRenderState.meshState().unbindIndicesBuffer();
2410 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
ztenghui63d41ab2014-02-14 13:13:41 -08002411 }
Romain Guy04299382012-07-18 17:15:41 -07002412
Chris Craik710f46d2012-09-17 17:25:49 -07002413 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002414 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002415 }
Chris Craik65cd6122012-12-10 17:56:27 -08002416
Tom Hudson107843d2014-09-08 11:26:26 -04002417 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002418}
2419
2420/**
Chris Craik65cd6122012-12-10 17:56:27 -08002421 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2422 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2423 * screen space in all directions. However, instead of using a fragment shader to compute the
2424 * translucency of the color from its position, we simply use a varying parameter to define how far
2425 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2426 *
2427 * Doesn't yet support joins, caps, or path effects.
2428 */
Tom Hudson107843d2014-09-08 11:26:26 -04002429void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002430 VertexBuffer vertexBuffer;
2431 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002432 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002433 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002434}
2435
2436/**
2437 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2438 * and additional geometry for defining an alpha slope perimeter.
2439 *
2440 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2441 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2442 * in-shader alpha region, but found it to be taxing on some GPUs.
2443 *
2444 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2445 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002446 */
Tom Hudson107843d2014-09-08 11:26:26 -04002447void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002448 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002449
Chris Craik65cd6122012-12-10 17:56:27 -08002450 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002451
Chris Craik65cd6122012-12-10 17:56:27 -08002452 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002453 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2454 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002455
Chris Craik05f3d6e2014-06-02 16:27:04 -07002456 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002457 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002458 }
2459
Chris Craikbf759452014-08-11 16:00:44 -07002460 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002461 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002462}
2463
Tom Hudson107843d2014-09-08 11:26:26 -04002464void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002465 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002466
Chris Craik6d29c8d2013-05-08 18:35:44 -07002467 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002468
Chris Craik6d29c8d2013-05-08 18:35:44 -07002469 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002470 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002471
Chris Craik05f3d6e2014-06-02 16:27:04 -07002472 const Rect& bounds = buffer.getBounds();
2473 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002474 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002475 }
2476
Chris Craikbf759452014-08-11 16:00:44 -07002477 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002478 drawVertexBuffer(buffer, paint, displayFlags);
2479
2480 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002481}
2482
Tom Hudson107843d2014-09-08 11:26:26 -04002483void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002484 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002485 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002486
Rob Tsuk487a92c2015-01-06 13:22:54 -08002487 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002488 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002489
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002490 SkPaint paint;
2491 paint.setColor(color);
2492 paint.setXfermodeMode(mode);
2493
2494 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002495
Tom Hudson107843d2014-09-08 11:26:26 -04002496 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002497}
Romain Guy9d5316e2010-06-24 19:30:36 -07002498
Tom Hudson107843d2014-09-08 11:26:26 -04002499void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002500 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002501 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002502 const AutoTexture autoCleanup(texture);
2503
2504 const float x = left + texture->left - texture->offset;
2505 const float y = top + texture->top - texture->offset;
2506
2507 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002508
Tom Hudson107843d2014-09-08 11:26:26 -04002509 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002510}
2511
Tom Hudson107843d2014-09-08 11:26:26 -04002512void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002513 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002514 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002515 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002516 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002517 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002518 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002519
Chris Craike84a2082014-12-22 14:28:49 -08002520 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002521 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002522 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002523 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002524 drawShape(left, top, texture, p);
2525 } else {
2526 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2527 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2528 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002529 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002530}
2531
Tom Hudson107843d2014-09-08 11:26:26 -04002532void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002533 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002534 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002535 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002536 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002537 }
Chris Craike84a2082014-12-22 14:28:49 -08002538 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002539 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002540 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002541 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002542 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002543 SkPath path;
2544 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2545 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2546 } else {
2547 path.addCircle(x, y, radius);
2548 }
2549 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002550 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002551}
Romain Guy01d58e42011-01-19 21:54:02 -08002552
Tom Hudson107843d2014-09-08 11:26:26 -04002553void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002554 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002555 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002556 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002557 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002558 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002559 }
Romain Guy01d58e42011-01-19 21:54:02 -08002560
Chris Craike84a2082014-12-22 14:28:49 -08002561 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002562 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002563 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002564 drawShape(left, top, texture, p);
2565 } else {
2566 SkPath path;
2567 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2568 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2569 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2570 }
2571 path.addOval(rect);
2572 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002573 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002574}
2575
Tom Hudson107843d2014-09-08 11:26:26 -04002576void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002577 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002578 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002579 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002580 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002581 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002582 }
2583
Chris Craik780c1282012-10-04 14:10:49 -07002584 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08002585 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002586 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002587 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002588 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002589 drawShape(left, top, texture, p);
2590 return;
Chris Craik780c1282012-10-04 14:10:49 -07002591 }
Chris Craik780c1282012-10-04 14:10:49 -07002592 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2593 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2594 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2595 }
2596
2597 SkPath path;
2598 if (useCenter) {
2599 path.moveTo(rect.centerX(), rect.centerY());
2600 }
2601 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2602 if (useCenter) {
2603 path.close();
2604 }
Tom Hudson107843d2014-09-08 11:26:26 -04002605 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002606}
2607
Romain Guycf8675e2012-10-02 12:32:25 -07002608// See SkPaintDefaults.h
2609#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2610
Tom Hudson107843d2014-09-08 11:26:26 -04002611void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002612 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002613 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002614 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002615 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002616 return;
Romain Guy6926c722010-07-12 20:20:03 -07002617 }
2618
Chris Craik710f46d2012-09-17 17:25:49 -07002619 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002620 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08002621 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07002622 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002623 mCaches.textureState().activateTexture(0);
Romain Guycf8675e2012-10-02 12:32:25 -07002624 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002625 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002626 drawShape(left, top, texture, p);
2627 } else {
2628 SkPath path;
2629 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2630 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2631 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2632 }
2633 path.addRect(rect);
2634 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002635 }
Chet Haase858aa932011-05-12 09:06:00 -07002636 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002637 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2638 SkPath path;
2639 path.addRect(left, top, right, bottom);
2640 drawConvexPath(path, p);
2641 } else {
2642 drawColorRect(left, top, right, bottom, p);
2643
2644 mDirty = true;
2645 }
Chet Haase858aa932011-05-12 09:06:00 -07002646 }
Romain Guyc7d53492010-06-25 13:41:57 -07002647}
Romain Guy9d5316e2010-06-24 19:30:36 -07002648
Chris Craikd218a922014-01-02 17:13:34 -08002649void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2650 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002651 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002652 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07002653
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002654 TextShadow textShadow;
2655 if (!getTextShadow(paint, &textShadow)) {
2656 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2657 }
2658
Raph Levien416a8472012-07-19 22:48:17 -07002659 // NOTE: The drop shadow will not perform gamma correction
2660 // if shader-based correction is enabled
2661 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2662 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002663 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002664 // If the drop shadow exceeds the max texture size or couldn't be
2665 // allocated, skip drawing
2666 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002667 const AutoTexture autoCleanup(shadow);
2668
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002669 const float sx = x - shadow->left + textShadow.dx;
2670 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002671
Tom Hudson984162f2014-10-10 13:38:16 -04002672 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002673 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002674 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002675 }
2676
2677 setupDraw();
2678 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002679 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002680 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002681 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002682 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002683 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002684 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2685 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002686 setupDrawTexture(shadow->id);
2687 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002688 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002689 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08002690 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Raph Levien416a8472012-07-19 22:48:17 -07002691
Chris Craik117bdbc2015-02-05 10:12:38 -08002692 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Raph Levien416a8472012-07-19 22:48:17 -07002693}
2694
Romain Guy768bffc2013-02-27 13:50:45 -08002695bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002696 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05002697 return MathUtils::isZero(alpha)
2698 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002699}
2700
Tom Hudson107843d2014-09-08 11:26:26 -04002701void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002702 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002703 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002704 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002705 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002706
Romain Guy671d6cf2012-01-18 12:39:17 -08002707 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002708 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002709 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002710 }
2711
Chris Craik65fe5ee2015-01-26 18:06:29 -08002712 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002713
Romain Guy671d6cf2012-01-18 12:39:17 -08002714 float x = 0.0f;
2715 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002716 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002717 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002718 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2719 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002720 }
2721
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002722 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002723 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002724
2725 int alpha;
2726 SkXfermode::Mode mode;
2727 getAlphaAndMode(paint, &alpha, &mode);
2728
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002729 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002730 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002731 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002732 }
2733
Romain Guy671d6cf2012-01-18 12:39:17 -08002734 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002735 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002736 if (pureTranslate && !linearFilter) {
2737 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2738 }
Romain Guy257ae352013-03-20 16:31:12 -07002739 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002740
Rob Tsuk487a92c2015-01-06 13:22:54 -08002741 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002742 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2743
Romain Guy211370f2012-02-01 16:10:55 -08002744 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002745
Victoria Lease1e546812013-06-25 14:25:17 -07002746 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002747 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002748 positions, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002749 if (hasActiveLayer) {
2750 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002751 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002752 }
2753 dirtyLayerUnchecked(bounds, getRegion());
2754 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002755 }
Chet Haase48659092012-05-31 15:21:51 -07002756
Tom Hudson107843d2014-09-08 11:26:26 -04002757 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002758}
2759
Chris Craik59744b72014-07-01 17:56:52 -07002760bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002761 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002762 outMatrix->setIdentity();
2763 return false;
2764 } else if (CC_UNLIKELY(transform.isPerspective())) {
2765 outMatrix->setIdentity();
2766 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002767 }
Chris Craik59744b72014-07-01 17:56:52 -07002768
2769 /**
Chris Craika736cd92014-08-04 13:18:38 -07002770 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2771 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002772 */
2773 float sx, sy;
2774 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002775 outMatrix->setScale(
2776 roundf(fmaxf(1.0f, sx)),
2777 roundf(fmaxf(1.0f, sy)));
2778 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002779}
2780
Tom Hudson984162f2014-10-10 13:38:16 -04002781int OpenGLRenderer::getSaveCount() const {
2782 return mState.getSaveCount();
2783}
2784
2785int OpenGLRenderer::save(int flags) {
2786 return mState.save(flags);
2787}
2788
2789void OpenGLRenderer::restore() {
2790 return mState.restore();
2791}
2792
2793void OpenGLRenderer::restoreToCount(int saveCount) {
2794 return mState.restoreToCount(saveCount);
2795}
2796
2797void OpenGLRenderer::translate(float dx, float dy, float dz) {
2798 return mState.translate(dx, dy, dz);
2799}
2800
2801void OpenGLRenderer::rotate(float degrees) {
2802 return mState.rotate(degrees);
2803}
2804
2805void OpenGLRenderer::scale(float sx, float sy) {
2806 return mState.scale(sx, sy);
2807}
2808
2809void OpenGLRenderer::skew(float sx, float sy) {
2810 return mState.skew(sx, sy);
2811}
2812
2813void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2814 mState.setMatrix(matrix);
2815}
2816
2817void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2818 mState.concatMatrix(matrix);
2819}
2820
2821bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2822 return mState.clipRect(left, top, right, bottom, op);
2823}
2824
2825bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2826 return mState.clipPath(path, op);
2827}
2828
2829bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2830 return mState.clipRegion(region, op);
2831}
2832
2833void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2834 mState.setClippingOutline(allocator, outline);
2835}
2836
2837void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2838 const Rect& rect, float radius, bool highPriority) {
2839 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2840}
2841
Tom Hudson107843d2014-09-08 11:26:26 -04002842void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002843 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002844 DrawOpMode drawOpMode) {
2845
Chris Craik527a3aa2013-03-04 10:19:31 -08002846 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002847 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2848 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002849 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002850 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002851 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002852 }
Romain Guycac5fd32011-12-01 20:08:50 -08002853 }
2854
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002855 const float oldX = x;
2856 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002857
Chris Craikd6b65f62014-01-01 14:45:21 -08002858 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002859 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002860
Romain Guy211370f2012-02-01 16:10:55 -08002861 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002862 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2863 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002864 }
2865
Romain Guy86568192010-12-14 15:55:39 -08002866 int alpha;
2867 SkXfermode::Mode mode;
2868 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002869
Romain Guyc74f45a2013-02-26 19:10:14 -08002870 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2871
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002872 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002873 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002874 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002875 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002876 }
2877
Romain Guy19d4dd82013-03-04 11:14:26 -08002878 const bool hasActiveLayer = hasLayer();
2879
Romain Guy624234f2013-03-05 16:43:31 -08002880 // We only pass a partial transform to the font renderer. That partial
2881 // matrix defines how glyphs are rasterized. Typically we want glyphs
2882 // to be rasterized at their final size on screen, which means the partial
2883 // matrix needs to take the scale factor into account.
2884 // When a partial matrix is used to transform glyphs during rasterization,
2885 // the mesh is generated with the inverse transform (in the case of scale,
2886 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2887 // apply the full transform matrix at draw time in the vertex shader.
2888 // Applying the full matrix in the shader is the easiest way to handle
2889 // rotation and perspective and allows us to always generated quads in the
2890 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002891 SkMatrix fontTransform;
2892 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2893 || fabs(y - (int) y) > 0.0f
2894 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002895 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002896 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002897
Romain Guy624234f2013-03-05 16:43:31 -08002898 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08002899 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002900 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 -07002901
Raph Levien996e57c2012-07-23 15:22:52 -07002902 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002903 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002904
2905 // don't call issuedrawcommand, do it at end of batch
2906 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002907 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002908 SkPaint paintCopy(*paint);
2909 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2910 status = fontRenderer.renderPosText(&paintCopy, 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 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002913 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002914 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002915 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002916
Chris Craik527a3aa2013-03-04 10:19:31 -08002917 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002918 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002919 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002920 }
Chris Craik41541822013-05-03 16:35:54 -07002921 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002922 }
Romain Guy694b5192010-07-21 21:33:20 -07002923
Chris Craike63f7c622013-10-17 10:30:55 -07002924 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002925
Tom Hudson107843d2014-09-08 11:26:26 -04002926 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002927}
2928
Tom Hudson107843d2014-09-08 11:26:26 -04002929void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002930 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002931 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002932 return;
Romain Guy03d58522012-02-24 17:54:07 -08002933 }
2934
Chris Craik39a908c2013-06-13 14:39:01 -07002935 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002936 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002937
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002938 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002939 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002940 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002941
2942 int alpha;
2943 SkXfermode::Mode mode;
2944 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002945 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002946
Tom Hudson984162f2014-10-10 13:38:16 -04002947 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002948 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 -08002949
Romain Guy97771732012-02-28 18:17:02 -08002950 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002951
2952 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chris Craike84a2082014-12-22 14:28:49 -08002953 hOffset, vOffset, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002954 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002955 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002956 dirtyLayerUnchecked(bounds, getRegion());
2957 }
Romain Guy97771732012-02-28 18:17:02 -08002958 }
Chet Haase48659092012-05-31 15:21:51 -07002959
Tom Hudson107843d2014-09-08 11:26:26 -04002960 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002961}
2962
Tom Hudson107843d2014-09-08 11:26:26 -04002963void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002964 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002965
Chris Craik44eb2c02015-01-29 09:45:09 -08002966 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002967
Romain Guyfb8b7632010-08-23 21:05:08 -07002968 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002969 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002970 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002971
Romain Guy8b55f372010-08-18 17:10:07 -07002972 const float x = texture->left - texture->offset;
2973 const float y = texture->top - texture->offset;
2974
Romain Guy01d58e42011-01-19 21:54:02 -08002975 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002976 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002977}
2978
Tom Hudson107843d2014-09-08 11:26:26 -04002979void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002980 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002981 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002982 }
2983
Chris Craike84a2082014-12-22 14:28:49 -08002984 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002985 if (layer->isTextureLayer()) {
2986 transform = &layer->getTransform();
2987 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002988 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002989 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002990 }
2991 }
2992
Chris Craik39a908c2013-06-13 14:39:01 -07002993 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08002994 const bool rejected = mState.calculateQuickRejectForScissor(
2995 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2996 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002997
2998 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002999 if (transform && !transform->isIdentity()) {
3000 restore();
3001 }
Tom Hudson107843d2014-09-08 11:26:26 -04003002 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08003003 }
3004
Chris Craik62d307c2014-07-29 10:35:13 -07003005 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
3006 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
3007
Romain Guy5bb3c732012-11-29 17:52:58 -08003008 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003009
Chris Craik65fe5ee2015-01-26 18:06:29 -08003010 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08003011 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003012
Romain Guy211370f2012-02-01 16:10:55 -08003013 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08003014 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003015 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3016 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003017 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003018
Chris Craik16ecda52013-03-29 10:59:59 -07003019 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003020 setupDraw();
3021 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003022 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003023 setupDrawColorFilter(layer->getColorFilter());
3024 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003025 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003026 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003027 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07003028 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08003029 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3030 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3031 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003032
Romain Guyd21b6e12011-11-30 20:21:23 -08003033 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003034 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003035 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003036 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003037 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003038 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003039 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3040 }
Romain Guyf219da52011-01-16 12:54:25 -08003041
Romain Guy448455f2013-07-22 13:57:50 -07003042 TextureVertex* mesh = &layer->mesh[0];
3043 GLsizei elementsCount = layer->meshElementCount;
3044
3045 while (elementsCount > 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08003046 GLsizei drawCount = min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Romain Guy448455f2013-07-22 13:57:50 -07003047
Romain Guy3380cfd2013-08-15 16:57:57 -07003048 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003049 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
Chris Craike84a2082014-12-22 14:28:49 -08003050 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr));
Romain Guy448455f2013-07-22 13:57:50 -07003051
3052 elementsCount -= drawCount;
3053 // Though there are 4 vertices in a quad, we use 6 indices per
3054 // quad to draw with GL_TRIANGLES
3055 mesh += (drawCount / 6) * 4;
3056 }
Romain Guyf219da52011-01-16 12:54:25 -08003057
Romain Guy3a3133d2011-02-01 22:59:58 -08003058#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003059 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003060#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003061 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003062
Romain Guy5bb3c732012-11-29 17:52:58 -08003063 if (layer->debugDrawUpdate) {
3064 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003065
3066 SkPaint paint;
3067 paint.setColor(0x7f00ff00);
3068 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003069 }
Romain Guyf219da52011-01-16 12:54:25 -08003070 }
Chris Craik34416ea2013-04-15 16:08:28 -07003071 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003072
Romain Guyb2e2f242012-10-17 18:18:35 -07003073 if (transform && !transform->isIdentity()) {
3074 restore();
3075 }
3076
Tom Hudson107843d2014-09-08 11:26:26 -04003077 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003078}
3079
Romain Guy6926c722010-07-12 20:20:03 -07003080///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003081// Draw filters
3082///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003083void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3084 // We should never get here since we apply the draw filter when stashing
3085 // the paints in the DisplayList.
3086 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003087}
3088
3089///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003090// Drawing implementation
3091///////////////////////////////////////////////////////////////////////////////
3092
Chris Craikd218a922014-01-02 17:13:34 -08003093Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003094 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003095 if (!texture) {
3096 return mCaches.textureCache.get(bitmap);
3097 }
3098 return texture;
3099}
3100
Romain Guy01d58e42011-01-19 21:54:02 -08003101void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003102 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003103 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003104 return;
3105 }
3106
3107 int alpha;
3108 SkXfermode::Mode mode;
3109 getAlphaAndMode(paint, &alpha, &mode);
3110
3111 setupDraw();
3112 setupDrawWithTexture(true);
3113 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003114 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003115 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003116 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003117 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003118 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3119 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003120 setupDrawTexture(texture->id);
3121 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003122 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003123 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08003124 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Romain Guy01d58e42011-01-19 21:54:02 -08003125
Chris Craik117bdbc2015-02-05 10:12:38 -08003126 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003127}
3128
Romain Guyf607bdc2010-09-10 19:20:06 -07003129// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003130#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3131#define kStdUnderline_Offset (1.0f / 9.0f)
3132#define kStdUnderline_Thickness (1.0f / 18.0f)
3133
Chris Craikd218a922014-01-02 17:13:34 -08003134void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3135 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003136 // Handle underline and strike-through
3137 uint32_t flags = paint->getFlags();
3138 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003139 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003140
Romain Guy211370f2012-02-01 16:10:55 -08003141 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003142 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003143 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003144
Raph Levien8b4072d2012-07-30 15:50:00 -07003145 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003146 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003147
Romain Guyf6834472011-01-23 13:32:12 -08003148 int linesCount = 0;
3149 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3150 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3151
3152 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003153 float points[pointsCount];
3154 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003155
3156 if (flags & SkPaint::kUnderlineText_Flag) {
3157 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003158 points[currentPoint++] = left;
3159 points[currentPoint++] = top;
3160 points[currentPoint++] = left + underlineWidth;
3161 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003162 }
3163
3164 if (flags & SkPaint::kStrikeThruText_Flag) {
3165 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003166 points[currentPoint++] = left;
3167 points[currentPoint++] = top;
3168 points[currentPoint++] = left + underlineWidth;
3169 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003170 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003171
Romain Guy726aeba2011-06-01 14:52:00 -07003172 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003173
Romain Guy726aeba2011-06-01 14:52:00 -07003174 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003175 }
3176 }
3177}
3178
Tom Hudson107843d2014-09-08 11:26:26 -04003179void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003180 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003181 return;
Romain Guy672433d2013-01-04 19:05:13 -08003182 }
3183
Tom Hudson107843d2014-09-08 11:26:26 -04003184 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003185}
3186
Tom Hudson107843d2014-09-08 11:26:26 -04003187void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003188 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003189 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003190
Chris Craik15a07a22014-01-26 13:43:53 -08003191 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08003192 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07003193
3194 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003195 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003196
ztenghui14a4e352014-08-13 10:44:39 -07003197 // The caller has made sure casterAlpha > 0.
3198 float ambientShadowAlpha = mAmbientShadowAlpha;
3199 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3200 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3201 }
3202 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3203 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003204 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003205 }
ztenghui7b4516e2014-01-07 10:42:55 -08003206
ztenghui14a4e352014-08-13 10:44:39 -07003207 float spotShadowAlpha = mSpotShadowAlpha;
3208 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3209 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3210 }
3211 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3212 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003213 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003214 }
ztenghui7b4516e2014-01-07 10:42:55 -08003215
Tom Hudson107843d2014-09-08 11:26:26 -04003216 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003217}
3218
Tom Hudson107843d2014-09-08 11:26:26 -04003219void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003220 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003221 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003222 return;
Romain Guy3b753822013-03-05 10:27:35 -08003223 }
Romain Guy735738c2012-12-03 12:34:51 -08003224
Romain Guy672433d2013-01-04 19:05:13 -08003225 float left = FLT_MAX;
3226 float top = FLT_MAX;
3227 float right = FLT_MIN;
3228 float bottom = FLT_MIN;
3229
Romain Guy448455f2013-07-22 13:57:50 -07003230 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003231 Vertex* vertex = mesh;
3232
Chris Craik2af46352012-11-26 18:30:17 -08003233 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003234 float l = rects[index + 0];
3235 float t = rects[index + 1];
3236 float r = rects[index + 2];
3237 float b = rects[index + 3];
3238
Romain Guy3b753822013-03-05 10:27:35 -08003239 Vertex::set(vertex++, l, t);
3240 Vertex::set(vertex++, r, t);
3241 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003242 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003243
Romain Guy3b753822013-03-05 10:27:35 -08003244 left = fminf(left, l);
3245 top = fminf(top, t);
3246 right = fmaxf(right, r);
3247 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003248 }
3249
Chris Craikf0a59072013-11-19 18:00:46 -08003250 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003251 return;
Romain Guya362c692013-02-04 13:50:16 -08003252 }
Romain Guy672433d2013-01-04 19:05:13 -08003253
Chris Craik2ab95d72015-02-06 15:25:51 -08003254 if (!paint->getShader() && !currentSnapshot()->roundRectClipState) {
3255 const Matrix4& transform = ignoreTransform ? Matrix4::identity() : *currentTransform();
3256 Glop glop;
3257 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
3258 aBuilder.setMeshIndexedQuads(&mesh[0], count / 4)
3259 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
3260 .setModelViewOffsetRect(0, 0, Rect(left, top, right, bottom))
3261 .setPaint(*paint, currentSnapshot()->alpha)
3262 .build();
3263 renderGlop(glop);
3264 return;
3265 }
3266
3267 int color = paint->getColor();
3268 // If a shader is set, preserve only the alpha
3269 if (getShader(paint)) {
3270 color |= 0x00ffffff;
3271 }
3272
Romain Guy672433d2013-01-04 19:05:13 -08003273 setupDraw();
3274 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003275 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003276 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003277 setupDrawColorFilter(getColorFilter(paint));
3278 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003279 setupDrawProgram();
3280 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003281 setupDrawModelView(kModelViewMode_Translate, false,
3282 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003283 setupDrawColorUniforms(getShader(paint));
3284 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003285 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003286
Romain Guy8ce00302013-01-15 18:51:42 -08003287 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003288 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003289 }
3290
Chris Craik4063a0e2013-11-15 16:06:56 -08003291 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003292
Tom Hudson107843d2014-09-08 11:26:26 -04003293 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003294}
3295
Romain Guy026c5e162010-06-28 17:12:22 -07003296void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003297 const SkPaint* paint, bool ignoreTransform) {
Chris Craik117bdbc2015-02-05 10:12:38 -08003298
3299 if (!paint->getShader() && !currentSnapshot()->roundRectClipState) {
3300 const Matrix4& transform = ignoreTransform ? Matrix4::identity() : *currentTransform();
3301 Glop glop;
3302 GlopBuilder aBuilder(mRenderState, mCaches, &glop);
3303 aBuilder.setMeshUnitQuad()
3304 .setTransform(currentSnapshot()->getOrthoMatrix(), transform, false)
3305 .setModelViewMapUnitToRect(Rect(left, top, right, bottom))
Chris Craik2ab95d72015-02-06 15:25:51 -08003306 .setPaint(*paint, currentSnapshot()->alpha)
Chris Craik117bdbc2015-02-05 10:12:38 -08003307 .build();
3308 renderGlop(glop);
3309 return;
3310 }
3311
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003312 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003313 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003314 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003315 color |= 0x00ffffff;
3316 }
3317
Romain Guy70ca14e2010-12-13 18:24:33 -08003318 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003319 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003320 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003321 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003322 setupDrawColorFilter(getColorFilter(paint));
3323 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003324 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003325 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3326 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003327 setupDrawColorUniforms(getShader(paint));
3328 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003329 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003330 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003331
Chris Craik117bdbc2015-02-05 10:12:38 -08003332 glDrawArrays(GL_TRIANGLE_STRIP, 0, kUnitQuadCount);
Romain Guyc95c8d62010-09-17 15:31:32 -07003333}
3334
Romain Guy82ba8142010-07-09 13:25:56 -07003335void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003336 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003337 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003338
Chris Craike84a2082014-12-22 14:28:49 -08003339 GLvoid* vertices = (GLvoid*) nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -08003340 GLvoid* texCoords = (GLvoid*) kMeshTextureOffset;
Romain Guy3b748a42013-04-17 18:54:38 -07003341
3342 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003343 vertices = &mMeshVertices[0].x;
3344 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003345
3346 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3347 texture->uvMapper->map(uvs);
3348
3349 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3350 }
3351
Chris Craikd6b65f62014-01-01 14:45:21 -08003352 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3353 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3354 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003355
Romain Guyd21b6e12011-11-30 20:21:23 -08003356 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003357 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003358 paint, texture->blend, vertices, texCoords,
Chris Craik117bdbc2015-02-05 10:12:38 -08003359 GL_TRIANGLE_STRIP, kUnitQuadCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003360 } else {
Chris Craik678625242014-02-28 12:26:34 -08003361 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003362 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Chris Craik117bdbc2015-02-05 10:12:38 -08003363 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, kUnitQuadCount);
Romain Guy3b748a42013-04-17 18:54:38 -07003364 }
3365
3366 if (texture->uvMapper) {
3367 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003368 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003369}
3370
Romain Guyf7f93552010-07-08 19:17:03 -07003371void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003372 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003373 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003374 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3375 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003376
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003377 int a;
3378 SkXfermode::Mode mode;
3379 getAlphaAndMode(paint, &a, &mode);
3380 const float alpha = a / 255.0f;
3381
Romain Guy746b7402010-10-26 16:27:31 -07003382 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003383 setupDrawWithTexture();
3384 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003385 setupDrawColorFilter(getColorFilter(paint));
3386 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003387 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003388 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003389 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003390 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003391 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003392 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003393 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003394
Romain Guy6820ac82010-09-15 18:11:50 -07003395 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003396}
3397
Romain Guy3b748a42013-04-17 18:54:38 -07003398void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003399 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003400 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003401 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3402 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003403
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003404 int a;
3405 SkXfermode::Mode mode;
3406 getAlphaAndMode(paint, &a, &mode);
3407 const float alpha = a / 255.0f;
3408
Romain Guy3b748a42013-04-17 18:54:38 -07003409 setupDraw();
3410 setupDrawWithTexture();
3411 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003412 setupDrawColorFilter(getColorFilter(paint));
3413 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003414 setupDrawProgram();
3415 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003416 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003417 setupDrawTexture(texture);
3418 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003419 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003420 setupDrawMeshIndices(vertices, texCoords, vbo);
3421
Chris Craike84a2082014-12-22 14:28:49 -08003422 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy3b748a42013-04-17 18:54:38 -07003423}
3424
Romain Guy886b2752013-01-04 12:26:18 -08003425void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003426 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003427 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003428 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003429
Chris Craike84a2082014-12-22 14:28:49 -08003430 int color = paint != nullptr ? paint->getColor() : 0;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003431 int alpha;
3432 SkXfermode::Mode mode;
3433 getAlphaAndMode(paint, &alpha, &mode);
3434
Romain Guy886b2752013-01-04 12:26:18 -08003435 setupDraw();
3436 setupDrawWithTexture(true);
Chris Craike84a2082014-12-22 14:28:49 -08003437 if (paint != nullptr) {
Romain Guy886b2752013-01-04 12:26:18 -08003438 setupDrawAlpha8Color(color, alpha);
3439 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003440 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003441 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003442 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003443 setupDrawProgram();
3444 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003445 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003446 setupDrawTexture(texture);
3447 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003448 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003449 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003450 setupDrawMesh(vertices, texCoords);
3451
3452 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003453}
3454
Romain Guya5aed0d2010-09-09 14:42:43 -07003455void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003456 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003457
Chris Craik117bdbc2015-02-05 10:12:38 -08003458 if (currentSnapshot()->roundRectClipState != nullptr /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003459 blend = true;
3460 mDescription.hasRoundRectClip = true;
3461 }
3462 mSkipOutlineClip = true;
3463
Romain Guy82ba8142010-07-09 13:25:56 -07003464 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003465
Romain Guy82ba8142010-07-09 13:25:56 -07003466 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003467 // These blend modes are not supported by OpenGL directly and have
3468 // to be implemented using shaders. Since the shader will perform
3469 // the blending, turn blending off here
3470 // If the blend mode cannot be implemented using shaders, fall
3471 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003472 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Chris Craik117bdbc2015-02-05 10:12:38 -08003473 if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003474 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003475 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003476
Chris Craik44eb2c02015-01-29 09:45:09 -08003477 mRenderState.blend().disable();
Romain Guy82bc7a72012-01-03 14:13:39 -08003478 return;
3479 } else {
3480 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003481 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003482 }
Chris Craik44eb2c02015-01-29 09:45:09 -08003483 mRenderState.blend().enable(mode, swapSrcDst);
3484 } else {
3485 mRenderState.blend().disable();
Romain Guy82ba8142010-07-09 13:25:56 -07003486 }
Romain Guybd6b79b2010-06-26 00:13:53 -07003487}
3488
Romain Guy026c5e162010-06-28 17:12:22 -07003489void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003490 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003491 TextureVertex::setUV(v++, u1, v1);
3492 TextureVertex::setUV(v++, u2, v1);
3493 TextureVertex::setUV(v++, u1, v2);
3494 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003495}
3496
Chris Craike84a2082014-12-22 14:28:49 -08003497void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
3498 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003499 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003500 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3501 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003502 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003503 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003504 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003505}
3506
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003507float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003508 float alpha;
3509 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3510 alpha = mDrawModifiers.mOverrideLayerAlpha;
3511 } else {
3512 alpha = layer->getAlpha() / 255.0f;
3513 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003514 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003515}
3516
Romain Guy9d5316e2010-06-24 19:30:36 -07003517}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003518}; // namespace android