blob: 7e9734f9e57de7461c23f35904f3b82cfb5332a4 [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 Guy03d58522012-02-24 17:54:07 -080024#include <SkPathMeasure.h>
Romain Guy694b5192010-07-21 21:33:20 -070025#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070028#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Romain Guy08aa2cb2011-03-17 11:06:57 -070030#include <private/hwui/DrawGlInfo.h>
31
Romain Guy5b3b3522010-10-27 18:57:51 -070032#include <ui/Rect.h>
33
Romain Guy85bf02f2010-06-22 13:11:24 -070034#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080035#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080036#include "DisplayListRenderer.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 Craikc3566d02013-02-04 16:16:33 -0800115 mDrawModifiers.mShader = NULL;
116 mDrawModifiers.mColorFilter = NULL;
117 mDrawModifiers.mHasShadow = false;
118 mDrawModifiers.mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700119
Romain Guyac670c02010-07-27 17:39:27 -0700120 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
121
Romain Guyae5575b2010-07-29 18:48:04 -0700122 mFirstSnapshot = new Snapshot;
Romain Guy87e2f7572012-09-24 11:37:12 -0700123
124 mScissorOptimizationDisabled = false;
Chris Craikc3566d02013-02-04 16:16:33 -0800125 mDrawDeferDisabled = false;
126 mDrawReorderDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700127}
128
Romain Guy85bf02f2010-06-22 13:11:24 -0700129OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700130 // The context has already been destroyed at this point, do not call
131 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700132}
133
Romain Guy87e2f7572012-09-24 11:37:12 -0700134void OpenGLRenderer::initProperties() {
135 char property[PROPERTY_VALUE_MAX];
136 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
137 mScissorOptimizationDisabled = !strcasecmp(property, "true");
138 INIT_LOGD(" Scissor optimization %s",
139 mScissorOptimizationDisabled ? "disabled" : "enabled");
140 } else {
141 INIT_LOGD(" Scissor optimization enabled");
142 }
Chris Craikc3566d02013-02-04 16:16:33 -0800143
144 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
145 mDrawDeferDisabled = !strcasecmp(property, "true");
146 INIT_LOGD(" Draw defer %s", mDrawDeferDisabled ? "disabled" : "enabled");
147 } else {
148 INIT_LOGD(" Draw defer enabled");
149 }
150
151 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
152 mDrawReorderDisabled = !strcasecmp(property, "true");
153 INIT_LOGD(" Draw reorder %s", mDrawReorderDisabled ? "disabled" : "enabled");
154 } else {
155 INIT_LOGD(" Draw reorder enabled");
156 }
Romain Guy13631f32012-01-30 17:41:55 -0800157}
158
159///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700160// Setup
161///////////////////////////////////////////////////////////////////////////////
162
Romain Guyef359272013-01-31 19:07:29 -0800163void OpenGLRenderer::setName(const char* name) {
164 if (name) {
165 mName.setTo(name);
166 } else {
167 mName.clear();
168 }
169}
170
171const char* OpenGLRenderer::getName() const {
172 return mName.string();
173}
174
Romain Guy49c5fc02012-05-15 11:10:01 -0700175bool OpenGLRenderer::isDeferred() {
176 return false;
177}
178
Romain Guy85bf02f2010-06-22 13:11:24 -0700179void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700180 initViewport(width, height);
181
182 glDisable(GL_DITHER);
183 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
184
185 glEnableVertexAttribArray(Program::kBindingPosition);
186}
187
188void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700189 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700190
191 mWidth = width;
192 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700193
194 mFirstSnapshot->height = height;
195 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700196}
197
Romain Guy7c25aab2012-10-18 15:05:02 -0700198status_t OpenGLRenderer::prepare(bool opaque) {
Chet Haase44b2fe32012-06-06 19:03:58 -0700199 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800200}
201
Romain Guyc3fedaf2013-01-29 17:26:25 -0800202status_t OpenGLRenderer::prepareDirty(float left, float top,
203 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800204 mCaches.clearGarbage();
205
Romain Guy8aef54f2010-09-01 15:13:49 -0700206 mSnapshot = new Snapshot(mFirstSnapshot,
207 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800208 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700209 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700210
Romain Guy7d7b5492011-01-24 16:33:45 -0800211 mSnapshot->setClip(left, top, right, bottom);
Romain Guy41308e22012-10-22 20:02:43 -0700212 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700213
Romain Guy11cb6422012-09-21 00:39:43 -0700214 updateLayers();
215
Romain Guydcfc8362013-01-03 13:08:57 -0800216 discardFramebuffer(left, top, right, bottom);
Romain Guy45e4c3d2012-09-11 17:17:07 -0700217
Romain Guyddf74372012-05-22 14:07:07 -0700218 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800219
Romain Guy54c1a642012-09-27 17:55:46 -0700220 // Functors break the tiling extension in pretty spectacular ways
221 // This ensures we don't use tiling when a functor is going to be
222 // invoked during the frame
223 mSuppressTiling = mCaches.hasRegisteredFunctors();
224
Romain Guy2b7028e2012-09-19 17:25:38 -0700225 mTilingSnapshot = mSnapshot;
Romain Guy57b52682012-09-20 17:38:46 -0700226 startTiling(mTilingSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700227
Romain Guy7c450aa2012-09-21 19:15:00 -0700228 debugOverdraw(true, true);
229
Romain Guy7c25aab2012-10-18 15:05:02 -0700230 return clear(left, top, right, bottom, opaque);
231}
232
Romain Guydcfc8362013-01-03 13:08:57 -0800233void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
234 // If we know that we are going to redraw the entire framebuffer,
235 // perform a discard to let the driver know we don't need to preserve
236 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800237 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800238 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800239 const bool isFbo = getTargetFbo() == 0;
240 const GLenum attachments[] = {
241 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
242 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800243 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
244 }
245}
246
Romain Guy7c25aab2012-10-18 15:05:02 -0700247status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700248 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700249 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700250 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700251 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700252 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700253 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700254
Romain Guy7c25aab2012-10-18 15:05:02 -0700255 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700256 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700257}
258
259void OpenGLRenderer::syncState() {
260 glViewport(0, 0, mWidth, mHeight);
261
262 if (mCaches.blend) {
263 glEnable(GL_BLEND);
264 } else {
265 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700266 }
Romain Guybb9524b2010-06-22 18:56:38 -0700267}
268
Romain Guy57b52682012-09-20 17:38:46 -0700269void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700270 if (!mSuppressTiling) {
271 Rect* clip = mTilingSnapshot->clipRect;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800272 if (s->flags & Snapshot::kFlagFboTarget) {
273 clip = &s->layer->clipRect;
Romain Guy54c1a642012-09-27 17:55:46 -0700274 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700275
Romain Guyc3fedaf2013-01-29 17:26:25 -0800276 startTiling(*clip, s->height, opaque);
277 }
278}
279
280void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
281 if (!mSuppressTiling) {
282 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800283 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700284 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700285}
286
287void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700288 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700289}
290
Romain Guyb025b9c2010-09-16 14:16:48 -0700291void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700292 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700293 endTiling();
294
Romain Guy11cb6422012-09-21 00:39:43 -0700295 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700296#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700297 GLenum status = GL_NO_ERROR;
298 while ((status = glGetError()) != GL_NO_ERROR) {
299 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
300 switch (status) {
301 case GL_INVALID_ENUM:
302 ALOGE(" GL_INVALID_ENUM");
303 break;
304 case GL_INVALID_VALUE:
305 ALOGE(" GL_INVALID_VALUE");
306 break;
307 case GL_INVALID_OPERATION:
308 ALOGE(" GL_INVALID_OPERATION");
309 break;
310 case GL_OUT_OF_MEMORY:
311 ALOGE(" Out of memory!");
312 break;
313 }
Romain Guya07105b2011-01-10 21:14:18 -0800314 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700315#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700316
Romain Guyc15008e2010-11-10 11:59:15 -0800317#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800318 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700319#else
320 if (mCaches.getDebugLevel() & kDebugMemory) {
321 mCaches.dumpMemoryUsage();
322 }
Romain Guyc15008e2010-11-10 11:59:15 -0800323#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700324 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700325}
326
Romain Guy6c319ca2011-01-11 14:29:25 -0800327void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700328 if (mCaches.currentProgram) {
329 if (mCaches.currentProgram->isInUse()) {
330 mCaches.currentProgram->remove();
331 mCaches.currentProgram = NULL;
332 }
333 }
Romain Guy50c0f092010-10-19 11:42:22 -0700334 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800335 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800336 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800337 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700338 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700339}
340
Romain Guy6c319ca2011-01-11 14:29:25 -0800341void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800342 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800343 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700344 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700345 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700346
Romain Guy3e263fa2011-12-12 16:47:48 -0800347 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
348
Chet Haase80250612012-08-15 13:46:54 -0700349 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700350 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800351 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700352 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700353
Romain Guya1d3c912011-12-13 14:55:06 -0800354 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700355
Romain Guy50c0f092010-10-19 11:42:22 -0700356 mCaches.blend = true;
357 glEnable(GL_BLEND);
358 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
359 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700360}
361
Romain Guy35643dd2012-09-18 15:40:58 -0700362void OpenGLRenderer::resumeAfterLayer() {
363 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
364 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
365 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700366 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700367
368 mCaches.resetScissor();
369 dirtyClip();
370}
371
Romain Guyba6be8a2012-04-23 18:22:09 -0700372void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700373 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700374}
375
376void OpenGLRenderer::attachFunctor(Functor* functor) {
377 mFunctors.add(functor);
378}
379
Romain Guy8f3b8e32012-03-27 16:33:45 -0700380status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
381 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700382 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700383
Romain Guyba6be8a2012-04-23 18:22:09 -0700384 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800385 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700386 SortedVector<Functor*> functors(mFunctors);
387 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700388
Romain Guyba6be8a2012-04-23 18:22:09 -0700389 DrawGlInfo info;
390 info.clipLeft = 0;
391 info.clipTop = 0;
392 info.clipRight = 0;
393 info.clipBottom = 0;
394 info.isLayer = false;
395 info.width = 0;
396 info.height = 0;
397 memset(info.transform, 0, sizeof(float) * 16);
398
399 for (size_t i = 0; i < count; i++) {
400 Functor* f = functors.itemAt(i);
401 result |= (*f)(DrawGlInfo::kModeProcess, &info);
402
Chris Craikc2c95432012-04-25 15:13:52 -0700403 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700404 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
405 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700406 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700407
Chris Craikc2c95432012-04-25 15:13:52 -0700408 if (result & DrawGlInfo::kStatusInvoke) {
409 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700410 }
411 }
Chris Craikd15321b2012-11-28 14:45:04 -0800412 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700413 }
414
415 return result;
416}
417
418status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800419 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700420 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700421
Romain Guy8a4ac612012-07-17 17:32:48 -0700422 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800423 if (mDirtyClip) {
424 setScissorFromClip();
425 }
Romain Guyd643bb52011-03-01 14:55:21 -0800426
Romain Guy80911b82011-03-16 15:30:12 -0700427 Rect clip(*mSnapshot->clipRect);
428 clip.snapToPixelBoundaries();
429
Romain Guyd643bb52011-03-01 14:55:21 -0800430 // Since we don't know what the functor will draw, let's dirty
431 // tne entire clip region
432 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800433 dirtyLayerUnchecked(clip, getRegion());
434 }
Romain Guyd643bb52011-03-01 14:55:21 -0800435
Romain Guy08aa2cb2011-03-17 11:06:57 -0700436 DrawGlInfo info;
437 info.clipLeft = clip.left;
438 info.clipTop = clip.top;
439 info.clipRight = clip.right;
440 info.clipBottom = clip.bottom;
441 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700442 info.width = getSnapshot()->viewport.getWidth();
443 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700444 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700445
Chet Haase48659092012-05-31 15:21:51 -0700446 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800447
Romain Guy8f3b8e32012-03-27 16:33:45 -0700448 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700449 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800450 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700451
Chris Craik65924a32012-04-05 17:52:11 -0700452 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700453 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700454 }
Romain Guycabfcc12011-03-07 18:06:46 -0800455 }
456
Chet Haasedaf98e92011-01-10 14:10:36 -0800457 resume();
Romain Guy65549432012-03-26 16:45:05 -0700458 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800459}
460
Romain Guyf6a11b82010-06-23 17:47:49 -0700461///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700462// Debug
463///////////////////////////////////////////////////////////////////////////////
464
465void OpenGLRenderer::startMark(const char* name) const {
466 mCaches.startMark(0, name);
467}
468
469void OpenGLRenderer::endMark() const {
470 mCaches.endMark();
471}
472
473void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
474 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
475 if (clear) {
476 mCaches.disableScissor();
477 mCaches.stencil.clear();
478 }
479 if (enable) {
480 mCaches.stencil.enableDebugWrite();
481 } else {
482 mCaches.stencil.disable();
483 }
484 }
485}
486
487void OpenGLRenderer::renderOverdraw() {
488 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
489 const Rect* clip = mTilingSnapshot->clipRect;
490
491 mCaches.enableScissor();
492 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
493 clip->right - clip->left, clip->bottom - clip->top);
494
495 mCaches.stencil.enableDebugTest(2);
496 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
497 mCaches.stencil.enableDebugTest(3);
498 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
499 mCaches.stencil.enableDebugTest(4);
500 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
501 mCaches.stencil.enableDebugTest(4, true);
502 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
503 mCaches.stencil.disable();
504 }
505}
506
507///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700508// Layers
509///////////////////////////////////////////////////////////////////////////////
510
511bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
512 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
513 OpenGLRenderer* renderer = layer->renderer;
514 Rect& dirty = layer->dirtyRect;
515
Romain Guy7c450aa2012-09-21 19:15:00 -0700516 if (inFrame) {
517 endTiling();
518 debugOverdraw(false, false);
519 }
Romain Guy11cb6422012-09-21 00:39:43 -0700520
521 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
522 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
523 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
524 renderer->finish();
525
526 if (inFrame) {
527 resumeAfterLayer();
528 startTiling(mSnapshot);
529 }
530
531 dirty.setEmpty();
532 layer->deferredUpdateScheduled = false;
533 layer->renderer = NULL;
534 layer->displayList = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -0800535 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700536
537 return true;
538 }
539
540 return false;
541}
542
543void OpenGLRenderer::updateLayers() {
544 int count = mLayerUpdates.size();
545 if (count > 0) {
546 startMark("Layer Updates");
547
548 // Note: it is very important to update the layers in reverse order
549 for (int i = count - 1; i >= 0; i--) {
550 Layer* layer = mLayerUpdates.itemAt(i);
551 updateLayer(layer, false);
552 mCaches.resourceCache.decrementRefcount(layer);
553 }
554 mLayerUpdates.clear();
555
556 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
557 endMark();
558 }
559}
560
561void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
562 if (layer) {
563 mLayerUpdates.push_back(layer);
564 mCaches.resourceCache.incrementRefcount(layer);
565 }
566}
567
568void OpenGLRenderer::clearLayerUpdates() {
569 size_t count = mLayerUpdates.size();
570 if (count > 0) {
571 mCaches.resourceCache.lock();
572 for (size_t i = 0; i < count; i++) {
573 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
574 }
575 mCaches.resourceCache.unlock();
576 mLayerUpdates.clear();
577 }
578}
579
580///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700581// State management
582///////////////////////////////////////////////////////////////////////////////
583
Romain Guybb9524b2010-06-22 18:56:38 -0700584int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700585 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700586}
587
588int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700589 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700590}
591
592void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700593 if (mSaveCount > 1) {
594 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700595 }
Romain Guybb9524b2010-06-22 18:56:38 -0700596}
597
598void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700599 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700600
Romain Guy8fb95422010-08-17 18:38:51 -0700601 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700602 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700603 }
Romain Guybb9524b2010-06-22 18:56:38 -0700604}
605
Romain Guy8aef54f2010-09-01 15:13:49 -0700606int OpenGLRenderer::saveSnapshot(int flags) {
607 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700608 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700609}
610
611bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700612 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700613 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700614 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700615
Romain Guybd6b79b2010-06-26 00:13:53 -0700616 sp<Snapshot> current = mSnapshot;
617 sp<Snapshot> previous = mSnapshot->previous;
618
Romain Guyeb993562010-10-05 18:14:38 -0700619 if (restoreOrtho) {
620 Rect& r = previous->viewport;
621 glViewport(r.left, r.top, r.right, r.bottom);
622 mOrthoMatrix.load(current->orthoMatrix);
623 }
624
Romain Guy8b55f372010-08-18 17:10:07 -0700625 mSaveCount--;
626 mSnapshot = previous;
627
Romain Guy2542d192010-08-18 11:47:12 -0700628 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700629 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700630 }
Romain Guy2542d192010-08-18 11:47:12 -0700631
Romain Guy5ec99242010-11-03 16:19:08 -0700632 if (restoreLayer) {
633 composeLayer(current, previous);
634 }
635
Romain Guy2542d192010-08-18 11:47:12 -0700636 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700637}
638
Romain Guyf6a11b82010-06-23 17:47:49 -0700639///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700640// Layers
641///////////////////////////////////////////////////////////////////////////////
642
643int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700644 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700645 const GLuint previousFbo = mSnapshot->fbo;
646 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700647
Romain Guyaf636eb2010-12-09 17:47:21 -0800648 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700649 int alpha = 255;
650 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700651
Romain Guye45362c2010-11-03 19:58:32 -0700652 if (p) {
653 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700654 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700655 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700656 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700657 }
Romain Guyd55a8612010-06-28 17:42:46 -0700658
Chet Haased48885a2012-08-28 17:43:28 -0700659 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700660 }
Romain Guyd55a8612010-06-28 17:42:46 -0700661
662 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700663}
664
665int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
666 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700667 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700668 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700669 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700670 SkPaint paint;
671 paint.setAlpha(alpha);
672 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700673 }
Romain Guyd55a8612010-06-28 17:42:46 -0700674}
Romain Guybd6b79b2010-06-26 00:13:53 -0700675
Romain Guy1c740bc2010-09-13 18:00:09 -0700676/**
677 * Layers are viewed by Skia are slightly different than layers in image editing
678 * programs (for instance.) When a layer is created, previously created layers
679 * and the frame buffer still receive every drawing command. For instance, if a
680 * layer is created and a shape intersecting the bounds of the layers and the
681 * framebuffer is draw, the shape will be drawn on both (unless the layer was
682 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
683 *
684 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
685 * texture. Unfortunately, this is inefficient as it requires every primitive to
686 * be drawn n + 1 times, where n is the number of active layers. In practice this
687 * means, for every primitive:
688 * - Switch active frame buffer
689 * - Change viewport, clip and projection matrix
690 * - Issue the drawing
691 *
692 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700693 * To avoid this, layers are implemented in a different way here, at least in the
694 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
695 * is set. When this flag is set we can redirect all drawing operations into a
696 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700697 *
698 * This implementation relies on the frame buffer being at least RGBA 8888. When
699 * a layer is created, only a texture is created, not an FBO. The content of the
700 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700701 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700702 * buffer and drawing continues as normal. This technique therefore treats the
703 * frame buffer as a scratch buffer for the layers.
704 *
705 * To compose the layers back onto the frame buffer, each layer texture
706 * (containing the original frame buffer data) is drawn as a simple quad over
707 * the frame buffer. The trick is that the quad is set as the composition
708 * destination in the blending equation, and the frame buffer becomes the source
709 * of the composition.
710 *
711 * Drawing layers with an alpha value requires an extra step before composition.
712 * An empty quad is drawn over the layer's region in the frame buffer. This quad
713 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
714 * quad is used to multiply the colors in the frame buffer. This is achieved by
715 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
716 * GL_ZERO, GL_SRC_ALPHA.
717 *
718 * Because glCopyTexImage2D() can be slow, an alternative implementation might
719 * be use to draw a single clipped layer. The implementation described above
720 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700721 *
722 * (1) The frame buffer is actually not cleared right away. To allow the GPU
723 * to potentially optimize series of calls to glCopyTexImage2D, the frame
724 * buffer is left untouched until the first drawing operation. Only when
725 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700726 */
Chet Haased48885a2012-08-28 17:43:28 -0700727bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
728 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700729 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700730 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700731
Romain Guyeb993562010-10-05 18:14:38 -0700732 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
733
Romain Guyf607bdc2010-09-10 19:20:06 -0700734 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700735 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700736 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700737 Rect untransformedBounds(bounds);
738 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700739
Chet Haased48885a2012-08-28 17:43:28 -0700740 // Layers only make sense if they are in the framebuffer's bounds
741 if (bounds.intersect(*mSnapshot->clipRect)) {
742 // We cannot work with sub-pixels in this case
743 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700744
Chet Haased48885a2012-08-28 17:43:28 -0700745 // When the layer is not an FBO, we may use glCopyTexImage so we
746 // need to make sure the layer does not extend outside the bounds
747 // of the framebuffer
748 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700749 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700750 } else if (fboLayer) {
751 clip.set(bounds);
752 mat4 inverse;
753 inverse.loadInverse(*mSnapshot->transform);
754 inverse.mapRect(clip);
755 clip.snapToPixelBoundaries();
756 if (clip.intersect(untransformedBounds)) {
757 clip.translate(-left, -top);
758 bounds.set(untransformedBounds);
759 } else {
760 clip.setEmpty();
761 }
Romain Guyad37cd32011-03-15 11:12:25 -0700762 }
Chet Haased48885a2012-08-28 17:43:28 -0700763 } else {
764 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700765 }
Romain Guybf434112010-09-16 14:40:17 -0700766
Romain Guy746b7402010-10-26 16:27:31 -0700767 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700768 bounds.getHeight() > mCaches.maxTextureSize ||
769 (fboLayer && clip.isEmpty())) {
770 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700771 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700772 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700773 }
774
775 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700776 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700777 return false;
778 }
Romain Guyf18fd992010-07-08 11:45:51 -0700779
Romain Guya1d3c912011-12-13 14:55:06 -0800780 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700781 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700782 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700783 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700784 }
785
Romain Guy9ace8f52011-07-07 20:50:11 -0700786 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700787 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700788 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
789 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800790 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700791 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700792 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700793
Romain Guy8fb95422010-08-17 18:38:51 -0700794 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700795 mSnapshot->flags |= Snapshot::kFlagIsLayer;
796 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700797
Romain Guyeb993562010-10-05 18:14:38 -0700798 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700799 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700800 } else {
801 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700802 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800803 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700804 if (layer->isEmpty()) {
805 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700806 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700807 layer->getWidth(), layer->getHeight(), 0);
808 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800809 } else {
810 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700811 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800812 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700813
Romain Guy54be1cd2011-06-13 19:04:27 -0700814 // Enqueue the buffer coordinates to clear the corresponding region later
815 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700816 }
Romain Guyeb993562010-10-05 18:14:38 -0700817 }
Romain Guyf86ef572010-07-01 11:05:42 -0700818
Romain Guyd55a8612010-06-28 17:42:46 -0700819 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700820}
821
Chet Haased48885a2012-08-28 17:43:28 -0700822bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800823 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700824 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700825
Chet Haased48885a2012-08-28 17:43:28 -0700826 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800827 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
828 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700829 mSnapshot->fbo = layer->getFbo();
830 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
831 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
832 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
833 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700834 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700835
Romain Guy2b7028e2012-09-19 17:25:38 -0700836 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700837 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700838 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700839 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
840 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700841
842 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700843 if (layer->isEmpty()) {
844 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
845 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700846 }
847
848 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700849 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700850
Romain Guyf735c8e2013-01-31 17:45:55 -0800851 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700852
853 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700854 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800855 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700856 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700857 glClear(GL_COLOR_BUFFER_BIT);
858
859 dirtyClip();
860
861 // Change the ortho projection
862 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
863 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
864
865 return true;
866}
867
Romain Guy1c740bc2010-09-13 18:00:09 -0700868/**
869 * Read the documentation of createLayer() before doing anything in this method.
870 */
Romain Guy1d83e192010-08-17 11:37:00 -0700871void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
872 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000873 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700874 return;
875 }
876
Romain Guy8ce00302013-01-15 18:51:42 -0800877 Layer* layer = current->layer;
878 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700879 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700880
881 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700882 endTiling();
883
Romain Guye0aa84b2012-04-03 19:30:26 -0700884 // Detach the texture from the FBO
885 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800886
887 layer->removeFbo(false);
888
Romain Guyeb993562010-10-05 18:14:38 -0700889 // Unbind current FBO and restore previous one
890 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700891 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700892
893 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700894 }
895
Romain Guy9ace8f52011-07-07 20:50:11 -0700896 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700897 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700898 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700899 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700900 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700901 }
902
Romain Guy03750a02010-10-18 14:06:08 -0700903 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700904
Romain Guya1d3c912011-12-13 14:55:06 -0800905 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700906
Romain Guy5b3b3522010-10-27 18:57:51 -0700907 // When the layer is stored in an FBO, we can save a bit of fillrate by
908 // drawing only the dirty region
909 if (fboLayer) {
910 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700911 if (layer->getColorFilter()) {
912 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800913 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700914 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700915 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800916 resetColorFilter();
917 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700918 } else if (!rect.isEmpty()) {
919 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
920 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700921 }
Romain Guy8b55f372010-08-18 17:10:07 -0700922
Romain Guy746b7402010-10-26 16:27:31 -0700923 dirtyClip();
924
Romain Guyeb993562010-10-05 18:14:38 -0700925 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700926 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700927 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700928 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700929 }
930}
931
Romain Guyaa6c24c2011-04-28 18:40:04 -0700932void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700933 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700934
935 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700936 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700937 setupDrawWithTexture();
938 } else {
939 setupDrawWithExternalTexture();
940 }
941 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700942 setupDrawColor(alpha, alpha, alpha, alpha);
943 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700944 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700945 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700946 setupDrawPureColorUniforms();
947 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700948 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
949 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700950 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700951 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700952 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700953 if (mSnapshot->transform->isPureTranslate() &&
954 layer->getWidth() == (uint32_t) rect.getWidth() &&
955 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700956 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
957 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
958
Romain Guyd21b6e12011-11-30 20:21:23 -0800959 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700960 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
961 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800962 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700963 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
964 }
965 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700966 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
967
968 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
969
970 finishDrawTexture();
971}
972
Romain Guy5b3b3522010-10-27 18:57:51 -0700973void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700974 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700975 const Rect& texCoords = layer->texCoords;
976 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
977 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700978
Romain Guy9ace8f52011-07-07 20:50:11 -0700979 float x = rect.left;
980 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700981 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700982 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700983 layer->getHeight() == (uint32_t) rect.getHeight();
984
985 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700986 // When we're swapping, the layer is already in screen coordinates
987 if (!swap) {
988 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
989 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
990 }
991
Romain Guyd21b6e12011-11-30 20:21:23 -0800992 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700993 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800994 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700995 }
996
997 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
998 layer->getTexture(), layer->getAlpha() / 255.0f,
999 layer->getMode(), layer->isBlend(),
1000 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1001 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001002
Romain Guyaa6c24c2011-04-28 18:40:04 -07001003 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1004 } else {
1005 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1006 drawTextureLayer(layer, rect);
1007 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1008 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001009}
1010
1011void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001012 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001013 layer->setRegionAsRect();
1014
Romain Guy40667672011-03-18 14:34:03 -07001015 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -07001016
Romain Guy5b3b3522010-10-27 18:57:51 -07001017 layer->region.clear();
1018 return;
1019 }
1020
Romain Guy8a3957d2011-09-07 17:55:15 -07001021 // TODO: See LayerRenderer.cpp::generateMesh() for important
1022 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -08001023 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001024 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001025 const android::Rect* rects;
1026 Region safeRegion;
1027 if (CC_LIKELY(hasRectToRectTransform())) {
1028 rects = layer->region.getArray(&count);
1029 } else {
1030 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1031 rects = safeRegion.getArray(&count);
1032 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001033
Romain Guy9ace8f52011-07-07 20:50:11 -07001034 const float alpha = layer->getAlpha() / 255.0f;
1035 const float texX = 1.0f / float(layer->getWidth());
1036 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001037 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001038
Romain Guy8ce00302013-01-15 18:51:42 -08001039 setupDraw();
1040
1041 // We must get (and therefore bind) the region mesh buffer
1042 // after we setup drawing in case we need to mess with the
1043 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001044 TextureVertex* mesh = mCaches.getRegionMesh();
1045 GLsizei numQuads = 0;
1046
Romain Guy7230a742011-01-10 22:26:16 -08001047 setupDrawWithTexture();
1048 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001049 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001050 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001051 setupDrawProgram();
1052 setupDrawDirtyRegionsDisabled();
1053 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001054 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001055 setupDrawTexture(layer->getTexture());
1056 if (mSnapshot->transform->isPureTranslate()) {
1057 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
1058 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
1059
Romain Guyd21b6e12011-11-30 20:21:23 -08001060 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001061 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1062 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001063 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001064 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1065 }
Romain Guy15bc6432011-12-13 13:11:32 -08001066 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001067
1068 for (size_t i = 0; i < count; i++) {
1069 const android::Rect* r = &rects[i];
1070
1071 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001072 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001073 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001074 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001075
1076 // TODO: Reject quads outside of the clip
1077 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1078 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1079 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1080 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1081
1082 numQuads++;
1083
1084 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1085 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1086 numQuads = 0;
1087 mesh = mCaches.getRegionMesh();
1088 }
1089 }
1090
1091 if (numQuads > 0) {
1092 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1093 }
1094
Romain Guy7230a742011-01-10 22:26:16 -08001095 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001096
1097#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001098 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001099#endif
1100
1101 layer->region.clear();
1102 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001103}
1104
Romain Guy3a3133d2011-02-01 22:59:58 -08001105void OpenGLRenderer::drawRegionRects(const Region& region) {
1106#if DEBUG_LAYERS_AS_REGIONS
1107 size_t count;
1108 const android::Rect* rects = region.getArray(&count);
1109
1110 uint32_t colors[] = {
1111 0x7fff0000, 0x7f00ff00,
1112 0x7f0000ff, 0x7fff00ff,
1113 };
1114
1115 int offset = 0;
1116 int32_t top = rects[0].top;
1117
1118 for (size_t i = 0; i < count; i++) {
1119 if (top != rects[i].top) {
1120 offset ^= 0x2;
1121 top = rects[i].top;
1122 }
1123
1124 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1125 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1126 SkXfermode::kSrcOver_Mode);
1127 }
1128#endif
1129}
1130
Romain Guy8ce00302013-01-15 18:51:42 -08001131void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1132 SkXfermode::Mode mode, bool dirty) {
1133 int count = 0;
1134 Vector<float> rects;
1135
1136 SkRegion::Iterator it(region);
1137 while (!it.done()) {
1138 const SkIRect& r = it.rect();
1139 rects.push(r.fLeft);
1140 rects.push(r.fTop);
1141 rects.push(r.fRight);
1142 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001143 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001144 it.next();
1145 }
1146
Romain Guy3bbacf22013-02-06 16:51:04 -08001147 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001148}
1149
Romain Guy5b3b3522010-10-27 18:57:51 -07001150void OpenGLRenderer::dirtyLayer(const float left, const float top,
1151 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001152 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001153 Rect bounds(left, top, right, bottom);
1154 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001155 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001156 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001157}
1158
1159void OpenGLRenderer::dirtyLayer(const float left, const float top,
1160 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001161 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001162 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001163 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001164 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001165}
1166
1167void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001168 if (bounds.intersect(*mSnapshot->clipRect)) {
1169 bounds.snapToPixelBoundaries();
1170 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1171 if (!dirty.isEmpty()) {
1172 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001173 }
1174 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001175}
1176
Romain Guy54be1cd2011-06-13 19:04:27 -07001177void OpenGLRenderer::clearLayerRegions() {
1178 const size_t count = mLayers.size();
1179 if (count == 0) return;
1180
1181 if (!mSnapshot->isIgnored()) {
1182 // Doing several glScissor/glClear here can negatively impact
1183 // GPUs with a tiler architecture, instead we draw quads with
1184 // the Clear blending mode
1185
1186 // The list contains bounds that have already been clipped
1187 // against their initial clip rect, and the current clip
1188 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001189 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001190
1191 Vertex mesh[count * 6];
1192 Vertex* vertex = mesh;
1193
1194 for (uint32_t i = 0; i < count; i++) {
1195 Rect* bounds = mLayers.itemAt(i);
1196
1197 Vertex::set(vertex++, bounds->left, bounds->bottom);
1198 Vertex::set(vertex++, bounds->left, bounds->top);
1199 Vertex::set(vertex++, bounds->right, bounds->top);
1200 Vertex::set(vertex++, bounds->left, bounds->bottom);
1201 Vertex::set(vertex++, bounds->right, bounds->top);
1202 Vertex::set(vertex++, bounds->right, bounds->bottom);
1203
1204 delete bounds;
1205 }
Romain Guye67307c2013-02-11 18:01:20 -08001206 // We must clear the list of dirty rects before we
1207 // call setupDraw() to prevent stencil setup to do
1208 // the same thing again
1209 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001210
1211 setupDraw(false);
1212 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1213 setupDrawBlending(true, SkXfermode::kClear_Mode);
1214 setupDrawProgram();
1215 setupDrawPureColorUniforms();
1216 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001217 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001218
Romain Guy54be1cd2011-06-13 19:04:27 -07001219 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001220
1221 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001222 } else {
1223 for (uint32_t i = 0; i < count; i++) {
1224 delete mLayers.itemAt(i);
1225 }
Romain Guye67307c2013-02-11 18:01:20 -08001226 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001227 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001228}
1229
Romain Guybd6b79b2010-06-26 00:13:53 -07001230///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001231// State Deferral
1232///////////////////////////////////////////////////////////////////////////////
1233
1234bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state) {
1235 const Rect& currentClip = *(mSnapshot->clipRect);
1236 const mat4& currentMatrix = *(mSnapshot->transform);
1237
1238 // state only has bounds initialized in local coordinates
1239 if (!state.mBounds.isEmpty()) {
1240 currentMatrix.mapRect(state.mBounds);
1241 if (!state.mBounds.intersect(currentClip)) {
1242 // quick rejected
1243 return true;
1244 }
Chris Craik5d116762013-02-19 17:49:31 -08001245 } else {
1246 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001247 }
1248
1249 state.mClip.set(currentClip);
1250 state.mMatrix.load(currentMatrix);
1251 state.mDrawModifiers = mDrawModifiers;
1252 return false;
1253}
1254
1255void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state) {
1256 mSnapshot->transform->load(state.mMatrix);
1257
1258 // NOTE: a clip RECT will be saved and restored, but DeferredDisplayState doesn't support
1259 // complex clips. In the future, we should add support for deferral of operations clipped by
1260 // these. for now, we don't defer with complex clips (see OpenGLRenderer::disallowDeferral())
1261 mSnapshot->setClip(state.mClip.left, state.mClip.top, state.mClip.right, state.mClip.bottom);
1262 dirtyClip();
1263 mDrawModifiers = state.mDrawModifiers;
1264}
1265
1266///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001267// Transforms
1268///////////////////////////////////////////////////////////////////////////////
1269
1270void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001271 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001272}
1273
1274void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001275 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001276}
1277
1278void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001279 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001280}
1281
Romain Guy807daf72011-01-18 11:19:19 -08001282void OpenGLRenderer::skew(float sx, float sy) {
1283 mSnapshot->transform->skew(sx, sy);
1284}
1285
Romain Guyf6a11b82010-06-23 17:47:49 -07001286void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001287 if (matrix) {
1288 mSnapshot->transform->load(*matrix);
1289 } else {
1290 mSnapshot->transform->loadIdentity();
1291 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001292}
1293
Chris Craikb98a0162013-02-21 11:30:22 -08001294bool OpenGLRenderer::hasRectToRectTransform() {
1295 return CC_LIKELY(mSnapshot->transform->rectToRect());
1296}
1297
Romain Guyf6a11b82010-06-23 17:47:49 -07001298void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001299 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001300}
1301
1302void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001303 SkMatrix transform;
1304 mSnapshot->transform->copyTo(transform);
1305 transform.preConcat(*matrix);
1306 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001307}
1308
1309///////////////////////////////////////////////////////////////////////////////
1310// Clipping
1311///////////////////////////////////////////////////////////////////////////////
1312
Romain Guybb9524b2010-06-22 18:56:38 -07001313void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001314 Rect clip(*mSnapshot->clipRect);
1315 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001316
Romain Guy8a4ac612012-07-17 17:32:48 -07001317 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1318 clip.getWidth(), clip.getHeight())) {
1319 mDirtyClip = false;
1320 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001321}
1322
Romain Guy8ce00302013-01-15 18:51:42 -08001323void OpenGLRenderer::ensureStencilBuffer() {
1324 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1325 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1326 // just hope we have one when hasLayer() returns false.
1327 if (hasLayer()) {
1328 attachStencilBufferToLayer(mSnapshot->layer);
1329 }
1330}
1331
1332void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1333 // The layer's FBO is already bound when we reach this stage
1334 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001335 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1336 // is attached after we initiated tiling. We must turn it off,
1337 // attach the new render buffer then turn tiling back on
1338 endTiling();
1339
Romain Guy8d4aeb72013-02-12 16:08:55 -08001340 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001341 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001342 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001343
Romain Guyf735c8e2013-01-31 17:45:55 -08001344 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001345 }
1346}
1347
1348void OpenGLRenderer::setStencilFromClip() {
1349 if (!mCaches.debugOverdraw) {
1350 if (!mSnapshot->clipRegion->isEmpty()) {
1351 // NOTE: The order here is important, we must set dirtyClip to false
1352 // before any draw call to avoid calling back into this method
1353 mDirtyClip = false;
1354
1355 ensureStencilBuffer();
1356
1357 mCaches.stencil.enableWrite();
1358
1359 // Clear the stencil but first make sure we restrict drawing
1360 // to the region's bounds
1361 bool resetScissor = mCaches.enableScissor();
1362 if (resetScissor) {
1363 // The scissor was not set so we now need to update it
1364 setScissorFromClip();
1365 }
1366 mCaches.stencil.clear();
1367 if (resetScissor) mCaches.disableScissor();
1368
1369 // NOTE: We could use the region contour path to generate a smaller mesh
1370 // Since we are using the stencil we could use the red book path
1371 // drawing technique. It might increase bandwidth usage though.
1372
1373 // The last parameter is important: we are not drawing in the color buffer
1374 // so we don't want to dirty the current layer, if any
1375 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1376
1377 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001378
1379 // Draw the region used to generate the stencil if the appropriate debug
1380 // mode is enabled
1381 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1382 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1383 }
Romain Guy8ce00302013-01-15 18:51:42 -08001384 } else {
1385 mCaches.stencil.disable();
1386 }
1387 }
1388}
1389
Romain Guy9d5316e2010-06-24 19:30:36 -07001390const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001391 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001392}
1393
Romain Guy8a4ac612012-07-17 17:32:48 -07001394bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1395 if (mSnapshot->isIgnored()) {
1396 return true;
1397 }
1398
1399 Rect r(left, top, right, bottom);
1400 mSnapshot->transform->mapRect(r);
1401 r.snapToPixelBoundaries();
1402
1403 Rect clipRect(*mSnapshot->clipRect);
1404 clipRect.snapToPixelBoundaries();
1405
1406 return !clipRect.intersects(r);
1407}
1408
Romain Guy35643dd2012-09-18 15:40:58 -07001409bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1410 Rect& transformed, Rect& clip) {
1411 if (mSnapshot->isIgnored()) {
1412 return true;
1413 }
1414
1415 transformed.set(left, top, right, bottom);
1416 mSnapshot->transform->mapRect(transformed);
1417 transformed.snapToPixelBoundaries();
1418
1419 clip.set(*mSnapshot->clipRect);
1420 clip.snapToPixelBoundaries();
1421
1422 return !clip.intersects(transformed);
1423}
1424
Romain Guy672433d2013-01-04 19:05:13 -08001425bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1426 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001427 if (paint->getStyle() != SkPaint::kFill_Style) {
1428 float outset = paint->getStrokeWidth() * 0.5f;
1429 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1430 } else {
1431 return quickReject(left, top, right, bottom);
1432 }
1433}
1434
Romain Guyc7d53492010-06-25 13:41:57 -07001435bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001436 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001437 return true;
1438 }
1439
Romain Guy1d83e192010-08-17 11:37:00 -07001440 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001441 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001442 r.snapToPixelBoundaries();
1443
1444 Rect clipRect(*mSnapshot->clipRect);
1445 clipRect.snapToPixelBoundaries();
1446
Romain Guy586cae32012-07-13 15:28:31 -07001447 bool rejected = !clipRect.intersects(r);
1448 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001449 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001450 }
1451
1452 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001453}
1454
Romain Guy8ce00302013-01-15 18:51:42 -08001455void OpenGLRenderer::debugClip() {
1456#if DEBUG_CLIP_REGIONS
1457 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1458 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1459 }
1460#endif
1461}
1462
Romain Guy079ba2c2010-07-16 14:12:24 -07001463bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001464 if (CC_LIKELY(mSnapshot->transform->rectToRect())) {
1465 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1466 if (clipped) {
1467 dirtyClip();
1468 }
1469 return !mSnapshot->clipRect->isEmpty();
1470 }
1471
1472 SkPath path;
1473 path.addRect(left, top, right, bottom);
1474
1475 return clipPath(&path, op);
1476}
1477
1478bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1479 SkMatrix transform;
1480 mSnapshot->transform->copyTo(transform);
1481
1482 SkPath transformed;
1483 path->transform(transform, &transformed);
1484
1485 SkRegion clip;
1486 if (!mSnapshot->clipRegion->isEmpty()) {
1487 clip.setRegion(*mSnapshot->clipRegion);
1488 } else {
1489 Rect* bounds = mSnapshot->clipRect;
1490 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1491 }
1492
1493 SkRegion region;
1494 region.setPath(transformed, clip);
1495
1496 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001497 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001498 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001499 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001500 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001501}
1502
Romain Guy735738c2012-12-03 12:34:51 -08001503bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001504 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1505 if (clipped) {
1506 dirtyClip();
1507 }
1508 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001509}
1510
Chet Haasea23eed82012-04-12 15:19:04 -07001511Rect* OpenGLRenderer::getClipRect() {
1512 return mSnapshot->clipRect;
1513}
1514
Romain Guyf6a11b82010-06-23 17:47:49 -07001515///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001516// Drawing commands
1517///////////////////////////////////////////////////////////////////////////////
1518
Romain Guy54be1cd2011-06-13 19:04:27 -07001519void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001520 // TODO: It would be best if we could do this before quickReject()
1521 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001522 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001523 // Make sure setScissor & setStencil happen at the beginning of
1524 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001525 if (mDirtyClip) {
1526 if (mCaches.scissorEnabled) {
1527 setScissorFromClip();
1528 }
Romain Guy8ce00302013-01-15 18:51:42 -08001529 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001530 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001531
Romain Guy70ca14e2010-12-13 18:24:33 -08001532 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001533
Romain Guy70ca14e2010-12-13 18:24:33 -08001534 mSetShaderColor = false;
1535 mColorSet = false;
1536 mColorA = mColorR = mColorG = mColorB = 0.0f;
1537 mTextureUnit = 0;
1538 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001539
1540 // Enable debug highlight when what we're about to draw is tested against
1541 // the stencil buffer and if stencil highlight debugging is on
1542 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1543 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1544 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001545}
1546
1547void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1548 mDescription.hasTexture = true;
1549 mDescription.hasAlpha8Texture = isAlpha8;
1550}
1551
Romain Guyff316ec2013-02-13 18:39:43 -08001552void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1553 mDescription.hasTexture = true;
1554 mDescription.hasColors = true;
1555 mDescription.hasAlpha8Texture = isAlpha8;
1556}
1557
Romain Guyaa6c24c2011-04-28 18:40:04 -07001558void OpenGLRenderer::setupDrawWithExternalTexture() {
1559 mDescription.hasExternalTexture = true;
1560}
1561
Romain Guy15bc6432011-12-13 13:11:32 -08001562void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001563 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001564}
1565
Chris Craik710f46d2012-09-17 17:25:49 -07001566void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001567 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001568}
1569
Romain Guyed6fcb02011-03-21 13:11:28 -07001570void OpenGLRenderer::setupDrawPoint(float pointSize) {
1571 mDescription.isPoint = true;
1572 mDescription.pointSize = pointSize;
1573}
1574
Romain Guy8d0d4782010-12-14 20:13:35 -08001575void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1576 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001577 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1578 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1579 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001580 mColorSet = true;
1581 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1582}
1583
Romain Guy86568192010-12-14 15:55:39 -08001584void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1585 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001586 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1587 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1588 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001589 mColorSet = true;
1590 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1591}
1592
Romain Guy41210632012-07-16 17:04:24 -07001593void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1594 mCaches.fontRenderer->describe(mDescription, paint);
1595}
1596
Romain Guy70ca14e2010-12-13 18:24:33 -08001597void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1598 mColorA = a;
1599 mColorR = r;
1600 mColorG = g;
1601 mColorB = b;
1602 mColorSet = true;
1603 mSetShaderColor = mDescription.setColor(r, g, b, a);
1604}
1605
1606void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001607 if (mDrawModifiers.mShader) {
1608 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001609 }
1610}
1611
1612void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001613 if (mDrawModifiers.mColorFilter) {
1614 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001615 }
1616}
1617
Romain Guyf09ef512011-05-27 11:43:46 -07001618void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1619 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1620 mColorA = 1.0f;
1621 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001622 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001623 }
1624}
1625
Romain Guy70ca14e2010-12-13 18:24:33 -08001626void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001627 // When the blending mode is kClear_Mode, we need to use a modulate color
1628 // argb=1,0,0,0
1629 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001630 bool blend = (mColorSet && mColorA < 1.0f) ||
1631 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1632 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001633}
1634
1635void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001636 // When the blending mode is kClear_Mode, we need to use a modulate color
1637 // argb=1,0,0,0
1638 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001639 blend |= (mColorSet && mColorA < 1.0f) ||
1640 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1641 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1642 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001643}
1644
1645void OpenGLRenderer::setupDrawProgram() {
1646 useProgram(mCaches.programCache.get(mDescription));
1647}
1648
1649void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1650 mTrackDirtyRegions = false;
1651}
1652
1653void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1654 bool ignoreTransform) {
1655 mModelView.loadTranslate(left, top, 0.0f);
1656 if (!ignoreTransform) {
1657 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1658 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1659 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001660 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001661 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1662 }
1663}
1664
Chet Haase8a5cc922011-04-26 07:28:09 -07001665void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guyc74f45a2013-02-26 19:10:14 -08001666 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001667}
1668
Romain Guy70ca14e2010-12-13 18:24:33 -08001669void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1670 bool ignoreTransform, bool ignoreModelView) {
1671 if (!ignoreModelView) {
1672 mModelView.loadTranslate(left, top, 0.0f);
1673 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001674 } else {
1675 mModelView.loadIdentity();
1676 }
Romain Guy86568192010-12-14 15:55:39 -08001677 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1678 if (!ignoreTransform) {
1679 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1680 if (mTrackDirtyRegions && dirty) {
1681 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1682 }
1683 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001684 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001685 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1686 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001687}
1688
Romain Guyed6fcb02011-03-21 13:11:28 -07001689void OpenGLRenderer::setupDrawPointUniforms() {
1690 int slot = mCaches.currentProgram->getUniform("pointSize");
1691 glUniform1f(slot, mDescription.pointSize);
1692}
1693
Romain Guy70ca14e2010-12-13 18:24:33 -08001694void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001695 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001696 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1697 }
1698}
1699
Romain Guy86568192010-12-14 15:55:39 -08001700void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001701 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001702 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001703 }
1704}
1705
Romain Guy70ca14e2010-12-13 18:24:33 -08001706void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001707 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001708 if (ignoreTransform) {
1709 mModelView.loadInverse(*mSnapshot->transform);
1710 }
Chris Craikc3566d02013-02-04 16:16:33 -08001711 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1712 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001713 }
1714}
1715
Romain Guy8d0d4782010-12-14 20:13:35 -08001716void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001717 if (mDrawModifiers.mShader) {
1718 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001719 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001720 }
1721}
1722
Romain Guy70ca14e2010-12-13 18:24:33 -08001723void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001724 if (mDrawModifiers.mColorFilter) {
1725 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001726 }
1727}
1728
Romain Guy41210632012-07-16 17:04:24 -07001729void OpenGLRenderer::setupDrawTextGammaUniforms() {
1730 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1731}
1732
Romain Guy70ca14e2010-12-13 18:24:33 -08001733void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001734 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001735 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001736 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001737}
1738
1739void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1740 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001741 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001742 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001743}
1744
Romain Guyaa6c24c2011-04-28 18:40:04 -07001745void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1746 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001747 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001748 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001749}
1750
Romain Guy8f0095c2011-05-02 17:24:22 -07001751void OpenGLRenderer::setupDrawTextureTransform() {
1752 mDescription.hasTextureTransform = true;
1753}
1754
1755void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001756 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1757 GL_FALSE, &transform.data[0]);
1758}
1759
Romain Guy70ca14e2010-12-13 18:24:33 -08001760void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001761 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001762 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001763 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001764 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001765 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001766 }
Romain Guyd71dd362011-12-12 19:03:35 -08001767
Chris Craikcb4d6002012-09-25 12:00:29 -07001768 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001769 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001770 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001771 }
1772
1773 mCaches.unbindIndicesBuffer();
1774}
1775
Romain Guyff316ec2013-02-13 18:39:43 -08001776void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1777 bool force = mCaches.unbindMeshBuffer();
1778 GLsizei stride = sizeof(ColorTextureVertex);
1779
1780 mCaches.bindPositionVertexPointer(force, vertices, stride);
1781 if (mCaches.currentProgram->texCoords >= 0) {
1782 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1783 }
1784 int slot = mCaches.currentProgram->getAttrib("colors");
1785 if (slot >= 0) {
1786 glEnableVertexAttribArray(slot);
1787 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1788 }
1789
1790 mCaches.unbindIndicesBuffer();
1791}
1792
Romain Guy15bc6432011-12-13 13:11:32 -08001793void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1794 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001795 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001796 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001797 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001798 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001799}
1800
Chet Haase5b0200b2011-04-13 17:58:08 -07001801void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001802 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001803 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001804 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001805}
1806
Romain Guy70ca14e2010-12-13 18:24:33 -08001807void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001808}
1809
1810///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001811// Drawing
1812///////////////////////////////////////////////////////////////////////////////
1813
Chris Craikc3566d02013-02-04 16:16:33 -08001814status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty, int32_t flags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001815 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1816 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001817 if (displayList && displayList->isRenderable()) {
Chris Craikc3566d02013-02-04 16:16:33 -08001818 if (CC_UNLIKELY(mDrawDeferDisabled)) {
1819 return displayList->replay(*this, dirty, flags, 0);
1820 }
1821
1822 DeferredDisplayList deferredList;
1823 return displayList->replay(*this, dirty, flags, 0, &deferredList);
Romain Guy0fe478e2010-11-08 12:08:41 -08001824 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001825
Romain Guy65549432012-03-26 16:45:05 -07001826 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001827}
1828
Chris Craikc3566d02013-02-04 16:16:33 -08001829void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07001830 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08001831 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07001832 }
1833}
1834
Romain Guya168d732011-03-18 16:50:13 -07001835void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1836 int alpha;
1837 SkXfermode::Mode mode;
1838 getAlphaAndMode(paint, &alpha, &mode);
1839
Romain Guy886b2752013-01-04 12:26:18 -08001840 int color = paint != NULL ? paint->getColor() : 0;
1841
Romain Guya168d732011-03-18 16:50:13 -07001842 float x = left;
1843 float y = top;
1844
Romain Guy886b2752013-01-04 12:26:18 -08001845 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1846
Romain Guya168d732011-03-18 16:50:13 -07001847 bool ignoreTransform = false;
1848 if (mSnapshot->transform->isPureTranslate()) {
1849 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1850 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1851 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001852
1853 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001854 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001855 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001856 }
1857
Romain Guy886b2752013-01-04 12:26:18 -08001858 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1859 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1860 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001861}
1862
Chet Haase48659092012-05-31 15:21:51 -07001863status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001864 const float right = left + bitmap->width();
1865 const float bottom = top + bitmap->height();
1866
1867 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001868 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001869 }
1870
Romain Guya1d3c912011-12-13 14:55:06 -08001871 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001872 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001873 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001874 const AutoTexture autoCleanup(texture);
1875
Romain Guy211370f2012-02-01 16:10:55 -08001876 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001877 drawAlphaBitmap(texture, left, top, paint);
1878 } else {
1879 drawTextureRect(left, top, right, bottom, texture, paint);
1880 }
Chet Haase48659092012-05-31 15:21:51 -07001881
1882 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001883}
1884
Chet Haase48659092012-05-31 15:21:51 -07001885status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001886 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1887 const mat4 transform(*matrix);
1888 transform.mapRect(r);
1889
Romain Guy6926c722010-07-12 20:20:03 -07001890 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001891 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001892 }
1893
Romain Guya1d3c912011-12-13 14:55:06 -08001894 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001895 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001896 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001897 const AutoTexture autoCleanup(texture);
1898
Romain Guy5b3b3522010-10-27 18:57:51 -07001899 // This could be done in a cheaper way, all we need is pass the matrix
1900 // to the vertex shader. The save/restore is a bit overkill.
1901 save(SkCanvas::kMatrix_SaveFlag);
1902 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08001903 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1904 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
1905 } else {
1906 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1907 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001908 restore();
Chet Haase48659092012-05-31 15:21:51 -07001909
1910 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001911}
1912
Chet Haase48659092012-05-31 15:21:51 -07001913status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001914 const float right = left + bitmap->width();
1915 const float bottom = top + bitmap->height();
1916
1917 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001918 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001919 }
1920
1921 mCaches.activeTexture(0);
1922 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1923 const AutoTexture autoCleanup(texture);
1924
Romain Guy886b2752013-01-04 12:26:18 -08001925 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1926 drawAlphaBitmap(texture, left, top, paint);
1927 } else {
1928 drawTextureRect(left, top, right, bottom, texture, paint);
1929 }
Chet Haase48659092012-05-31 15:21:51 -07001930
1931 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001932}
1933
Chet Haase48659092012-05-31 15:21:51 -07001934status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001935 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001936 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001937 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001938 }
1939
Romain Guyb18d2d02011-02-10 15:52:54 -08001940 float left = FLT_MAX;
1941 float top = FLT_MAX;
1942 float right = FLT_MIN;
1943 float bottom = FLT_MIN;
1944
Romain Guya92bb4d2012-10-16 11:08:44 -07001945 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001946
Romain Guyff316ec2013-02-13 18:39:43 -08001947 ColorTextureVertex mesh[count];
1948 ColorTextureVertex* vertex = mesh;
1949
1950 bool cleanupColors = false;
1951 if (!colors) {
1952 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
1953 colors = new int[colorsCount];
1954 memset(colors, 0xff, colorsCount * sizeof(int));
1955 cleanupColors = true;
1956 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001957
Romain Guy5a7b4662011-01-20 19:09:30 -08001958 for (int32_t y = 0; y < meshHeight; y++) {
1959 for (int32_t x = 0; x < meshWidth; x++) {
1960 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1961
1962 float u1 = float(x) / meshWidth;
1963 float u2 = float(x + 1) / meshWidth;
1964 float v1 = float(y) / meshHeight;
1965 float v2 = float(y + 1) / meshHeight;
1966
1967 int ax = i + (meshWidth + 1) * 2;
1968 int ay = ax + 1;
1969 int bx = i;
1970 int by = bx + 1;
1971 int cx = i + 2;
1972 int cy = cx + 1;
1973 int dx = i + (meshWidth + 1) * 2 + 2;
1974 int dy = dx + 1;
1975
Romain Guyff316ec2013-02-13 18:39:43 -08001976 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1977 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
1978 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08001979
Romain Guyff316ec2013-02-13 18:39:43 -08001980 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1981 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
1982 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08001983
Romain Guya92bb4d2012-10-16 11:08:44 -07001984 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1985 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1986 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1987 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001988 }
1989 }
1990
Romain Guya92bb4d2012-10-16 11:08:44 -07001991 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08001992 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07001993 return DrawGlInfo::kStatusDone;
1994 }
1995
1996 mCaches.activeTexture(0);
1997 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guyff316ec2013-02-13 18:39:43 -08001998 if (!texture) {
1999 if (cleanupColors) delete[] colors;
2000 return DrawGlInfo::kStatusDone;
2001 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002002 const AutoTexture autoCleanup(texture);
2003
2004 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2005 texture->setFilter(FILTER(paint), true);
2006
2007 int alpha;
2008 SkXfermode::Mode mode;
2009 getAlphaAndMode(paint, &alpha, &mode);
2010
Romain Guyff316ec2013-02-13 18:39:43 -08002011 float a = alpha / 255.0f;
2012
Romain Guya92bb4d2012-10-16 11:08:44 -07002013 if (hasLayer()) {
Romain Guyb18d2d02011-02-10 15:52:54 -08002014 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
2015 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002016
Romain Guyff316ec2013-02-13 18:39:43 -08002017 setupDraw();
2018 setupDrawWithTextureAndColor();
2019 setupDrawColor(a, a, a, a);
2020 setupDrawColorFilter();
2021 setupDrawBlending(true, mode, false);
2022 setupDrawProgram();
2023 setupDrawDirtyRegionsDisabled();
2024 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2025 setupDrawTexture(texture->id);
2026 setupDrawPureColorUniforms();
2027 setupDrawColorFilterUniforms();
2028 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2029
2030 glDrawArrays(GL_TRIANGLES, 0, count);
2031
2032 finishDrawTexture();
2033
2034 int slot = mCaches.currentProgram->getAttrib("colors");
2035 if (slot >= 0) {
2036 glDisableVertexAttribArray(slot);
2037 }
2038
2039 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002040
2041 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002042}
2043
Chet Haase48659092012-05-31 15:21:51 -07002044status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002045 float srcLeft, float srcTop, float srcRight, float srcBottom,
2046 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002047 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002048 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002049 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002050 }
2051
Romain Guya1d3c912011-12-13 14:55:06 -08002052 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002053 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002054 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002055 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002056
Romain Guy8ba548f2010-06-30 19:21:21 -07002057 const float width = texture->width;
2058 const float height = texture->height;
2059
Romain Guy68169722011-08-22 17:33:33 -07002060 const float u1 = fmax(0.0f, srcLeft / width);
2061 const float v1 = fmax(0.0f, srcTop / height);
2062 const float u2 = fmin(1.0f, srcRight / width);
2063 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07002064
Romain Guy03750a02010-10-18 14:06:08 -07002065 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002066 resetDrawTextureTexCoords(u1, v1, u2, v2);
2067
Romain Guy03750a02010-10-18 14:06:08 -07002068 int alpha;
2069 SkXfermode::Mode mode;
2070 getAlphaAndMode(paint, &alpha, &mode);
2071
Romain Guyd21b6e12011-11-30 20:21:23 -08002072 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2073
Romain Guy886b2752013-01-04 12:26:18 -08002074 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2075 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002076
Romain Guy886b2752013-01-04 12:26:18 -08002077 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2078 // Apply a scale transform on the canvas only when a shader is in use
2079 // Skia handles the ratio between the dst and src rects as a scale factor
2080 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002081 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002082 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002083
Romain Guy886b2752013-01-04 12:26:18 -08002084 if (CC_LIKELY(mSnapshot->transform->isPureTranslate() && !useScaleTransform)) {
2085 float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
2086 float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
2087
2088 dstRight = x + (dstRight - dstLeft);
2089 dstBottom = y + (dstBottom - dstTop);
2090
2091 dstLeft = x;
2092 dstTop = y;
2093
2094 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2095 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002096 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002097 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002098 }
2099
2100 if (CC_UNLIKELY(useScaleTransform)) {
2101 save(SkCanvas::kMatrix_SaveFlag);
2102 translate(dstLeft, dstTop);
2103 scale(scaleX, scaleY);
2104
2105 dstLeft = 0.0f;
2106 dstTop = 0.0f;
2107
2108 dstRight = srcRight - srcLeft;
2109 dstBottom = srcBottom - srcTop;
2110 }
2111
2112 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2113 int color = paint ? paint->getColor() : 0;
2114 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2115 texture->id, paint != NULL, color, alpha, mode,
2116 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2117 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2118 } else {
2119 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2120 texture->id, alpha / 255.0f, mode, texture->blend,
2121 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2122 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2123 }
2124
2125 if (CC_UNLIKELY(useScaleTransform)) {
2126 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002127 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002128
2129 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002130
2131 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002132}
2133
Chet Haase48659092012-05-31 15:21:51 -07002134status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07002135 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07002136 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002137 int alpha;
2138 SkXfermode::Mode mode;
2139 getAlphaAndModeDirect(paint, &alpha, &mode);
2140
2141 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2142 left, top, right, bottom, alpha, mode);
2143}
2144
2145status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2146 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2147 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002148 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002149 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002150 }
2151
Romain Guybe6f9dc2012-07-16 12:41:17 -07002152 alpha *= mSnapshot->alpha;
2153
Romain Guy2728f962010-10-08 18:36:15 -07002154 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002155 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002156
Romain Guy211370f2012-02-01 16:10:55 -08002157 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002158 mCaches.activeTexture(0);
2159 Texture* texture = mCaches.textureCache.get(bitmap);
2160 if (!texture) return DrawGlInfo::kStatusDone;
2161 const AutoTexture autoCleanup(texture);
2162 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2163 texture->setFilter(GL_LINEAR, true);
2164
Romain Guy6620c6d2010-12-06 18:07:02 -08002165 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002166 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002167 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002168 const float offsetX = left + mSnapshot->transform->getTranslateX();
2169 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002170 const size_t count = mesh->quads.size();
2171 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002172 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002173 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002174 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2175 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2176 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002177 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002178 dirtyLayer(left + bounds.left, top + bounds.top,
2179 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08002180 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002181 }
2182 }
2183
Romain Guy211370f2012-02-01 16:10:55 -08002184 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002185 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2186 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2187
2188 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2189 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2190 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2191 true, !mesh->hasEmptyQuads);
2192 } else {
2193 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2194 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2195 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2196 true, !mesh->hasEmptyQuads);
2197 }
Romain Guy054dc182010-10-15 17:55:25 -07002198 }
Chet Haase48659092012-05-31 15:21:51 -07002199
2200 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002201}
2202
Chris Craik65cd6122012-12-10 17:56:27 -08002203status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2204 bool useOffset) {
2205 if (!vertexBuffer.getSize()) {
2206 // no vertices to draw
2207 return DrawGlInfo::kStatusDone;
2208 }
2209
Chris Craikcb4d6002012-09-25 12:00:29 -07002210 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002211 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2212 bool isAA = paint->isAntiAlias();
2213
Chris Craik710f46d2012-09-17 17:25:49 -07002214 setupDraw();
2215 setupDrawNoTexture();
2216 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002217 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2218 setupDrawColorFilter();
2219 setupDrawShader();
2220 setupDrawBlending(isAA, mode);
2221 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002222 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002223 setupDrawColorUniforms();
2224 setupDrawColorFilterUniforms();
2225 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002226
Chris Craik710f46d2012-09-17 17:25:49 -07002227 void* vertices = vertexBuffer.getBuffer();
2228 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002229 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002230 mCaches.resetTexCoordsVertexPointer();
2231 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002232
Chris Craik710f46d2012-09-17 17:25:49 -07002233 int alphaSlot = -1;
2234 if (isAA) {
2235 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2236 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002237
Chris Craik710f46d2012-09-17 17:25:49 -07002238 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002239 glEnableVertexAttribArray(alphaSlot);
2240 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002241 }
Romain Guy04299382012-07-18 17:15:41 -07002242
Chris Craik710f46d2012-09-17 17:25:49 -07002243 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002244
Chris Craik710f46d2012-09-17 17:25:49 -07002245 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002246 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002247 }
Chris Craik65cd6122012-12-10 17:56:27 -08002248
2249 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002250}
2251
2252/**
Chris Craik65cd6122012-12-10 17:56:27 -08002253 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2254 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2255 * screen space in all directions. However, instead of using a fragment shader to compute the
2256 * translucency of the color from its position, we simply use a varying parameter to define how far
2257 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2258 *
2259 * Doesn't yet support joins, caps, or path effects.
2260 */
2261status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2262 VertexBuffer vertexBuffer;
2263 // TODO: try clipping large paths to viewport
2264 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2265
2266 SkRect bounds = path.getBounds();
2267 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2268 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
2269
2270 return drawVertexBuffer(vertexBuffer, paint);
2271}
2272
2273/**
2274 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2275 * and additional geometry for defining an alpha slope perimeter.
2276 *
2277 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2278 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2279 * in-shader alpha region, but found it to be taxing on some GPUs.
2280 *
2281 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2282 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002283 */
Chet Haase48659092012-05-31 15:21:51 -07002284status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002285 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002286
Chris Craik65cd6122012-12-10 17:56:27 -08002287 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002288
Chris Craik65cd6122012-12-10 17:56:27 -08002289 VertexBuffer buffer;
2290 SkRect bounds;
2291 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002292
2293 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2294 return DrawGlInfo::kStatusDone;
2295 }
2296
Chris Craik65cd6122012-12-10 17:56:27 -08002297 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy7b631422012-04-04 11:38:54 -07002298
Chris Craik65cd6122012-12-10 17:56:27 -08002299 bool useOffset = !paint->isAntiAlias();
2300 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002301}
2302
Chet Haase48659092012-05-31 15:21:51 -07002303status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2304 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002305
2306 // TODO: The paint's cap style defines whether the points are square or circular
2307 // TODO: Handle AA for round points
2308
Chet Haase5b0200b2011-04-13 17:58:08 -07002309 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002310 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002311 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002312 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002313 if (isHairLine) {
2314 // Now that we know it's hairline, we can set the effective width, to be used later
2315 strokeWidth = 1.0f;
2316 }
2317 const float halfWidth = strokeWidth / 2;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002318
Romain Guyed6fcb02011-03-21 13:11:28 -07002319 int alpha;
2320 SkXfermode::Mode mode;
2321 getAlphaAndMode(paint, &alpha, &mode);
2322
2323 int verticesCount = count >> 1;
2324 int generatedVerticesCount = 0;
2325
2326 TextureVertex pointsData[verticesCount];
2327 TextureVertex* vertex = &pointsData[0];
2328
Romain Guy04299382012-07-18 17:15:41 -07002329 // TODO: We should optimize this method to not generate vertices for points
2330 // that lie outside of the clip.
2331 mCaches.enableScissor();
2332
Romain Guyed6fcb02011-03-21 13:11:28 -07002333 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002334 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002335 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002336 setupDrawColor(paint->getColor(), alpha);
2337 setupDrawColorFilter();
2338 setupDrawShader();
2339 setupDrawBlending(mode);
2340 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002341 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002342 setupDrawColorUniforms();
2343 setupDrawColorFilterUniforms();
2344 setupDrawPointUniforms();
2345 setupDrawShaderIdentityUniforms();
2346 setupDrawMesh(vertex);
2347
2348 for (int i = 0; i < count; i += 2) {
2349 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2350 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002351
Chet Haase8a5cc922011-04-26 07:28:09 -07002352 float left = points[i] - halfWidth;
2353 float right = points[i] + halfWidth;
2354 float top = points[i + 1] - halfWidth;
2355 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002356
Chet Haase8a5cc922011-04-26 07:28:09 -07002357 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002358 }
2359
2360 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002361
2362 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002363}
2364
Chet Haase48659092012-05-31 15:21:51 -07002365status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002366 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002367 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002368
Romain Guyae88e5e2010-10-22 17:49:18 -07002369 Rect& clip(*mSnapshot->clipRect);
2370 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002371
Romain Guy3d58c032010-07-14 16:34:53 -07002372 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002373
2374 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002375}
Romain Guy9d5316e2010-06-24 19:30:36 -07002376
Chet Haase48659092012-05-31 15:21:51 -07002377status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2378 SkPaint* paint) {
2379 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002380 const AutoTexture autoCleanup(texture);
2381
2382 const float x = left + texture->left - texture->offset;
2383 const float y = top + texture->top - texture->offset;
2384
2385 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002386
2387 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002388}
2389
Chet Haase48659092012-05-31 15:21:51 -07002390status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002391 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002392 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002393 return DrawGlInfo::kStatusDone;
2394 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002395
Chris Craikcb4d6002012-09-25 12:00:29 -07002396 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002397 mCaches.activeTexture(0);
2398 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2399 right - left, bottom - top, rx, ry, p);
2400 return drawShape(left, top, texture, p);
2401 }
2402
2403 SkPath path;
2404 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002405 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2406 float outset = p->getStrokeWidth() / 2;
2407 rect.outset(outset, outset);
2408 rx += outset;
2409 ry += outset;
2410 }
Chris Craik710f46d2012-09-17 17:25:49 -07002411 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002412 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002413}
2414
Chris Craik710f46d2012-09-17 17:25:49 -07002415status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002416 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2417 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002418 return DrawGlInfo::kStatusDone;
2419 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002420 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002421 mCaches.activeTexture(0);
2422 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2423 return drawShape(x - radius, y - radius, texture, p);
2424 }
2425
2426 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002427 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2428 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2429 } else {
2430 path.addCircle(x, y, radius);
2431 }
Chris Craik65cd6122012-12-10 17:56:27 -08002432 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002433}
Romain Guy01d58e42011-01-19 21:54:02 -08002434
Chet Haase48659092012-05-31 15:21:51 -07002435status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002436 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002437 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002438 return DrawGlInfo::kStatusDone;
2439 }
Romain Guy01d58e42011-01-19 21:54:02 -08002440
Chris Craikcb4d6002012-09-25 12:00:29 -07002441 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002442 mCaches.activeTexture(0);
2443 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2444 return drawShape(left, top, texture, p);
2445 }
2446
2447 SkPath path;
2448 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002449 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2450 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2451 }
Chris Craik710f46d2012-09-17 17:25:49 -07002452 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002453 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002454}
2455
Chet Haase48659092012-05-31 15:21:51 -07002456status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002457 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
2458 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
2459 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002460 }
2461
Chris Craik780c1282012-10-04 14:10:49 -07002462 if (fabs(sweepAngle) >= 360.0f) {
2463 return drawOval(left, top, right, bottom, p);
2464 }
2465
2466 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002467 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002468 mCaches.activeTexture(0);
2469 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2470 startAngle, sweepAngle, useCenter, p);
2471 return drawShape(left, top, texture, p);
2472 }
2473
2474 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2475 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2476 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2477 }
2478
2479 SkPath path;
2480 if (useCenter) {
2481 path.moveTo(rect.centerX(), rect.centerY());
2482 }
2483 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2484 if (useCenter) {
2485 path.close();
2486 }
Chris Craik65cd6122012-12-10 17:56:27 -08002487 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002488}
2489
Romain Guycf8675e2012-10-02 12:32:25 -07002490// See SkPaintDefaults.h
2491#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2492
Chet Haase48659092012-05-31 15:21:51 -07002493status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002494 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002495 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002496 }
2497
Chris Craik710f46d2012-09-17 17:25:49 -07002498 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002499 // only fill style is supported by drawConvexPath, since others have to handle joins
2500 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2501 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2502 mCaches.activeTexture(0);
2503 const PathTexture* texture =
2504 mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2505 return drawShape(left, top, texture, p);
2506 }
2507
2508 SkPath path;
2509 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2510 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2511 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2512 }
2513 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002514 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002515 }
2516
Romain Guy181d0a62011-06-09 18:52:38 -07002517 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002518 SkPath path;
2519 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002520 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002521 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002522 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002523 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002524 }
Romain Guyc7d53492010-06-25 13:41:57 -07002525}
Romain Guy9d5316e2010-06-24 19:30:36 -07002526
Raph Levien416a8472012-07-19 22:48:17 -07002527void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2528 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2529 float x, float y) {
2530 mCaches.activeTexture(0);
2531
2532 // NOTE: The drop shadow will not perform gamma correction
2533 // if shader-based correction is enabled
2534 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2535 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002536 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Raph Levien416a8472012-07-19 22:48:17 -07002537 const AutoTexture autoCleanup(shadow);
2538
Chris Craikc3566d02013-02-04 16:16:33 -08002539 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2540 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002541
Chris Craikc3566d02013-02-04 16:16:33 -08002542 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2543 int shadowColor = mDrawModifiers.mShadowColor;
2544 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002545 shadowColor = 0xffffffff;
2546 }
2547
2548 setupDraw();
2549 setupDrawWithTexture(true);
2550 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2551 setupDrawColorFilter();
2552 setupDrawShader();
2553 setupDrawBlending(true, mode);
2554 setupDrawProgram();
2555 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2556 setupDrawTexture(shadow->id);
2557 setupDrawPureColorUniforms();
2558 setupDrawColorFilterUniforms();
2559 setupDrawShaderUniforms();
2560 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2561
2562 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2563}
2564
Romain Guy768bffc2013-02-27 13:50:45 -08002565bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2566 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2567 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2568}
2569
Chet Haase48659092012-05-31 15:21:51 -07002570status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002571 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002572 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002573 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002574 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002575
Romain Guy671d6cf2012-01-18 12:39:17 -08002576 // NOTE: Skia does not support perspective transform on drawPosText yet
2577 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002578 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002579 }
2580
2581 float x = 0.0f;
2582 float y = 0.0f;
2583 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2584 if (pureTranslate) {
2585 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2586 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2587 }
2588
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002589 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002590 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002591
2592 int alpha;
2593 SkXfermode::Mode mode;
2594 getAlphaAndMode(paint, &alpha, &mode);
2595
Chris Craikc3566d02013-02-04 16:16:33 -08002596 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002597 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2598 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002599 }
2600
Romain Guy671d6cf2012-01-18 12:39:17 -08002601 // Pick the appropriate texture filtering
2602 bool linearFilter = mSnapshot->transform->changesBounds();
2603 if (pureTranslate && !linearFilter) {
2604 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2605 }
2606
2607 mCaches.activeTexture(0);
2608 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002609 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002610 setupDrawDirtyRegionsDisabled();
2611 setupDrawWithTexture(true);
2612 setupDrawAlpha8Color(paint->getColor(), alpha);
2613 setupDrawColorFilter();
2614 setupDrawShader();
2615 setupDrawBlending(true, mode);
2616 setupDrawProgram();
2617 setupDrawModelView(x, y, x, y, pureTranslate, true);
2618 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2619 setupDrawPureColorUniforms();
2620 setupDrawColorFilterUniforms();
2621 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002622 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002623
2624 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2625 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2626
Romain Guy211370f2012-02-01 16:10:55 -08002627 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002628
2629 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2630 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002631 if (hasActiveLayer) {
2632 if (!pureTranslate) {
2633 mSnapshot->transform->mapRect(bounds);
2634 }
2635 dirtyLayerUnchecked(bounds, getRegion());
2636 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002637 }
Chet Haase48659092012-05-31 15:21:51 -07002638
2639 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002640}
2641
Romain Guyc2525952012-07-27 16:41:22 -07002642status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002643 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy768bffc2013-02-27 13:50:45 -08002644 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002645 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002646 }
2647
Chet Haasea1cff502012-02-21 13:43:44 -08002648 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002649 switch (paint->getTextAlign()) {
2650 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002651 x -= length / 2.0f;
2652 break;
2653 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002654 x -= length;
2655 break;
2656 default:
2657 break;
2658 }
2659
Romain Guycac5fd32011-12-01 20:08:50 -08002660 SkPaint::FontMetrics metrics;
2661 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002662 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002663 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002664 }
2665
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002666 const float oldX = x;
2667 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002668 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya4adcf02013-02-28 12:15:35 -08002669 const bool isPerspective = mSnapshot->transform->isPerspective();
Romain Guyc74f45a2013-02-26 19:10:14 -08002670
Romain Guy211370f2012-02-01 16:10:55 -08002671 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002672 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2673 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2674 }
2675
Romain Guy86568192010-12-14 15:55:39 -08002676 int alpha;
2677 SkXfermode::Mode mode;
2678 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002679
Romain Guyc74f45a2013-02-26 19:10:14 -08002680 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2681
Chris Craikc3566d02013-02-04 16:16:33 -08002682 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002683 fontRenderer.setFont(paint, mat4::identity());
2684 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2685 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002686 }
2687
Romain Guyc74f45a2013-02-26 19:10:14 -08002688 fontRenderer.setFont(paint, pureTranslate ? mat4::identity() : *mSnapshot->transform);
2689
Romain Guy6620c6d2010-12-06 18:07:02 -08002690 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002691 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -07002692
Romain Guy16c88082012-06-11 16:03:47 -07002693 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002694 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002695 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002696 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002697 setupDrawDirtyRegionsDisabled();
2698 setupDrawWithTexture(true);
2699 setupDrawAlpha8Color(paint->getColor(), alpha);
2700 setupDrawColorFilter();
2701 setupDrawShader();
2702 setupDrawBlending(true, mode);
2703 setupDrawProgram();
Romain Guya4adcf02013-02-28 12:15:35 -08002704 setupDrawModelView(x, y, x, y, !isPerspective, true);
Romain Guy16c88082012-06-11 16:03:47 -07002705 // See comment above; the font renderer must use texture unit 0
2706 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002707 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2708 setupDrawPureColorUniforms();
2709 setupDrawColorFilterUniforms();
Romain Guya4adcf02013-02-28 12:15:35 -08002710 setupDrawShaderUniforms(!isPerspective);
Romain Guy41210632012-07-16 17:04:24 -07002711 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002712
Romain Guyc74f45a2013-02-26 19:10:14 -08002713 const Rect* clip = mSnapshot->hasPerspectiveTransform() ? NULL : mSnapshot->clipRect;
Romain Guy5b3b3522010-10-27 18:57:51 -07002714 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2715
Romain Guy211370f2012-02-01 16:10:55 -08002716 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002717
Raph Levien996e57c2012-07-23 15:22:52 -07002718 bool status;
Romain Guya3dc55f2012-09-28 13:55:44 -07002719 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002720 SkPaint paintCopy(*paint);
2721 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2722 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002723 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002724 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002725 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002726 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002727 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002728
2729 if (status && hasActiveLayer) {
Romain Guya4adcf02013-02-28 12:15:35 -08002730 if (isPerspective) {
2731 mSnapshot->transform->mapRect(bounds);
2732 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002733 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002734 }
Romain Guy694b5192010-07-21 21:33:20 -07002735
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002736 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002737
2738 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002739}
2740
Chet Haase48659092012-05-31 15:21:51 -07002741status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002742 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002743 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002744 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002745 }
2746
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002747 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002748 fontRenderer.setFont(paint, mat4::identity());
Romain Guy03d58522012-02-24 17:54:07 -08002749
2750 int alpha;
2751 SkXfermode::Mode mode;
2752 getAlphaAndMode(paint, &alpha, &mode);
2753
2754 mCaches.activeTexture(0);
2755 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002756 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002757 setupDrawDirtyRegionsDisabled();
2758 setupDrawWithTexture(true);
2759 setupDrawAlpha8Color(paint->getColor(), alpha);
2760 setupDrawColorFilter();
2761 setupDrawShader();
2762 setupDrawBlending(true, mode);
2763 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002764 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002765 setupDrawTexture(fontRenderer.getTexture(true));
2766 setupDrawPureColorUniforms();
2767 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002768 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002769 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002770
Romain Guy97771732012-02-28 18:17:02 -08002771 const Rect* clip = &mSnapshot->getLocalClip();
2772 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 -08002773
Romain Guy97771732012-02-28 18:17:02 -08002774 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002775
2776 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2777 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002778 if (hasActiveLayer) {
2779 mSnapshot->transform->mapRect(bounds);
2780 dirtyLayerUnchecked(bounds, getRegion());
2781 }
Romain Guy97771732012-02-28 18:17:02 -08002782 }
Chet Haase48659092012-05-31 15:21:51 -07002783
2784 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002785}
2786
Chet Haase48659092012-05-31 15:21:51 -07002787status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2788 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002789
Romain Guya1d3c912011-12-13 14:55:06 -08002790 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002791
Romain Guyfb8b7632010-08-23 21:05:08 -07002792 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002793 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002794 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002795
Romain Guy8b55f372010-08-18 17:10:07 -07002796 const float x = texture->left - texture->offset;
2797 const float y = texture->top - texture->offset;
2798
Romain Guy01d58e42011-01-19 21:54:02 -08002799 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002800
2801 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002802}
2803
Chet Haase48659092012-05-31 15:21:51 -07002804status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002805 if (!layer) {
2806 return DrawGlInfo::kStatusDone;
2807 }
2808
Romain Guyb2e2f242012-10-17 18:18:35 -07002809 mat4* transform = NULL;
2810 if (layer->isTextureLayer()) {
2811 transform = &layer->getTransform();
2812 if (!transform->isIdentity()) {
2813 save(0);
2814 mSnapshot->transform->multiply(*transform);
2815 }
2816 }
2817
Romain Guy35643dd2012-09-18 15:40:58 -07002818 Rect transformed;
2819 Rect clip;
2820 const bool rejected = quickRejectNoScissor(x, y,
2821 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2822
2823 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002824 if (transform && !transform->isIdentity()) {
2825 restore();
2826 }
Chet Haase48659092012-05-31 15:21:51 -07002827 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002828 }
2829
Romain Guy5bb3c732012-11-29 17:52:58 -08002830 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002831
Romain Guy87e2f7572012-09-24 11:37:12 -07002832 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002833 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002834
Romain Guy211370f2012-02-01 16:10:55 -08002835 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08002836 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
2837 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07002838
Romain Guyc88e3572011-01-22 00:32:12 -08002839 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002840 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002841 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002842 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002843 setupDraw();
2844 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002845 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002846 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002847 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002848 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002849 setupDrawPureColorUniforms();
2850 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002851 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002852 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002853 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2854 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002855
Romain Guyd21b6e12011-11-30 20:21:23 -08002856 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002857 setupDrawModelViewTranslate(tx, ty,
2858 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002859 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002860 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002861 setupDrawModelViewTranslate(x, y,
2862 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2863 }
Romain Guyc88e3572011-01-22 00:32:12 -08002864 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002865
Romain Guyc88e3572011-01-22 00:32:12 -08002866 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2867 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002868
Romain Guyc88e3572011-01-22 00:32:12 -08002869 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002870
2871#if DEBUG_LAYERS_AS_REGIONS
2872 drawRegionRects(layer->region);
2873#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002874 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002875
Chris Craikc3566d02013-02-04 16:16:33 -08002876 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07002877
Romain Guy5bb3c732012-11-29 17:52:58 -08002878 if (layer->debugDrawUpdate) {
2879 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002880 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2881 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2882 }
Romain Guyf219da52011-01-16 12:54:25 -08002883 }
Chet Haase48659092012-05-31 15:21:51 -07002884
Romain Guyb2e2f242012-10-17 18:18:35 -07002885 if (transform && !transform->isIdentity()) {
2886 restore();
2887 }
2888
Chet Haase48659092012-05-31 15:21:51 -07002889 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002890}
2891
Romain Guy6926c722010-07-12 20:20:03 -07002892///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002893// Shaders
2894///////////////////////////////////////////////////////////////////////////////
2895
2896void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08002897 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002898}
2899
Romain Guy06f96e22010-07-30 19:18:16 -07002900void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08002901 mDrawModifiers.mShader = shader;
2902 if (mDrawModifiers.mShader) {
2903 mDrawModifiers.mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002904 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002905}
2906
Romain Guyd27977d2010-07-14 19:18:51 -07002907///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002908// Color filters
2909///////////////////////////////////////////////////////////////////////////////
2910
2911void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08002912 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07002913}
2914
2915void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08002916 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07002917}
2918
2919///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002920// Drop shadow
2921///////////////////////////////////////////////////////////////////////////////
2922
2923void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08002924 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07002925}
2926
2927void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08002928 mDrawModifiers.mHasShadow = true;
2929 mDrawModifiers.mShadowRadius = radius;
2930 mDrawModifiers.mShadowDx = dx;
2931 mDrawModifiers.mShadowDy = dy;
2932 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07002933}
2934
2935///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002936// Draw filters
2937///////////////////////////////////////////////////////////////////////////////
2938
2939void OpenGLRenderer::resetPaintFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08002940 mDrawModifiers.mHasDrawFilter = false;
Romain Guy5ff9df62012-01-23 17:09:05 -08002941}
2942
2943void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08002944 mDrawModifiers.mHasDrawFilter = true;
2945 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2946 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08002947}
2948
Romain Guy758724f2013-02-27 11:53:12 -08002949SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint, bool alwaysCopy) {
2950 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
2951 if (CC_UNLIKELY(alwaysCopy)) {
2952 mFilteredPaint = *paint;
2953 return &mFilteredPaint;
2954 }
2955 return paint;
2956 }
Romain Guy5ff9df62012-01-23 17:09:05 -08002957
2958 uint32_t flags = paint->getFlags();
2959
2960 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08002961 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
2962 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08002963
2964 return &mFilteredPaint;
2965}
2966
2967///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002968// Drawing implementation
2969///////////////////////////////////////////////////////////////////////////////
2970
Romain Guy01d58e42011-01-19 21:54:02 -08002971void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2972 float x, float y, SkPaint* paint) {
2973 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2974 return;
2975 }
2976
2977 int alpha;
2978 SkXfermode::Mode mode;
2979 getAlphaAndMode(paint, &alpha, &mode);
2980
2981 setupDraw();
2982 setupDrawWithTexture(true);
2983 setupDrawAlpha8Color(paint->getColor(), alpha);
2984 setupDrawColorFilter();
2985 setupDrawShader();
2986 setupDrawBlending(true, mode);
2987 setupDrawProgram();
2988 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2989 setupDrawTexture(texture->id);
2990 setupDrawPureColorUniforms();
2991 setupDrawColorFilterUniforms();
2992 setupDrawShaderUniforms();
2993 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2994
2995 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2996
2997 finishDrawTexture();
2998}
2999
Romain Guyf607bdc2010-09-10 19:20:06 -07003000// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003001#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3002#define kStdUnderline_Offset (1.0f / 9.0f)
3003#define kStdUnderline_Thickness (1.0f / 18.0f)
3004
3005void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3006 float x, float y, SkPaint* paint) {
3007 // Handle underline and strike-through
3008 uint32_t flags = paint->getFlags();
3009 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003010 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003011 float underlineWidth = length;
3012 // If length is > 0.0f, we already measured the text for the text alignment
3013 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003014 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003015 }
3016
Romain Guy211370f2012-02-01 16:10:55 -08003017 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003018 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003019 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003020
Raph Levien8b4072d2012-07-30 15:50:00 -07003021 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003022 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003023
Romain Guyf6834472011-01-23 13:32:12 -08003024 int linesCount = 0;
3025 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3026 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3027
3028 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003029 float points[pointsCount];
3030 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003031
3032 if (flags & SkPaint::kUnderlineText_Flag) {
3033 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003034 points[currentPoint++] = left;
3035 points[currentPoint++] = top;
3036 points[currentPoint++] = left + underlineWidth;
3037 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003038 }
3039
3040 if (flags & SkPaint::kStrikeThruText_Flag) {
3041 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003042 points[currentPoint++] = left;
3043 points[currentPoint++] = top;
3044 points[currentPoint++] = left + underlineWidth;
3045 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003046 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003047
Romain Guy726aeba2011-06-01 14:52:00 -07003048 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003049
Romain Guy726aeba2011-06-01 14:52:00 -07003050 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003051 }
3052 }
3053}
3054
Romain Guy672433d2013-01-04 19:05:13 -08003055status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3056 if (mSnapshot->isIgnored()) {
3057 return DrawGlInfo::kStatusDone;
3058 }
3059
Romain Guy735738c2012-12-03 12:34:51 -08003060 int color = paint->getColor();
3061 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003062 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003063 color |= 0x00ffffff;
3064 }
3065 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3066
3067 return drawColorRects(rects, count, color, mode);
3068}
3069
3070status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003071 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy735738c2012-12-03 12:34:51 -08003072
Romain Guy672433d2013-01-04 19:05:13 -08003073 float left = FLT_MAX;
3074 float top = FLT_MAX;
3075 float right = FLT_MIN;
3076 float bottom = FLT_MIN;
3077
3078 int vertexCount = 0;
3079 Vertex mesh[count * 6];
3080 Vertex* vertex = mesh;
3081
Chris Craik2af46352012-11-26 18:30:17 -08003082 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003083 float l = rects[index + 0];
3084 float t = rects[index + 1];
3085 float r = rects[index + 2];
3086 float b = rects[index + 3];
3087
Romain Guy8ce00302013-01-15 18:51:42 -08003088 if (ignoreTransform || !quickRejectNoScissor(left, top, right, bottom)) {
Romain Guy672433d2013-01-04 19:05:13 -08003089 Vertex::set(vertex++, l, b);
3090 Vertex::set(vertex++, l, t);
3091 Vertex::set(vertex++, r, t);
3092 Vertex::set(vertex++, l, b);
3093 Vertex::set(vertex++, r, t);
3094 Vertex::set(vertex++, r, b);
3095
3096 vertexCount += 6;
3097
3098 left = fminf(left, l);
3099 top = fminf(top, t);
3100 right = fmaxf(right, r);
3101 bottom = fmaxf(bottom, b);
3102 }
3103 }
3104
Romain Guy3bbacf22013-02-06 16:51:04 -08003105 if (count == 0 || (clip && quickReject(left, top, right, bottom))) {
Romain Guya362c692013-02-04 13:50:16 -08003106 return DrawGlInfo::kStatusDone;
3107 }
Romain Guy672433d2013-01-04 19:05:13 -08003108
Romain Guy672433d2013-01-04 19:05:13 -08003109 setupDraw();
3110 setupDrawNoTexture();
3111 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3112 setupDrawShader();
3113 setupDrawColorFilter();
3114 setupDrawBlending(mode);
3115 setupDrawProgram();
3116 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003117 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003118 setupDrawColorUniforms();
3119 setupDrawShaderUniforms();
3120 setupDrawColorFilterUniforms();
3121 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3122
Romain Guy8ce00302013-01-15 18:51:42 -08003123 if (dirty && hasLayer()) {
Romain Guy672433d2013-01-04 19:05:13 -08003124 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
3125 }
3126
3127 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3128
3129 return DrawGlInfo::kStatusDrew;
3130}
3131
Romain Guy026c5e162010-06-28 17:12:22 -07003132void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003133 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003134 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003135 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003136 color |= 0x00ffffff;
3137 }
3138
Romain Guy70ca14e2010-12-13 18:24:33 -08003139 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003140 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003141 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003142 setupDrawShader();
3143 setupDrawColorFilter();
3144 setupDrawBlending(mode);
3145 setupDrawProgram();
3146 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3147 setupDrawColorUniforms();
3148 setupDrawShaderUniforms(ignoreTransform);
3149 setupDrawColorFilterUniforms();
3150 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003151
Romain Guyc95c8d62010-09-17 15:31:32 -07003152 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3153}
3154
Romain Guy82ba8142010-07-09 13:25:56 -07003155void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003156 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003157 int alpha;
3158 SkXfermode::Mode mode;
3159 getAlphaAndMode(paint, &alpha, &mode);
3160
Romain Guyd21b6e12011-11-30 20:21:23 -08003161 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003162
Romain Guy211370f2012-02-01 16:10:55 -08003163 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08003164 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
3165 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
3166
Romain Guyd21b6e12011-11-30 20:21:23 -08003167 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003168 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3169 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3170 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3171 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003172 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003173 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3174 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3175 GL_TRIANGLE_STRIP, gMeshCount);
3176 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003177}
3178
Romain Guybd6b79b2010-06-26 00:13:53 -07003179void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003180 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3181 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003182 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003183}
3184
3185void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003186 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003187 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003188 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003189
Romain Guy746b7402010-10-26 16:27:31 -07003190 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003191 setupDrawWithTexture();
3192 setupDrawColor(alpha, alpha, alpha, alpha);
3193 setupDrawColorFilter();
3194 setupDrawBlending(blend, mode, swapSrcDst);
3195 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003196 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003197 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003198 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003199 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003200 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003201 }
Romain Guy886b2752013-01-04 12:26:18 -08003202 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003203 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003204 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003205 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003206
Romain Guy6820ac82010-09-15 18:11:50 -07003207 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003208
3209 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003210}
3211
Romain Guy886b2752013-01-04 12:26:18 -08003212void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3213 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3214 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3215 bool ignoreTransform, bool dirty) {
3216
3217 setupDraw();
3218 setupDrawWithTexture(true);
3219 if (hasColor) {
3220 setupDrawAlpha8Color(color, alpha);
3221 }
3222 setupDrawColorFilter();
3223 setupDrawShader();
3224 setupDrawBlending(true, mode);
3225 setupDrawProgram();
3226 if (!dirty) setupDrawDirtyRegionsDisabled();
3227 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3228 setupDrawTexture(texture);
3229 setupDrawPureColorUniforms();
3230 setupDrawColorFilterUniforms();
3231 setupDrawShaderUniforms();
3232 setupDrawMesh(vertices, texCoords);
3233
3234 glDrawArrays(drawMode, 0, elementsCount);
3235
3236 finishDrawTexture();
3237}
3238
Romain Guya5aed0d2010-09-09 14:42:43 -07003239void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003240 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003241 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003242
Romain Guy82ba8142010-07-09 13:25:56 -07003243 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003244 // These blend modes are not supported by OpenGL directly and have
3245 // to be implemented using shaders. Since the shader will perform
3246 // the blending, turn blending off here
3247 // If the blend mode cannot be implemented using shaders, fall
3248 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003249 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003250 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003251 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003252 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003253
Romain Guy82bc7a72012-01-03 14:13:39 -08003254 if (mCaches.blend) {
3255 glDisable(GL_BLEND);
3256 mCaches.blend = false;
3257 }
3258
3259 return;
3260 } else {
3261 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003262 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003263 }
3264
3265 if (!mCaches.blend) {
3266 glEnable(GL_BLEND);
3267 }
3268
3269 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3270 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3271
3272 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3273 glBlendFunc(sourceMode, destMode);
3274 mCaches.lastSrcMode = sourceMode;
3275 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003276 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003277 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003278 glDisable(GL_BLEND);
3279 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003280 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003281}
3282
Romain Guy889f8d12010-07-29 14:37:42 -07003283bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003284 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003285 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003286 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003287 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003288 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003289 }
Romain Guy6926c722010-07-12 20:20:03 -07003290 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003291}
3292
Romain Guy026c5e162010-06-28 17:12:22 -07003293void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003294 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003295 TextureVertex::setUV(v++, u1, v1);
3296 TextureVertex::setUV(v++, u2, v1);
3297 TextureVertex::setUV(v++, u1, v2);
3298 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003299}
3300
Chet Haase5c13d892010-10-08 08:37:55 -07003301void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003302 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003303 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003304}
3305
Romain Guy9d5316e2010-06-24 19:30:36 -07003306}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003307}; // namespace android