blob: 1c4c3271ef2a6e940d9fa7cea4576e7ae3817a14 [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 Craik797b95b2014-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 Guy0f667532013-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());
Chris Craike84a2082014-12-22 14:28:49 -0800683 writableSnapshot()->roundRectClipState = nullptr;
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) {
Chris Craik07adacf2014-12-19 10:08:40 -0800743 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
744 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700745
Romain Guyeb993562010-10-05 18:14:38 -0700746 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
747
Romain Guyf607bdc2010-09-10 19:20:06 -0700748 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700749 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700750 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700751 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000752 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700753
754 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400755 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700756 return false;
757 }
Romain Guyf18fd992010-07-08 11:45:51 -0700758
Romain Guya1d3c912011-12-13 14:55:06 -0800759 mCaches.activeTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700760 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700761 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700762 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700763 }
764
Derek Sollenberger674554f2014-02-19 16:47:32 +0000765 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700766 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700767 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
768 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500769
Chet Haasea23eed82012-04-12 15:19:04 -0700770 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700771 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700772 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda57022010-07-06 11:39:32 -0700773
Romain Guy8fb95422010-08-17 18:38:51 -0700774 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400775 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
776 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700777
Chris Craika8bea8e2014-09-24 11:29:43 -0700778 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
779 fboLayer ? "" : "unclipped ",
780 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700781 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700782 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700783 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700784 } else {
785 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700786 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800787 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700788 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700789 // Workaround for some GL drivers. When reading pixels lying outside
790 // of the window we should get undefined values for those pixels.
791 // Unfortunately some drivers will turn the entire target texture black
792 // when reading outside of the window.
793 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800794 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700795 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800796 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700797
Chris Craika64a2be2014-05-14 14:17:01 -0700798 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
799 bounds.left, getViewportHeight() - bounds.bottom,
800 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700801
Romain Guy54be1cd2011-06-13 19:04:27 -0700802 // Enqueue the buffer coordinates to clear the corresponding region later
803 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700804 }
Romain Guyeb993562010-10-05 18:14:38 -0700805 }
Romain Guyf86ef572010-07-01 11:05:42 -0700806
Romain Guyd55a8612010-06-28 17:42:46 -0700807 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700808}
809
Chris Craike63f7c622013-10-17 10:30:55 -0700810bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800811 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700812 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700813
Tom Hudson984162f2014-10-10 13:38:16 -0400814 writableSnapshot()->region = &writableSnapshot()->layer->region;
815 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
816 writableSnapshot()->fbo = layer->getFbo();
817 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
818 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
819 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800820 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700821
Romain Guy2b7028e2012-09-19 17:25:38 -0700822 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700823 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700824 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700825 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700826 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700827
828 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700829 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700830 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700831 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700832 }
833
834 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700835 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700836
henry.uh_chen33f5a592014-07-02 19:36:56 +0800837 // Expand the startTiling region by 1
838 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700839
840 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700841 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800842 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700843 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700844 glClear(GL_COLOR_BUFFER_BIT);
845
846 dirtyClip();
847
848 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700849 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700850 return true;
851}
852
Romain Guy1c740bc2010-09-13 18:00:09 -0700853/**
854 * Read the documentation of createLayer() before doing anything in this method.
855 */
Chris Craik14e51302013-12-30 15:32:54 -0800856void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
857 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000858 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700859 return;
860 }
861
Chris Craik14e51302013-12-30 15:32:54 -0800862 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800863 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800864 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700865
Chris Craik39a908c2013-06-13 14:39:01 -0700866 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400867 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800868 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -0700869 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
870
Romain Guyeb993562010-10-05 18:14:38 -0700871 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700872 endTiling();
873
Romain Guye0aa84b2012-04-03 19:30:26 -0700874 // Detach the texture from the FBO
875 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800876
877 layer->removeFbo(false);
878
Romain Guyeb993562010-10-05 18:14:38 -0700879 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700880 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700881 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700882
Chris Craikd6b65f62014-01-01 14:45:21 -0800883 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700884 }
885
Romain Guy9ace8f52011-07-07 20:50:11 -0700886 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500887 SkPaint layerPaint;
888 layerPaint.setAlpha(layer->getAlpha());
889 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
890 layerPaint.setColorFilter(layer->getColorFilter());
891
892 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700893 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700894 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700895 }
896
Romain Guy03750a02010-10-18 14:06:08 -0700897 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700898
Romain Guya1d3c912011-12-13 14:55:06 -0800899 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700900
Romain Guy5b3b3522010-10-27 18:57:51 -0700901 // When the layer is stored in an FBO, we can save a bit of fillrate by
902 // drawing only the dirty region
903 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800904 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700905 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700906 } else if (!rect.isEmpty()) {
907 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530908
909 save(0);
910 // the layer contains screen buffer content that shouldn't be alpha modulated
911 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400912 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700913 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530914 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700915 }
Romain Guy8b55f372010-08-18 17:10:07 -0700916
Romain Guy746b7402010-10-26 16:27:31 -0700917 dirtyClip();
918
Romain Guyeb993562010-10-05 18:14:38 -0700919 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800920 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700921 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800922 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800923 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700924 }
925}
926
Romain Guyaa6c24c2011-04-28 18:40:04 -0700927void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700928 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700929
930 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700931 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700932 setupDrawWithTexture();
933 } else {
934 setupDrawWithExternalTexture();
935 }
936 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700937 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500938 setupDrawColorFilter(layer->getColorFilter());
939 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700940 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700941 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500942 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700943 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
944 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700945 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700946 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700947 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800948 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800949 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700950 layer->getWidth() == (uint32_t) rect.getWidth() &&
951 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800952 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
953 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700954
Romain Guyd21b6e12011-11-30 20:21:23 -0800955 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800956 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
957 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700958 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800959 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800960 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
961 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700962 }
963 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700964 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700965
966 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700967}
968
Romain Guy5b3b3522010-10-27 18:57:51 -0700969void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700970 if (layer->isTextureLayer()) {
971 EVENT_LOGD("composeTextureLayerRect");
972 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
973 drawTextureLayer(layer, rect);
974 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
975 } else {
976 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700977 const Rect& texCoords = layer->texCoords;
978 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
979 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700980
Romain Guy9ace8f52011-07-07 20:50:11 -0700981 float x = rect.left;
982 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -0800983 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700984 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700985 layer->getHeight() == (uint32_t) rect.getHeight();
986
987 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700988 // When we're swapping, the layer is already in screen coordinates
989 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800990 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
991 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700992 }
993
Romain Guyd21b6e12011-11-30 20:21:23 -0800994 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700995 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800996 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700997 }
998
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500999 SkPaint layerPaint;
1000 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
1001 layerPaint.setXfermodeMode(layer->getMode());
1002 layerPaint.setColorFilter(layer->getColorFilter());
1003
1004 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001005 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001006 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001007 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001008 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001009
Romain Guyaa6c24c2011-04-28 18:40:04 -07001010 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001011 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001012}
1013
Chris Craik34416ea2013-04-15 16:08:28 -07001014/**
1015 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1016 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1017 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1018 * by saveLayer's restore
1019 */
Tom Hudson984162f2014-10-10 13:38:16 -04001020#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1021 DRAW_COMMAND; \
1022 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
1023 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1024 DRAW_COMMAND; \
1025 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1026 } \
Chris Craik34416ea2013-04-15 16:08:28 -07001027 }
1028
1029#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1030
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001031// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
1032// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
1033class LayerShader : public SkShader {
1034public:
1035 LayerShader(Layer* layer, const SkMatrix* localMatrix)
1036 : INHERITED(localMatrix)
1037 , mLayer(layer) {
1038 }
1039
Chris Craike84a2082014-12-22 14:28:49 -08001040 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001041 if (data) {
1042 *data = static_cast<void*>(mLayer);
1043 }
1044 return true;
1045 }
1046
Chris Craike84a2082014-12-22 14:28:49 -08001047 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001048 return !mLayer->isBlend();
1049 }
1050
1051protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +00001052 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001053 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
1054 }
1055
Chris Craike84a2082014-12-22 14:28:49 -08001056 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001057 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1058 }
1059
Chris Craike84a2082014-12-22 14:28:49 -08001060 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001061 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -08001062 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001063 }
1064private:
1065 // Unowned.
1066 Layer* mLayer;
1067 typedef SkShader INHERITED;
1068};
1069
Romain Guy5b3b3522010-10-27 18:57:51 -07001070void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001071 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1072
1073 if (layer->getConvexMask()) {
1074 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1075
1076 // clip to the area of the layer the mask can be larger
1077 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1078
1079 SkPaint paint;
1080 paint.setAntiAlias(true);
1081 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1082
Chris Craik3f0854292014-04-15 16:18:08 -07001083 // create LayerShader to map SaveLayer content into subsequent draw
1084 SkMatrix shaderMatrix;
1085 shaderMatrix.setTranslate(rect.left, rect.bottom);
1086 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001087 LayerShader layerShader(layer, &shaderMatrix);
1088 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001089
1090 // Since the drawing primitive is defined in local drawing space,
1091 // we don't need to modify the draw matrix
1092 const SkPath* maskPath = layer->getConvexMask();
1093 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1094
Chris Craike84a2082014-12-22 14:28:49 -08001095 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -07001096 restore();
1097
1098 return;
1099 }
1100
Romain Guy5b3b3522010-10-27 18:57:51 -07001101 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001102 layer->setRegionAsRect();
1103
Chris Craik34416ea2013-04-15 16:08:28 -07001104 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001105
Romain Guy5b3b3522010-10-27 18:57:51 -07001106 layer->region.clear();
1107 return;
1108 }
1109
Chris Craik62d307c2014-07-29 10:35:13 -07001110 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001111 // standard Region based draw
1112 size_t count;
1113 const android::Rect* rects;
1114 Region safeRegion;
1115 if (CC_LIKELY(hasRectToRectTransform())) {
1116 rects = layer->region.getArray(&count);
1117 } else {
1118 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1119 rects = safeRegion.getArray(&count);
1120 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001121
Chris Craik3f0854292014-04-15 16:18:08 -07001122 const float alpha = getLayerAlpha(layer);
1123 const float texX = 1.0f / float(layer->getWidth());
1124 const float texY = 1.0f / float(layer->getHeight());
1125 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001126
Chris Craik3f0854292014-04-15 16:18:08 -07001127 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001128
Chris Craik3f0854292014-04-15 16:18:08 -07001129 // We must get (and therefore bind) the region mesh buffer
1130 // after we setup drawing in case we need to mess with the
1131 // stencil buffer in setupDraw()
1132 TextureVertex* mesh = mCaches.getRegionMesh();
1133 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001134
Chris Craik3f0854292014-04-15 16:18:08 -07001135 setupDrawWithTexture();
1136 setupDrawColor(alpha, alpha, alpha, alpha);
1137 setupDrawColorFilter(layer->getColorFilter());
1138 setupDrawBlending(layer);
1139 setupDrawProgram();
1140 setupDrawDirtyRegionsDisabled();
1141 setupDrawPureColorUniforms();
1142 setupDrawColorFilterUniforms(layer->getColorFilter());
1143 setupDrawTexture(layer->getTexture());
1144 if (currentTransform()->isPureTranslate()) {
1145 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1146 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001147
Chris Craik3f0854292014-04-15 16:18:08 -07001148 layer->setFilter(GL_NEAREST);
1149 setupDrawModelView(kModelViewMode_Translate, false,
1150 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1151 } else {
1152 layer->setFilter(GL_LINEAR);
1153 setupDrawModelView(kModelViewMode_Translate, false,
1154 rect.left, rect.top, rect.right, rect.bottom);
1155 }
1156 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001157
Chris Craik3f0854292014-04-15 16:18:08 -07001158 for (size_t i = 0; i < count; i++) {
1159 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001160
Chris Craik3f0854292014-04-15 16:18:08 -07001161 const float u1 = r->left * texX;
1162 const float v1 = (height - r->top) * texY;
1163 const float u2 = r->right * texX;
1164 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001165
Chris Craik3f0854292014-04-15 16:18:08 -07001166 // TODO: Reject quads outside of the clip
1167 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1168 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1169 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1170 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001171
Chris Craik3f0854292014-04-15 16:18:08 -07001172 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001173
Chris Craik3f0854292014-04-15 16:18:08 -07001174 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001175 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001176 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001177 numQuads = 0;
1178 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001179 }
Chris Craik3f0854292014-04-15 16:18:08 -07001180 }
1181
1182 if (numQuads > 0) {
1183 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001184 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001185 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001186
Romain Guy5b3b3522010-10-27 18:57:51 -07001187#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001188 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001189#endif
1190
Chris Craik3f0854292014-04-15 16:18:08 -07001191 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001192}
1193
Romain Guy3a3133d2011-02-01 22:59:58 -08001194#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001195void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001196 size_t count;
1197 const android::Rect* rects = region.getArray(&count);
1198
1199 uint32_t colors[] = {
1200 0x7fff0000, 0x7f00ff00,
1201 0x7f0000ff, 0x7fff00ff,
1202 };
1203
1204 int offset = 0;
1205 int32_t top = rects[0].top;
1206
1207 for (size_t i = 0; i < count; i++) {
1208 if (top != rects[i].top) {
1209 offset ^= 0x2;
1210 top = rects[i].top;
1211 }
1212
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001213 SkPaint paint;
1214 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001215 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001216 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001217 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001218}
Chris Craike63f7c622013-10-17 10:30:55 -07001219#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001220
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001221void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001222 Vector<float> rects;
1223
1224 SkRegion::Iterator it(region);
1225 while (!it.done()) {
1226 const SkIRect& r = it.rect();
1227 rects.push(r.fLeft);
1228 rects.push(r.fTop);
1229 rects.push(r.fRight);
1230 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001231 it.next();
1232 }
1233
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001234 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001235}
1236
Romain Guy5b3b3522010-10-27 18:57:51 -07001237void OpenGLRenderer::dirtyLayer(const float left, const float top,
1238 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001239 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001240 Rect bounds(left, top, right, bottom);
1241 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001242 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001243 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001244}
1245
1246void OpenGLRenderer::dirtyLayer(const float left, const float top,
1247 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001248 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001249 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001250 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001251 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001252}
1253
1254void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Tom Hudson984162f2014-10-10 13:38:16 -04001255 if (bounds.intersect(*mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001256 bounds.snapToPixelBoundaries();
1257 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1258 if (!dirty.isEmpty()) {
1259 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001260 }
1261 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001262}
1263
Chris Craik4063a0e2013-11-15 16:06:56 -08001264void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001265 GLsizei elementsCount = quadsCount * 6;
1266 while (elementsCount > 0) {
1267 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1268
Romain Guy3380cfd2013-08-15 16:57:57 -07001269 setupDrawIndexedVertices(&mesh[0].x);
Chris Craike84a2082014-12-22 14:28:49 -08001270 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy448455f2013-07-22 13:57:50 -07001271
1272 elementsCount -= drawCount;
1273 // Though there are 4 vertices in a quad, we use 6 indices per
1274 // quad to draw with GL_TRIANGLES
1275 mesh += (drawCount / 6) * 4;
1276 }
1277}
1278
Romain Guy54be1cd2011-06-13 19:04:27 -07001279void OpenGLRenderer::clearLayerRegions() {
1280 const size_t count = mLayers.size();
1281 if (count == 0) return;
1282
Tom Hudson984162f2014-10-10 13:38:16 -04001283 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001284 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001285 // Doing several glScissor/glClear here can negatively impact
1286 // GPUs with a tiler architecture, instead we draw quads with
1287 // the Clear blending mode
1288
1289 // The list contains bounds that have already been clipped
1290 // against their initial clip rect, and the current clip
1291 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001292 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001293
Romain Guy448455f2013-07-22 13:57:50 -07001294 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001295 Vertex* vertex = mesh;
1296
1297 for (uint32_t i = 0; i < count; i++) {
1298 Rect* bounds = mLayers.itemAt(i);
1299
Romain Guy54be1cd2011-06-13 19:04:27 -07001300 Vertex::set(vertex++, bounds->left, bounds->top);
1301 Vertex::set(vertex++, bounds->right, bounds->top);
1302 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001303 Vertex::set(vertex++, bounds->right, bounds->bottom);
1304
1305 delete bounds;
1306 }
Romain Guye67307c2013-02-11 18:01:20 -08001307 // We must clear the list of dirty rects before we
1308 // call setupDraw() to prevent stencil setup to do
1309 // the same thing again
1310 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001311
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001312 SkPaint clearPaint;
1313 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1314
Romain Guy54be1cd2011-06-13 19:04:27 -07001315 setupDraw(false);
1316 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001317 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001318 setupDrawProgram();
1319 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001320 setupDrawModelView(kModelViewMode_Translate, false,
1321 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001322
Chris Craik4063a0e2013-11-15 16:06:56 -08001323 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001324
1325 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001326 } else {
1327 for (uint32_t i = 0; i < count; i++) {
1328 delete mLayers.itemAt(i);
1329 }
Romain Guye67307c2013-02-11 18:01:20 -08001330 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001331 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001332}
1333
Romain Guybd6b79b2010-06-26 00:13:53 -07001334///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001335// State Deferral
1336///////////////////////////////////////////////////////////////////////////////
1337
Chris Craikff785832013-03-08 13:12:16 -08001338bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Tom Hudson984162f2014-10-10 13:38:16 -04001339 const Rect* currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001340 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001341
Chris Craikff785832013-03-08 13:12:16 -08001342 if (stateDeferFlags & kStateDeferFlag_Draw) {
1343 // state has bounds initialized in local coordinates
1344 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001345 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001346 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001347 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1348 // is used, it should more closely duplicate the quickReject logic (in how it uses
1349 // snapToPixelBoundaries)
1350
Chris Craikd6b65f62014-01-01 14:45:21 -08001351 if(!clippedBounds.intersect(*currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001352 // quick rejected
1353 return true;
1354 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001355
Chris Craika02c4ed2013-06-14 13:43:58 -07001356 state.mClipSideFlags = kClipSide_None;
Chris Craikd6b65f62014-01-01 14:45:21 -08001357 if (!currentClip->contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001358 int& flags = state.mClipSideFlags;
1359 // op partially clipped, so record which sides are clipped for clip-aware merging
Chris Craikd6b65f62014-01-01 14:45:21 -08001360 if (currentClip->left > state.mBounds.left) flags |= kClipSide_Left;
1361 if (currentClip->top > state.mBounds.top) flags |= kClipSide_Top;
1362 if (currentClip->right < state.mBounds.right) flags |= kClipSide_Right;
1363 if (currentClip->bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001364 }
1365 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001366 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001367 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1368 // overdraw avoidance (since we don't know what it overlaps)
1369 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikd6b65f62014-01-01 14:45:21 -08001370 state.mBounds.set(*currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001371 }
1372 }
1373
Chris Craik527a3aa2013-03-04 10:19:31 -08001374 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1375 if (state.mClipValid) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001376 state.mClip.set(*currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001377 }
1378
Chris Craik7273daa2013-03-28 11:25:24 -07001379 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1380 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001381 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001382 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001383 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001384
1385 // always store/restore, since it's just a pointer
1386 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001387 return false;
1388}
1389
Chris Craik527a3aa2013-03-04 10:19:31 -08001390void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001391 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001392 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001393 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001394 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001395
Chris Craik527a3aa2013-03-04 10:19:31 -08001396 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001397 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001398 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001399 dirtyClip();
1400 }
Chris Craikc3566d02013-02-04 16:16:33 -08001401}
1402
Chris Craik28ce94a2013-05-31 11:38:03 -07001403/**
1404 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1405 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1406 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1407 *
1408 * This method should be called when restoreDisplayState() won't be restoring the clip
1409 */
1410void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001411 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001412 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001413 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001414 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001415 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001416 dirtyClip();
Chris Craike84a2082014-12-22 14:28:49 -08001417 mCaches.setScissorEnabled(clipRect != nullptr || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001418}
1419
Chris Craikc3566d02013-02-04 16:16:33 -08001420///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001421// Clipping
1422///////////////////////////////////////////////////////////////////////////////
1423
Romain Guybb9524b2010-06-22 18:56:38 -07001424void OpenGLRenderer::setScissorFromClip() {
Tom Hudson984162f2014-10-10 13:38:16 -04001425 Rect clip(*mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001426 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001427
Chris Craika64a2be2014-05-14 14:17:01 -07001428 if (mCaches.setScissor(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001429 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001430 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001431 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001432}
1433
Romain Guy8ce00302013-01-15 18:51:42 -08001434void OpenGLRenderer::ensureStencilBuffer() {
1435 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1436 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1437 // just hope we have one when hasLayer() returns false.
1438 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001439 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001440 }
1441}
1442
1443void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1444 // The layer's FBO is already bound when we reach this stage
1445 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001446 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1447 // is attached after we initiated tiling. We must turn it off,
1448 // attach the new render buffer then turn tiling back on
1449 endTiling();
1450
Romain Guy8d4aeb72013-02-12 16:08:55 -08001451 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001452 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001453 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001454
Romain Guyf735c8e2013-01-31 17:45:55 -08001455 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001456 }
1457}
1458
1459void OpenGLRenderer::setStencilFromClip() {
1460 if (!mCaches.debugOverdraw) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001461 if (!currentSnapshot()->clipRegion->isEmpty()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001462 EVENT_LOGD("setStencilFromClip - enabling");
1463
Romain Guy8ce00302013-01-15 18:51:42 -08001464 // NOTE: The order here is important, we must set dirtyClip to false
1465 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001466 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001467
1468 ensureStencilBuffer();
1469
1470 mCaches.stencil.enableWrite();
1471
Chris Craikdeeda3d2014-05-05 19:09:33 -07001472 // Clear and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001473 // to the region's bounds
1474 bool resetScissor = mCaches.enableScissor();
1475 if (resetScissor) {
1476 // The scissor was not set so we now need to update it
1477 setScissorFromClip();
1478 }
1479 mCaches.stencil.clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001480
1481 // stash and disable the outline clip state, since stencil doesn't account for outline
1482 bool storedSkipOutlineClip = mSkipOutlineClip;
1483 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001484
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001485 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001486 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001487 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1488
Romain Guy8ce00302013-01-15 18:51:42 -08001489 // NOTE: We could use the region contour path to generate a smaller mesh
1490 // Since we are using the stencil we could use the red book path
1491 // drawing technique. It might increase bandwidth usage though.
1492
1493 // The last parameter is important: we are not drawing in the color buffer
1494 // so we don't want to dirty the current layer, if any
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001495 drawRegionRects(*(currentSnapshot()->clipRegion), paint, false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001496 if (resetScissor) mCaches.disableScissor();
1497 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001498
1499 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001500
1501 // Draw the region used to generate the stencil if the appropriate debug
1502 // mode is enabled
1503 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001504 paint.setColor(0x7f0000ff);
1505 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
1506 drawRegionRects(*(currentSnapshot()->clipRegion), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001507 }
Romain Guy8ce00302013-01-15 18:51:42 -08001508 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001509 EVENT_LOGD("setStencilFromClip - disabling");
Romain Guy8ce00302013-01-15 18:51:42 -08001510 mCaches.stencil.disable();
1511 }
1512 }
1513}
1514
Chris Craikf0a59072013-11-19 18:00:46 -08001515/**
1516 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1517 *
1518 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1519 * style, and tessellated AA ramp
1520 */
1521bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001522 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001523 bool snapOut = paint && paint->isAntiAlias();
1524
1525 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1526 float outset = paint->getStrokeWidth() * 0.5f;
1527 left -= outset;
1528 top -= outset;
1529 right += outset;
1530 bottom += outset;
1531 }
1532
Chris Craikdeeda3d2014-05-05 19:09:33 -07001533 bool clipRequired = false;
1534 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001535 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001536 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001537 return true;
1538 }
1539
Chris Craikf23b25a2014-06-26 15:46:20 -07001540 // not quick rejected, so enable the scissor if clipRequired
1541 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1542 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001543 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001544}
1545
Romain Guy8ce00302013-01-15 18:51:42 -08001546void OpenGLRenderer::debugClip() {
1547#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001548 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001549 SkPaint paint;
1550 paint.setColor(0x7f00ff00);
1551 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1552
Romain Guy8ce00302013-01-15 18:51:42 -08001553 }
1554#endif
1555}
1556
Romain Guyf6a11b82010-06-23 17:47:49 -07001557///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001558// Drawing commands
1559///////////////////////////////////////////////////////////////////////////////
1560
Chris Craik62d307c2014-07-29 10:35:13 -07001561void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001562 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001563 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001564 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001565 // Make sure setScissor & setStencil happen at the beginning of
1566 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001567 if (mState.getDirtyClip()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001568 if (mCaches.scissorEnabled) {
1569 setScissorFromClip();
1570 }
Chris Craik62d307c2014-07-29 10:35:13 -07001571
Dohyun Leeadc0d9d2014-11-24 21:08:15 +09001572 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001573 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001574
Romain Guy70ca14e2010-12-13 18:24:33 -08001575 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001576
Romain Guy70ca14e2010-12-13 18:24:33 -08001577 mSetShaderColor = false;
1578 mColorSet = false;
1579 mColorA = mColorR = mColorG = mColorB = 0.0f;
1580 mTextureUnit = 0;
1581 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001582
1583 // Enable debug highlight when what we're about to draw is tested against
1584 // the stencil buffer and if stencil highlight debugging is on
1585 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1586 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1587 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001588}
1589
1590void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1591 mDescription.hasTexture = true;
1592 mDescription.hasAlpha8Texture = isAlpha8;
1593}
1594
Romain Guyff316ec2013-02-13 18:39:43 -08001595void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1596 mDescription.hasTexture = true;
1597 mDescription.hasColors = true;
1598 mDescription.hasAlpha8Texture = isAlpha8;
1599}
1600
Romain Guyaa6c24c2011-04-28 18:40:04 -07001601void OpenGLRenderer::setupDrawWithExternalTexture() {
1602 mDescription.hasExternalTexture = true;
1603}
1604
Romain Guy15bc6432011-12-13 13:11:32 -08001605void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001606 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001607}
1608
Chris Craik91a8c7c2014-08-12 14:31:35 -07001609void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1610 mDescription.hasVertexAlpha = true;
1611 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001612}
1613
Romain Guy8d0d4782010-12-14 20:13:35 -08001614void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1615 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001616 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1617 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1618 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001619 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001620 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001621}
1622
Romain Guy86568192010-12-14 15:55:39 -08001623void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1624 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001625 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1626 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1627 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001628 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001629 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001630}
1631
Romain Guy41210632012-07-16 17:04:24 -07001632void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1633 mCaches.fontRenderer->describe(mDescription, paint);
1634}
1635
Romain Guy70ca14e2010-12-13 18:24:33 -08001636void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1637 mColorA = a;
1638 mColorR = r;
1639 mColorG = g;
1640 mColorB = b;
1641 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001642 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001643}
1644
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001645void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
Chris Craike84a2082014-12-22 14:28:49 -08001646 if (shader != nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001647 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001648 }
1649}
1650
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001651void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001652 if (filter == nullptr) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001653 return;
1654 }
1655
1656 SkXfermode::Mode mode;
Chris Craike84a2082014-12-22 14:28:49 -08001657 if (filter->asColorMode(nullptr, &mode)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001658 mDescription.colorOp = ProgramDescription::kColorBlend;
1659 mDescription.colorMode = mode;
Chris Craike84a2082014-12-22 14:28:49 -08001660 } else if (filter->asColorMatrix(nullptr)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001661 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001662 }
1663}
1664
Romain Guyf09ef512011-05-27 11:43:46 -07001665void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1666 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1667 mColorA = 1.0f;
1668 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001669 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001670 }
1671}
1672
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001673void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1674 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001675 // When the blending mode is kClear_Mode, we need to use a modulate color
1676 // argb=1,0,0,0
1677 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001678 // TODO: check shader blending, once we have shader drawing support for layers.
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001679 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001680 (mColorSet && mColorA < 1.0f) || isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001681 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001682}
1683
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001684void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1685 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001686 // When the blending mode is kClear_Mode, we need to use a modulate color
1687 // argb=1,0,0,0
1688 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001689 blend |= (mColorSet && mColorA < 1.0f) ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001690 (getShader(paint) && !getShader(paint)->isOpaque()) ||
1691 isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001692 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001693}
1694
1695void OpenGLRenderer::setupDrawProgram() {
1696 useProgram(mCaches.programCache.get(mDescription));
Chris Craikdeeda3d2014-05-05 19:09:33 -07001697 if (mDescription.hasRoundRectClip) {
1698 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001699 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001700 const Rect& innerRect = state->innerRect;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001701 glUniform4f(mCaches.currentProgram->getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001702 innerRect.left, innerRect.top,
1703 innerRect.right, innerRect.bottom);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001704 glUniformMatrix4fv(mCaches.currentProgram->getUniform("roundRectInvTransform"),
1705 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001706
1707 // add half pixel to round out integer rect space to cover pixel centers
1708 float roundedOutRadius = state->radius + 0.5f;
1709 glUniform1f(mCaches.currentProgram->getUniform("roundRectRadius"),
1710 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001711 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001712}
1713
1714void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1715 mTrackDirtyRegions = false;
1716}
1717
Chris Craik4063a0e2013-11-15 16:06:56 -08001718void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1719 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001720 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001721 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001722 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001723 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001724
Romain Guy86568192010-12-14 15:55:39 -08001725 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001726 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Chris Craike84a2082014-12-22 14:28:49 -08001727 mCaches.currentProgram->set(writableSnapshot()->getOrthoMatrix(),
1728 mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001729 if (dirty && mTrackDirtyRegions) {
1730 if (!ignoreTransform) {
1731 dirtyLayer(left, top, right, bottom, *currentTransform());
1732 } else {
1733 dirtyLayer(left, top, right, bottom);
1734 }
Romain Guy86568192010-12-14 15:55:39 -08001735 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001736}
1737
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001738void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1739 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001740 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1741 }
1742}
1743
Romain Guy86568192010-12-14 15:55:39 -08001744void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001745 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001746 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001747 }
1748}
1749
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001750void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
Chris Craike84a2082014-12-22 14:28:49 -08001751 if (shader == nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001752 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001753 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001754
1755 if (ignoreTransform) {
1756 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1757 // because it was built into modelView / the geometry, and the description needs to
1758 // compensate.
1759 mat4 modelViewWithoutTransform;
1760 modelViewWithoutTransform.loadInverse(*currentTransform());
1761 modelViewWithoutTransform.multiply(mModelViewMatrix);
1762 mModelViewMatrix.load(modelViewWithoutTransform);
1763 }
1764
1765 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001766}
1767
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001768void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001769 if (nullptr == filter) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001770 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001771 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001772
1773 SkColor color;
1774 SkXfermode::Mode mode;
1775 if (filter->asColorMode(&color, &mode)) {
1776 const int alpha = SkColorGetA(color);
1777 const GLfloat a = alpha / 255.0f;
1778 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1779 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1780 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1781 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1782 return;
1783 }
1784
1785 SkScalar srcColorMatrix[20];
1786 if (filter->asColorMatrix(srcColorMatrix)) {
1787
1788 float colorMatrix[16];
1789 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1790 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1791 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1792 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1793
1794 // Skia uses the range [0..255] for the addition vector, but we need
1795 // the [0..1] range to apply the vector in GLSL
1796 float colorVector[4];
1797 colorVector[0] = srcColorMatrix[4] / 255.0f;
1798 colorVector[1] = srcColorMatrix[9] / 255.0f;
1799 colorVector[2] = srcColorMatrix[14] / 255.0f;
1800 colorVector[3] = srcColorMatrix[19] / 255.0f;
1801
1802 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1803 GL_FALSE, colorMatrix);
1804 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1805 return;
1806 }
1807
1808 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001809}
1810
Romain Guy41210632012-07-16 17:04:24 -07001811void OpenGLRenderer::setupDrawTextGammaUniforms() {
1812 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1813}
1814
Romain Guy70ca14e2010-12-13 18:24:33 -08001815void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001816 bool force = mCaches.bindMeshBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08001817 mCaches.bindPositionVertexPointer(force, nullptr);
Romain Guy15bc6432011-12-13 13:11:32 -08001818 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001819}
1820
1821void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001822 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001823 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001824 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001825}
1826
Romain Guyaa6c24c2011-04-28 18:40:04 -07001827void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1828 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001829 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001830 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001831}
1832
Romain Guy8f0095c2011-05-02 17:24:22 -07001833void OpenGLRenderer::setupDrawTextureTransform() {
1834 mDescription.hasTextureTransform = true;
1835}
1836
1837void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001838 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1839 GL_FALSE, &transform.data[0]);
1840}
1841
Chris Craik564acf72014-01-02 16:46:18 -08001842void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1843 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001844 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001845 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001846 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001847 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001848 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001849 }
Romain Guyd71dd362011-12-12 19:03:35 -08001850
Chris Craikcb4d6002012-09-25 12:00:29 -07001851 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001852 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001853 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001854 }
1855
1856 mCaches.unbindIndicesBuffer();
1857}
1858
Chris Craik564acf72014-01-02 16:46:18 -08001859void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1860 const GLvoid* texCoords, const GLvoid* colors) {
Romain Guyff316ec2013-02-13 18:39:43 -08001861 bool force = mCaches.unbindMeshBuffer();
1862 GLsizei stride = sizeof(ColorTextureVertex);
1863
1864 mCaches.bindPositionVertexPointer(force, vertices, stride);
1865 if (mCaches.currentProgram->texCoords >= 0) {
1866 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1867 }
1868 int slot = mCaches.currentProgram->getAttrib("colors");
1869 if (slot >= 0) {
1870 glEnableVertexAttribArray(slot);
1871 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1872 }
1873
1874 mCaches.unbindIndicesBuffer();
1875}
1876
Chris Craik564acf72014-01-02 16:46:18 -08001877void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1878 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001879 bool force = false;
1880 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1881 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1882 // use the default VBO found in Caches
1883 if (!vertices || vbo) {
1884 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1885 } else {
1886 force = mCaches.unbindMeshBuffer();
1887 }
ztenghui63d41ab2014-02-14 13:13:41 -08001888 mCaches.bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001889
Chris Craikcb4d6002012-09-25 12:00:29 -07001890 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001891 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001892 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001893 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001894}
1895
Romain Guy448455f2013-07-22 13:57:50 -07001896void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001897 bool force = mCaches.unbindMeshBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08001898 mCaches.bindQuadIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001899 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001900}
1901
Romain Guy70ca14e2010-12-13 18:24:33 -08001902///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001903// Drawing
1904///////////////////////////////////////////////////////////////////////////////
1905
Tom Hudson107843d2014-09-08 11:26:26 -04001906void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001907 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1908 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001909 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001910 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001911 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001912 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001913 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001914 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001915 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001916 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001917 }
1918
Chris Craikef8d6f22014-12-17 11:10:28 -08001919 // Don't avoid overdraw when visualizing, since that makes it harder to
1920 // debug where it's coming from, and when the problem occurs.
1921 bool avoidOverdraw = !mCaches.debugOverdraw;
Chris Craika5f09182014-12-17 15:11:30 -08001922 DeferredDisplayList deferredList(*mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001923 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001924 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001925
1926 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001927 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001928
Tom Hudson107843d2014-09-08 11:26:26 -04001929 deferredList.flush(*this, dirty);
1930 } else {
1931 // Even if there is no drawing command(Ex: invisible),
1932 // it still needs startFrame to clear buffer and start tiling.
1933 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001934 }
1935}
1936
Chris Craike84a2082014-12-22 14:28:49 -08001937void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top,
1938 const SkPaint* paint) {
Romain Guya168d732011-03-18 16:50:13 -07001939 float x = left;
1940 float y = top;
1941
Romain Guy886b2752013-01-04 12:26:18 -08001942 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1943
Romain Guya168d732011-03-18 16:50:13 -07001944 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001945 if (currentTransform()->isPureTranslate()) {
1946 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1947 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001948 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001949
1950 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001951 } else {
Chris Craik678625242014-02-28 12:26:34 -08001952 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001953 }
1954
Romain Guy3b748a42013-04-17 18:54:38 -07001955 // No need to check for a UV mapper on the texture object, only ARGB_8888
1956 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001957 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Chris Craike84a2082014-12-22 14:28:49 -08001958 paint, (GLvoid*) nullptr, (GLvoid*) gMeshTextureOffset,
Romain Guy3b748a42013-04-17 18:54:38 -07001959 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001960}
1961
Romain Guy03c00b52013-06-20 18:30:28 -07001962/**
1963 * Important note: this method is intended to draw batches of bitmaps and
1964 * will not set the scissor enable or dirty the current layer, if any.
1965 * The caller is responsible for properly dirtying the current layer.
1966 */
Tom Hudson107843d2014-09-08 11:26:26 -04001967void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001968 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1969 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001970 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001971 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001972 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001973
Chris Craik527a3aa2013-03-04 10:19:31 -08001974 const AutoTexture autoCleanup(texture);
1975
Chris Craik527a3aa2013-03-04 10:19:31 -08001976 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08001977 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08001978
1979 const float x = (int) floorf(bounds.left + 0.5f);
1980 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04001981 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001982 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001983 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001984 GL_TRIANGLES, bitmapCount * 6, true,
1985 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001986 } else {
1987 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001988 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001989 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
1990 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001991 }
1992
Tom Hudson107843d2014-09-08 11:26:26 -04001993 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08001994}
1995
Tom Hudson107843d2014-09-08 11:26:26 -04001996void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07001997 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04001998 return;
Romain Guy6926c722010-07-12 20:20:03 -07001999 }
2000
Romain Guya1d3c912011-12-13 14:55:06 -08002001 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002002 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002003 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002004 const AutoTexture autoCleanup(texture);
2005
Mike Reed1103b322014-07-08 12:36:44 -04002006 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002007 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002008 } else {
Chris Craik79647502014-08-06 13:42:24 -07002009 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002010 }
Chet Haase48659092012-05-31 15:21:51 -07002011
Tom Hudson107843d2014-09-08 11:26:26 -04002012 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002013}
2014
Tom Hudson107843d2014-09-08 11:26:26 -04002015void OpenGLRenderer::drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002016 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002017 return;
Romain Guye651cc62012-05-14 19:44:40 -07002018 }
2019
2020 mCaches.activeTexture(0);
2021 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2022 const AutoTexture autoCleanup(texture);
2023
Mike Reed1103b322014-07-08 12:36:44 -04002024 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002025 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002026 } else {
Chris Craik79647502014-08-06 13:42:24 -07002027 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002028 }
Chet Haase48659092012-05-31 15:21:51 -07002029
Tom Hudson107843d2014-09-08 11:26:26 -04002030 mDirty = true;
Romain Guye651cc62012-05-14 19:44:40 -07002031}
2032
Tom Hudson107843d2014-09-08 11:26:26 -04002033void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002034 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002035 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002036 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002037 }
2038
Chris Craik39a908c2013-06-13 14:39:01 -07002039 // TODO: use quickReject on bounds from vertices
2040 mCaches.enableScissor();
2041
Romain Guyb18d2d02011-02-10 15:52:54 -08002042 float left = FLT_MAX;
2043 float top = FLT_MAX;
2044 float right = FLT_MIN;
2045 float bottom = FLT_MIN;
2046
Romain Guya92bb4d2012-10-16 11:08:44 -07002047 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002048
Chris Craik564acf72014-01-02 16:46:18 -08002049 Vector<ColorTextureVertex> mesh; // TODO: use C++11 unique_ptr
2050 mesh.setCapacity(count);
2051 ColorTextureVertex* vertex = mesh.editArray();
Romain Guyff316ec2013-02-13 18:39:43 -08002052
2053 bool cleanupColors = false;
2054 if (!colors) {
2055 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craikd218a922014-01-02 17:13:34 -08002056 int* newColors = new int[colorsCount];
2057 memset(newColors, 0xff, colorsCount * sizeof(int));
2058 colors = newColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002059 cleanupColors = true;
2060 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002061
Romain Guy3b748a42013-04-17 18:54:38 -07002062 mCaches.activeTexture(0);
John Reckebd52612014-12-10 16:47:36 -08002063 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002064 const UvMapper& mapper(getMapper(texture));
2065
Romain Guy5a7b4662011-01-20 19:09:30 -08002066 for (int32_t y = 0; y < meshHeight; y++) {
2067 for (int32_t x = 0; x < meshWidth; x++) {
2068 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2069
2070 float u1 = float(x) / meshWidth;
2071 float u2 = float(x + 1) / meshWidth;
2072 float v1 = float(y) / meshHeight;
2073 float v2 = float(y + 1) / meshHeight;
2074
Romain Guy3b748a42013-04-17 18:54:38 -07002075 mapper.map(u1, v1, u2, v2);
2076
Romain Guy5a7b4662011-01-20 19:09:30 -08002077 int ax = i + (meshWidth + 1) * 2;
2078 int ay = ax + 1;
2079 int bx = i;
2080 int by = bx + 1;
2081 int cx = i + 2;
2082 int cy = cx + 1;
2083 int dx = i + (meshWidth + 1) * 2 + 2;
2084 int dy = dx + 1;
2085
Romain Guyff316ec2013-02-13 18:39:43 -08002086 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2087 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2088 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002089
Romain Guyff316ec2013-02-13 18:39:43 -08002090 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2091 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2092 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002093
Romain Guya92bb4d2012-10-16 11:08:44 -07002094 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2095 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2096 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2097 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002098 }
2099 }
2100
Chris Craikf0a59072013-11-19 18:00:46 -08002101 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002102 if (cleanupColors) delete[] colors;
Tom Hudson107843d2014-09-08 11:26:26 -04002103 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002104 }
2105
Romain Guyff316ec2013-02-13 18:39:43 -08002106 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002107 texture = mCaches.textureCache.get(bitmap);
2108 if (!texture) {
2109 if (cleanupColors) delete[] colors;
Tom Hudson107843d2014-09-08 11:26:26 -04002110 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002111 }
Romain Guyff316ec2013-02-13 18:39:43 -08002112 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002113 const AutoTexture autoCleanup(texture);
2114
2115 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002116 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002117
2118 int alpha;
2119 SkXfermode::Mode mode;
2120 getAlphaAndMode(paint, &alpha, &mode);
2121
Romain Guyff316ec2013-02-13 18:39:43 -08002122 float a = alpha / 255.0f;
2123
Romain Guya92bb4d2012-10-16 11:08:44 -07002124 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002125 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002126 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002127
Romain Guyff316ec2013-02-13 18:39:43 -08002128 setupDraw();
2129 setupDrawWithTextureAndColor();
2130 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002131 setupDrawColorFilter(getColorFilter(paint));
2132 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002133 setupDrawProgram();
2134 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002135 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002136 setupDrawTexture(texture->id);
2137 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002138 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002139 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002140
2141 glDrawArrays(GL_TRIANGLES, 0, count);
2142
Romain Guyff316ec2013-02-13 18:39:43 -08002143 int slot = mCaches.currentProgram->getAttrib("colors");
2144 if (slot >= 0) {
2145 glDisableVertexAttribArray(slot);
2146 }
2147
2148 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002149
Tom Hudson107843d2014-09-08 11:26:26 -04002150 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002151}
2152
Tom Hudson107843d2014-09-08 11:26:26 -04002153void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002154 float srcLeft, float srcTop, float srcRight, float srcBottom,
2155 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002156 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002157 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002158 return;
Romain Guy6926c722010-07-12 20:20:03 -07002159 }
2160
Romain Guya1d3c912011-12-13 14:55:06 -08002161 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002162 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002163 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002164 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002165
Romain Guy8ba548f2010-06-30 19:21:21 -07002166 const float width = texture->width;
2167 const float height = texture->height;
2168
Romain Guy3b748a42013-04-17 18:54:38 -07002169 float u1 = fmax(0.0f, srcLeft / width);
2170 float v1 = fmax(0.0f, srcTop / height);
2171 float u2 = fmin(1.0f, srcRight / width);
2172 float v2 = fmin(1.0f, srcBottom / height);
2173
2174 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002175
Romain Guy03750a02010-10-18 14:06:08 -07002176 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002177 resetDrawTextureTexCoords(u1, v1, u2, v2);
2178
Romain Guyd21b6e12011-11-30 20:21:23 -08002179 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2180
Romain Guy886b2752013-01-04 12:26:18 -08002181 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2182 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002183
Romain Guy886b2752013-01-04 12:26:18 -08002184 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2185 // Apply a scale transform on the canvas only when a shader is in use
2186 // Skia handles the ratio between the dst and src rects as a scale factor
2187 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002188 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002189 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002190
Chris Craikd6b65f62014-01-01 14:45:21 -08002191 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2192 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2193 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002194
2195 dstRight = x + (dstRight - dstLeft);
2196 dstBottom = y + (dstBottom - dstTop);
2197
2198 dstLeft = x;
2199 dstTop = y;
2200
Chris Craik678625242014-02-28 12:26:34 -08002201 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002202 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002203 } else {
Chris Craik678625242014-02-28 12:26:34 -08002204 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002205 }
2206
2207 if (CC_UNLIKELY(useScaleTransform)) {
2208 save(SkCanvas::kMatrix_SaveFlag);
2209 translate(dstLeft, dstTop);
2210 scale(scaleX, scaleY);
2211
2212 dstLeft = 0.0f;
2213 dstTop = 0.0f;
2214
2215 dstRight = srcRight - srcLeft;
2216 dstBottom = srcBottom - srcTop;
2217 }
2218
Mike Reed1103b322014-07-08 12:36:44 -04002219 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002220 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002221 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002222 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002223 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2224 } else {
2225 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002226 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002227 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002228 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2229 }
2230
2231 if (CC_UNLIKELY(useScaleTransform)) {
2232 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002233 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002234
2235 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002236
Tom Hudson107843d2014-09-08 11:26:26 -04002237 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002238}
2239
Tom Hudson107843d2014-09-08 11:26:26 -04002240void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002241 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002242 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002243 return;
Romain Guy6926c722010-07-12 20:20:03 -07002244 }
2245
John Reckebd52612014-12-10 16:47:36 -08002246 AssetAtlas::Entry* entry = mRenderState.assetAtlas().getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002247 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2248 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002249
Tom Hudson107843d2014-09-08 11:26:26 -04002250 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002251}
2252
Tom Hudson107843d2014-09-08 11:26:26 -04002253void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002254 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2255 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002256 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002257 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002258 }
2259
Romain Guy211370f2012-02-01 16:10:55 -08002260 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002261 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002262 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002263 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002264 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002265
Romain Guya4adcf02013-02-28 12:15:35 -08002266 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2267 texture->setFilter(GL_LINEAR, true);
2268
Chris Craikd6b65f62014-01-01 14:45:21 -08002269 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002270 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002271 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002272 const float offsetX = left + currentTransform()->getTranslateX();
2273 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002274 const size_t count = mesh->quads.size();
2275 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002276 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002277 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002278 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2279 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2280 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002281 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002282 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002283 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002284 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002285 }
2286 }
2287
Chris Craik4063a0e2013-11-15 16:06:56 -08002288 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002289 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002290 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2291 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002292
Romain Guy3b748a42013-04-17 18:54:38 -07002293 right = x + right - left;
2294 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002295 left = x;
2296 top = y;
2297 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002298 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002299 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2300 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002301 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2302 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002303 }
Chet Haase48659092012-05-31 15:21:51 -07002304
Tom Hudson107843d2014-09-08 11:26:26 -04002305 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002306}
2307
Romain Guy03c00b52013-06-20 18:30:28 -07002308/**
2309 * Important note: this method is intended to draw batches of 9-patch objects and
2310 * will not set the scissor enable or dirty the current layer, if any.
2311 * The caller is responsible for properly dirtying the current layer.
2312 */
Tom Hudson107843d2014-09-08 11:26:26 -04002313void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002314 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002315 mCaches.activeTexture(0);
2316 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002317 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002318 const AutoTexture autoCleanup(texture);
2319
2320 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2321 texture->setFilter(GL_LINEAR, true);
2322
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002323 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2324 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002325 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002326
Tom Hudson107843d2014-09-08 11:26:26 -04002327 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002328}
2329
Tom Hudson107843d2014-09-08 11:26:26 -04002330void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002331 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002332 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002333 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002334 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002335 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002336 }
2337
Chris Craik0d5ac952014-07-15 13:01:02 -07002338 Rect bounds(vertexBuffer.getBounds());
2339 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002340 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2341
Chris Craikcb4d6002012-09-25 12:00:29 -07002342 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002343 bool isAA = paint->isAntiAlias();
2344
Chris Craik710f46d2012-09-17 17:25:49 -07002345 setupDraw();
2346 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002347 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Tom Hudson984162f2014-10-10 13:38:16 -04002348 setupDrawColor(color, ((color >> 24) & 0xFF) * writableSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002349 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002350 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002351 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002352 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002353 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2354 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002355 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002356 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002357 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002358
ztenghui55bfb4e2013-12-03 10:38:55 -08002359 const void* vertices = vertexBuffer.getBuffer();
Andreas Gampeedaecc12014-11-10 20:54:07 -08002360 mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002361 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002362 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002363
Chris Craik710f46d2012-09-17 17:25:49 -07002364 int alphaSlot = -1;
2365 if (isAA) {
2366 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2367 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002368 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002369 glEnableVertexAttribArray(alphaSlot);
2370 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002371 }
Romain Guy04299382012-07-18 17:15:41 -07002372
Chris Craik05f3d6e2014-06-02 16:27:04 -07002373 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2374 if (mode == VertexBuffer::kStandard) {
ztenghui63d41ab2014-02-14 13:13:41 -08002375 mCaches.unbindIndicesBuffer();
2376 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002377 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002378 mCaches.bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002379 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT,
2380 GL_UNSIGNED_SHORT, nullptr);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002381 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
ztenghui50ecf842014-03-11 16:52:30 -07002382 mCaches.bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002383 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT,
2384 GL_UNSIGNED_SHORT, nullptr);
ztenghuid5e8ade2014-08-13 15:48:02 -07002385 } else if (mode == VertexBuffer::kIndices) {
2386 mCaches.unbindIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002387 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(),
2388 GL_UNSIGNED_SHORT, 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 Craike84a2082014-12-22 14:28:49 -08002498 if (p->getPathEffect() != nullptr) {
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 Craike84a2082014-12-22 14:28:49 -08002516 if (p->getPathEffect() != nullptr) {
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 Craike84a2082014-12-22 14:28:49 -08002539 if (p->getPathEffect() != nullptr) {
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 Craike84a2082014-12-22 14:28:49 -08002563 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || 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
Chris Craike84a2082014-12-22 14:28:49 -08002599 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07002600 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));
Chris Craike84a2082014-12-22 14:28:49 -08002668 setupDrawMesh(nullptr, (GLvoid*) gMeshTextureOffset);
Raph Levien416a8472012-07-19 22:48:17 -07002669
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) {
Chris Craike84a2082014-12-22 14:28:49 -08002680 if (text == nullptr || 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,
Chris Craike84a2082014-12-22 14:28:49 -08002725 positions, hasActiveLayer ? &bounds : nullptr, &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
Chris Craike84a2082014-12-22 14:28:49 -08002828 if (text == nullptr || 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
Chris Craike84a2082014-12-22 14:28:49 -08002878 const Rect* clip = !pureTranslate ? nullptr : 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 Craike84a2082014-12-22 14:28:49 -08002890 positions, hasActiveLayer ? &layerBounds : nullptr, &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 Craike84a2082014-12-22 14:28:49 -08002893 positions, hasActiveLayer ? &layerBounds : nullptr, &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) {
Chris Craike84a2082014-12-22 14:28:49 -08002910 if (text == nullptr || 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,
Chris Craike84a2082014-12-22 14:28:49 -08002932 hOffset, vOffset, hasActiveLayer ? &bounds : nullptr, &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
Chris Craike84a2082014-12-22 14:28:49 -08002963 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002964 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;
Chris Craike84a2082014-12-22 14:28:49 -08002973 const bool rejected = mState.calculateQuickRejectForScissor(
2974 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2975 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002976
2977 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002978 if (transform && !transform->isIdentity()) {
2979 restore();
2980 }
Tom Hudson107843d2014-09-08 11:26:26 -04002981 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002982 }
2983
Chris Craik62d307c2014-07-29 10:35:13 -07002984 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2985 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2986
Romain Guy5bb3c732012-11-29 17:52:58 -08002987 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002988
Chris Craik39a908c2013-06-13 14:39:01 -07002989 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08002990 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002991
Romain Guy211370f2012-02-01 16:10:55 -08002992 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002993 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002994 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2995 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002996 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002997
Chris Craik16ecda52013-03-29 10:59:59 -07002998 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002999 setupDraw();
3000 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003001 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003002 setupDrawColorFilter(layer->getColorFilter());
3003 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003004 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003005 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003006 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07003007 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08003008 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3009 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3010 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003011
Romain Guyd21b6e12011-11-30 20:21:23 -08003012 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003013 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003014 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003015 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003016 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003017 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003018 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3019 }
Romain Guyf219da52011-01-16 12:54:25 -08003020
Romain Guy448455f2013-07-22 13:57:50 -07003021 TextureVertex* mesh = &layer->mesh[0];
3022 GLsizei elementsCount = layer->meshElementCount;
3023
3024 while (elementsCount > 0) {
3025 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3026
Romain Guy3380cfd2013-08-15 16:57:57 -07003027 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003028 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
Chris Craike84a2082014-12-22 14:28:49 -08003029 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr));
Romain Guy448455f2013-07-22 13:57:50 -07003030
3031 elementsCount -= drawCount;
3032 // Though there are 4 vertices in a quad, we use 6 indices per
3033 // quad to draw with GL_TRIANGLES
3034 mesh += (drawCount / 6) * 4;
3035 }
Romain Guyf219da52011-01-16 12:54:25 -08003036
Romain Guy3a3133d2011-02-01 22:59:58 -08003037#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003038 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003039#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003040 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003041
Romain Guy5bb3c732012-11-29 17:52:58 -08003042 if (layer->debugDrawUpdate) {
3043 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003044
3045 SkPaint paint;
3046 paint.setColor(0x7f00ff00);
3047 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003048 }
Romain Guyf219da52011-01-16 12:54:25 -08003049 }
Chris Craik34416ea2013-04-15 16:08:28 -07003050 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003051
Romain Guyb2e2f242012-10-17 18:18:35 -07003052 if (transform && !transform->isIdentity()) {
3053 restore();
3054 }
3055
Tom Hudson107843d2014-09-08 11:26:26 -04003056 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003057}
3058
Romain Guy6926c722010-07-12 20:20:03 -07003059///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003060// Draw filters
3061///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003062void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3063 // We should never get here since we apply the draw filter when stashing
3064 // the paints in the DisplayList.
3065 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003066}
3067
3068///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003069// Drawing implementation
3070///////////////////////////////////////////////////////////////////////////////
3071
Chris Craikd218a922014-01-02 17:13:34 -08003072Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003073 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003074 if (!texture) {
3075 return mCaches.textureCache.get(bitmap);
3076 }
3077 return texture;
3078}
3079
Romain Guy01d58e42011-01-19 21:54:02 -08003080void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003081 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003082 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003083 return;
3084 }
3085
3086 int alpha;
3087 SkXfermode::Mode mode;
3088 getAlphaAndMode(paint, &alpha, &mode);
3089
3090 setupDraw();
3091 setupDrawWithTexture(true);
3092 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003093 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003094 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003095 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003096 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003097 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3098 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003099 setupDrawTexture(texture->id);
3100 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003101 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003102 setupDrawShaderUniforms(getShader(paint));
Chris Craike84a2082014-12-22 14:28:49 -08003103 setupDrawMesh(nullptr, (GLvoid*) gMeshTextureOffset);
Romain Guy01d58e42011-01-19 21:54:02 -08003104
3105 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003106}
3107
Romain Guyf607bdc2010-09-10 19:20:06 -07003108// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003109#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3110#define kStdUnderline_Offset (1.0f / 9.0f)
3111#define kStdUnderline_Thickness (1.0f / 18.0f)
3112
Chris Craikd218a922014-01-02 17:13:34 -08003113void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3114 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003115 // Handle underline and strike-through
3116 uint32_t flags = paint->getFlags();
3117 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003118 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003119
Romain Guy211370f2012-02-01 16:10:55 -08003120 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003121 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003122 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003123
Raph Levien8b4072d2012-07-30 15:50:00 -07003124 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003125 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003126
Romain Guyf6834472011-01-23 13:32:12 -08003127 int linesCount = 0;
3128 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3129 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3130
3131 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003132 float points[pointsCount];
3133 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003134
3135 if (flags & SkPaint::kUnderlineText_Flag) {
3136 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003137 points[currentPoint++] = left;
3138 points[currentPoint++] = top;
3139 points[currentPoint++] = left + underlineWidth;
3140 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003141 }
3142
3143 if (flags & SkPaint::kStrikeThruText_Flag) {
3144 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003145 points[currentPoint++] = left;
3146 points[currentPoint++] = top;
3147 points[currentPoint++] = left + underlineWidth;
3148 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003149 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003150
Romain Guy726aeba2011-06-01 14:52:00 -07003151 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003152
Romain Guy726aeba2011-06-01 14:52:00 -07003153 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003154 }
3155 }
3156}
3157
Tom Hudson107843d2014-09-08 11:26:26 -04003158void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003159 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003160 return;
Romain Guy672433d2013-01-04 19:05:13 -08003161 }
3162
Tom Hudson107843d2014-09-08 11:26:26 -04003163 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003164}
3165
Tom Hudson107843d2014-09-08 11:26:26 -04003166void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003167 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003168 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003169
Chris Craik15a07a22014-01-26 13:43:53 -08003170 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craikf57776b2013-10-25 18:30:17 -07003171 mCaches.enableScissor();
3172
3173 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003174 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003175
ztenghui14a4e352014-08-13 10:44:39 -07003176 // The caller has made sure casterAlpha > 0.
3177 float ambientShadowAlpha = mAmbientShadowAlpha;
3178 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3179 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3180 }
3181 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3182 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003183 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003184 }
ztenghui7b4516e2014-01-07 10:42:55 -08003185
ztenghui14a4e352014-08-13 10:44:39 -07003186 float spotShadowAlpha = mSpotShadowAlpha;
3187 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3188 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3189 }
3190 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3191 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003192 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003193 }
ztenghui7b4516e2014-01-07 10:42:55 -08003194
Tom Hudson107843d2014-09-08 11:26:26 -04003195 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003196}
3197
Tom Hudson107843d2014-09-08 11:26:26 -04003198void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003199 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003200 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003201 return;
Romain Guy3b753822013-03-05 10:27:35 -08003202 }
Romain Guy735738c2012-12-03 12:34:51 -08003203
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003204 int color = paint->getColor();
3205 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003206 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003207 color |= 0x00ffffff;
3208 }
3209
Romain Guy672433d2013-01-04 19:05:13 -08003210 float left = FLT_MAX;
3211 float top = FLT_MAX;
3212 float right = FLT_MIN;
3213 float bottom = FLT_MIN;
3214
Romain Guy448455f2013-07-22 13:57:50 -07003215 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003216 Vertex* vertex = mesh;
3217
Chris Craik2af46352012-11-26 18:30:17 -08003218 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003219 float l = rects[index + 0];
3220 float t = rects[index + 1];
3221 float r = rects[index + 2];
3222 float b = rects[index + 3];
3223
Romain Guy3b753822013-03-05 10:27:35 -08003224 Vertex::set(vertex++, l, t);
3225 Vertex::set(vertex++, r, t);
3226 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003227 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003228
Romain Guy3b753822013-03-05 10:27:35 -08003229 left = fminf(left, l);
3230 top = fminf(top, t);
3231 right = fmaxf(right, r);
3232 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003233 }
3234
Chris Craikf0a59072013-11-19 18:00:46 -08003235 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003236 return;
Romain Guya362c692013-02-04 13:50:16 -08003237 }
Romain Guy672433d2013-01-04 19:05:13 -08003238
Romain Guy672433d2013-01-04 19:05:13 -08003239 setupDraw();
3240 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003241 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003242 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003243 setupDrawColorFilter(getColorFilter(paint));
3244 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003245 setupDrawProgram();
3246 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003247 setupDrawModelView(kModelViewMode_Translate, false,
3248 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003249 setupDrawColorUniforms(getShader(paint));
3250 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003251 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003252
Romain Guy8ce00302013-01-15 18:51:42 -08003253 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003254 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003255 }
3256
Chris Craik4063a0e2013-11-15 16:06:56 -08003257 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003258
Tom Hudson107843d2014-09-08 11:26:26 -04003259 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003260}
3261
Romain Guy026c5e162010-06-28 17:12:22 -07003262void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003263 const SkPaint* paint, bool ignoreTransform) {
3264 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003265 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003266 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003267 color |= 0x00ffffff;
3268 }
3269
Romain Guy70ca14e2010-12-13 18:24:33 -08003270 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003271 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003272 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003273 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003274 setupDrawColorFilter(getColorFilter(paint));
3275 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003276 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003277 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3278 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003279 setupDrawColorUniforms(getShader(paint));
3280 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003281 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003282 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003283
Romain Guyc95c8d62010-09-17 15:31:32 -07003284 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3285}
3286
Romain Guy82ba8142010-07-09 13:25:56 -07003287void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003288 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003289 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003290
Chris Craike84a2082014-12-22 14:28:49 -08003291 GLvoid* vertices = (GLvoid*) nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -07003292 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3293
3294 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003295 vertices = &mMeshVertices[0].x;
3296 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003297
3298 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3299 texture->uvMapper->map(uvs);
3300
3301 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3302 }
3303
Chris Craikd6b65f62014-01-01 14:45:21 -08003304 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3305 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3306 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003307
Romain Guyd21b6e12011-11-30 20:21:23 -08003308 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003309 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003310 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003311 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003312 } else {
Chris Craik678625242014-02-28 12:26:34 -08003313 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003314 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003315 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3316 }
3317
3318 if (texture->uvMapper) {
3319 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003320 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003321}
3322
Romain Guyf7f93552010-07-08 19:17:03 -07003323void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003324 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003325 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003326 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3327 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003328
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003329 int a;
3330 SkXfermode::Mode mode;
3331 getAlphaAndMode(paint, &a, &mode);
3332 const float alpha = a / 255.0f;
3333
Romain Guy746b7402010-10-26 16:27:31 -07003334 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003335 setupDrawWithTexture();
3336 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003337 setupDrawColorFilter(getColorFilter(paint));
3338 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003339 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003340 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003341 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003342 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003343 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003344 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003345 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003346
Romain Guy6820ac82010-09-15 18:11:50 -07003347 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003348}
3349
Romain Guy3b748a42013-04-17 18:54:38 -07003350void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003351 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003352 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003353 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3354 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003355
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003356 int a;
3357 SkXfermode::Mode mode;
3358 getAlphaAndMode(paint, &a, &mode);
3359 const float alpha = a / 255.0f;
3360
Romain Guy3b748a42013-04-17 18:54:38 -07003361 setupDraw();
3362 setupDrawWithTexture();
3363 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003364 setupDrawColorFilter(getColorFilter(paint));
3365 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003366 setupDrawProgram();
3367 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003368 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003369 setupDrawTexture(texture);
3370 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003371 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003372 setupDrawMeshIndices(vertices, texCoords, vbo);
3373
Chris Craike84a2082014-12-22 14:28:49 -08003374 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy3b748a42013-04-17 18:54:38 -07003375}
3376
Romain Guy886b2752013-01-04 12:26:18 -08003377void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003378 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003379 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003380 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003381
Chris Craike84a2082014-12-22 14:28:49 -08003382 int color = paint != nullptr ? paint->getColor() : 0;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003383 int alpha;
3384 SkXfermode::Mode mode;
3385 getAlphaAndMode(paint, &alpha, &mode);
3386
Romain Guy886b2752013-01-04 12:26:18 -08003387 setupDraw();
3388 setupDrawWithTexture(true);
Chris Craike84a2082014-12-22 14:28:49 -08003389 if (paint != nullptr) {
Romain Guy886b2752013-01-04 12:26:18 -08003390 setupDrawAlpha8Color(color, alpha);
3391 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003392 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003393 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003394 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003395 setupDrawProgram();
3396 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003397 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003398 setupDrawTexture(texture);
3399 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003400 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003401 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003402 setupDrawMesh(vertices, texCoords);
3403
3404 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003405}
3406
Romain Guya5aed0d2010-09-09 14:42:43 -07003407void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003408 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003409
Chris Craike84a2082014-12-22 14:28:49 -08003410 if (writableSnapshot()->roundRectClipState != nullptr /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003411 blend = true;
3412 mDescription.hasRoundRectClip = true;
3413 }
3414 mSkipOutlineClip = true;
3415
Romain Guy82ba8142010-07-09 13:25:56 -07003416 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003417
Romain Guy82ba8142010-07-09 13:25:56 -07003418 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003419 // These blend modes are not supported by OpenGL directly and have
3420 // to be implemented using shaders. Since the shader will perform
3421 // the blending, turn blending off here
3422 // If the blend mode cannot be implemented using shaders, fall
3423 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003424 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003425 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003426 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003427 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003428
Romain Guy82bc7a72012-01-03 14:13:39 -08003429 if (mCaches.blend) {
3430 glDisable(GL_BLEND);
3431 mCaches.blend = false;
3432 }
3433
3434 return;
3435 } else {
3436 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003437 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003438 }
3439
3440 if (!mCaches.blend) {
3441 glEnable(GL_BLEND);
3442 }
3443
3444 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3445 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3446
3447 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3448 glBlendFunc(sourceMode, destMode);
3449 mCaches.lastSrcMode = sourceMode;
3450 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003451 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003452 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003453 glDisable(GL_BLEND);
3454 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003455 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003456}
3457
Romain Guy889f8d12010-07-29 14:37:42 -07003458bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003459 if (!program->isInUse()) {
Chris Craike84a2082014-12-22 14:28:49 -08003460 if (mCaches.currentProgram != nullptr) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003461 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003462 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003463 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003464 }
Romain Guy6926c722010-07-12 20:20:03 -07003465 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003466}
3467
Romain Guy026c5e162010-06-28 17:12:22 -07003468void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003469 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003470 TextureVertex::setUV(v++, u1, v1);
3471 TextureVertex::setUV(v++, u2, v1);
3472 TextureVertex::setUV(v++, u1, v2);
3473 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003474}
3475
Chris Craike84a2082014-12-22 14:28:49 -08003476void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
3477 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003478 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003479 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3480 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003481 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003482 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003483 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003484}
3485
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003486float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003487 float alpha;
3488 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3489 alpha = mDrawModifiers.mOverrideLayerAlpha;
3490 } else {
3491 alpha = layer->getAlpha() / 255.0f;
3492 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003493 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003494}
3495
Romain Guy9d5316e2010-06-24 19:30:36 -07003496}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003497}; // namespace android