blob: 91cc4e04c4f5eea73048f34be7199a97b6956baf [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>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy08aa2cb2011-03-17 11:06:57 -070029#include <private/hwui/DrawGlInfo.h>
30
Romain Guy5b3b3522010-10-27 18:57:51 -070031#include <ui/Rect.h>
32
Romain Guy85bf02f2010-06-22 13:11:24 -070033#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080034#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Romain Guy40543602013-06-12 15:31:28 -070036#include "Fence.h"
Chris Craik65cd6122012-12-10 17:56:27 -080037#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070038#include "Properties.h"
Romain Guya957eea2010-12-08 18:34:42 -080039#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070040
41namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070042namespace uirenderer {
43
44///////////////////////////////////////////////////////////////////////////////
45// Defines
46///////////////////////////////////////////////////////////////////////////////
47
Romain Guy759ea802010-09-16 20:49:46 -070048#define RAD_TO_DEG (180.0f / 3.14159265f)
49#define MIN_ANGLE 0.001f
50
Romain Guyf8773082012-07-12 18:01:00 -070051#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070052
Romain Guy713e1bb2012-10-16 18:44:09 -070053#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080054
Romain Guy9d5316e2010-06-24 19:30:36 -070055///////////////////////////////////////////////////////////////////////////////
56// Globals
57///////////////////////////////////////////////////////////////////////////////
58
Romain Guy889f8d12010-07-29 14:37:42 -070059/**
60 * Structure mapping Skia xfermodes to OpenGL blending factors.
61 */
62struct Blender {
63 SkXfermode::Mode mode;
64 GLenum src;
65 GLenum dst;
66}; // struct Blender
67
Romain Guy026c5e162010-06-28 17:12:22 -070068// In this array, the index of each Blender equals the value of the first
69// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
70static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070071 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
73 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
74 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
75 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
76 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
78 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
79 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
82 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050084 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070085 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070086};
Romain Guye4d01122010-06-16 18:44:05 -070087
Romain Guy87a76572010-09-13 18:11:21 -070088// This array contains the swapped version of each SkXfermode. For instance
89// this array's SrcOver blending mode is actually DstOver. You can refer to
90// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070091static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070092 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
94 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
95 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
96 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
98 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
101 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
102 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
104 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500105 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700106 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700107};
108
Romain Guyf6a11b82010-06-23 17:47:49 -0700109///////////////////////////////////////////////////////////////////////////////
110// Constructors/destructor
111///////////////////////////////////////////////////////////////////////////////
112
Romain Guy3bbacf22013-02-06 16:51:04 -0800113OpenGLRenderer::OpenGLRenderer():
114 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800115 // *set* draw modifiers to be 0
116 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700117 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700118
Romain Guyac670c02010-07-27 17:39:27 -0700119 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
120
Romain Guyae5575b2010-07-29 18:48:04 -0700121 mFirstSnapshot = new Snapshot;
Romain Guy96885eb2013-03-26 15:05:58 -0700122 mFrameStarted = false;
Romain Guy78dd96d2013-05-03 14:24:16 -0700123 mCountOverdraw = false;
Romain Guy87e2f7572012-09-24 11:37:12 -0700124
125 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700126}
127
Romain Guy85bf02f2010-06-22 13:11:24 -0700128OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700129 // The context has already been destroyed at this point, do not call
130 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700131}
132
Romain Guy87e2f7572012-09-24 11:37:12 -0700133void OpenGLRenderer::initProperties() {
134 char property[PROPERTY_VALUE_MAX];
135 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
136 mScissorOptimizationDisabled = !strcasecmp(property, "true");
137 INIT_LOGD(" Scissor optimization %s",
138 mScissorOptimizationDisabled ? "disabled" : "enabled");
139 } else {
140 INIT_LOGD(" Scissor optimization enabled");
141 }
Romain Guy13631f32012-01-30 17:41:55 -0800142}
143
144///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700145// Setup
146///////////////////////////////////////////////////////////////////////////////
147
Romain Guyef359272013-01-31 19:07:29 -0800148void OpenGLRenderer::setName(const char* name) {
149 if (name) {
150 mName.setTo(name);
151 } else {
152 mName.clear();
153 }
154}
155
156const char* OpenGLRenderer::getName() const {
157 return mName.string();
158}
159
Romain Guy49c5fc02012-05-15 11:10:01 -0700160bool OpenGLRenderer::isDeferred() {
161 return false;
162}
163
Romain Guy85bf02f2010-06-22 13:11:24 -0700164void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700165 initViewport(width, height);
166
167 glDisable(GL_DITHER);
168 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
169
170 glEnableVertexAttribArray(Program::kBindingPosition);
171}
172
173void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700174 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700175
176 mWidth = width;
177 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700178
179 mFirstSnapshot->height = height;
180 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700181}
182
Romain Guy96885eb2013-03-26 15:05:58 -0700183void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800184 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800185 mCaches.clearGarbage();
186
Romain Guy96885eb2013-03-26 15:05:58 -0700187 mOpaque = opaque;
Romain Guy8aef54f2010-09-01 15:13:49 -0700188 mSnapshot = new Snapshot(mFirstSnapshot,
189 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800190 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700191 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700192
Romain Guy7d7b5492011-01-24 16:33:45 -0800193 mSnapshot->setClip(left, top, right, bottom);
Chris Craik5f803622013-03-21 14:39:04 -0700194 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700195}
196
197status_t OpenGLRenderer::startFrame() {
198 if (mFrameStarted) return DrawGlInfo::kStatusDone;
199 mFrameStarted = true;
200
Romain Guy41308e22012-10-22 20:02:43 -0700201 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700202
Romain Guy96885eb2013-03-26 15:05:58 -0700203 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700204
Romain Guy96885eb2013-03-26 15:05:58 -0700205 glViewport(0, 0, mWidth, mHeight);
Romain Guy7d7b5492011-01-24 16:33:45 -0800206
Romain Guy54c1a642012-09-27 17:55:46 -0700207 // Functors break the tiling extension in pretty spectacular ways
208 // This ensures we don't use tiling when a functor is going to be
209 // invoked during the frame
210 mSuppressTiling = mCaches.hasRegisteredFunctors();
211
Chris Craik5f803622013-03-21 14:39:04 -0700212 startTiling(mSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700213
Romain Guy7c450aa2012-09-21 19:15:00 -0700214 debugOverdraw(true, true);
215
Romain Guy96885eb2013-03-26 15:05:58 -0700216 return clear(mTilingClip.left, mTilingClip.top,
217 mTilingClip.right, mTilingClip.bottom, mOpaque);
218}
219
220status_t OpenGLRenderer::prepare(bool opaque) {
221 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
222}
223
224status_t OpenGLRenderer::prepareDirty(float left, float top,
225 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700226
Romain Guy96885eb2013-03-26 15:05:58 -0700227 setupFrameState(left, top, right, bottom, opaque);
228
229 // Layer renderers will start the frame immediately
230 // The framebuffer renderer will first defer the display list
231 // for each layer and wait until the first drawing command
232 // to start the frame
233 if (mSnapshot->fbo == 0) {
234 syncState();
235 updateLayers();
236 } else {
237 return startFrame();
238 }
239
240 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700241}
242
Romain Guydcfc8362013-01-03 13:08:57 -0800243void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
244 // If we know that we are going to redraw the entire framebuffer,
245 // perform a discard to let the driver know we don't need to preserve
246 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800247 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800248 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800249 const bool isFbo = getTargetFbo() == 0;
250 const GLenum attachments[] = {
251 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
252 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800253 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
254 }
255}
256
Romain Guy7c25aab2012-10-18 15:05:02 -0700257status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700258 if (!opaque || mCountOverdraw) {
Romain Guy586cae32012-07-13 15:28:31 -0700259 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700260 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700261 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700262 return DrawGlInfo::kStatusDrew;
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();
Chet Haase44b2fe32012-06-06 19:03:58 -0700266 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700267}
268
269void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700270 if (mCaches.blend) {
271 glEnable(GL_BLEND);
272 } else {
273 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700274 }
Romain Guybb9524b2010-06-22 18:56:38 -0700275}
276
Romain Guy57b52682012-09-20 17:38:46 -0700277void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700278 if (!mSuppressTiling) {
Chris Craik5f803622013-03-21 14:39:04 -0700279 Rect* clip = &mTilingClip;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800280 if (s->flags & Snapshot::kFlagFboTarget) {
Chris Craik5f803622013-03-21 14:39:04 -0700281 clip = &(s->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700282 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700283
Romain Guyc3fedaf2013-01-29 17:26:25 -0800284 startTiling(*clip, s->height, opaque);
285 }
286}
287
288void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
289 if (!mSuppressTiling) {
290 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800291 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700292 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700293}
294
295void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700296 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700297}
298
Romain Guyb025b9c2010-09-16 14:16:48 -0700299void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700300 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700301 endTiling();
302
Romain Guyca89e2a2013-03-08 17:44:20 -0800303 // When finish() is invoked on FBO 0 we've reached the end
304 // of the current frame
305 if (getTargetFbo() == 0) {
306 mCaches.pathCache.trim();
307 }
308
Romain Guy11cb6422012-09-21 00:39:43 -0700309 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700310#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700311 GLenum status = GL_NO_ERROR;
312 while ((status = glGetError()) != GL_NO_ERROR) {
313 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
314 switch (status) {
315 case GL_INVALID_ENUM:
316 ALOGE(" GL_INVALID_ENUM");
317 break;
318 case GL_INVALID_VALUE:
319 ALOGE(" GL_INVALID_VALUE");
320 break;
321 case GL_INVALID_OPERATION:
322 ALOGE(" GL_INVALID_OPERATION");
323 break;
324 case GL_OUT_OF_MEMORY:
325 ALOGE(" Out of memory!");
326 break;
327 }
Romain Guya07105b2011-01-10 21:14:18 -0800328 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700329#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700330
Romain Guyc15008e2010-11-10 11:59:15 -0800331#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800332 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700333#else
334 if (mCaches.getDebugLevel() & kDebugMemory) {
335 mCaches.dumpMemoryUsage();
336 }
Romain Guyc15008e2010-11-10 11:59:15 -0800337#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700338 }
Romain Guy96885eb2013-03-26 15:05:58 -0700339
Romain Guy78dd96d2013-05-03 14:24:16 -0700340 if (mCountOverdraw) {
341 countOverdraw();
342 }
343
Romain Guy96885eb2013-03-26 15:05:58 -0700344 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700345}
346
Romain Guy6c319ca2011-01-11 14:29:25 -0800347void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700348 if (mCaches.currentProgram) {
349 if (mCaches.currentProgram->isInUse()) {
350 mCaches.currentProgram->remove();
351 mCaches.currentProgram = NULL;
352 }
353 }
Romain Guy50c0f092010-10-19 11:42:22 -0700354 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800355 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800356 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800357 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700358 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700359}
360
Romain Guy6c319ca2011-01-11 14:29:25 -0800361void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800362 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800363 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700364 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700365 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700366
Romain Guy3e263fa2011-12-12 16:47:48 -0800367 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
368
Chet Haase80250612012-08-15 13:46:54 -0700369 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700370 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800371 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700372 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700373
Romain Guya1d3c912011-12-13 14:55:06 -0800374 mCaches.activeTexture(0);
Romain Guy8aa195d2013-06-04 18:00:09 -0700375 mCaches.resetBoundTextures();
Romain Guyf607bdc2010-09-10 19:20:06 -0700376
Romain Guy50c0f092010-10-19 11:42:22 -0700377 mCaches.blend = true;
378 glEnable(GL_BLEND);
379 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
380 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700381}
382
Romain Guy35643dd2012-09-18 15:40:58 -0700383void OpenGLRenderer::resumeAfterLayer() {
384 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
385 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
386 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700387 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700388
389 mCaches.resetScissor();
390 dirtyClip();
391}
392
Romain Guyba6be8a2012-04-23 18:22:09 -0700393void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700394 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700395}
396
397void OpenGLRenderer::attachFunctor(Functor* functor) {
398 mFunctors.add(functor);
399}
400
Romain Guy8f3b8e32012-03-27 16:33:45 -0700401status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
402 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700403 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700404
Romain Guyba6be8a2012-04-23 18:22:09 -0700405 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800406 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700407 SortedVector<Functor*> functors(mFunctors);
408 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700409
Romain Guyba6be8a2012-04-23 18:22:09 -0700410 DrawGlInfo info;
411 info.clipLeft = 0;
412 info.clipTop = 0;
413 info.clipRight = 0;
414 info.clipBottom = 0;
415 info.isLayer = false;
416 info.width = 0;
417 info.height = 0;
418 memset(info.transform, 0, sizeof(float) * 16);
419
420 for (size_t i = 0; i < count; i++) {
421 Functor* f = functors.itemAt(i);
422 result |= (*f)(DrawGlInfo::kModeProcess, &info);
423
Chris Craikc2c95432012-04-25 15:13:52 -0700424 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700425 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
426 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700427 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700428
Chris Craikc2c95432012-04-25 15:13:52 -0700429 if (result & DrawGlInfo::kStatusInvoke) {
430 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700431 }
432 }
Chris Craikd15321b2012-11-28 14:45:04 -0800433 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700434 }
435
436 return result;
437}
438
439status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700440 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
441
Chet Haasedaf98e92011-01-10 14:10:36 -0800442 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700443 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700444
Romain Guy8a4ac612012-07-17 17:32:48 -0700445 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800446 if (mDirtyClip) {
447 setScissorFromClip();
448 }
Romain Guyd643bb52011-03-01 14:55:21 -0800449
Romain Guy80911b82011-03-16 15:30:12 -0700450 Rect clip(*mSnapshot->clipRect);
451 clip.snapToPixelBoundaries();
452
Romain Guyd643bb52011-03-01 14:55:21 -0800453 // Since we don't know what the functor will draw, let's dirty
454 // tne entire clip region
455 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800456 dirtyLayerUnchecked(clip, getRegion());
457 }
Romain Guyd643bb52011-03-01 14:55:21 -0800458
Romain Guy08aa2cb2011-03-17 11:06:57 -0700459 DrawGlInfo info;
460 info.clipLeft = clip.left;
461 info.clipTop = clip.top;
462 info.clipRight = clip.right;
463 info.clipBottom = clip.bottom;
464 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700465 info.width = getSnapshot()->viewport.getWidth();
466 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700467 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700468
Chris Craik4a2bff72013-04-16 13:50:16 -0700469 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800470
Romain Guy8f3b8e32012-03-27 16:33:45 -0700471 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700472 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800473 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700474
Chris Craik65924a32012-04-05 17:52:11 -0700475 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700476 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700477 }
Romain Guycabfcc12011-03-07 18:06:46 -0800478 }
479
Chet Haasedaf98e92011-01-10 14:10:36 -0800480 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700481 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800482}
483
Romain Guyf6a11b82010-06-23 17:47:49 -0700484///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700485// Debug
486///////////////////////////////////////////////////////////////////////////////
487
Romain Guy0f667532013-03-01 14:31:04 -0800488void OpenGLRenderer::eventMark(const char* name) const {
489 mCaches.eventMark(0, name);
490}
491
Romain Guy87e2f7572012-09-24 11:37:12 -0700492void OpenGLRenderer::startMark(const char* name) const {
493 mCaches.startMark(0, name);
494}
495
496void OpenGLRenderer::endMark() const {
497 mCaches.endMark();
498}
499
500void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
501 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
502 if (clear) {
503 mCaches.disableScissor();
504 mCaches.stencil.clear();
505 }
506 if (enable) {
507 mCaches.stencil.enableDebugWrite();
508 } else {
509 mCaches.stencil.disable();
510 }
511 }
512}
513
514void OpenGLRenderer::renderOverdraw() {
515 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700516 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700517
518 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700519 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700520 clip->right - clip->left, clip->bottom - clip->top);
521
522 mCaches.stencil.enableDebugTest(2);
523 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
524 mCaches.stencil.enableDebugTest(3);
525 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
526 mCaches.stencil.enableDebugTest(4);
527 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
528 mCaches.stencil.enableDebugTest(4, true);
529 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
530 mCaches.stencil.disable();
531 }
532}
533
Romain Guy78dd96d2013-05-03 14:24:16 -0700534void OpenGLRenderer::countOverdraw() {
535 size_t count = mWidth * mHeight;
536 uint32_t* buffer = new uint32_t[count];
537 glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
538
539 size_t total = 0;
540 for (size_t i = 0; i < count; i++) {
541 total += buffer[i] & 0xff;
542 }
543
544 mOverdraw = total / float(count);
545
546 delete[] buffer;
547}
548
Romain Guy87e2f7572012-09-24 11:37:12 -0700549///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700550// Layers
551///////////////////////////////////////////////////////////////////////////////
552
553bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700554 if (layer->deferredUpdateScheduled && layer->renderer &&
555 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700556 ATRACE_CALL();
557
Romain Guy11cb6422012-09-21 00:39:43 -0700558 Rect& dirty = layer->dirtyRect;
559
Romain Guy7c450aa2012-09-21 19:15:00 -0700560 if (inFrame) {
561 endTiling();
562 debugOverdraw(false, false);
563 }
Romain Guy11cb6422012-09-21 00:39:43 -0700564
Romain Guy96885eb2013-03-26 15:05:58 -0700565 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700566 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700567 } else {
568 layer->defer();
569 }
Romain Guy11cb6422012-09-21 00:39:43 -0700570
571 if (inFrame) {
572 resumeAfterLayer();
573 startTiling(mSnapshot);
574 }
575
Romain Guy5bb3c732012-11-29 17:52:58 -0800576 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700577 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700578
579 return true;
580 }
581
582 return false;
583}
584
585void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700586 // If draw deferring is enabled this method will simply defer
587 // the display list of each individual layer. The layers remain
588 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700589 int count = mLayerUpdates.size();
590 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700591 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
592 startMark("Layer Updates");
593 } else {
594 startMark("Defer Layer Updates");
595 }
Romain Guy11cb6422012-09-21 00:39:43 -0700596
Chris Craik1206b9b2013-04-04 14:46:24 -0700597 // Note: it is very important to update the layers in order
598 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700599 Layer* layer = mLayerUpdates.itemAt(i);
600 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700601 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
602 mCaches.resourceCache.decrementRefcount(layer);
603 }
Romain Guy11cb6422012-09-21 00:39:43 -0700604 }
Romain Guy11cb6422012-09-21 00:39:43 -0700605
Romain Guy96885eb2013-03-26 15:05:58 -0700606 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
607 mLayerUpdates.clear();
608 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
609 }
610 endMark();
611 }
612}
613
614void OpenGLRenderer::flushLayers() {
615 int count = mLayerUpdates.size();
616 if (count > 0) {
617 startMark("Apply Layer Updates");
618 char layerName[12];
619
Chris Craik1206b9b2013-04-04 14:46:24 -0700620 // Note: it is very important to update the layers in order
621 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700622 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700623 startMark(layerName);
624
Romain Guy40543602013-06-12 15:31:28 -0700625 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700626 Layer* layer = mLayerUpdates.itemAt(i);
627 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700628 ATRACE_END();
629
Romain Guy02b49b72013-03-29 12:37:16 -0700630 mCaches.resourceCache.decrementRefcount(layer);
631
Romain Guy96885eb2013-03-26 15:05:58 -0700632 endMark();
633 }
634
635 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700636 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700637
Romain Guy11cb6422012-09-21 00:39:43 -0700638 endMark();
639 }
640}
641
642void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
643 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700644 // Make sure we don't introduce duplicates.
645 // SortedVector would do this automatically but we need to respect
646 // the insertion order. The linear search is not an issue since
647 // this list is usually very short (typically one item, at most a few)
648 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
649 if (mLayerUpdates.itemAt(i) == layer) {
650 return;
651 }
652 }
Romain Guy11cb6422012-09-21 00:39:43 -0700653 mLayerUpdates.push_back(layer);
654 mCaches.resourceCache.incrementRefcount(layer);
655 }
656}
657
658void OpenGLRenderer::clearLayerUpdates() {
659 size_t count = mLayerUpdates.size();
660 if (count > 0) {
661 mCaches.resourceCache.lock();
662 for (size_t i = 0; i < count; i++) {
663 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
664 }
665 mCaches.resourceCache.unlock();
666 mLayerUpdates.clear();
667 }
668}
669
Romain Guy40543602013-06-12 15:31:28 -0700670void OpenGLRenderer::flushLayerUpdates() {
671 syncState();
672 updateLayers();
673 flushLayers();
674 // Wait for all the layer updates to be executed
675 AutoFence fence;
676}
677
Romain Guy11cb6422012-09-21 00:39:43 -0700678///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700679// State management
680///////////////////////////////////////////////////////////////////////////////
681
Romain Guybb9524b2010-06-22 18:56:38 -0700682int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700683 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700684}
685
686int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700687 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700688}
689
690void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700691 if (mSaveCount > 1) {
692 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700693 }
Romain Guybb9524b2010-06-22 18:56:38 -0700694}
695
696void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700697 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700698
Romain Guy8fb95422010-08-17 18:38:51 -0700699 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700700 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700701 }
Romain Guybb9524b2010-06-22 18:56:38 -0700702}
703
Romain Guy8aef54f2010-09-01 15:13:49 -0700704int OpenGLRenderer::saveSnapshot(int flags) {
705 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700706 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700707}
708
709bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700710 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700711 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700712 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700713
Romain Guybd6b79b2010-06-26 00:13:53 -0700714 sp<Snapshot> current = mSnapshot;
715 sp<Snapshot> previous = mSnapshot->previous;
716
Romain Guyeb993562010-10-05 18:14:38 -0700717 if (restoreOrtho) {
718 Rect& r = previous->viewport;
719 glViewport(r.left, r.top, r.right, r.bottom);
720 mOrthoMatrix.load(current->orthoMatrix);
721 }
722
Romain Guy8b55f372010-08-18 17:10:07 -0700723 mSaveCount--;
724 mSnapshot = previous;
725
Romain Guy2542d192010-08-18 11:47:12 -0700726 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700727 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700728 }
Romain Guy2542d192010-08-18 11:47:12 -0700729
Romain Guy5ec99242010-11-03 16:19:08 -0700730 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700731 endMark(); // Savelayer
732 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700733 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700734 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700735 }
736
Romain Guy2542d192010-08-18 11:47:12 -0700737 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700738}
739
Romain Guyf6a11b82010-06-23 17:47:49 -0700740///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700741// Layers
742///////////////////////////////////////////////////////////////////////////////
743
744int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800745 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700746 const GLuint previousFbo = mSnapshot->fbo;
747 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700748
Romain Guyaf636eb2010-12-09 17:47:21 -0800749 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700750 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700751 }
Romain Guyd55a8612010-06-28 17:42:46 -0700752
753 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700754}
755
Chris Craikd90144d2013-03-19 15:03:48 -0700756void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
757 const Rect untransformedBounds(bounds);
758
759 currentTransform().mapRect(bounds);
760
761 // Layers only make sense if they are in the framebuffer's bounds
762 if (bounds.intersect(*mSnapshot->clipRect)) {
763 // We cannot work with sub-pixels in this case
764 bounds.snapToPixelBoundaries();
765
766 // When the layer is not an FBO, we may use glCopyTexImage so we
767 // need to make sure the layer does not extend outside the bounds
768 // of the framebuffer
769 if (!bounds.intersect(mSnapshot->previous->viewport)) {
770 bounds.setEmpty();
771 } else if (fboLayer) {
772 clip.set(bounds);
773 mat4 inverse;
774 inverse.loadInverse(currentTransform());
775 inverse.mapRect(clip);
776 clip.snapToPixelBoundaries();
777 if (clip.intersect(untransformedBounds)) {
778 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
779 bounds.set(untransformedBounds);
780 } else {
781 clip.setEmpty();
782 }
783 }
784 } else {
785 bounds.setEmpty();
786 }
787}
788
Chris Craik408eb122013-03-26 18:55:15 -0700789void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
790 bool fboLayer, int alpha) {
791 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
792 bounds.getHeight() > mCaches.maxTextureSize ||
793 (fboLayer && clip.isEmpty())) {
794 mSnapshot->empty = fboLayer;
795 } else {
796 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
797 }
798}
799
Chris Craikd90144d2013-03-19 15:03:48 -0700800int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
801 int alpha, SkXfermode::Mode mode, int flags) {
802 const GLuint previousFbo = mSnapshot->fbo;
803 const int count = saveSnapshot(flags);
804
805 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
806 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
807 // operations will be able to store and restore the current clip and transform info, and
808 // quick rejection will be correct (for display lists)
809
810 Rect bounds(left, top, right, bottom);
811 Rect clip;
812 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700813 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700814
Chris Craik408eb122013-03-26 18:55:15 -0700815 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700816 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
817 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
818 }
819 }
820
821 return count;
822}
823
824
Romain Guy1c740bc2010-09-13 18:00:09 -0700825/**
826 * Layers are viewed by Skia are slightly different than layers in image editing
827 * programs (for instance.) When a layer is created, previously created layers
828 * and the frame buffer still receive every drawing command. For instance, if a
829 * layer is created and a shape intersecting the bounds of the layers and the
830 * framebuffer is draw, the shape will be drawn on both (unless the layer was
831 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
832 *
833 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
834 * texture. Unfortunately, this is inefficient as it requires every primitive to
835 * be drawn n + 1 times, where n is the number of active layers. In practice this
836 * means, for every primitive:
837 * - Switch active frame buffer
838 * - Change viewport, clip and projection matrix
839 * - Issue the drawing
840 *
841 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700842 * To avoid this, layers are implemented in a different way here, at least in the
843 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
844 * is set. When this flag is set we can redirect all drawing operations into a
845 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700846 *
847 * This implementation relies on the frame buffer being at least RGBA 8888. When
848 * a layer is created, only a texture is created, not an FBO. The content of the
849 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700850 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700851 * buffer and drawing continues as normal. This technique therefore treats the
852 * frame buffer as a scratch buffer for the layers.
853 *
854 * To compose the layers back onto the frame buffer, each layer texture
855 * (containing the original frame buffer data) is drawn as a simple quad over
856 * the frame buffer. The trick is that the quad is set as the composition
857 * destination in the blending equation, and the frame buffer becomes the source
858 * of the composition.
859 *
860 * Drawing layers with an alpha value requires an extra step before composition.
861 * An empty quad is drawn over the layer's region in the frame buffer. This quad
862 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
863 * quad is used to multiply the colors in the frame buffer. This is achieved by
864 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
865 * GL_ZERO, GL_SRC_ALPHA.
866 *
867 * Because glCopyTexImage2D() can be slow, an alternative implementation might
868 * be use to draw a single clipped layer. The implementation described above
869 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700870 *
871 * (1) The frame buffer is actually not cleared right away. To allow the GPU
872 * to potentially optimize series of calls to glCopyTexImage2D, the frame
873 * buffer is left untouched until the first drawing operation. Only when
874 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700875 */
Chet Haased48885a2012-08-28 17:43:28 -0700876bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
877 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700878 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700879 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700880
Romain Guyeb993562010-10-05 18:14:38 -0700881 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
882
Romain Guyf607bdc2010-09-10 19:20:06 -0700883 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700884 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700885 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700886 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700887 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700888
889 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700890 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700891 return false;
892 }
Romain Guyf18fd992010-07-08 11:45:51 -0700893
Romain Guya1d3c912011-12-13 14:55:06 -0800894 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700895 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700896 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700897 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700898 }
899
Romain Guy9ace8f52011-07-07 20:50:11 -0700900 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700901 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700902 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
903 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800904 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700905 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700906 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700907
Romain Guy8fb95422010-08-17 18:38:51 -0700908 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700909 mSnapshot->flags |= Snapshot::kFlagIsLayer;
910 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700911
Chris Craik7273daa2013-03-28 11:25:24 -0700912 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700913 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700914 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700915 } else {
916 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700917 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800918 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700919 if (layer->isEmpty()) {
920 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700921 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700922 layer->getWidth(), layer->getHeight(), 0);
923 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800924 } else {
925 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700926 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800927 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700928
Romain Guy54be1cd2011-06-13 19:04:27 -0700929 // Enqueue the buffer coordinates to clear the corresponding region later
930 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700931 }
Romain Guyeb993562010-10-05 18:14:38 -0700932 }
Romain Guyf86ef572010-07-01 11:05:42 -0700933
Romain Guyd55a8612010-06-28 17:42:46 -0700934 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700935}
936
Chet Haased48885a2012-08-28 17:43:28 -0700937bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800938 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700939 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700940
Chet Haased48885a2012-08-28 17:43:28 -0700941 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800942 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
943 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700944 mSnapshot->fbo = layer->getFbo();
945 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
946 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
947 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
948 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700949 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700950
Romain Guy2b7028e2012-09-19 17:25:38 -0700951 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700952 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700953 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700954 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
955 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700956
957 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700958 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700959 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700960 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700961 }
962
963 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700964 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700965
Romain Guyf735c8e2013-01-31 17:45:55 -0800966 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700967
968 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700969 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800970 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700971 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700972 glClear(GL_COLOR_BUFFER_BIT);
973
974 dirtyClip();
975
976 // Change the ortho projection
977 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
978 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
979
980 return true;
981}
982
Romain Guy1c740bc2010-09-13 18:00:09 -0700983/**
984 * Read the documentation of createLayer() before doing anything in this method.
985 */
Romain Guy1d83e192010-08-17 11:37:00 -0700986void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
987 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000988 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700989 return;
990 }
991
Romain Guy8ce00302013-01-15 18:51:42 -0800992 Layer* layer = current->layer;
993 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700994 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700995
Chris Craik39a908c2013-06-13 14:39:01 -0700996 bool clipRequired = false;
997 quickRejectNoScissor(rect, &clipRequired); // safely ignore return, should never be rejected
998 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
999
Romain Guyeb993562010-10-05 18:14:38 -07001000 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -07001001 endTiling();
1002
Romain Guye0aa84b2012-04-03 19:30:26 -07001003 // Detach the texture from the FBO
1004 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -08001005
1006 layer->removeFbo(false);
1007
Romain Guyeb993562010-10-05 18:14:38 -07001008 // Unbind current FBO and restore previous one
1009 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -07001010 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -07001011
1012 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -07001013 }
1014
Romain Guy9ace8f52011-07-07 20:50:11 -07001015 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001016 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -07001017 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001018 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001019 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001020 }
1021
Romain Guy03750a02010-10-18 14:06:08 -07001022 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001023
Romain Guya1d3c912011-12-13 14:55:06 -08001024 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001025
Romain Guy5b3b3522010-10-27 18:57:51 -07001026 // When the layer is stored in an FBO, we can save a bit of fillrate by
1027 // drawing only the dirty region
1028 if (fboLayer) {
1029 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001030 if (layer->getColorFilter()) {
1031 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001032 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001033 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001034 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001035 resetColorFilter();
1036 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001037 } else if (!rect.isEmpty()) {
1038 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
1039 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001040 }
Romain Guy8b55f372010-08-18 17:10:07 -07001041
Romain Guy746b7402010-10-26 16:27:31 -07001042 dirtyClip();
1043
Romain Guyeb993562010-10-05 18:14:38 -07001044 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001045 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001046 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001047 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001048 }
1049}
1050
Romain Guyaa6c24c2011-04-28 18:40:04 -07001051void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craike83569c2013-03-20 16:57:09 -07001052 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guyaa6c24c2011-04-28 18:40:04 -07001053
1054 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001055 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001056 setupDrawWithTexture();
1057 } else {
1058 setupDrawWithExternalTexture();
1059 }
1060 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001061 setupDrawColor(alpha, alpha, alpha, alpha);
1062 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001063 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001064 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001065 setupDrawPureColorUniforms();
1066 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001067 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1068 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001069 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001070 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001071 }
Romain Guy3b753822013-03-05 10:27:35 -08001072 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001073 layer->getWidth() == (uint32_t) rect.getWidth() &&
1074 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001075 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1076 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001077
Romain Guyd21b6e12011-11-30 20:21:23 -08001078 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001079 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1080 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001081 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001082 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1083 }
1084 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001085 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1086
1087 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1088
1089 finishDrawTexture();
1090}
1091
Romain Guy5b3b3522010-10-27 18:57:51 -07001092void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001093 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001094 const Rect& texCoords = layer->texCoords;
1095 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1096 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001097
Romain Guy9ace8f52011-07-07 20:50:11 -07001098 float x = rect.left;
1099 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001100 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001101 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001102 layer->getHeight() == (uint32_t) rect.getHeight();
1103
1104 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001105 // When we're swapping, the layer is already in screen coordinates
1106 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001107 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1108 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001109 }
1110
Romain Guyd21b6e12011-11-30 20:21:23 -08001111 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001112 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001113 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001114 }
1115
Chris Craik16ecda52013-03-29 10:59:59 -07001116 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001117 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001118 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001119 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001120 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1121 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001122
Romain Guyaa6c24c2011-04-28 18:40:04 -07001123 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1124 } else {
1125 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1126 drawTextureLayer(layer, rect);
1127 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1128 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001129}
1130
Chris Craik34416ea2013-04-15 16:08:28 -07001131/**
1132 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1133 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1134 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1135 * by saveLayer's restore
1136 */
1137#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1138 DRAW_COMMAND; \
1139 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1140 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1141 DRAW_COMMAND; \
1142 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1143 } \
1144 }
1145
1146#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1147
Romain Guy5b3b3522010-10-27 18:57:51 -07001148void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001149 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001150 layer->setRegionAsRect();
1151
Chris Craik34416ea2013-04-15 16:08:28 -07001152 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001153
Romain Guy5b3b3522010-10-27 18:57:51 -07001154 layer->region.clear();
1155 return;
1156 }
1157
Romain Guy8a3957d2011-09-07 17:55:15 -07001158 // TODO: See LayerRenderer.cpp::generateMesh() for important
1159 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -08001160 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001161 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001162 const android::Rect* rects;
1163 Region safeRegion;
1164 if (CC_LIKELY(hasRectToRectTransform())) {
1165 rects = layer->region.getArray(&count);
1166 } else {
1167 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1168 rects = safeRegion.getArray(&count);
1169 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001170
Chris Craik16ecda52013-03-29 10:59:59 -07001171 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001172 const float texX = 1.0f / float(layer->getWidth());
1173 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001174 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001175
Romain Guy8ce00302013-01-15 18:51:42 -08001176 setupDraw();
1177
1178 // We must get (and therefore bind) the region mesh buffer
1179 // after we setup drawing in case we need to mess with the
1180 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001181 TextureVertex* mesh = mCaches.getRegionMesh();
1182 GLsizei numQuads = 0;
1183
Romain Guy7230a742011-01-10 22:26:16 -08001184 setupDrawWithTexture();
1185 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001186 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001187 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001188 setupDrawProgram();
1189 setupDrawDirtyRegionsDisabled();
1190 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001191 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001192 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001193 if (currentTransform().isPureTranslate()) {
1194 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1195 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001196
Romain Guyd21b6e12011-11-30 20:21:23 -08001197 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001198 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1199 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001200 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001201 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1202 }
Romain Guy15bc6432011-12-13 13:11:32 -08001203 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001204
1205 for (size_t i = 0; i < count; i++) {
1206 const android::Rect* r = &rects[i];
1207
1208 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001209 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001210 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001211 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001212
1213 // TODO: Reject quads outside of the clip
1214 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1215 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1216 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1217 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1218
1219 numQuads++;
1220
1221 if (numQuads >= REGION_MESH_QUAD_COUNT) {
Chris Craik34416ea2013-04-15 16:08:28 -07001222 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1223 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001224 numQuads = 0;
1225 mesh = mCaches.getRegionMesh();
1226 }
1227 }
1228
1229 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001230 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1231 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001232 }
1233
Romain Guy7230a742011-01-10 22:26:16 -08001234 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001235
1236#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001237 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001238#endif
1239
1240 layer->region.clear();
1241 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001242}
1243
Romain Guy3a3133d2011-02-01 22:59:58 -08001244void OpenGLRenderer::drawRegionRects(const Region& region) {
1245#if DEBUG_LAYERS_AS_REGIONS
1246 size_t count;
1247 const android::Rect* rects = region.getArray(&count);
1248
1249 uint32_t colors[] = {
1250 0x7fff0000, 0x7f00ff00,
1251 0x7f0000ff, 0x7fff00ff,
1252 };
1253
1254 int offset = 0;
1255 int32_t top = rects[0].top;
1256
1257 for (size_t i = 0; i < count; i++) {
1258 if (top != rects[i].top) {
1259 offset ^= 0x2;
1260 top = rects[i].top;
1261 }
1262
1263 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1264 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1265 SkXfermode::kSrcOver_Mode);
1266 }
1267#endif
1268}
1269
Romain Guy8ce00302013-01-15 18:51:42 -08001270void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1271 SkXfermode::Mode mode, bool dirty) {
1272 int count = 0;
1273 Vector<float> rects;
1274
1275 SkRegion::Iterator it(region);
1276 while (!it.done()) {
1277 const SkIRect& r = it.rect();
1278 rects.push(r.fLeft);
1279 rects.push(r.fTop);
1280 rects.push(r.fRight);
1281 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001282 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001283 it.next();
1284 }
1285
Romain Guy3bbacf22013-02-06 16:51:04 -08001286 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001287}
1288
Romain Guy5b3b3522010-10-27 18:57:51 -07001289void OpenGLRenderer::dirtyLayer(const float left, const float top,
1290 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001291 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001292 Rect bounds(left, top, right, bottom);
1293 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001294 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001295 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001296}
1297
1298void OpenGLRenderer::dirtyLayer(const float left, const float top,
1299 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001300 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001301 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001302 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001303 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001304}
1305
1306void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001307 if (bounds.intersect(*mSnapshot->clipRect)) {
1308 bounds.snapToPixelBoundaries();
1309 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1310 if (!dirty.isEmpty()) {
1311 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001312 }
1313 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001314}
1315
Romain Guy54be1cd2011-06-13 19:04:27 -07001316void OpenGLRenderer::clearLayerRegions() {
1317 const size_t count = mLayers.size();
1318 if (count == 0) return;
1319
1320 if (!mSnapshot->isIgnored()) {
1321 // Doing several glScissor/glClear here can negatively impact
1322 // GPUs with a tiler architecture, instead we draw quads with
1323 // the Clear blending mode
1324
1325 // The list contains bounds that have already been clipped
1326 // against their initial clip rect, and the current clip
1327 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001328 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001329
1330 Vertex mesh[count * 6];
1331 Vertex* vertex = mesh;
1332
1333 for (uint32_t i = 0; i < count; i++) {
1334 Rect* bounds = mLayers.itemAt(i);
1335
1336 Vertex::set(vertex++, bounds->left, bounds->bottom);
1337 Vertex::set(vertex++, bounds->left, bounds->top);
1338 Vertex::set(vertex++, bounds->right, bounds->top);
1339 Vertex::set(vertex++, bounds->left, bounds->bottom);
1340 Vertex::set(vertex++, bounds->right, bounds->top);
1341 Vertex::set(vertex++, bounds->right, bounds->bottom);
1342
1343 delete bounds;
1344 }
Romain Guye67307c2013-02-11 18:01:20 -08001345 // We must clear the list of dirty rects before we
1346 // call setupDraw() to prevent stencil setup to do
1347 // the same thing again
1348 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001349
1350 setupDraw(false);
1351 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1352 setupDrawBlending(true, SkXfermode::kClear_Mode);
1353 setupDrawProgram();
1354 setupDrawPureColorUniforms();
1355 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001356 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001357
Romain Guy54be1cd2011-06-13 19:04:27 -07001358 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001359
1360 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001361 } else {
1362 for (uint32_t i = 0; i < count; i++) {
1363 delete mLayers.itemAt(i);
1364 }
Romain Guye67307c2013-02-11 18:01:20 -08001365 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001366 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001367}
1368
Romain Guybd6b79b2010-06-26 00:13:53 -07001369///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001370// State Deferral
1371///////////////////////////////////////////////////////////////////////////////
1372
Chris Craikff785832013-03-08 13:12:16 -08001373bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001374 const Rect& currentClip = *(mSnapshot->clipRect);
1375 const mat4& currentMatrix = *(mSnapshot->transform);
1376
Chris Craikff785832013-03-08 13:12:16 -08001377 if (stateDeferFlags & kStateDeferFlag_Draw) {
1378 // state has bounds initialized in local coordinates
1379 if (!state.mBounds.isEmpty()) {
1380 currentMatrix.mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001381 Rect clippedBounds(state.mBounds);
1382 if(!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001383 // quick rejected
1384 return true;
1385 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001386
Chris Craika02c4ed2013-06-14 13:43:58 -07001387 state.mClipSideFlags = kClipSide_None;
Chris Craik28ce94a2013-05-31 11:38:03 -07001388 if (!currentClip.contains(state.mBounds)) {
1389 int& flags = state.mClipSideFlags;
1390 // op partially clipped, so record which sides are clipped for clip-aware merging
1391 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1392 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1393 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1394 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
1395 }
1396 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001397 } else {
Romain Guy2db5e992013-05-21 15:29:59 -07001398 // If we don't have bounds, let's assume we're clipped
1399 // to prevent merging
Chris Craik28ce94a2013-05-31 11:38:03 -07001400 state.mClipSideFlags = kClipSide_Full;
Chris Craikff785832013-03-08 13:12:16 -08001401 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001402 }
1403 }
1404
Chris Craik527a3aa2013-03-04 10:19:31 -08001405 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1406 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001407 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001408 }
1409
Chris Craik7273daa2013-03-28 11:25:24 -07001410 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1411 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001412 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001413 state.mDrawModifiers = mDrawModifiers;
1414 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001415 return false;
1416}
1417
Chris Craik527a3aa2013-03-04 10:19:31 -08001418void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001419 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001420 mDrawModifiers = state.mDrawModifiers;
1421 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001422
Chris Craik527a3aa2013-03-04 10:19:31 -08001423 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001424 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1425 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001426 dirtyClip();
1427 }
Chris Craikc3566d02013-02-04 16:16:33 -08001428}
1429
Chris Craik28ce94a2013-05-31 11:38:03 -07001430/**
1431 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1432 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1433 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1434 *
1435 * This method should be called when restoreDisplayState() won't be restoring the clip
1436 */
1437void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1438 if (clipRect != NULL) {
1439 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1440 } else {
1441 mSnapshot->setClip(0, 0, mWidth, mHeight);
1442 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001443 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001444 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001445}
1446
Chris Craikc3566d02013-02-04 16:16:33 -08001447///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001448// Transforms
1449///////////////////////////////////////////////////////////////////////////////
1450
1451void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy4c2547f2013-06-11 16:19:24 -07001452 currentTransform().translate(dx, dy);
Romain Guyf6a11b82010-06-23 17:47:49 -07001453}
1454
1455void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001456 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001457}
1458
1459void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001460 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001461}
1462
Romain Guy807daf72011-01-18 11:19:19 -08001463void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001464 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001465}
1466
Romain Guyf6a11b82010-06-23 17:47:49 -07001467void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001468 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001469 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001470 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001471 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001472 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001473}
1474
Chris Craikb98a0162013-02-21 11:30:22 -08001475bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001476 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001477}
1478
Romain Guyf6a11b82010-06-23 17:47:49 -07001479void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001480 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001481}
1482
1483void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001484 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001485 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001486 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001487 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001488}
1489
1490///////////////////////////////////////////////////////////////////////////////
1491// Clipping
1492///////////////////////////////////////////////////////////////////////////////
1493
Romain Guybb9524b2010-06-22 18:56:38 -07001494void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001495 Rect clip(*mSnapshot->clipRect);
1496 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001497
Romain Guy8a4ac612012-07-17 17:32:48 -07001498 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1499 clip.getWidth(), clip.getHeight())) {
1500 mDirtyClip = false;
1501 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001502}
1503
Romain Guy8ce00302013-01-15 18:51:42 -08001504void OpenGLRenderer::ensureStencilBuffer() {
1505 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1506 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1507 // just hope we have one when hasLayer() returns false.
1508 if (hasLayer()) {
1509 attachStencilBufferToLayer(mSnapshot->layer);
1510 }
1511}
1512
1513void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1514 // The layer's FBO is already bound when we reach this stage
1515 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001516 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1517 // is attached after we initiated tiling. We must turn it off,
1518 // attach the new render buffer then turn tiling back on
1519 endTiling();
1520
Romain Guy8d4aeb72013-02-12 16:08:55 -08001521 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001522 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001523 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001524
Romain Guyf735c8e2013-01-31 17:45:55 -08001525 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001526 }
1527}
1528
1529void OpenGLRenderer::setStencilFromClip() {
1530 if (!mCaches.debugOverdraw) {
1531 if (!mSnapshot->clipRegion->isEmpty()) {
1532 // NOTE: The order here is important, we must set dirtyClip to false
1533 // before any draw call to avoid calling back into this method
1534 mDirtyClip = false;
1535
1536 ensureStencilBuffer();
1537
1538 mCaches.stencil.enableWrite();
1539
1540 // Clear the stencil but first make sure we restrict drawing
1541 // to the region's bounds
1542 bool resetScissor = mCaches.enableScissor();
1543 if (resetScissor) {
1544 // The scissor was not set so we now need to update it
1545 setScissorFromClip();
1546 }
1547 mCaches.stencil.clear();
1548 if (resetScissor) mCaches.disableScissor();
1549
1550 // NOTE: We could use the region contour path to generate a smaller mesh
1551 // Since we are using the stencil we could use the red book path
1552 // drawing technique. It might increase bandwidth usage though.
1553
1554 // The last parameter is important: we are not drawing in the color buffer
1555 // so we don't want to dirty the current layer, if any
1556 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1557
1558 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001559
1560 // Draw the region used to generate the stencil if the appropriate debug
1561 // mode is enabled
1562 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1563 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1564 }
Romain Guy8ce00302013-01-15 18:51:42 -08001565 } else {
1566 mCaches.stencil.disable();
1567 }
1568 }
1569}
1570
Romain Guy9d5316e2010-06-24 19:30:36 -07001571const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001572 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001573}
1574
Chris Craik39a908c2013-06-13 14:39:01 -07001575bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1576 bool* clipRequired) {
1577 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001578 return true;
1579 }
1580
1581 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001582 currentTransform().mapRect(r);
Romain Guy8a4ac612012-07-17 17:32:48 -07001583 r.snapToPixelBoundaries();
1584
1585 Rect clipRect(*mSnapshot->clipRect);
1586 clipRect.snapToPixelBoundaries();
1587
Chris Craik39a908c2013-06-13 14:39:01 -07001588 if (!clipRect.intersects(r)) return true;
Romain Guy8a4ac612012-07-17 17:32:48 -07001589
Chris Craik39a908c2013-06-13 14:39:01 -07001590 if (clipRequired) *clipRequired = !clipRect.contains(r);
1591 return false;
Romain Guy35643dd2012-09-18 15:40:58 -07001592}
1593
Romain Guy672433d2013-01-04 19:05:13 -08001594bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1595 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001596 if (paint->getStyle() != SkPaint::kFill_Style) {
1597 float outset = paint->getStrokeWidth() * 0.5f;
1598 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1599 } else {
1600 return quickReject(left, top, right, bottom);
1601 }
1602}
1603
Romain Guyc7d53492010-06-25 13:41:57 -07001604bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craik39a908c2013-06-13 14:39:01 -07001605 bool clipRequired = false;
1606 if (quickRejectNoScissor(left, top, right, bottom, &clipRequired)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001607 return true;
1608 }
1609
Chris Craik39a908c2013-06-13 14:39:01 -07001610 if (!isDeferred()) {
1611 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001612 }
Chris Craik39a908c2013-06-13 14:39:01 -07001613 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001614}
1615
Romain Guy8ce00302013-01-15 18:51:42 -08001616void OpenGLRenderer::debugClip() {
1617#if DEBUG_CLIP_REGIONS
1618 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1619 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1620 }
1621#endif
1622}
1623
Romain Guy079ba2c2010-07-16 14:12:24 -07001624bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001625 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001626 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1627 if (clipped) {
1628 dirtyClip();
1629 }
1630 return !mSnapshot->clipRect->isEmpty();
1631 }
1632
1633 SkPath path;
1634 path.addRect(left, top, right, bottom);
1635
1636 return clipPath(&path, op);
1637}
1638
1639bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1640 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001641 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001642
1643 SkPath transformed;
1644 path->transform(transform, &transformed);
1645
1646 SkRegion clip;
1647 if (!mSnapshot->clipRegion->isEmpty()) {
1648 clip.setRegion(*mSnapshot->clipRegion);
1649 } else {
1650 Rect* bounds = mSnapshot->clipRect;
1651 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1652 }
1653
1654 SkRegion region;
1655 region.setPath(transformed, clip);
1656
1657 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001658 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001659 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001660 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001661 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001662}
1663
Romain Guy735738c2012-12-03 12:34:51 -08001664bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001665 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1666 if (clipped) {
1667 dirtyClip();
1668 }
1669 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001670}
1671
Chet Haasea23eed82012-04-12 15:19:04 -07001672Rect* OpenGLRenderer::getClipRect() {
1673 return mSnapshot->clipRect;
1674}
1675
Romain Guyf6a11b82010-06-23 17:47:49 -07001676///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001677// Drawing commands
1678///////////////////////////////////////////////////////////////////////////////
1679
Romain Guy54be1cd2011-06-13 19:04:27 -07001680void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001681 // TODO: It would be best if we could do this before quickReject()
1682 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001683 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001684 // Make sure setScissor & setStencil happen at the beginning of
1685 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001686 if (mDirtyClip) {
1687 if (mCaches.scissorEnabled) {
1688 setScissorFromClip();
1689 }
Romain Guy8ce00302013-01-15 18:51:42 -08001690 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001691 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001692
Romain Guy70ca14e2010-12-13 18:24:33 -08001693 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001694
Romain Guy70ca14e2010-12-13 18:24:33 -08001695 mSetShaderColor = false;
1696 mColorSet = false;
1697 mColorA = mColorR = mColorG = mColorB = 0.0f;
1698 mTextureUnit = 0;
1699 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001700
1701 // Enable debug highlight when what we're about to draw is tested against
1702 // the stencil buffer and if stencil highlight debugging is on
1703 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1704 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1705 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001706
1707 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001708}
1709
1710void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1711 mDescription.hasTexture = true;
1712 mDescription.hasAlpha8Texture = isAlpha8;
1713}
1714
Romain Guyff316ec2013-02-13 18:39:43 -08001715void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1716 mDescription.hasTexture = true;
1717 mDescription.hasColors = true;
1718 mDescription.hasAlpha8Texture = isAlpha8;
1719}
1720
Romain Guyaa6c24c2011-04-28 18:40:04 -07001721void OpenGLRenderer::setupDrawWithExternalTexture() {
1722 mDescription.hasExternalTexture = true;
1723}
1724
Romain Guy15bc6432011-12-13 13:11:32 -08001725void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001726 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001727}
1728
Chris Craik710f46d2012-09-17 17:25:49 -07001729void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001730 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001731}
1732
Romain Guy8d0d4782010-12-14 20:13:35 -08001733void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1734 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001735 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1736 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1737 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001738 mColorSet = true;
1739 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1740}
1741
Romain Guy86568192010-12-14 15:55:39 -08001742void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1743 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001744 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1745 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1746 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001747 mColorSet = true;
1748 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1749}
1750
Romain Guy41210632012-07-16 17:04:24 -07001751void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1752 mCaches.fontRenderer->describe(mDescription, paint);
1753}
1754
Romain Guy70ca14e2010-12-13 18:24:33 -08001755void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1756 mColorA = a;
1757 mColorR = r;
1758 mColorG = g;
1759 mColorB = b;
1760 mColorSet = true;
1761 mSetShaderColor = mDescription.setColor(r, g, b, a);
1762}
1763
1764void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001765 if (mDrawModifiers.mShader) {
1766 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001767 }
1768}
1769
1770void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001771 if (mDrawModifiers.mColorFilter) {
1772 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001773 }
1774}
1775
Romain Guyf09ef512011-05-27 11:43:46 -07001776void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1777 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1778 mColorA = 1.0f;
1779 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001780 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001781 }
1782}
1783
Romain Guy70ca14e2010-12-13 18:24:33 -08001784void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001785 // When the blending mode is kClear_Mode, we need to use a modulate color
1786 // argb=1,0,0,0
1787 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001788 bool blend = (mColorSet && mColorA < 1.0f) ||
1789 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1790 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001791}
1792
1793void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001794 // When the blending mode is kClear_Mode, we need to use a modulate color
1795 // argb=1,0,0,0
1796 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001797 blend |= (mColorSet && mColorA < 1.0f) ||
1798 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1799 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1800 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001801}
1802
1803void OpenGLRenderer::setupDrawProgram() {
1804 useProgram(mCaches.programCache.get(mDescription));
1805}
1806
1807void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1808 mTrackDirtyRegions = false;
1809}
1810
1811void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1812 bool ignoreTransform) {
1813 mModelView.loadTranslate(left, top, 0.0f);
1814 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001815 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1816 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001817 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001818 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001819 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1820 }
1821}
1822
Chet Haase8a5cc922011-04-26 07:28:09 -07001823void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001824 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001825}
1826
Romain Guy70ca14e2010-12-13 18:24:33 -08001827void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1828 bool ignoreTransform, bool ignoreModelView) {
1829 if (!ignoreModelView) {
1830 mModelView.loadTranslate(left, top, 0.0f);
1831 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001832 } else {
1833 mModelView.loadIdentity();
1834 }
Romain Guy86568192010-12-14 15:55:39 -08001835 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1836 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001837 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001838 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001839 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001840 }
1841 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001842 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001843 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1844 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001845}
1846
1847void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001848 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001849 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1850 }
1851}
1852
Romain Guy86568192010-12-14 15:55:39 -08001853void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001854 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001855 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001856 }
1857}
1858
Romain Guy70ca14e2010-12-13 18:24:33 -08001859void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001860 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001861 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001862 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001863 }
Chris Craikc3566d02013-02-04 16:16:33 -08001864 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1865 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001866 }
1867}
1868
Romain Guy8d0d4782010-12-14 20:13:35 -08001869void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001870 if (mDrawModifiers.mShader) {
1871 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001872 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001873 }
1874}
1875
Romain Guy70ca14e2010-12-13 18:24:33 -08001876void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001877 if (mDrawModifiers.mColorFilter) {
1878 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001879 }
1880}
1881
Romain Guy41210632012-07-16 17:04:24 -07001882void OpenGLRenderer::setupDrawTextGammaUniforms() {
1883 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1884}
1885
Romain Guy70ca14e2010-12-13 18:24:33 -08001886void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001887 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001888 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001889 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001890}
1891
1892void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001893 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001894 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001895 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001896}
1897
Romain Guyaa6c24c2011-04-28 18:40:04 -07001898void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1899 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001900 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001901 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001902}
1903
Romain Guy8f0095c2011-05-02 17:24:22 -07001904void OpenGLRenderer::setupDrawTextureTransform() {
1905 mDescription.hasTextureTransform = true;
1906}
1907
1908void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001909 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1910 GL_FALSE, &transform.data[0]);
1911}
1912
Romain Guy70ca14e2010-12-13 18:24:33 -08001913void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001914 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001915 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001916 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001917 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001918 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001919 }
Romain Guyd71dd362011-12-12 19:03:35 -08001920
Chris Craikcb4d6002012-09-25 12:00:29 -07001921 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001922 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001923 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001924 }
1925
1926 mCaches.unbindIndicesBuffer();
1927}
1928
Romain Guyff316ec2013-02-13 18:39:43 -08001929void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1930 bool force = mCaches.unbindMeshBuffer();
1931 GLsizei stride = sizeof(ColorTextureVertex);
1932
1933 mCaches.bindPositionVertexPointer(force, vertices, stride);
1934 if (mCaches.currentProgram->texCoords >= 0) {
1935 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1936 }
1937 int slot = mCaches.currentProgram->getAttrib("colors");
1938 if (slot >= 0) {
1939 glEnableVertexAttribArray(slot);
1940 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1941 }
1942
1943 mCaches.unbindIndicesBuffer();
1944}
1945
Romain Guy3b748a42013-04-17 18:54:38 -07001946void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1947 bool force = false;
1948 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1949 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1950 // use the default VBO found in Caches
1951 if (!vertices || vbo) {
1952 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1953 } else {
1954 force = mCaches.unbindMeshBuffer();
1955 }
1956 mCaches.bindIndicesBuffer();
1957
Chris Craikcb4d6002012-09-25 12:00:29 -07001958 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001959 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001960 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001961 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001962}
1963
Chet Haase5b0200b2011-04-13 17:58:08 -07001964void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001965 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001966 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001967 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001968}
1969
Romain Guy70ca14e2010-12-13 18:24:33 -08001970void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001971}
1972
1973///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001974// Drawing
1975///////////////////////////////////////////////////////////////////////////////
1976
Chris Craikff785832013-03-08 13:12:16 -08001977status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
1978 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07001979 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08001980 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1981 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001982 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07001983 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07001984 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001985 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
1986 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07001987 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08001988 }
1989
Chris Craik28ce94a2013-05-31 11:38:03 -07001990 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
1991 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001992 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
1993 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001994
1995 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07001996 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001997
Chet Haase58d110a2013-04-10 07:43:29 -07001998 return status | deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08001999 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002000
Romain Guy65549432012-03-26 16:45:05 -07002001 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08002002}
2003
Chris Craikc3566d02013-02-04 16:16:33 -08002004void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07002005 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08002006 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07002007 }
2008}
2009
Romain Guya168d732011-03-18 16:50:13 -07002010void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2011 int alpha;
2012 SkXfermode::Mode mode;
2013 getAlphaAndMode(paint, &alpha, &mode);
2014
Romain Guy886b2752013-01-04 12:26:18 -08002015 int color = paint != NULL ? paint->getColor() : 0;
2016
Romain Guya168d732011-03-18 16:50:13 -07002017 float x = left;
2018 float y = top;
2019
Romain Guy886b2752013-01-04 12:26:18 -08002020 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2021
Romain Guya168d732011-03-18 16:50:13 -07002022 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002023 if (currentTransform().isPureTranslate()) {
2024 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2025 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002026 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002027
2028 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002029 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002030 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002031 }
2032
Romain Guy3b748a42013-04-17 18:54:38 -07002033 // No need to check for a UV mapper on the texture object, only ARGB_8888
2034 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002035 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002036 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2037 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002038}
2039
Chris Craik527a3aa2013-03-04 10:19:31 -08002040status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, int bitmapCount, TextureVertex* vertices,
Romain Guy2db5e992013-05-21 15:29:59 -07002041 bool transformed, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002042 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002043 Texture* texture = getTexture(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002044 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002045
Chris Craik527a3aa2013-03-04 10:19:31 -08002046 const AutoTexture autoCleanup(texture);
2047
2048 int alpha;
2049 SkXfermode::Mode mode;
2050 getAlphaAndMode(paint, &alpha, &mode);
2051
2052 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy2db5e992013-05-21 15:29:59 -07002053 texture->setFilter(transformed ? FILTER(paint) : GL_NEAREST, true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002054
2055 const float x = (int) floorf(bounds.left + 0.5f);
2056 const float y = (int) floorf(bounds.top + 0.5f);
2057 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2058 int color = paint != NULL ? paint->getColor() : 0;
2059 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2060 texture->id, paint != NULL, color, alpha, mode,
2061 &vertices[0].position[0], &vertices[0].texture[0],
2062 GL_TRIANGLES, bitmapCount * 6, true, true);
2063 } else {
2064 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2065 texture->id, alpha / 255.0f, mode, texture->blend,
2066 &vertices[0].position[0], &vertices[0].texture[0],
2067 GL_TRIANGLES, bitmapCount * 6, false, true, 0, true);
2068 }
2069
2070 return DrawGlInfo::kStatusDrew;
2071}
2072
Chet Haase48659092012-05-31 15:21:51 -07002073status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002074 const float right = left + bitmap->width();
2075 const float bottom = top + bitmap->height();
2076
2077 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002078 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002079 }
2080
Romain Guya1d3c912011-12-13 14:55:06 -08002081 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002082 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002083 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002084 const AutoTexture autoCleanup(texture);
2085
Romain Guy211370f2012-02-01 16:10:55 -08002086 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002087 drawAlphaBitmap(texture, left, top, paint);
2088 } else {
2089 drawTextureRect(left, top, right, bottom, texture, paint);
2090 }
Chet Haase48659092012-05-31 15:21:51 -07002091
2092 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002093}
2094
Chet Haase48659092012-05-31 15:21:51 -07002095status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002096 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2097 const mat4 transform(*matrix);
2098 transform.mapRect(r);
2099
Romain Guy6926c722010-07-12 20:20:03 -07002100 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002101 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002102 }
2103
Romain Guya1d3c912011-12-13 14:55:06 -08002104 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002105 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002106 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002107 const AutoTexture autoCleanup(texture);
2108
Romain Guy5b3b3522010-10-27 18:57:51 -07002109 // This could be done in a cheaper way, all we need is pass the matrix
2110 // to the vertex shader. The save/restore is a bit overkill.
2111 save(SkCanvas::kMatrix_SaveFlag);
2112 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002113 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2114 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2115 } else {
2116 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2117 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002118 restore();
Chet Haase48659092012-05-31 15:21:51 -07002119
2120 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002121}
2122
Chet Haase48659092012-05-31 15:21:51 -07002123status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002124 const float right = left + bitmap->width();
2125 const float bottom = top + bitmap->height();
2126
2127 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002128 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002129 }
2130
2131 mCaches.activeTexture(0);
2132 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2133 const AutoTexture autoCleanup(texture);
2134
Romain Guy886b2752013-01-04 12:26:18 -08002135 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2136 drawAlphaBitmap(texture, left, top, paint);
2137 } else {
2138 drawTextureRect(left, top, right, bottom, texture, paint);
2139 }
Chet Haase48659092012-05-31 15:21:51 -07002140
2141 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002142}
2143
Chet Haase48659092012-05-31 15:21:51 -07002144status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002145 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002146 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002147 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002148 }
2149
Chris Craik39a908c2013-06-13 14:39:01 -07002150 // TODO: use quickReject on bounds from vertices
2151 mCaches.enableScissor();
2152
Romain Guyb18d2d02011-02-10 15:52:54 -08002153 float left = FLT_MAX;
2154 float top = FLT_MAX;
2155 float right = FLT_MIN;
2156 float bottom = FLT_MIN;
2157
Romain Guya92bb4d2012-10-16 11:08:44 -07002158 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002159
Romain Guyff316ec2013-02-13 18:39:43 -08002160 ColorTextureVertex mesh[count];
2161 ColorTextureVertex* vertex = mesh;
2162
2163 bool cleanupColors = false;
2164 if (!colors) {
2165 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2166 colors = new int[colorsCount];
2167 memset(colors, 0xff, colorsCount * sizeof(int));
2168 cleanupColors = true;
2169 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002170
Romain Guy3b748a42013-04-17 18:54:38 -07002171 mCaches.activeTexture(0);
2172 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2173 const UvMapper& mapper(getMapper(texture));
2174
Romain Guy5a7b4662011-01-20 19:09:30 -08002175 for (int32_t y = 0; y < meshHeight; y++) {
2176 for (int32_t x = 0; x < meshWidth; x++) {
2177 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2178
2179 float u1 = float(x) / meshWidth;
2180 float u2 = float(x + 1) / meshWidth;
2181 float v1 = float(y) / meshHeight;
2182 float v2 = float(y + 1) / meshHeight;
2183
Romain Guy3b748a42013-04-17 18:54:38 -07002184 mapper.map(u1, v1, u2, v2);
2185
Romain Guy5a7b4662011-01-20 19:09:30 -08002186 int ax = i + (meshWidth + 1) * 2;
2187 int ay = ax + 1;
2188 int bx = i;
2189 int by = bx + 1;
2190 int cx = i + 2;
2191 int cy = cx + 1;
2192 int dx = i + (meshWidth + 1) * 2 + 2;
2193 int dy = dx + 1;
2194
Romain Guyff316ec2013-02-13 18:39:43 -08002195 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2196 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2197 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002198
Romain Guyff316ec2013-02-13 18:39:43 -08002199 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2200 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2201 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002202
Romain Guya92bb4d2012-10-16 11:08:44 -07002203 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2204 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2205 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2206 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002207 }
2208 }
2209
Romain Guya92bb4d2012-10-16 11:08:44 -07002210 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002211 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002212 return DrawGlInfo::kStatusDone;
2213 }
2214
Romain Guyff316ec2013-02-13 18:39:43 -08002215 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002216 texture = mCaches.textureCache.get(bitmap);
2217 if (!texture) {
2218 if (cleanupColors) delete[] colors;
2219 return DrawGlInfo::kStatusDone;
2220 }
Romain Guyff316ec2013-02-13 18:39:43 -08002221 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002222 const AutoTexture autoCleanup(texture);
2223
2224 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2225 texture->setFilter(FILTER(paint), true);
2226
2227 int alpha;
2228 SkXfermode::Mode mode;
2229 getAlphaAndMode(paint, &alpha, &mode);
2230
Romain Guyff316ec2013-02-13 18:39:43 -08002231 float a = alpha / 255.0f;
2232
Romain Guya92bb4d2012-10-16 11:08:44 -07002233 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002234 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002235 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002236
Romain Guyff316ec2013-02-13 18:39:43 -08002237 setupDraw();
2238 setupDrawWithTextureAndColor();
2239 setupDrawColor(a, a, a, a);
2240 setupDrawColorFilter();
2241 setupDrawBlending(true, mode, false);
2242 setupDrawProgram();
2243 setupDrawDirtyRegionsDisabled();
2244 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2245 setupDrawTexture(texture->id);
2246 setupDrawPureColorUniforms();
2247 setupDrawColorFilterUniforms();
2248 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2249
2250 glDrawArrays(GL_TRIANGLES, 0, count);
2251
2252 finishDrawTexture();
2253
2254 int slot = mCaches.currentProgram->getAttrib("colors");
2255 if (slot >= 0) {
2256 glDisableVertexAttribArray(slot);
2257 }
2258
2259 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002260
2261 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002262}
2263
Chet Haase48659092012-05-31 15:21:51 -07002264status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002265 float srcLeft, float srcTop, float srcRight, float srcBottom,
2266 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002267 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002268 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002269 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002270 }
2271
Romain Guya1d3c912011-12-13 14:55:06 -08002272 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002273 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002274 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002275 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002276
Romain Guy8ba548f2010-06-30 19:21:21 -07002277 const float width = texture->width;
2278 const float height = texture->height;
2279
Romain Guy3b748a42013-04-17 18:54:38 -07002280 float u1 = fmax(0.0f, srcLeft / width);
2281 float v1 = fmax(0.0f, srcTop / height);
2282 float u2 = fmin(1.0f, srcRight / width);
2283 float v2 = fmin(1.0f, srcBottom / height);
2284
2285 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002286
Romain Guy03750a02010-10-18 14:06:08 -07002287 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002288 resetDrawTextureTexCoords(u1, v1, u2, v2);
2289
Romain Guy03750a02010-10-18 14:06:08 -07002290 int alpha;
2291 SkXfermode::Mode mode;
2292 getAlphaAndMode(paint, &alpha, &mode);
2293
Romain Guyd21b6e12011-11-30 20:21:23 -08002294 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2295
Romain Guy886b2752013-01-04 12:26:18 -08002296 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2297 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002298
Romain Guy886b2752013-01-04 12:26:18 -08002299 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2300 // Apply a scale transform on the canvas only when a shader is in use
2301 // Skia handles the ratio between the dst and src rects as a scale factor
2302 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002303 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002304 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002305
Romain Guy3b753822013-03-05 10:27:35 -08002306 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2307 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2308 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002309
2310 dstRight = x + (dstRight - dstLeft);
2311 dstBottom = y + (dstBottom - dstTop);
2312
2313 dstLeft = x;
2314 dstTop = y;
2315
2316 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2317 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002318 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002319 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002320 }
2321
2322 if (CC_UNLIKELY(useScaleTransform)) {
2323 save(SkCanvas::kMatrix_SaveFlag);
2324 translate(dstLeft, dstTop);
2325 scale(scaleX, scaleY);
2326
2327 dstLeft = 0.0f;
2328 dstTop = 0.0f;
2329
2330 dstRight = srcRight - srcLeft;
2331 dstBottom = srcBottom - srcTop;
2332 }
2333
2334 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2335 int color = paint ? paint->getColor() : 0;
2336 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2337 texture->id, paint != NULL, color, alpha, mode,
2338 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2339 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2340 } else {
2341 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2342 texture->id, alpha / 255.0f, mode, texture->blend,
2343 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2344 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2345 }
2346
2347 if (CC_UNLIKELY(useScaleTransform)) {
2348 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002349 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002350
2351 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002352
2353 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002354}
2355
Romain Guy3b748a42013-04-17 18:54:38 -07002356status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002357 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002358 int alpha;
2359 SkXfermode::Mode mode;
Chris Craik16ecda52013-03-29 10:59:59 -07002360 getAlphaAndMode(paint, &alpha, &mode);
Romain Guybe6f9dc2012-07-16 12:41:17 -07002361
Romain Guy6926c722010-07-12 20:20:03 -07002362 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002363 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002364 }
2365
Romain Guy4c2547f2013-06-11 16:19:24 -07002366 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002367 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2368 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002369
Romain Guy4c2547f2013-06-11 16:19:24 -07002370 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, alpha, mode);
2371}
2372
2373status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh,
2374 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2375 int alpha, SkXfermode::Mode mode) {
2376
2377 if (quickReject(left, top, right, bottom)) {
2378 return DrawGlInfo::kStatusDone;
2379 }
2380
Romain Guy211370f2012-02-01 16:10:55 -08002381 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002382 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002383 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002384 if (!texture) return DrawGlInfo::kStatusDone;
2385 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002386
Romain Guya4adcf02013-02-28 12:15:35 -08002387 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2388 texture->setFilter(GL_LINEAR, true);
2389
Romain Guy3b753822013-03-05 10:27:35 -08002390 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002391 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002392 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002393 const float offsetX = left + currentTransform().getTranslateX();
2394 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002395 const size_t count = mesh->quads.size();
2396 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002397 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002398 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002399 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2400 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2401 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002402 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002403 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002404 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002405 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002406 }
2407 }
2408
Romain Guy3b748a42013-04-17 18:54:38 -07002409 alpha *= mSnapshot->alpha;
2410
Romain Guy211370f2012-02-01 16:10:55 -08002411 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002412 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2413 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002414
Romain Guy3b748a42013-04-17 18:54:38 -07002415 right = x + right - left;
2416 bottom = y + bottom - top;
2417 drawIndexedTextureMesh(x, y, right, bottom, texture->id, alpha / 255.0f,
2418 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2419 GL_TRIANGLES, mesh->indexCount, false, true,
2420 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002421 } else {
Romain Guy3b748a42013-04-17 18:54:38 -07002422 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2423 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2424 GL_TRIANGLES, mesh->indexCount, false, false,
2425 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002426 }
Romain Guy054dc182010-10-15 17:55:25 -07002427 }
Chet Haase48659092012-05-31 15:21:51 -07002428
2429 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002430}
2431
Chris Craik65cd6122012-12-10 17:56:27 -08002432status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2433 bool useOffset) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002434 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002435 // no vertices to draw
2436 return DrawGlInfo::kStatusDone;
2437 }
2438
Chris Craikcb4d6002012-09-25 12:00:29 -07002439 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002440 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2441 bool isAA = paint->isAntiAlias();
2442
Chris Craik710f46d2012-09-17 17:25:49 -07002443 setupDraw();
2444 setupDrawNoTexture();
2445 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002446 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2447 setupDrawColorFilter();
2448 setupDrawShader();
2449 setupDrawBlending(isAA, mode);
2450 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002451 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002452 setupDrawColorUniforms();
2453 setupDrawColorFilterUniforms();
2454 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002455
Chris Craik710f46d2012-09-17 17:25:49 -07002456 void* vertices = vertexBuffer.getBuffer();
2457 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002458 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002459 mCaches.resetTexCoordsVertexPointer();
2460 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002461
Chris Craik710f46d2012-09-17 17:25:49 -07002462 int alphaSlot = -1;
2463 if (isAA) {
2464 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2465 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002466
Chris Craik710f46d2012-09-17 17:25:49 -07002467 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002468 glEnableVertexAttribArray(alphaSlot);
2469 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002470 }
Romain Guy04299382012-07-18 17:15:41 -07002471
Chris Craik6d29c8d2013-05-08 18:35:44 -07002472 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002473
Chris Craik710f46d2012-09-17 17:25:49 -07002474 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002475 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002476 }
Chris Craik65cd6122012-12-10 17:56:27 -08002477
2478 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002479}
2480
2481/**
Chris Craik65cd6122012-12-10 17:56:27 -08002482 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2483 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2484 * screen space in all directions. However, instead of using a fragment shader to compute the
2485 * translucency of the color from its position, we simply use a varying parameter to define how far
2486 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2487 *
2488 * Doesn't yet support joins, caps, or path effects.
2489 */
2490status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2491 VertexBuffer vertexBuffer;
2492 // TODO: try clipping large paths to viewport
2493 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2494
Romain Guyc46d07a2013-03-15 19:06:39 -07002495 if (hasLayer()) {
2496 SkRect bounds = path.getBounds();
2497 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2498 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2499 }
Chris Craik65cd6122012-12-10 17:56:27 -08002500
2501 return drawVertexBuffer(vertexBuffer, paint);
2502}
2503
2504/**
2505 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2506 * and additional geometry for defining an alpha slope perimeter.
2507 *
2508 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2509 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2510 * in-shader alpha region, but found it to be taxing on some GPUs.
2511 *
2512 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2513 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002514 */
Chet Haase48659092012-05-31 15:21:51 -07002515status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002516 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002517
Chris Craik65cd6122012-12-10 17:56:27 -08002518 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002519
Chris Craik65cd6122012-12-10 17:56:27 -08002520 VertexBuffer buffer;
2521 SkRect bounds;
2522 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002523
2524 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2525 return DrawGlInfo::kStatusDone;
2526 }
2527
Romain Guy3b753822013-03-05 10:27:35 -08002528 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002529
Chris Craik65cd6122012-12-10 17:56:27 -08002530 bool useOffset = !paint->isAntiAlias();
2531 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002532}
2533
Chet Haase48659092012-05-31 15:21:51 -07002534status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002535 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002536
Chris Craik6d29c8d2013-05-08 18:35:44 -07002537 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002538
Chris Craik6d29c8d2013-05-08 18:35:44 -07002539 VertexBuffer buffer;
2540 SkRect bounds;
2541 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002542
Chris Craik6d29c8d2013-05-08 18:35:44 -07002543 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2544 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002545 }
2546
Chris Craik6d29c8d2013-05-08 18:35:44 -07002547 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002548
Chris Craik6d29c8d2013-05-08 18:35:44 -07002549 bool useOffset = !paint->isAntiAlias();
2550 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002551}
2552
Chet Haase48659092012-05-31 15:21:51 -07002553status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002554 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002555 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002556
Romain Guyae88e5e2010-10-22 17:49:18 -07002557 Rect& clip(*mSnapshot->clipRect);
2558 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002559
Romain Guy3d58c032010-07-14 16:34:53 -07002560 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002561
2562 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002563}
Romain Guy9d5316e2010-06-24 19:30:36 -07002564
Chet Haase48659092012-05-31 15:21:51 -07002565status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2566 SkPaint* paint) {
2567 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002568 const AutoTexture autoCleanup(texture);
2569
2570 const float x = left + texture->left - texture->offset;
2571 const float y = top + texture->top - texture->offset;
2572
2573 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002574
2575 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002576}
2577
Chet Haase48659092012-05-31 15:21:51 -07002578status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002579 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002580 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2581 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002582 return DrawGlInfo::kStatusDone;
2583 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002584
Chris Craikcb4d6002012-09-25 12:00:29 -07002585 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002586 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002587 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002588 right - left, bottom - top, rx, ry, p);
2589 return drawShape(left, top, texture, p);
2590 }
2591
2592 SkPath path;
2593 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002594 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2595 float outset = p->getStrokeWidth() / 2;
2596 rect.outset(outset, outset);
2597 rx += outset;
2598 ry += outset;
2599 }
Chris Craik710f46d2012-09-17 17:25:49 -07002600 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002601 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002602}
2603
Chris Craik710f46d2012-09-17 17:25:49 -07002604status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002605 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002606 x + radius, y + radius, p) ||
2607 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002608 return DrawGlInfo::kStatusDone;
2609 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002610 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002611 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002612 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002613 return drawShape(x - radius, y - radius, texture, p);
2614 }
2615
2616 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002617 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2618 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2619 } else {
2620 path.addCircle(x, y, radius);
2621 }
Chris Craik65cd6122012-12-10 17:56:27 -08002622 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002623}
Romain Guy01d58e42011-01-19 21:54:02 -08002624
Chet Haase48659092012-05-31 15:21:51 -07002625status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002626 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002627 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2628 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002629 return DrawGlInfo::kStatusDone;
2630 }
Romain Guy01d58e42011-01-19 21:54:02 -08002631
Chris Craikcb4d6002012-09-25 12:00:29 -07002632 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002633 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002634 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002635 return drawShape(left, top, texture, p);
2636 }
2637
2638 SkPath path;
2639 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002640 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2641 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2642 }
Chris Craik710f46d2012-09-17 17:25:49 -07002643 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002644 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002645}
2646
Chet Haase48659092012-05-31 15:21:51 -07002647status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002648 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002649 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2650 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002651 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002652 }
2653
Chris Craik780c1282012-10-04 14:10:49 -07002654 if (fabs(sweepAngle) >= 360.0f) {
2655 return drawOval(left, top, right, bottom, p);
2656 }
2657
2658 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002659 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002660 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002661 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002662 startAngle, sweepAngle, useCenter, p);
2663 return drawShape(left, top, texture, p);
2664 }
2665
2666 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2667 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2668 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2669 }
2670
2671 SkPath path;
2672 if (useCenter) {
2673 path.moveTo(rect.centerX(), rect.centerY());
2674 }
2675 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2676 if (useCenter) {
2677 path.close();
2678 }
Chris Craik65cd6122012-12-10 17:56:27 -08002679 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002680}
2681
Romain Guycf8675e2012-10-02 12:32:25 -07002682// See SkPaintDefaults.h
2683#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2684
Chet Haase48659092012-05-31 15:21:51 -07002685status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002686 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2687 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002688 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002689 }
2690
Chris Craik710f46d2012-09-17 17:25:49 -07002691 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002692 // only fill style is supported by drawConvexPath, since others have to handle joins
2693 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2694 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2695 mCaches.activeTexture(0);
2696 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002697 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002698 return drawShape(left, top, texture, p);
2699 }
2700
2701 SkPath path;
2702 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2703 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2704 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2705 }
2706 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002707 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002708 }
2709
Romain Guy3b753822013-03-05 10:27:35 -08002710 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002711 SkPath path;
2712 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002713 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002714 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002715 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002716 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002717 }
Romain Guyc7d53492010-06-25 13:41:57 -07002718}
Romain Guy9d5316e2010-06-24 19:30:36 -07002719
Raph Levien416a8472012-07-19 22:48:17 -07002720void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2721 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2722 float x, float y) {
2723 mCaches.activeTexture(0);
2724
2725 // NOTE: The drop shadow will not perform gamma correction
2726 // if shader-based correction is enabled
2727 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2728 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002729 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002730 // If the drop shadow exceeds the max texture size or couldn't be
2731 // allocated, skip drawing
2732 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002733 const AutoTexture autoCleanup(shadow);
2734
Chris Craikc3566d02013-02-04 16:16:33 -08002735 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2736 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002737
Chris Craikc3566d02013-02-04 16:16:33 -08002738 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2739 int shadowColor = mDrawModifiers.mShadowColor;
2740 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002741 shadowColor = 0xffffffff;
2742 }
2743
2744 setupDraw();
2745 setupDrawWithTexture(true);
2746 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2747 setupDrawColorFilter();
2748 setupDrawShader();
2749 setupDrawBlending(true, mode);
2750 setupDrawProgram();
2751 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2752 setupDrawTexture(shadow->id);
2753 setupDrawPureColorUniforms();
2754 setupDrawColorFilterUniforms();
2755 setupDrawShaderUniforms();
2756 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2757
2758 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2759}
2760
Romain Guy768bffc2013-02-27 13:50:45 -08002761bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2762 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2763 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2764}
2765
Romain Guy257ae352013-03-20 16:31:12 -07002766class TextSetupFunctor: public Functor {
2767public:
2768 TextSetupFunctor(OpenGLRenderer& renderer, float x, float y, bool pureTranslate,
2769 int alpha, SkXfermode::Mode mode, SkPaint* paint): Functor(),
2770 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate),
2771 alpha(alpha), mode(mode), paint(paint) {
2772 }
2773 ~TextSetupFunctor() { }
2774
2775 status_t operator ()(int what, void* data) {
2776 renderer.setupDraw();
2777 renderer.setupDrawTextGamma(paint);
2778 renderer.setupDrawDirtyRegionsDisabled();
2779 renderer.setupDrawWithTexture(true);
2780 renderer.setupDrawAlpha8Color(paint->getColor(), alpha);
2781 renderer.setupDrawColorFilter();
2782 renderer.setupDrawShader();
2783 renderer.setupDrawBlending(true, mode);
2784 renderer.setupDrawProgram();
2785 renderer.setupDrawModelView(x, y, x, y, pureTranslate, true);
2786 // Calling setupDrawTexture with the name 0 will enable the
2787 // uv attributes and increase the texture unit count
2788 // texture binding will be performed by the font renderer as
2789 // needed
2790 renderer.setupDrawTexture(0);
2791 renderer.setupDrawPureColorUniforms();
2792 renderer.setupDrawColorFilterUniforms();
2793 renderer.setupDrawShaderUniforms(pureTranslate);
2794 renderer.setupDrawTextGammaUniforms();
2795
2796 return NO_ERROR;
2797 }
2798
2799 OpenGLRenderer& renderer;
2800 float x;
2801 float y;
2802 bool pureTranslate;
2803 int alpha;
2804 SkXfermode::Mode mode;
2805 SkPaint* paint;
2806};
2807
Chet Haase48659092012-05-31 15:21:51 -07002808status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002809 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002810 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002811 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002812 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002813
Romain Guy671d6cf2012-01-18 12:39:17 -08002814 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002815 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002816 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002817 }
2818
Chris Craik39a908c2013-06-13 14:39:01 -07002819 mCaches.enableScissor();
2820
Romain Guy671d6cf2012-01-18 12:39:17 -08002821 float x = 0.0f;
2822 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002823 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002824 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002825 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2826 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002827 }
2828
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002829 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002830 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002831
2832 int alpha;
2833 SkXfermode::Mode mode;
2834 getAlphaAndMode(paint, &alpha, &mode);
2835
Chris Craikc3566d02013-02-04 16:16:33 -08002836 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002837 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2838 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002839 }
2840
Romain Guy671d6cf2012-01-18 12:39:17 -08002841 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002842 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002843 if (pureTranslate && !linearFilter) {
2844 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2845 }
Romain Guy257ae352013-03-20 16:31:12 -07002846 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002847
2848 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2849 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2850
Romain Guy211370f2012-02-01 16:10:55 -08002851 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002852
Romain Guy257ae352013-03-20 16:31:12 -07002853 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002854 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002855 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002856 if (hasActiveLayer) {
2857 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002858 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002859 }
2860 dirtyLayerUnchecked(bounds, getRegion());
2861 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002862 }
Chet Haase48659092012-05-31 15:21:51 -07002863
2864 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002865}
2866
Romain Guy624234f2013-03-05 16:43:31 -08002867mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2868 mat4 fontTransform;
2869 if (CC_LIKELY(transform.isPureTranslate())) {
2870 fontTransform = mat4::identity();
2871 } else {
2872 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002873 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002874 } else {
2875 float sx, sy;
2876 currentTransform().decomposeScale(sx, sy);
2877 fontTransform.loadScale(sx, sy, 1.0f);
2878 }
2879 }
2880 return fontTransform;
2881}
2882
Chris Craik41541822013-05-03 16:35:54 -07002883status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2884 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002885 DrawOpMode drawOpMode) {
2886
Chris Craik527a3aa2013-03-04 10:19:31 -08002887 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002888 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2889 // drawing as ops from DeferredDisplayList are already filtered for these
2890 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
2891 quickReject(bounds)) {
2892 return DrawGlInfo::kStatusDone;
2893 }
Romain Guycac5fd32011-12-01 20:08:50 -08002894 }
2895
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002896 const float oldX = x;
2897 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002898
2899 const mat4& transform = currentTransform();
2900 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002901
Romain Guy211370f2012-02-01 16:10:55 -08002902 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002903 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2904 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002905 }
2906
Romain Guy86568192010-12-14 15:55:39 -08002907 int alpha;
2908 SkXfermode::Mode mode;
2909 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002910
Romain Guyc74f45a2013-02-26 19:10:14 -08002911 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2912
Chris Craikc3566d02013-02-04 16:16:33 -08002913 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002914 fontRenderer.setFont(paint, mat4::identity());
2915 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2916 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002917 }
2918
Romain Guy19d4dd82013-03-04 11:14:26 -08002919 const bool hasActiveLayer = hasLayer();
2920
Romain Guy624234f2013-03-05 16:43:31 -08002921 // We only pass a partial transform to the font renderer. That partial
2922 // matrix defines how glyphs are rasterized. Typically we want glyphs
2923 // to be rasterized at their final size on screen, which means the partial
2924 // matrix needs to take the scale factor into account.
2925 // When a partial matrix is used to transform glyphs during rasterization,
2926 // the mesh is generated with the inverse transform (in the case of scale,
2927 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2928 // apply the full transform matrix at draw time in the vertex shader.
2929 // Applying the full matrix in the shader is the easiest way to handle
2930 // rotation and perspective and allows us to always generated quads in the
2931 // font renderer which greatly simplifies the code, clipping in particular.
2932 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002933 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002934
Romain Guy6620c6d2010-12-06 18:07:02 -08002935 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002936 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002937 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002938
Romain Guy624234f2013-03-05 16:43:31 -08002939 // TODO: Implement better clipping for scaled/rotated text
2940 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07002941 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 -07002942
Raph Levien996e57c2012-07-23 15:22:52 -07002943 bool status;
Romain Guy257ae352013-03-20 16:31:12 -07002944 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002945
2946 // don't call issuedrawcommand, do it at end of batch
2947 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002948 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002949 SkPaint paintCopy(*paint);
2950 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2951 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002952 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002953 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002954 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002955 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002956 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002957
Chris Craik527a3aa2013-03-04 10:19:31 -08002958 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002959 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002960 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002961 }
Chris Craik41541822013-05-03 16:35:54 -07002962 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002963 }
Romain Guy694b5192010-07-21 21:33:20 -07002964
Chris Craik41541822013-05-03 16:35:54 -07002965 drawTextDecorations(text, bytesCount, totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002966
2967 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002968}
2969
Chet Haase48659092012-05-31 15:21:51 -07002970status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002971 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002972 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002973 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002974 }
2975
Chris Craik39a908c2013-06-13 14:39:01 -07002976 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2977 mCaches.enableScissor();
2978
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002979 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002980 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07002981 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002982
2983 int alpha;
2984 SkXfermode::Mode mode;
2985 getAlphaAndMode(paint, &alpha, &mode);
2986
Romain Guy03d58522012-02-24 17:54:07 -08002987 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002988 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002989 setupDrawDirtyRegionsDisabled();
2990 setupDrawWithTexture(true);
2991 setupDrawAlpha8Color(paint->getColor(), alpha);
2992 setupDrawColorFilter();
2993 setupDrawShader();
2994 setupDrawBlending(true, mode);
2995 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002996 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy257ae352013-03-20 16:31:12 -07002997 // Calling setupDrawTexture with the name 0 will enable the
2998 // uv attributes and increase the texture unit count
2999 // texture binding will be performed by the font renderer as
3000 // needed
3001 setupDrawTexture(0);
Romain Guy03d58522012-02-24 17:54:07 -08003002 setupDrawPureColorUniforms();
3003 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08003004 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07003005 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08003006
Romain Guy97771732012-02-28 18:17:02 -08003007 const Rect* clip = &mSnapshot->getLocalClip();
3008 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 -08003009
Romain Guy97771732012-02-28 18:17:02 -08003010 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003011
3012 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
3013 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08003014 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003015 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003016 dirtyLayerUnchecked(bounds, getRegion());
3017 }
Romain Guy97771732012-02-28 18:17:02 -08003018 }
Chet Haase48659092012-05-31 15:21:51 -07003019
3020 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003021}
3022
Chet Haase48659092012-05-31 15:21:51 -07003023status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3024 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003025
Romain Guya1d3c912011-12-13 14:55:06 -08003026 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003027
Romain Guyfb8b7632010-08-23 21:05:08 -07003028 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003029 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003030 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003031
Romain Guy8b55f372010-08-18 17:10:07 -07003032 const float x = texture->left - texture->offset;
3033 const float y = texture->top - texture->offset;
3034
Romain Guy01d58e42011-01-19 21:54:02 -08003035 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003036
3037 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003038}
3039
Chris Craika08f95c2013-03-15 17:24:33 -07003040status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003041 if (!layer) {
3042 return DrawGlInfo::kStatusDone;
3043 }
3044
Romain Guyb2e2f242012-10-17 18:18:35 -07003045 mat4* transform = NULL;
3046 if (layer->isTextureLayer()) {
3047 transform = &layer->getTransform();
3048 if (!transform->isIdentity()) {
3049 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003050 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003051 }
3052 }
3053
Chris Craik39a908c2013-06-13 14:39:01 -07003054 bool clipRequired = false;
Romain Guy35643dd2012-09-18 15:40:58 -07003055 const bool rejected = quickRejectNoScissor(x, y,
Chris Craik39a908c2013-06-13 14:39:01 -07003056 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired);
Romain Guy35643dd2012-09-18 15:40:58 -07003057
3058 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003059 if (transform && !transform->isIdentity()) {
3060 restore();
3061 }
Chet Haase48659092012-05-31 15:21:51 -07003062 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003063 }
3064
Romain Guy5bb3c732012-11-29 17:52:58 -08003065 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003066
Chris Craik39a908c2013-06-13 14:39:01 -07003067 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003068 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003069
Romain Guy211370f2012-02-01 16:10:55 -08003070 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003071 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3072 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003073
Romain Guyc88e3572011-01-22 00:32:12 -08003074 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003075 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3076 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003077 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003078 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003079 setupDraw();
3080 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003081 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003082 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003083 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003084 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003085 setupDrawPureColorUniforms();
3086 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003087 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003088 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3089 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3090 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003091
Romain Guyd21b6e12011-11-30 20:21:23 -08003092 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003093 setupDrawModelViewTranslate(tx, ty,
3094 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003095 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003096 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003097 setupDrawModelViewTranslate(x, y,
3098 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3099 }
Romain Guyc88e3572011-01-22 00:32:12 -08003100 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08003101
Chris Craik34416ea2013-04-15 16:08:28 -07003102 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3103 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
3104 GL_UNSIGNED_SHORT, layer->meshIndices));
Romain Guyf219da52011-01-16 12:54:25 -08003105
Romain Guyc88e3572011-01-22 00:32:12 -08003106 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08003107
3108#if DEBUG_LAYERS_AS_REGIONS
3109 drawRegionRects(layer->region);
3110#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003111 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003112
Chris Craikc3566d02013-02-04 16:16:33 -08003113 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003114
Romain Guy5bb3c732012-11-29 17:52:58 -08003115 if (layer->debugDrawUpdate) {
3116 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003117 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3118 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3119 }
Romain Guyf219da52011-01-16 12:54:25 -08003120 }
Chris Craik34416ea2013-04-15 16:08:28 -07003121 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003122
Romain Guyb2e2f242012-10-17 18:18:35 -07003123 if (transform && !transform->isIdentity()) {
3124 restore();
3125 }
3126
Chet Haase48659092012-05-31 15:21:51 -07003127 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003128}
3129
Romain Guy6926c722010-07-12 20:20:03 -07003130///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003131// Shaders
3132///////////////////////////////////////////////////////////////////////////////
3133
3134void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003135 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003136}
3137
Romain Guy06f96e22010-07-30 19:18:16 -07003138void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003139 mDrawModifiers.mShader = shader;
3140 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003141 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003142 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003143}
3144
Romain Guyd27977d2010-07-14 19:18:51 -07003145///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003146// Color filters
3147///////////////////////////////////////////////////////////////////////////////
3148
3149void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003150 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003151}
3152
3153void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003154 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003155}
3156
3157///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003158// Drop shadow
3159///////////////////////////////////////////////////////////////////////////////
3160
3161void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003162 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003163}
3164
3165void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003166 mDrawModifiers.mHasShadow = true;
3167 mDrawModifiers.mShadowRadius = radius;
3168 mDrawModifiers.mShadowDx = dx;
3169 mDrawModifiers.mShadowDy = dy;
3170 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003171}
3172
3173///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003174// Draw filters
3175///////////////////////////////////////////////////////////////////////////////
3176
3177void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003178 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3179 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003180 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003181 mDrawModifiers.mPaintFilterClearBits = 0;
3182 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003183}
3184
3185void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003186 mDrawModifiers.mHasDrawFilter = true;
3187 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3188 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003189}
3190
Chris Craika08f95c2013-03-15 17:24:33 -07003191SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003192 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003193 return paint;
3194 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003195
3196 uint32_t flags = paint->getFlags();
3197
3198 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003199 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3200 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003201
3202 return &mFilteredPaint;
3203}
3204
3205///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003206// Drawing implementation
3207///////////////////////////////////////////////////////////////////////////////
3208
Romain Guy3b748a42013-04-17 18:54:38 -07003209Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3210 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3211 if (!texture) {
3212 return mCaches.textureCache.get(bitmap);
3213 }
3214 return texture;
3215}
3216
Romain Guy01d58e42011-01-19 21:54:02 -08003217void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3218 float x, float y, SkPaint* paint) {
3219 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3220 return;
3221 }
3222
3223 int alpha;
3224 SkXfermode::Mode mode;
3225 getAlphaAndMode(paint, &alpha, &mode);
3226
3227 setupDraw();
3228 setupDrawWithTexture(true);
3229 setupDrawAlpha8Color(paint->getColor(), alpha);
3230 setupDrawColorFilter();
3231 setupDrawShader();
3232 setupDrawBlending(true, mode);
3233 setupDrawProgram();
3234 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3235 setupDrawTexture(texture->id);
3236 setupDrawPureColorUniforms();
3237 setupDrawColorFilterUniforms();
3238 setupDrawShaderUniforms();
3239 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3240
3241 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3242
3243 finishDrawTexture();
3244}
3245
Romain Guyf607bdc2010-09-10 19:20:06 -07003246// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003247#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3248#define kStdUnderline_Offset (1.0f / 9.0f)
3249#define kStdUnderline_Thickness (1.0f / 18.0f)
3250
Chris Craik41541822013-05-03 16:35:54 -07003251void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float underlineWidth,
Romain Guy0a417492010-08-16 20:26:20 -07003252 float x, float y, SkPaint* paint) {
3253 // Handle underline and strike-through
3254 uint32_t flags = paint->getFlags();
3255 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003256 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003257
Romain Guy211370f2012-02-01 16:10:55 -08003258 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003259 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003260 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003261
Raph Levien8b4072d2012-07-30 15:50:00 -07003262 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003263 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003264
Romain Guyf6834472011-01-23 13:32:12 -08003265 int linesCount = 0;
3266 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3267 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3268
3269 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003270 float points[pointsCount];
3271 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003272
3273 if (flags & SkPaint::kUnderlineText_Flag) {
3274 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003275 points[currentPoint++] = left;
3276 points[currentPoint++] = top;
3277 points[currentPoint++] = left + underlineWidth;
3278 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003279 }
3280
3281 if (flags & SkPaint::kStrikeThruText_Flag) {
3282 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003283 points[currentPoint++] = left;
3284 points[currentPoint++] = top;
3285 points[currentPoint++] = left + underlineWidth;
3286 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003287 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003288
Romain Guy726aeba2011-06-01 14:52:00 -07003289 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003290
Romain Guy726aeba2011-06-01 14:52:00 -07003291 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003292 }
3293 }
3294}
3295
Romain Guy672433d2013-01-04 19:05:13 -08003296status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3297 if (mSnapshot->isIgnored()) {
3298 return DrawGlInfo::kStatusDone;
3299 }
3300
Romain Guy735738c2012-12-03 12:34:51 -08003301 int color = paint->getColor();
3302 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003303 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003304 color |= 0x00ffffff;
3305 }
3306 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3307
3308 return drawColorRects(rects, count, color, mode);
3309}
3310
3311status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003312 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003313 if (count == 0) {
3314 return DrawGlInfo::kStatusDone;
3315 }
Romain Guy735738c2012-12-03 12:34:51 -08003316
Romain Guy672433d2013-01-04 19:05:13 -08003317 float left = FLT_MAX;
3318 float top = FLT_MAX;
3319 float right = FLT_MIN;
3320 float bottom = FLT_MIN;
3321
3322 int vertexCount = 0;
3323 Vertex mesh[count * 6];
3324 Vertex* vertex = mesh;
3325
Chris Craik2af46352012-11-26 18:30:17 -08003326 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003327 float l = rects[index + 0];
3328 float t = rects[index + 1];
3329 float r = rects[index + 2];
3330 float b = rects[index + 3];
3331
Romain Guy3b753822013-03-05 10:27:35 -08003332 Vertex::set(vertex++, l, b);
3333 Vertex::set(vertex++, l, t);
3334 Vertex::set(vertex++, r, t);
3335 Vertex::set(vertex++, l, b);
3336 Vertex::set(vertex++, r, t);
3337 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003338
Romain Guy3b753822013-03-05 10:27:35 -08003339 vertexCount += 6;
Romain Guy672433d2013-01-04 19:05:13 -08003340
Romain Guy3b753822013-03-05 10:27:35 -08003341 left = fminf(left, l);
3342 top = fminf(top, t);
3343 right = fmaxf(right, r);
3344 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003345 }
3346
Romain Guy3b753822013-03-05 10:27:35 -08003347 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003348 return DrawGlInfo::kStatusDone;
3349 }
Romain Guy672433d2013-01-04 19:05:13 -08003350
Romain Guy672433d2013-01-04 19:05:13 -08003351 setupDraw();
3352 setupDrawNoTexture();
3353 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3354 setupDrawShader();
3355 setupDrawColorFilter();
3356 setupDrawBlending(mode);
3357 setupDrawProgram();
3358 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003359 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003360 setupDrawColorUniforms();
3361 setupDrawShaderUniforms();
3362 setupDrawColorFilterUniforms();
3363 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3364
Romain Guy8ce00302013-01-15 18:51:42 -08003365 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003366 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003367 }
3368
3369 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3370
3371 return DrawGlInfo::kStatusDrew;
3372}
3373
Romain Guy026c5e162010-06-28 17:12:22 -07003374void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003375 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003376 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003377 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003378 color |= 0x00ffffff;
3379 }
3380
Romain Guy70ca14e2010-12-13 18:24:33 -08003381 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003382 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003383 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003384 setupDrawShader();
3385 setupDrawColorFilter();
3386 setupDrawBlending(mode);
3387 setupDrawProgram();
3388 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3389 setupDrawColorUniforms();
3390 setupDrawShaderUniforms(ignoreTransform);
3391 setupDrawColorFilterUniforms();
3392 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003393
Romain Guyc95c8d62010-09-17 15:31:32 -07003394 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3395}
3396
Romain Guy82ba8142010-07-09 13:25:56 -07003397void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003398 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003399 int alpha;
3400 SkXfermode::Mode mode;
3401 getAlphaAndMode(paint, &alpha, &mode);
3402
Romain Guyd21b6e12011-11-30 20:21:23 -08003403 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003404
Romain Guy3b748a42013-04-17 18:54:38 -07003405 GLvoid* vertices = (GLvoid*) NULL;
3406 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3407
3408 if (texture->uvMapper) {
3409 vertices = &mMeshVertices[0].position[0];
3410 texCoords = &mMeshVertices[0].texture[0];
3411
3412 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3413 texture->uvMapper->map(uvs);
3414
3415 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3416 }
3417
Romain Guy3b753822013-03-05 10:27:35 -08003418 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3419 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3420 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003421
Romain Guyd21b6e12011-11-30 20:21:23 -08003422 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003423 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003424 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3425 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003426 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003427 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003428 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003429 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3430 }
3431
3432 if (texture->uvMapper) {
3433 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003434 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003435}
3436
Romain Guybd6b79b2010-06-26 00:13:53 -07003437void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003438 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3439 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003440 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003441}
3442
3443void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003444 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003445 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003446 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003447
Romain Guy746b7402010-10-26 16:27:31 -07003448 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003449 setupDrawWithTexture();
3450 setupDrawColor(alpha, alpha, alpha, alpha);
3451 setupDrawColorFilter();
3452 setupDrawBlending(blend, mode, swapSrcDst);
3453 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003454 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003455 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003456 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003457 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003458 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003459 }
Romain Guy886b2752013-01-04 12:26:18 -08003460 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003461 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003462 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003463 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003464
Romain Guy6820ac82010-09-15 18:11:50 -07003465 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003466
3467 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003468}
3469
Romain Guy3b748a42013-04-17 18:54:38 -07003470void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3471 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3472 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3473 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
3474
3475 setupDraw();
3476 setupDrawWithTexture();
3477 setupDrawColor(alpha, alpha, alpha, alpha);
3478 setupDrawColorFilter();
3479 setupDrawBlending(blend, mode, swapSrcDst);
3480 setupDrawProgram();
3481 if (!dirty) setupDrawDirtyRegionsDisabled();
3482 if (!ignoreScale) {
3483 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3484 } else {
3485 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3486 }
3487 setupDrawTexture(texture);
3488 setupDrawPureColorUniforms();
3489 setupDrawColorFilterUniforms();
3490 setupDrawMeshIndices(vertices, texCoords, vbo);
3491
3492 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
3493
3494 finishDrawTexture();
3495}
3496
Romain Guy886b2752013-01-04 12:26:18 -08003497void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3498 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3499 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik527a3aa2013-03-04 10:19:31 -08003500 bool ignoreTransform, bool ignoreScale, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003501
3502 setupDraw();
3503 setupDrawWithTexture(true);
3504 if (hasColor) {
3505 setupDrawAlpha8Color(color, alpha);
3506 }
3507 setupDrawColorFilter();
3508 setupDrawShader();
3509 setupDrawBlending(true, mode);
3510 setupDrawProgram();
3511 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik527a3aa2013-03-04 10:19:31 -08003512 if (!ignoreScale) {
3513 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3514 } else {
3515 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3516 }
Romain Guy886b2752013-01-04 12:26:18 -08003517 setupDrawTexture(texture);
3518 setupDrawPureColorUniforms();
3519 setupDrawColorFilterUniforms();
3520 setupDrawShaderUniforms();
3521 setupDrawMesh(vertices, texCoords);
3522
3523 glDrawArrays(drawMode, 0, elementsCount);
3524
3525 finishDrawTexture();
3526}
3527
Romain Guya5aed0d2010-09-09 14:42:43 -07003528void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003529 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003530 if (mCountOverdraw) {
3531 if (!mCaches.blend) glEnable(GL_BLEND);
3532 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3533 glBlendFunc(GL_ONE, GL_ONE);
3534 }
3535
3536 mCaches.blend = true;
3537 mCaches.lastSrcMode = GL_ONE;
3538 mCaches.lastDstMode = GL_ONE;
3539
3540 return;
3541 }
3542
Romain Guy82ba8142010-07-09 13:25:56 -07003543 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003544
Romain Guy82ba8142010-07-09 13:25:56 -07003545 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003546 // These blend modes are not supported by OpenGL directly and have
3547 // to be implemented using shaders. Since the shader will perform
3548 // the blending, turn blending off here
3549 // If the blend mode cannot be implemented using shaders, fall
3550 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003551 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003552 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003553 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003554 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003555
Romain Guy82bc7a72012-01-03 14:13:39 -08003556 if (mCaches.blend) {
3557 glDisable(GL_BLEND);
3558 mCaches.blend = false;
3559 }
3560
3561 return;
3562 } else {
3563 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003564 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003565 }
3566
3567 if (!mCaches.blend) {
3568 glEnable(GL_BLEND);
3569 }
3570
3571 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3572 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3573
3574 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3575 glBlendFunc(sourceMode, destMode);
3576 mCaches.lastSrcMode = sourceMode;
3577 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003578 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003579 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003580 glDisable(GL_BLEND);
3581 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003582 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003583}
3584
Romain Guy889f8d12010-07-29 14:37:42 -07003585bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003586 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003587 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003588 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003589 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003590 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003591 }
Romain Guy6926c722010-07-12 20:20:03 -07003592 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003593}
3594
Romain Guy026c5e162010-06-28 17:12:22 -07003595void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003596 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003597 TextureVertex::setUV(v++, u1, v1);
3598 TextureVertex::setUV(v++, u2, v1);
3599 TextureVertex::setUV(v++, u1, v2);
3600 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003601}
3602
Chris Craik16ecda52013-03-29 10:59:59 -07003603void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003604 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003605 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3606 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003607 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003608 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003609 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003610}
3611
Chris Craik16ecda52013-03-29 10:59:59 -07003612float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3613 float alpha;
3614 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3615 alpha = mDrawModifiers.mOverrideLayerAlpha;
3616 } else {
3617 alpha = layer->getAlpha() / 255.0f;
3618 }
3619 return alpha * mSnapshot->alpha;
3620}
3621
Romain Guy9d5316e2010-06-24 19:30:36 -07003622}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003623}; // namespace android