blob: 237833716e45889997e85c73c273d04a064ea5e7 [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
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
Chris Craik65fe5ee2015-01-26 18:06:29 -080019#include "OpenGLRenderer.h"
20
21#include "DeferredDisplayList.h"
22#include "DisplayListRenderer.h"
23#include "Fence.h"
24#include "GammaFontRenderer.h"
25#include "Patch.h"
26#include "PathTessellator.h"
27#include "Properties.h"
28#include "RenderNode.h"
29#include "renderstate/RenderState.h"
30#include "ShadowTessellator.h"
31#include "SkiaShader.h"
32#include "Vector.h"
33#include "VertexBuffer.h"
34#include "utils/GLUtils.h"
35#include "utils/PaintUtils.h"
36#include "utils/TraceUtils.h"
37
Romain Guye4d01122010-06-16 18:44:05 -070038#include <stdlib.h>
39#include <stdint.h>
40#include <sys/types.h>
41
Romain Guy5cbbce52010-06-27 22:59:20 -070042#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070043#include <SkColor.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040044#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070045#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070046
Romain Guye4d01122010-06-16 18:44:05 -070047#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070048#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070049
Romain Guy08aa2cb2011-03-17 11:06:57 -070050#include <private/hwui/DrawGlInfo.h>
51
Romain Guy5b3b3522010-10-27 18:57:51 -070052#include <ui/Rect.h>
53
Chris Craik62d307c2014-07-29 10:35:13 -070054#if DEBUG_DETAILED_EVENTS
55 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
56#else
57 #define EVENT_LOGD(...)
58#endif
59
Romain Guye4d01122010-06-16 18:44:05 -070060namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070061namespace uirenderer {
62
Chris Craik678625242014-02-28 12:26:34 -080063static GLenum getFilter(const SkPaint* paint) {
64 if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) {
65 return GL_LINEAR;
66 }
67 return GL_NEAREST;
68}
Romain Guyd21b6e12011-11-30 20:21:23 -080069
Romain Guy9d5316e2010-06-24 19:30:36 -070070///////////////////////////////////////////////////////////////////////////////
71// Globals
72///////////////////////////////////////////////////////////////////////////////
73
Romain Guyf607bdc2010-09-10 19:20:06 -070074
Romain Guyf6a11b82010-06-23 17:47:49 -070075///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -070076// Functions
77///////////////////////////////////////////////////////////////////////////////
78
79template<typename T>
80static inline T min(T a, T b) {
81 return a < b ? a : b;
82}
83
84///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -070085// Constructors/destructor
86///////////////////////////////////////////////////////////////////////////////
87
John Reck3b202512014-06-23 13:13:08 -070088OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -040089 : mState(*this)
Chris Craik058fc642014-07-23 18:19:28 -070090 , mCaches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -070091 , mExtensions(Extensions::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -070092 , mRenderState(renderState)
Chris Craik65fe5ee2015-01-26 18:06:29 -080093 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -070094 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -070095 , mSuppressTiling(false)
96 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -040097 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -070098 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -070099 , mLightRadius(FLT_MIN)
100 , mAmbientShadowAlpha(0)
101 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800102 // *set* draw modifiers to be 0
103 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700104 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700105
Chris Craik96a5c4c2015-01-27 15:46:35 -0800106 memcpy(mMeshVertices, kMeshVertices, sizeof(kMeshVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700107}
108
Romain Guy85bf02f2010-06-22 13:11:24 -0700109OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700110 // The context has already been destroyed at this point, do not call
111 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700112}
113
Romain Guy87e2f7572012-09-24 11:37:12 -0700114void OpenGLRenderer::initProperties() {
115 char property[PROPERTY_VALUE_MAX];
116 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
117 mScissorOptimizationDisabled = !strcasecmp(property, "true");
118 INIT_LOGD(" Scissor optimization %s",
119 mScissorOptimizationDisabled ? "disabled" : "enabled");
120 } else {
121 INIT_LOGD(" Scissor optimization enabled");
122 }
Romain Guy13631f32012-01-30 17:41:55 -0800123}
124
Chris Craik058fc642014-07-23 18:19:28 -0700125void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
126 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
127 mLightCenter = lightCenter;
128 mLightRadius = lightRadius;
129 mAmbientShadowAlpha = ambientShadowAlpha;
130 mSpotShadowAlpha = spotShadowAlpha;
131}
132
Romain Guy13631f32012-01-30 17:41:55 -0800133///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700134// Setup
135///////////////////////////////////////////////////////////////////////////////
136
Chris Craik797b95b22014-05-20 18:10:25 -0700137void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700138 glDisable(GL_DITHER);
139 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
140
141 glEnableVertexAttribArray(Program::kBindingPosition);
Chris Craik284b2432014-09-18 16:05:35 -0700142 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700143}
144
Romain Guy96885eb2013-03-26 15:05:58 -0700145void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800146 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800147 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400148 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700149 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700150 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700151}
152
Tom Hudson107843d2014-09-08 11:26:26 -0400153void OpenGLRenderer::startFrame() {
154 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700155 mFrameStarted = true;
156
Tom Hudson984162f2014-10-10 13:38:16 -0400157 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700158
Romain Guy96885eb2013-03-26 15:05:58 -0700159 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700160
Tom Hudson984162f2014-10-10 13:38:16 -0400161 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800162
Romain Guy54c1a642012-09-27 17:55:46 -0700163 // Functors break the tiling extension in pretty spectacular ways
164 // This ensures we don't use tiling when a functor is going to be
165 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700166 mSuppressTiling = mCaches.hasRegisteredFunctors()
167 || mFirstFrameAfterResize;
168 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700169
Chris Craikd6b65f62014-01-01 14:45:21 -0800170 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700171
Romain Guy7c450aa2012-09-21 19:15:00 -0700172 debugOverdraw(true, true);
173
Tom Hudson107843d2014-09-08 11:26:26 -0400174 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700175 mTilingClip.right, mTilingClip.bottom, mOpaque);
176}
177
Tom Hudson107843d2014-09-08 11:26:26 -0400178void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700179 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700180
Romain Guy96885eb2013-03-26 15:05:58 -0700181 setupFrameState(left, top, right, bottom, opaque);
182
183 // Layer renderers will start the frame immediately
184 // The framebuffer renderer will first defer the display list
185 // for each layer and wait until the first drawing command
186 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800187 if (currentSnapshot()->fbo == 0) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800188 mRenderState.blend().syncEnabled();
Romain Guy96885eb2013-03-26 15:05:58 -0700189 updateLayers();
190 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400191 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700192 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700193}
194
Romain Guydcfc8362013-01-03 13:08:57 -0800195void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
196 // If we know that we are going to redraw the entire framebuffer,
197 // perform a discard to let the driver know we don't need to preserve
198 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800199 if (mExtensions.hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400200 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
201 const bool isFbo = onGetTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800202 const GLenum attachments[] = {
203 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
204 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800205 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
206 }
207}
208
Tom Hudson107843d2014-09-08 11:26:26 -0400209void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700210 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800211 mRenderState.scissor().setEnabled(true);
212 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700213 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400214 mDirty = true;
215 return;
Romain Guyddf74372012-05-22 14:07:07 -0700216 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700217
Chris Craik65fe5ee2015-01-26 18:06:29 -0800218 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700219}
220
henry.uh_chen33f5a592014-07-02 19:36:56 +0800221void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700222 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800223 const Snapshot* snapshot = currentSnapshot();
224
Chris Craik14e51302013-12-30 15:32:54 -0800225 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800226 if (snapshot->flags & Snapshot::kFlagFboTarget) {
227 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700228 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700229
henry.uh_chen33f5a592014-07-02 19:36:56 +0800230 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800231 }
232}
233
henry.uh_chen33f5a592014-07-02 19:36:56 +0800234void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800235 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800236 if(expand) {
237 // Expand the startTiling region by 1
238 int leftNotZero = (clip.left > 0) ? 1 : 0;
239 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
240
241 mCaches.startTiling(
242 clip.left - leftNotZero,
243 windowHeight - clip.bottom - topNotZero,
244 clip.right - clip.left + leftNotZero + 1,
245 clip.bottom - clip.top + topNotZero + 1,
246 opaque);
247 } else {
248 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800249 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800250 }
Romain Guy54c1a642012-09-27 17:55:46 -0700251 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700252}
253
254void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700255 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700256}
257
Tom Hudson107843d2014-09-08 11:26:26 -0400258bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700259 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700260 endTiling();
Chris Craik4ac36f82014-12-09 16:54:03 -0800261 mTempPaths.clear();
262
Romain Guyca89e2a2013-03-08 17:44:20 -0800263 // When finish() is invoked on FBO 0 we've reached the end
264 // of the current frame
Tom Hudson984162f2014-10-10 13:38:16 -0400265 if (onGetTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800266 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700267 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800268 }
269
Romain Guy11cb6422012-09-21 00:39:43 -0700270 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700271#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700272 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700273#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700274
Romain Guyc15008e2010-11-10 11:59:15 -0800275#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800276 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700277#else
278 if (mCaches.getDebugLevel() & kDebugMemory) {
279 mCaches.dumpMemoryUsage();
280 }
Romain Guyc15008e2010-11-10 11:59:15 -0800281#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700282 }
Romain Guy96885eb2013-03-26 15:05:58 -0700283
284 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400285
286 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700287}
288
Romain Guy35643dd2012-09-18 15:40:58 -0700289void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700290 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
291 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700292 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700293
Chris Craik65fe5ee2015-01-26 18:06:29 -0800294 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700295 dirtyClip();
296}
297
Andreas Gampe64bb4132014-11-22 00:35:09 +0000298void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400299 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700300
Rob Tsuk487a92c2015-01-06 13:22:54 -0800301 Rect clip(mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700302 clip.snapToPixelBoundaries();
303
Romain Guyd643bb52011-03-01 14:55:21 -0800304 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800305 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800306 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800307 dirtyLayerUnchecked(clip, getRegion());
308 }
Romain Guyd643bb52011-03-01 14:55:21 -0800309
Romain Guy08aa2cb2011-03-17 11:06:57 -0700310 DrawGlInfo info;
311 info.clipLeft = clip.left;
312 info.clipTop = clip.top;
313 info.clipRight = clip.right;
314 info.clipBottom = clip.bottom;
315 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700316 info.width = getViewportWidth();
317 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800318 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700319
Tom Hudson984162f2014-10-10 13:38:16 -0400320 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700321 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400322 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700323 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
324 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800325 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700326 setScissorFromClip();
327 }
Chris Craik54f574a2013-08-26 11:23:46 -0700328
John Reck3b202512014-06-23 13:13:08 -0700329 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
330 // Scissor may have been modified, reset dirty clip
331 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800332
Tom Hudson107843d2014-09-08 11:26:26 -0400333 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800334}
335
Romain Guyf6a11b82010-06-23 17:47:49 -0700336///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700337// Debug
338///////////////////////////////////////////////////////////////////////////////
339
Chris Craik62d307c2014-07-29 10:35:13 -0700340void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
341#if DEBUG_DETAILED_EVENTS
342 const int BUFFER_SIZE = 256;
343 va_list ap;
344 char buf[BUFFER_SIZE];
345
346 va_start(ap, fmt);
347 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
348 va_end(ap);
349
350 eventMark(buf);
351#endif
352}
353
354
Romain Guy0f6675332013-03-01 14:31:04 -0800355void OpenGLRenderer::eventMark(const char* name) const {
356 mCaches.eventMark(0, name);
357}
358
Romain Guy87e2f7572012-09-24 11:37:12 -0700359void OpenGLRenderer::startMark(const char* name) const {
360 mCaches.startMark(0, name);
361}
362
363void OpenGLRenderer::endMark() const {
364 mCaches.endMark();
365}
366
367void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700368 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700369}
370
371void OpenGLRenderer::renderOverdraw() {
Tom Hudson984162f2014-10-10 13:38:16 -0400372 if (mCaches.debugOverdraw && onGetTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700373 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700374
Chris Craik65fe5ee2015-01-26 18:06:29 -0800375 mRenderState.scissor().setEnabled(true);
376 mRenderState.scissor().set(clip->left,
377 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
378 clip->right - clip->left,
379 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700380
Romain Guy627c6fd2013-08-21 11:53:18 -0700381 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800382 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700383 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
384
385 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800386 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700387 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
388
389 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800390 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700391 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
392
393 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800394 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700395 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
396
Chris Craik96a5c4c2015-01-27 15:46:35 -0800397 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700398 }
399}
400
401///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700402// Layers
403///////////////////////////////////////////////////////////////////////////////
404
405bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700406 if (layer->deferredUpdateScheduled && layer->renderer
407 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700408
Romain Guy7c450aa2012-09-21 19:15:00 -0700409 if (inFrame) {
410 endTiling();
411 debugOverdraw(false, false);
412 }
Romain Guy11cb6422012-09-21 00:39:43 -0700413
Romain Guy96885eb2013-03-26 15:05:58 -0700414 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700415 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700416 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700417 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700418 }
Romain Guy11cb6422012-09-21 00:39:43 -0700419
420 if (inFrame) {
421 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800422 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700423 }
424
Romain Guy5bb3c732012-11-29 17:52:58 -0800425 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700426 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700427
428 return true;
429 }
430
431 return false;
432}
433
434void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700435 // If draw deferring is enabled this method will simply defer
436 // the display list of each individual layer. The layers remain
437 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700438 int count = mLayerUpdates.size();
439 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700440 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
441 startMark("Layer Updates");
442 } else {
443 startMark("Defer Layer Updates");
444 }
Romain Guy11cb6422012-09-21 00:39:43 -0700445
Chris Craik1206b9b2013-04-04 14:46:24 -0700446 // Note: it is very important to update the layers in order
447 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700448 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700449 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700450 }
Romain Guy11cb6422012-09-21 00:39:43 -0700451
Romain Guy96885eb2013-03-26 15:05:58 -0700452 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
453 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400454 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700455 }
456 endMark();
457 }
458}
459
460void OpenGLRenderer::flushLayers() {
461 int count = mLayerUpdates.size();
462 if (count > 0) {
463 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700464
Chris Craik1206b9b2013-04-04 14:46:24 -0700465 // Note: it is very important to update the layers in order
466 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800467 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700468 }
469
470 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400471 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700472
Romain Guy11cb6422012-09-21 00:39:43 -0700473 endMark();
474 }
475}
476
477void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
478 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700479 // Make sure we don't introduce duplicates.
480 // SortedVector would do this automatically but we need to respect
481 // the insertion order. The linear search is not an issue since
482 // this list is usually very short (typically one item, at most a few)
483 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
484 if (mLayerUpdates.itemAt(i) == layer) {
485 return;
486 }
487 }
Romain Guy11cb6422012-09-21 00:39:43 -0700488 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700489 }
490}
491
Romain Guye93482f2013-06-17 13:14:51 -0700492void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
493 if (layer) {
494 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
495 if (mLayerUpdates.itemAt(i) == layer) {
496 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700497 break;
498 }
499 }
500 }
501}
502
Romain Guy40543602013-06-12 15:31:28 -0700503void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800504 ATRACE_NAME("Update HW Layers");
Chris Craik44eb2c02015-01-29 09:45:09 -0800505 mRenderState.blend().syncEnabled();
Romain Guy40543602013-06-12 15:31:28 -0700506 updateLayers();
507 flushLayers();
508 // Wait for all the layer updates to be executed
509 AutoFence fence;
510}
511
John Reck443a7142014-09-04 17:40:05 -0700512void OpenGLRenderer::markLayersAsBuildLayers() {
513 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
514 mLayerUpdates[i]->wasBuildLayered = true;
515 }
516}
517
Romain Guy11cb6422012-09-21 00:39:43 -0700518///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700519// State management
520///////////////////////////////////////////////////////////////////////////////
521
Chris Craik14e51302013-12-30 15:32:54 -0800522void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700523 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800524 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
525 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700526
Chris Craika64a2be2014-05-14 14:17:01 -0700527 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700528 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700529 }
530
Romain Guy2542d192010-08-18 11:47:12 -0700531 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700532 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700533 }
Romain Guy2542d192010-08-18 11:47:12 -0700534
Romain Guy5ec99242010-11-03 16:19:08 -0700535 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700536 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700537 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700538 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800539 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700540 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700541 }
Romain Guybb9524b2010-06-22 18:56:38 -0700542}
543
Romain Guyf6a11b82010-06-23 17:47:49 -0700544///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700545// Layers
546///////////////////////////////////////////////////////////////////////////////
547
548int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700549 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700550 // force matrix/clip isolation for layer
551 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
552
Tom Hudson984162f2014-10-10 13:38:16 -0400553 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700554
Tom Hudson984162f2014-10-10 13:38:16 -0400555 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700556 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700557 }
Romain Guyd55a8612010-06-28 17:42:46 -0700558
559 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700560}
561
Chris Craikd90144d2013-03-19 15:03:48 -0700562void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
563 const Rect untransformedBounds(bounds);
564
Chris Craikd6b65f62014-01-01 14:45:21 -0800565 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700566
567 // Layers only make sense if they are in the framebuffer's bounds
Rob Tsuk487a92c2015-01-06 13:22:54 -0800568 if (bounds.intersect(mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700569 // We cannot work with sub-pixels in this case
570 bounds.snapToPixelBoundaries();
571
572 // When the layer is not an FBO, we may use glCopyTexImage so we
573 // need to make sure the layer does not extend outside the bounds
574 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700575 const Snapshot& previous = *(currentSnapshot()->previous);
576 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
577 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700578 bounds.setEmpty();
579 } else if (fboLayer) {
580 clip.set(bounds);
581 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800582 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700583 inverse.mapRect(clip);
584 clip.snapToPixelBoundaries();
585 if (clip.intersect(untransformedBounds)) {
586 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
587 bounds.set(untransformedBounds);
588 } else {
589 clip.setEmpty();
590 }
591 }
592 } else {
593 bounds.setEmpty();
594 }
595}
596
Chris Craik408eb122013-03-26 18:55:15 -0700597void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
598 bool fboLayer, int alpha) {
599 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
600 bounds.getHeight() > mCaches.maxTextureSize ||
601 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400602 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700603 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400604 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700605 }
606}
607
Chris Craikd90144d2013-03-19 15:03:48 -0700608int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500609 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400610 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700611
Tom Hudson984162f2014-10-10 13:38:16 -0400612 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700613 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
614 // operations will be able to store and restore the current clip and transform info, and
615 // quick rejection will be correct (for display lists)
616
617 Rect bounds(left, top, right, bottom);
618 Rect clip;
619 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500620 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700621
Tom Hudson984162f2014-10-10 13:38:16 -0400622 if (!mState.currentlyIgnored()) {
623 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
624 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
625 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800626 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700627 }
628 }
629
630 return count;
631}
632
Romain Guy1c740bc2010-09-13 18:00:09 -0700633/**
634 * Layers are viewed by Skia are slightly different than layers in image editing
635 * programs (for instance.) When a layer is created, previously created layers
636 * and the frame buffer still receive every drawing command. For instance, if a
637 * layer is created and a shape intersecting the bounds of the layers and the
638 * framebuffer is draw, the shape will be drawn on both (unless the layer was
639 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
640 *
641 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
642 * texture. Unfortunately, this is inefficient as it requires every primitive to
643 * be drawn n + 1 times, where n is the number of active layers. In practice this
644 * means, for every primitive:
645 * - Switch active frame buffer
646 * - Change viewport, clip and projection matrix
647 * - Issue the drawing
648 *
649 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700650 * To avoid this, layers are implemented in a different way here, at least in the
651 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
652 * is set. When this flag is set we can redirect all drawing operations into a
653 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700654 *
655 * This implementation relies on the frame buffer being at least RGBA 8888. When
656 * a layer is created, only a texture is created, not an FBO. The content of the
657 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700658 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700659 * buffer and drawing continues as normal. This technique therefore treats the
660 * frame buffer as a scratch buffer for the layers.
661 *
662 * To compose the layers back onto the frame buffer, each layer texture
663 * (containing the original frame buffer data) is drawn as a simple quad over
664 * the frame buffer. The trick is that the quad is set as the composition
665 * destination in the blending equation, and the frame buffer becomes the source
666 * of the composition.
667 *
668 * Drawing layers with an alpha value requires an extra step before composition.
669 * An empty quad is drawn over the layer's region in the frame buffer. This quad
670 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
671 * quad is used to multiply the colors in the frame buffer. This is achieved by
672 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
673 * GL_ZERO, GL_SRC_ALPHA.
674 *
675 * Because glCopyTexImage2D() can be slow, an alternative implementation might
676 * be use to draw a single clipped layer. The implementation described above
677 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700678 *
679 * (1) The frame buffer is actually not cleared right away. To allow the GPU
680 * to potentially optimize series of calls to glCopyTexImage2D, the frame
681 * buffer is left untouched until the first drawing operation. Only when
682 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700683 */
Chet Haased48885a2012-08-28 17:43:28 -0700684bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700685 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800686 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
687 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700688
Romain Guyeb993562010-10-05 18:14:38 -0700689 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
690
Romain Guyf607bdc2010-09-10 19:20:06 -0700691 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700692 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700693 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700694 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000695 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700696
697 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400698 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700699 return false;
700 }
Romain Guyf18fd992010-07-08 11:45:51 -0700701
Chris Craik44eb2c02015-01-29 09:45:09 -0800702 mCaches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700703 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700704 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700705 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700706 }
707
Derek Sollenberger674554f2014-02-19 16:47:32 +0000708 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700709 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700710 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
711 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500712
Chet Haasea23eed82012-04-12 15:19:04 -0700713 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700714 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700715 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700716
Romain Guy8fb95422010-08-17 18:38:51 -0700717 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400718 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
719 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700720
Chris Craika8bea8e2014-09-24 11:29:43 -0700721 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
722 fboLayer ? "" : "unclipped ",
723 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700724 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700725 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700726 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700727 } else {
728 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700729 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800730 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700731 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700732 // Workaround for some GL drivers. When reading pixels lying outside
733 // of the window we should get undefined values for those pixels.
734 // Unfortunately some drivers will turn the entire target texture black
735 // when reading outside of the window.
736 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800737 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700738 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800739 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700740
Chris Craika64a2be2014-05-14 14:17:01 -0700741 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
742 bounds.left, getViewportHeight() - bounds.bottom,
743 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700744
Romain Guy54be1cd2011-06-13 19:04:27 -0700745 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800746 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700747 }
Romain Guyeb993562010-10-05 18:14:38 -0700748 }
Romain Guyf86ef572010-07-01 11:05:42 -0700749
Romain Guyd55a8612010-06-28 17:42:46 -0700750 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700751}
752
Chris Craike63f7c622013-10-17 10:30:55 -0700753bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800754 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700755 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700756
Tom Hudson984162f2014-10-10 13:38:16 -0400757 writableSnapshot()->region = &writableSnapshot()->layer->region;
758 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
759 writableSnapshot()->fbo = layer->getFbo();
760 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
761 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
762 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800763 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700764
Romain Guy2b7028e2012-09-19 17:25:38 -0700765 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700766 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700767 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700768 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700769 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700770
771 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700772 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700773 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700774 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700775 }
776
777 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700778 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700779
henry.uh_chen33f5a592014-07-02 19:36:56 +0800780 // Expand the startTiling region by 1
781 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700782
783 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800784 mRenderState.scissor().setEnabled(true);
785 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700786 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700787 glClear(GL_COLOR_BUFFER_BIT);
788
789 dirtyClip();
790
791 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700792 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700793 return true;
794}
795
Romain Guy1c740bc2010-09-13 18:00:09 -0700796/**
797 * Read the documentation of createLayer() before doing anything in this method.
798 */
Chris Craik14e51302013-12-30 15:32:54 -0800799void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
800 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000801 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700802 return;
803 }
804
Chris Craik14e51302013-12-30 15:32:54 -0800805 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800806 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800807 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700808
Chris Craik39a908c2013-06-13 14:39:01 -0700809 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400810 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800811 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800812 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700813
Romain Guyeb993562010-10-05 18:14:38 -0700814 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700815 endTiling();
816
Romain Guye0aa84b2012-04-03 19:30:26 -0700817 // Detach the texture from the FBO
818 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800819
820 layer->removeFbo(false);
821
Romain Guyeb993562010-10-05 18:14:38 -0700822 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700823 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700824 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700825
Chris Craikd6b65f62014-01-01 14:45:21 -0800826 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700827 }
828
Romain Guy9ace8f52011-07-07 20:50:11 -0700829 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500830 SkPaint layerPaint;
831 layerPaint.setAlpha(layer->getAlpha());
832 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
833 layerPaint.setColorFilter(layer->getColorFilter());
834
835 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700836 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700837 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700838 }
839
Chris Craik96a5c4c2015-01-27 15:46:35 -0800840 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700841
Chris Craik44eb2c02015-01-29 09:45:09 -0800842 mCaches.textureState().activateTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700843
Romain Guy5b3b3522010-10-27 18:57:51 -0700844 // When the layer is stored in an FBO, we can save a bit of fillrate by
845 // drawing only the dirty region
846 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800847 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700848 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700849 } else if (!rect.isEmpty()) {
850 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530851
852 save(0);
853 // the layer contains screen buffer content that shouldn't be alpha modulated
854 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400855 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700856 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530857 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700858 }
Romain Guy8b55f372010-08-18 17:10:07 -0700859
Romain Guy746b7402010-10-26 16:27:31 -0700860 dirtyClip();
861
Romain Guyeb993562010-10-05 18:14:38 -0700862 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800863 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700864 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800865 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800866 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700867 }
868}
869
Romain Guyaa6c24c2011-04-28 18:40:04 -0700870void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700871 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700872
873 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700874 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700875 setupDrawWithTexture();
876 } else {
877 setupDrawWithExternalTexture();
878 }
879 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700880 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500881 setupDrawColorFilter(layer->getColorFilter());
882 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700883 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700884 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500885 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700886 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
887 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700888 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700889 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700890 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800891 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800892 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700893 layer->getWidth() == (uint32_t) rect.getWidth() &&
894 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800895 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
896 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700897
Romain Guyd21b6e12011-11-30 20:21:23 -0800898 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800899 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
900 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700901 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800902 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800903 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
904 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700905 }
906 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700907 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700908
Chris Craik96a5c4c2015-01-27 15:46:35 -0800909 glDrawArrays(GL_TRIANGLE_STRIP, 0, kMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700910}
911
Romain Guy5b3b3522010-10-27 18:57:51 -0700912void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700913 if (layer->isTextureLayer()) {
914 EVENT_LOGD("composeTextureLayerRect");
915 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
916 drawTextureLayer(layer, rect);
917 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
918 } else {
919 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700920 const Rect& texCoords = layer->texCoords;
921 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
922 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700923
Romain Guy9ace8f52011-07-07 20:50:11 -0700924 float x = rect.left;
925 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -0800926 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700927 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700928 layer->getHeight() == (uint32_t) rect.getHeight();
929
930 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700931 // When we're swapping, the layer is already in screen coordinates
932 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800933 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
934 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700935 }
936
Romain Guyd21b6e12011-11-30 20:21:23 -0800937 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700938 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800939 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700940 }
941
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500942 SkPaint layerPaint;
943 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
944 layerPaint.setXfermodeMode(layer->getMode());
945 layerPaint.setColorFilter(layer->getColorFilter());
946
947 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700948 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500949 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -0700950 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik96a5c4c2015-01-27 15:46:35 -0800951 GL_TRIANGLE_STRIP, kMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700952
Romain Guyaa6c24c2011-04-28 18:40:04 -0700953 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700954 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700955}
956
Chris Craik34416ea2013-04-15 16:08:28 -0700957/**
958 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
959 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
960 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
961 * by saveLayer's restore
962 */
Tom Hudson984162f2014-10-10 13:38:16 -0400963#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
964 DRAW_COMMAND; \
965 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
966 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
967 DRAW_COMMAND; \
968 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
969 } \
Chris Craik34416ea2013-04-15 16:08:28 -0700970 }
971
972#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
973
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400974// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
975// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
976class LayerShader : public SkShader {
977public:
978 LayerShader(Layer* layer, const SkMatrix* localMatrix)
979 : INHERITED(localMatrix)
980 , mLayer(layer) {
981 }
982
Chris Craike84a2082014-12-22 14:28:49 -0800983 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400984 if (data) {
985 *data = static_cast<void*>(mLayer);
986 }
987 return true;
988 }
989
Chris Craike84a2082014-12-22 14:28:49 -0800990 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400991 return !mLayer->isBlend();
992 }
993
994protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000995 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400996 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
997 }
998
Chris Craike84a2082014-12-22 14:28:49 -0800999 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001000 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1001 }
1002
Chris Craike84a2082014-12-22 14:28:49 -08001003 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001004 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -08001005 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001006 }
1007private:
1008 // Unowned.
1009 Layer* mLayer;
1010 typedef SkShader INHERITED;
1011};
1012
Romain Guy5b3b3522010-10-27 18:57:51 -07001013void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001014 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1015
1016 if (layer->getConvexMask()) {
1017 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1018
1019 // clip to the area of the layer the mask can be larger
1020 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1021
1022 SkPaint paint;
1023 paint.setAntiAlias(true);
1024 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1025
Chris Craik3f0854292014-04-15 16:18:08 -07001026 // create LayerShader to map SaveLayer content into subsequent draw
1027 SkMatrix shaderMatrix;
1028 shaderMatrix.setTranslate(rect.left, rect.bottom);
1029 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001030 LayerShader layerShader(layer, &shaderMatrix);
1031 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001032
1033 // Since the drawing primitive is defined in local drawing space,
1034 // we don't need to modify the draw matrix
1035 const SkPath* maskPath = layer->getConvexMask();
1036 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1037
Chris Craike84a2082014-12-22 14:28:49 -08001038 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -07001039 restore();
1040
1041 return;
1042 }
1043
Romain Guy5b3b3522010-10-27 18:57:51 -07001044 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001045 layer->setRegionAsRect();
1046
Chris Craik34416ea2013-04-15 16:08:28 -07001047 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001048
Romain Guy5b3b3522010-10-27 18:57:51 -07001049 layer->region.clear();
1050 return;
1051 }
1052
Chris Craik62d307c2014-07-29 10:35:13 -07001053 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001054 // standard Region based draw
1055 size_t count;
1056 const android::Rect* rects;
1057 Region safeRegion;
1058 if (CC_LIKELY(hasRectToRectTransform())) {
1059 rects = layer->region.getArray(&count);
1060 } else {
1061 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1062 rects = safeRegion.getArray(&count);
1063 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001064
Chris Craik3f0854292014-04-15 16:18:08 -07001065 const float alpha = getLayerAlpha(layer);
1066 const float texX = 1.0f / float(layer->getWidth());
1067 const float texY = 1.0f / float(layer->getHeight());
1068 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001069
Chris Craik3f0854292014-04-15 16:18:08 -07001070 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001071
Chris Craik3f0854292014-04-15 16:18:08 -07001072 // We must get (and therefore bind) the region mesh buffer
1073 // after we setup drawing in case we need to mess with the
1074 // stencil buffer in setupDraw()
1075 TextureVertex* mesh = mCaches.getRegionMesh();
1076 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001077
Chris Craik3f0854292014-04-15 16:18:08 -07001078 setupDrawWithTexture();
1079 setupDrawColor(alpha, alpha, alpha, alpha);
1080 setupDrawColorFilter(layer->getColorFilter());
1081 setupDrawBlending(layer);
1082 setupDrawProgram();
1083 setupDrawDirtyRegionsDisabled();
1084 setupDrawPureColorUniforms();
1085 setupDrawColorFilterUniforms(layer->getColorFilter());
1086 setupDrawTexture(layer->getTexture());
1087 if (currentTransform()->isPureTranslate()) {
1088 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1089 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001090
Chris Craik3f0854292014-04-15 16:18:08 -07001091 layer->setFilter(GL_NEAREST);
1092 setupDrawModelView(kModelViewMode_Translate, false,
1093 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1094 } else {
1095 layer->setFilter(GL_LINEAR);
1096 setupDrawModelView(kModelViewMode_Translate, false,
1097 rect.left, rect.top, rect.right, rect.bottom);
1098 }
1099 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001100
Chris Craik3f0854292014-04-15 16:18:08 -07001101 for (size_t i = 0; i < count; i++) {
1102 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001103
Chris Craik3f0854292014-04-15 16:18:08 -07001104 const float u1 = r->left * texX;
1105 const float v1 = (height - r->top) * texY;
1106 const float u2 = r->right * texX;
1107 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001108
Chris Craik3f0854292014-04-15 16:18:08 -07001109 // TODO: Reject quads outside of the clip
1110 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1111 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1112 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1113 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001114
Chris Craik3f0854292014-04-15 16:18:08 -07001115 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001116
Chris Craik96a5c4c2015-01-27 15:46:35 -08001117 if (numQuads >= kMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001118 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001119 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001120 numQuads = 0;
1121 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001122 }
Chris Craik3f0854292014-04-15 16:18:08 -07001123 }
1124
1125 if (numQuads > 0) {
1126 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001127 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001128 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001129
Romain Guy5b3b3522010-10-27 18:57:51 -07001130#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001131 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001132#endif
1133
Chris Craik3f0854292014-04-15 16:18:08 -07001134 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001135}
1136
Romain Guy3a3133d2011-02-01 22:59:58 -08001137#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001138void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001139 size_t count;
1140 const android::Rect* rects = region.getArray(&count);
1141
1142 uint32_t colors[] = {
1143 0x7fff0000, 0x7f00ff00,
1144 0x7f0000ff, 0x7fff00ff,
1145 };
1146
1147 int offset = 0;
1148 int32_t top = rects[0].top;
1149
1150 for (size_t i = 0; i < count; i++) {
1151 if (top != rects[i].top) {
1152 offset ^= 0x2;
1153 top = rects[i].top;
1154 }
1155
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001156 SkPaint paint;
1157 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001158 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001159 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001160 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001161}
Chris Craike63f7c622013-10-17 10:30:55 -07001162#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001163
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001164void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001165 Vector<float> rects;
1166
1167 SkRegion::Iterator it(region);
1168 while (!it.done()) {
1169 const SkIRect& r = it.rect();
1170 rects.push(r.fLeft);
1171 rects.push(r.fTop);
1172 rects.push(r.fRight);
1173 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001174 it.next();
1175 }
1176
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001177 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001178}
1179
Romain Guy5b3b3522010-10-27 18:57:51 -07001180void OpenGLRenderer::dirtyLayer(const float left, const float top,
1181 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001182 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001183 Rect bounds(left, top, right, bottom);
1184 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001185 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001186 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001187}
1188
1189void OpenGLRenderer::dirtyLayer(const float left, const float top,
1190 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001191 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001192 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001193 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001194 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001195}
1196
1197void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001198 if (bounds.intersect(mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001199 bounds.snapToPixelBoundaries();
1200 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1201 if (!dirty.isEmpty()) {
1202 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001203 }
1204 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001205}
1206
Chris Craik4063a0e2013-11-15 16:06:56 -08001207void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001208 GLsizei elementsCount = quadsCount * 6;
1209 while (elementsCount > 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001210 GLsizei drawCount = min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Romain Guy448455f2013-07-22 13:57:50 -07001211
Romain Guy3380cfd2013-08-15 16:57:57 -07001212 setupDrawIndexedVertices(&mesh[0].x);
Chris Craike84a2082014-12-22 14:28:49 -08001213 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy448455f2013-07-22 13:57:50 -07001214
1215 elementsCount -= drawCount;
1216 // Though there are 4 vertices in a quad, we use 6 indices per
1217 // quad to draw with GL_TRIANGLES
1218 mesh += (drawCount / 6) * 4;
1219 }
1220}
1221
Romain Guy54be1cd2011-06-13 19:04:27 -07001222void OpenGLRenderer::clearLayerRegions() {
1223 const size_t count = mLayers.size();
1224 if (count == 0) return;
1225
Tom Hudson984162f2014-10-10 13:38:16 -04001226 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001227 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001228 // Doing several glScissor/glClear here can negatively impact
1229 // GPUs with a tiler architecture, instead we draw quads with
1230 // the Clear blending mode
1231
1232 // The list contains bounds that have already been clipped
1233 // against their initial clip rect, and the current clip
1234 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001235 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001236
Romain Guy448455f2013-07-22 13:57:50 -07001237 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001238 Vertex* vertex = mesh;
1239
1240 for (uint32_t i = 0; i < count; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001241 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001242
Chris Craik51d6a3d2014-12-22 17:16:56 -08001243 Vertex::set(vertex++, bounds.left, bounds.top);
1244 Vertex::set(vertex++, bounds.right, bounds.top);
1245 Vertex::set(vertex++, bounds.left, bounds.bottom);
1246 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001247 }
Romain Guye67307c2013-02-11 18:01:20 -08001248 // We must clear the list of dirty rects before we
1249 // call setupDraw() to prevent stencil setup to do
1250 // the same thing again
1251 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001252
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001253 SkPaint clearPaint;
1254 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1255
Romain Guy54be1cd2011-06-13 19:04:27 -07001256 setupDraw(false);
1257 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001258 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001259 setupDrawProgram();
1260 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001261 setupDrawModelView(kModelViewMode_Translate, false,
1262 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001263
Chris Craik4063a0e2013-11-15 16:06:56 -08001264 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001265
Chris Craik65fe5ee2015-01-26 18:06:29 -08001266 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001267 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001268 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001269 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001270}
1271
Romain Guybd6b79b2010-06-26 00:13:53 -07001272///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001273// State Deferral
1274///////////////////////////////////////////////////////////////////////////////
1275
Chris Craikff785832013-03-08 13:12:16 -08001276bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001277 const Rect& currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001278 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001279
Chris Craikff785832013-03-08 13:12:16 -08001280 if (stateDeferFlags & kStateDeferFlag_Draw) {
1281 // state has bounds initialized in local coordinates
1282 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001283 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001284 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001285 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1286 // is used, it should more closely duplicate the quickReject logic (in how it uses
1287 // snapToPixelBoundaries)
1288
Rob Tsuk487a92c2015-01-06 13:22:54 -08001289 if (!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001290 // quick rejected
1291 return true;
1292 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001293
Chris Craika02c4ed2013-06-14 13:43:58 -07001294 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001295 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001296 int& flags = state.mClipSideFlags;
1297 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001298 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1299 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1300 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1301 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001302 }
1303 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001304 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001305 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1306 // overdraw avoidance (since we don't know what it overlaps)
1307 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001308 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001309 }
1310 }
1311
Chris Craik527a3aa2013-03-04 10:19:31 -08001312 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1313 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001314 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001315 }
1316
Chris Craik7273daa2013-03-28 11:25:24 -07001317 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1318 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001319 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001320 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001321 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001322
1323 // always store/restore, since it's just a pointer
1324 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001325 return false;
1326}
1327
Chris Craik527a3aa2013-03-04 10:19:31 -08001328void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001329 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001330 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001331 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001332 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001333
Chris Craik527a3aa2013-03-04 10:19:31 -08001334 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001335 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001336 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001337 dirtyClip();
1338 }
Chris Craikc3566d02013-02-04 16:16:33 -08001339}
1340
Chris Craik28ce94a2013-05-31 11:38:03 -07001341/**
1342 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1343 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1344 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1345 *
1346 * This method should be called when restoreDisplayState() won't be restoring the clip
1347 */
1348void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001349 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001350 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001351 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001352 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001353 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001354 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001355 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1356 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001357}
1358
Chris Craikc3566d02013-02-04 16:16:33 -08001359///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001360// Clipping
1361///////////////////////////////////////////////////////////////////////////////
1362
Romain Guybb9524b2010-06-22 18:56:38 -07001363void OpenGLRenderer::setScissorFromClip() {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001364 Rect clip(mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001365 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001366
Chris Craik65fe5ee2015-01-26 18:06:29 -08001367 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001368 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001369 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001370 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001371}
1372
Romain Guy8ce00302013-01-15 18:51:42 -08001373void OpenGLRenderer::ensureStencilBuffer() {
1374 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1375 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1376 // just hope we have one when hasLayer() returns false.
1377 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001378 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001379 }
1380}
1381
1382void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1383 // The layer's FBO is already bound when we reach this stage
1384 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001385 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1386 // is attached after we initiated tiling. We must turn it off,
1387 // attach the new render buffer then turn tiling back on
1388 endTiling();
1389
Romain Guy8d4aeb72013-02-12 16:08:55 -08001390 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001391 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001392 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001393
Romain Guyf735c8e2013-01-31 17:45:55 -08001394 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001395 }
1396}
1397
Rob Tsuk487a92c2015-01-06 13:22:54 -08001398static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1399 float x, float y) {
1400 Vertex v;
1401 v.x = x;
1402 v.y = y;
1403 transform.mapPoint(v.x, v.y);
1404 rectangleVertices.push_back(v);
1405}
1406
1407static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1408 Vertex v;
1409 v.x = x;
1410 v.y = y;
1411 rectangleVertices.push_back(v);
1412}
1413
1414void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
1415 int count = rectangleList.getTransformedRectanglesCount();
1416 std::vector<Vertex> rectangleVertices(count * 4);
1417 Rect scissorBox = rectangleList.calculateBounds();
1418 scissorBox.snapToPixelBoundaries();
1419 for (int i = 0; i < count; ++i) {
1420 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1421 const Matrix4& transform = tr.getTransform();
1422 Rect bounds = tr.getBounds();
1423 if (transform.rectToRect()) {
1424 transform.mapRect(bounds);
1425 if (!bounds.intersect(scissorBox)) {
1426 bounds.setEmpty();
1427 } else {
1428 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1429 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1430 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1431 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1432 }
1433 } else {
1434 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1435 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1436 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1437 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1438 }
1439 }
1440
Chris Craik65fe5ee2015-01-26 18:06:29 -08001441 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001442 scissorBox.getWidth(), scissorBox.getHeight());
1443
1444 const SkPaint* paint = nullptr;
1445 setupDraw();
1446 setupDrawNoTexture();
1447 setupDrawColor(0, 0xff * currentSnapshot()->alpha);
1448 setupDrawShader(getShader(paint));
1449 setupDrawColorFilter(getColorFilter(paint));
1450 setupDrawBlending(paint);
1451 setupDrawProgram();
1452 setupDrawDirtyRegionsDisabled();
1453 setupDrawModelView(kModelViewMode_Translate, false,
1454 0.0f, 0.0f, 0.0f, 0.0f, true);
1455 setupDrawColorUniforms(getShader(paint));
1456 setupDrawShaderUniforms(getShader(paint));
1457 setupDrawColorFilterUniforms(getColorFilter(paint));
1458
1459 issueIndexedQuadDraw(&rectangleVertices[0], rectangleVertices.size() / 4);
1460}
1461
Romain Guy8ce00302013-01-15 18:51:42 -08001462void OpenGLRenderer::setStencilFromClip() {
1463 if (!mCaches.debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001464 if (!currentSnapshot()->clipIsSimple()) {
1465 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001466 EVENT_LOGD("setStencilFromClip - enabling");
1467
Romain Guy8ce00302013-01-15 18:51:42 -08001468 // NOTE: The order here is important, we must set dirtyClip to false
1469 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001470 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001471
1472 ensureStencilBuffer();
1473
Rob Tsuk487a92c2015-01-06 13:22:54 -08001474 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001475
Rob Tsuk487a92c2015-01-06 13:22:54 -08001476 bool isRectangleList = clipArea.isRectangleList();
1477 if (isRectangleList) {
1478 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1479 } else {
1480 incrementThreshold = 0;
1481 }
1482
Chris Craik96a5c4c2015-01-27 15:46:35 -08001483 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001484
1485 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001486 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001487 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001488 if (resetScissor) {
1489 // The scissor was not set so we now need to update it
1490 setScissorFromClip();
1491 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001492
Chris Craik96a5c4c2015-01-27 15:46:35 -08001493 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001494
1495 // stash and disable the outline clip state, since stencil doesn't account for outline
1496 bool storedSkipOutlineClip = mSkipOutlineClip;
1497 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001498
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001499 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001500 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001501 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1502
Rob Tsuk487a92c2015-01-06 13:22:54 -08001503 if (isRectangleList) {
1504 drawRectangleList(clipArea.getRectangleList());
1505 } else {
1506 // NOTE: We could use the region contour path to generate a smaller mesh
1507 // Since we are using the stencil we could use the red book path
1508 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001509
Rob Tsuk487a92c2015-01-06 13:22:54 -08001510 // The last parameter is important: we are not drawing in the color buffer
1511 // so we don't want to dirty the current layer, if any
1512 drawRegionRects(clipArea.getClipRegion(), paint, false);
1513 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001514 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001515 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001516
Chris Craik96a5c4c2015-01-27 15:46:35 -08001517 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001518
1519 // Draw the region used to generate the stencil if the appropriate debug
1520 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001521 // TODO: Implement for rectangle list clip areas
1522 if (mCaches.debugStencilClip == Caches::kStencilShowRegion &&
1523 !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001524 paint.setColor(0x7f0000ff);
1525 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001526 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001527 }
Romain Guy8ce00302013-01-15 18:51:42 -08001528 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001529 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001530 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001531 }
1532 }
1533}
1534
Chris Craikf0a59072013-11-19 18:00:46 -08001535/**
1536 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1537 *
1538 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1539 * style, and tessellated AA ramp
1540 */
1541bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001542 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001543 bool snapOut = paint && paint->isAntiAlias();
1544
1545 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1546 float outset = paint->getStrokeWidth() * 0.5f;
1547 left -= outset;
1548 top -= outset;
1549 right += outset;
1550 bottom += outset;
1551 }
1552
Chris Craikdeeda3d2014-05-05 19:09:33 -07001553 bool clipRequired = false;
1554 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001555 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001556 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001557 return true;
1558 }
1559
Chris Craikf23b25a2014-06-26 15:46:20 -07001560 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001561 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001562 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001563 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001564}
1565
Romain Guy8ce00302013-01-15 18:51:42 -08001566void OpenGLRenderer::debugClip() {
1567#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001568 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001569 SkPaint paint;
1570 paint.setColor(0x7f00ff00);
1571 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1572
Romain Guy8ce00302013-01-15 18:51:42 -08001573 }
1574#endif
1575}
1576
Romain Guyf6a11b82010-06-23 17:47:49 -07001577///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001578// Drawing commands
1579///////////////////////////////////////////////////////////////////////////////
1580
Chris Craik62d307c2014-07-29 10:35:13 -07001581void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001582 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001583 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001584 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001585 // Make sure setScissor & setStencil happen at the beginning of
1586 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001587 if (mState.getDirtyClip()) {
Chris Craik65fe5ee2015-01-26 18:06:29 -08001588 if (mRenderState.scissor().isEnabled()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001589 setScissorFromClip();
1590 }
Chris Craik62d307c2014-07-29 10:35:13 -07001591
Dohyun Leeadc0d9d2014-11-24 21:08:15 +09001592 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001593 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001594
Romain Guy70ca14e2010-12-13 18:24:33 -08001595 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001596
Romain Guy70ca14e2010-12-13 18:24:33 -08001597 mSetShaderColor = false;
1598 mColorSet = false;
1599 mColorA = mColorR = mColorG = mColorB = 0.0f;
1600 mTextureUnit = 0;
1601 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001602
1603 // Enable debug highlight when what we're about to draw is tested against
1604 // the stencil buffer and if stencil highlight debugging is on
1605 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1606 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
Chris Craik96a5c4c2015-01-27 15:46:35 -08001607 mRenderState.stencil().isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001608}
1609
1610void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1611 mDescription.hasTexture = true;
1612 mDescription.hasAlpha8Texture = isAlpha8;
1613}
1614
Romain Guyff316ec2013-02-13 18:39:43 -08001615void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1616 mDescription.hasTexture = true;
1617 mDescription.hasColors = true;
1618 mDescription.hasAlpha8Texture = isAlpha8;
1619}
1620
Romain Guyaa6c24c2011-04-28 18:40:04 -07001621void OpenGLRenderer::setupDrawWithExternalTexture() {
1622 mDescription.hasExternalTexture = true;
1623}
1624
Romain Guy15bc6432011-12-13 13:11:32 -08001625void OpenGLRenderer::setupDrawNoTexture() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001626 mRenderState.meshState().disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001627}
1628
Chris Craik91a8c7c2014-08-12 14:31:35 -07001629void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1630 mDescription.hasVertexAlpha = true;
1631 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001632}
1633
Romain Guy8d0d4782010-12-14 20:13:35 -08001634void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1635 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001636 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1637 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1638 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001639 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001640 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001641}
1642
Romain Guy86568192010-12-14 15:55:39 -08001643void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1644 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001645 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1646 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1647 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001648 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001649 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001650}
1651
Romain Guy41210632012-07-16 17:04:24 -07001652void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1653 mCaches.fontRenderer->describe(mDescription, paint);
1654}
1655
Romain Guy70ca14e2010-12-13 18:24:33 -08001656void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1657 mColorA = a;
1658 mColorR = r;
1659 mColorG = g;
1660 mColorB = b;
1661 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001662 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001663}
1664
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001665void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
Chris Craike84a2082014-12-22 14:28:49 -08001666 if (shader != nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001667 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001668 }
1669}
1670
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001671void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001672 if (filter == nullptr) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001673 return;
1674 }
1675
1676 SkXfermode::Mode mode;
Chris Craike84a2082014-12-22 14:28:49 -08001677 if (filter->asColorMode(nullptr, &mode)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001678 mDescription.colorOp = ProgramDescription::kColorBlend;
1679 mDescription.colorMode = mode;
Chris Craike84a2082014-12-22 14:28:49 -08001680 } else if (filter->asColorMatrix(nullptr)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001681 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001682 }
1683}
1684
Romain Guyf09ef512011-05-27 11:43:46 -07001685void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1686 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1687 mColorA = 1.0f;
1688 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001689 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001690 }
1691}
1692
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001693void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1694 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001695 // When the blending mode is kClear_Mode, we need to use a modulate color
1696 // argb=1,0,0,0
1697 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001698 // TODO: check shader blending, once we have shader drawing support for layers.
Tom Hudson8dfaa492014-12-09 15:03:44 -05001699 bool blend = layer->isBlend()
1700 || getLayerAlpha(layer) < 1.0f
1701 || (mColorSet && mColorA < 1.0f)
1702 || PaintUtils::isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001703 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001704}
1705
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001706void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1707 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001708 // When the blending mode is kClear_Mode, we need to use a modulate color
1709 // argb=1,0,0,0
1710 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001711 blend |= (mColorSet && mColorA < 1.0f) ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001712 (getShader(paint) && !getShader(paint)->isOpaque()) ||
Tom Hudson8dfaa492014-12-09 15:03:44 -05001713 PaintUtils::isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001714 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001715}
1716
1717void OpenGLRenderer::setupDrawProgram() {
1718 useProgram(mCaches.programCache.get(mDescription));
Chris Craikdeeda3d2014-05-05 19:09:33 -07001719 if (mDescription.hasRoundRectClip) {
1720 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001721 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001722 const Rect& innerRect = state->innerRect;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001723 glUniform4f(mCaches.currentProgram->getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001724 innerRect.left, innerRect.top,
1725 innerRect.right, innerRect.bottom);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001726 glUniformMatrix4fv(mCaches.currentProgram->getUniform("roundRectInvTransform"),
1727 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001728
1729 // add half pixel to round out integer rect space to cover pixel centers
1730 float roundedOutRadius = state->radius + 0.5f;
1731 glUniform1f(mCaches.currentProgram->getUniform("roundRectRadius"),
1732 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001733 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001734}
1735
1736void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1737 mTrackDirtyRegions = false;
1738}
1739
Chris Craik4063a0e2013-11-15 16:06:56 -08001740void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1741 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001742 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001743 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001744 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001745 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001746
Romain Guy86568192010-12-14 15:55:39 -08001747 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001748 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Chris Craike84a2082014-12-22 14:28:49 -08001749 mCaches.currentProgram->set(writableSnapshot()->getOrthoMatrix(),
1750 mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001751 if (dirty && mTrackDirtyRegions) {
1752 if (!ignoreTransform) {
1753 dirtyLayer(left, top, right, bottom, *currentTransform());
1754 } else {
1755 dirtyLayer(left, top, right, bottom);
1756 }
Romain Guy86568192010-12-14 15:55:39 -08001757 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001758}
1759
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001760void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1761 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001762 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1763 }
1764}
1765
Romain Guy86568192010-12-14 15:55:39 -08001766void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001767 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001768 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001769 }
1770}
1771
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001772void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
Chris Craike84a2082014-12-22 14:28:49 -08001773 if (shader == nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001774 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001775 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001776
1777 if (ignoreTransform) {
1778 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1779 // because it was built into modelView / the geometry, and the description needs to
1780 // compensate.
1781 mat4 modelViewWithoutTransform;
1782 modelViewWithoutTransform.loadInverse(*currentTransform());
1783 modelViewWithoutTransform.multiply(mModelViewMatrix);
1784 mModelViewMatrix.load(modelViewWithoutTransform);
1785 }
1786
1787 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001788}
1789
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001790void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001791 if (nullptr == filter) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001792 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001793 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001794
1795 SkColor color;
1796 SkXfermode::Mode mode;
1797 if (filter->asColorMode(&color, &mode)) {
1798 const int alpha = SkColorGetA(color);
1799 const GLfloat a = alpha / 255.0f;
1800 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1801 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1802 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1803 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1804 return;
1805 }
1806
1807 SkScalar srcColorMatrix[20];
1808 if (filter->asColorMatrix(srcColorMatrix)) {
1809
1810 float colorMatrix[16];
1811 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1812 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1813 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1814 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1815
1816 // Skia uses the range [0..255] for the addition vector, but we need
1817 // the [0..1] range to apply the vector in GLSL
1818 float colorVector[4];
1819 colorVector[0] = srcColorMatrix[4] / 255.0f;
1820 colorVector[1] = srcColorMatrix[9] / 255.0f;
1821 colorVector[2] = srcColorMatrix[14] / 255.0f;
1822 colorVector[3] = srcColorMatrix[19] / 255.0f;
1823
1824 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1825 GL_FALSE, colorMatrix);
1826 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1827 return;
1828 }
1829
1830 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001831}
1832
Romain Guy41210632012-07-16 17:04:24 -07001833void OpenGLRenderer::setupDrawTextGammaUniforms() {
1834 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1835}
1836
Romain Guy70ca14e2010-12-13 18:24:33 -08001837void OpenGLRenderer::setupDrawSimpleMesh() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001838 bool force = mRenderState.meshState().bindMeshBuffer();
1839 mRenderState.meshState().bindPositionVertexPointer(mCaches.currentProgram, force, nullptr);
1840 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001841}
1842
1843void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001844 if (texture) mCaches.textureState().bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001845 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001846 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001847}
1848
Romain Guyaa6c24c2011-04-28 18:40:04 -07001849void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001850 mCaches.textureState().bindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001851 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001852 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001853}
1854
Romain Guy8f0095c2011-05-02 17:24:22 -07001855void OpenGLRenderer::setupDrawTextureTransform() {
1856 mDescription.hasTextureTransform = true;
1857}
1858
1859void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001860 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1861 GL_FALSE, &transform.data[0]);
1862}
1863
Chris Craik564acf72014-01-02 16:46:18 -08001864void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1865 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001866 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001867 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001868 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001869 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001870 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001871 }
Romain Guyd71dd362011-12-12 19:03:35 -08001872
Chris Craik96a5c4c2015-01-27 15:46:35 -08001873 mRenderState.meshState().bindPositionVertexPointer(mCaches.currentProgram, force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001874 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001875 mRenderState.meshState().bindTexCoordsVertexPointer(mCaches.currentProgram, force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001876 }
1877
Chris Craik96a5c4c2015-01-27 15:46:35 -08001878 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -08001879}
1880
Chris Craik564acf72014-01-02 16:46:18 -08001881void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1882 const GLvoid* texCoords, const GLvoid* colors) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001883 bool force = mRenderState.meshState().unbindMeshBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001884 GLsizei stride = sizeof(ColorTextureVertex);
1885
Chris Craik96a5c4c2015-01-27 15:46:35 -08001886 mRenderState.meshState().bindPositionVertexPointer(mCaches.currentProgram, force,
1887 vertices, stride);
Romain Guyff316ec2013-02-13 18:39:43 -08001888 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001889 mRenderState.meshState().bindTexCoordsVertexPointer(mCaches.currentProgram, force,
1890 texCoords, stride);
Romain Guyff316ec2013-02-13 18:39:43 -08001891 }
1892 int slot = mCaches.currentProgram->getAttrib("colors");
1893 if (slot >= 0) {
1894 glEnableVertexAttribArray(slot);
1895 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1896 }
1897
Chris Craik96a5c4c2015-01-27 15:46:35 -08001898 mRenderState.meshState().unbindIndicesBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001899}
1900
Chris Craik564acf72014-01-02 16:46:18 -08001901void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1902 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001903 bool force = false;
1904 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1905 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
Chris Craik96a5c4c2015-01-27 15:46:35 -08001906 // use the default VBO found in RenderState
Romain Guy3b748a42013-04-17 18:54:38 -07001907 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001908 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy3b748a42013-04-17 18:54:38 -07001909 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001910 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001911 }
Chris Craik96a5c4c2015-01-27 15:46:35 -08001912 mRenderState.meshState().bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001913
Chris Craik96a5c4c2015-01-27 15:46:35 -08001914 mRenderState.meshState().bindPositionVertexPointer(mCaches.currentProgram, force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001915 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001916 mRenderState.meshState().bindTexCoordsVertexPointer(mCaches.currentProgram,
1917 force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001918 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001919}
1920
Romain Guy448455f2013-07-22 13:57:50 -07001921void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001922 bool force = mRenderState.meshState().unbindMeshBuffer();
1923 mRenderState.meshState().bindQuadIndicesBuffer();
1924 mRenderState.meshState().bindPositionVertexPointer(mCaches.currentProgram, force,
1925 vertices, kVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001926}
1927
Romain Guy70ca14e2010-12-13 18:24:33 -08001928///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001929// Drawing
1930///////////////////////////////////////////////////////////////////////////////
1931
Tom Hudson107843d2014-09-08 11:26:26 -04001932void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001933 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1934 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001935 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001936 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001937 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001938 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001939 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001940 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001941 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001942 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001943 }
1944
Chris Craikef8d6f22014-12-17 11:10:28 -08001945 // Don't avoid overdraw when visualizing, since that makes it harder to
1946 // debug where it's coming from, and when the problem occurs.
1947 bool avoidOverdraw = !mCaches.debugOverdraw;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001948 DeferredDisplayList deferredList(mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001949 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001950 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001951
1952 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001953 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001954
Tom Hudson107843d2014-09-08 11:26:26 -04001955 deferredList.flush(*this, dirty);
1956 } else {
1957 // Even if there is no drawing command(Ex: invisible),
1958 // it still needs startFrame to clear buffer and start tiling.
1959 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001960 }
1961}
1962
Chris Craike84a2082014-12-22 14:28:49 -08001963void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top,
1964 const SkPaint* paint) {
Romain Guya168d732011-03-18 16:50:13 -07001965 float x = left;
1966 float y = top;
1967
Romain Guy886b2752013-01-04 12:26:18 -08001968 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1969
Romain Guya168d732011-03-18 16:50:13 -07001970 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001971 if (currentTransform()->isPureTranslate()) {
1972 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1973 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001974 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001975
1976 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001977 } else {
Chris Craik678625242014-02-28 12:26:34 -08001978 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001979 }
1980
Romain Guy3b748a42013-04-17 18:54:38 -07001981 // No need to check for a UV mapper on the texture object, only ARGB_8888
1982 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001983 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Chris Craik96a5c4c2015-01-27 15:46:35 -08001984 paint, (GLvoid*) nullptr, (GLvoid*) kMeshTextureOffset,
1985 GL_TRIANGLE_STRIP, kMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001986}
1987
Romain Guy03c00b52013-06-20 18:30:28 -07001988/**
1989 * Important note: this method is intended to draw batches of bitmaps and
1990 * will not set the scissor enable or dirty the current layer, if any.
1991 * The caller is responsible for properly dirtying the current layer.
1992 */
Tom Hudson107843d2014-09-08 11:26:26 -04001993void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001994 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1995 const Rect& bounds, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08001996 mCaches.textureState().activateTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001997 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001998 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001999
Chris Craik527a3aa2013-03-04 10:19:31 -08002000 const AutoTexture autoCleanup(texture);
2001
Chris Craik527a3aa2013-03-04 10:19:31 -08002002 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002003 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002004
2005 const float x = (int) floorf(bounds.left + 0.5f);
2006 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04002007 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002008 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002009 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002010 GL_TRIANGLES, bitmapCount * 6, true,
2011 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002012 } else {
2013 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002014 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002015 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2016 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002017 }
2018
Tom Hudson107843d2014-09-08 11:26:26 -04002019 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08002020}
2021
Tom Hudson107843d2014-09-08 11:26:26 -04002022void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002023 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002024 return;
Romain Guy6926c722010-07-12 20:20:03 -07002025 }
2026
Chris Craik44eb2c02015-01-29 09:45:09 -08002027 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002028 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002029 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002030 const AutoTexture autoCleanup(texture);
2031
Mike Reed1103b322014-07-08 12:36:44 -04002032 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002033 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002034 } else {
Chris Craik79647502014-08-06 13:42:24 -07002035 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002036 }
Chet Haase48659092012-05-31 15:21:51 -07002037
Tom Hudson107843d2014-09-08 11:26:26 -04002038 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002039}
2040
Tom Hudson107843d2014-09-08 11:26:26 -04002041void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002042 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002043 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002044 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002045 }
2046
Chris Craik39a908c2013-06-13 14:39:01 -07002047 // TODO: use quickReject on bounds from vertices
Chris Craik65fe5ee2015-01-26 18:06:29 -08002048 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002049
Romain Guyb18d2d02011-02-10 15:52:54 -08002050 float left = FLT_MAX;
2051 float top = FLT_MAX;
2052 float right = FLT_MIN;
2053 float bottom = FLT_MIN;
2054
Romain Guya92bb4d2012-10-16 11:08:44 -07002055 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002056
Chris Craik51d6a3d2014-12-22 17:16:56 -08002057 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[count]);
2058 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08002059
Chris Craik51d6a3d2014-12-22 17:16:56 -08002060 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002061 if (!colors) {
2062 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08002063 tempColors.reset(new int[colorsCount]);
2064 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
2065 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08002066 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002067
Chris Craik44eb2c02015-01-29 09:45:09 -08002068 mCaches.textureState().activateTexture(0);
John Reckebd52612014-12-10 16:47:36 -08002069 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002070 const UvMapper& mapper(getMapper(texture));
2071
Romain Guy5a7b4662011-01-20 19:09:30 -08002072 for (int32_t y = 0; y < meshHeight; y++) {
2073 for (int32_t x = 0; x < meshWidth; x++) {
2074 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2075
2076 float u1 = float(x) / meshWidth;
2077 float u2 = float(x + 1) / meshWidth;
2078 float v1 = float(y) / meshHeight;
2079 float v2 = float(y + 1) / meshHeight;
2080
Romain Guy3b748a42013-04-17 18:54:38 -07002081 mapper.map(u1, v1, u2, v2);
2082
Romain Guy5a7b4662011-01-20 19:09:30 -08002083 int ax = i + (meshWidth + 1) * 2;
2084 int ay = ax + 1;
2085 int bx = i;
2086 int by = bx + 1;
2087 int cx = i + 2;
2088 int cy = cx + 1;
2089 int dx = i + (meshWidth + 1) * 2 + 2;
2090 int dy = dx + 1;
2091
Romain Guyff316ec2013-02-13 18:39:43 -08002092 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2093 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2094 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002095
Romain Guyff316ec2013-02-13 18:39:43 -08002096 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2097 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2098 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002099
Romain Guya92bb4d2012-10-16 11:08:44 -07002100 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2101 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2102 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2103 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002104 }
2105 }
2106
Chris Craikf0a59072013-11-19 18:00:46 -08002107 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002108 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002109 }
2110
Romain Guyff316ec2013-02-13 18:39:43 -08002111 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002112 texture = mCaches.textureCache.get(bitmap);
2113 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04002114 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002115 }
Romain Guyff316ec2013-02-13 18:39:43 -08002116 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002117 const AutoTexture autoCleanup(texture);
2118
2119 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002120 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002121
2122 int alpha;
2123 SkXfermode::Mode mode;
2124 getAlphaAndMode(paint, &alpha, &mode);
2125
Romain Guyff316ec2013-02-13 18:39:43 -08002126 float a = alpha / 255.0f;
2127
Romain Guya92bb4d2012-10-16 11:08:44 -07002128 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002129 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002130 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002131
Romain Guyff316ec2013-02-13 18:39:43 -08002132 setupDraw();
2133 setupDrawWithTextureAndColor();
2134 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002135 setupDrawColorFilter(getColorFilter(paint));
2136 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002137 setupDrawProgram();
2138 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002139 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002140 setupDrawTexture(texture->id);
2141 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002142 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002143 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002144
2145 glDrawArrays(GL_TRIANGLES, 0, count);
2146
Romain Guyff316ec2013-02-13 18:39:43 -08002147 int slot = mCaches.currentProgram->getAttrib("colors");
2148 if (slot >= 0) {
2149 glDisableVertexAttribArray(slot);
2150 }
2151
Tom Hudson107843d2014-09-08 11:26:26 -04002152 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002153}
2154
Tom Hudson107843d2014-09-08 11:26:26 -04002155void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002156 float srcLeft, float srcTop, float srcRight, float srcBottom,
2157 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002158 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002159 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002160 return;
Romain Guy6926c722010-07-12 20:20:03 -07002161 }
2162
Chris Craik44eb2c02015-01-29 09:45:09 -08002163 mCaches.textureState().activateTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002164 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002165 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002166 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002167
Romain Guy8ba548f2010-06-30 19:21:21 -07002168 const float width = texture->width;
2169 const float height = texture->height;
2170
Romain Guy3b748a42013-04-17 18:54:38 -07002171 float u1 = fmax(0.0f, srcLeft / width);
2172 float v1 = fmax(0.0f, srcTop / height);
2173 float u2 = fmin(1.0f, srcRight / width);
2174 float v2 = fmin(1.0f, srcBottom / height);
2175
2176 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002177
Chris Craik96a5c4c2015-01-27 15:46:35 -08002178 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002179 resetDrawTextureTexCoords(u1, v1, u2, v2);
2180
Romain Guyd21b6e12011-11-30 20:21:23 -08002181 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2182
Romain Guy886b2752013-01-04 12:26:18 -08002183 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2184 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002185
Romain Guy886b2752013-01-04 12:26:18 -08002186 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2187 // Apply a scale transform on the canvas only when a shader is in use
2188 // Skia handles the ratio between the dst and src rects as a scale factor
2189 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002190 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002191 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002192
Chris Craikd6b65f62014-01-01 14:45:21 -08002193 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2194 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2195 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002196
2197 dstRight = x + (dstRight - dstLeft);
2198 dstBottom = y + (dstBottom - dstTop);
2199
2200 dstLeft = x;
2201 dstTop = y;
2202
Chris Craik678625242014-02-28 12:26:34 -08002203 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002204 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002205 } else {
Chris Craik678625242014-02-28 12:26:34 -08002206 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002207 }
2208
2209 if (CC_UNLIKELY(useScaleTransform)) {
2210 save(SkCanvas::kMatrix_SaveFlag);
2211 translate(dstLeft, dstTop);
2212 scale(scaleX, scaleY);
2213
2214 dstLeft = 0.0f;
2215 dstTop = 0.0f;
2216
2217 dstRight = srcRight - srcLeft;
2218 dstBottom = srcBottom - srcTop;
2219 }
2220
Mike Reed1103b322014-07-08 12:36:44 -04002221 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002222 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002223 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002224 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik96a5c4c2015-01-27 15:46:35 -08002225 GL_TRIANGLE_STRIP, kMeshCount, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002226 } else {
2227 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002228 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002229 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik96a5c4c2015-01-27 15:46:35 -08002230 GL_TRIANGLE_STRIP, kMeshCount, false, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002231 }
2232
2233 if (CC_UNLIKELY(useScaleTransform)) {
2234 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002235 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002236
2237 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002238
Tom Hudson107843d2014-09-08 11:26:26 -04002239 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002240}
2241
Tom Hudson107843d2014-09-08 11:26:26 -04002242void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002243 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002244 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002245 return;
Romain Guy6926c722010-07-12 20:20:03 -07002246 }
2247
John Reckebd52612014-12-10 16:47:36 -08002248 AssetAtlas::Entry* entry = mRenderState.assetAtlas().getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002249 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2250 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002251
Tom Hudson107843d2014-09-08 11:26:26 -04002252 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002253}
2254
Tom Hudson107843d2014-09-08 11:26:26 -04002255void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002256 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2257 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002258 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002259 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002260 }
2261
Romain Guy211370f2012-02-01 16:10:55 -08002262 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002263 mCaches.textureState().activateTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002264 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002265 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002266 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002267
Romain Guya4adcf02013-02-28 12:15:35 -08002268 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2269 texture->setFilter(GL_LINEAR, true);
2270
Chris Craikd6b65f62014-01-01 14:45:21 -08002271 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002272 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002273 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002274 const float offsetX = left + currentTransform()->getTranslateX();
2275 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002276 const size_t count = mesh->quads.size();
2277 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002278 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002279 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002280 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2281 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2282 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002283 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002284 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002285 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002286 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002287 }
2288 }
2289
Chris Craik4063a0e2013-11-15 16:06:56 -08002290 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002291 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002292 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2293 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002294
Romain Guy3b748a42013-04-17 18:54:38 -07002295 right = x + right - left;
2296 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002297 left = x;
2298 top = y;
2299 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002300 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002301 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2302 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002303 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2304 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002305 }
Chet Haase48659092012-05-31 15:21:51 -07002306
Tom Hudson107843d2014-09-08 11:26:26 -04002307 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002308}
2309
Romain Guy03c00b52013-06-20 18:30:28 -07002310/**
2311 * Important note: this method is intended to draw batches of 9-patch objects and
2312 * will not set the scissor enable or dirty the current layer, if any.
2313 * The caller is responsible for properly dirtying the current layer.
2314 */
Tom Hudson107843d2014-09-08 11:26:26 -04002315void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002316 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002317 mCaches.textureState().activateTexture(0);
Romain Guy03c00b52013-06-20 18:30:28 -07002318 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002319 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002320 const AutoTexture autoCleanup(texture);
2321
2322 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2323 texture->setFilter(GL_LINEAR, true);
2324
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002325 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2326 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002327 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002328
Tom Hudson107843d2014-09-08 11:26:26 -04002329 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002330}
2331
Tom Hudson107843d2014-09-08 11:26:26 -04002332void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002333 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002334 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002335 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002336 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002337 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002338 }
2339
Chris Craik0d5ac952014-07-15 13:01:02 -07002340 Rect bounds(vertexBuffer.getBounds());
2341 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002342 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2343
Chris Craikcb4d6002012-09-25 12:00:29 -07002344 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002345 bool isAA = paint->isAntiAlias();
2346
Chris Craik710f46d2012-09-17 17:25:49 -07002347 setupDraw();
2348 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002349 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Tom Hudson984162f2014-10-10 13:38:16 -04002350 setupDrawColor(color, ((color >> 24) & 0xFF) * writableSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002351 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002352 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002353 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002354 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002355 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2356 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002357 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002358 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002359 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002360
ztenghui55bfb4e2013-12-03 10:38:55 -08002361 const void* vertices = vertexBuffer.getBuffer();
Chris Craik96a5c4c2015-01-27 15:46:35 -08002362 mRenderState.meshState().unbindMeshBuffer();
2363 mRenderState.meshState().bindPositionVertexPointer(mCaches.currentProgram,
2364 true, vertices, isAA ? kAlphaVertexStride : kVertexStride);
2365 mRenderState.meshState().resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002366
Chris Craik710f46d2012-09-17 17:25:49 -07002367 int alphaSlot = -1;
2368 if (isAA) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002369 void* alphaCoords = ((GLbyte*) vertices) + kVertexAlphaOffset;
Chris Craik710f46d2012-09-17 17:25:49 -07002370 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002371 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002372 glEnableVertexAttribArray(alphaSlot);
Chris Craik96a5c4c2015-01-27 15:46:35 -08002373 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, kAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002374 }
Romain Guy04299382012-07-18 17:15:41 -07002375
Chris Craik05f3d6e2014-06-02 16:27:04 -07002376 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2377 if (mode == VertexBuffer::kStandard) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002378 mRenderState.meshState().unbindIndicesBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08002379 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002380 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002381 mRenderState.meshState().bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002382 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT,
2383 GL_UNSIGNED_SHORT, nullptr);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002384 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002385 mRenderState.meshState().bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002386 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT,
2387 GL_UNSIGNED_SHORT, nullptr);
ztenghuid5e8ade2014-08-13 15:48:02 -07002388 } else if (mode == VertexBuffer::kIndices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002389 mRenderState.meshState().unbindIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002390 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(),
2391 GL_UNSIGNED_SHORT, vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002392 }
Romain Guy04299382012-07-18 17:15:41 -07002393
Chris Craik710f46d2012-09-17 17:25:49 -07002394 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002395 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002396 }
Chris Craik65cd6122012-12-10 17:56:27 -08002397
Tom Hudson107843d2014-09-08 11:26:26 -04002398 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002399}
2400
2401/**
Chris Craik65cd6122012-12-10 17:56:27 -08002402 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2403 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2404 * screen space in all directions. However, instead of using a fragment shader to compute the
2405 * translucency of the color from its position, we simply use a varying parameter to define how far
2406 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2407 *
2408 * Doesn't yet support joins, caps, or path effects.
2409 */
Tom Hudson107843d2014-09-08 11:26:26 -04002410void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002411 VertexBuffer vertexBuffer;
2412 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002413 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002414 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002415}
2416
2417/**
2418 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2419 * and additional geometry for defining an alpha slope perimeter.
2420 *
2421 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2422 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2423 * in-shader alpha region, but found it to be taxing on some GPUs.
2424 *
2425 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2426 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002427 */
Tom Hudson107843d2014-09-08 11:26:26 -04002428void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002429 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002430
Chris Craik65cd6122012-12-10 17:56:27 -08002431 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002432
Chris Craik65cd6122012-12-10 17:56:27 -08002433 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002434 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2435 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002436
Chris Craik05f3d6e2014-06-02 16:27:04 -07002437 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002438 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002439 }
2440
Chris Craikbf759452014-08-11 16:00:44 -07002441 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002442 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002443}
2444
Tom Hudson107843d2014-09-08 11:26:26 -04002445void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002446 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002447
Chris Craik6d29c8d2013-05-08 18:35:44 -07002448 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002449
Chris Craik6d29c8d2013-05-08 18:35:44 -07002450 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002451 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002452
Chris Craik05f3d6e2014-06-02 16:27:04 -07002453 const Rect& bounds = buffer.getBounds();
2454 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002455 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002456 }
2457
Chris Craikbf759452014-08-11 16:00:44 -07002458 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002459 drawVertexBuffer(buffer, paint, displayFlags);
2460
2461 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002462}
2463
Tom Hudson107843d2014-09-08 11:26:26 -04002464void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002465 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002466 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002467
Rob Tsuk487a92c2015-01-06 13:22:54 -08002468 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002469 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002470
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002471 SkPaint paint;
2472 paint.setColor(color);
2473 paint.setXfermodeMode(mode);
2474
2475 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002476
Tom Hudson107843d2014-09-08 11:26:26 -04002477 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002478}
Romain Guy9d5316e2010-06-24 19:30:36 -07002479
Tom Hudson107843d2014-09-08 11:26:26 -04002480void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002481 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002482 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002483 const AutoTexture autoCleanup(texture);
2484
2485 const float x = left + texture->left - texture->offset;
2486 const float y = top + texture->top - texture->offset;
2487
2488 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002489
Tom Hudson107843d2014-09-08 11:26:26 -04002490 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002491}
2492
Tom Hudson107843d2014-09-08 11:26:26 -04002493void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002494 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002495 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002496 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002497 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002498 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002499 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002500
Chris Craike84a2082014-12-22 14:28:49 -08002501 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002502 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002503 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002504 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002505 drawShape(left, top, texture, p);
2506 } else {
2507 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2508 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2509 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002510 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002511}
2512
Tom Hudson107843d2014-09-08 11:26:26 -04002513void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002514 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002515 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002516 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002517 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002518 }
Chris Craike84a2082014-12-22 14:28:49 -08002519 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002520 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002521 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002522 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002523 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002524 SkPath path;
2525 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2526 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2527 } else {
2528 path.addCircle(x, y, radius);
2529 }
2530 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002531 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002532}
Romain Guy01d58e42011-01-19 21:54:02 -08002533
Tom Hudson107843d2014-09-08 11:26:26 -04002534void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002535 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002536 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002537 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002538 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002539 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002540 }
Romain Guy01d58e42011-01-19 21:54:02 -08002541
Chris Craike84a2082014-12-22 14:28:49 -08002542 if (p->getPathEffect() != nullptr) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002543 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002544 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002545 drawShape(left, top, texture, p);
2546 } else {
2547 SkPath path;
2548 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2549 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2550 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2551 }
2552 path.addOval(rect);
2553 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002554 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002555}
2556
Tom Hudson107843d2014-09-08 11:26:26 -04002557void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002558 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002559 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002560 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002561 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002562 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002563 }
2564
Chris Craik780c1282012-10-04 14:10:49 -07002565 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08002566 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002567 mCaches.textureState().activateTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002568 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002569 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002570 drawShape(left, top, texture, p);
2571 return;
Chris Craik780c1282012-10-04 14:10:49 -07002572 }
Chris Craik780c1282012-10-04 14:10:49 -07002573 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2574 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2575 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2576 }
2577
2578 SkPath path;
2579 if (useCenter) {
2580 path.moveTo(rect.centerX(), rect.centerY());
2581 }
2582 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2583 if (useCenter) {
2584 path.close();
2585 }
Tom Hudson107843d2014-09-08 11:26:26 -04002586 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002587}
2588
Romain Guycf8675e2012-10-02 12:32:25 -07002589// See SkPaintDefaults.h
2590#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2591
Tom Hudson107843d2014-09-08 11:26:26 -04002592void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002593 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002594 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002595 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002596 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002597 return;
Romain Guy6926c722010-07-12 20:20:03 -07002598 }
2599
Chris Craik710f46d2012-09-17 17:25:49 -07002600 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002601 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08002602 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07002603 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002604 mCaches.textureState().activateTexture(0);
Romain Guycf8675e2012-10-02 12:32:25 -07002605 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002606 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002607 drawShape(left, top, texture, p);
2608 } else {
2609 SkPath path;
2610 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2611 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2612 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2613 }
2614 path.addRect(rect);
2615 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002616 }
Chet Haase858aa932011-05-12 09:06:00 -07002617 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002618 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2619 SkPath path;
2620 path.addRect(left, top, right, bottom);
2621 drawConvexPath(path, p);
2622 } else {
2623 drawColorRect(left, top, right, bottom, p);
2624
2625 mDirty = true;
2626 }
Chet Haase858aa932011-05-12 09:06:00 -07002627 }
Romain Guyc7d53492010-06-25 13:41:57 -07002628}
Romain Guy9d5316e2010-06-24 19:30:36 -07002629
Chris Craikd218a922014-01-02 17:13:34 -08002630void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2631 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002632 FontRenderer& fontRenderer, int alpha, float x, float y) {
Chris Craik44eb2c02015-01-29 09:45:09 -08002633 mCaches.textureState().activateTexture(0);
Raph Levien416a8472012-07-19 22:48:17 -07002634
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002635 TextShadow textShadow;
2636 if (!getTextShadow(paint, &textShadow)) {
2637 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2638 }
2639
Raph Levien416a8472012-07-19 22:48:17 -07002640 // NOTE: The drop shadow will not perform gamma correction
2641 // if shader-based correction is enabled
2642 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2643 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002644 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002645 // If the drop shadow exceeds the max texture size or couldn't be
2646 // allocated, skip drawing
2647 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002648 const AutoTexture autoCleanup(shadow);
2649
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002650 const float sx = x - shadow->left + textShadow.dx;
2651 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002652
Tom Hudson984162f2014-10-10 13:38:16 -04002653 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002654 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002655 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002656 }
2657
2658 setupDraw();
2659 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002660 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002661 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002662 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002663 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002664 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002665 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2666 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002667 setupDrawTexture(shadow->id);
2668 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002669 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002670 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08002671 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Raph Levien416a8472012-07-19 22:48:17 -07002672
Chris Craik96a5c4c2015-01-27 15:46:35 -08002673 glDrawArrays(GL_TRIANGLE_STRIP, 0, kMeshCount);
Raph Levien416a8472012-07-19 22:48:17 -07002674}
2675
Romain Guy768bffc2013-02-27 13:50:45 -08002676bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002677 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05002678 return MathUtils::isZero(alpha)
2679 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002680}
2681
Tom Hudson107843d2014-09-08 11:26:26 -04002682void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002683 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002684 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002685 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002686 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002687
Romain Guy671d6cf2012-01-18 12:39:17 -08002688 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002689 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002690 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002691 }
2692
Chris Craik65fe5ee2015-01-26 18:06:29 -08002693 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002694
Romain Guy671d6cf2012-01-18 12:39:17 -08002695 float x = 0.0f;
2696 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002697 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002698 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002699 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2700 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002701 }
2702
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002703 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002704 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002705
2706 int alpha;
2707 SkXfermode::Mode mode;
2708 getAlphaAndMode(paint, &alpha, &mode);
2709
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002710 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002711 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002712 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002713 }
2714
Romain Guy671d6cf2012-01-18 12:39:17 -08002715 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002716 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002717 if (pureTranslate && !linearFilter) {
2718 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2719 }
Romain Guy257ae352013-03-20 16:31:12 -07002720 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002721
Rob Tsuk487a92c2015-01-06 13:22:54 -08002722 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002723 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2724
Romain Guy211370f2012-02-01 16:10:55 -08002725 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002726
Victoria Lease1e546812013-06-25 14:25:17 -07002727 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002728 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002729 positions, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002730 if (hasActiveLayer) {
2731 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002732 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002733 }
2734 dirtyLayerUnchecked(bounds, getRegion());
2735 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002736 }
Chet Haase48659092012-05-31 15:21:51 -07002737
Tom Hudson107843d2014-09-08 11:26:26 -04002738 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002739}
2740
Chris Craik59744b72014-07-01 17:56:52 -07002741bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002742 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002743 outMatrix->setIdentity();
2744 return false;
2745 } else if (CC_UNLIKELY(transform.isPerspective())) {
2746 outMatrix->setIdentity();
2747 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002748 }
Chris Craik59744b72014-07-01 17:56:52 -07002749
2750 /**
Chris Craika736cd92014-08-04 13:18:38 -07002751 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2752 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002753 */
2754 float sx, sy;
2755 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002756 outMatrix->setScale(
2757 roundf(fmaxf(1.0f, sx)),
2758 roundf(fmaxf(1.0f, sy)));
2759 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002760}
2761
Tom Hudson984162f2014-10-10 13:38:16 -04002762int OpenGLRenderer::getSaveCount() const {
2763 return mState.getSaveCount();
2764}
2765
2766int OpenGLRenderer::save(int flags) {
2767 return mState.save(flags);
2768}
2769
2770void OpenGLRenderer::restore() {
2771 return mState.restore();
2772}
2773
2774void OpenGLRenderer::restoreToCount(int saveCount) {
2775 return mState.restoreToCount(saveCount);
2776}
2777
2778void OpenGLRenderer::translate(float dx, float dy, float dz) {
2779 return mState.translate(dx, dy, dz);
2780}
2781
2782void OpenGLRenderer::rotate(float degrees) {
2783 return mState.rotate(degrees);
2784}
2785
2786void OpenGLRenderer::scale(float sx, float sy) {
2787 return mState.scale(sx, sy);
2788}
2789
2790void OpenGLRenderer::skew(float sx, float sy) {
2791 return mState.skew(sx, sy);
2792}
2793
2794void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2795 mState.setMatrix(matrix);
2796}
2797
2798void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2799 mState.concatMatrix(matrix);
2800}
2801
2802bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2803 return mState.clipRect(left, top, right, bottom, op);
2804}
2805
2806bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2807 return mState.clipPath(path, op);
2808}
2809
2810bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2811 return mState.clipRegion(region, op);
2812}
2813
2814void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2815 mState.setClippingOutline(allocator, outline);
2816}
2817
2818void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2819 const Rect& rect, float radius, bool highPriority) {
2820 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2821}
2822
Tom Hudson107843d2014-09-08 11:26:26 -04002823void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002824 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002825 DrawOpMode drawOpMode) {
2826
Chris Craik527a3aa2013-03-04 10:19:31 -08002827 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002828 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2829 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002830 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002831 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002832 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002833 }
Romain Guycac5fd32011-12-01 20:08:50 -08002834 }
2835
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002836 const float oldX = x;
2837 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002838
Chris Craikd6b65f62014-01-01 14:45:21 -08002839 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002840 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002841
Romain Guy211370f2012-02-01 16:10:55 -08002842 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002843 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2844 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002845 }
2846
Romain Guy86568192010-12-14 15:55:39 -08002847 int alpha;
2848 SkXfermode::Mode mode;
2849 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002850
Romain Guyc74f45a2013-02-26 19:10:14 -08002851 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2852
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002853 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002854 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002855 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002856 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002857 }
2858
Romain Guy19d4dd82013-03-04 11:14:26 -08002859 const bool hasActiveLayer = hasLayer();
2860
Romain Guy624234f2013-03-05 16:43:31 -08002861 // We only pass a partial transform to the font renderer. That partial
2862 // matrix defines how glyphs are rasterized. Typically we want glyphs
2863 // to be rasterized at their final size on screen, which means the partial
2864 // matrix needs to take the scale factor into account.
2865 // When a partial matrix is used to transform glyphs during rasterization,
2866 // the mesh is generated with the inverse transform (in the case of scale,
2867 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2868 // apply the full transform matrix at draw time in the vertex shader.
2869 // Applying the full matrix in the shader is the easiest way to handle
2870 // rotation and perspective and allows us to always generated quads in the
2871 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002872 SkMatrix fontTransform;
2873 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2874 || fabs(y - (int) y) > 0.0f
2875 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002876 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002877 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002878
Romain Guy624234f2013-03-05 16:43:31 -08002879 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08002880 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002881 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 -07002882
Raph Levien996e57c2012-07-23 15:22:52 -07002883 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002884 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002885
2886 // don't call issuedrawcommand, do it at end of batch
2887 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002888 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002889 SkPaint paintCopy(*paint);
2890 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2891 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002892 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002893 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002894 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002895 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002896 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002897
Chris Craik527a3aa2013-03-04 10:19:31 -08002898 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002899 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002900 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002901 }
Chris Craik41541822013-05-03 16:35:54 -07002902 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002903 }
Romain Guy694b5192010-07-21 21:33:20 -07002904
Chris Craike63f7c622013-10-17 10:30:55 -07002905 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002906
Tom Hudson107843d2014-09-08 11:26:26 -04002907 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002908}
2909
Tom Hudson107843d2014-09-08 11:26:26 -04002910void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002911 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002912 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002913 return;
Romain Guy03d58522012-02-24 17:54:07 -08002914 }
2915
Chris Craik39a908c2013-06-13 14:39:01 -07002916 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002917 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002918
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002919 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002920 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002921 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002922
2923 int alpha;
2924 SkXfermode::Mode mode;
2925 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002926 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002927
Tom Hudson984162f2014-10-10 13:38:16 -04002928 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002929 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 -08002930
Romain Guy97771732012-02-28 18:17:02 -08002931 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002932
2933 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chris Craike84a2082014-12-22 14:28:49 -08002934 hOffset, vOffset, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002935 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002936 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002937 dirtyLayerUnchecked(bounds, getRegion());
2938 }
Romain Guy97771732012-02-28 18:17:02 -08002939 }
Chet Haase48659092012-05-31 15:21:51 -07002940
Tom Hudson107843d2014-09-08 11:26:26 -04002941 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002942}
2943
Tom Hudson107843d2014-09-08 11:26:26 -04002944void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002945 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002946
Chris Craik44eb2c02015-01-29 09:45:09 -08002947 mCaches.textureState().activateTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002948
Romain Guyfb8b7632010-08-23 21:05:08 -07002949 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002950 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002951 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002952
Romain Guy8b55f372010-08-18 17:10:07 -07002953 const float x = texture->left - texture->offset;
2954 const float y = texture->top - texture->offset;
2955
Romain Guy01d58e42011-01-19 21:54:02 -08002956 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002957 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002958}
2959
Tom Hudson107843d2014-09-08 11:26:26 -04002960void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002961 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002962 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002963 }
2964
Chris Craike84a2082014-12-22 14:28:49 -08002965 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002966 if (layer->isTextureLayer()) {
2967 transform = &layer->getTransform();
2968 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002969 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002970 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002971 }
2972 }
2973
Chris Craik39a908c2013-06-13 14:39:01 -07002974 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08002975 const bool rejected = mState.calculateQuickRejectForScissor(
2976 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2977 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002978
2979 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002980 if (transform && !transform->isIdentity()) {
2981 restore();
2982 }
Tom Hudson107843d2014-09-08 11:26:26 -04002983 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002984 }
2985
Chris Craik62d307c2014-07-29 10:35:13 -07002986 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2987 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2988
Romain Guy5bb3c732012-11-29 17:52:58 -08002989 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002990
Chris Craik65fe5ee2015-01-26 18:06:29 -08002991 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik44eb2c02015-01-29 09:45:09 -08002992 mCaches.textureState().activateTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002993
Romain Guy211370f2012-02-01 16:10:55 -08002994 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002995 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002996 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2997 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002998 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002999
Chris Craik16ecda52013-03-29 10:59:59 -07003000 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003001 setupDraw();
3002 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003003 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003004 setupDrawColorFilter(layer->getColorFilter());
3005 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003006 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003007 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003008 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07003009 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08003010 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3011 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3012 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003013
Romain Guyd21b6e12011-11-30 20:21:23 -08003014 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003015 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003016 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003017 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003018 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003019 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003020 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3021 }
Romain Guyf219da52011-01-16 12:54:25 -08003022
Romain Guy448455f2013-07-22 13:57:50 -07003023 TextureVertex* mesh = &layer->mesh[0];
3024 GLsizei elementsCount = layer->meshElementCount;
3025
3026 while (elementsCount > 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08003027 GLsizei drawCount = min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Romain Guy448455f2013-07-22 13:57:50 -07003028
Romain Guy3380cfd2013-08-15 16:57:57 -07003029 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003030 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
Chris Craike84a2082014-12-22 14:28:49 -08003031 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr));
Romain Guy448455f2013-07-22 13:57:50 -07003032
3033 elementsCount -= drawCount;
3034 // Though there are 4 vertices in a quad, we use 6 indices per
3035 // quad to draw with GL_TRIANGLES
3036 mesh += (drawCount / 6) * 4;
3037 }
Romain Guyf219da52011-01-16 12:54:25 -08003038
Romain Guy3a3133d2011-02-01 22:59:58 -08003039#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003040 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003041#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003042 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003043
Romain Guy5bb3c732012-11-29 17:52:58 -08003044 if (layer->debugDrawUpdate) {
3045 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003046
3047 SkPaint paint;
3048 paint.setColor(0x7f00ff00);
3049 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003050 }
Romain Guyf219da52011-01-16 12:54:25 -08003051 }
Chris Craik34416ea2013-04-15 16:08:28 -07003052 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003053
Romain Guyb2e2f242012-10-17 18:18:35 -07003054 if (transform && !transform->isIdentity()) {
3055 restore();
3056 }
3057
Tom Hudson107843d2014-09-08 11:26:26 -04003058 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003059}
3060
Romain Guy6926c722010-07-12 20:20:03 -07003061///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003062// Draw filters
3063///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003064void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3065 // We should never get here since we apply the draw filter when stashing
3066 // the paints in the DisplayList.
3067 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003068}
3069
3070///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003071// Drawing implementation
3072///////////////////////////////////////////////////////////////////////////////
3073
Chris Craikd218a922014-01-02 17:13:34 -08003074Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003075 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003076 if (!texture) {
3077 return mCaches.textureCache.get(bitmap);
3078 }
3079 return texture;
3080}
3081
Romain Guy01d58e42011-01-19 21:54:02 -08003082void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003083 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003084 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003085 return;
3086 }
3087
3088 int alpha;
3089 SkXfermode::Mode mode;
3090 getAlphaAndMode(paint, &alpha, &mode);
3091
3092 setupDraw();
3093 setupDrawWithTexture(true);
3094 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003095 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003096 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003097 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003098 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003099 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3100 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003101 setupDrawTexture(texture->id);
3102 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003103 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003104 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08003105 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Romain Guy01d58e42011-01-19 21:54:02 -08003106
Chris Craik96a5c4c2015-01-27 15:46:35 -08003107 glDrawArrays(GL_TRIANGLE_STRIP, 0, kMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003108}
3109
Romain Guyf607bdc2010-09-10 19:20:06 -07003110// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003111#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3112#define kStdUnderline_Offset (1.0f / 9.0f)
3113#define kStdUnderline_Thickness (1.0f / 18.0f)
3114
Chris Craikd218a922014-01-02 17:13:34 -08003115void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3116 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003117 // Handle underline and strike-through
3118 uint32_t flags = paint->getFlags();
3119 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003120 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003121
Romain Guy211370f2012-02-01 16:10:55 -08003122 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003123 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003124 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003125
Raph Levien8b4072d2012-07-30 15:50:00 -07003126 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003127 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003128
Romain Guyf6834472011-01-23 13:32:12 -08003129 int linesCount = 0;
3130 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3131 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3132
3133 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003134 float points[pointsCount];
3135 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003136
3137 if (flags & SkPaint::kUnderlineText_Flag) {
3138 top = y + textSize * kStdUnderline_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 }
3144
3145 if (flags & SkPaint::kStrikeThruText_Flag) {
3146 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003147 points[currentPoint++] = left;
3148 points[currentPoint++] = top;
3149 points[currentPoint++] = left + underlineWidth;
3150 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003151 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003152
Romain Guy726aeba2011-06-01 14:52:00 -07003153 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003154
Romain Guy726aeba2011-06-01 14:52:00 -07003155 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003156 }
3157 }
3158}
3159
Tom Hudson107843d2014-09-08 11:26:26 -04003160void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003161 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003162 return;
Romain Guy672433d2013-01-04 19:05:13 -08003163 }
3164
Tom Hudson107843d2014-09-08 11:26:26 -04003165 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003166}
3167
Tom Hudson107843d2014-09-08 11:26:26 -04003168void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003169 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003170 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003171
Chris Craik15a07a22014-01-26 13:43:53 -08003172 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08003173 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07003174
3175 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003176 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003177
ztenghui14a4e352014-08-13 10:44:39 -07003178 // The caller has made sure casterAlpha > 0.
3179 float ambientShadowAlpha = mAmbientShadowAlpha;
3180 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3181 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3182 }
3183 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3184 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003185 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003186 }
ztenghui7b4516e2014-01-07 10:42:55 -08003187
ztenghui14a4e352014-08-13 10:44:39 -07003188 float spotShadowAlpha = mSpotShadowAlpha;
3189 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3190 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3191 }
3192 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3193 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003194 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003195 }
ztenghui7b4516e2014-01-07 10:42:55 -08003196
Tom Hudson107843d2014-09-08 11:26:26 -04003197 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003198}
3199
Tom Hudson107843d2014-09-08 11:26:26 -04003200void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003201 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003202 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003203 return;
Romain Guy3b753822013-03-05 10:27:35 -08003204 }
Romain Guy735738c2012-12-03 12:34:51 -08003205
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003206 int color = paint->getColor();
3207 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003208 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003209 color |= 0x00ffffff;
3210 }
3211
Romain Guy672433d2013-01-04 19:05:13 -08003212 float left = FLT_MAX;
3213 float top = FLT_MAX;
3214 float right = FLT_MIN;
3215 float bottom = FLT_MIN;
3216
Romain Guy448455f2013-07-22 13:57:50 -07003217 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003218 Vertex* vertex = mesh;
3219
Chris Craik2af46352012-11-26 18:30:17 -08003220 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003221 float l = rects[index + 0];
3222 float t = rects[index + 1];
3223 float r = rects[index + 2];
3224 float b = rects[index + 3];
3225
Romain Guy3b753822013-03-05 10:27:35 -08003226 Vertex::set(vertex++, l, t);
3227 Vertex::set(vertex++, r, t);
3228 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003229 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003230
Romain Guy3b753822013-03-05 10:27:35 -08003231 left = fminf(left, l);
3232 top = fminf(top, t);
3233 right = fmaxf(right, r);
3234 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003235 }
3236
Chris Craikf0a59072013-11-19 18:00:46 -08003237 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003238 return;
Romain Guya362c692013-02-04 13:50:16 -08003239 }
Romain Guy672433d2013-01-04 19:05:13 -08003240
Romain Guy672433d2013-01-04 19:05:13 -08003241 setupDraw();
3242 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003243 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003244 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003245 setupDrawColorFilter(getColorFilter(paint));
3246 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003247 setupDrawProgram();
3248 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003249 setupDrawModelView(kModelViewMode_Translate, false,
3250 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003251 setupDrawColorUniforms(getShader(paint));
3252 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003253 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003254
Romain Guy8ce00302013-01-15 18:51:42 -08003255 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003256 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003257 }
3258
Chris Craik4063a0e2013-11-15 16:06:56 -08003259 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003260
Tom Hudson107843d2014-09-08 11:26:26 -04003261 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003262}
3263
Romain Guy026c5e162010-06-28 17:12:22 -07003264void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003265 const SkPaint* paint, bool ignoreTransform) {
3266 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003267 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003268 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003269 color |= 0x00ffffff;
3270 }
3271
Romain Guy70ca14e2010-12-13 18:24:33 -08003272 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003273 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003274 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003275 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003276 setupDrawColorFilter(getColorFilter(paint));
3277 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003278 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003279 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3280 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003281 setupDrawColorUniforms(getShader(paint));
3282 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003283 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003284 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003285
Chris Craik96a5c4c2015-01-27 15:46:35 -08003286 glDrawArrays(GL_TRIANGLE_STRIP, 0, kMeshCount);
Romain Guyc95c8d62010-09-17 15:31:32 -07003287}
3288
Romain Guy82ba8142010-07-09 13:25:56 -07003289void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003290 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003291 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003292
Chris Craike84a2082014-12-22 14:28:49 -08003293 GLvoid* vertices = (GLvoid*) nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -08003294 GLvoid* texCoords = (GLvoid*) kMeshTextureOffset;
Romain Guy3b748a42013-04-17 18:54:38 -07003295
3296 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003297 vertices = &mMeshVertices[0].x;
3298 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003299
3300 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3301 texture->uvMapper->map(uvs);
3302
3303 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3304 }
3305
Chris Craikd6b65f62014-01-01 14:45:21 -08003306 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3307 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3308 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003309
Romain Guyd21b6e12011-11-30 20:21:23 -08003310 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003311 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003312 paint, texture->blend, vertices, texCoords,
Chris Craik96a5c4c2015-01-27 15:46:35 -08003313 GL_TRIANGLE_STRIP, kMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003314 } else {
Chris Craik678625242014-02-28 12:26:34 -08003315 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003316 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Chris Craik96a5c4c2015-01-27 15:46:35 -08003317 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, kMeshCount);
Romain Guy3b748a42013-04-17 18:54:38 -07003318 }
3319
3320 if (texture->uvMapper) {
3321 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003322 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003323}
3324
Romain Guyf7f93552010-07-08 19:17:03 -07003325void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003326 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003327 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003328 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3329 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003330
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003331 int a;
3332 SkXfermode::Mode mode;
3333 getAlphaAndMode(paint, &a, &mode);
3334 const float alpha = a / 255.0f;
3335
Romain Guy746b7402010-10-26 16:27:31 -07003336 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003337 setupDrawWithTexture();
3338 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003339 setupDrawColorFilter(getColorFilter(paint));
3340 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003341 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003342 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003343 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003344 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003345 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003346 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003347 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003348
Romain Guy6820ac82010-09-15 18:11:50 -07003349 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003350}
3351
Romain Guy3b748a42013-04-17 18:54:38 -07003352void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003353 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003354 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003355 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3356 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003357
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003358 int a;
3359 SkXfermode::Mode mode;
3360 getAlphaAndMode(paint, &a, &mode);
3361 const float alpha = a / 255.0f;
3362
Romain Guy3b748a42013-04-17 18:54:38 -07003363 setupDraw();
3364 setupDrawWithTexture();
3365 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003366 setupDrawColorFilter(getColorFilter(paint));
3367 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003368 setupDrawProgram();
3369 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003370 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003371 setupDrawTexture(texture);
3372 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003373 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003374 setupDrawMeshIndices(vertices, texCoords, vbo);
3375
Chris Craike84a2082014-12-22 14:28:49 -08003376 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy3b748a42013-04-17 18:54:38 -07003377}
3378
Romain Guy886b2752013-01-04 12:26:18 -08003379void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003380 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003381 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003382 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003383
Chris Craike84a2082014-12-22 14:28:49 -08003384 int color = paint != nullptr ? paint->getColor() : 0;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003385 int alpha;
3386 SkXfermode::Mode mode;
3387 getAlphaAndMode(paint, &alpha, &mode);
3388
Romain Guy886b2752013-01-04 12:26:18 -08003389 setupDraw();
3390 setupDrawWithTexture(true);
Chris Craike84a2082014-12-22 14:28:49 -08003391 if (paint != nullptr) {
Romain Guy886b2752013-01-04 12:26:18 -08003392 setupDrawAlpha8Color(color, alpha);
3393 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003394 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003395 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003396 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003397 setupDrawProgram();
3398 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003399 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003400 setupDrawTexture(texture);
3401 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003402 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003403 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003404 setupDrawMesh(vertices, texCoords);
3405
3406 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003407}
3408
Romain Guya5aed0d2010-09-09 14:42:43 -07003409void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003410 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003411
Chris Craike84a2082014-12-22 14:28:49 -08003412 if (writableSnapshot()->roundRectClipState != nullptr /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003413 blend = true;
3414 mDescription.hasRoundRectClip = true;
3415 }
3416 mSkipOutlineClip = true;
3417
Romain Guy82ba8142010-07-09 13:25:56 -07003418 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003419
Romain Guy82ba8142010-07-09 13:25:56 -07003420 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003421 // These blend modes are not supported by OpenGL directly and have
3422 // to be implemented using shaders. Since the shader will perform
3423 // the blending, turn blending off here
3424 // If the blend mode cannot be implemented using shaders, fall
3425 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003426 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003427 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003428 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003429 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003430
Chris Craik44eb2c02015-01-29 09:45:09 -08003431 mRenderState.blend().disable();
Romain Guy82bc7a72012-01-03 14:13:39 -08003432 return;
3433 } else {
3434 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003435 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003436 }
Chris Craik44eb2c02015-01-29 09:45:09 -08003437 mRenderState.blend().enable(mode, swapSrcDst);
3438 } else {
3439 mRenderState.blend().disable();
Romain Guy82ba8142010-07-09 13:25:56 -07003440 }
Romain Guybd6b79b2010-06-26 00:13:53 -07003441}
3442
Romain Guy889f8d12010-07-29 14:37:42 -07003443bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003444 if (!program->isInUse()) {
Chris Craike84a2082014-12-22 14:28:49 -08003445 if (mCaches.currentProgram != nullptr) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003446 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003447 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003448 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003449 }
Romain Guy6926c722010-07-12 20:20:03 -07003450 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003451}
3452
Romain Guy026c5e162010-06-28 17:12:22 -07003453void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003454 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003455 TextureVertex::setUV(v++, u1, v1);
3456 TextureVertex::setUV(v++, u2, v1);
3457 TextureVertex::setUV(v++, u1, v2);
3458 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003459}
3460
Chris Craike84a2082014-12-22 14:28:49 -08003461void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
3462 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003463 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003464 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3465 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003466 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003467 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003468 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003469}
3470
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003471float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003472 float alpha;
3473 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3474 alpha = mDrawModifiers.mOverrideLayerAlpha;
3475 } else {
3476 alpha = layer->getAlpha() / 255.0f;
3477 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003478 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003479}
3480
Romain Guy9d5316e2010-06-24 19:30:36 -07003481}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003482}; // namespace android