blob: b56ce4fda0b0863125c68334bd9d4ee721787420 [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"
22#include "Patch.h"
23#include "PathTessellator.h"
24#include "Properties.h"
25#include "RenderNode.h"
26#include "renderstate/RenderState.h"
27#include "ShadowTessellator.h"
28#include "SkiaShader.h"
29#include "Vector.h"
30#include "VertexBuffer.h"
31#include "utils/GLUtils.h"
32#include "utils/PaintUtils.h"
33#include "utils/TraceUtils.h"
34
Romain Guye4d01122010-06-16 18:44:05 -070035#include <stdlib.h>
36#include <stdint.h>
37#include <sys/types.h>
38
Romain Guy5cbbce52010-06-27 22:59:20 -070039#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070040#include <SkColor.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040041#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070042#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070043
Romain Guye4d01122010-06-16 18:44:05 -070044#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070045#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070046
Romain Guy08aa2cb2011-03-17 11:06:57 -070047#include <private/hwui/DrawGlInfo.h>
48
Romain Guy5b3b3522010-10-27 18:57:51 -070049#include <ui/Rect.h>
50
Chris Craik62d307c2014-07-29 10:35:13 -070051#if DEBUG_DETAILED_EVENTS
52 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
53#else
54 #define EVENT_LOGD(...)
55#endif
56
Romain Guye4d01122010-06-16 18:44:05 -070057namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070058namespace uirenderer {
59
Chris Craik678625242014-02-28 12:26:34 -080060static GLenum getFilter(const SkPaint* paint) {
61 if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) {
62 return GL_LINEAR;
63 }
64 return GL_NEAREST;
65}
Romain Guyd21b6e12011-11-30 20:21:23 -080066
Romain Guy9d5316e2010-06-24 19:30:36 -070067///////////////////////////////////////////////////////////////////////////////
68// Globals
69///////////////////////////////////////////////////////////////////////////////
70
Romain Guyf607bdc2010-09-10 19:20:06 -070071
Romain Guyf6a11b82010-06-23 17:47:49 -070072///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -070073// Functions
74///////////////////////////////////////////////////////////////////////////////
75
76template<typename T>
77static inline T min(T a, T b) {
78 return a < b ? a : b;
79}
80
81///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -070082// Constructors/destructor
83///////////////////////////////////////////////////////////////////////////////
84
John Reck3b202512014-06-23 13:13:08 -070085OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -040086 : mState(*this)
Chris Craik058fc642014-07-23 18:19:28 -070087 , mCaches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -070088 , mExtensions(Extensions::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -070089 , mRenderState(renderState)
Chris Craik65fe5ee2015-01-26 18:06:29 -080090 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -070091 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -070092 , mSuppressTiling(false)
93 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -040094 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -070095 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -070096 , mLightRadius(FLT_MIN)
97 , mAmbientShadowAlpha(0)
98 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -080099 // *set* draw modifiers to be 0
100 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700101 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700102
Chris Craik96a5c4c2015-01-27 15:46:35 -0800103 memcpy(mMeshVertices, kMeshVertices, sizeof(kMeshVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700104}
105
Romain Guy85bf02f2010-06-22 13:11:24 -0700106OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700107 // The context has already been destroyed at this point, do not call
108 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700109}
110
Romain Guy87e2f7572012-09-24 11:37:12 -0700111void OpenGLRenderer::initProperties() {
112 char property[PROPERTY_VALUE_MAX];
113 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
114 mScissorOptimizationDisabled = !strcasecmp(property, "true");
115 INIT_LOGD(" Scissor optimization %s",
116 mScissorOptimizationDisabled ? "disabled" : "enabled");
117 } else {
118 INIT_LOGD(" Scissor optimization enabled");
119 }
Romain Guy13631f32012-01-30 17:41:55 -0800120}
121
Chris Craik058fc642014-07-23 18:19:28 -0700122void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
123 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
124 mLightCenter = lightCenter;
125 mLightRadius = lightRadius;
126 mAmbientShadowAlpha = ambientShadowAlpha;
127 mSpotShadowAlpha = spotShadowAlpha;
128}
129
Romain Guy13631f32012-01-30 17:41:55 -0800130///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700131// Setup
132///////////////////////////////////////////////////////////////////////////////
133
Chris Craik797b95b2014-05-20 18:10:25 -0700134void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700135 glDisable(GL_DITHER);
136 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Chris Craik284b2432014-09-18 16:05:35 -0700137 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700138}
139
Romain Guy96885eb2013-03-26 15:05:58 -0700140void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800141 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800142 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400143 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700144 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700145 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700146}
147
Tom Hudson107843d2014-09-08 11:26:26 -0400148void OpenGLRenderer::startFrame() {
149 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700150 mFrameStarted = true;
151
Tom Hudson984162f2014-10-10 13:38:16 -0400152 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700153
Romain Guy96885eb2013-03-26 15:05:58 -0700154 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700155
Tom Hudson984162f2014-10-10 13:38:16 -0400156 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800157
Romain Guy54c1a642012-09-27 17:55:46 -0700158 // Functors break the tiling extension in pretty spectacular ways
159 // This ensures we don't use tiling when a functor is going to be
160 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700161 mSuppressTiling = mCaches.hasRegisteredFunctors()
162 || mFirstFrameAfterResize;
163 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700164
Chris Craikd6b65f62014-01-01 14:45:21 -0800165 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700166
Romain Guy7c450aa2012-09-21 19:15:00 -0700167 debugOverdraw(true, true);
168
Tom Hudson107843d2014-09-08 11:26:26 -0400169 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700170 mTilingClip.right, mTilingClip.bottom, mOpaque);
171}
172
Tom Hudson107843d2014-09-08 11:26:26 -0400173void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700174 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700175
Romain Guy96885eb2013-03-26 15:05:58 -0700176 setupFrameState(left, top, right, bottom, opaque);
177
178 // Layer renderers will start the frame immediately
179 // The framebuffer renderer will first defer the display list
180 // for each layer and wait until the first drawing command
181 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800182 if (currentSnapshot()->fbo == 0) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800183 mRenderState.blend().syncEnabled();
Romain Guy96885eb2013-03-26 15:05:58 -0700184 updateLayers();
185 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400186 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700187 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700188}
189
Romain Guydcfc8362013-01-03 13:08:57 -0800190void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
191 // If we know that we are going to redraw the entire framebuffer,
192 // perform a discard to let the driver know we don't need to preserve
193 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800194 if (mExtensions.hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400195 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
196 const bool isFbo = onGetTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800197 const GLenum attachments[] = {
198 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
199 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800200 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
201 }
202}
203
Tom Hudson107843d2014-09-08 11:26:26 -0400204void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700205 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800206 mRenderState.scissor().setEnabled(true);
207 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700208 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400209 mDirty = true;
210 return;
Romain Guyddf74372012-05-22 14:07:07 -0700211 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700212
Chris Craik65fe5ee2015-01-26 18:06:29 -0800213 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700214}
215
henry.uh_chen33f5a592014-07-02 19:36:56 +0800216void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700217 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800218 const Snapshot* snapshot = currentSnapshot();
219
Chris Craik14e51302013-12-30 15:32:54 -0800220 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800221 if (snapshot->flags & Snapshot::kFlagFboTarget) {
222 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700223 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700224
henry.uh_chen33f5a592014-07-02 19:36:56 +0800225 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800226 }
227}
228
henry.uh_chen33f5a592014-07-02 19:36:56 +0800229void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800230 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800231 if(expand) {
232 // Expand the startTiling region by 1
233 int leftNotZero = (clip.left > 0) ? 1 : 0;
234 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
235
236 mCaches.startTiling(
237 clip.left - leftNotZero,
238 windowHeight - clip.bottom - topNotZero,
239 clip.right - clip.left + leftNotZero + 1,
240 clip.bottom - clip.top + topNotZero + 1,
241 opaque);
242 } else {
243 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800244 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800245 }
Romain Guy54c1a642012-09-27 17:55:46 -0700246 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700247}
248
249void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700250 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700251}
252
Tom Hudson107843d2014-09-08 11:26:26 -0400253bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700254 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700255 endTiling();
Chris Craik4ac36f82014-12-09 16:54:03 -0800256 mTempPaths.clear();
257
Romain Guyca89e2a2013-03-08 17:44:20 -0800258 // When finish() is invoked on FBO 0 we've reached the end
259 // of the current frame
Tom Hudson984162f2014-10-10 13:38:16 -0400260 if (onGetTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800261 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700262 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800263 }
264
Romain Guy11cb6422012-09-21 00:39:43 -0700265 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700266#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700267 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700268#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700269
Romain Guyc15008e2010-11-10 11:59:15 -0800270#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800271 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700272#else
273 if (mCaches.getDebugLevel() & kDebugMemory) {
274 mCaches.dumpMemoryUsage();
275 }
Romain Guyc15008e2010-11-10 11:59:15 -0800276#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700277 }
Romain Guy96885eb2013-03-26 15:05:58 -0700278
279 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400280
281 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700282}
283
Romain Guy35643dd2012-09-18 15:40:58 -0700284void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700285 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
286 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700287 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700288
Chris Craik65fe5ee2015-01-26 18:06:29 -0800289 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700290 dirtyClip();
291}
292
Andreas Gampe64bb4132014-11-22 00:35:09 +0000293void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400294 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700295
Rob Tsuk487a92c2015-01-06 13:22:54 -0800296 Rect clip(mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700297 clip.snapToPixelBoundaries();
298
Romain Guyd643bb52011-03-01 14:55:21 -0800299 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800300 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800301 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800302 dirtyLayerUnchecked(clip, getRegion());
303 }
Romain Guyd643bb52011-03-01 14:55:21 -0800304
Romain Guy08aa2cb2011-03-17 11:06:57 -0700305 DrawGlInfo info;
306 info.clipLeft = clip.left;
307 info.clipTop = clip.top;
308 info.clipRight = clip.right;
309 info.clipBottom = clip.bottom;
310 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700311 info.width = getViewportWidth();
312 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800313 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700314
Tom Hudson984162f2014-10-10 13:38:16 -0400315 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700316 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400317 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700318 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
319 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800320 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700321 setScissorFromClip();
322 }
Chris Craik54f574a2013-08-26 11:23:46 -0700323
John Reck3b202512014-06-23 13:13:08 -0700324 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
325 // Scissor may have been modified, reset dirty clip
326 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800327
Tom Hudson107843d2014-09-08 11:26:26 -0400328 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800329}
330
Romain Guyf6a11b82010-06-23 17:47:49 -0700331///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700332// Debug
333///////////////////////////////////////////////////////////////////////////////
334
Chris Craik62d307c2014-07-29 10:35:13 -0700335void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
336#if DEBUG_DETAILED_EVENTS
337 const int BUFFER_SIZE = 256;
338 va_list ap;
339 char buf[BUFFER_SIZE];
340
341 va_start(ap, fmt);
342 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
343 va_end(ap);
344
345 eventMark(buf);
346#endif
347}
348
349
Romain Guy0f6675332013-03-01 14:31:04 -0800350void OpenGLRenderer::eventMark(const char* name) const {
351 mCaches.eventMark(0, name);
352}
353
Romain Guy87e2f7572012-09-24 11:37:12 -0700354void OpenGLRenderer::startMark(const char* name) const {
355 mCaches.startMark(0, name);
356}
357
358void OpenGLRenderer::endMark() const {
359 mCaches.endMark();
360}
361
362void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700363 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700364}
365
366void OpenGLRenderer::renderOverdraw() {
Tom Hudson984162f2014-10-10 13:38:16 -0400367 if (mCaches.debugOverdraw && onGetTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700368 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700369
Chris Craik65fe5ee2015-01-26 18:06:29 -0800370 mRenderState.scissor().setEnabled(true);
371 mRenderState.scissor().set(clip->left,
372 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
373 clip->right - clip->left,
374 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700375
Romain Guy627c6fd2013-08-21 11:53:18 -0700376 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800377 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700378 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
379
380 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800381 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700382 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
383
384 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800385 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700386 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
387
388 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800389 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700390 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
391
Chris Craik96a5c4c2015-01-27 15:46:35 -0800392 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700393 }
394}
395
396///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700397// Layers
398///////////////////////////////////////////////////////////////////////////////
399
400bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700401 if (layer->deferredUpdateScheduled && layer->renderer
402 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700403
Romain Guy7c450aa2012-09-21 19:15:00 -0700404 if (inFrame) {
405 endTiling();
406 debugOverdraw(false, false);
407 }
Romain Guy11cb6422012-09-21 00:39:43 -0700408
Romain Guy96885eb2013-03-26 15:05:58 -0700409 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700410 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700411 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700412 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700413 }
Romain Guy11cb6422012-09-21 00:39:43 -0700414
415 if (inFrame) {
416 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800417 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700418 }
419
Romain Guy5bb3c732012-11-29 17:52:58 -0800420 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700421 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700422
423 return true;
424 }
425
426 return false;
427}
428
429void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700430 // If draw deferring is enabled this method will simply defer
431 // the display list of each individual layer. The layers remain
432 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700433 int count = mLayerUpdates.size();
434 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700435 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
436 startMark("Layer Updates");
437 } else {
438 startMark("Defer Layer Updates");
439 }
Romain Guy11cb6422012-09-21 00:39:43 -0700440
Chris Craik1206b9b2013-04-04 14:46:24 -0700441 // Note: it is very important to update the layers in order
442 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700443 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700444 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700445 }
Romain Guy11cb6422012-09-21 00:39:43 -0700446
Romain Guy96885eb2013-03-26 15:05:58 -0700447 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
448 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400449 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700450 }
451 endMark();
452 }
453}
454
455void OpenGLRenderer::flushLayers() {
456 int count = mLayerUpdates.size();
457 if (count > 0) {
458 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700459
Chris Craik1206b9b2013-04-04 14:46:24 -0700460 // Note: it is very important to update the layers in order
461 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800462 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700463 }
464
465 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400466 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700467
Romain Guy11cb6422012-09-21 00:39:43 -0700468 endMark();
469 }
470}
471
472void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
473 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700474 // Make sure we don't introduce duplicates.
475 // SortedVector would do this automatically but we need to respect
476 // the insertion order. The linear search is not an issue since
477 // this list is usually very short (typically one item, at most a few)
478 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
479 if (mLayerUpdates.itemAt(i) == layer) {
480 return;
481 }
482 }
Romain Guy11cb6422012-09-21 00:39:43 -0700483 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700484 }
485}
486
Romain Guye93482f2013-06-17 13:14:51 -0700487void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
488 if (layer) {
489 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
490 if (mLayerUpdates.itemAt(i) == layer) {
491 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700492 break;
493 }
494 }
495 }
496}
497
Romain Guy40543602013-06-12 15:31:28 -0700498void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800499 ATRACE_NAME("Update HW Layers");
Chris Craik44eb2c02015-01-29 09:45:09 -0800500 mRenderState.blend().syncEnabled();
Romain Guy40543602013-06-12 15:31:28 -0700501 updateLayers();
502 flushLayers();
503 // Wait for all the layer updates to be executed
John Reck55156372015-01-21 07:46:37 -0800504 glFinish();
Romain Guy40543602013-06-12 15:31:28 -0700505}
506
John Reck443a7142014-09-04 17:40:05 -0700507void OpenGLRenderer::markLayersAsBuildLayers() {
508 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
509 mLayerUpdates[i]->wasBuildLayered = true;
510 }
511}
512
Romain Guy11cb6422012-09-21 00:39:43 -0700513///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700514// State management
515///////////////////////////////////////////////////////////////////////////////
516
Chris Craik14e51302013-12-30 15:32:54 -0800517void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700518 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800519 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
520 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700521
Chris Craika64a2be2014-05-14 14:17:01 -0700522 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700523 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700524 }
525
Romain Guy2542d192010-08-18 11:47:12 -0700526 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700527 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700528 }
Romain Guy2542d192010-08-18 11:47:12 -0700529
Romain Guy5ec99242010-11-03 16:19:08 -0700530 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700531 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700532 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700533 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800534 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700535 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700536 }
Romain Guybb9524b2010-06-22 18:56:38 -0700537}
538
Romain Guyf6a11b82010-06-23 17:47:49 -0700539///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700540// Layers
541///////////////////////////////////////////////////////////////////////////////
542
543int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700544 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700545 // force matrix/clip isolation for layer
546 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
547
Tom Hudson984162f2014-10-10 13:38:16 -0400548 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700549
Tom Hudson984162f2014-10-10 13:38:16 -0400550 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700551 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700552 }
Romain Guyd55a8612010-06-28 17:42:46 -0700553
554 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700555}
556
Chris Craikd90144d2013-03-19 15:03:48 -0700557void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
558 const Rect untransformedBounds(bounds);
559
Chris Craikd6b65f62014-01-01 14:45:21 -0800560 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700561
562 // Layers only make sense if they are in the framebuffer's bounds
Rob Tsuk487a92c2015-01-06 13:22:54 -0800563 if (bounds.intersect(mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700564 // We cannot work with sub-pixels in this case
565 bounds.snapToPixelBoundaries();
566
567 // When the layer is not an FBO, we may use glCopyTexImage so we
568 // need to make sure the layer does not extend outside the bounds
569 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700570 const Snapshot& previous = *(currentSnapshot()->previous);
571 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
572 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700573 bounds.setEmpty();
574 } else if (fboLayer) {
575 clip.set(bounds);
576 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800577 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700578 inverse.mapRect(clip);
579 clip.snapToPixelBoundaries();
580 if (clip.intersect(untransformedBounds)) {
581 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
582 bounds.set(untransformedBounds);
583 } else {
584 clip.setEmpty();
585 }
586 }
587 } else {
588 bounds.setEmpty();
589 }
590}
591
Chris Craik408eb122013-03-26 18:55:15 -0700592void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
593 bool fboLayer, int alpha) {
594 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
595 bounds.getHeight() > mCaches.maxTextureSize ||
596 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400597 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700598 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400599 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700600 }
601}
602
Chris Craikd90144d2013-03-19 15:03:48 -0700603int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500604 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400605 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700606
Tom Hudson984162f2014-10-10 13:38:16 -0400607 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700608 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
609 // operations will be able to store and restore the current clip and transform info, and
610 // quick rejection will be correct (for display lists)
611
612 Rect bounds(left, top, right, bottom);
613 Rect clip;
614 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500615 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700616
Tom Hudson984162f2014-10-10 13:38:16 -0400617 if (!mState.currentlyIgnored()) {
618 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
619 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
620 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800621 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700622 }
623 }
624
625 return count;
626}
627
Romain Guy1c740bc2010-09-13 18:00:09 -0700628/**
629 * Layers are viewed by Skia are slightly different than layers in image editing
630 * programs (for instance.) When a layer is created, previously created layers
631 * and the frame buffer still receive every drawing command. For instance, if a
632 * layer is created and a shape intersecting the bounds of the layers and the
633 * framebuffer is draw, the shape will be drawn on both (unless the layer was
634 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
635 *
636 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
637 * texture. Unfortunately, this is inefficient as it requires every primitive to
638 * be drawn n + 1 times, where n is the number of active layers. In practice this
639 * means, for every primitive:
640 * - Switch active frame buffer
641 * - Change viewport, clip and projection matrix
642 * - Issue the drawing
643 *
644 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700645 * To avoid this, layers are implemented in a different way here, at least in the
646 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
647 * is set. When this flag is set we can redirect all drawing operations into a
648 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700649 *
650 * This implementation relies on the frame buffer being at least RGBA 8888. When
651 * a layer is created, only a texture is created, not an FBO. The content of the
652 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700653 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700654 * buffer and drawing continues as normal. This technique therefore treats the
655 * frame buffer as a scratch buffer for the layers.
656 *
657 * To compose the layers back onto the frame buffer, each layer texture
658 * (containing the original frame buffer data) is drawn as a simple quad over
659 * the frame buffer. The trick is that the quad is set as the composition
660 * destination in the blending equation, and the frame buffer becomes the source
661 * of the composition.
662 *
663 * Drawing layers with an alpha value requires an extra step before composition.
664 * An empty quad is drawn over the layer's region in the frame buffer. This quad
665 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
666 * quad is used to multiply the colors in the frame buffer. This is achieved by
667 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
668 * GL_ZERO, GL_SRC_ALPHA.
669 *
670 * Because glCopyTexImage2D() can be slow, an alternative implementation might
671 * be use to draw a single clipped layer. The implementation described above
672 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700673 *
674 * (1) The frame buffer is actually not cleared right away. To allow the GPU
675 * to potentially optimize series of calls to glCopyTexImage2D, the frame
676 * buffer is left untouched until the first drawing operation. Only when
677 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700678 */
Chet Haased48885a2012-08-28 17:43:28 -0700679bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700680 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800681 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
682 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700683
Romain Guyeb993562010-10-05 18:14:38 -0700684 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
685
Romain Guyf607bdc2010-09-10 19:20:06 -0700686 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700687 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700688 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700689 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000690 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700691
692 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400693 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700694 return false;
695 }
Romain Guyf18fd992010-07-08 11:45:51 -0700696
Chris Craik44eb2c02015-01-29 09:45:09 -0800697 mCaches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700698 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700699 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700700 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700701 }
702
Derek Sollenberger674554f2014-02-19 16:47:32 +0000703 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700704 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700705 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
706 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500707
Chet Haasea23eed82012-04-12 15:19:04 -0700708 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700709 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700710 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700711
Romain Guy8fb95422010-08-17 18:38:51 -0700712 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400713 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
714 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700715
Chris Craika8bea8e2014-09-24 11:29:43 -0700716 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
717 fboLayer ? "" : "unclipped ",
718 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700719 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700720 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700721 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700722 } else {
723 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700724 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800725 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700726 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700727 // Workaround for some GL drivers. When reading pixels lying outside
728 // of the window we should get undefined values for those pixels.
729 // Unfortunately some drivers will turn the entire target texture black
730 // when reading outside of the window.
731 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800732 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700733 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800734 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700735
Chris Craika64a2be2014-05-14 14:17:01 -0700736 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
737 bounds.left, getViewportHeight() - bounds.bottom,
738 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700739
Romain Guy54be1cd2011-06-13 19:04:27 -0700740 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800741 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700742 }
Romain Guyeb993562010-10-05 18:14:38 -0700743 }
Romain Guyf86ef572010-07-01 11:05:42 -0700744
Romain Guyd55a8612010-06-28 17:42:46 -0700745 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700746}
747
Chris Craike63f7c622013-10-17 10:30:55 -0700748bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800749 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700750 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700751
Tom Hudson984162f2014-10-10 13:38:16 -0400752 writableSnapshot()->region = &writableSnapshot()->layer->region;
753 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
754 writableSnapshot()->fbo = layer->getFbo();
755 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
756 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
757 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800758 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700759
Romain Guy2b7028e2012-09-19 17:25:38 -0700760 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700761 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700762 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700763 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700764 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700765
766 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700767 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700768 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700769 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700770 }
771
772 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700773 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700774
henry.uh_chen33f5a592014-07-02 19:36:56 +0800775 // Expand the startTiling region by 1
776 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700777
778 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800779 mRenderState.scissor().setEnabled(true);
780 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700781 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700782 glClear(GL_COLOR_BUFFER_BIT);
783
784 dirtyClip();
785
786 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700787 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700788 return true;
789}
790
Romain Guy1c740bc2010-09-13 18:00:09 -0700791/**
792 * Read the documentation of createLayer() before doing anything in this method.
793 */
Chris Craik14e51302013-12-30 15:32:54 -0800794void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
795 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000796 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700797 return;
798 }
799
Chris Craik14e51302013-12-30 15:32:54 -0800800 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800801 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800802 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700803
Chris Craik39a908c2013-06-13 14:39:01 -0700804 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400805 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800806 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800807 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700808
Romain Guyeb993562010-10-05 18:14:38 -0700809 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700810 endTiling();
811
Romain Guye0aa84b2012-04-03 19:30:26 -0700812 // Detach the texture from the FBO
813 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800814
815 layer->removeFbo(false);
816
Romain Guyeb993562010-10-05 18:14:38 -0700817 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700818 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700819 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700820
Chris Craikd6b65f62014-01-01 14:45:21 -0800821 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700822 }
823
Romain Guy9ace8f52011-07-07 20:50:11 -0700824 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500825 SkPaint layerPaint;
826 layerPaint.setAlpha(layer->getAlpha());
827 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
828 layerPaint.setColorFilter(layer->getColorFilter());
829
830 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700831 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700832 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700833 }
834
Chris Craik96a5c4c2015-01-27 15:46:35 -0800835 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700836
Chris Craik44eb2c02015-01-29 09:45:09 -0800837 mCaches.textureState().activateTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700838
Romain Guy5b3b3522010-10-27 18:57:51 -0700839 // When the layer is stored in an FBO, we can save a bit of fillrate by
840 // drawing only the dirty region
841 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800842 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700843 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700844 } else if (!rect.isEmpty()) {
845 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530846
847 save(0);
848 // the layer contains screen buffer content that shouldn't be alpha modulated
849 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400850 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700851 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530852 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700853 }
Romain Guy8b55f372010-08-18 17:10:07 -0700854
Romain Guy746b7402010-10-26 16:27:31 -0700855 dirtyClip();
856
Romain Guyeb993562010-10-05 18:14:38 -0700857 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800858 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700859 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800860 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800861 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700862 }
863}
864
Romain Guyaa6c24c2011-04-28 18:40:04 -0700865void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700866 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700867
868 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700869 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700870 setupDrawWithTexture();
871 } else {
872 setupDrawWithExternalTexture();
873 }
874 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700875 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500876 setupDrawColorFilter(layer->getColorFilter());
877 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700878 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700879 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500880 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700881 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
882 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700883 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700884 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700885 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800886 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800887 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700888 layer->getWidth() == (uint32_t) rect.getWidth() &&
889 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800890 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
891 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700892
Romain Guyd21b6e12011-11-30 20:21:23 -0800893 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800894 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
895 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700896 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800897 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800898 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
899 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700900 }
901 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700902 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700903
Chris Craik96a5c4c2015-01-27 15:46:35 -0800904 glDrawArrays(GL_TRIANGLE_STRIP, 0, kMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700905}
906
Romain Guy5b3b3522010-10-27 18:57:51 -0700907void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700908 if (layer->isTextureLayer()) {
909 EVENT_LOGD("composeTextureLayerRect");
910 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
911 drawTextureLayer(layer, rect);
912 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
913 } else {
914 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700915 const Rect& texCoords = layer->texCoords;
916 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
917 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700918
Romain Guy9ace8f52011-07-07 20:50:11 -0700919 float x = rect.left;
920 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -0800921 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700922 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700923 layer->getHeight() == (uint32_t) rect.getHeight();
924
925 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700926 // When we're swapping, the layer is already in screen coordinates
927 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800928 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
929 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700930 }
931
Romain Guyd21b6e12011-11-30 20:21:23 -0800932 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700933 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800934 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700935 }
936
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500937 SkPaint layerPaint;
938 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
939 layerPaint.setXfermodeMode(layer->getMode());
940 layerPaint.setColorFilter(layer->getColorFilter());
941
942 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700943 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500944 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -0700945 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik96a5c4c2015-01-27 15:46:35 -0800946 GL_TRIANGLE_STRIP, kMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700947
Romain Guyaa6c24c2011-04-28 18:40:04 -0700948 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700949 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700950}
951
Chris Craik34416ea2013-04-15 16:08:28 -0700952/**
953 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
954 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
955 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
956 * by saveLayer's restore
957 */
Tom Hudson984162f2014-10-10 13:38:16 -0400958#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
959 DRAW_COMMAND; \
960 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
961 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
962 DRAW_COMMAND; \
963 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
964 } \
Chris Craik34416ea2013-04-15 16:08:28 -0700965 }
966
967#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
968
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400969// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
970// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
971class LayerShader : public SkShader {
972public:
973 LayerShader(Layer* layer, const SkMatrix* localMatrix)
974 : INHERITED(localMatrix)
975 , mLayer(layer) {
976 }
977
Chris Craike84a2082014-12-22 14:28:49 -0800978 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400979 if (data) {
980 *data = static_cast<void*>(mLayer);
981 }
982 return true;
983 }
984
Chris Craike84a2082014-12-22 14:28:49 -0800985 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400986 return !mLayer->isBlend();
987 }
988
989protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000990 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400991 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
992 }
993
Chris Craike84a2082014-12-22 14:28:49 -0800994 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400995 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
996 }
997
Chris Craike84a2082014-12-22 14:28:49 -0800998 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400999 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -08001000 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001001 }
1002private:
1003 // Unowned.
1004 Layer* mLayer;
1005 typedef SkShader INHERITED;
1006};
1007
Romain Guy5b3b3522010-10-27 18:57:51 -07001008void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001009 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1010
1011 if (layer->getConvexMask()) {
1012 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1013
1014 // clip to the area of the layer the mask can be larger
1015 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1016
1017 SkPaint paint;
1018 paint.setAntiAlias(true);
1019 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1020
Chris Craik3f0854292014-04-15 16:18:08 -07001021 // create LayerShader to map SaveLayer content into subsequent draw
1022 SkMatrix shaderMatrix;
1023 shaderMatrix.setTranslate(rect.left, rect.bottom);
1024 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001025 LayerShader layerShader(layer, &shaderMatrix);
1026 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001027
1028 // Since the drawing primitive is defined in local drawing space,
1029 // we don't need to modify the draw matrix
1030 const SkPath* maskPath = layer->getConvexMask();
1031 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1032
Chris Craike84a2082014-12-22 14:28:49 -08001033 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -07001034 restore();
1035
1036 return;
1037 }
1038
Romain Guy5b3b3522010-10-27 18:57:51 -07001039 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001040 layer->setRegionAsRect();
1041
Chris Craik34416ea2013-04-15 16:08:28 -07001042 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001043
Romain Guy5b3b3522010-10-27 18:57:51 -07001044 layer->region.clear();
1045 return;
1046 }
1047
Chris Craik62d307c2014-07-29 10:35:13 -07001048 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001049 // standard Region based draw
1050 size_t count;
1051 const android::Rect* rects;
1052 Region safeRegion;
1053 if (CC_LIKELY(hasRectToRectTransform())) {
1054 rects = layer->region.getArray(&count);
1055 } else {
1056 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1057 rects = safeRegion.getArray(&count);
1058 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001059
Chris Craik3f0854292014-04-15 16:18:08 -07001060 const float alpha = getLayerAlpha(layer);
1061 const float texX = 1.0f / float(layer->getWidth());
1062 const float texY = 1.0f / float(layer->getHeight());
1063 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001064
Chris Craik3f0854292014-04-15 16:18:08 -07001065 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001066
Chris Craik3f0854292014-04-15 16:18:08 -07001067 // We must get (and therefore bind) the region mesh buffer
1068 // after we setup drawing in case we need to mess with the
1069 // stencil buffer in setupDraw()
1070 TextureVertex* mesh = mCaches.getRegionMesh();
1071 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001072
Chris Craik3f0854292014-04-15 16:18:08 -07001073 setupDrawWithTexture();
1074 setupDrawColor(alpha, alpha, alpha, alpha);
1075 setupDrawColorFilter(layer->getColorFilter());
1076 setupDrawBlending(layer);
1077 setupDrawProgram();
1078 setupDrawDirtyRegionsDisabled();
1079 setupDrawPureColorUniforms();
1080 setupDrawColorFilterUniforms(layer->getColorFilter());
1081 setupDrawTexture(layer->getTexture());
1082 if (currentTransform()->isPureTranslate()) {
1083 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1084 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001085
Chris Craik3f0854292014-04-15 16:18:08 -07001086 layer->setFilter(GL_NEAREST);
1087 setupDrawModelView(kModelViewMode_Translate, false,
1088 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1089 } else {
1090 layer->setFilter(GL_LINEAR);
1091 setupDrawModelView(kModelViewMode_Translate, false,
1092 rect.left, rect.top, rect.right, rect.bottom);
1093 }
1094 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001095
Chris Craik3f0854292014-04-15 16:18:08 -07001096 for (size_t i = 0; i < count; i++) {
1097 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001098
Chris Craik3f0854292014-04-15 16:18:08 -07001099 const float u1 = r->left * texX;
1100 const float v1 = (height - r->top) * texY;
1101 const float u2 = r->right * texX;
1102 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001103
Chris Craik3f0854292014-04-15 16:18:08 -07001104 // TODO: Reject quads outside of the clip
1105 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1106 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1107 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1108 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001109
Chris Craik3f0854292014-04-15 16:18:08 -07001110 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001111
Chris Craik96a5c4c2015-01-27 15:46:35 -08001112 if (numQuads >= kMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001113 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001114 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001115 numQuads = 0;
1116 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001117 }
Chris Craik3f0854292014-04-15 16:18:08 -07001118 }
1119
1120 if (numQuads > 0) {
1121 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001122 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001123 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001124
Romain Guy5b3b3522010-10-27 18:57:51 -07001125#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001126 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001127#endif
1128
Chris Craik3f0854292014-04-15 16:18:08 -07001129 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001130}
1131
Romain Guy3a3133d2011-02-01 22:59:58 -08001132#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001133void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001134 size_t count;
1135 const android::Rect* rects = region.getArray(&count);
1136
1137 uint32_t colors[] = {
1138 0x7fff0000, 0x7f00ff00,
1139 0x7f0000ff, 0x7fff00ff,
1140 };
1141
1142 int offset = 0;
1143 int32_t top = rects[0].top;
1144
1145 for (size_t i = 0; i < count; i++) {
1146 if (top != rects[i].top) {
1147 offset ^= 0x2;
1148 top = rects[i].top;
1149 }
1150
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001151 SkPaint paint;
1152 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001153 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001154 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001155 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001156}
Chris Craike63f7c622013-10-17 10:30:55 -07001157#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001158
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001159void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001160 Vector<float> rects;
1161
1162 SkRegion::Iterator it(region);
1163 while (!it.done()) {
1164 const SkIRect& r = it.rect();
1165 rects.push(r.fLeft);
1166 rects.push(r.fTop);
1167 rects.push(r.fRight);
1168 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001169 it.next();
1170 }
1171
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001172 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001173}
1174
Romain Guy5b3b3522010-10-27 18:57:51 -07001175void OpenGLRenderer::dirtyLayer(const float left, const float top,
1176 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001177 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001178 Rect bounds(left, top, right, bottom);
1179 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001180 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001181 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001182}
1183
1184void OpenGLRenderer::dirtyLayer(const float left, const float top,
1185 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001186 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001187 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001188 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001189 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001190}
1191
1192void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001193 if (bounds.intersect(mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001194 bounds.snapToPixelBoundaries();
1195 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1196 if (!dirty.isEmpty()) {
1197 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001198 }
1199 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001200}
1201
Chris Craik4063a0e2013-11-15 16:06:56 -08001202void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001203 GLsizei elementsCount = quadsCount * 6;
1204 while (elementsCount > 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001205 GLsizei drawCount = min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Romain Guy448455f2013-07-22 13:57:50 -07001206
Romain Guy3380cfd2013-08-15 16:57:57 -07001207 setupDrawIndexedVertices(&mesh[0].x);
Chris Craike84a2082014-12-22 14:28:49 -08001208 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy448455f2013-07-22 13:57:50 -07001209
1210 elementsCount -= drawCount;
1211 // Though there are 4 vertices in a quad, we use 6 indices per
1212 // quad to draw with GL_TRIANGLES
1213 mesh += (drawCount / 6) * 4;
1214 }
1215}
1216
Romain Guy54be1cd2011-06-13 19:04:27 -07001217void OpenGLRenderer::clearLayerRegions() {
1218 const size_t count = mLayers.size();
1219 if (count == 0) return;
1220
Tom Hudson984162f2014-10-10 13:38:16 -04001221 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001222 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001223 // Doing several glScissor/glClear here can negatively impact
1224 // GPUs with a tiler architecture, instead we draw quads with
1225 // the Clear blending mode
1226
1227 // The list contains bounds that have already been clipped
1228 // against their initial clip rect, and the current clip
1229 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001230 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001231
Romain Guy448455f2013-07-22 13:57:50 -07001232 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001233 Vertex* vertex = mesh;
1234
1235 for (uint32_t i = 0; i < count; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001236 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001237
Chris Craik51d6a3d2014-12-22 17:16:56 -08001238 Vertex::set(vertex++, bounds.left, bounds.top);
1239 Vertex::set(vertex++, bounds.right, bounds.top);
1240 Vertex::set(vertex++, bounds.left, bounds.bottom);
1241 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001242 }
Romain Guye67307c2013-02-11 18:01:20 -08001243 // We must clear the list of dirty rects before we
1244 // call setupDraw() to prevent stencil setup to do
1245 // the same thing again
1246 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001247
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001248 SkPaint clearPaint;
1249 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1250
Romain Guy54be1cd2011-06-13 19:04:27 -07001251 setupDraw(false);
1252 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001253 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001254 setupDrawProgram();
1255 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001256 setupDrawModelView(kModelViewMode_Translate, false,
1257 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001258
Chris Craik4063a0e2013-11-15 16:06:56 -08001259 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001260
Chris Craik65fe5ee2015-01-26 18:06:29 -08001261 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001262 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001263 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001264 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001265}
1266
Romain Guybd6b79b2010-06-26 00:13:53 -07001267///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001268// State Deferral
1269///////////////////////////////////////////////////////////////////////////////
1270
Chris Craikff785832013-03-08 13:12:16 -08001271bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001272 const Rect& currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001273 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001274
Chris Craikff785832013-03-08 13:12:16 -08001275 if (stateDeferFlags & kStateDeferFlag_Draw) {
1276 // state has bounds initialized in local coordinates
1277 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001278 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001279 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001280 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1281 // is used, it should more closely duplicate the quickReject logic (in how it uses
1282 // snapToPixelBoundaries)
1283
Rob Tsuk487a92c2015-01-06 13:22:54 -08001284 if (!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001285 // quick rejected
1286 return true;
1287 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001288
Chris Craika02c4ed2013-06-14 13:43:58 -07001289 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001290 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001291 int& flags = state.mClipSideFlags;
1292 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001293 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1294 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1295 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1296 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001297 }
1298 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001299 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001300 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1301 // overdraw avoidance (since we don't know what it overlaps)
1302 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001303 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001304 }
1305 }
1306
Chris Craik527a3aa2013-03-04 10:19:31 -08001307 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1308 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001309 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001310 }
1311
Chris Craik7273daa2013-03-28 11:25:24 -07001312 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1313 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001314 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001315 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001316 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001317
1318 // always store/restore, since it's just a pointer
1319 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001320 return false;
1321}
1322
Chris Craik527a3aa2013-03-04 10:19:31 -08001323void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001324 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001325 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001326 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001327 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001328
Chris Craik527a3aa2013-03-04 10:19:31 -08001329 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001330 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001331 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001332 dirtyClip();
1333 }
Chris Craikc3566d02013-02-04 16:16:33 -08001334}
1335
Chris Craik28ce94a2013-05-31 11:38:03 -07001336/**
1337 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1338 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1339 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1340 *
1341 * This method should be called when restoreDisplayState() won't be restoring the clip
1342 */
1343void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001344 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001345 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001346 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001347 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001348 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001349 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001350 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1351 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001352}
1353
Chris Craikc3566d02013-02-04 16:16:33 -08001354///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001355// Clipping
1356///////////////////////////////////////////////////////////////////////////////
1357
Romain Guybb9524b2010-06-22 18:56:38 -07001358void OpenGLRenderer::setScissorFromClip() {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001359 Rect clip(mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001360 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001361
Chris Craik65fe5ee2015-01-26 18:06:29 -08001362 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001363 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001364 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001365 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001366}
1367
Romain Guy8ce00302013-01-15 18:51:42 -08001368void OpenGLRenderer::ensureStencilBuffer() {
1369 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1370 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1371 // just hope we have one when hasLayer() returns false.
1372 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001373 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001374 }
1375}
1376
1377void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1378 // The layer's FBO is already bound when we reach this stage
1379 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001380 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1381 // is attached after we initiated tiling. We must turn it off,
1382 // attach the new render buffer then turn tiling back on
1383 endTiling();
1384
Romain Guy8d4aeb72013-02-12 16:08:55 -08001385 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001386 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001387 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001388
Romain Guyf735c8e2013-01-31 17:45:55 -08001389 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001390 }
1391}
1392
Rob Tsuk487a92c2015-01-06 13:22:54 -08001393static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1394 float x, float y) {
1395 Vertex v;
1396 v.x = x;
1397 v.y = y;
1398 transform.mapPoint(v.x, v.y);
1399 rectangleVertices.push_back(v);
1400}
1401
1402static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1403 Vertex v;
1404 v.x = x;
1405 v.y = y;
1406 rectangleVertices.push_back(v);
1407}
1408
1409void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
1410 int count = rectangleList.getTransformedRectanglesCount();
1411 std::vector<Vertex> rectangleVertices(count * 4);
1412 Rect scissorBox = rectangleList.calculateBounds();
1413 scissorBox.snapToPixelBoundaries();
1414 for (int i = 0; i < count; ++i) {
1415 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1416 const Matrix4& transform = tr.getTransform();
1417 Rect bounds = tr.getBounds();
1418 if (transform.rectToRect()) {
1419 transform.mapRect(bounds);
1420 if (!bounds.intersect(scissorBox)) {
1421 bounds.setEmpty();
1422 } else {
1423 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1424 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1425 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1426 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1427 }
1428 } else {
1429 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1430 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1431 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1432 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1433 }
1434 }
1435
Chris Craik65fe5ee2015-01-26 18:06:29 -08001436 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001437 scissorBox.getWidth(), scissorBox.getHeight());
1438
1439 const SkPaint* paint = nullptr;
1440 setupDraw();
1441 setupDrawNoTexture();
1442 setupDrawColor(0, 0xff * currentSnapshot()->alpha);
1443 setupDrawShader(getShader(paint));
1444 setupDrawColorFilter(getColorFilter(paint));
1445 setupDrawBlending(paint);
1446 setupDrawProgram();
1447 setupDrawDirtyRegionsDisabled();
1448 setupDrawModelView(kModelViewMode_Translate, false,
1449 0.0f, 0.0f, 0.0f, 0.0f, true);
1450 setupDrawColorUniforms(getShader(paint));
1451 setupDrawShaderUniforms(getShader(paint));
1452 setupDrawColorFilterUniforms(getColorFilter(paint));
1453
1454 issueIndexedQuadDraw(&rectangleVertices[0], rectangleVertices.size() / 4);
1455}
1456
Romain Guy8ce00302013-01-15 18:51:42 -08001457void OpenGLRenderer::setStencilFromClip() {
1458 if (!mCaches.debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001459 if (!currentSnapshot()->clipIsSimple()) {
1460 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001461 EVENT_LOGD("setStencilFromClip - enabling");
1462
Romain Guy8ce00302013-01-15 18:51:42 -08001463 // NOTE: The order here is important, we must set dirtyClip to false
1464 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001465 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001466
1467 ensureStencilBuffer();
1468
Rob Tsuk487a92c2015-01-06 13:22:54 -08001469 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001470
Rob Tsuk487a92c2015-01-06 13:22:54 -08001471 bool isRectangleList = clipArea.isRectangleList();
1472 if (isRectangleList) {
1473 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1474 } else {
1475 incrementThreshold = 0;
1476 }
1477
Chris Craik96a5c4c2015-01-27 15:46:35 -08001478 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001479
1480 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001481 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001482 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001483 if (resetScissor) {
1484 // The scissor was not set so we now need to update it
1485 setScissorFromClip();
1486 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001487
Chris Craik96a5c4c2015-01-27 15:46:35 -08001488 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001489
1490 // stash and disable the outline clip state, since stencil doesn't account for outline
1491 bool storedSkipOutlineClip = mSkipOutlineClip;
1492 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001493
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001494 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001495 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001496 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1497
Rob Tsuk487a92c2015-01-06 13:22:54 -08001498 if (isRectangleList) {
1499 drawRectangleList(clipArea.getRectangleList());
1500 } else {
1501 // NOTE: We could use the region contour path to generate a smaller mesh
1502 // Since we are using the stencil we could use the red book path
1503 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001504
Rob Tsuk487a92c2015-01-06 13:22:54 -08001505 // The last parameter is important: we are not drawing in the color buffer
1506 // so we don't want to dirty the current layer, if any
1507 drawRegionRects(clipArea.getClipRegion(), paint, false);
1508 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001509 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001510 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001511
Chris Craik96a5c4c2015-01-27 15:46:35 -08001512 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001513
1514 // Draw the region used to generate the stencil if the appropriate debug
1515 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001516 // TODO: Implement for rectangle list clip areas
1517 if (mCaches.debugStencilClip == Caches::kStencilShowRegion &&
1518 !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001519 paint.setColor(0x7f0000ff);
1520 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001521 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001522 }
Romain Guy8ce00302013-01-15 18:51:42 -08001523 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001524 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001525 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001526 }
1527 }
1528}
1529
Chris Craikf0a59072013-11-19 18:00:46 -08001530/**
1531 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1532 *
1533 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1534 * style, and tessellated AA ramp
1535 */
1536bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001537 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001538 bool snapOut = paint && paint->isAntiAlias();
1539
1540 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1541 float outset = paint->getStrokeWidth() * 0.5f;
1542 left -= outset;
1543 top -= outset;
1544 right += outset;
1545 bottom += outset;
1546 }
1547
Chris Craikdeeda3d2014-05-05 19:09:33 -07001548 bool clipRequired = false;
1549 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001550 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001551 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001552 return true;
1553 }
1554
Chris Craikf23b25a2014-06-26 15:46:20 -07001555 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001556 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001557 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001558 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001559}
1560
Romain Guy8ce00302013-01-15 18:51:42 -08001561void OpenGLRenderer::debugClip() {
1562#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001563 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001564 SkPaint paint;
1565 paint.setColor(0x7f00ff00);
1566 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1567
Romain Guy8ce00302013-01-15 18:51:42 -08001568 }
1569#endif
1570}
1571
Romain Guyf6a11b82010-06-23 17:47:49 -07001572///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001573// Drawing commands
1574///////////////////////////////////////////////////////////////////////////////
1575
Chris Craik62d307c2014-07-29 10:35:13 -07001576void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001577 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001578 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001579 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001580 // Make sure setScissor & setStencil happen at the beginning of
1581 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001582 if (mState.getDirtyClip()) {
Chris Craik65fe5ee2015-01-26 18:06:29 -08001583 if (mRenderState.scissor().isEnabled()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001584 setScissorFromClip();
1585 }
Chris Craik62d307c2014-07-29 10:35:13 -07001586
Dohyun Leeadc0d9d2014-11-24 21:08:15 +09001587 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001588 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001589
Romain Guy70ca14e2010-12-13 18:24:33 -08001590 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001591
Romain Guy70ca14e2010-12-13 18:24:33 -08001592 mSetShaderColor = false;
1593 mColorSet = false;
1594 mColorA = mColorR = mColorG = mColorB = 0.0f;
1595 mTextureUnit = 0;
1596 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001597
1598 // Enable debug highlight when what we're about to draw is tested against
1599 // the stencil buffer and if stencil highlight debugging is on
1600 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1601 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
Chris Craik96a5c4c2015-01-27 15:46:35 -08001602 mRenderState.stencil().isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001603}
1604
1605void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1606 mDescription.hasTexture = true;
1607 mDescription.hasAlpha8Texture = isAlpha8;
1608}
1609
Romain Guyff316ec2013-02-13 18:39:43 -08001610void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1611 mDescription.hasTexture = true;
1612 mDescription.hasColors = true;
1613 mDescription.hasAlpha8Texture = isAlpha8;
1614}
1615
Romain Guyaa6c24c2011-04-28 18:40:04 -07001616void OpenGLRenderer::setupDrawWithExternalTexture() {
1617 mDescription.hasExternalTexture = true;
1618}
1619
Romain Guy15bc6432011-12-13 13:11:32 -08001620void OpenGLRenderer::setupDrawNoTexture() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001621 mRenderState.meshState().disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001622}
1623
Chris Craik91a8c7c2014-08-12 14:31:35 -07001624void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1625 mDescription.hasVertexAlpha = true;
1626 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001627}
1628
Romain Guy8d0d4782010-12-14 20:13:35 -08001629void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1630 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001631 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1632 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1633 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001634 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001635 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001636}
1637
Romain Guy86568192010-12-14 15:55:39 -08001638void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1639 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001640 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1641 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1642 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001643 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001644 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001645}
1646
Romain Guy41210632012-07-16 17:04:24 -07001647void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1648 mCaches.fontRenderer->describe(mDescription, paint);
1649}
1650
Romain Guy70ca14e2010-12-13 18:24:33 -08001651void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1652 mColorA = a;
1653 mColorR = r;
1654 mColorG = g;
1655 mColorB = b;
1656 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001657 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001658}
1659
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001660void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
Chris Craike84a2082014-12-22 14:28:49 -08001661 if (shader != nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001662 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001663 }
1664}
1665
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001666void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001667 if (filter == nullptr) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001668 return;
1669 }
1670
1671 SkXfermode::Mode mode;
Chris Craike84a2082014-12-22 14:28:49 -08001672 if (filter->asColorMode(nullptr, &mode)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001673 mDescription.colorOp = ProgramDescription::kColorBlend;
1674 mDescription.colorMode = mode;
Chris Craike84a2082014-12-22 14:28:49 -08001675 } else if (filter->asColorMatrix(nullptr)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001676 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001677 }
1678}
1679
Romain Guyf09ef512011-05-27 11:43:46 -07001680void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1681 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1682 mColorA = 1.0f;
1683 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001684 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001685 }
1686}
1687
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001688void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1689 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001690 // When the blending mode is kClear_Mode, we need to use a modulate color
1691 // argb=1,0,0,0
1692 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001693 // TODO: check shader blending, once we have shader drawing support for layers.
Tom Hudson8dfaa492014-12-09 15:03:44 -05001694 bool blend = layer->isBlend()
1695 || getLayerAlpha(layer) < 1.0f
1696 || (mColorSet && mColorA < 1.0f)
1697 || PaintUtils::isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001698 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001699}
1700
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001701void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1702 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001703 // When the blending mode is kClear_Mode, we need to use a modulate color
1704 // argb=1,0,0,0
1705 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001706 blend |= (mColorSet && mColorA < 1.0f) ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001707 (getShader(paint) && !getShader(paint)->isOpaque()) ||
Tom Hudson8dfaa492014-12-09 15:03:44 -05001708 PaintUtils::isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001709 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001710}
1711
1712void OpenGLRenderer::setupDrawProgram() {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001713 mCaches.setProgram(mDescription);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001714 if (mDescription.hasRoundRectClip) {
1715 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001716 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001717 const Rect& innerRect = state->innerRect;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001718 glUniform4f(mCaches.program().getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001719 innerRect.left, innerRect.top,
1720 innerRect.right, innerRect.bottom);
Chris Craik6c15ffa2015-02-02 13:50:55 -08001721 glUniformMatrix4fv(mCaches.program().getUniform("roundRectInvTransform"),
Chris Craikdeeda3d2014-05-05 19:09:33 -07001722 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001723
1724 // add half pixel to round out integer rect space to cover pixel centers
1725 float roundedOutRadius = state->radius + 0.5f;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001726 glUniform1f(mCaches.program().getUniform("roundRectRadius"),
Chris Craik4340c262014-09-11 18:58:45 -07001727 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001728 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001729}
1730
1731void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1732 mTrackDirtyRegions = false;
1733}
1734
Chris Craik4063a0e2013-11-15 16:06:56 -08001735void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1736 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001737 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001738 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001739 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001740 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001741
Romain Guy86568192010-12-14 15:55:39 -08001742 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001743 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001744
1745 mCaches.program().set(currentSnapshot()->getOrthoMatrix(),
Chris Craike84a2082014-12-22 14:28:49 -08001746 mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001747 if (dirty && mTrackDirtyRegions) {
1748 if (!ignoreTransform) {
1749 dirtyLayer(left, top, right, bottom, *currentTransform());
1750 } else {
1751 dirtyLayer(left, top, right, bottom);
1752 }
Romain Guy86568192010-12-14 15:55:39 -08001753 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001754}
1755
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001756void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1757 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001758 mCaches.program().setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001759 }
1760}
1761
Romain Guy86568192010-12-14 15:55:39 -08001762void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001763 if (mSetShaderColor) {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001764 mCaches.program().setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001765 }
1766}
1767
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001768void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
Chris Craike84a2082014-12-22 14:28:49 -08001769 if (shader == nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001770 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001771 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001772
1773 if (ignoreTransform) {
1774 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1775 // because it was built into modelView / the geometry, and the description needs to
1776 // compensate.
1777 mat4 modelViewWithoutTransform;
1778 modelViewWithoutTransform.loadInverse(*currentTransform());
1779 modelViewWithoutTransform.multiply(mModelViewMatrix);
1780 mModelViewMatrix.load(modelViewWithoutTransform);
1781 }
1782
1783 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001784}
1785
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001786void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001787 if (nullptr == filter) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001788 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001789 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001790
1791 SkColor color;
1792 SkXfermode::Mode mode;
1793 if (filter->asColorMode(&color, &mode)) {
1794 const int alpha = SkColorGetA(color);
1795 const GLfloat a = alpha / 255.0f;
1796 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1797 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1798 const GLfloat b = a * SkColorGetB(color) / 255.0f;
Chris Craik6c15ffa2015-02-02 13:50:55 -08001799 glUniform4f(mCaches.program().getUniform("colorBlend"), r, g, b, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001800 return;
1801 }
1802
1803 SkScalar srcColorMatrix[20];
1804 if (filter->asColorMatrix(srcColorMatrix)) {
1805
1806 float colorMatrix[16];
1807 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1808 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1809 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1810 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1811
1812 // Skia uses the range [0..255] for the addition vector, but we need
1813 // the [0..1] range to apply the vector in GLSL
1814 float colorVector[4];
1815 colorVector[0] = srcColorMatrix[4] / 255.0f;
1816 colorVector[1] = srcColorMatrix[9] / 255.0f;
1817 colorVector[2] = srcColorMatrix[14] / 255.0f;
1818 colorVector[3] = srcColorMatrix[19] / 255.0f;
1819
Chris Craik6c15ffa2015-02-02 13:50:55 -08001820 glUniformMatrix4fv(mCaches.program().getUniform("colorMatrix"), 1,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001821 GL_FALSE, colorMatrix);
Chris Craik6c15ffa2015-02-02 13:50:55 -08001822 glUniform4fv(mCaches.program().getUniform("colorMatrixVector"), 1, colorVector);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001823 return;
1824 }
1825
1826 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001827}
1828
Romain Guy41210632012-07-16 17:04:24 -07001829void OpenGLRenderer::setupDrawTextGammaUniforms() {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001830 mCaches.fontRenderer->setupProgram(mDescription, mCaches.program());
Romain Guy41210632012-07-16 17:04:24 -07001831}
1832
Romain Guy70ca14e2010-12-13 18:24:33 -08001833void OpenGLRenderer::setupDrawSimpleMesh() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001834 bool force = mRenderState.meshState().bindMeshBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001835 mRenderState.meshState().bindPositionVertexPointer(force, nullptr);
Chris Craik96a5c4c2015-01-27 15:46:35 -08001836 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001837}
1838
1839void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001840 if (texture) mCaches.textureState().bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001841 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001842 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001843}
1844
Romain Guyaa6c24c2011-04-28 18:40:04 -07001845void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001846 mCaches.textureState().bindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001847 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001848 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001849}
1850
Romain Guy8f0095c2011-05-02 17:24:22 -07001851void OpenGLRenderer::setupDrawTextureTransform() {
1852 mDescription.hasTextureTransform = true;
1853}
1854
1855void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Chris Craik6c15ffa2015-02-02 13:50:55 -08001856 glUniformMatrix4fv(mCaches.program().getUniform("mainTextureTransform"), 1,
Romain Guyaa6c24c2011-04-28 18:40:04 -07001857 GL_FALSE, &transform.data[0]);
1858}
1859
Chris Craik564acf72014-01-02 16:46:18 -08001860void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1861 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001862 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001863 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001864 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001865 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001866 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001867 }
Romain Guyd71dd362011-12-12 19:03:35 -08001868
Chris Craik6c15ffa2015-02-02 13:50:55 -08001869 mRenderState.meshState().bindPositionVertexPointer(force, vertices);
1870 if (mCaches.program().texCoords >= 0) {
1871 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001872 }
1873
Chris Craik96a5c4c2015-01-27 15:46:35 -08001874 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -08001875}
1876
Chris Craik564acf72014-01-02 16:46:18 -08001877void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1878 const GLvoid* texCoords, const GLvoid* colors) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001879 bool force = mRenderState.meshState().unbindMeshBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001880 GLsizei stride = sizeof(ColorTextureVertex);
1881
Chris Craik6c15ffa2015-02-02 13:50:55 -08001882 mRenderState.meshState().bindPositionVertexPointer(force, vertices, stride);
1883 if (mCaches.program().texCoords >= 0) {
1884 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords, stride);
Romain Guyff316ec2013-02-13 18:39:43 -08001885 }
Chris Craik6c15ffa2015-02-02 13:50:55 -08001886 int slot = mCaches.program().getAttrib("colors");
Romain Guyff316ec2013-02-13 18:39:43 -08001887 if (slot >= 0) {
1888 glEnableVertexAttribArray(slot);
1889 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1890 }
1891
Chris Craik96a5c4c2015-01-27 15:46:35 -08001892 mRenderState.meshState().unbindIndicesBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001893}
1894
Chris Craik564acf72014-01-02 16:46:18 -08001895void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1896 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001897 bool force = false;
1898 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1899 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
Chris Craik96a5c4c2015-01-27 15:46:35 -08001900 // use the default VBO found in RenderState
Romain Guy3b748a42013-04-17 18:54:38 -07001901 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001902 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy3b748a42013-04-17 18:54:38 -07001903 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001904 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001905 }
Chris Craik96a5c4c2015-01-27 15:46:35 -08001906 mRenderState.meshState().bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001907
Chris Craik6c15ffa2015-02-02 13:50:55 -08001908 mRenderState.meshState().bindPositionVertexPointer(force, vertices);
1909 if (mCaches.program().texCoords >= 0) {
1910 mRenderState.meshState().bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001911 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001912}
1913
Romain Guy448455f2013-07-22 13:57:50 -07001914void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001915 bool force = mRenderState.meshState().unbindMeshBuffer();
1916 mRenderState.meshState().bindQuadIndicesBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08001917 mRenderState.meshState().bindPositionVertexPointer(force, vertices, kVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001918}
1919
Romain Guy70ca14e2010-12-13 18:24:33 -08001920///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001921// Drawing
1922///////////////////////////////////////////////////////////////////////////////
1923
Tom Hudson107843d2014-09-08 11:26:26 -04001924void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001925 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1926 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001927 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001928 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001929 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001930 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001931 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001932 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001933 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001934 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001935 }
1936
Chris Craikef8d6f22014-12-17 11:10:28 -08001937 // Don't avoid overdraw when visualizing, since that makes it harder to
1938 // debug where it's coming from, and when the problem occurs.
1939 bool avoidOverdraw = !mCaches.debugOverdraw;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001940 DeferredDisplayList deferredList(mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001941 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001942 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001943
1944 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001945 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001946
Tom Hudson107843d2014-09-08 11:26:26 -04001947 deferredList.flush(*this, dirty);
1948 } else {
1949 // Even if there is no drawing command(Ex: invisible),
1950 // it still needs startFrame to clear buffer and start tiling.
1951 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001952 }
1953}
1954
Chris Craike84a2082014-12-22 14:28:49 -08001955void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top,
1956 const SkPaint* paint) {
Romain Guya168d732011-03-18 16:50:13 -07001957 float x = left;
1958 float y = top;
1959
Romain Guy886b2752013-01-04 12:26:18 -08001960 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1961
Romain Guya168d732011-03-18 16:50:13 -07001962 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001963 if (currentTransform()->isPureTranslate()) {
1964 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1965 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001966 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001967
1968 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001969 } else {
Chris Craik678625242014-02-28 12:26:34 -08001970 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001971 }
1972
Romain Guy3b748a42013-04-17 18:54:38 -07001973 // No need to check for a UV mapper on the texture object, only ARGB_8888
1974 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001975 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Chris Craik96a5c4c2015-01-27 15:46:35 -08001976 paint, (GLvoid*) nullptr, (GLvoid*) kMeshTextureOffset,
1977 GL_TRIANGLE_STRIP, kMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001978}
1979
Romain Guy03c00b52013-06-20 18:30:28 -07001980/**
1981 * Important note: this method is intended to draw batches of bitmaps and
1982 * will not set the scissor enable or dirty the current layer, if any.
1983 * The caller is responsible for properly dirtying the current layer.
1984 */
Tom Hudson107843d2014-09-08 11:26:26 -04001985void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001986 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1987 const Rect& bounds, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001988 mCaches.textureState().activateTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001989 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001990 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001991
Chris Craik527a3aa2013-03-04 10:19:31 -08001992 const AutoTexture autoCleanup(texture);
1993
Chris Craik527a3aa2013-03-04 10:19:31 -08001994 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08001995 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08001996
1997 const float x = (int) floorf(bounds.left + 0.5f);
1998 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04001999 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002000 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002001 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002002 GL_TRIANGLES, bitmapCount * 6, true,
2003 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002004 } else {
2005 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002006 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002007 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2008 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002009 }
2010
Tom Hudson107843d2014-09-08 11:26:26 -04002011 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08002012}
2013
Tom Hudson107843d2014-09-08 11:26:26 -04002014void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002015 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002016 return;
Romain Guy6926c722010-07-12 20:20:03 -07002017 }
2018
Chris Craik44eb2c02015-01-29 09:45:09 -08002019 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002020 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002021 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002022 const AutoTexture autoCleanup(texture);
2023
Mike Reed1103b322014-07-08 12:36:44 -04002024 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002025 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002026 } else {
Chris Craik79647502014-08-06 13:42:24 -07002027 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002028 }
Chet Haase48659092012-05-31 15:21:51 -07002029
Tom Hudson107843d2014-09-08 11:26:26 -04002030 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002031}
2032
Tom Hudson107843d2014-09-08 11:26:26 -04002033void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002034 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002035 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002036 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002037 }
2038
Chris Craik39a908c2013-06-13 14:39:01 -07002039 // TODO: use quickReject on bounds from vertices
Chris Craik65fe5ee2015-01-26 18:06:29 -08002040 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002041
Romain Guyb18d2d02011-02-10 15:52:54 -08002042 float left = FLT_MAX;
2043 float top = FLT_MAX;
2044 float right = FLT_MIN;
2045 float bottom = FLT_MIN;
2046
Romain Guya92bb4d2012-10-16 11:08:44 -07002047 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002048
Chris Craik51d6a3d2014-12-22 17:16:56 -08002049 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[count]);
2050 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08002051
Chris Craik51d6a3d2014-12-22 17:16:56 -08002052 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002053 if (!colors) {
2054 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08002055 tempColors.reset(new int[colorsCount]);
2056 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
2057 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08002058 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002059
Chris Craik44eb2c02015-01-29 09:45:09 -08002060 mCaches.textureState().activateTexture(0);
John Reckebd52612014-12-10 16:47:36 -08002061 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002062 const UvMapper& mapper(getMapper(texture));
2063
Romain Guy5a7b4662011-01-20 19:09:30 -08002064 for (int32_t y = 0; y < meshHeight; y++) {
2065 for (int32_t x = 0; x < meshWidth; x++) {
2066 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2067
2068 float u1 = float(x) / meshWidth;
2069 float u2 = float(x + 1) / meshWidth;
2070 float v1 = float(y) / meshHeight;
2071 float v2 = float(y + 1) / meshHeight;
2072
Romain Guy3b748a42013-04-17 18:54:38 -07002073 mapper.map(u1, v1, u2, v2);
2074
Romain Guy5a7b4662011-01-20 19:09:30 -08002075 int ax = i + (meshWidth + 1) * 2;
2076 int ay = ax + 1;
2077 int bx = i;
2078 int by = bx + 1;
2079 int cx = i + 2;
2080 int cy = cx + 1;
2081 int dx = i + (meshWidth + 1) * 2 + 2;
2082 int dy = dx + 1;
2083
Romain Guyff316ec2013-02-13 18:39:43 -08002084 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2085 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2086 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002087
Romain Guyff316ec2013-02-13 18:39:43 -08002088 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2089 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2090 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002091
Romain Guya92bb4d2012-10-16 11:08:44 -07002092 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2093 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2094 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2095 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002096 }
2097 }
2098
Chris Craikf0a59072013-11-19 18:00:46 -08002099 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002100 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002101 }
2102
Romain Guyff316ec2013-02-13 18:39:43 -08002103 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002104 texture = mCaches.textureCache.get(bitmap);
2105 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04002106 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002107 }
Romain Guyff316ec2013-02-13 18:39:43 -08002108 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002109 const AutoTexture autoCleanup(texture);
2110
2111 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002112 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002113
2114 int alpha;
2115 SkXfermode::Mode mode;
2116 getAlphaAndMode(paint, &alpha, &mode);
2117
Romain Guyff316ec2013-02-13 18:39:43 -08002118 float a = alpha / 255.0f;
2119
Romain Guya92bb4d2012-10-16 11:08:44 -07002120 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002121 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002122 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002123
Romain Guyff316ec2013-02-13 18:39:43 -08002124 setupDraw();
2125 setupDrawWithTextureAndColor();
2126 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002127 setupDrawColorFilter(getColorFilter(paint));
2128 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002129 setupDrawProgram();
2130 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002131 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002132 setupDrawTexture(texture->id);
2133 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002134 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002135 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002136
2137 glDrawArrays(GL_TRIANGLES, 0, count);
2138
Chris Craik6c15ffa2015-02-02 13:50:55 -08002139 int slot = mCaches.program().getAttrib("colors");
Romain Guyff316ec2013-02-13 18:39:43 -08002140 if (slot >= 0) {
2141 glDisableVertexAttribArray(slot);
2142 }
2143
Tom Hudson107843d2014-09-08 11:26:26 -04002144 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002145}
2146
Tom Hudson107843d2014-09-08 11:26:26 -04002147void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002148 float srcLeft, float srcTop, float srcRight, float srcBottom,
2149 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002150 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002151 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002152 return;
Romain Guy6926c722010-07-12 20:20:03 -07002153 }
2154
Chris Craik44eb2c02015-01-29 09:45:09 -08002155 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002156 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002157 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002158 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002159
Romain Guy8ba548f2010-06-30 19:21:21 -07002160 const float width = texture->width;
2161 const float height = texture->height;
2162
Romain Guy3b748a42013-04-17 18:54:38 -07002163 float u1 = fmax(0.0f, srcLeft / width);
2164 float v1 = fmax(0.0f, srcTop / height);
2165 float u2 = fmin(1.0f, srcRight / width);
2166 float v2 = fmin(1.0f, srcBottom / height);
2167
2168 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002169
Chris Craik96a5c4c2015-01-27 15:46:35 -08002170 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002171 resetDrawTextureTexCoords(u1, v1, u2, v2);
2172
Romain Guyd21b6e12011-11-30 20:21:23 -08002173 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2174
Romain Guy886b2752013-01-04 12:26:18 -08002175 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2176 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002177
Romain Guy886b2752013-01-04 12:26:18 -08002178 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2179 // Apply a scale transform on the canvas only when a shader is in use
2180 // Skia handles the ratio between the dst and src rects as a scale factor
2181 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002182 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002183 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002184
Chris Craikd6b65f62014-01-01 14:45:21 -08002185 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2186 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2187 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002188
2189 dstRight = x + (dstRight - dstLeft);
2190 dstBottom = y + (dstBottom - dstTop);
2191
2192 dstLeft = x;
2193 dstTop = y;
2194
Chris Craik678625242014-02-28 12:26:34 -08002195 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002196 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002197 } else {
Chris Craik678625242014-02-28 12:26:34 -08002198 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002199 }
2200
2201 if (CC_UNLIKELY(useScaleTransform)) {
2202 save(SkCanvas::kMatrix_SaveFlag);
2203 translate(dstLeft, dstTop);
2204 scale(scaleX, scaleY);
2205
2206 dstLeft = 0.0f;
2207 dstTop = 0.0f;
2208
2209 dstRight = srcRight - srcLeft;
2210 dstBottom = srcBottom - srcTop;
2211 }
2212
Mike Reed1103b322014-07-08 12:36:44 -04002213 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002214 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002215 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002216 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik96a5c4c2015-01-27 15:46:35 -08002217 GL_TRIANGLE_STRIP, kMeshCount, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002218 } else {
2219 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002220 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002221 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik96a5c4c2015-01-27 15:46:35 -08002222 GL_TRIANGLE_STRIP, kMeshCount, false, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002223 }
2224
2225 if (CC_UNLIKELY(useScaleTransform)) {
2226 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002227 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002228
2229 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002230
Tom Hudson107843d2014-09-08 11:26:26 -04002231 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002232}
2233
Tom Hudson107843d2014-09-08 11:26:26 -04002234void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002235 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002236 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002237 return;
Romain Guy6926c722010-07-12 20:20:03 -07002238 }
2239
John Reckebd52612014-12-10 16:47:36 -08002240 AssetAtlas::Entry* entry = mRenderState.assetAtlas().getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002241 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2242 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002243
Tom Hudson107843d2014-09-08 11:26:26 -04002244 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002245}
2246
Tom Hudson107843d2014-09-08 11:26:26 -04002247void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002248 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2249 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002250 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002251 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002252 }
2253
Romain Guy211370f2012-02-01 16:10:55 -08002254 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002255 mCaches.textureState().activateTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002256 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002257 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002258 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002259
Romain Guya4adcf02013-02-28 12:15:35 -08002260 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2261 texture->setFilter(GL_LINEAR, true);
2262
Chris Craikd6b65f62014-01-01 14:45:21 -08002263 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002264 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002265 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002266 const float offsetX = left + currentTransform()->getTranslateX();
2267 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002268 const size_t count = mesh->quads.size();
2269 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002270 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002271 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002272 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2273 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2274 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002275 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002276 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002277 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002278 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002279 }
2280 }
2281
Chris Craik4063a0e2013-11-15 16:06:56 -08002282 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002283 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002284 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2285 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002286
Romain Guy3b748a42013-04-17 18:54:38 -07002287 right = x + right - left;
2288 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002289 left = x;
2290 top = y;
2291 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002292 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002293 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2294 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002295 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2296 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002297 }
Chet Haase48659092012-05-31 15:21:51 -07002298
Tom Hudson107843d2014-09-08 11:26:26 -04002299 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002300}
2301
Romain Guy03c00b52013-06-20 18:30:28 -07002302/**
2303 * Important note: this method is intended to draw batches of 9-patch objects and
2304 * will not set the scissor enable or dirty the current layer, if any.
2305 * The caller is responsible for properly dirtying the current layer.
2306 */
Tom Hudson107843d2014-09-08 11:26:26 -04002307void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002308 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002309 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07002310 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002311 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002312 const AutoTexture autoCleanup(texture);
2313
2314 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2315 texture->setFilter(GL_LINEAR, true);
2316
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002317 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2318 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002319 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002320
Tom Hudson107843d2014-09-08 11:26:26 -04002321 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002322}
2323
Tom Hudson107843d2014-09-08 11:26:26 -04002324void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002325 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002326 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002327 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002328 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002329 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002330 }
2331
Chris Craik0d5ac952014-07-15 13:01:02 -07002332 Rect bounds(vertexBuffer.getBounds());
2333 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002334 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2335
Chris Craikcb4d6002012-09-25 12:00:29 -07002336 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002337 bool isAA = paint->isAntiAlias();
2338
Chris Craik710f46d2012-09-17 17:25:49 -07002339 setupDraw();
2340 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002341 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Tom Hudson984162f2014-10-10 13:38:16 -04002342 setupDrawColor(color, ((color >> 24) & 0xFF) * writableSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002343 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002344 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002345 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002346 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002347 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2348 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002349 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002350 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002351 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002352
ztenghui55bfb4e2013-12-03 10:38:55 -08002353 const void* vertices = vertexBuffer.getBuffer();
Chris Craik96a5c4c2015-01-27 15:46:35 -08002354 mRenderState.meshState().unbindMeshBuffer();
Chris Craik6c15ffa2015-02-02 13:50:55 -08002355 mRenderState.meshState().bindPositionVertexPointer(true, vertices,
2356 isAA ? kAlphaVertexStride : kVertexStride);
Chris Craik96a5c4c2015-01-27 15:46:35 -08002357 mRenderState.meshState().resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002358
Chris Craik710f46d2012-09-17 17:25:49 -07002359 int alphaSlot = -1;
2360 if (isAA) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002361 void* alphaCoords = ((GLbyte*) vertices) + kVertexAlphaOffset;
Chris Craik6c15ffa2015-02-02 13:50:55 -08002362 alphaSlot = mCaches.program().getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002363 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002364 glEnableVertexAttribArray(alphaSlot);
Chris Craik96a5c4c2015-01-27 15:46:35 -08002365 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, kAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002366 }
Romain Guy04299382012-07-18 17:15:41 -07002367
Chris Craik05f3d6e2014-06-02 16:27:04 -07002368 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2369 if (mode == VertexBuffer::kStandard) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002370 mRenderState.meshState().unbindIndicesBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08002371 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002372 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002373 mRenderState.meshState().bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002374 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT,
2375 GL_UNSIGNED_SHORT, nullptr);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002376 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002377 mRenderState.meshState().bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002378 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT,
2379 GL_UNSIGNED_SHORT, nullptr);
ztenghuid5e8ade2014-08-13 15:48:02 -07002380 } else if (mode == VertexBuffer::kIndices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002381 mRenderState.meshState().unbindIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002382 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(),
2383 GL_UNSIGNED_SHORT, vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002384 }
Romain Guy04299382012-07-18 17:15:41 -07002385
Chris Craik710f46d2012-09-17 17:25:49 -07002386 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002387 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002388 }
Chris Craik65cd6122012-12-10 17:56:27 -08002389
Tom Hudson107843d2014-09-08 11:26:26 -04002390 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002391}
2392
2393/**
Chris Craik65cd6122012-12-10 17:56:27 -08002394 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2395 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2396 * screen space in all directions. However, instead of using a fragment shader to compute the
2397 * translucency of the color from its position, we simply use a varying parameter to define how far
2398 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2399 *
2400 * Doesn't yet support joins, caps, or path effects.
2401 */
Tom Hudson107843d2014-09-08 11:26:26 -04002402void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002403 VertexBuffer vertexBuffer;
2404 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002405 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002406 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002407}
2408
2409/**
2410 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2411 * and additional geometry for defining an alpha slope perimeter.
2412 *
2413 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2414 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2415 * in-shader alpha region, but found it to be taxing on some GPUs.
2416 *
2417 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2418 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002419 */
Tom Hudson107843d2014-09-08 11:26:26 -04002420void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002421 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002422
Chris Craik65cd6122012-12-10 17:56:27 -08002423 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002424
Chris Craik65cd6122012-12-10 17:56:27 -08002425 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002426 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2427 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002428
Chris Craik05f3d6e2014-06-02 16:27:04 -07002429 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002430 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002431 }
2432
Chris Craikbf759452014-08-11 16:00:44 -07002433 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002434 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002435}
2436
Tom Hudson107843d2014-09-08 11:26:26 -04002437void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002438 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002439
Chris Craik6d29c8d2013-05-08 18:35:44 -07002440 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002441
Chris Craik6d29c8d2013-05-08 18:35:44 -07002442 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002443 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002444
Chris Craik05f3d6e2014-06-02 16:27:04 -07002445 const Rect& bounds = buffer.getBounds();
2446 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002447 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002448 }
2449
Chris Craikbf759452014-08-11 16:00:44 -07002450 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002451 drawVertexBuffer(buffer, paint, displayFlags);
2452
2453 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002454}
2455
Tom Hudson107843d2014-09-08 11:26:26 -04002456void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002457 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002458 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002459
Rob Tsuk487a92c2015-01-06 13:22:54 -08002460 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002461 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002462
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002463 SkPaint paint;
2464 paint.setColor(color);
2465 paint.setXfermodeMode(mode);
2466
2467 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002468
Tom Hudson107843d2014-09-08 11:26:26 -04002469 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002470}
Romain Guy9d5316e2010-06-24 19:30:36 -07002471
Tom Hudson107843d2014-09-08 11:26:26 -04002472void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002473 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002474 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002475 const AutoTexture autoCleanup(texture);
2476
2477 const float x = left + texture->left - texture->offset;
2478 const float y = top + texture->top - texture->offset;
2479
2480 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002481
Tom Hudson107843d2014-09-08 11:26:26 -04002482 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002483}
2484
Tom Hudson107843d2014-09-08 11:26:26 -04002485void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002486 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002487 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002488 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002489 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002490 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002491 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002492
Chris Craike84a2082014-12-22 14:28:49 -08002493 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002494 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002495 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002496 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002497 drawShape(left, top, texture, p);
2498 } else {
2499 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2500 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2501 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002502 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002503}
2504
Tom Hudson107843d2014-09-08 11:26:26 -04002505void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002506 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002507 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002508 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002509 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002510 }
Chris Craike84a2082014-12-22 14:28:49 -08002511 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002512 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002513 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002514 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002515 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002516 SkPath path;
2517 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2518 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2519 } else {
2520 path.addCircle(x, y, radius);
2521 }
2522 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002523 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002524}
Romain Guy01d58e42011-01-19 21:54:02 -08002525
Tom Hudson107843d2014-09-08 11:26:26 -04002526void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002527 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002528 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002529 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002530 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002531 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002532 }
Romain Guy01d58e42011-01-19 21:54:02 -08002533
Chris Craike84a2082014-12-22 14:28:49 -08002534 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002535 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002536 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002537 drawShape(left, top, texture, p);
2538 } else {
2539 SkPath path;
2540 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2541 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2542 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2543 }
2544 path.addOval(rect);
2545 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002546 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002547}
2548
Tom Hudson107843d2014-09-08 11:26:26 -04002549void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002550 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002551 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002552 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002553 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002554 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002555 }
2556
Chris Craik780c1282012-10-04 14:10:49 -07002557 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08002558 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002559 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002560 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002561 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002562 drawShape(left, top, texture, p);
2563 return;
Chris Craik780c1282012-10-04 14:10:49 -07002564 }
Chris Craik780c1282012-10-04 14:10:49 -07002565 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2566 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2567 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2568 }
2569
2570 SkPath path;
2571 if (useCenter) {
2572 path.moveTo(rect.centerX(), rect.centerY());
2573 }
2574 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2575 if (useCenter) {
2576 path.close();
2577 }
Tom Hudson107843d2014-09-08 11:26:26 -04002578 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002579}
2580
Romain Guycf8675e2012-10-02 12:32:25 -07002581// See SkPaintDefaults.h
2582#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2583
Tom Hudson107843d2014-09-08 11:26:26 -04002584void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002585 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002586 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002587 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002588 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002589 return;
Romain Guy6926c722010-07-12 20:20:03 -07002590 }
2591
Chris Craik710f46d2012-09-17 17:25:49 -07002592 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002593 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08002594 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07002595 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002596 mCaches.textureState().activateTexture(0);
Romain Guycf8675e2012-10-02 12:32:25 -07002597 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002598 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002599 drawShape(left, top, texture, p);
2600 } else {
2601 SkPath path;
2602 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2603 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2604 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2605 }
2606 path.addRect(rect);
2607 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002608 }
Chet Haase858aa932011-05-12 09:06:00 -07002609 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002610 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2611 SkPath path;
2612 path.addRect(left, top, right, bottom);
2613 drawConvexPath(path, p);
2614 } else {
2615 drawColorRect(left, top, right, bottom, p);
2616
2617 mDirty = true;
2618 }
Chet Haase858aa932011-05-12 09:06:00 -07002619 }
Romain Guyc7d53492010-06-25 13:41:57 -07002620}
Romain Guy9d5316e2010-06-24 19:30:36 -07002621
Chris Craikd218a922014-01-02 17:13:34 -08002622void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2623 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002624 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002625 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07002626
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002627 TextShadow textShadow;
2628 if (!getTextShadow(paint, &textShadow)) {
2629 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2630 }
2631
Raph Levien416a8472012-07-19 22:48:17 -07002632 // NOTE: The drop shadow will not perform gamma correction
2633 // if shader-based correction is enabled
2634 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2635 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002636 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002637 // If the drop shadow exceeds the max texture size or couldn't be
2638 // allocated, skip drawing
2639 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002640 const AutoTexture autoCleanup(shadow);
2641
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002642 const float sx = x - shadow->left + textShadow.dx;
2643 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002644
Tom Hudson984162f2014-10-10 13:38:16 -04002645 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002646 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002647 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002648 }
2649
2650 setupDraw();
2651 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002652 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002653 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002654 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002655 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002656 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002657 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2658 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002659 setupDrawTexture(shadow->id);
2660 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002661 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002662 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08002663 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Raph Levien416a8472012-07-19 22:48:17 -07002664
Chris Craik96a5c4c2015-01-27 15:46:35 -08002665 glDrawArrays(GL_TRIANGLE_STRIP, 0, kMeshCount);
Raph Levien416a8472012-07-19 22:48:17 -07002666}
2667
Romain Guy768bffc2013-02-27 13:50:45 -08002668bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002669 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05002670 return MathUtils::isZero(alpha)
2671 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002672}
2673
Tom Hudson107843d2014-09-08 11:26:26 -04002674void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002675 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002676 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002677 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002678 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002679
Romain Guy671d6cf2012-01-18 12:39:17 -08002680 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002681 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002682 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002683 }
2684
Chris Craik65fe5ee2015-01-26 18:06:29 -08002685 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002686
Romain Guy671d6cf2012-01-18 12:39:17 -08002687 float x = 0.0f;
2688 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002689 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002690 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002691 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2692 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002693 }
2694
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002695 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002696 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002697
2698 int alpha;
2699 SkXfermode::Mode mode;
2700 getAlphaAndMode(paint, &alpha, &mode);
2701
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002702 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002703 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002704 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002705 }
2706
Romain Guy671d6cf2012-01-18 12:39:17 -08002707 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002708 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002709 if (pureTranslate && !linearFilter) {
2710 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2711 }
Romain Guy257ae352013-03-20 16:31:12 -07002712 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002713
Rob Tsuk487a92c2015-01-06 13:22:54 -08002714 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002715 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2716
Romain Guy211370f2012-02-01 16:10:55 -08002717 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002718
Victoria Lease1e546812013-06-25 14:25:17 -07002719 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002720 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002721 positions, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002722 if (hasActiveLayer) {
2723 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002724 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002725 }
2726 dirtyLayerUnchecked(bounds, getRegion());
2727 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002728 }
Chet Haase48659092012-05-31 15:21:51 -07002729
Tom Hudson107843d2014-09-08 11:26:26 -04002730 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002731}
2732
Chris Craik59744b72014-07-01 17:56:52 -07002733bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002734 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002735 outMatrix->setIdentity();
2736 return false;
2737 } else if (CC_UNLIKELY(transform.isPerspective())) {
2738 outMatrix->setIdentity();
2739 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002740 }
Chris Craik59744b72014-07-01 17:56:52 -07002741
2742 /**
Chris Craika736cd92014-08-04 13:18:38 -07002743 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2744 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002745 */
2746 float sx, sy;
2747 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002748 outMatrix->setScale(
2749 roundf(fmaxf(1.0f, sx)),
2750 roundf(fmaxf(1.0f, sy)));
2751 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002752}
2753
Tom Hudson984162f2014-10-10 13:38:16 -04002754int OpenGLRenderer::getSaveCount() const {
2755 return mState.getSaveCount();
2756}
2757
2758int OpenGLRenderer::save(int flags) {
2759 return mState.save(flags);
2760}
2761
2762void OpenGLRenderer::restore() {
2763 return mState.restore();
2764}
2765
2766void OpenGLRenderer::restoreToCount(int saveCount) {
2767 return mState.restoreToCount(saveCount);
2768}
2769
2770void OpenGLRenderer::translate(float dx, float dy, float dz) {
2771 return mState.translate(dx, dy, dz);
2772}
2773
2774void OpenGLRenderer::rotate(float degrees) {
2775 return mState.rotate(degrees);
2776}
2777
2778void OpenGLRenderer::scale(float sx, float sy) {
2779 return mState.scale(sx, sy);
2780}
2781
2782void OpenGLRenderer::skew(float sx, float sy) {
2783 return mState.skew(sx, sy);
2784}
2785
2786void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2787 mState.setMatrix(matrix);
2788}
2789
2790void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2791 mState.concatMatrix(matrix);
2792}
2793
2794bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2795 return mState.clipRect(left, top, right, bottom, op);
2796}
2797
2798bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2799 return mState.clipPath(path, op);
2800}
2801
2802bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2803 return mState.clipRegion(region, op);
2804}
2805
2806void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2807 mState.setClippingOutline(allocator, outline);
2808}
2809
2810void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2811 const Rect& rect, float radius, bool highPriority) {
2812 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2813}
2814
Tom Hudson107843d2014-09-08 11:26:26 -04002815void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002816 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002817 DrawOpMode drawOpMode) {
2818
Chris Craik527a3aa2013-03-04 10:19:31 -08002819 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002820 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2821 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002822 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002823 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002824 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002825 }
Romain Guycac5fd32011-12-01 20:08:50 -08002826 }
2827
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002828 const float oldX = x;
2829 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002830
Chris Craikd6b65f62014-01-01 14:45:21 -08002831 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002832 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002833
Romain Guy211370f2012-02-01 16:10:55 -08002834 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002835 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2836 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002837 }
2838
Romain Guy86568192010-12-14 15:55:39 -08002839 int alpha;
2840 SkXfermode::Mode mode;
2841 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002842
Romain Guyc74f45a2013-02-26 19:10:14 -08002843 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2844
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002845 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002846 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002847 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002848 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002849 }
2850
Romain Guy19d4dd82013-03-04 11:14:26 -08002851 const bool hasActiveLayer = hasLayer();
2852
Romain Guy624234f2013-03-05 16:43:31 -08002853 // We only pass a partial transform to the font renderer. That partial
2854 // matrix defines how glyphs are rasterized. Typically we want glyphs
2855 // to be rasterized at their final size on screen, which means the partial
2856 // matrix needs to take the scale factor into account.
2857 // When a partial matrix is used to transform glyphs during rasterization,
2858 // the mesh is generated with the inverse transform (in the case of scale,
2859 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2860 // apply the full transform matrix at draw time in the vertex shader.
2861 // Applying the full matrix in the shader is the easiest way to handle
2862 // rotation and perspective and allows us to always generated quads in the
2863 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002864 SkMatrix fontTransform;
2865 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2866 || fabs(y - (int) y) > 0.0f
2867 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002868 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002869 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002870
Romain Guy624234f2013-03-05 16:43:31 -08002871 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08002872 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002873 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 -07002874
Raph Levien996e57c2012-07-23 15:22:52 -07002875 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002876 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002877
2878 // don't call issuedrawcommand, do it at end of batch
2879 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002880 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002881 SkPaint paintCopy(*paint);
2882 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2883 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002884 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002885 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002886 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002887 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002888 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002889
Chris Craik527a3aa2013-03-04 10:19:31 -08002890 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002891 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002892 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002893 }
Chris Craik41541822013-05-03 16:35:54 -07002894 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002895 }
Romain Guy694b5192010-07-21 21:33:20 -07002896
Chris Craike63f7c622013-10-17 10:30:55 -07002897 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002898
Tom Hudson107843d2014-09-08 11:26:26 -04002899 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002900}
2901
Tom Hudson107843d2014-09-08 11:26:26 -04002902void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002903 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002904 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002905 return;
Romain Guy03d58522012-02-24 17:54:07 -08002906 }
2907
Chris Craik39a908c2013-06-13 14:39:01 -07002908 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002909 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002910
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002911 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002912 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002913 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002914
2915 int alpha;
2916 SkXfermode::Mode mode;
2917 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002918 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002919
Tom Hudson984162f2014-10-10 13:38:16 -04002920 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002921 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 -08002922
Romain Guy97771732012-02-28 18:17:02 -08002923 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002924
2925 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chris Craike84a2082014-12-22 14:28:49 -08002926 hOffset, vOffset, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002927 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002928 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002929 dirtyLayerUnchecked(bounds, getRegion());
2930 }
Romain Guy97771732012-02-28 18:17:02 -08002931 }
Chet Haase48659092012-05-31 15:21:51 -07002932
Tom Hudson107843d2014-09-08 11:26:26 -04002933 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002934}
2935
Tom Hudson107843d2014-09-08 11:26:26 -04002936void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002937 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002938
Chris Craik44eb2c02015-01-29 09:45:09 -08002939 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002940
Romain Guyfb8b7632010-08-23 21:05:08 -07002941 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002942 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002943 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002944
Romain Guy8b55f372010-08-18 17:10:07 -07002945 const float x = texture->left - texture->offset;
2946 const float y = texture->top - texture->offset;
2947
Romain Guy01d58e42011-01-19 21:54:02 -08002948 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002949 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002950}
2951
Tom Hudson107843d2014-09-08 11:26:26 -04002952void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002953 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002954 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002955 }
2956
Chris Craike84a2082014-12-22 14:28:49 -08002957 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002958 if (layer->isTextureLayer()) {
2959 transform = &layer->getTransform();
2960 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002961 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002962 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002963 }
2964 }
2965
Chris Craik39a908c2013-06-13 14:39:01 -07002966 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08002967 const bool rejected = mState.calculateQuickRejectForScissor(
2968 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2969 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002970
2971 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002972 if (transform && !transform->isIdentity()) {
2973 restore();
2974 }
Tom Hudson107843d2014-09-08 11:26:26 -04002975 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002976 }
2977
Chris Craik62d307c2014-07-29 10:35:13 -07002978 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2979 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2980
Romain Guy5bb3c732012-11-29 17:52:58 -08002981 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002982
Chris Craik65fe5ee2015-01-26 18:06:29 -08002983 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08002984 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002985
Romain Guy211370f2012-02-01 16:10:55 -08002986 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002987 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002988 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2989 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002990 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002991
Chris Craik16ecda52013-03-29 10:59:59 -07002992 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002993 setupDraw();
2994 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002995 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002996 setupDrawColorFilter(layer->getColorFilter());
2997 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002998 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002999 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003000 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07003001 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08003002 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3003 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3004 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003005
Romain Guyd21b6e12011-11-30 20:21:23 -08003006 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003007 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003008 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003009 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003010 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003011 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003012 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3013 }
Romain Guyf219da52011-01-16 12:54:25 -08003014
Romain Guy448455f2013-07-22 13:57:50 -07003015 TextureVertex* mesh = &layer->mesh[0];
3016 GLsizei elementsCount = layer->meshElementCount;
3017
3018 while (elementsCount > 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08003019 GLsizei drawCount = min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Romain Guy448455f2013-07-22 13:57:50 -07003020
Romain Guy3380cfd2013-08-15 16:57:57 -07003021 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003022 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
Chris Craike84a2082014-12-22 14:28:49 -08003023 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr));
Romain Guy448455f2013-07-22 13:57:50 -07003024
3025 elementsCount -= drawCount;
3026 // Though there are 4 vertices in a quad, we use 6 indices per
3027 // quad to draw with GL_TRIANGLES
3028 mesh += (drawCount / 6) * 4;
3029 }
Romain Guyf219da52011-01-16 12:54:25 -08003030
Romain Guy3a3133d2011-02-01 22:59:58 -08003031#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003032 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003033#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003034 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003035
Romain Guy5bb3c732012-11-29 17:52:58 -08003036 if (layer->debugDrawUpdate) {
3037 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003038
3039 SkPaint paint;
3040 paint.setColor(0x7f00ff00);
3041 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003042 }
Romain Guyf219da52011-01-16 12:54:25 -08003043 }
Chris Craik34416ea2013-04-15 16:08:28 -07003044 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003045
Romain Guyb2e2f242012-10-17 18:18:35 -07003046 if (transform && !transform->isIdentity()) {
3047 restore();
3048 }
3049
Tom Hudson107843d2014-09-08 11:26:26 -04003050 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003051}
3052
Romain Guy6926c722010-07-12 20:20:03 -07003053///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003054// Draw filters
3055///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003056void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3057 // We should never get here since we apply the draw filter when stashing
3058 // the paints in the DisplayList.
3059 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003060}
3061
3062///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003063// Drawing implementation
3064///////////////////////////////////////////////////////////////////////////////
3065
Chris Craikd218a922014-01-02 17:13:34 -08003066Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003067 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003068 if (!texture) {
3069 return mCaches.textureCache.get(bitmap);
3070 }
3071 return texture;
3072}
3073
Romain Guy01d58e42011-01-19 21:54:02 -08003074void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003075 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003076 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003077 return;
3078 }
3079
3080 int alpha;
3081 SkXfermode::Mode mode;
3082 getAlphaAndMode(paint, &alpha, &mode);
3083
3084 setupDraw();
3085 setupDrawWithTexture(true);
3086 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003087 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003088 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003089 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003090 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003091 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3092 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003093 setupDrawTexture(texture->id);
3094 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003095 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003096 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08003097 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Romain Guy01d58e42011-01-19 21:54:02 -08003098
Chris Craik96a5c4c2015-01-27 15:46:35 -08003099 glDrawArrays(GL_TRIANGLE_STRIP, 0, kMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003100}
3101
Romain Guyf607bdc2010-09-10 19:20:06 -07003102// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003103#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3104#define kStdUnderline_Offset (1.0f / 9.0f)
3105#define kStdUnderline_Thickness (1.0f / 18.0f)
3106
Chris Craikd218a922014-01-02 17:13:34 -08003107void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3108 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003109 // Handle underline and strike-through
3110 uint32_t flags = paint->getFlags();
3111 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003112 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003113
Romain Guy211370f2012-02-01 16:10:55 -08003114 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003115 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003116 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003117
Raph Levien8b4072d2012-07-30 15:50:00 -07003118 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003119 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003120
Romain Guyf6834472011-01-23 13:32:12 -08003121 int linesCount = 0;
3122 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3123 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3124
3125 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003126 float points[pointsCount];
3127 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003128
3129 if (flags & SkPaint::kUnderlineText_Flag) {
3130 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003131 points[currentPoint++] = left;
3132 points[currentPoint++] = top;
3133 points[currentPoint++] = left + underlineWidth;
3134 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003135 }
3136
3137 if (flags & SkPaint::kStrikeThruText_Flag) {
3138 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003139 points[currentPoint++] = left;
3140 points[currentPoint++] = top;
3141 points[currentPoint++] = left + underlineWidth;
3142 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003143 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003144
Romain Guy726aeba2011-06-01 14:52:00 -07003145 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003146
Romain Guy726aeba2011-06-01 14:52:00 -07003147 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003148 }
3149 }
3150}
3151
Tom Hudson107843d2014-09-08 11:26:26 -04003152void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003153 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003154 return;
Romain Guy672433d2013-01-04 19:05:13 -08003155 }
3156
Tom Hudson107843d2014-09-08 11:26:26 -04003157 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003158}
3159
Tom Hudson107843d2014-09-08 11:26:26 -04003160void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003161 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003162 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003163
Chris Craik15a07a22014-01-26 13:43:53 -08003164 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08003165 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07003166
3167 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003168 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003169
ztenghui14a4e352014-08-13 10:44:39 -07003170 // The caller has made sure casterAlpha > 0.
3171 float ambientShadowAlpha = mAmbientShadowAlpha;
3172 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3173 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3174 }
3175 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3176 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003177 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003178 }
ztenghui7b4516e2014-01-07 10:42:55 -08003179
ztenghui14a4e352014-08-13 10:44:39 -07003180 float spotShadowAlpha = mSpotShadowAlpha;
3181 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3182 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3183 }
3184 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3185 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003186 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003187 }
ztenghui7b4516e2014-01-07 10:42:55 -08003188
Tom Hudson107843d2014-09-08 11:26:26 -04003189 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003190}
3191
Tom Hudson107843d2014-09-08 11:26:26 -04003192void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003193 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003194 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003195 return;
Romain Guy3b753822013-03-05 10:27:35 -08003196 }
Romain Guy735738c2012-12-03 12:34:51 -08003197
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003198 int color = paint->getColor();
3199 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003200 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003201 color |= 0x00ffffff;
3202 }
3203
Romain Guy672433d2013-01-04 19:05:13 -08003204 float left = FLT_MAX;
3205 float top = FLT_MAX;
3206 float right = FLT_MIN;
3207 float bottom = FLT_MIN;
3208
Romain Guy448455f2013-07-22 13:57:50 -07003209 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003210 Vertex* vertex = mesh;
3211
Chris Craik2af46352012-11-26 18:30:17 -08003212 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003213 float l = rects[index + 0];
3214 float t = rects[index + 1];
3215 float r = rects[index + 2];
3216 float b = rects[index + 3];
3217
Romain Guy3b753822013-03-05 10:27:35 -08003218 Vertex::set(vertex++, l, t);
3219 Vertex::set(vertex++, r, t);
3220 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003221 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003222
Romain Guy3b753822013-03-05 10:27:35 -08003223 left = fminf(left, l);
3224 top = fminf(top, t);
3225 right = fmaxf(right, r);
3226 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003227 }
3228
Chris Craikf0a59072013-11-19 18:00:46 -08003229 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003230 return;
Romain Guya362c692013-02-04 13:50:16 -08003231 }
Romain Guy672433d2013-01-04 19:05:13 -08003232
Romain Guy672433d2013-01-04 19:05:13 -08003233 setupDraw();
3234 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003235 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003236 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003237 setupDrawColorFilter(getColorFilter(paint));
3238 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003239 setupDrawProgram();
3240 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003241 setupDrawModelView(kModelViewMode_Translate, false,
3242 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003243 setupDrawColorUniforms(getShader(paint));
3244 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003245 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003246
Romain Guy8ce00302013-01-15 18:51:42 -08003247 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003248 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003249 }
3250
Chris Craik4063a0e2013-11-15 16:06:56 -08003251 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003252
Tom Hudson107843d2014-09-08 11:26:26 -04003253 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003254}
3255
Romain Guy026c5e162010-06-28 17:12:22 -07003256void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003257 const SkPaint* paint, bool ignoreTransform) {
3258 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003259 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003260 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003261 color |= 0x00ffffff;
3262 }
3263
Romain Guy70ca14e2010-12-13 18:24:33 -08003264 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003265 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003266 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003267 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003268 setupDrawColorFilter(getColorFilter(paint));
3269 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003270 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003271 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3272 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003273 setupDrawColorUniforms(getShader(paint));
3274 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003275 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003276 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003277
Chris Craik96a5c4c2015-01-27 15:46:35 -08003278 glDrawArrays(GL_TRIANGLE_STRIP, 0, kMeshCount);
Romain Guyc95c8d62010-09-17 15:31:32 -07003279}
3280
Romain Guy82ba8142010-07-09 13:25:56 -07003281void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003282 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003283 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003284
Chris Craike84a2082014-12-22 14:28:49 -08003285 GLvoid* vertices = (GLvoid*) nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -08003286 GLvoid* texCoords = (GLvoid*) kMeshTextureOffset;
Romain Guy3b748a42013-04-17 18:54:38 -07003287
3288 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003289 vertices = &mMeshVertices[0].x;
3290 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003291
3292 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3293 texture->uvMapper->map(uvs);
3294
3295 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3296 }
3297
Chris Craikd6b65f62014-01-01 14:45:21 -08003298 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3299 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3300 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003301
Romain Guyd21b6e12011-11-30 20:21:23 -08003302 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003303 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003304 paint, texture->blend, vertices, texCoords,
Chris Craik96a5c4c2015-01-27 15:46:35 -08003305 GL_TRIANGLE_STRIP, kMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003306 } else {
Chris Craik678625242014-02-28 12:26:34 -08003307 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003308 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Chris Craik96a5c4c2015-01-27 15:46:35 -08003309 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, kMeshCount);
Romain Guy3b748a42013-04-17 18:54:38 -07003310 }
3311
3312 if (texture->uvMapper) {
3313 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003314 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003315}
3316
Romain Guyf7f93552010-07-08 19:17:03 -07003317void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003318 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003319 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003320 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3321 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003322
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003323 int a;
3324 SkXfermode::Mode mode;
3325 getAlphaAndMode(paint, &a, &mode);
3326 const float alpha = a / 255.0f;
3327
Romain Guy746b7402010-10-26 16:27:31 -07003328 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003329 setupDrawWithTexture();
3330 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003331 setupDrawColorFilter(getColorFilter(paint));
3332 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003333 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003334 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003335 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003336 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003337 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003338 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003339 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003340
Romain Guy6820ac82010-09-15 18:11:50 -07003341 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003342}
3343
Romain Guy3b748a42013-04-17 18:54:38 -07003344void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003345 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003346 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003347 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3348 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003349
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003350 int a;
3351 SkXfermode::Mode mode;
3352 getAlphaAndMode(paint, &a, &mode);
3353 const float alpha = a / 255.0f;
3354
Romain Guy3b748a42013-04-17 18:54:38 -07003355 setupDraw();
3356 setupDrawWithTexture();
3357 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003358 setupDrawColorFilter(getColorFilter(paint));
3359 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003360 setupDrawProgram();
3361 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003362 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003363 setupDrawTexture(texture);
3364 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003365 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003366 setupDrawMeshIndices(vertices, texCoords, vbo);
3367
Chris Craike84a2082014-12-22 14:28:49 -08003368 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy3b748a42013-04-17 18:54:38 -07003369}
3370
Romain Guy886b2752013-01-04 12:26:18 -08003371void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003372 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003373 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003374 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003375
Chris Craike84a2082014-12-22 14:28:49 -08003376 int color = paint != nullptr ? paint->getColor() : 0;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003377 int alpha;
3378 SkXfermode::Mode mode;
3379 getAlphaAndMode(paint, &alpha, &mode);
3380
Romain Guy886b2752013-01-04 12:26:18 -08003381 setupDraw();
3382 setupDrawWithTexture(true);
Chris Craike84a2082014-12-22 14:28:49 -08003383 if (paint != nullptr) {
Romain Guy886b2752013-01-04 12:26:18 -08003384 setupDrawAlpha8Color(color, alpha);
3385 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003386 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003387 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003388 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003389 setupDrawProgram();
3390 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003391 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003392 setupDrawTexture(texture);
3393 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003394 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003395 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003396 setupDrawMesh(vertices, texCoords);
3397
3398 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003399}
3400
Romain Guya5aed0d2010-09-09 14:42:43 -07003401void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003402 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003403
Chris Craike84a2082014-12-22 14:28:49 -08003404 if (writableSnapshot()->roundRectClipState != nullptr /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003405 blend = true;
3406 mDescription.hasRoundRectClip = true;
3407 }
3408 mSkipOutlineClip = true;
3409
Romain Guy82ba8142010-07-09 13:25:56 -07003410 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003411
Romain Guy82ba8142010-07-09 13:25:56 -07003412 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003413 // These blend modes are not supported by OpenGL directly and have
3414 // to be implemented using shaders. Since the shader will perform
3415 // the blending, turn blending off here
3416 // If the blend mode cannot be implemented using shaders, fall
3417 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003418 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003419 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003420 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003421 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003422
Chris Craik44eb2c02015-01-29 09:45:09 -08003423 mRenderState.blend().disable();
Romain Guy82bc7a72012-01-03 14:13:39 -08003424 return;
3425 } else {
3426 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003427 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003428 }
Chris Craik44eb2c02015-01-29 09:45:09 -08003429 mRenderState.blend().enable(mode, swapSrcDst);
3430 } else {
3431 mRenderState.blend().disable();
Romain Guy82ba8142010-07-09 13:25:56 -07003432 }
Romain Guybd6b79b2010-06-26 00:13:53 -07003433}
3434
Romain Guy026c5e162010-06-28 17:12:22 -07003435void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003436 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003437 TextureVertex::setUV(v++, u1, v1);
3438 TextureVertex::setUV(v++, u2, v1);
3439 TextureVertex::setUV(v++, u1, v2);
3440 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003441}
3442
Chris Craike84a2082014-12-22 14:28:49 -08003443void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
3444 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003445 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003446 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3447 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003448 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003449 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003450 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003451}
3452
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003453float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003454 float alpha;
3455 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3456 alpha = mDrawModifiers.mOverrideLayerAlpha;
3457 } else {
3458 alpha = layer->getAlpha() / 255.0f;
3459 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003460 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003461}
3462
Romain Guy9d5316e2010-06-24 19:30:36 -07003463}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003464}; // namespace android