blob: 3f67f54a1c6ff78ec4f5cd5f6d8d99d8d95f33ae [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
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070024#include <SkColor.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040025#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070026#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070027
Romain Guye4d01122010-06-16 18:44:05 -070028#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070029#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070030
Romain Guy08aa2cb2011-03-17 11:06:57 -070031#include <private/hwui/DrawGlInfo.h>
32
Romain Guy5b3b3522010-10-27 18:57:51 -070033#include <ui/Rect.h>
34
Romain Guy85bf02f2010-06-22 13:11:24 -070035#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080036#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080037#include "DisplayListRenderer.h"
Romain Guy40543602013-06-12 15:31:28 -070038#include "Fence.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040039#include "GammaFontRenderer.h"
40#include "Patch.h"
Chris Craik65cd6122012-12-10 17:56:27 -080041#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070042#include "Properties.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040043#include "RenderNode.h"
44#include "RenderState.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080045#include "ShadowTessellator.h"
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040046#include "SkiaShader.h"
Romain Guya957eea2010-12-08 18:34:42 -080047#include "Vector.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080048#include "VertexBuffer.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040049#include "utils/GLUtils.h"
Chris Craik06e7fe52014-11-20 17:27:36 -080050#include "utils/TraceUtils.h"
Romain Guye4d01122010-06-16 18:44:05 -070051
Chris Craik62d307c2014-07-29 10:35:13 -070052#if DEBUG_DETAILED_EVENTS
53 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
54#else
55 #define EVENT_LOGD(...)
56#endif
57
Romain Guye4d01122010-06-16 18:44:05 -070058namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070059namespace uirenderer {
60
Chris Craik678625242014-02-28 12:26:34 -080061static GLenum getFilter(const SkPaint* paint) {
62 if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) {
63 return GL_LINEAR;
64 }
65 return GL_NEAREST;
66}
Romain Guyd21b6e12011-11-30 20:21:23 -080067
Romain Guy9d5316e2010-06-24 19:30:36 -070068///////////////////////////////////////////////////////////////////////////////
69// Globals
70///////////////////////////////////////////////////////////////////////////////
71
Romain Guy889f8d12010-07-29 14:37:42 -070072/**
73 * Structure mapping Skia xfermodes to OpenGL blending factors.
74 */
75struct Blender {
76 SkXfermode::Mode mode;
77 GLenum src;
78 GLenum dst;
79}; // struct Blender
80
Romain Guy026c5e162010-06-28 17:12:22 -070081// In this array, the index of each Blender equals the value of the first
82// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
83static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070084 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
85 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
86 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
87 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
88 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
89 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
90 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
91 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
92 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
93 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
94 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
95 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
96 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050097 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070098 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070099};
Romain Guye4d01122010-06-16 18:44:05 -0700100
Romain Guy87a76572010-09-13 18:11:21 -0700101// This array contains the swapped version of each SkXfermode. For instance
102// this array's SrcOver blending mode is actually DstOver. You can refer to
103// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -0700104static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -0700105 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
106 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
107 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
108 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
109 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
110 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
111 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
112 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
113 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
114 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
115 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
116 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
117 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500118 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700119 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700120};
121
Romain Guyf6a11b82010-06-23 17:47:49 -0700122///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700123// Functions
124///////////////////////////////////////////////////////////////////////////////
125
126template<typename T>
127static inline T min(T a, T b) {
128 return a < b ? a : b;
129}
130
131///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700132// Constructors/destructor
133///////////////////////////////////////////////////////////////////////////////
134
John Reck3b202512014-06-23 13:13:08 -0700135OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -0400136 : mState(*this)
137 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -0700138 , mCaches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -0700139 , mExtensions(Extensions::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -0700140 , mRenderState(renderState)
141 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -0700142 , mSuppressTiling(false)
143 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -0400144 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -0700145 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -0700146 , mLightRadius(FLT_MIN)
147 , mAmbientShadowAlpha(0)
148 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800149 // *set* draw modifiers to be 0
150 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700151 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700152
Romain Guyac670c02010-07-27 17:39:27 -0700153 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700154}
155
Romain Guy85bf02f2010-06-22 13:11:24 -0700156OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700157 // The context has already been destroyed at this point, do not call
158 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700159}
160
Romain Guy87e2f7572012-09-24 11:37:12 -0700161void OpenGLRenderer::initProperties() {
162 char property[PROPERTY_VALUE_MAX];
163 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
164 mScissorOptimizationDisabled = !strcasecmp(property, "true");
165 INIT_LOGD(" Scissor optimization %s",
166 mScissorOptimizationDisabled ? "disabled" : "enabled");
167 } else {
168 INIT_LOGD(" Scissor optimization enabled");
169 }
Romain Guy13631f32012-01-30 17:41:55 -0800170}
171
Chris Craik058fc642014-07-23 18:19:28 -0700172void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
173 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
174 mLightCenter = lightCenter;
175 mLightRadius = lightRadius;
176 mAmbientShadowAlpha = ambientShadowAlpha;
177 mSpotShadowAlpha = spotShadowAlpha;
178}
179
Romain Guy13631f32012-01-30 17:41:55 -0800180///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700181// Setup
182///////////////////////////////////////////////////////////////////////////////
183
Chris Craik797b95b22014-05-20 18:10:25 -0700184void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700185 glDisable(GL_DITHER);
186 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
187
188 glEnableVertexAttribArray(Program::kBindingPosition);
Chris Craik284b2432014-09-18 16:05:35 -0700189 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700190}
191
Romain Guy96885eb2013-03-26 15:05:58 -0700192void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800193 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800194 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400195 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700196 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700197 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700198}
199
Tom Hudson107843d2014-09-08 11:26:26 -0400200void OpenGLRenderer::startFrame() {
201 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700202 mFrameStarted = true;
203
Tom Hudson984162f2014-10-10 13:38:16 -0400204 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700205
Romain Guy96885eb2013-03-26 15:05:58 -0700206 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700207
Tom Hudson984162f2014-10-10 13:38:16 -0400208 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800209
Romain Guy54c1a642012-09-27 17:55:46 -0700210 // Functors break the tiling extension in pretty spectacular ways
211 // This ensures we don't use tiling when a functor is going to be
212 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700213 mSuppressTiling = mCaches.hasRegisteredFunctors()
214 || mFirstFrameAfterResize;
215 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700216
Chris Craikd6b65f62014-01-01 14:45:21 -0800217 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700218
Romain Guy7c450aa2012-09-21 19:15:00 -0700219 debugOverdraw(true, true);
220
Tom Hudson107843d2014-09-08 11:26:26 -0400221 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700222 mTilingClip.right, mTilingClip.bottom, mOpaque);
223}
224
Tom Hudson107843d2014-09-08 11:26:26 -0400225void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700226 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700227
Romain Guy96885eb2013-03-26 15:05:58 -0700228 setupFrameState(left, top, right, bottom, opaque);
229
230 // Layer renderers will start the frame immediately
231 // The framebuffer renderer will first defer the display list
232 // for each layer and wait until the first drawing command
233 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800234 if (currentSnapshot()->fbo == 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700235 syncState();
236 updateLayers();
237 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400238 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700239 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700240}
241
Romain Guydcfc8362013-01-03 13:08:57 -0800242void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
243 // If we know that we are going to redraw the entire framebuffer,
244 // perform a discard to let the driver know we don't need to preserve
245 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800246 if (mExtensions.hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400247 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
248 const bool isFbo = onGetTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800249 const GLenum attachments[] = {
250 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
251 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800252 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
253 }
254}
255
Tom Hudson107843d2014-09-08 11:26:26 -0400256void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700257 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700258 mCaches.enableScissor();
Chris Craika64a2be2014-05-14 14:17:01 -0700259 mCaches.setScissor(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700260 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400261 mDirty = true;
262 return;
Romain Guyddf74372012-05-22 14:07:07 -0700263 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700264
Romain Guy7c25aab2012-10-18 15:05:02 -0700265 mCaches.resetScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700266}
267
268void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700269 if (mCaches.blend) {
270 glEnable(GL_BLEND);
271 } else {
272 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700273 }
Romain Guybb9524b2010-06-22 18:56:38 -0700274}
275
henry.uh_chen33f5a592014-07-02 19:36:56 +0800276void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700277 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800278 const Snapshot* snapshot = currentSnapshot();
279
Chris Craik14e51302013-12-30 15:32:54 -0800280 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800281 if (snapshot->flags & Snapshot::kFlagFboTarget) {
282 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700283 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700284
henry.uh_chen33f5a592014-07-02 19:36:56 +0800285 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800286 }
287}
288
henry.uh_chen33f5a592014-07-02 19:36:56 +0800289void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800290 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800291 if(expand) {
292 // Expand the startTiling region by 1
293 int leftNotZero = (clip.left > 0) ? 1 : 0;
294 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
295
296 mCaches.startTiling(
297 clip.left - leftNotZero,
298 windowHeight - clip.bottom - topNotZero,
299 clip.right - clip.left + leftNotZero + 1,
300 clip.bottom - clip.top + topNotZero + 1,
301 opaque);
302 } else {
303 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800304 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800305 }
Romain Guy54c1a642012-09-27 17:55:46 -0700306 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700307}
308
309void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700310 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700311}
312
Tom Hudson107843d2014-09-08 11:26:26 -0400313bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700314 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700315 endTiling();
316
Chris Craik4ac36f82014-12-09 16:54:03 -0800317 for (size_t i = 0; i < mTempPaths.size(); i++) {
318 delete mTempPaths[i];
319 }
320 mTempPaths.clear();
321
Romain Guyca89e2a2013-03-08 17:44:20 -0800322 // When finish() is invoked on FBO 0 we've reached the end
323 // of the current frame
Tom Hudson984162f2014-10-10 13:38:16 -0400324 if (onGetTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800325 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700326 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800327 }
328
Romain Guy11cb6422012-09-21 00:39:43 -0700329 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700330#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700331 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700332#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700333
Romain Guyc15008e2010-11-10 11:59:15 -0800334#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800335 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700336#else
337 if (mCaches.getDebugLevel() & kDebugMemory) {
338 mCaches.dumpMemoryUsage();
339 }
Romain Guyc15008e2010-11-10 11:59:15 -0800340#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700341 }
Romain Guy96885eb2013-03-26 15:05:58 -0700342
343 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400344
345 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700346}
347
Romain Guy35643dd2012-09-18 15:40:58 -0700348void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700349 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
350 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700351 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700352
353 mCaches.resetScissor();
354 dirtyClip();
355}
356
Andreas Gampe64bb4132014-11-22 00:35:09 +0000357void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400358 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700359
Tom Hudson984162f2014-10-10 13:38:16 -0400360 Rect clip(*mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700361 clip.snapToPixelBoundaries();
362
Romain Guyd643bb52011-03-01 14:55:21 -0800363 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800364 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800365 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800366 dirtyLayerUnchecked(clip, getRegion());
367 }
Romain Guyd643bb52011-03-01 14:55:21 -0800368
Romain Guy08aa2cb2011-03-17 11:06:57 -0700369 DrawGlInfo info;
370 info.clipLeft = clip.left;
371 info.clipTop = clip.top;
372 info.clipRight = clip.right;
373 info.clipBottom = clip.bottom;
374 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700375 info.width = getViewportWidth();
376 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800377 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700378
Tom Hudson984162f2014-10-10 13:38:16 -0400379 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700380 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400381 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700382 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
383 }
John Reck3b202512014-06-23 13:13:08 -0700384 if (mCaches.enableScissor() || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700385 setScissorFromClip();
386 }
Chris Craik54f574a2013-08-26 11:23:46 -0700387
John Reck3b202512014-06-23 13:13:08 -0700388 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
389 // Scissor may have been modified, reset dirty clip
390 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800391
Tom Hudson107843d2014-09-08 11:26:26 -0400392 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800393}
394
Romain Guyf6a11b82010-06-23 17:47:49 -0700395///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700396// Debug
397///////////////////////////////////////////////////////////////////////////////
398
Chris Craik62d307c2014-07-29 10:35:13 -0700399void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
400#if DEBUG_DETAILED_EVENTS
401 const int BUFFER_SIZE = 256;
402 va_list ap;
403 char buf[BUFFER_SIZE];
404
405 va_start(ap, fmt);
406 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
407 va_end(ap);
408
409 eventMark(buf);
410#endif
411}
412
413
Romain Guy0f6675332013-03-01 14:31:04 -0800414void OpenGLRenderer::eventMark(const char* name) const {
415 mCaches.eventMark(0, name);
416}
417
Romain Guy87e2f7572012-09-24 11:37:12 -0700418void OpenGLRenderer::startMark(const char* name) const {
419 mCaches.startMark(0, name);
420}
421
422void OpenGLRenderer::endMark() const {
423 mCaches.endMark();
424}
425
426void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700427 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700428}
429
430void OpenGLRenderer::renderOverdraw() {
Tom Hudson984162f2014-10-10 13:38:16 -0400431 if (mCaches.debugOverdraw && onGetTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700432 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700433
434 mCaches.enableScissor();
Tom Hudson984162f2014-10-10 13:38:16 -0400435 mCaches.setScissor(clip->left, mState.firstSnapshot()->getViewportHeight() - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700436 clip->right - clip->left, clip->bottom - clip->top);
437
Romain Guy627c6fd2013-08-21 11:53:18 -0700438 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700439 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700440 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
441
442 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700443 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700444 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
445
446 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700447 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700448 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
449
450 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700451 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700452 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
453
Romain Guy87e2f7572012-09-24 11:37:12 -0700454 mCaches.stencil.disable();
455 }
456}
457
458///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700459// Layers
460///////////////////////////////////////////////////////////////////////////////
461
462bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700463 if (layer->deferredUpdateScheduled && layer->renderer
464 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700465
Romain Guy7c450aa2012-09-21 19:15:00 -0700466 if (inFrame) {
467 endTiling();
468 debugOverdraw(false, false);
469 }
Romain Guy11cb6422012-09-21 00:39:43 -0700470
Romain Guy96885eb2013-03-26 15:05:58 -0700471 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700472 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700473 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700474 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700475 }
Romain Guy11cb6422012-09-21 00:39:43 -0700476
477 if (inFrame) {
478 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800479 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700480 }
481
Romain Guy5bb3c732012-11-29 17:52:58 -0800482 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700483 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700484
485 return true;
486 }
487
488 return false;
489}
490
491void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700492 // If draw deferring is enabled this method will simply defer
493 // the display list of each individual layer. The layers remain
494 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700495 int count = mLayerUpdates.size();
496 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700497 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
498 startMark("Layer Updates");
499 } else {
500 startMark("Defer Layer Updates");
501 }
Romain Guy11cb6422012-09-21 00:39:43 -0700502
Chris Craik1206b9b2013-04-04 14:46:24 -0700503 // Note: it is very important to update the layers in order
504 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700505 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700506 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700507 }
Romain Guy11cb6422012-09-21 00:39:43 -0700508
Romain Guy96885eb2013-03-26 15:05:58 -0700509 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
510 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400511 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700512 }
513 endMark();
514 }
515}
516
517void OpenGLRenderer::flushLayers() {
518 int count = mLayerUpdates.size();
519 if (count > 0) {
520 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700521
Chris Craik1206b9b2013-04-04 14:46:24 -0700522 // Note: it is very important to update the layers in order
523 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800524 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700525 }
526
527 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400528 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700529
Romain Guy11cb6422012-09-21 00:39:43 -0700530 endMark();
531 }
532}
533
534void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
535 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700536 // Make sure we don't introduce duplicates.
537 // SortedVector would do this automatically but we need to respect
538 // the insertion order. The linear search is not an issue since
539 // this list is usually very short (typically one item, at most a few)
540 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
541 if (mLayerUpdates.itemAt(i) == layer) {
542 return;
543 }
544 }
Romain Guy11cb6422012-09-21 00:39:43 -0700545 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700546 }
547}
548
Romain Guye93482f2013-06-17 13:14:51 -0700549void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
550 if (layer) {
551 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
552 if (mLayerUpdates.itemAt(i) == layer) {
553 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700554 break;
555 }
556 }
557 }
558}
559
Romain Guy40543602013-06-12 15:31:28 -0700560void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800561 ATRACE_NAME("Update HW Layers");
Romain Guy40543602013-06-12 15:31:28 -0700562 syncState();
563 updateLayers();
564 flushLayers();
565 // Wait for all the layer updates to be executed
566 AutoFence fence;
567}
568
John Reck443a7142014-09-04 17:40:05 -0700569void OpenGLRenderer::markLayersAsBuildLayers() {
570 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
571 mLayerUpdates[i]->wasBuildLayered = true;
572 }
573}
574
Romain Guy11cb6422012-09-21 00:39:43 -0700575///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700576// State management
577///////////////////////////////////////////////////////////////////////////////
578
Chris Craik14e51302013-12-30 15:32:54 -0800579void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700580 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800581 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
582 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700583
Chris Craika64a2be2014-05-14 14:17:01 -0700584 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700585 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700586 }
587
Romain Guy2542d192010-08-18 11:47:12 -0700588 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700589 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700590 }
Romain Guy2542d192010-08-18 11:47:12 -0700591
Romain Guy5ec99242010-11-03 16:19:08 -0700592 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700593 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700594 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700595 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800596 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700597 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700598 }
Romain Guybb9524b2010-06-22 18:56:38 -0700599}
600
Romain Guyf6a11b82010-06-23 17:47:49 -0700601///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700602// Layers
603///////////////////////////////////////////////////////////////////////////////
604
605int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700606 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700607 // force matrix/clip isolation for layer
608 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
609
Tom Hudson984162f2014-10-10 13:38:16 -0400610 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700611
Tom Hudson984162f2014-10-10 13:38:16 -0400612 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700613 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700614 }
Romain Guyd55a8612010-06-28 17:42:46 -0700615
616 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700617}
618
Chris Craikd90144d2013-03-19 15:03:48 -0700619void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
620 const Rect untransformedBounds(bounds);
621
Chris Craikd6b65f62014-01-01 14:45:21 -0800622 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700623
624 // Layers only make sense if they are in the framebuffer's bounds
Tom Hudson984162f2014-10-10 13:38:16 -0400625 if (bounds.intersect(*mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700626 // We cannot work with sub-pixels in this case
627 bounds.snapToPixelBoundaries();
628
629 // When the layer is not an FBO, we may use glCopyTexImage so we
630 // need to make sure the layer does not extend outside the bounds
631 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700632 const Snapshot& previous = *(currentSnapshot()->previous);
633 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
634 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700635 bounds.setEmpty();
636 } else if (fboLayer) {
637 clip.set(bounds);
638 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800639 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700640 inverse.mapRect(clip);
641 clip.snapToPixelBoundaries();
642 if (clip.intersect(untransformedBounds)) {
643 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
644 bounds.set(untransformedBounds);
645 } else {
646 clip.setEmpty();
647 }
648 }
649 } else {
650 bounds.setEmpty();
651 }
652}
653
Chris Craik408eb122013-03-26 18:55:15 -0700654void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
655 bool fboLayer, int alpha) {
656 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
657 bounds.getHeight() > mCaches.maxTextureSize ||
658 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400659 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700660 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400661 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700662 }
663}
664
Chris Craikd90144d2013-03-19 15:03:48 -0700665int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500666 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400667 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700668
Tom Hudson984162f2014-10-10 13:38:16 -0400669 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700670 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
671 // operations will be able to store and restore the current clip and transform info, and
672 // quick rejection will be correct (for display lists)
673
674 Rect bounds(left, top, right, bottom);
675 Rect clip;
676 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500677 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700678
Tom Hudson984162f2014-10-10 13:38:16 -0400679 if (!mState.currentlyIgnored()) {
680 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
681 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
682 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
683 writableSnapshot()->roundRectClipState = NULL;
Chris Craikd90144d2013-03-19 15:03:48 -0700684 }
685 }
686
687 return count;
688}
689
Romain Guy1c740bc2010-09-13 18:00:09 -0700690/**
691 * Layers are viewed by Skia are slightly different than layers in image editing
692 * programs (for instance.) When a layer is created, previously created layers
693 * and the frame buffer still receive every drawing command. For instance, if a
694 * layer is created and a shape intersecting the bounds of the layers and the
695 * framebuffer is draw, the shape will be drawn on both (unless the layer was
696 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
697 *
698 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
699 * texture. Unfortunately, this is inefficient as it requires every primitive to
700 * be drawn n + 1 times, where n is the number of active layers. In practice this
701 * means, for every primitive:
702 * - Switch active frame buffer
703 * - Change viewport, clip and projection matrix
704 * - Issue the drawing
705 *
706 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700707 * To avoid this, layers are implemented in a different way here, at least in the
708 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
709 * is set. When this flag is set we can redirect all drawing operations into a
710 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700711 *
712 * This implementation relies on the frame buffer being at least RGBA 8888. When
713 * a layer is created, only a texture is created, not an FBO. The content of the
714 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700715 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700716 * buffer and drawing continues as normal. This technique therefore treats the
717 * frame buffer as a scratch buffer for the layers.
718 *
719 * To compose the layers back onto the frame buffer, each layer texture
720 * (containing the original frame buffer data) is drawn as a simple quad over
721 * the frame buffer. The trick is that the quad is set as the composition
722 * destination in the blending equation, and the frame buffer becomes the source
723 * of the composition.
724 *
725 * Drawing layers with an alpha value requires an extra step before composition.
726 * An empty quad is drawn over the layer's region in the frame buffer. This quad
727 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
728 * quad is used to multiply the colors in the frame buffer. This is achieved by
729 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
730 * GL_ZERO, GL_SRC_ALPHA.
731 *
732 * Because glCopyTexImage2D() can be slow, an alternative implementation might
733 * be use to draw a single clipped layer. The implementation described above
734 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700735 *
736 * (1) The frame buffer is actually not cleared right away. To allow the GPU
737 * to potentially optimize series of calls to glCopyTexImage2D, the frame
738 * buffer is left untouched until the first drawing operation. Only when
739 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700740 */
Chet Haased48885a2012-08-28 17:43:28 -0700741bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700742 const SkPaint* paint, int flags, const SkPath* convexMask) {
Andreas Gampeedaecc12014-11-10 20:54:07 -0800743 if (kDebugLayers) {
744 ALOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
745 ALOGD("Layer cache size = %d", mCaches.layerCache.getSize());
746 }
Romain Guy8ba548f2010-06-30 19:21:21 -0700747
Romain Guyeb993562010-10-05 18:14:38 -0700748 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
749
Romain Guyf607bdc2010-09-10 19:20:06 -0700750 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700751 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700752 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700753 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000754 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700755
756 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400757 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700758 return false;
759 }
Romain Guyf18fd992010-07-08 11:45:51 -0700760
Romain Guya1d3c912011-12-13 14:55:06 -0800761 mCaches.activeTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700762 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700763 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700764 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700765 }
766
Derek Sollenberger674554f2014-02-19 16:47:32 +0000767 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700768 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700769 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
770 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500771
Chet Haasea23eed82012-04-12 15:19:04 -0700772 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700773 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700774 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700775
Romain Guy8fb95422010-08-17 18:38:51 -0700776 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400777 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
778 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700779
Chris Craika8bea8e2014-09-24 11:29:43 -0700780 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
781 fboLayer ? "" : "unclipped ",
782 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700783 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700784 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700785 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700786 } else {
787 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700788 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800789 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700790 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700791 // Workaround for some GL drivers. When reading pixels lying outside
792 // of the window we should get undefined values for those pixels.
793 // Unfortunately some drivers will turn the entire target texture black
794 // when reading outside of the window.
795 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
796 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700797 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800798 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700799
Chris Craika64a2be2014-05-14 14:17:01 -0700800 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
801 bounds.left, getViewportHeight() - bounds.bottom,
802 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700803
Romain Guy54be1cd2011-06-13 19:04:27 -0700804 // Enqueue the buffer coordinates to clear the corresponding region later
805 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700806 }
Romain Guyeb993562010-10-05 18:14:38 -0700807 }
Romain Guyf86ef572010-07-01 11:05:42 -0700808
Romain Guyd55a8612010-06-28 17:42:46 -0700809 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700810}
811
Chris Craike63f7c622013-10-17 10:30:55 -0700812bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800813 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700814 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700815
Tom Hudson984162f2014-10-10 13:38:16 -0400816 writableSnapshot()->region = &writableSnapshot()->layer->region;
817 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
818 writableSnapshot()->fbo = layer->getFbo();
819 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
820 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
821 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
822 writableSnapshot()->roundRectClipState = NULL;
Romain Guy5b3b3522010-10-27 18:57:51 -0700823
Romain Guy2b7028e2012-09-19 17:25:38 -0700824 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700825 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700826 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700827 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700828 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700829
830 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700831 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700832 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700833 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700834 }
835
836 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700837 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700838
henry.uh_chen33f5a592014-07-02 19:36:56 +0800839 // Expand the startTiling region by 1
840 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700841
842 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700843 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800844 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700845 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700846 glClear(GL_COLOR_BUFFER_BIT);
847
848 dirtyClip();
849
850 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700851 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700852 return true;
853}
854
Romain Guy1c740bc2010-09-13 18:00:09 -0700855/**
856 * Read the documentation of createLayer() before doing anything in this method.
857 */
Chris Craik14e51302013-12-30 15:32:54 -0800858void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
859 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000860 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700861 return;
862 }
863
Chris Craik14e51302013-12-30 15:32:54 -0800864 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800865 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800866 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700867
Chris Craik39a908c2013-06-13 14:39:01 -0700868 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400869 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700870 &clipRequired, NULL, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -0700871 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
872
Romain Guyeb993562010-10-05 18:14:38 -0700873 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700874 endTiling();
875
Romain Guye0aa84b2012-04-03 19:30:26 -0700876 // Detach the texture from the FBO
877 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800878
879 layer->removeFbo(false);
880
Romain Guyeb993562010-10-05 18:14:38 -0700881 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700882 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700883 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700884
Chris Craikd6b65f62014-01-01 14:45:21 -0800885 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700886 }
887
Romain Guy9ace8f52011-07-07 20:50:11 -0700888 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500889 SkPaint layerPaint;
890 layerPaint.setAlpha(layer->getAlpha());
891 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
892 layerPaint.setColorFilter(layer->getColorFilter());
893
894 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700895 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700896 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700897 }
898
Romain Guy03750a02010-10-18 14:06:08 -0700899 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700900
Romain Guya1d3c912011-12-13 14:55:06 -0800901 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700902
Romain Guy5b3b3522010-10-27 18:57:51 -0700903 // When the layer is stored in an FBO, we can save a bit of fillrate by
904 // drawing only the dirty region
905 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800906 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700907 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700908 } else if (!rect.isEmpty()) {
909 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530910
911 save(0);
912 // the layer contains screen buffer content that shouldn't be alpha modulated
913 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400914 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700915 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530916 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700917 }
Romain Guy8b55f372010-08-18 17:10:07 -0700918
Romain Guy746b7402010-10-26 16:27:31 -0700919 dirtyClip();
920
Romain Guyeb993562010-10-05 18:14:38 -0700921 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craik3f0854292014-04-15 16:18:08 -0700922 layer->setConvexMask(NULL);
Romain Guy8550c4c2010-10-08 15:49:53 -0700923 if (!mCaches.layerCache.put(layer)) {
Andreas Gampeedaecc12014-11-10 20:54:07 -0800924 if (kDebugLayers) {
925 ALOGD("Deleting layer");
926 }
John Reck0e89e2b2014-10-31 14:49:06 -0700927 layer->decStrong(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700928 }
929}
930
Romain Guyaa6c24c2011-04-28 18:40:04 -0700931void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700932 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700933
934 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700935 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700936 setupDrawWithTexture();
937 } else {
938 setupDrawWithExternalTexture();
939 }
940 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700941 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500942 setupDrawColorFilter(layer->getColorFilter());
943 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700944 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700945 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500946 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700947 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
948 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700949 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700950 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700951 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800952 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800953 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700954 layer->getWidth() == (uint32_t) rect.getWidth() &&
955 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800956 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
957 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700958
Romain Guyd21b6e12011-11-30 20:21:23 -0800959 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800960 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
961 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700962 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800963 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800964 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
965 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700966 }
967 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700968 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700969
970 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700971}
972
Romain Guy5b3b3522010-10-27 18:57:51 -0700973void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700974 if (layer->isTextureLayer()) {
975 EVENT_LOGD("composeTextureLayerRect");
976 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
977 drawTextureLayer(layer, rect);
978 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
979 } else {
980 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700981 const Rect& texCoords = layer->texCoords;
982 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
983 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700984
Romain Guy9ace8f52011-07-07 20:50:11 -0700985 float x = rect.left;
986 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -0800987 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700988 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700989 layer->getHeight() == (uint32_t) rect.getHeight();
990
991 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700992 // When we're swapping, the layer is already in screen coordinates
993 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800994 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
995 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700996 }
997
Romain Guyd21b6e12011-11-30 20:21:23 -0800998 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700999 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001000 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001001 }
1002
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001003 SkPaint layerPaint;
1004 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
1005 layerPaint.setXfermodeMode(layer->getMode());
1006 layerPaint.setColorFilter(layer->getColorFilter());
1007
1008 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001009 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001010 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001011 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001012 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001013
Romain Guyaa6c24c2011-04-28 18:40:04 -07001014 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001015 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001016}
1017
Chris Craik34416ea2013-04-15 16:08:28 -07001018/**
1019 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1020 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1021 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1022 * by saveLayer's restore
1023 */
Tom Hudson984162f2014-10-10 13:38:16 -04001024#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1025 DRAW_COMMAND; \
1026 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
1027 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1028 DRAW_COMMAND; \
1029 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1030 } \
Chris Craik34416ea2013-04-15 16:08:28 -07001031 }
1032
1033#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1034
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001035// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
1036// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
1037class LayerShader : public SkShader {
1038public:
1039 LayerShader(Layer* layer, const SkMatrix* localMatrix)
1040 : INHERITED(localMatrix)
1041 , mLayer(layer) {
1042 }
1043
1044 virtual bool asACustomShader(void** data) const {
1045 if (data) {
1046 *data = static_cast<void*>(mLayer);
1047 }
1048 return true;
1049 }
1050
1051 virtual bool isOpaque() const {
1052 return !mLayer->isBlend();
1053 }
1054
1055protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +00001056 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001057 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
1058 }
1059
1060 virtual void flatten(SkWriteBuffer&) const {
1061 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1062 }
1063
1064 virtual Factory getFactory() const {
1065 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
1066 return NULL;
1067 }
1068private:
1069 // Unowned.
1070 Layer* mLayer;
1071 typedef SkShader INHERITED;
1072};
1073
Romain Guy5b3b3522010-10-27 18:57:51 -07001074void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001075 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1076
1077 if (layer->getConvexMask()) {
1078 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1079
1080 // clip to the area of the layer the mask can be larger
1081 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1082
1083 SkPaint paint;
1084 paint.setAntiAlias(true);
1085 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1086
Chris Craik3f0854292014-04-15 16:18:08 -07001087 // create LayerShader to map SaveLayer content into subsequent draw
1088 SkMatrix shaderMatrix;
1089 shaderMatrix.setTranslate(rect.left, rect.bottom);
1090 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001091 LayerShader layerShader(layer, &shaderMatrix);
1092 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001093
1094 // Since the drawing primitive is defined in local drawing space,
1095 // we don't need to modify the draw matrix
1096 const SkPath* maskPath = layer->getConvexMask();
1097 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1098
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001099 paint.setShader(NULL);
Chris Craik3f0854292014-04-15 16:18:08 -07001100 restore();
1101
1102 return;
1103 }
1104
Romain Guy5b3b3522010-10-27 18:57:51 -07001105 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001106 layer->setRegionAsRect();
1107
Chris Craik34416ea2013-04-15 16:08:28 -07001108 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001109
Romain Guy5b3b3522010-10-27 18:57:51 -07001110 layer->region.clear();
1111 return;
1112 }
1113
Chris Craik62d307c2014-07-29 10:35:13 -07001114 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001115 // standard Region based draw
1116 size_t count;
1117 const android::Rect* rects;
1118 Region safeRegion;
1119 if (CC_LIKELY(hasRectToRectTransform())) {
1120 rects = layer->region.getArray(&count);
1121 } else {
1122 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1123 rects = safeRegion.getArray(&count);
1124 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001125
Chris Craik3f0854292014-04-15 16:18:08 -07001126 const float alpha = getLayerAlpha(layer);
1127 const float texX = 1.0f / float(layer->getWidth());
1128 const float texY = 1.0f / float(layer->getHeight());
1129 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001130
Chris Craik3f0854292014-04-15 16:18:08 -07001131 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001132
Chris Craik3f0854292014-04-15 16:18:08 -07001133 // We must get (and therefore bind) the region mesh buffer
1134 // after we setup drawing in case we need to mess with the
1135 // stencil buffer in setupDraw()
1136 TextureVertex* mesh = mCaches.getRegionMesh();
1137 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001138
Chris Craik3f0854292014-04-15 16:18:08 -07001139 setupDrawWithTexture();
1140 setupDrawColor(alpha, alpha, alpha, alpha);
1141 setupDrawColorFilter(layer->getColorFilter());
1142 setupDrawBlending(layer);
1143 setupDrawProgram();
1144 setupDrawDirtyRegionsDisabled();
1145 setupDrawPureColorUniforms();
1146 setupDrawColorFilterUniforms(layer->getColorFilter());
1147 setupDrawTexture(layer->getTexture());
1148 if (currentTransform()->isPureTranslate()) {
1149 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1150 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001151
Chris Craik3f0854292014-04-15 16:18:08 -07001152 layer->setFilter(GL_NEAREST);
1153 setupDrawModelView(kModelViewMode_Translate, false,
1154 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1155 } else {
1156 layer->setFilter(GL_LINEAR);
1157 setupDrawModelView(kModelViewMode_Translate, false,
1158 rect.left, rect.top, rect.right, rect.bottom);
1159 }
1160 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001161
Chris Craik3f0854292014-04-15 16:18:08 -07001162 for (size_t i = 0; i < count; i++) {
1163 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001164
Chris Craik3f0854292014-04-15 16:18:08 -07001165 const float u1 = r->left * texX;
1166 const float v1 = (height - r->top) * texY;
1167 const float u2 = r->right * texX;
1168 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001169
Chris Craik3f0854292014-04-15 16:18:08 -07001170 // TODO: Reject quads outside of the clip
1171 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1172 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1173 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1174 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001175
Chris Craik3f0854292014-04-15 16:18:08 -07001176 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001177
Chris Craik3f0854292014-04-15 16:18:08 -07001178 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001179 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1180 GL_UNSIGNED_SHORT, NULL));
Chris Craik3f0854292014-04-15 16:18:08 -07001181 numQuads = 0;
1182 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001183 }
Chris Craik3f0854292014-04-15 16:18:08 -07001184 }
1185
1186 if (numQuads > 0) {
1187 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1188 GL_UNSIGNED_SHORT, NULL));
1189 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001190
Romain Guy5b3b3522010-10-27 18:57:51 -07001191#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001192 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001193#endif
1194
Chris Craik3f0854292014-04-15 16:18:08 -07001195 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001196}
1197
Romain Guy3a3133d2011-02-01 22:59:58 -08001198#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001199void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001200 size_t count;
1201 const android::Rect* rects = region.getArray(&count);
1202
1203 uint32_t colors[] = {
1204 0x7fff0000, 0x7f00ff00,
1205 0x7f0000ff, 0x7fff00ff,
1206 };
1207
1208 int offset = 0;
1209 int32_t top = rects[0].top;
1210
1211 for (size_t i = 0; i < count; i++) {
1212 if (top != rects[i].top) {
1213 offset ^= 0x2;
1214 top = rects[i].top;
1215 }
1216
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001217 SkPaint paint;
1218 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001219 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001220 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001221 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001222}
Chris Craike63f7c622013-10-17 10:30:55 -07001223#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001224
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001225void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001226 Vector<float> rects;
1227
1228 SkRegion::Iterator it(region);
1229 while (!it.done()) {
1230 const SkIRect& r = it.rect();
1231 rects.push(r.fLeft);
1232 rects.push(r.fTop);
1233 rects.push(r.fRight);
1234 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001235 it.next();
1236 }
1237
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001238 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001239}
1240
Romain Guy5b3b3522010-10-27 18:57:51 -07001241void OpenGLRenderer::dirtyLayer(const float left, const float top,
1242 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001243 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001244 Rect bounds(left, top, right, bottom);
1245 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001246 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001247 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001248}
1249
1250void OpenGLRenderer::dirtyLayer(const float left, const float top,
1251 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001252 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001253 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001254 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001255 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001256}
1257
1258void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Tom Hudson984162f2014-10-10 13:38:16 -04001259 if (bounds.intersect(*mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001260 bounds.snapToPixelBoundaries();
1261 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1262 if (!dirty.isEmpty()) {
1263 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001264 }
1265 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001266}
1267
Chris Craik4063a0e2013-11-15 16:06:56 -08001268void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001269 GLsizei elementsCount = quadsCount * 6;
1270 while (elementsCount > 0) {
1271 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1272
Romain Guy3380cfd2013-08-15 16:57:57 -07001273 setupDrawIndexedVertices(&mesh[0].x);
Romain Guy448455f2013-07-22 13:57:50 -07001274 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1275
1276 elementsCount -= drawCount;
1277 // Though there are 4 vertices in a quad, we use 6 indices per
1278 // quad to draw with GL_TRIANGLES
1279 mesh += (drawCount / 6) * 4;
1280 }
1281}
1282
Romain Guy54be1cd2011-06-13 19:04:27 -07001283void OpenGLRenderer::clearLayerRegions() {
1284 const size_t count = mLayers.size();
1285 if (count == 0) return;
1286
Tom Hudson984162f2014-10-10 13:38:16 -04001287 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001288 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001289 // Doing several glScissor/glClear here can negatively impact
1290 // GPUs with a tiler architecture, instead we draw quads with
1291 // the Clear blending mode
1292
1293 // The list contains bounds that have already been clipped
1294 // against their initial clip rect, and the current clip
1295 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001296 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001297
Romain Guy448455f2013-07-22 13:57:50 -07001298 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001299 Vertex* vertex = mesh;
1300
1301 for (uint32_t i = 0; i < count; i++) {
1302 Rect* bounds = mLayers.itemAt(i);
1303
Romain Guy54be1cd2011-06-13 19:04:27 -07001304 Vertex::set(vertex++, bounds->left, bounds->top);
1305 Vertex::set(vertex++, bounds->right, bounds->top);
1306 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001307 Vertex::set(vertex++, bounds->right, bounds->bottom);
1308
1309 delete bounds;
1310 }
Romain Guye67307c2013-02-11 18:01:20 -08001311 // We must clear the list of dirty rects before we
1312 // call setupDraw() to prevent stencil setup to do
1313 // the same thing again
1314 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001315
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001316 SkPaint clearPaint;
1317 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1318
Romain Guy54be1cd2011-06-13 19:04:27 -07001319 setupDraw(false);
1320 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001321 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001322 setupDrawProgram();
1323 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001324 setupDrawModelView(kModelViewMode_Translate, false,
1325 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001326
Chris Craik4063a0e2013-11-15 16:06:56 -08001327 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001328
1329 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001330 } else {
1331 for (uint32_t i = 0; i < count; i++) {
1332 delete mLayers.itemAt(i);
1333 }
Romain Guye67307c2013-02-11 18:01:20 -08001334 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001335 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001336}
1337
Romain Guybd6b79b2010-06-26 00:13:53 -07001338///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001339// State Deferral
1340///////////////////////////////////////////////////////////////////////////////
1341
Chris Craikff785832013-03-08 13:12:16 -08001342bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Tom Hudson984162f2014-10-10 13:38:16 -04001343 const Rect* currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001344 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001345
Chris Craikff785832013-03-08 13:12:16 -08001346 if (stateDeferFlags & kStateDeferFlag_Draw) {
1347 // state has bounds initialized in local coordinates
1348 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001349 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001350 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001351 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1352 // is used, it should more closely duplicate the quickReject logic (in how it uses
1353 // snapToPixelBoundaries)
1354
Chris Craikd6b65f62014-01-01 14:45:21 -08001355 if(!clippedBounds.intersect(*currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001356 // quick rejected
1357 return true;
1358 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001359
Chris Craika02c4ed2013-06-14 13:43:58 -07001360 state.mClipSideFlags = kClipSide_None;
Chris Craikd6b65f62014-01-01 14:45:21 -08001361 if (!currentClip->contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001362 int& flags = state.mClipSideFlags;
1363 // op partially clipped, so record which sides are clipped for clip-aware merging
Chris Craikd6b65f62014-01-01 14:45:21 -08001364 if (currentClip->left > state.mBounds.left) flags |= kClipSide_Left;
1365 if (currentClip->top > state.mBounds.top) flags |= kClipSide_Top;
1366 if (currentClip->right < state.mBounds.right) flags |= kClipSide_Right;
1367 if (currentClip->bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001368 }
1369 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001370 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001371 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1372 // overdraw avoidance (since we don't know what it overlaps)
1373 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikd6b65f62014-01-01 14:45:21 -08001374 state.mBounds.set(*currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001375 }
1376 }
1377
Chris Craik527a3aa2013-03-04 10:19:31 -08001378 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1379 if (state.mClipValid) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001380 state.mClip.set(*currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001381 }
1382
Chris Craik7273daa2013-03-28 11:25:24 -07001383 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1384 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001385 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001386 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001387 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001388
1389 // always store/restore, since it's just a pointer
1390 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001391 return false;
1392}
1393
Chris Craik527a3aa2013-03-04 10:19:31 -08001394void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001395 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001396 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001397 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001398 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001399
Chris Craik527a3aa2013-03-04 10:19:31 -08001400 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001401 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001402 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001403 dirtyClip();
1404 }
Chris Craikc3566d02013-02-04 16:16:33 -08001405}
1406
Chris Craik28ce94a2013-05-31 11:38:03 -07001407/**
1408 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1409 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1410 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1411 *
1412 * This method should be called when restoreDisplayState() won't be restoring the clip
1413 */
1414void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1415 if (clipRect != NULL) {
Tom Hudson984162f2014-10-10 13:38:16 -04001416 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001417 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001418 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001419 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001420 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001421 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001422}
1423
Chris Craikc3566d02013-02-04 16:16:33 -08001424///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001425// Clipping
1426///////////////////////////////////////////////////////////////////////////////
1427
Romain Guybb9524b2010-06-22 18:56:38 -07001428void OpenGLRenderer::setScissorFromClip() {
Tom Hudson984162f2014-10-10 13:38:16 -04001429 Rect clip(*mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001430 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001431
Chris Craika64a2be2014-05-14 14:17:01 -07001432 if (mCaches.setScissor(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001433 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001434 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001435 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001436}
1437
Romain Guy8ce00302013-01-15 18:51:42 -08001438void OpenGLRenderer::ensureStencilBuffer() {
1439 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1440 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1441 // just hope we have one when hasLayer() returns false.
1442 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001443 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001444 }
1445}
1446
1447void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1448 // The layer's FBO is already bound when we reach this stage
1449 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001450 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1451 // is attached after we initiated tiling. We must turn it off,
1452 // attach the new render buffer then turn tiling back on
1453 endTiling();
1454
Romain Guy8d4aeb72013-02-12 16:08:55 -08001455 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001456 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001457 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001458
Romain Guyf735c8e2013-01-31 17:45:55 -08001459 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001460 }
1461}
1462
1463void OpenGLRenderer::setStencilFromClip() {
1464 if (!mCaches.debugOverdraw) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001465 if (!currentSnapshot()->clipRegion->isEmpty()) {
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
1474 mCaches.stencil.enableWrite();
1475
Chris Craikdeeda3d2014-05-05 19:09:33 -07001476 // Clear and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001477 // to the region's bounds
1478 bool resetScissor = mCaches.enableScissor();
1479 if (resetScissor) {
1480 // The scissor was not set so we now need to update it
1481 setScissorFromClip();
1482 }
1483 mCaches.stencil.clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001484
1485 // stash and disable the outline clip state, since stencil doesn't account for outline
1486 bool storedSkipOutlineClip = mSkipOutlineClip;
1487 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001488
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001489 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001490 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001491 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1492
Romain Guy8ce00302013-01-15 18:51:42 -08001493 // NOTE: We could use the region contour path to generate a smaller mesh
1494 // Since we are using the stencil we could use the red book path
1495 // drawing technique. It might increase bandwidth usage though.
1496
1497 // The last parameter is important: we are not drawing in the color buffer
1498 // so we don't want to dirty the current layer, if any
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001499 drawRegionRects(*(currentSnapshot()->clipRegion), paint, false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001500 if (resetScissor) mCaches.disableScissor();
1501 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001502
1503 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001504
1505 // Draw the region used to generate the stencil if the appropriate debug
1506 // mode is enabled
1507 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001508 paint.setColor(0x7f0000ff);
1509 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
1510 drawRegionRects(*(currentSnapshot()->clipRegion), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001511 }
Romain Guy8ce00302013-01-15 18:51:42 -08001512 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001513 EVENT_LOGD("setStencilFromClip - disabling");
Romain Guy8ce00302013-01-15 18:51:42 -08001514 mCaches.stencil.disable();
1515 }
1516 }
1517}
1518
Chris Craikf0a59072013-11-19 18:00:46 -08001519/**
1520 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1521 *
1522 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1523 * style, and tessellated AA ramp
1524 */
1525bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001526 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001527 bool snapOut = paint && paint->isAntiAlias();
1528
1529 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1530 float outset = paint->getStrokeWidth() * 0.5f;
1531 left -= outset;
1532 top -= outset;
1533 right += outset;
1534 bottom += outset;
1535 }
1536
Chris Craikdeeda3d2014-05-05 19:09:33 -07001537 bool clipRequired = false;
1538 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001539 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001540 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001541 return true;
1542 }
1543
Chris Craikf23b25a2014-06-26 15:46:20 -07001544 // not quick rejected, so enable the scissor if clipRequired
1545 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1546 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001547 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001548}
1549
Romain Guy8ce00302013-01-15 18:51:42 -08001550void OpenGLRenderer::debugClip() {
1551#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001552 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001553 SkPaint paint;
1554 paint.setColor(0x7f00ff00);
1555 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1556
Romain Guy8ce00302013-01-15 18:51:42 -08001557 }
1558#endif
1559}
1560
Romain Guyf6a11b82010-06-23 17:47:49 -07001561///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001562// Drawing commands
1563///////////////////////////////////////////////////////////////////////////////
1564
Chris Craik62d307c2014-07-29 10:35:13 -07001565void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001566 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001567 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001568 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001569 // Make sure setScissor & setStencil happen at the beginning of
1570 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001571 if (mState.getDirtyClip()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001572 if (mCaches.scissorEnabled) {
1573 setScissorFromClip();
1574 }
Chris Craik62d307c2014-07-29 10:35:13 -07001575
Dohyun Leeadc0d9d2014-11-24 21:08:15 +09001576 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001577 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001578
Romain Guy70ca14e2010-12-13 18:24:33 -08001579 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001580
Romain Guy70ca14e2010-12-13 18:24:33 -08001581 mSetShaderColor = false;
1582 mColorSet = false;
1583 mColorA = mColorR = mColorG = mColorB = 0.0f;
1584 mTextureUnit = 0;
1585 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001586
1587 // Enable debug highlight when what we're about to draw is tested against
1588 // the stencil buffer and if stencil highlight debugging is on
1589 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1590 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1591 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001592}
1593
1594void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1595 mDescription.hasTexture = true;
1596 mDescription.hasAlpha8Texture = isAlpha8;
1597}
1598
Romain Guyff316ec2013-02-13 18:39:43 -08001599void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1600 mDescription.hasTexture = true;
1601 mDescription.hasColors = true;
1602 mDescription.hasAlpha8Texture = isAlpha8;
1603}
1604
Romain Guyaa6c24c2011-04-28 18:40:04 -07001605void OpenGLRenderer::setupDrawWithExternalTexture() {
1606 mDescription.hasExternalTexture = true;
1607}
1608
Romain Guy15bc6432011-12-13 13:11:32 -08001609void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001610 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001611}
1612
Chris Craik91a8c7c2014-08-12 14:31:35 -07001613void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1614 mDescription.hasVertexAlpha = true;
1615 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001616}
1617
Romain Guy8d0d4782010-12-14 20:13:35 -08001618void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1619 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001620 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1621 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1622 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001623 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001624 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001625}
1626
Romain Guy86568192010-12-14 15:55:39 -08001627void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1628 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001629 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1630 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1631 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001632 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001633 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001634}
1635
Romain Guy41210632012-07-16 17:04:24 -07001636void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1637 mCaches.fontRenderer->describe(mDescription, paint);
1638}
1639
Romain Guy70ca14e2010-12-13 18:24:33 -08001640void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1641 mColorA = a;
1642 mColorR = r;
1643 mColorG = g;
1644 mColorB = b;
1645 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001646 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001647}
1648
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001649void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
1650 if (shader != NULL) {
1651 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001652 }
1653}
1654
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001655void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
1656 if (filter == NULL) {
1657 return;
1658 }
1659
1660 SkXfermode::Mode mode;
1661 if (filter->asColorMode(NULL, &mode)) {
1662 mDescription.colorOp = ProgramDescription::kColorBlend;
1663 mDescription.colorMode = mode;
1664 } else if (filter->asColorMatrix(NULL)) {
1665 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001666 }
1667}
1668
Romain Guyf09ef512011-05-27 11:43:46 -07001669void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1670 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1671 mColorA = 1.0f;
1672 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001673 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001674 }
1675}
1676
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001677void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1678 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001679 // When the blending mode is kClear_Mode, we need to use a modulate color
1680 // argb=1,0,0,0
1681 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001682 // TODO: check shader blending, once we have shader drawing support for layers.
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001683 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001684 (mColorSet && mColorA < 1.0f) || isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001685 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001686}
1687
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001688void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1689 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001690 // When the blending mode is kClear_Mode, we need to use a modulate color
1691 // argb=1,0,0,0
1692 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001693 blend |= (mColorSet && mColorA < 1.0f) ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001694 (getShader(paint) && !getShader(paint)->isOpaque()) ||
1695 isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001696 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001697}
1698
1699void OpenGLRenderer::setupDrawProgram() {
1700 useProgram(mCaches.programCache.get(mDescription));
Chris Craikdeeda3d2014-05-05 19:09:33 -07001701 if (mDescription.hasRoundRectClip) {
1702 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001703 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001704 const Rect& innerRect = state->innerRect;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001705 glUniform4f(mCaches.currentProgram->getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001706 innerRect.left, innerRect.top,
1707 innerRect.right, innerRect.bottom);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001708 glUniformMatrix4fv(mCaches.currentProgram->getUniform("roundRectInvTransform"),
1709 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001710
1711 // add half pixel to round out integer rect space to cover pixel centers
1712 float roundedOutRadius = state->radius + 0.5f;
1713 glUniform1f(mCaches.currentProgram->getUniform("roundRectRadius"),
1714 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001715 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001716}
1717
1718void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1719 mTrackDirtyRegions = false;
1720}
1721
Chris Craik4063a0e2013-11-15 16:06:56 -08001722void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1723 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001724 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001725 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001726 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001727 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001728
Romain Guy86568192010-12-14 15:55:39 -08001729 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001730 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Tom Hudson984162f2014-10-10 13:38:16 -04001731 mCaches.currentProgram->set(writableSnapshot()->getOrthoMatrix(), mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001732 if (dirty && mTrackDirtyRegions) {
1733 if (!ignoreTransform) {
1734 dirtyLayer(left, top, right, bottom, *currentTransform());
1735 } else {
1736 dirtyLayer(left, top, right, bottom);
1737 }
Romain Guy86568192010-12-14 15:55:39 -08001738 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001739}
1740
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001741void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1742 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001743 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1744 }
1745}
1746
Romain Guy86568192010-12-14 15:55:39 -08001747void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001748 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001749 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001750 }
1751}
1752
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001753void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
1754 if (shader == NULL) {
1755 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001756 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001757
1758 if (ignoreTransform) {
1759 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1760 // because it was built into modelView / the geometry, and the description needs to
1761 // compensate.
1762 mat4 modelViewWithoutTransform;
1763 modelViewWithoutTransform.loadInverse(*currentTransform());
1764 modelViewWithoutTransform.multiply(mModelViewMatrix);
1765 mModelViewMatrix.load(modelViewWithoutTransform);
1766 }
1767
1768 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001769}
1770
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001771void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
1772 if (NULL == filter) {
1773 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001774 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001775
1776 SkColor color;
1777 SkXfermode::Mode mode;
1778 if (filter->asColorMode(&color, &mode)) {
1779 const int alpha = SkColorGetA(color);
1780 const GLfloat a = alpha / 255.0f;
1781 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1782 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1783 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1784 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1785 return;
1786 }
1787
1788 SkScalar srcColorMatrix[20];
1789 if (filter->asColorMatrix(srcColorMatrix)) {
1790
1791 float colorMatrix[16];
1792 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1793 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1794 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1795 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1796
1797 // Skia uses the range [0..255] for the addition vector, but we need
1798 // the [0..1] range to apply the vector in GLSL
1799 float colorVector[4];
1800 colorVector[0] = srcColorMatrix[4] / 255.0f;
1801 colorVector[1] = srcColorMatrix[9] / 255.0f;
1802 colorVector[2] = srcColorMatrix[14] / 255.0f;
1803 colorVector[3] = srcColorMatrix[19] / 255.0f;
1804
1805 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1806 GL_FALSE, colorMatrix);
1807 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1808 return;
1809 }
1810
1811 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001812}
1813
Romain Guy41210632012-07-16 17:04:24 -07001814void OpenGLRenderer::setupDrawTextGammaUniforms() {
1815 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1816}
1817
Romain Guy70ca14e2010-12-13 18:24:33 -08001818void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001819 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001820 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001821 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001822}
1823
1824void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001825 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001826 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001827 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001828}
1829
Romain Guyaa6c24c2011-04-28 18:40:04 -07001830void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1831 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001832 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001833 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001834}
1835
Romain Guy8f0095c2011-05-02 17:24:22 -07001836void OpenGLRenderer::setupDrawTextureTransform() {
1837 mDescription.hasTextureTransform = true;
1838}
1839
1840void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001841 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1842 GL_FALSE, &transform.data[0]);
1843}
1844
Chris Craik564acf72014-01-02 16:46:18 -08001845void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1846 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001847 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001848 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001849 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001850 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001851 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001852 }
Romain Guyd71dd362011-12-12 19:03:35 -08001853
Chris Craikcb4d6002012-09-25 12:00:29 -07001854 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001855 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001856 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001857 }
1858
1859 mCaches.unbindIndicesBuffer();
1860}
1861
Chris Craik564acf72014-01-02 16:46:18 -08001862void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1863 const GLvoid* texCoords, const GLvoid* colors) {
Romain Guyff316ec2013-02-13 18:39:43 -08001864 bool force = mCaches.unbindMeshBuffer();
1865 GLsizei stride = sizeof(ColorTextureVertex);
1866
1867 mCaches.bindPositionVertexPointer(force, vertices, stride);
1868 if (mCaches.currentProgram->texCoords >= 0) {
1869 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1870 }
1871 int slot = mCaches.currentProgram->getAttrib("colors");
1872 if (slot >= 0) {
1873 glEnableVertexAttribArray(slot);
1874 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1875 }
1876
1877 mCaches.unbindIndicesBuffer();
1878}
1879
Chris Craik564acf72014-01-02 16:46:18 -08001880void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1881 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001882 bool force = false;
1883 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1884 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1885 // use the default VBO found in Caches
1886 if (!vertices || vbo) {
1887 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1888 } else {
1889 force = mCaches.unbindMeshBuffer();
1890 }
ztenghui63d41ab2014-02-14 13:13:41 -08001891 mCaches.bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001892
Chris Craikcb4d6002012-09-25 12:00:29 -07001893 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001894 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001895 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001896 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001897}
1898
Romain Guy448455f2013-07-22 13:57:50 -07001899void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001900 bool force = mCaches.unbindMeshBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08001901 mCaches.bindQuadIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001902 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001903}
1904
Romain Guy70ca14e2010-12-13 18:24:33 -08001905///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001906// Drawing
1907///////////////////////////////////////////////////////////////////////////////
1908
Tom Hudson107843d2014-09-08 11:26:26 -04001909void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001910 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1911 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001912 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001913 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001914 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001915 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001916 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001917 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001918 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001919 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001920 }
1921
Chris Craikef8d6f22014-12-17 11:10:28 -08001922 // Don't avoid overdraw when visualizing, since that makes it harder to
1923 // debug where it's coming from, and when the problem occurs.
1924 bool avoidOverdraw = !mCaches.debugOverdraw;
Chris Craika5f09182014-12-17 15:11:30 -08001925 DeferredDisplayList deferredList(*mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001926 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001927 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001928
1929 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001930 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001931
Tom Hudson107843d2014-09-08 11:26:26 -04001932 deferredList.flush(*this, dirty);
1933 } else {
1934 // Even if there is no drawing command(Ex: invisible),
1935 // it still needs startFrame to clear buffer and start tiling.
1936 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001937 }
1938}
1939
Chris Craikd218a922014-01-02 17:13:34 -08001940void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, const SkPaint* paint) {
Romain Guya168d732011-03-18 16:50:13 -07001941 float x = left;
1942 float y = top;
1943
Romain Guy886b2752013-01-04 12:26:18 -08001944 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1945
Romain Guya168d732011-03-18 16:50:13 -07001946 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001947 if (currentTransform()->isPureTranslate()) {
1948 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1949 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001950 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001951
1952 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001953 } else {
Chris Craik678625242014-02-28 12:26:34 -08001954 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001955 }
1956
Romain Guy3b748a42013-04-17 18:54:38 -07001957 // No need to check for a UV mapper on the texture object, only ARGB_8888
1958 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001959 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001960 paint, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy3b748a42013-04-17 18:54:38 -07001961 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001962}
1963
Romain Guy03c00b52013-06-20 18:30:28 -07001964/**
1965 * Important note: this method is intended to draw batches of bitmaps and
1966 * will not set the scissor enable or dirty the current layer, if any.
1967 * The caller is responsible for properly dirtying the current layer.
1968 */
Tom Hudson107843d2014-09-08 11:26:26 -04001969void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001970 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1971 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001972 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001973 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001974 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001975
Chris Craik527a3aa2013-03-04 10:19:31 -08001976 const AutoTexture autoCleanup(texture);
1977
Chris Craik527a3aa2013-03-04 10:19:31 -08001978 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08001979 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08001980
1981 const float x = (int) floorf(bounds.left + 0.5f);
1982 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04001983 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001984 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001985 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001986 GL_TRIANGLES, bitmapCount * 6, true,
1987 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001988 } else {
1989 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001990 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001991 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
1992 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001993 }
1994
Tom Hudson107843d2014-09-08 11:26:26 -04001995 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08001996}
1997
Tom Hudson107843d2014-09-08 11:26:26 -04001998void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07001999 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002000 return;
Romain Guy6926c722010-07-12 20:20:03 -07002001 }
2002
Romain Guya1d3c912011-12-13 14:55:06 -08002003 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002004 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002005 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002006 const AutoTexture autoCleanup(texture);
2007
Mike Reed1103b322014-07-08 12:36:44 -04002008 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002009 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002010 } else {
Chris Craik79647502014-08-06 13:42:24 -07002011 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002012 }
Chet Haase48659092012-05-31 15:21:51 -07002013
Tom Hudson107843d2014-09-08 11:26:26 -04002014 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002015}
2016
Tom Hudson107843d2014-09-08 11:26:26 -04002017void OpenGLRenderer::drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002018 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002019 return;
Romain Guye651cc62012-05-14 19:44:40 -07002020 }
2021
2022 mCaches.activeTexture(0);
2023 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2024 const AutoTexture autoCleanup(texture);
2025
Mike Reed1103b322014-07-08 12:36:44 -04002026 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002027 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002028 } else {
Chris Craik79647502014-08-06 13:42:24 -07002029 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002030 }
Chet Haase48659092012-05-31 15:21:51 -07002031
Tom Hudson107843d2014-09-08 11:26:26 -04002032 mDirty = true;
Romain Guye651cc62012-05-14 19:44:40 -07002033}
2034
Tom Hudson107843d2014-09-08 11:26:26 -04002035void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002036 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002037 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002038 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002039 }
2040
Chris Craik39a908c2013-06-13 14:39:01 -07002041 // TODO: use quickReject on bounds from vertices
2042 mCaches.enableScissor();
2043
Romain Guyb18d2d02011-02-10 15:52:54 -08002044 float left = FLT_MAX;
2045 float top = FLT_MAX;
2046 float right = FLT_MIN;
2047 float bottom = FLT_MIN;
2048
Romain Guya92bb4d2012-10-16 11:08:44 -07002049 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002050
Chris Craik564acf72014-01-02 16:46:18 -08002051 Vector<ColorTextureVertex> mesh; // TODO: use C++11 unique_ptr
2052 mesh.setCapacity(count);
2053 ColorTextureVertex* vertex = mesh.editArray();
Romain Guyff316ec2013-02-13 18:39:43 -08002054
2055 bool cleanupColors = false;
2056 if (!colors) {
2057 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craikd218a922014-01-02 17:13:34 -08002058 int* newColors = new int[colorsCount];
2059 memset(newColors, 0xff, colorsCount * sizeof(int));
2060 colors = newColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002061 cleanupColors = true;
2062 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002063
Romain Guy3b748a42013-04-17 18:54:38 -07002064 mCaches.activeTexture(0);
John Reckebd52612014-12-10 16:47:36 -08002065 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002066 const UvMapper& mapper(getMapper(texture));
2067
Romain Guy5a7b4662011-01-20 19:09:30 -08002068 for (int32_t y = 0; y < meshHeight; y++) {
2069 for (int32_t x = 0; x < meshWidth; x++) {
2070 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2071
2072 float u1 = float(x) / meshWidth;
2073 float u2 = float(x + 1) / meshWidth;
2074 float v1 = float(y) / meshHeight;
2075 float v2 = float(y + 1) / meshHeight;
2076
Romain Guy3b748a42013-04-17 18:54:38 -07002077 mapper.map(u1, v1, u2, v2);
2078
Romain Guy5a7b4662011-01-20 19:09:30 -08002079 int ax = i + (meshWidth + 1) * 2;
2080 int ay = ax + 1;
2081 int bx = i;
2082 int by = bx + 1;
2083 int cx = i + 2;
2084 int cy = cx + 1;
2085 int dx = i + (meshWidth + 1) * 2 + 2;
2086 int dy = dx + 1;
2087
Romain Guyff316ec2013-02-13 18:39:43 -08002088 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2089 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2090 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002091
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[bx], vertices[by], u1, v1, colors[bx / 2]);
2094 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002095
Romain Guya92bb4d2012-10-16 11:08:44 -07002096 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2097 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2098 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2099 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002100 }
2101 }
2102
Chris Craikf0a59072013-11-19 18:00:46 -08002103 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002104 if (cleanupColors) delete[] colors;
Tom Hudson107843d2014-09-08 11:26:26 -04002105 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002106 }
2107
Romain Guyff316ec2013-02-13 18:39:43 -08002108 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002109 texture = mCaches.textureCache.get(bitmap);
2110 if (!texture) {
2111 if (cleanupColors) delete[] colors;
Tom Hudson107843d2014-09-08 11:26:26 -04002112 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002113 }
Romain Guyff316ec2013-02-13 18:39:43 -08002114 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002115 const AutoTexture autoCleanup(texture);
2116
2117 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002118 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002119
2120 int alpha;
2121 SkXfermode::Mode mode;
2122 getAlphaAndMode(paint, &alpha, &mode);
2123
Romain Guyff316ec2013-02-13 18:39:43 -08002124 float a = alpha / 255.0f;
2125
Romain Guya92bb4d2012-10-16 11:08:44 -07002126 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002127 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002128 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002129
Romain Guyff316ec2013-02-13 18:39:43 -08002130 setupDraw();
2131 setupDrawWithTextureAndColor();
2132 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002133 setupDrawColorFilter(getColorFilter(paint));
2134 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002135 setupDrawProgram();
2136 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002137 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002138 setupDrawTexture(texture->id);
2139 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002140 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002141 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002142
2143 glDrawArrays(GL_TRIANGLES, 0, count);
2144
Romain Guyff316ec2013-02-13 18:39:43 -08002145 int slot = mCaches.currentProgram->getAttrib("colors");
2146 if (slot >= 0) {
2147 glDisableVertexAttribArray(slot);
2148 }
2149
2150 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002151
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
Romain Guya1d3c912011-12-13 14:55:06 -08002163 mCaches.activeTexture(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
Romain Guy03750a02010-10-18 14:06:08 -07002178 mCaches.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,
Romain Guy886b2752013-01-04 12:26:18 -08002225 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2226 } 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,
Romain Guy886b2752013-01-04 12:26:18 -08002230 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2231 }
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)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002263 mCaches.activeTexture(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) {
Romain Guy03c00b52013-06-20 18:30:28 -07002317 mCaches.activeTexture(0);
2318 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();
Andreas Gampeedaecc12014-11-10 20:54:07 -08002362 mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002363 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002364 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002365
Chris Craik710f46d2012-09-17 17:25:49 -07002366 int alphaSlot = -1;
2367 if (isAA) {
2368 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2369 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002370 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002371 glEnableVertexAttribArray(alphaSlot);
2372 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002373 }
Romain Guy04299382012-07-18 17:15:41 -07002374
Chris Craik05f3d6e2014-06-02 16:27:04 -07002375 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2376 if (mode == VertexBuffer::kStandard) {
ztenghui63d41ab2014-02-14 13:13:41 -08002377 mCaches.unbindIndicesBuffer();
2378 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002379 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002380 mCaches.bindShadowIndicesBuffer();
ztenghui50ecf842014-03-11 16:52:30 -07002381 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002382 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
ztenghui50ecf842014-03-11 16:52:30 -07002383 mCaches.bindShadowIndicesBuffer();
2384 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
ztenghuid5e8ade2014-08-13 15:48:02 -07002385 } else if (mode == VertexBuffer::kIndices) {
2386 mCaches.unbindIndicesBuffer();
2387 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(), GL_UNSIGNED_SHORT,
2388 vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002389 }
Romain Guy04299382012-07-18 17:15:41 -07002390
Chris Craik710f46d2012-09-17 17:25:49 -07002391 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002392 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002393 }
Chris Craik65cd6122012-12-10 17:56:27 -08002394
Tom Hudson107843d2014-09-08 11:26:26 -04002395 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002396}
2397
2398/**
Chris Craik65cd6122012-12-10 17:56:27 -08002399 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2400 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2401 * screen space in all directions. However, instead of using a fragment shader to compute the
2402 * translucency of the color from its position, we simply use a varying parameter to define how far
2403 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2404 *
2405 * Doesn't yet support joins, caps, or path effects.
2406 */
Tom Hudson107843d2014-09-08 11:26:26 -04002407void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002408 VertexBuffer vertexBuffer;
2409 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002410 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002411 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002412}
2413
2414/**
2415 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2416 * and additional geometry for defining an alpha slope perimeter.
2417 *
2418 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2419 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2420 * in-shader alpha region, but found it to be taxing on some GPUs.
2421 *
2422 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2423 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002424 */
Tom Hudson107843d2014-09-08 11:26:26 -04002425void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002426 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002427
Chris Craik65cd6122012-12-10 17:56:27 -08002428 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002429
Chris Craik65cd6122012-12-10 17:56:27 -08002430 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002431 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2432 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002433
Chris Craik05f3d6e2014-06-02 16:27:04 -07002434 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002435 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002436 }
2437
Chris Craikbf759452014-08-11 16:00:44 -07002438 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002439 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002440}
2441
Tom Hudson107843d2014-09-08 11:26:26 -04002442void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002443 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002444
Chris Craik6d29c8d2013-05-08 18:35:44 -07002445 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002446
Chris Craik6d29c8d2013-05-08 18:35:44 -07002447 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002448 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002449
Chris Craik05f3d6e2014-06-02 16:27:04 -07002450 const Rect& bounds = buffer.getBounds();
2451 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002452 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002453 }
2454
Chris Craikbf759452014-08-11 16:00:44 -07002455 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002456 drawVertexBuffer(buffer, paint, displayFlags);
2457
2458 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002459}
2460
Tom Hudson107843d2014-09-08 11:26:26 -04002461void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002462 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002463 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002464
Tom Hudson984162f2014-10-10 13:38:16 -04002465 Rect clip(*mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002466 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002467
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002468 SkPaint paint;
2469 paint.setColor(color);
2470 paint.setXfermodeMode(mode);
2471
2472 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002473
Tom Hudson107843d2014-09-08 11:26:26 -04002474 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002475}
Romain Guy9d5316e2010-06-24 19:30:36 -07002476
Tom Hudson107843d2014-09-08 11:26:26 -04002477void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002478 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002479 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002480 const AutoTexture autoCleanup(texture);
2481
2482 const float x = left + texture->left - texture->offset;
2483 const float y = top + texture->top - texture->offset;
2484
2485 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002486
Tom Hudson107843d2014-09-08 11:26:26 -04002487 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002488}
2489
Tom Hudson107843d2014-09-08 11:26:26 -04002490void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002491 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002492 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002493 || quickRejectSetupScissor(left, top, right, bottom, p)
2494 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002495 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002496 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002497
Chris Craikcb4d6002012-09-25 12:00:29 -07002498 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002499 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002500 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002501 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002502 drawShape(left, top, texture, p);
2503 } else {
2504 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2505 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2506 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002507 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002508}
2509
Tom Hudson107843d2014-09-08 11:26:26 -04002510void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002511 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002512 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
2513 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002514 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002515 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002516 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002517 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002518 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002519 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002520 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002521 SkPath path;
2522 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2523 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2524 } else {
2525 path.addCircle(x, y, radius);
2526 }
2527 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002528 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002529}
Romain Guy01d58e42011-01-19 21:54:02 -08002530
Tom Hudson107843d2014-09-08 11:26:26 -04002531void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002532 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002533 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002534 || quickRejectSetupScissor(left, top, right, bottom, p)
2535 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002536 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002537 }
Romain Guy01d58e42011-01-19 21:54:02 -08002538
Chris Craikcb4d6002012-09-25 12:00:29 -07002539 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002540 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002541 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002542 drawShape(left, top, texture, p);
2543 } else {
2544 SkPath path;
2545 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2546 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2547 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2548 }
2549 path.addOval(rect);
2550 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002551 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002552}
2553
Tom Hudson107843d2014-09-08 11:26:26 -04002554void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002555 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002556 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002557 || quickRejectSetupScissor(left, top, right, bottom, p)
2558 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002559 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002560 }
2561
Chris Craik780c1282012-10-04 14:10:49 -07002562 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002563 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002564 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002565 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002566 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002567 drawShape(left, top, texture, p);
2568 return;
Chris Craik780c1282012-10-04 14:10:49 -07002569 }
Chris Craik780c1282012-10-04 14:10:49 -07002570 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2571 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2572 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2573 }
2574
2575 SkPath path;
2576 if (useCenter) {
2577 path.moveTo(rect.centerX(), rect.centerY());
2578 }
2579 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2580 if (useCenter) {
2581 path.close();
2582 }
Tom Hudson107843d2014-09-08 11:26:26 -04002583 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002584}
2585
Romain Guycf8675e2012-10-02 12:32:25 -07002586// See SkPaintDefaults.h
2587#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2588
Tom Hudson107843d2014-09-08 11:26:26 -04002589void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002590 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002591 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002592 || quickRejectSetupScissor(left, top, right, bottom, p)
2593 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002594 return;
Romain Guy6926c722010-07-12 20:20:03 -07002595 }
2596
Chris Craik710f46d2012-09-17 17:25:49 -07002597 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002598 // only fill style is supported by drawConvexPath, since others have to handle joins
2599 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2600 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2601 mCaches.activeTexture(0);
2602 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002603 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002604 drawShape(left, top, texture, p);
2605 } else {
2606 SkPath path;
2607 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2608 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2609 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2610 }
2611 path.addRect(rect);
2612 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002613 }
Chet Haase858aa932011-05-12 09:06:00 -07002614 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002615 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2616 SkPath path;
2617 path.addRect(left, top, right, bottom);
2618 drawConvexPath(path, p);
2619 } else {
2620 drawColorRect(left, top, right, bottom, p);
2621
2622 mDirty = true;
2623 }
Chet Haase858aa932011-05-12 09:06:00 -07002624 }
Romain Guyc7d53492010-06-25 13:41:57 -07002625}
Romain Guy9d5316e2010-06-24 19:30:36 -07002626
Chris Craikd218a922014-01-02 17:13:34 -08002627void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2628 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002629 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002630 mCaches.activeTexture(0);
2631
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002632 TextShadow textShadow;
2633 if (!getTextShadow(paint, &textShadow)) {
2634 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2635 }
2636
Raph Levien416a8472012-07-19 22:48:17 -07002637 // NOTE: The drop shadow will not perform gamma correction
2638 // if shader-based correction is enabled
2639 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2640 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002641 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002642 // If the drop shadow exceeds the max texture size or couldn't be
2643 // allocated, skip drawing
2644 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002645 const AutoTexture autoCleanup(shadow);
2646
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002647 const float sx = x - shadow->left + textShadow.dx;
2648 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002649
Tom Hudson984162f2014-10-10 13:38:16 -04002650 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002651 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002652 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002653 }
2654
2655 setupDraw();
2656 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002657 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002658 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002659 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002660 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002661 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002662 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2663 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002664 setupDrawTexture(shadow->id);
2665 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002666 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002667 setupDrawShaderUniforms(getShader(paint));
Raph Levien416a8472012-07-19 22:48:17 -07002668 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2669
2670 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2671}
2672
Romain Guy768bffc2013-02-27 13:50:45 -08002673bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002674 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Romain Guy768bffc2013-02-27 13:50:45 -08002675 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2676}
2677
Tom Hudson107843d2014-09-08 11:26:26 -04002678void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002679 const float* positions, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002680 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002681 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002682 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002683
Romain Guy671d6cf2012-01-18 12:39:17 -08002684 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002685 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002686 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002687 }
2688
Chris Craik39a908c2013-06-13 14:39:01 -07002689 mCaches.enableScissor();
2690
Romain Guy671d6cf2012-01-18 12:39:17 -08002691 float x = 0.0f;
2692 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002693 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002694 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002695 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2696 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002697 }
2698
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002699 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002700 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002701
2702 int alpha;
2703 SkXfermode::Mode mode;
2704 getAlphaAndMode(paint, &alpha, &mode);
2705
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002706 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002707 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002708 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002709 }
2710
Romain Guy671d6cf2012-01-18 12:39:17 -08002711 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002712 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002713 if (pureTranslate && !linearFilter) {
2714 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2715 }
Romain Guy257ae352013-03-20 16:31:12 -07002716 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002717
Tom Hudson984162f2014-10-10 13:38:16 -04002718 const Rect* clip = pureTranslate ? writableSnapshot()->clipRect : &writableSnapshot()->getLocalClip();
Romain Guy671d6cf2012-01-18 12:39:17 -08002719 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2720
Romain Guy211370f2012-02-01 16:10:55 -08002721 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002722
Victoria Lease1e546812013-06-25 14:25:17 -07002723 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002724 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002725 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002726 if (hasActiveLayer) {
2727 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002728 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002729 }
2730 dirtyLayerUnchecked(bounds, getRegion());
2731 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002732 }
Chet Haase48659092012-05-31 15:21:51 -07002733
Tom Hudson107843d2014-09-08 11:26:26 -04002734 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002735}
2736
Chris Craik59744b72014-07-01 17:56:52 -07002737bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002738 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002739 outMatrix->setIdentity();
2740 return false;
2741 } else if (CC_UNLIKELY(transform.isPerspective())) {
2742 outMatrix->setIdentity();
2743 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002744 }
Chris Craik59744b72014-07-01 17:56:52 -07002745
2746 /**
Chris Craika736cd92014-08-04 13:18:38 -07002747 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2748 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002749 */
2750 float sx, sy;
2751 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002752 outMatrix->setScale(
2753 roundf(fmaxf(1.0f, sx)),
2754 roundf(fmaxf(1.0f, sy)));
2755 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002756}
2757
Tom Hudson984162f2014-10-10 13:38:16 -04002758int OpenGLRenderer::getSaveCount() const {
2759 return mState.getSaveCount();
2760}
2761
2762int OpenGLRenderer::save(int flags) {
2763 return mState.save(flags);
2764}
2765
2766void OpenGLRenderer::restore() {
2767 return mState.restore();
2768}
2769
2770void OpenGLRenderer::restoreToCount(int saveCount) {
2771 return mState.restoreToCount(saveCount);
2772}
2773
2774void OpenGLRenderer::translate(float dx, float dy, float dz) {
2775 return mState.translate(dx, dy, dz);
2776}
2777
2778void OpenGLRenderer::rotate(float degrees) {
2779 return mState.rotate(degrees);
2780}
2781
2782void OpenGLRenderer::scale(float sx, float sy) {
2783 return mState.scale(sx, sy);
2784}
2785
2786void OpenGLRenderer::skew(float sx, float sy) {
2787 return mState.skew(sx, sy);
2788}
2789
2790void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2791 mState.setMatrix(matrix);
2792}
2793
2794void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2795 mState.concatMatrix(matrix);
2796}
2797
2798bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2799 return mState.clipRect(left, top, right, bottom, op);
2800}
2801
2802bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2803 return mState.clipPath(path, op);
2804}
2805
2806bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2807 return mState.clipRegion(region, op);
2808}
2809
2810void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2811 mState.setClippingOutline(allocator, outline);
2812}
2813
2814void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2815 const Rect& rect, float radius, bool highPriority) {
2816 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2817}
2818
2819
2820
Tom Hudson107843d2014-09-08 11:26:26 -04002821void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002822 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002823 DrawOpMode drawOpMode) {
2824
Chris Craik527a3aa2013-03-04 10:19:31 -08002825 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002826 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2827 // drawing as ops from DeferredDisplayList are already filtered for these
Tom Hudson984162f2014-10-10 13:38:16 -04002828 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002829 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002830 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002831 }
Romain Guycac5fd32011-12-01 20:08:50 -08002832 }
2833
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002834 const float oldX = x;
2835 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002836
Chris Craikd6b65f62014-01-01 14:45:21 -08002837 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002838 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002839
Romain Guy211370f2012-02-01 16:10:55 -08002840 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002841 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2842 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002843 }
2844
Romain Guy86568192010-12-14 15:55:39 -08002845 int alpha;
2846 SkXfermode::Mode mode;
2847 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002848
Romain Guyc74f45a2013-02-26 19:10:14 -08002849 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2850
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002851 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002852 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002853 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002854 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002855 }
2856
Romain Guy19d4dd82013-03-04 11:14:26 -08002857 const bool hasActiveLayer = hasLayer();
2858
Romain Guy624234f2013-03-05 16:43:31 -08002859 // We only pass a partial transform to the font renderer. That partial
2860 // matrix defines how glyphs are rasterized. Typically we want glyphs
2861 // to be rasterized at their final size on screen, which means the partial
2862 // matrix needs to take the scale factor into account.
2863 // When a partial matrix is used to transform glyphs during rasterization,
2864 // the mesh is generated with the inverse transform (in the case of scale,
2865 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2866 // apply the full transform matrix at draw time in the vertex shader.
2867 // Applying the full matrix in the shader is the easiest way to handle
2868 // rotation and perspective and allows us to always generated quads in the
2869 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002870 SkMatrix fontTransform;
2871 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2872 || fabs(y - (int) y) > 0.0f
2873 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002874 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002875 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002876
Romain Guy624234f2013-03-05 16:43:31 -08002877 // TODO: Implement better clipping for scaled/rotated text
Tom Hudson984162f2014-10-10 13:38:16 -04002878 const Rect* clip = !pureTranslate ? NULL : mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002879 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 -07002880
Raph Levien996e57c2012-07-23 15:22:52 -07002881 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002882 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002883
2884 // don't call issuedrawcommand, do it at end of batch
2885 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002886 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002887 SkPaint paintCopy(*paint);
2888 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2889 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002890 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002891 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002892 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002893 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002894 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002895
Chris Craik527a3aa2013-03-04 10:19:31 -08002896 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002897 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002898 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002899 }
Chris Craik41541822013-05-03 16:35:54 -07002900 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002901 }
Romain Guy694b5192010-07-21 21:33:20 -07002902
Chris Craike63f7c622013-10-17 10:30:55 -07002903 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002904
Tom Hudson107843d2014-09-08 11:26:26 -04002905 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002906}
2907
Tom Hudson107843d2014-09-08 11:26:26 -04002908void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002909 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002910 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002911 return;
Romain Guy03d58522012-02-24 17:54:07 -08002912 }
2913
Chris Craik39a908c2013-06-13 14:39:01 -07002914 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2915 mCaches.enableScissor();
2916
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002917 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002918 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002919 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002920
2921 int alpha;
2922 SkXfermode::Mode mode;
2923 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002924 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002925
Tom Hudson984162f2014-10-10 13:38:16 -04002926 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002927 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 -08002928
Romain Guy97771732012-02-28 18:17:02 -08002929 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002930
2931 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07002932 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002933 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002934 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002935 dirtyLayerUnchecked(bounds, getRegion());
2936 }
Romain Guy97771732012-02-28 18:17:02 -08002937 }
Chet Haase48659092012-05-31 15:21:51 -07002938
Tom Hudson107843d2014-09-08 11:26:26 -04002939 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002940}
2941
Tom Hudson107843d2014-09-08 11:26:26 -04002942void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002943 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002944
Romain Guya1d3c912011-12-13 14:55:06 -08002945 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002946
Romain Guyfb8b7632010-08-23 21:05:08 -07002947 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002948 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002949 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002950
Romain Guy8b55f372010-08-18 17:10:07 -07002951 const float x = texture->left - texture->offset;
2952 const float y = texture->top - texture->offset;
2953
Romain Guy01d58e42011-01-19 21:54:02 -08002954 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002955 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002956}
2957
Tom Hudson107843d2014-09-08 11:26:26 -04002958void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002959 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002960 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002961 }
2962
Romain Guyb2e2f242012-10-17 18:18:35 -07002963 mat4* transform = NULL;
2964 if (layer->isTextureLayer()) {
2965 transform = &layer->getTransform();
2966 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002967 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002968 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002969 }
2970 }
2971
Chris Craik39a908c2013-06-13 14:39:01 -07002972 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04002973 const bool rejected = mState.calculateQuickRejectForScissor(x, y,
Chris Craikdeeda3d2014-05-05 19:09:33 -07002974 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, NULL, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002975
2976 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002977 if (transform && !transform->isIdentity()) {
2978 restore();
2979 }
Tom Hudson107843d2014-09-08 11:26:26 -04002980 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002981 }
2982
Chris Craik62d307c2014-07-29 10:35:13 -07002983 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2984 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2985
Romain Guy5bb3c732012-11-29 17:52:58 -08002986 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002987
Chris Craik39a908c2013-06-13 14:39:01 -07002988 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08002989 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002990
Romain Guy211370f2012-02-01 16:10:55 -08002991 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002992 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002993 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2994 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002995 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002996
Chris Craik16ecda52013-03-29 10:59:59 -07002997 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002998 setupDraw();
2999 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003000 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003001 setupDrawColorFilter(layer->getColorFilter());
3002 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003003 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003004 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003005 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07003006 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08003007 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3008 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3009 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003010
Romain Guyd21b6e12011-11-30 20:21:23 -08003011 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003012 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003013 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003014 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003015 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003016 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003017 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3018 }
Romain Guyf219da52011-01-16 12:54:25 -08003019
Romain Guy448455f2013-07-22 13:57:50 -07003020 TextureVertex* mesh = &layer->mesh[0];
3021 GLsizei elementsCount = layer->meshElementCount;
3022
3023 while (elementsCount > 0) {
3024 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3025
Romain Guy3380cfd2013-08-15 16:57:57 -07003026 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003027 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3028 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3029
3030 elementsCount -= drawCount;
3031 // Though there are 4 vertices in a quad, we use 6 indices per
3032 // quad to draw with GL_TRIANGLES
3033 mesh += (drawCount / 6) * 4;
3034 }
Romain Guyf219da52011-01-16 12:54:25 -08003035
Romain Guy3a3133d2011-02-01 22:59:58 -08003036#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003037 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003038#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003039 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003040
Romain Guy5bb3c732012-11-29 17:52:58 -08003041 if (layer->debugDrawUpdate) {
3042 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003043
3044 SkPaint paint;
3045 paint.setColor(0x7f00ff00);
3046 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003047 }
Romain Guyf219da52011-01-16 12:54:25 -08003048 }
Chris Craik34416ea2013-04-15 16:08:28 -07003049 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003050
Romain Guyb2e2f242012-10-17 18:18:35 -07003051 if (transform && !transform->isIdentity()) {
3052 restore();
3053 }
3054
Tom Hudson107843d2014-09-08 11:26:26 -04003055 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003056}
3057
Romain Guy6926c722010-07-12 20:20:03 -07003058///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003059// Draw filters
3060///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003061void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3062 // We should never get here since we apply the draw filter when stashing
3063 // the paints in the DisplayList.
3064 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003065}
3066
3067///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003068// Drawing implementation
3069///////////////////////////////////////////////////////////////////////////////
3070
Chris Craikd218a922014-01-02 17:13:34 -08003071Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003072 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003073 if (!texture) {
3074 return mCaches.textureCache.get(bitmap);
3075 }
3076 return texture;
3077}
3078
Romain Guy01d58e42011-01-19 21:54:02 -08003079void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003080 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003081 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003082 return;
3083 }
3084
3085 int alpha;
3086 SkXfermode::Mode mode;
3087 getAlphaAndMode(paint, &alpha, &mode);
3088
3089 setupDraw();
3090 setupDrawWithTexture(true);
3091 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003092 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003093 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003094 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003095 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003096 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3097 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003098 setupDrawTexture(texture->id);
3099 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003100 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003101 setupDrawShaderUniforms(getShader(paint));
Romain Guy01d58e42011-01-19 21:54:02 -08003102 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3103
3104 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003105}
3106
Romain Guyf607bdc2010-09-10 19:20:06 -07003107// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003108#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3109#define kStdUnderline_Offset (1.0f / 9.0f)
3110#define kStdUnderline_Thickness (1.0f / 18.0f)
3111
Chris Craikd218a922014-01-02 17:13:34 -08003112void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3113 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003114 // Handle underline and strike-through
3115 uint32_t flags = paint->getFlags();
3116 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003117 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003118
Romain Guy211370f2012-02-01 16:10:55 -08003119 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003120 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003121 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003122
Raph Levien8b4072d2012-07-30 15:50:00 -07003123 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003124 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003125
Romain Guyf6834472011-01-23 13:32:12 -08003126 int linesCount = 0;
3127 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3128 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3129
3130 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003131 float points[pointsCount];
3132 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003133
3134 if (flags & SkPaint::kUnderlineText_Flag) {
3135 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003136 points[currentPoint++] = left;
3137 points[currentPoint++] = top;
3138 points[currentPoint++] = left + underlineWidth;
3139 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003140 }
3141
3142 if (flags & SkPaint::kStrikeThruText_Flag) {
3143 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003144 points[currentPoint++] = left;
3145 points[currentPoint++] = top;
3146 points[currentPoint++] = left + underlineWidth;
3147 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003148 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003149
Romain Guy726aeba2011-06-01 14:52:00 -07003150 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003151
Romain Guy726aeba2011-06-01 14:52:00 -07003152 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003153 }
3154 }
3155}
3156
Tom Hudson107843d2014-09-08 11:26:26 -04003157void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003158 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003159 return;
Romain Guy672433d2013-01-04 19:05:13 -08003160 }
3161
Tom Hudson107843d2014-09-08 11:26:26 -04003162 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003163}
3164
Tom Hudson107843d2014-09-08 11:26:26 -04003165void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003166 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003167 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003168
Chris Craik15a07a22014-01-26 13:43:53 -08003169 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craikf57776b2013-10-25 18:30:17 -07003170 mCaches.enableScissor();
3171
3172 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003173 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003174
ztenghui14a4e352014-08-13 10:44:39 -07003175 // The caller has made sure casterAlpha > 0.
3176 float ambientShadowAlpha = mAmbientShadowAlpha;
3177 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3178 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3179 }
3180 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3181 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003182 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003183 }
ztenghui7b4516e2014-01-07 10:42:55 -08003184
ztenghui14a4e352014-08-13 10:44:39 -07003185 float spotShadowAlpha = mSpotShadowAlpha;
3186 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3187 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3188 }
3189 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3190 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003191 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003192 }
ztenghui7b4516e2014-01-07 10:42:55 -08003193
Tom Hudson107843d2014-09-08 11:26:26 -04003194 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003195}
3196
Tom Hudson107843d2014-09-08 11:26:26 -04003197void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003198 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003199 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003200 return;
Romain Guy3b753822013-03-05 10:27:35 -08003201 }
Romain Guy735738c2012-12-03 12:34:51 -08003202
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003203 int color = paint->getColor();
3204 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003205 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003206 color |= 0x00ffffff;
3207 }
3208
Romain Guy672433d2013-01-04 19:05:13 -08003209 float left = FLT_MAX;
3210 float top = FLT_MAX;
3211 float right = FLT_MIN;
3212 float bottom = FLT_MIN;
3213
Romain Guy448455f2013-07-22 13:57:50 -07003214 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003215 Vertex* vertex = mesh;
3216
Chris Craik2af46352012-11-26 18:30:17 -08003217 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003218 float l = rects[index + 0];
3219 float t = rects[index + 1];
3220 float r = rects[index + 2];
3221 float b = rects[index + 3];
3222
Romain Guy3b753822013-03-05 10:27:35 -08003223 Vertex::set(vertex++, l, t);
3224 Vertex::set(vertex++, r, t);
3225 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003226 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003227
Romain Guy3b753822013-03-05 10:27:35 -08003228 left = fminf(left, l);
3229 top = fminf(top, t);
3230 right = fmaxf(right, r);
3231 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003232 }
3233
Chris Craikf0a59072013-11-19 18:00:46 -08003234 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003235 return;
Romain Guya362c692013-02-04 13:50:16 -08003236 }
Romain Guy672433d2013-01-04 19:05:13 -08003237
Romain Guy672433d2013-01-04 19:05:13 -08003238 setupDraw();
3239 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003240 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003241 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003242 setupDrawColorFilter(getColorFilter(paint));
3243 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003244 setupDrawProgram();
3245 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003246 setupDrawModelView(kModelViewMode_Translate, false,
3247 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003248 setupDrawColorUniforms(getShader(paint));
3249 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003250 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003251
Romain Guy8ce00302013-01-15 18:51:42 -08003252 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003253 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003254 }
3255
Chris Craik4063a0e2013-11-15 16:06:56 -08003256 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003257
Tom Hudson107843d2014-09-08 11:26:26 -04003258 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003259}
3260
Romain Guy026c5e162010-06-28 17:12:22 -07003261void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003262 const SkPaint* paint, bool ignoreTransform) {
3263 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003264 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003265 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003266 color |= 0x00ffffff;
3267 }
3268
Romain Guy70ca14e2010-12-13 18:24:33 -08003269 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003270 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003271 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003272 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003273 setupDrawColorFilter(getColorFilter(paint));
3274 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003275 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003276 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3277 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003278 setupDrawColorUniforms(getShader(paint));
3279 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003280 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003281 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003282
Romain Guyc95c8d62010-09-17 15:31:32 -07003283 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3284}
3285
Romain Guy82ba8142010-07-09 13:25:56 -07003286void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003287 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003288 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003289
Romain Guy3b748a42013-04-17 18:54:38 -07003290 GLvoid* vertices = (GLvoid*) NULL;
3291 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3292
3293 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003294 vertices = &mMeshVertices[0].x;
3295 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003296
3297 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3298 texture->uvMapper->map(uvs);
3299
3300 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3301 }
3302
Chris Craikd6b65f62014-01-01 14:45:21 -08003303 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3304 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3305 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003306
Romain Guyd21b6e12011-11-30 20:21:23 -08003307 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003308 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003309 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003310 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003311 } else {
Chris Craik678625242014-02-28 12:26:34 -08003312 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003313 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003314 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3315 }
3316
3317 if (texture->uvMapper) {
3318 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003319 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003320}
3321
Romain Guyf7f93552010-07-08 19:17:03 -07003322void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003323 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003324 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003325 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3326 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003327
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003328 int a;
3329 SkXfermode::Mode mode;
3330 getAlphaAndMode(paint, &a, &mode);
3331 const float alpha = a / 255.0f;
3332
Romain Guy746b7402010-10-26 16:27:31 -07003333 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003334 setupDrawWithTexture();
3335 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003336 setupDrawColorFilter(getColorFilter(paint));
3337 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003338 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003339 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003340 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003341 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003342 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003343 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003344 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003345
Romain Guy6820ac82010-09-15 18:11:50 -07003346 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003347}
3348
Romain Guy3b748a42013-04-17 18:54:38 -07003349void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003350 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003351 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003352 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3353 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003354
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003355 int a;
3356 SkXfermode::Mode mode;
3357 getAlphaAndMode(paint, &a, &mode);
3358 const float alpha = a / 255.0f;
3359
Romain Guy3b748a42013-04-17 18:54:38 -07003360 setupDraw();
3361 setupDrawWithTexture();
3362 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003363 setupDrawColorFilter(getColorFilter(paint));
3364 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003365 setupDrawProgram();
3366 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003367 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003368 setupDrawTexture(texture);
3369 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003370 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003371 setupDrawMeshIndices(vertices, texCoords, vbo);
3372
3373 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003374}
3375
Romain Guy886b2752013-01-04 12:26:18 -08003376void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003377 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003378 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003379 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003380
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003381 int color = paint != NULL ? paint->getColor() : 0;
3382 int alpha;
3383 SkXfermode::Mode mode;
3384 getAlphaAndMode(paint, &alpha, &mode);
3385
Romain Guy886b2752013-01-04 12:26:18 -08003386 setupDraw();
3387 setupDrawWithTexture(true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003388 if (paint != NULL) {
Romain Guy886b2752013-01-04 12:26:18 -08003389 setupDrawAlpha8Color(color, alpha);
3390 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003391 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003392 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003393 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003394 setupDrawProgram();
3395 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003396 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003397 setupDrawTexture(texture);
3398 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003399 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003400 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003401 setupDrawMesh(vertices, texCoords);
3402
3403 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003404}
3405
Romain Guya5aed0d2010-09-09 14:42:43 -07003406void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003407 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003408
Tom Hudson984162f2014-10-10 13:38:16 -04003409 if (writableSnapshot()->roundRectClipState != NULL /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003410 blend = true;
3411 mDescription.hasRoundRectClip = true;
3412 }
3413 mSkipOutlineClip = true;
3414
Romain Guy82ba8142010-07-09 13:25:56 -07003415 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003416
Romain Guy82ba8142010-07-09 13:25:56 -07003417 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003418 // These blend modes are not supported by OpenGL directly and have
3419 // to be implemented using shaders. Since the shader will perform
3420 // the blending, turn blending off here
3421 // If the blend mode cannot be implemented using shaders, fall
3422 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003423 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003424 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003425 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003426 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003427
Romain Guy82bc7a72012-01-03 14:13:39 -08003428 if (mCaches.blend) {
3429 glDisable(GL_BLEND);
3430 mCaches.blend = false;
3431 }
3432
3433 return;
3434 } else {
3435 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003436 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003437 }
3438
3439 if (!mCaches.blend) {
3440 glEnable(GL_BLEND);
3441 }
3442
3443 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3444 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3445
3446 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3447 glBlendFunc(sourceMode, destMode);
3448 mCaches.lastSrcMode = sourceMode;
3449 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003450 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003451 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003452 glDisable(GL_BLEND);
3453 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003454 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003455}
3456
Romain Guy889f8d12010-07-29 14:37:42 -07003457bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003458 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003459 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003460 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003461 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003462 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003463 }
Romain Guy6926c722010-07-12 20:20:03 -07003464 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003465}
3466
Romain Guy026c5e162010-06-28 17:12:22 -07003467void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003468 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003469 TextureVertex::setUV(v++, u1, v1);
3470 TextureVertex::setUV(v++, u2, v1);
3471 TextureVertex::setUV(v++, u1, v2);
3472 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003473}
3474
Chris Craikd218a922014-01-02 17:13:34 -08003475void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003476 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003477 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3478 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003479 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003480 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003481 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003482}
3483
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003484float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003485 float alpha;
3486 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3487 alpha = mDrawModifiers.mOverrideLayerAlpha;
3488 } else {
3489 alpha = layer->getAlpha() / 255.0f;
3490 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003491 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003492}
3493
Romain Guy9d5316e2010-06-24 19:30:36 -07003494}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003495}; // namespace android