blob: f200c55287d29895c9eca915e04caf5f09cb29fd [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());
683 writableSnapshot()->roundRectClipState = NULL;
Chris Craikd90144d2013-03-19 15:03:48 -0700684 }
685 }
686
687 return count;
688}
689
Romain Guy1c740bc2010-09-13 18:00:09 -0700690/**
691 * Layers are viewed by Skia are slightly different than layers in image editing
692 * programs (for instance.) When a layer is created, previously created layers
693 * and the frame buffer still receive every drawing command. For instance, if a
694 * layer is created and a shape intersecting the bounds of the layers and the
695 * framebuffer is draw, the shape will be drawn on both (unless the layer was
696 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
697 *
698 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
699 * texture. Unfortunately, this is inefficient as it requires every primitive to
700 * be drawn n + 1 times, where n is the number of active layers. In practice this
701 * means, for every primitive:
702 * - Switch active frame buffer
703 * - Change viewport, clip and projection matrix
704 * - Issue the drawing
705 *
706 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700707 * To avoid this, layers are implemented in a different way here, at least in the
708 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
709 * is set. When this flag is set we can redirect all drawing operations into a
710 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700711 *
712 * This implementation relies on the frame buffer being at least RGBA 8888. When
713 * a layer is created, only a texture is created, not an FBO. The content of the
714 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700715 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700716 * buffer and drawing continues as normal. This technique therefore treats the
717 * frame buffer as a scratch buffer for the layers.
718 *
719 * To compose the layers back onto the frame buffer, each layer texture
720 * (containing the original frame buffer data) is drawn as a simple quad over
721 * the frame buffer. The trick is that the quad is set as the composition
722 * destination in the blending equation, and the frame buffer becomes the source
723 * of the composition.
724 *
725 * Drawing layers with an alpha value requires an extra step before composition.
726 * An empty quad is drawn over the layer's region in the frame buffer. This quad
727 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
728 * quad is used to multiply the colors in the frame buffer. This is achieved by
729 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
730 * GL_ZERO, GL_SRC_ALPHA.
731 *
732 * Because glCopyTexImage2D() can be slow, an alternative implementation might
733 * be use to draw a single clipped layer. The implementation described above
734 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700735 *
736 * (1) The frame buffer is actually not cleared right away. To allow the GPU
737 * to potentially optimize series of calls to glCopyTexImage2D, the frame
738 * buffer is left untouched until the first drawing operation. Only when
739 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700740 */
Chet Haased48885a2012-08-28 17:43:28 -0700741bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700742 const SkPaint* paint, int flags, const SkPath* convexMask) {
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(),
794 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
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());
820 writableSnapshot()->roundRectClipState = NULL;
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 Craikdeeda3d2014-05-05 19:09:33 -0700868 &clipRequired, NULL, 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 Craik3f0854292014-04-15 16:18:08 -0700920 layer->setConvexMask(NULL);
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");
John Reck0e89e2b2014-10-31 14:49:06 -0700923 layer->decStrong(0);
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
1040 virtual bool asACustomShader(void** data) const {
1041 if (data) {
1042 *data = static_cast<void*>(mLayer);
1043 }
1044 return true;
1045 }
1046
1047 virtual bool isOpaque() const {
1048 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
1056 virtual void flatten(SkWriteBuffer&) const {
1057 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1058 }
1059
1060 virtual Factory getFactory() const {
1061 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
1062 return NULL;
1063 }
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
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001095 paint.setShader(NULL);
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,
1176 GL_UNSIGNED_SHORT, NULL));
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,
1184 GL_UNSIGNED_SHORT, NULL));
1185 }
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);
Romain Guy448455f2013-07-22 13:57:50 -07001270 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1271
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) {
1411 if (clipRect != NULL) {
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 Craik28ce94a2013-05-31 11:38:03 -07001417 mCaches.setScissorEnabled(clipRect != NULL || 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) {
1646 if (shader != NULL) {
1647 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) {
1652 if (filter == NULL) {
1653 return;
1654 }
1655
1656 SkXfermode::Mode mode;
1657 if (filter->asColorMode(NULL, &mode)) {
1658 mDescription.colorOp = ProgramDescription::kColorBlend;
1659 mDescription.colorMode = mode;
1660 } else if (filter->asColorMatrix(NULL)) {
1661 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();
Tom Hudson984162f2014-10-10 13:38:16 -04001727 mCaches.currentProgram->set(writableSnapshot()->getOrthoMatrix(), mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001728 if (dirty && mTrackDirtyRegions) {
1729 if (!ignoreTransform) {
1730 dirtyLayer(left, top, right, bottom, *currentTransform());
1731 } else {
1732 dirtyLayer(left, top, right, bottom);
1733 }
Romain Guy86568192010-12-14 15:55:39 -08001734 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001735}
1736
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001737void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1738 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001739 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1740 }
1741}
1742
Romain Guy86568192010-12-14 15:55:39 -08001743void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001744 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001745 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001746 }
1747}
1748
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001749void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
1750 if (shader == NULL) {
1751 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001752 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001753
1754 if (ignoreTransform) {
1755 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1756 // because it was built into modelView / the geometry, and the description needs to
1757 // compensate.
1758 mat4 modelViewWithoutTransform;
1759 modelViewWithoutTransform.loadInverse(*currentTransform());
1760 modelViewWithoutTransform.multiply(mModelViewMatrix);
1761 mModelViewMatrix.load(modelViewWithoutTransform);
1762 }
1763
1764 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001765}
1766
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001767void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
1768 if (NULL == filter) {
1769 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001770 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001771
1772 SkColor color;
1773 SkXfermode::Mode mode;
1774 if (filter->asColorMode(&color, &mode)) {
1775 const int alpha = SkColorGetA(color);
1776 const GLfloat a = alpha / 255.0f;
1777 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1778 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1779 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1780 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1781 return;
1782 }
1783
1784 SkScalar srcColorMatrix[20];
1785 if (filter->asColorMatrix(srcColorMatrix)) {
1786
1787 float colorMatrix[16];
1788 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1789 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1790 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1791 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1792
1793 // Skia uses the range [0..255] for the addition vector, but we need
1794 // the [0..1] range to apply the vector in GLSL
1795 float colorVector[4];
1796 colorVector[0] = srcColorMatrix[4] / 255.0f;
1797 colorVector[1] = srcColorMatrix[9] / 255.0f;
1798 colorVector[2] = srcColorMatrix[14] / 255.0f;
1799 colorVector[3] = srcColorMatrix[19] / 255.0f;
1800
1801 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1802 GL_FALSE, colorMatrix);
1803 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1804 return;
1805 }
1806
1807 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001808}
1809
Romain Guy41210632012-07-16 17:04:24 -07001810void OpenGLRenderer::setupDrawTextGammaUniforms() {
1811 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1812}
1813
Romain Guy70ca14e2010-12-13 18:24:33 -08001814void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001815 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001816 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001817 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001818}
1819
1820void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001821 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001822 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001823 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001824}
1825
Romain Guyaa6c24c2011-04-28 18:40:04 -07001826void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1827 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001828 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001829 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001830}
1831
Romain Guy8f0095c2011-05-02 17:24:22 -07001832void OpenGLRenderer::setupDrawTextureTransform() {
1833 mDescription.hasTextureTransform = true;
1834}
1835
1836void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001837 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1838 GL_FALSE, &transform.data[0]);
1839}
1840
Chris Craik564acf72014-01-02 16:46:18 -08001841void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1842 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001843 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001844 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001845 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001846 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001847 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001848 }
Romain Guyd71dd362011-12-12 19:03:35 -08001849
Chris Craikcb4d6002012-09-25 12:00:29 -07001850 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001851 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001852 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001853 }
1854
1855 mCaches.unbindIndicesBuffer();
1856}
1857
Chris Craik564acf72014-01-02 16:46:18 -08001858void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1859 const GLvoid* texCoords, const GLvoid* colors) {
Romain Guyff316ec2013-02-13 18:39:43 -08001860 bool force = mCaches.unbindMeshBuffer();
1861 GLsizei stride = sizeof(ColorTextureVertex);
1862
1863 mCaches.bindPositionVertexPointer(force, vertices, stride);
1864 if (mCaches.currentProgram->texCoords >= 0) {
1865 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1866 }
1867 int slot = mCaches.currentProgram->getAttrib("colors");
1868 if (slot >= 0) {
1869 glEnableVertexAttribArray(slot);
1870 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1871 }
1872
1873 mCaches.unbindIndicesBuffer();
1874}
1875
Chris Craik564acf72014-01-02 16:46:18 -08001876void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1877 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001878 bool force = false;
1879 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1880 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1881 // use the default VBO found in Caches
1882 if (!vertices || vbo) {
1883 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1884 } else {
1885 force = mCaches.unbindMeshBuffer();
1886 }
ztenghui63d41ab2014-02-14 13:13:41 -08001887 mCaches.bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001888
Chris Craikcb4d6002012-09-25 12:00:29 -07001889 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001890 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001891 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001892 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001893}
1894
Romain Guy448455f2013-07-22 13:57:50 -07001895void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001896 bool force = mCaches.unbindMeshBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08001897 mCaches.bindQuadIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001898 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001899}
1900
Romain Guy70ca14e2010-12-13 18:24:33 -08001901///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001902// Drawing
1903///////////////////////////////////////////////////////////////////////////////
1904
Tom Hudson107843d2014-09-08 11:26:26 -04001905void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001906 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1907 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001908 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001909 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001910 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001911 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001912 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001913 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001914 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001915 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001916 }
1917
Chris Craikef8d6f22014-12-17 11:10:28 -08001918 // Don't avoid overdraw when visualizing, since that makes it harder to
1919 // debug where it's coming from, and when the problem occurs.
1920 bool avoidOverdraw = !mCaches.debugOverdraw;
Chris Craika5f09182014-12-17 15:11:30 -08001921 DeferredDisplayList deferredList(*mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001922 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001923 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001924
1925 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001926 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001927
Tom Hudson107843d2014-09-08 11:26:26 -04001928 deferredList.flush(*this, dirty);
1929 } else {
1930 // Even if there is no drawing command(Ex: invisible),
1931 // it still needs startFrame to clear buffer and start tiling.
1932 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001933 }
1934}
1935
Chris Craikd218a922014-01-02 17:13:34 -08001936void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, const SkPaint* paint) {
Romain Guya168d732011-03-18 16:50:13 -07001937 float x = left;
1938 float y = top;
1939
Romain Guy886b2752013-01-04 12:26:18 -08001940 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1941
Romain Guya168d732011-03-18 16:50:13 -07001942 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001943 if (currentTransform()->isPureTranslate()) {
1944 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1945 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001946 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001947
1948 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001949 } else {
Chris Craik678625242014-02-28 12:26:34 -08001950 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001951 }
1952
Romain Guy3b748a42013-04-17 18:54:38 -07001953 // No need to check for a UV mapper on the texture object, only ARGB_8888
1954 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001955 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001956 paint, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy3b748a42013-04-17 18:54:38 -07001957 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001958}
1959
Romain Guy03c00b52013-06-20 18:30:28 -07001960/**
1961 * Important note: this method is intended to draw batches of bitmaps and
1962 * will not set the scissor enable or dirty the current layer, if any.
1963 * The caller is responsible for properly dirtying the current layer.
1964 */
Tom Hudson107843d2014-09-08 11:26:26 -04001965void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001966 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1967 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001968 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001969 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001970 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001971
Chris Craik527a3aa2013-03-04 10:19:31 -08001972 const AutoTexture autoCleanup(texture);
1973
Chris Craik527a3aa2013-03-04 10:19:31 -08001974 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08001975 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08001976
1977 const float x = (int) floorf(bounds.left + 0.5f);
1978 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04001979 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001980 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001981 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001982 GL_TRIANGLES, bitmapCount * 6, true,
1983 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001984 } else {
1985 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001986 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001987 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
1988 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001989 }
1990
Tom Hudson107843d2014-09-08 11:26:26 -04001991 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08001992}
1993
Tom Hudson107843d2014-09-08 11:26:26 -04001994void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07001995 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04001996 return;
Romain Guy6926c722010-07-12 20:20:03 -07001997 }
1998
Romain Guya1d3c912011-12-13 14:55:06 -08001999 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002000 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002001 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002002 const AutoTexture autoCleanup(texture);
2003
Mike Reed1103b322014-07-08 12:36:44 -04002004 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002005 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002006 } else {
Chris Craik79647502014-08-06 13:42:24 -07002007 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002008 }
Chet Haase48659092012-05-31 15:21:51 -07002009
Tom Hudson107843d2014-09-08 11:26:26 -04002010 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002011}
2012
Tom Hudson107843d2014-09-08 11:26:26 -04002013void OpenGLRenderer::drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002014 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002015 return;
Romain Guye651cc62012-05-14 19:44:40 -07002016 }
2017
2018 mCaches.activeTexture(0);
2019 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2020 const AutoTexture autoCleanup(texture);
2021
Mike Reed1103b322014-07-08 12:36:44 -04002022 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002023 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002024 } else {
Chris Craik79647502014-08-06 13:42:24 -07002025 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002026 }
Chet Haase48659092012-05-31 15:21:51 -07002027
Tom Hudson107843d2014-09-08 11:26:26 -04002028 mDirty = true;
Romain Guye651cc62012-05-14 19:44:40 -07002029}
2030
Tom Hudson107843d2014-09-08 11:26:26 -04002031void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002032 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002033 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002034 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002035 }
2036
Chris Craik39a908c2013-06-13 14:39:01 -07002037 // TODO: use quickReject on bounds from vertices
2038 mCaches.enableScissor();
2039
Romain Guyb18d2d02011-02-10 15:52:54 -08002040 float left = FLT_MAX;
2041 float top = FLT_MAX;
2042 float right = FLT_MIN;
2043 float bottom = FLT_MIN;
2044
Romain Guya92bb4d2012-10-16 11:08:44 -07002045 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002046
Chris Craik564acf72014-01-02 16:46:18 -08002047 Vector<ColorTextureVertex> mesh; // TODO: use C++11 unique_ptr
2048 mesh.setCapacity(count);
2049 ColorTextureVertex* vertex = mesh.editArray();
Romain Guyff316ec2013-02-13 18:39:43 -08002050
2051 bool cleanupColors = false;
2052 if (!colors) {
2053 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craikd218a922014-01-02 17:13:34 -08002054 int* newColors = new int[colorsCount];
2055 memset(newColors, 0xff, colorsCount * sizeof(int));
2056 colors = newColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002057 cleanupColors = true;
2058 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002059
Romain Guy3b748a42013-04-17 18:54:38 -07002060 mCaches.activeTexture(0);
John Reckebd52612014-12-10 16:47:36 -08002061 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002062 const UvMapper& mapper(getMapper(texture));
2063
Romain Guy5a7b4662011-01-20 19:09:30 -08002064 for (int32_t y = 0; y < meshHeight; y++) {
2065 for (int32_t x = 0; x < meshWidth; x++) {
2066 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2067
2068 float u1 = float(x) / meshWidth;
2069 float u2 = float(x + 1) / meshWidth;
2070 float v1 = float(y) / meshHeight;
2071 float v2 = float(y + 1) / meshHeight;
2072
Romain Guy3b748a42013-04-17 18:54:38 -07002073 mapper.map(u1, v1, u2, v2);
2074
Romain Guy5a7b4662011-01-20 19:09:30 -08002075 int ax = i + (meshWidth + 1) * 2;
2076 int ay = ax + 1;
2077 int bx = i;
2078 int by = bx + 1;
2079 int cx = i + 2;
2080 int cy = cx + 1;
2081 int dx = i + (meshWidth + 1) * 2 + 2;
2082 int dy = dx + 1;
2083
Romain Guyff316ec2013-02-13 18:39:43 -08002084 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2085 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2086 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002087
Romain Guyff316ec2013-02-13 18:39:43 -08002088 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2089 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2090 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002091
Romain Guya92bb4d2012-10-16 11:08:44 -07002092 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2093 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2094 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2095 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002096 }
2097 }
2098
Chris Craikf0a59072013-11-19 18:00:46 -08002099 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002100 if (cleanupColors) delete[] colors;
Tom Hudson107843d2014-09-08 11:26:26 -04002101 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002102 }
2103
Romain Guyff316ec2013-02-13 18:39:43 -08002104 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002105 texture = mCaches.textureCache.get(bitmap);
2106 if (!texture) {
2107 if (cleanupColors) delete[] colors;
Tom Hudson107843d2014-09-08 11:26:26 -04002108 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002109 }
Romain Guyff316ec2013-02-13 18:39:43 -08002110 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002111 const AutoTexture autoCleanup(texture);
2112
2113 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002114 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002115
2116 int alpha;
2117 SkXfermode::Mode mode;
2118 getAlphaAndMode(paint, &alpha, &mode);
2119
Romain Guyff316ec2013-02-13 18:39:43 -08002120 float a = alpha / 255.0f;
2121
Romain Guya92bb4d2012-10-16 11:08:44 -07002122 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002123 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002124 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002125
Romain Guyff316ec2013-02-13 18:39:43 -08002126 setupDraw();
2127 setupDrawWithTextureAndColor();
2128 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002129 setupDrawColorFilter(getColorFilter(paint));
2130 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002131 setupDrawProgram();
2132 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002133 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002134 setupDrawTexture(texture->id);
2135 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002136 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002137 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002138
2139 glDrawArrays(GL_TRIANGLES, 0, count);
2140
Romain Guyff316ec2013-02-13 18:39:43 -08002141 int slot = mCaches.currentProgram->getAttrib("colors");
2142 if (slot >= 0) {
2143 glDisableVertexAttribArray(slot);
2144 }
2145
2146 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002147
Tom Hudson107843d2014-09-08 11:26:26 -04002148 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002149}
2150
Tom Hudson107843d2014-09-08 11:26:26 -04002151void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002152 float srcLeft, float srcTop, float srcRight, float srcBottom,
2153 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002154 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002155 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002156 return;
Romain Guy6926c722010-07-12 20:20:03 -07002157 }
2158
Romain Guya1d3c912011-12-13 14:55:06 -08002159 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002160 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002161 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002162 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002163
Romain Guy8ba548f2010-06-30 19:21:21 -07002164 const float width = texture->width;
2165 const float height = texture->height;
2166
Romain Guy3b748a42013-04-17 18:54:38 -07002167 float u1 = fmax(0.0f, srcLeft / width);
2168 float v1 = fmax(0.0f, srcTop / height);
2169 float u2 = fmin(1.0f, srcRight / width);
2170 float v2 = fmin(1.0f, srcBottom / height);
2171
2172 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002173
Romain Guy03750a02010-10-18 14:06:08 -07002174 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002175 resetDrawTextureTexCoords(u1, v1, u2, v2);
2176
Romain Guyd21b6e12011-11-30 20:21:23 -08002177 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2178
Romain Guy886b2752013-01-04 12:26:18 -08002179 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2180 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002181
Romain Guy886b2752013-01-04 12:26:18 -08002182 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2183 // Apply a scale transform on the canvas only when a shader is in use
2184 // Skia handles the ratio between the dst and src rects as a scale factor
2185 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002186 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002187 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002188
Chris Craikd6b65f62014-01-01 14:45:21 -08002189 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2190 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2191 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002192
2193 dstRight = x + (dstRight - dstLeft);
2194 dstBottom = y + (dstBottom - dstTop);
2195
2196 dstLeft = x;
2197 dstTop = y;
2198
Chris Craik678625242014-02-28 12:26:34 -08002199 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002200 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002201 } else {
Chris Craik678625242014-02-28 12:26:34 -08002202 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002203 }
2204
2205 if (CC_UNLIKELY(useScaleTransform)) {
2206 save(SkCanvas::kMatrix_SaveFlag);
2207 translate(dstLeft, dstTop);
2208 scale(scaleX, scaleY);
2209
2210 dstLeft = 0.0f;
2211 dstTop = 0.0f;
2212
2213 dstRight = srcRight - srcLeft;
2214 dstBottom = srcBottom - srcTop;
2215 }
2216
Mike Reed1103b322014-07-08 12:36:44 -04002217 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002218 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002219 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002220 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002221 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2222 } else {
2223 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002224 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002225 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002226 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2227 }
2228
2229 if (CC_UNLIKELY(useScaleTransform)) {
2230 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002231 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002232
2233 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002234
Tom Hudson107843d2014-09-08 11:26:26 -04002235 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002236}
2237
Tom Hudson107843d2014-09-08 11:26:26 -04002238void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002239 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002240 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002241 return;
Romain Guy6926c722010-07-12 20:20:03 -07002242 }
2243
John Reckebd52612014-12-10 16:47:36 -08002244 AssetAtlas::Entry* entry = mRenderState.assetAtlas().getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002245 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2246 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002247
Tom Hudson107843d2014-09-08 11:26:26 -04002248 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002249}
2250
Tom Hudson107843d2014-09-08 11:26:26 -04002251void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002252 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2253 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002254 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002255 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002256 }
2257
Romain Guy211370f2012-02-01 16:10:55 -08002258 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002259 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002260 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002261 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002262 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002263
Romain Guya4adcf02013-02-28 12:15:35 -08002264 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2265 texture->setFilter(GL_LINEAR, true);
2266
Chris Craikd6b65f62014-01-01 14:45:21 -08002267 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002268 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002269 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002270 const float offsetX = left + currentTransform()->getTranslateX();
2271 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002272 const size_t count = mesh->quads.size();
2273 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002274 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002275 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002276 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2277 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2278 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002279 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002280 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002281 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002282 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002283 }
2284 }
2285
Chris Craik4063a0e2013-11-15 16:06:56 -08002286 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002287 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002288 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2289 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002290
Romain Guy3b748a42013-04-17 18:54:38 -07002291 right = x + right - left;
2292 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002293 left = x;
2294 top = y;
2295 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002296 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002297 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2298 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002299 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2300 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002301 }
Chet Haase48659092012-05-31 15:21:51 -07002302
Tom Hudson107843d2014-09-08 11:26:26 -04002303 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002304}
2305
Romain Guy03c00b52013-06-20 18:30:28 -07002306/**
2307 * Important note: this method is intended to draw batches of 9-patch objects and
2308 * will not set the scissor enable or dirty the current layer, if any.
2309 * The caller is responsible for properly dirtying the current layer.
2310 */
Tom Hudson107843d2014-09-08 11:26:26 -04002311void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002312 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002313 mCaches.activeTexture(0);
2314 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002315 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002316 const AutoTexture autoCleanup(texture);
2317
2318 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2319 texture->setFilter(GL_LINEAR, true);
2320
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002321 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2322 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002323 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002324
Tom Hudson107843d2014-09-08 11:26:26 -04002325 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002326}
2327
Tom Hudson107843d2014-09-08 11:26:26 -04002328void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002329 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002330 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002331 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002332 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002333 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002334 }
2335
Chris Craik0d5ac952014-07-15 13:01:02 -07002336 Rect bounds(vertexBuffer.getBounds());
2337 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002338 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2339
Chris Craikcb4d6002012-09-25 12:00:29 -07002340 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002341 bool isAA = paint->isAntiAlias();
2342
Chris Craik710f46d2012-09-17 17:25:49 -07002343 setupDraw();
2344 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002345 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Tom Hudson984162f2014-10-10 13:38:16 -04002346 setupDrawColor(color, ((color >> 24) & 0xFF) * writableSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002347 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002348 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002349 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002350 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002351 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2352 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002353 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002354 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002355 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002356
ztenghui55bfb4e2013-12-03 10:38:55 -08002357 const void* vertices = vertexBuffer.getBuffer();
Andreas Gampeedaecc12014-11-10 20:54:07 -08002358 mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002359 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002360 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002361
Chris Craik710f46d2012-09-17 17:25:49 -07002362 int alphaSlot = -1;
2363 if (isAA) {
2364 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2365 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002366 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002367 glEnableVertexAttribArray(alphaSlot);
2368 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002369 }
Romain Guy04299382012-07-18 17:15:41 -07002370
Chris Craik05f3d6e2014-06-02 16:27:04 -07002371 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2372 if (mode == VertexBuffer::kStandard) {
ztenghui63d41ab2014-02-14 13:13:41 -08002373 mCaches.unbindIndicesBuffer();
2374 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002375 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002376 mCaches.bindShadowIndicesBuffer();
ztenghui50ecf842014-03-11 16:52:30 -07002377 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002378 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
ztenghui50ecf842014-03-11 16:52:30 -07002379 mCaches.bindShadowIndicesBuffer();
2380 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
ztenghuid5e8ade2014-08-13 15:48:02 -07002381 } else if (mode == VertexBuffer::kIndices) {
2382 mCaches.unbindIndicesBuffer();
2383 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(), GL_UNSIGNED_SHORT,
2384 vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002385 }
Romain Guy04299382012-07-18 17:15:41 -07002386
Chris Craik710f46d2012-09-17 17:25:49 -07002387 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002388 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002389 }
Chris Craik65cd6122012-12-10 17:56:27 -08002390
Tom Hudson107843d2014-09-08 11:26:26 -04002391 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002392}
2393
2394/**
Chris Craik65cd6122012-12-10 17:56:27 -08002395 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2396 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2397 * screen space in all directions. However, instead of using a fragment shader to compute the
2398 * translucency of the color from its position, we simply use a varying parameter to define how far
2399 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2400 *
2401 * Doesn't yet support joins, caps, or path effects.
2402 */
Tom Hudson107843d2014-09-08 11:26:26 -04002403void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002404 VertexBuffer vertexBuffer;
2405 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002406 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002407 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002408}
2409
2410/**
2411 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2412 * and additional geometry for defining an alpha slope perimeter.
2413 *
2414 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2415 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2416 * in-shader alpha region, but found it to be taxing on some GPUs.
2417 *
2418 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2419 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002420 */
Tom Hudson107843d2014-09-08 11:26:26 -04002421void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002422 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002423
Chris Craik65cd6122012-12-10 17:56:27 -08002424 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002425
Chris Craik65cd6122012-12-10 17:56:27 -08002426 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002427 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2428 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002429
Chris Craik05f3d6e2014-06-02 16:27:04 -07002430 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002431 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002432 }
2433
Chris Craikbf759452014-08-11 16:00:44 -07002434 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002435 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002436}
2437
Tom Hudson107843d2014-09-08 11:26:26 -04002438void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002439 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002440
Chris Craik6d29c8d2013-05-08 18:35:44 -07002441 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002442
Chris Craik6d29c8d2013-05-08 18:35:44 -07002443 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002444 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002445
Chris Craik05f3d6e2014-06-02 16:27:04 -07002446 const Rect& bounds = buffer.getBounds();
2447 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002448 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002449 }
2450
Chris Craikbf759452014-08-11 16:00:44 -07002451 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002452 drawVertexBuffer(buffer, paint, displayFlags);
2453
2454 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002455}
2456
Tom Hudson107843d2014-09-08 11:26:26 -04002457void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002458 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002459 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002460
Tom Hudson984162f2014-10-10 13:38:16 -04002461 Rect clip(*mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002462 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002463
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002464 SkPaint paint;
2465 paint.setColor(color);
2466 paint.setXfermodeMode(mode);
2467
2468 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002469
Tom Hudson107843d2014-09-08 11:26:26 -04002470 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002471}
Romain Guy9d5316e2010-06-24 19:30:36 -07002472
Tom Hudson107843d2014-09-08 11:26:26 -04002473void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002474 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002475 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002476 const AutoTexture autoCleanup(texture);
2477
2478 const float x = left + texture->left - texture->offset;
2479 const float y = top + texture->top - texture->offset;
2480
2481 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002482
Tom Hudson107843d2014-09-08 11:26:26 -04002483 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002484}
2485
Tom Hudson107843d2014-09-08 11:26:26 -04002486void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002487 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002488 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002489 || quickRejectSetupScissor(left, top, right, bottom, p)
2490 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002491 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002492 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002493
Chris Craikcb4d6002012-09-25 12:00:29 -07002494 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002495 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002496 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002497 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002498 drawShape(left, top, texture, p);
2499 } else {
2500 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2501 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2502 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002503 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002504}
2505
Tom Hudson107843d2014-09-08 11:26:26 -04002506void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002507 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002508 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
2509 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002510 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002511 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002512 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002513 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002514 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002515 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002516 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002517 SkPath path;
2518 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2519 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2520 } else {
2521 path.addCircle(x, y, radius);
2522 }
2523 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002524 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002525}
Romain Guy01d58e42011-01-19 21:54:02 -08002526
Tom Hudson107843d2014-09-08 11:26:26 -04002527void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002528 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002529 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002530 || quickRejectSetupScissor(left, top, right, bottom, p)
2531 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002532 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002533 }
Romain Guy01d58e42011-01-19 21:54:02 -08002534
Chris Craikcb4d6002012-09-25 12:00:29 -07002535 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002536 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002537 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002538 drawShape(left, top, texture, p);
2539 } else {
2540 SkPath path;
2541 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2542 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2543 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2544 }
2545 path.addOval(rect);
2546 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002547 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002548}
2549
Tom Hudson107843d2014-09-08 11:26:26 -04002550void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002551 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002552 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002553 || quickRejectSetupScissor(left, top, right, bottom, p)
2554 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002555 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002556 }
2557
Chris Craik780c1282012-10-04 14:10:49 -07002558 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002559 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002560 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002561 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002562 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002563 drawShape(left, top, texture, p);
2564 return;
Chris Craik780c1282012-10-04 14:10:49 -07002565 }
Chris Craik780c1282012-10-04 14:10:49 -07002566 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2567 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2568 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2569 }
2570
2571 SkPath path;
2572 if (useCenter) {
2573 path.moveTo(rect.centerX(), rect.centerY());
2574 }
2575 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2576 if (useCenter) {
2577 path.close();
2578 }
Tom Hudson107843d2014-09-08 11:26:26 -04002579 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002580}
2581
Romain Guycf8675e2012-10-02 12:32:25 -07002582// See SkPaintDefaults.h
2583#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2584
Tom Hudson107843d2014-09-08 11:26:26 -04002585void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002586 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002587 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002588 || quickRejectSetupScissor(left, top, right, bottom, p)
2589 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002590 return;
Romain Guy6926c722010-07-12 20:20:03 -07002591 }
2592
Chris Craik710f46d2012-09-17 17:25:49 -07002593 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002594 // only fill style is supported by drawConvexPath, since others have to handle joins
2595 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2596 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2597 mCaches.activeTexture(0);
2598 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002599 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002600 drawShape(left, top, texture, p);
2601 } else {
2602 SkPath path;
2603 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2604 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2605 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2606 }
2607 path.addRect(rect);
2608 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002609 }
Chet Haase858aa932011-05-12 09:06:00 -07002610 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002611 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2612 SkPath path;
2613 path.addRect(left, top, right, bottom);
2614 drawConvexPath(path, p);
2615 } else {
2616 drawColorRect(left, top, right, bottom, p);
2617
2618 mDirty = true;
2619 }
Chet Haase858aa932011-05-12 09:06:00 -07002620 }
Romain Guyc7d53492010-06-25 13:41:57 -07002621}
Romain Guy9d5316e2010-06-24 19:30:36 -07002622
Chris Craikd218a922014-01-02 17:13:34 -08002623void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2624 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002625 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002626 mCaches.activeTexture(0);
2627
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002628 TextShadow textShadow;
2629 if (!getTextShadow(paint, &textShadow)) {
2630 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2631 }
2632
Raph Levien416a8472012-07-19 22:48:17 -07002633 // NOTE: The drop shadow will not perform gamma correction
2634 // if shader-based correction is enabled
2635 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2636 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002637 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002638 // If the drop shadow exceeds the max texture size or couldn't be
2639 // allocated, skip drawing
2640 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002641 const AutoTexture autoCleanup(shadow);
2642
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002643 const float sx = x - shadow->left + textShadow.dx;
2644 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002645
Tom Hudson984162f2014-10-10 13:38:16 -04002646 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002647 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002648 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002649 }
2650
2651 setupDraw();
2652 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002653 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002654 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002655 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002656 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002657 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002658 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2659 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002660 setupDrawTexture(shadow->id);
2661 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002662 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002663 setupDrawShaderUniforms(getShader(paint));
Raph Levien416a8472012-07-19 22:48:17 -07002664 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2665
2666 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2667}
2668
Romain Guy768bffc2013-02-27 13:50:45 -08002669bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002670 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Romain Guy768bffc2013-02-27 13:50:45 -08002671 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2672}
2673
Tom Hudson107843d2014-09-08 11:26:26 -04002674void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002675 const float* positions, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002676 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002677 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002678 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002679
Romain Guy671d6cf2012-01-18 12:39:17 -08002680 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002681 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002682 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002683 }
2684
Chris Craik39a908c2013-06-13 14:39:01 -07002685 mCaches.enableScissor();
2686
Romain Guy671d6cf2012-01-18 12:39:17 -08002687 float x = 0.0f;
2688 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002689 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002690 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002691 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2692 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002693 }
2694
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002695 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002696 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002697
2698 int alpha;
2699 SkXfermode::Mode mode;
2700 getAlphaAndMode(paint, &alpha, &mode);
2701
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002702 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002703 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002704 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002705 }
2706
Romain Guy671d6cf2012-01-18 12:39:17 -08002707 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002708 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002709 if (pureTranslate && !linearFilter) {
2710 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2711 }
Romain Guy257ae352013-03-20 16:31:12 -07002712 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002713
Tom Hudson984162f2014-10-10 13:38:16 -04002714 const Rect* clip = pureTranslate ? writableSnapshot()->clipRect : &writableSnapshot()->getLocalClip();
Romain Guy671d6cf2012-01-18 12:39:17 -08002715 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2716
Romain Guy211370f2012-02-01 16:10:55 -08002717 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002718
Victoria Lease1e546812013-06-25 14:25:17 -07002719 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002720 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002721 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002722 if (hasActiveLayer) {
2723 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002724 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002725 }
2726 dirtyLayerUnchecked(bounds, getRegion());
2727 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002728 }
Chet Haase48659092012-05-31 15:21:51 -07002729
Tom Hudson107843d2014-09-08 11:26:26 -04002730 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002731}
2732
Chris Craik59744b72014-07-01 17:56:52 -07002733bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002734 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002735 outMatrix->setIdentity();
2736 return false;
2737 } else if (CC_UNLIKELY(transform.isPerspective())) {
2738 outMatrix->setIdentity();
2739 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002740 }
Chris Craik59744b72014-07-01 17:56:52 -07002741
2742 /**
Chris Craika736cd92014-08-04 13:18:38 -07002743 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2744 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002745 */
2746 float sx, sy;
2747 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002748 outMatrix->setScale(
2749 roundf(fmaxf(1.0f, sx)),
2750 roundf(fmaxf(1.0f, sy)));
2751 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002752}
2753
Tom Hudson984162f2014-10-10 13:38:16 -04002754int OpenGLRenderer::getSaveCount() const {
2755 return mState.getSaveCount();
2756}
2757
2758int OpenGLRenderer::save(int flags) {
2759 return mState.save(flags);
2760}
2761
2762void OpenGLRenderer::restore() {
2763 return mState.restore();
2764}
2765
2766void OpenGLRenderer::restoreToCount(int saveCount) {
2767 return mState.restoreToCount(saveCount);
2768}
2769
2770void OpenGLRenderer::translate(float dx, float dy, float dz) {
2771 return mState.translate(dx, dy, dz);
2772}
2773
2774void OpenGLRenderer::rotate(float degrees) {
2775 return mState.rotate(degrees);
2776}
2777
2778void OpenGLRenderer::scale(float sx, float sy) {
2779 return mState.scale(sx, sy);
2780}
2781
2782void OpenGLRenderer::skew(float sx, float sy) {
2783 return mState.skew(sx, sy);
2784}
2785
2786void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2787 mState.setMatrix(matrix);
2788}
2789
2790void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2791 mState.concatMatrix(matrix);
2792}
2793
2794bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2795 return mState.clipRect(left, top, right, bottom, op);
2796}
2797
2798bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2799 return mState.clipPath(path, op);
2800}
2801
2802bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2803 return mState.clipRegion(region, op);
2804}
2805
2806void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2807 mState.setClippingOutline(allocator, outline);
2808}
2809
2810void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2811 const Rect& rect, float radius, bool highPriority) {
2812 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2813}
2814
2815
2816
Tom Hudson107843d2014-09-08 11:26:26 -04002817void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002818 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002819 DrawOpMode drawOpMode) {
2820
Chris Craik527a3aa2013-03-04 10:19:31 -08002821 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002822 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2823 // drawing as ops from DeferredDisplayList are already filtered for these
Tom Hudson984162f2014-10-10 13:38:16 -04002824 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002825 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002826 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002827 }
Romain Guycac5fd32011-12-01 20:08:50 -08002828 }
2829
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002830 const float oldX = x;
2831 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002832
Chris Craikd6b65f62014-01-01 14:45:21 -08002833 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002834 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002835
Romain Guy211370f2012-02-01 16:10:55 -08002836 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002837 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2838 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002839 }
2840
Romain Guy86568192010-12-14 15:55:39 -08002841 int alpha;
2842 SkXfermode::Mode mode;
2843 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002844
Romain Guyc74f45a2013-02-26 19:10:14 -08002845 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2846
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002847 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002848 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002849 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002850 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002851 }
2852
Romain Guy19d4dd82013-03-04 11:14:26 -08002853 const bool hasActiveLayer = hasLayer();
2854
Romain Guy624234f2013-03-05 16:43:31 -08002855 // We only pass a partial transform to the font renderer. That partial
2856 // matrix defines how glyphs are rasterized. Typically we want glyphs
2857 // to be rasterized at their final size on screen, which means the partial
2858 // matrix needs to take the scale factor into account.
2859 // When a partial matrix is used to transform glyphs during rasterization,
2860 // the mesh is generated with the inverse transform (in the case of scale,
2861 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2862 // apply the full transform matrix at draw time in the vertex shader.
2863 // Applying the full matrix in the shader is the easiest way to handle
2864 // rotation and perspective and allows us to always generated quads in the
2865 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002866 SkMatrix fontTransform;
2867 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2868 || fabs(y - (int) y) > 0.0f
2869 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002870 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002871 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002872
Romain Guy624234f2013-03-05 16:43:31 -08002873 // TODO: Implement better clipping for scaled/rotated text
Tom Hudson984162f2014-10-10 13:38:16 -04002874 const Rect* clip = !pureTranslate ? NULL : mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002875 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 -07002876
Raph Levien996e57c2012-07-23 15:22:52 -07002877 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002878 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002879
2880 // don't call issuedrawcommand, do it at end of batch
2881 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002882 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002883 SkPaint paintCopy(*paint);
2884 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2885 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002886 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002887 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002888 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002889 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002890 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002891
Chris Craik527a3aa2013-03-04 10:19:31 -08002892 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002893 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002894 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002895 }
Chris Craik41541822013-05-03 16:35:54 -07002896 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002897 }
Romain Guy694b5192010-07-21 21:33:20 -07002898
Chris Craike63f7c622013-10-17 10:30:55 -07002899 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002900
Tom Hudson107843d2014-09-08 11:26:26 -04002901 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002902}
2903
Tom Hudson107843d2014-09-08 11:26:26 -04002904void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002905 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002906 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002907 return;
Romain Guy03d58522012-02-24 17:54:07 -08002908 }
2909
Chris Craik39a908c2013-06-13 14:39:01 -07002910 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2911 mCaches.enableScissor();
2912
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002913 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002914 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002915 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002916
2917 int alpha;
2918 SkXfermode::Mode mode;
2919 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002920 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002921
Tom Hudson984162f2014-10-10 13:38:16 -04002922 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002923 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 -08002924
Romain Guy97771732012-02-28 18:17:02 -08002925 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002926
2927 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07002928 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002929 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002930 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002931 dirtyLayerUnchecked(bounds, getRegion());
2932 }
Romain Guy97771732012-02-28 18:17:02 -08002933 }
Chet Haase48659092012-05-31 15:21:51 -07002934
Tom Hudson107843d2014-09-08 11:26:26 -04002935 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002936}
2937
Tom Hudson107843d2014-09-08 11:26:26 -04002938void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002939 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002940
Romain Guya1d3c912011-12-13 14:55:06 -08002941 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002942
Romain Guyfb8b7632010-08-23 21:05:08 -07002943 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002944 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002945 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002946
Romain Guy8b55f372010-08-18 17:10:07 -07002947 const float x = texture->left - texture->offset;
2948 const float y = texture->top - texture->offset;
2949
Romain Guy01d58e42011-01-19 21:54:02 -08002950 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002951 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002952}
2953
Tom Hudson107843d2014-09-08 11:26:26 -04002954void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002955 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002956 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002957 }
2958
Romain Guyb2e2f242012-10-17 18:18:35 -07002959 mat4* transform = NULL;
2960 if (layer->isTextureLayer()) {
2961 transform = &layer->getTransform();
2962 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002963 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002964 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002965 }
2966 }
2967
Chris Craik39a908c2013-06-13 14:39:01 -07002968 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04002969 const bool rejected = mState.calculateQuickRejectForScissor(x, y,
Chris Craikdeeda3d2014-05-05 19:09:33 -07002970 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, NULL, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002971
2972 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002973 if (transform && !transform->isIdentity()) {
2974 restore();
2975 }
Tom Hudson107843d2014-09-08 11:26:26 -04002976 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002977 }
2978
Chris Craik62d307c2014-07-29 10:35:13 -07002979 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2980 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2981
Romain Guy5bb3c732012-11-29 17:52:58 -08002982 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002983
Chris Craik39a908c2013-06-13 14:39:01 -07002984 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08002985 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002986
Romain Guy211370f2012-02-01 16:10:55 -08002987 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002988 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002989 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2990 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002991 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002992
Chris Craik16ecda52013-03-29 10:59:59 -07002993 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002994 setupDraw();
2995 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002996 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002997 setupDrawColorFilter(layer->getColorFilter());
2998 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002999 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003000 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003001 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07003002 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08003003 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3004 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3005 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003006
Romain Guyd21b6e12011-11-30 20:21:23 -08003007 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003008 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003009 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003010 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003011 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003012 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003013 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3014 }
Romain Guyf219da52011-01-16 12:54:25 -08003015
Romain Guy448455f2013-07-22 13:57:50 -07003016 TextureVertex* mesh = &layer->mesh[0];
3017 GLsizei elementsCount = layer->meshElementCount;
3018
3019 while (elementsCount > 0) {
3020 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3021
Romain Guy3380cfd2013-08-15 16:57:57 -07003022 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003023 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3024 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3025
3026 elementsCount -= drawCount;
3027 // Though there are 4 vertices in a quad, we use 6 indices per
3028 // quad to draw with GL_TRIANGLES
3029 mesh += (drawCount / 6) * 4;
3030 }
Romain Guyf219da52011-01-16 12:54:25 -08003031
Romain Guy3a3133d2011-02-01 22:59:58 -08003032#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003033 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003034#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003035 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003036
Romain Guy5bb3c732012-11-29 17:52:58 -08003037 if (layer->debugDrawUpdate) {
3038 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003039
3040 SkPaint paint;
3041 paint.setColor(0x7f00ff00);
3042 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003043 }
Romain Guyf219da52011-01-16 12:54:25 -08003044 }
Chris Craik34416ea2013-04-15 16:08:28 -07003045 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003046
Romain Guyb2e2f242012-10-17 18:18:35 -07003047 if (transform && !transform->isIdentity()) {
3048 restore();
3049 }
3050
Tom Hudson107843d2014-09-08 11:26:26 -04003051 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003052}
3053
Romain Guy6926c722010-07-12 20:20:03 -07003054///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003055// Draw filters
3056///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003057void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3058 // We should never get here since we apply the draw filter when stashing
3059 // the paints in the DisplayList.
3060 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003061}
3062
3063///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003064// Drawing implementation
3065///////////////////////////////////////////////////////////////////////////////
3066
Chris Craikd218a922014-01-02 17:13:34 -08003067Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003068 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003069 if (!texture) {
3070 return mCaches.textureCache.get(bitmap);
3071 }
3072 return texture;
3073}
3074
Romain Guy01d58e42011-01-19 21:54:02 -08003075void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003076 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003077 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003078 return;
3079 }
3080
3081 int alpha;
3082 SkXfermode::Mode mode;
3083 getAlphaAndMode(paint, &alpha, &mode);
3084
3085 setupDraw();
3086 setupDrawWithTexture(true);
3087 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003088 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003089 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003090 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003091 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003092 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3093 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003094 setupDrawTexture(texture->id);
3095 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003096 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003097 setupDrawShaderUniforms(getShader(paint));
Romain Guy01d58e42011-01-19 21:54:02 -08003098 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3099
3100 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003101}
3102
Romain Guyf607bdc2010-09-10 19:20:06 -07003103// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003104#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3105#define kStdUnderline_Offset (1.0f / 9.0f)
3106#define kStdUnderline_Thickness (1.0f / 18.0f)
3107
Chris Craikd218a922014-01-02 17:13:34 -08003108void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3109 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003110 // Handle underline and strike-through
3111 uint32_t flags = paint->getFlags();
3112 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003113 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003114
Romain Guy211370f2012-02-01 16:10:55 -08003115 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003116 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003117 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003118
Raph Levien8b4072d2012-07-30 15:50:00 -07003119 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003120 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003121
Romain Guyf6834472011-01-23 13:32:12 -08003122 int linesCount = 0;
3123 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3124 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3125
3126 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003127 float points[pointsCount];
3128 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003129
3130 if (flags & SkPaint::kUnderlineText_Flag) {
3131 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003132 points[currentPoint++] = left;
3133 points[currentPoint++] = top;
3134 points[currentPoint++] = left + underlineWidth;
3135 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003136 }
3137
3138 if (flags & SkPaint::kStrikeThruText_Flag) {
3139 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003140 points[currentPoint++] = left;
3141 points[currentPoint++] = top;
3142 points[currentPoint++] = left + underlineWidth;
3143 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003144 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003145
Romain Guy726aeba2011-06-01 14:52:00 -07003146 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003147
Romain Guy726aeba2011-06-01 14:52:00 -07003148 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003149 }
3150 }
3151}
3152
Tom Hudson107843d2014-09-08 11:26:26 -04003153void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003154 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003155 return;
Romain Guy672433d2013-01-04 19:05:13 -08003156 }
3157
Tom Hudson107843d2014-09-08 11:26:26 -04003158 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003159}
3160
Tom Hudson107843d2014-09-08 11:26:26 -04003161void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003162 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003163 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003164
Chris Craik15a07a22014-01-26 13:43:53 -08003165 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craikf57776b2013-10-25 18:30:17 -07003166 mCaches.enableScissor();
3167
3168 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003169 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003170
ztenghui14a4e352014-08-13 10:44:39 -07003171 // The caller has made sure casterAlpha > 0.
3172 float ambientShadowAlpha = mAmbientShadowAlpha;
3173 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3174 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3175 }
3176 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3177 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003178 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003179 }
ztenghui7b4516e2014-01-07 10:42:55 -08003180
ztenghui14a4e352014-08-13 10:44:39 -07003181 float spotShadowAlpha = mSpotShadowAlpha;
3182 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3183 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3184 }
3185 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3186 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003187 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003188 }
ztenghui7b4516e2014-01-07 10:42:55 -08003189
Tom Hudson107843d2014-09-08 11:26:26 -04003190 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003191}
3192
Tom Hudson107843d2014-09-08 11:26:26 -04003193void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003194 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003195 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003196 return;
Romain Guy3b753822013-03-05 10:27:35 -08003197 }
Romain Guy735738c2012-12-03 12:34:51 -08003198
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003199 int color = paint->getColor();
3200 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003201 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003202 color |= 0x00ffffff;
3203 }
3204
Romain Guy672433d2013-01-04 19:05:13 -08003205 float left = FLT_MAX;
3206 float top = FLT_MAX;
3207 float right = FLT_MIN;
3208 float bottom = FLT_MIN;
3209
Romain Guy448455f2013-07-22 13:57:50 -07003210 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003211 Vertex* vertex = mesh;
3212
Chris Craik2af46352012-11-26 18:30:17 -08003213 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003214 float l = rects[index + 0];
3215 float t = rects[index + 1];
3216 float r = rects[index + 2];
3217 float b = rects[index + 3];
3218
Romain Guy3b753822013-03-05 10:27:35 -08003219 Vertex::set(vertex++, l, t);
3220 Vertex::set(vertex++, r, t);
3221 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003222 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003223
Romain Guy3b753822013-03-05 10:27:35 -08003224 left = fminf(left, l);
3225 top = fminf(top, t);
3226 right = fmaxf(right, r);
3227 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003228 }
3229
Chris Craikf0a59072013-11-19 18:00:46 -08003230 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003231 return;
Romain Guya362c692013-02-04 13:50:16 -08003232 }
Romain Guy672433d2013-01-04 19:05:13 -08003233
Romain Guy672433d2013-01-04 19:05:13 -08003234 setupDraw();
3235 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003236 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003237 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003238 setupDrawColorFilter(getColorFilter(paint));
3239 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003240 setupDrawProgram();
3241 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003242 setupDrawModelView(kModelViewMode_Translate, false,
3243 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003244 setupDrawColorUniforms(getShader(paint));
3245 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003246 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003247
Romain Guy8ce00302013-01-15 18:51:42 -08003248 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003249 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003250 }
3251
Chris Craik4063a0e2013-11-15 16:06:56 -08003252 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003253
Tom Hudson107843d2014-09-08 11:26:26 -04003254 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003255}
3256
Romain Guy026c5e162010-06-28 17:12:22 -07003257void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003258 const SkPaint* paint, bool ignoreTransform) {
3259 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003260 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003261 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003262 color |= 0x00ffffff;
3263 }
3264
Romain Guy70ca14e2010-12-13 18:24:33 -08003265 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003266 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003267 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003268 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003269 setupDrawColorFilter(getColorFilter(paint));
3270 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003271 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003272 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3273 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003274 setupDrawColorUniforms(getShader(paint));
3275 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003276 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003277 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003278
Romain Guyc95c8d62010-09-17 15:31:32 -07003279 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3280}
3281
Romain Guy82ba8142010-07-09 13:25:56 -07003282void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003283 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003284 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003285
Romain Guy3b748a42013-04-17 18:54:38 -07003286 GLvoid* vertices = (GLvoid*) NULL;
3287 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3288
3289 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003290 vertices = &mMeshVertices[0].x;
3291 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003292
3293 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3294 texture->uvMapper->map(uvs);
3295
3296 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3297 }
3298
Chris Craikd6b65f62014-01-01 14:45:21 -08003299 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3300 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3301 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003302
Romain Guyd21b6e12011-11-30 20:21:23 -08003303 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003304 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003305 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003306 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003307 } else {
Chris Craik678625242014-02-28 12:26:34 -08003308 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003309 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003310 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3311 }
3312
3313 if (texture->uvMapper) {
3314 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003315 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003316}
3317
Romain Guyf7f93552010-07-08 19:17:03 -07003318void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003319 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003320 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003321 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3322 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003323
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003324 int a;
3325 SkXfermode::Mode mode;
3326 getAlphaAndMode(paint, &a, &mode);
3327 const float alpha = a / 255.0f;
3328
Romain Guy746b7402010-10-26 16:27:31 -07003329 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003330 setupDrawWithTexture();
3331 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003332 setupDrawColorFilter(getColorFilter(paint));
3333 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003334 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003335 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003336 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003337 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003338 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003339 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003340 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003341
Romain Guy6820ac82010-09-15 18:11:50 -07003342 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003343}
3344
Romain Guy3b748a42013-04-17 18:54:38 -07003345void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003346 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003347 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003348 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3349 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003350
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003351 int a;
3352 SkXfermode::Mode mode;
3353 getAlphaAndMode(paint, &a, &mode);
3354 const float alpha = a / 255.0f;
3355
Romain Guy3b748a42013-04-17 18:54:38 -07003356 setupDraw();
3357 setupDrawWithTexture();
3358 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003359 setupDrawColorFilter(getColorFilter(paint));
3360 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003361 setupDrawProgram();
3362 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003363 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003364 setupDrawTexture(texture);
3365 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003366 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003367 setupDrawMeshIndices(vertices, texCoords, vbo);
3368
3369 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003370}
3371
Romain Guy886b2752013-01-04 12:26:18 -08003372void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003373 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003374 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003375 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003376
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003377 int color = paint != NULL ? paint->getColor() : 0;
3378 int alpha;
3379 SkXfermode::Mode mode;
3380 getAlphaAndMode(paint, &alpha, &mode);
3381
Romain Guy886b2752013-01-04 12:26:18 -08003382 setupDraw();
3383 setupDrawWithTexture(true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003384 if (paint != NULL) {
Romain Guy886b2752013-01-04 12:26:18 -08003385 setupDrawAlpha8Color(color, alpha);
3386 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003387 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003388 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003389 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003390 setupDrawProgram();
3391 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003392 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003393 setupDrawTexture(texture);
3394 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003395 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003396 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003397 setupDrawMesh(vertices, texCoords);
3398
3399 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003400}
3401
Romain Guya5aed0d2010-09-09 14:42:43 -07003402void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003403 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003404
Tom Hudson984162f2014-10-10 13:38:16 -04003405 if (writableSnapshot()->roundRectClipState != NULL /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003406 blend = true;
3407 mDescription.hasRoundRectClip = true;
3408 }
3409 mSkipOutlineClip = true;
3410
Romain Guy82ba8142010-07-09 13:25:56 -07003411 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003412
Romain Guy82ba8142010-07-09 13:25:56 -07003413 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003414 // These blend modes are not supported by OpenGL directly and have
3415 // to be implemented using shaders. Since the shader will perform
3416 // the blending, turn blending off here
3417 // If the blend mode cannot be implemented using shaders, fall
3418 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003419 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003420 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003421 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003422 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003423
Romain Guy82bc7a72012-01-03 14:13:39 -08003424 if (mCaches.blend) {
3425 glDisable(GL_BLEND);
3426 mCaches.blend = false;
3427 }
3428
3429 return;
3430 } else {
3431 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003432 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003433 }
3434
3435 if (!mCaches.blend) {
3436 glEnable(GL_BLEND);
3437 }
3438
3439 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3440 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3441
3442 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3443 glBlendFunc(sourceMode, destMode);
3444 mCaches.lastSrcMode = sourceMode;
3445 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003446 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003447 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003448 glDisable(GL_BLEND);
3449 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003450 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003451}
3452
Romain Guy889f8d12010-07-29 14:37:42 -07003453bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003454 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003455 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003456 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003457 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003458 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003459 }
Romain Guy6926c722010-07-12 20:20:03 -07003460 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003461}
3462
Romain Guy026c5e162010-06-28 17:12:22 -07003463void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003464 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003465 TextureVertex::setUV(v++, u1, v1);
3466 TextureVertex::setUV(v++, u2, v1);
3467 TextureVertex::setUV(v++, u1, v2);
3468 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003469}
3470
Chris Craikd218a922014-01-02 17:13:34 -08003471void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003472 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003473 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3474 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003475 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003476 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003477 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003478}
3479
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003480float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003481 float alpha;
3482 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3483 alpha = mDrawModifiers.mOverrideLayerAlpha;
3484 } else {
3485 alpha = layer->getAlpha() / 255.0f;
3486 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003487 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003488}
3489
Romain Guy9d5316e2010-06-24 19:30:36 -07003490}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003491}; // namespace android