blob: 27a875c56a3d9d19d31a97d68e06bffe6ec63cea [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 Craikd90144d2013-03-19 15:03:48 -0700115 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;
Romain Guye4d01122010-06-16 18:44:05 -0700125}
126
Romain Guy85bf02f2010-06-22 13:11:24 -0700127OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700128 // The context has already been destroyed at this point, do not call
129 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700130}
131
Romain Guy87e2f7572012-09-24 11:37:12 -0700132void OpenGLRenderer::initProperties() {
133 char property[PROPERTY_VALUE_MAX];
134 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
135 mScissorOptimizationDisabled = !strcasecmp(property, "true");
136 INIT_LOGD(" Scissor optimization %s",
137 mScissorOptimizationDisabled ? "disabled" : "enabled");
138 } else {
139 INIT_LOGD(" Scissor optimization enabled");
140 }
Romain Guy13631f32012-01-30 17:41:55 -0800141}
142
143///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700144// Setup
145///////////////////////////////////////////////////////////////////////////////
146
Romain Guyef359272013-01-31 19:07:29 -0800147void OpenGLRenderer::setName(const char* name) {
148 if (name) {
149 mName.setTo(name);
150 } else {
151 mName.clear();
152 }
153}
154
155const char* OpenGLRenderer::getName() const {
156 return mName.string();
157}
158
Romain Guy49c5fc02012-05-15 11:10:01 -0700159bool OpenGLRenderer::isDeferred() {
160 return false;
161}
162
Romain Guy85bf02f2010-06-22 13:11:24 -0700163void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700164 initViewport(width, height);
165
166 glDisable(GL_DITHER);
167 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
168
169 glEnableVertexAttribArray(Program::kBindingPosition);
170}
171
172void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700173 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700174
175 mWidth = width;
176 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700177
178 mFirstSnapshot->height = height;
179 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700180}
181
Romain Guy7c25aab2012-10-18 15:05:02 -0700182status_t OpenGLRenderer::prepare(bool opaque) {
Chet Haase44b2fe32012-06-06 19:03:58 -0700183 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800184}
185
Romain Guyc3fedaf2013-01-29 17:26:25 -0800186status_t OpenGLRenderer::prepareDirty(float left, float top,
187 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800188 mCaches.clearGarbage();
189
Romain Guy8aef54f2010-09-01 15:13:49 -0700190 mSnapshot = new Snapshot(mFirstSnapshot,
191 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800192 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700193 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700194
Romain Guy7d7b5492011-01-24 16:33:45 -0800195 mSnapshot->setClip(left, top, right, bottom);
Chris Craik5f803622013-03-21 14:39:04 -0700196 mTilingClip.set(left, top, right, bottom);
Romain Guy41308e22012-10-22 20:02:43 -0700197 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700198
Romain Guy11cb6422012-09-21 00:39:43 -0700199 updateLayers();
200
Romain Guydcfc8362013-01-03 13:08:57 -0800201 discardFramebuffer(left, top, right, bottom);
Romain Guy45e4c3d2012-09-11 17:17:07 -0700202
Romain Guyddf74372012-05-22 14:07:07 -0700203 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800204
Romain Guy54c1a642012-09-27 17:55:46 -0700205 // Functors break the tiling extension in pretty spectacular ways
206 // This ensures we don't use tiling when a functor is going to be
207 // invoked during the frame
208 mSuppressTiling = mCaches.hasRegisteredFunctors();
209
Chris Craik5f803622013-03-21 14:39:04 -0700210 startTiling(mSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700211
Romain Guy7c450aa2012-09-21 19:15:00 -0700212 debugOverdraw(true, true);
213
Romain Guy7c25aab2012-10-18 15:05:02 -0700214 return clear(left, top, right, bottom, opaque);
215}
216
Romain Guydcfc8362013-01-03 13:08:57 -0800217void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
218 // If we know that we are going to redraw the entire framebuffer,
219 // perform a discard to let the driver know we don't need to preserve
220 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800221 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800222 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800223 const bool isFbo = getTargetFbo() == 0;
224 const GLenum attachments[] = {
225 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
226 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800227 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
228 }
229}
230
Romain Guy7c25aab2012-10-18 15:05:02 -0700231status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700232 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700233 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700234 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700235 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700236 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700237 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700238
Romain Guy7c25aab2012-10-18 15:05:02 -0700239 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700240 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700241}
242
243void OpenGLRenderer::syncState() {
244 glViewport(0, 0, mWidth, mHeight);
245
246 if (mCaches.blend) {
247 glEnable(GL_BLEND);
248 } else {
249 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700250 }
Romain Guybb9524b2010-06-22 18:56:38 -0700251}
252
Romain Guy57b52682012-09-20 17:38:46 -0700253void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700254 if (!mSuppressTiling) {
Chris Craik5f803622013-03-21 14:39:04 -0700255 Rect* clip = &mTilingClip;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800256 if (s->flags & Snapshot::kFlagFboTarget) {
Chris Craik5f803622013-03-21 14:39:04 -0700257 clip = &(s->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700258 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700259
Romain Guyc3fedaf2013-01-29 17:26:25 -0800260 startTiling(*clip, s->height, opaque);
261 }
262}
263
264void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
265 if (!mSuppressTiling) {
266 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800267 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700268 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700269}
270
271void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700272 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700273}
274
Romain Guyb025b9c2010-09-16 14:16:48 -0700275void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700276 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700277 endTiling();
278
Romain Guyca89e2a2013-03-08 17:44:20 -0800279 // When finish() is invoked on FBO 0 we've reached the end
280 // of the current frame
281 if (getTargetFbo() == 0) {
282 mCaches.pathCache.trim();
283 }
284
Romain Guy11cb6422012-09-21 00:39:43 -0700285 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700286#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700287 GLenum status = GL_NO_ERROR;
288 while ((status = glGetError()) != GL_NO_ERROR) {
289 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
290 switch (status) {
291 case GL_INVALID_ENUM:
292 ALOGE(" GL_INVALID_ENUM");
293 break;
294 case GL_INVALID_VALUE:
295 ALOGE(" GL_INVALID_VALUE");
296 break;
297 case GL_INVALID_OPERATION:
298 ALOGE(" GL_INVALID_OPERATION");
299 break;
300 case GL_OUT_OF_MEMORY:
301 ALOGE(" Out of memory!");
302 break;
303 }
Romain Guya07105b2011-01-10 21:14:18 -0800304 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700305#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700306
Romain Guyc15008e2010-11-10 11:59:15 -0800307#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800308 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700309#else
310 if (mCaches.getDebugLevel() & kDebugMemory) {
311 mCaches.dumpMemoryUsage();
312 }
Romain Guyc15008e2010-11-10 11:59:15 -0800313#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700314 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700315}
316
Romain Guy6c319ca2011-01-11 14:29:25 -0800317void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700318 if (mCaches.currentProgram) {
319 if (mCaches.currentProgram->isInUse()) {
320 mCaches.currentProgram->remove();
321 mCaches.currentProgram = NULL;
322 }
323 }
Romain Guy50c0f092010-10-19 11:42:22 -0700324 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800325 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800326 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800327 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700328 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700329}
330
Romain Guy6c319ca2011-01-11 14:29:25 -0800331void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800332 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800333 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700334 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700335 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700336
Romain Guy3e263fa2011-12-12 16:47:48 -0800337 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
338
Chet Haase80250612012-08-15 13:46:54 -0700339 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700340 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800341 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700342 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700343
Romain Guya1d3c912011-12-13 14:55:06 -0800344 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700345
Romain Guy50c0f092010-10-19 11:42:22 -0700346 mCaches.blend = true;
347 glEnable(GL_BLEND);
348 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
349 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700350}
351
Romain Guy35643dd2012-09-18 15:40:58 -0700352void OpenGLRenderer::resumeAfterLayer() {
353 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
354 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
355 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700356 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700357
358 mCaches.resetScissor();
359 dirtyClip();
360}
361
Romain Guyba6be8a2012-04-23 18:22:09 -0700362void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700363 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700364}
365
366void OpenGLRenderer::attachFunctor(Functor* functor) {
367 mFunctors.add(functor);
368}
369
Romain Guy8f3b8e32012-03-27 16:33:45 -0700370status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
371 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700372 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700373
Romain Guyba6be8a2012-04-23 18:22:09 -0700374 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800375 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700376 SortedVector<Functor*> functors(mFunctors);
377 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700378
Romain Guyba6be8a2012-04-23 18:22:09 -0700379 DrawGlInfo info;
380 info.clipLeft = 0;
381 info.clipTop = 0;
382 info.clipRight = 0;
383 info.clipBottom = 0;
384 info.isLayer = false;
385 info.width = 0;
386 info.height = 0;
387 memset(info.transform, 0, sizeof(float) * 16);
388
389 for (size_t i = 0; i < count; i++) {
390 Functor* f = functors.itemAt(i);
391 result |= (*f)(DrawGlInfo::kModeProcess, &info);
392
Chris Craikc2c95432012-04-25 15:13:52 -0700393 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700394 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
395 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700396 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700397
Chris Craikc2c95432012-04-25 15:13:52 -0700398 if (result & DrawGlInfo::kStatusInvoke) {
399 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700400 }
401 }
Chris Craikd15321b2012-11-28 14:45:04 -0800402 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700403 }
404
405 return result;
406}
407
408status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700409 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
410
Chet Haasedaf98e92011-01-10 14:10:36 -0800411 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700412 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700413
Romain Guy8a4ac612012-07-17 17:32:48 -0700414 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800415 if (mDirtyClip) {
416 setScissorFromClip();
417 }
Romain Guyd643bb52011-03-01 14:55:21 -0800418
Romain Guy80911b82011-03-16 15:30:12 -0700419 Rect clip(*mSnapshot->clipRect);
420 clip.snapToPixelBoundaries();
421
Romain Guyd643bb52011-03-01 14:55:21 -0800422 // Since we don't know what the functor will draw, let's dirty
423 // tne entire clip region
424 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800425 dirtyLayerUnchecked(clip, getRegion());
426 }
Romain Guyd643bb52011-03-01 14:55:21 -0800427
Romain Guy08aa2cb2011-03-17 11:06:57 -0700428 DrawGlInfo info;
429 info.clipLeft = clip.left;
430 info.clipTop = clip.top;
431 info.clipRight = clip.right;
432 info.clipBottom = clip.bottom;
433 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700434 info.width = getSnapshot()->viewport.getWidth();
435 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700436 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700437
Chet Haase48659092012-05-31 15:21:51 -0700438 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800439
Romain Guy8f3b8e32012-03-27 16:33:45 -0700440 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700441 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800442 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700443
Chris Craik65924a32012-04-05 17:52:11 -0700444 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700445 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700446 }
Romain Guycabfcc12011-03-07 18:06:46 -0800447 }
448
Chet Haasedaf98e92011-01-10 14:10:36 -0800449 resume();
Romain Guy65549432012-03-26 16:45:05 -0700450 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800451}
452
Romain Guyf6a11b82010-06-23 17:47:49 -0700453///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700454// Debug
455///////////////////////////////////////////////////////////////////////////////
456
Romain Guy0f667532013-03-01 14:31:04 -0800457void OpenGLRenderer::eventMark(const char* name) const {
458 mCaches.eventMark(0, name);
459}
460
Romain Guy87e2f7572012-09-24 11:37:12 -0700461void OpenGLRenderer::startMark(const char* name) const {
462 mCaches.startMark(0, name);
463}
464
465void OpenGLRenderer::endMark() const {
466 mCaches.endMark();
467}
468
469void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
470 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
471 if (clear) {
472 mCaches.disableScissor();
473 mCaches.stencil.clear();
474 }
475 if (enable) {
476 mCaches.stencil.enableDebugWrite();
477 } else {
478 mCaches.stencil.disable();
479 }
480 }
481}
482
483void OpenGLRenderer::renderOverdraw() {
484 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700485 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700486
487 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700488 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700489 clip->right - clip->left, clip->bottom - clip->top);
490
491 mCaches.stencil.enableDebugTest(2);
492 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
493 mCaches.stencil.enableDebugTest(3);
494 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
495 mCaches.stencil.enableDebugTest(4);
496 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
497 mCaches.stencil.enableDebugTest(4, true);
498 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
499 mCaches.stencil.disable();
500 }
501}
502
503///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700504// Layers
505///////////////////////////////////////////////////////////////////////////////
506
507bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
508 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
509 OpenGLRenderer* renderer = layer->renderer;
510 Rect& dirty = layer->dirtyRect;
511
Romain Guy7c450aa2012-09-21 19:15:00 -0700512 if (inFrame) {
513 endTiling();
514 debugOverdraw(false, false);
515 }
Romain Guy11cb6422012-09-21 00:39:43 -0700516
517 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
518 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
519 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
520 renderer->finish();
521
522 if (inFrame) {
523 resumeAfterLayer();
524 startTiling(mSnapshot);
525 }
526
527 dirty.setEmpty();
528 layer->deferredUpdateScheduled = false;
529 layer->renderer = NULL;
530 layer->displayList = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -0800531 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700532
533 return true;
534 }
535
536 return false;
537}
538
539void OpenGLRenderer::updateLayers() {
540 int count = mLayerUpdates.size();
541 if (count > 0) {
542 startMark("Layer Updates");
543
544 // Note: it is very important to update the layers in reverse order
545 for (int i = count - 1; i >= 0; i--) {
546 Layer* layer = mLayerUpdates.itemAt(i);
547 updateLayer(layer, false);
548 mCaches.resourceCache.decrementRefcount(layer);
549 }
550 mLayerUpdates.clear();
551
552 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
553 endMark();
554 }
555}
556
557void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
558 if (layer) {
559 mLayerUpdates.push_back(layer);
560 mCaches.resourceCache.incrementRefcount(layer);
561 }
562}
563
564void OpenGLRenderer::clearLayerUpdates() {
565 size_t count = mLayerUpdates.size();
566 if (count > 0) {
567 mCaches.resourceCache.lock();
568 for (size_t i = 0; i < count; i++) {
569 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
570 }
571 mCaches.resourceCache.unlock();
572 mLayerUpdates.clear();
573 }
574}
575
576///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700577// State management
578///////////////////////////////////////////////////////////////////////////////
579
Romain Guybb9524b2010-06-22 18:56:38 -0700580int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700581 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700582}
583
584int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700585 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700586}
587
588void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700589 if (mSaveCount > 1) {
590 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700591 }
Romain Guybb9524b2010-06-22 18:56:38 -0700592}
593
594void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700595 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700596
Romain Guy8fb95422010-08-17 18:38:51 -0700597 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700598 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700599 }
Romain Guybb9524b2010-06-22 18:56:38 -0700600}
601
Romain Guy8aef54f2010-09-01 15:13:49 -0700602int OpenGLRenderer::saveSnapshot(int flags) {
603 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700604 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700605}
606
607bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700608 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700609 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700610 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700611
Romain Guybd6b79b2010-06-26 00:13:53 -0700612 sp<Snapshot> current = mSnapshot;
613 sp<Snapshot> previous = mSnapshot->previous;
614
Romain Guyeb993562010-10-05 18:14:38 -0700615 if (restoreOrtho) {
616 Rect& r = previous->viewport;
617 glViewport(r.left, r.top, r.right, r.bottom);
618 mOrthoMatrix.load(current->orthoMatrix);
619 }
620
Romain Guy8b55f372010-08-18 17:10:07 -0700621 mSaveCount--;
622 mSnapshot = previous;
623
Romain Guy2542d192010-08-18 11:47:12 -0700624 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700625 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700626 }
Romain Guy2542d192010-08-18 11:47:12 -0700627
Romain Guy5ec99242010-11-03 16:19:08 -0700628 if (restoreLayer) {
629 composeLayer(current, previous);
630 }
631
Romain Guy2542d192010-08-18 11:47:12 -0700632 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700633}
634
Romain Guyf6a11b82010-06-23 17:47:49 -0700635///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700636// Layers
637///////////////////////////////////////////////////////////////////////////////
638
639int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800640 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700641 const GLuint previousFbo = mSnapshot->fbo;
642 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700643
Romain Guyaf636eb2010-12-09 17:47:21 -0800644 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700645 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700646 }
Romain Guyd55a8612010-06-28 17:42:46 -0700647
648 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700649}
650
Chris Craikd90144d2013-03-19 15:03:48 -0700651void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
652 const Rect untransformedBounds(bounds);
653
654 currentTransform().mapRect(bounds);
655
656 // Layers only make sense if they are in the framebuffer's bounds
657 if (bounds.intersect(*mSnapshot->clipRect)) {
658 // We cannot work with sub-pixels in this case
659 bounds.snapToPixelBoundaries();
660
661 // When the layer is not an FBO, we may use glCopyTexImage so we
662 // need to make sure the layer does not extend outside the bounds
663 // of the framebuffer
664 if (!bounds.intersect(mSnapshot->previous->viewport)) {
665 bounds.setEmpty();
666 } else if (fboLayer) {
667 clip.set(bounds);
668 mat4 inverse;
669 inverse.loadInverse(currentTransform());
670 inverse.mapRect(clip);
671 clip.snapToPixelBoundaries();
672 if (clip.intersect(untransformedBounds)) {
673 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
674 bounds.set(untransformedBounds);
675 } else {
676 clip.setEmpty();
677 }
678 }
679 } else {
680 bounds.setEmpty();
681 }
682}
683
Chris Craik408eb122013-03-26 18:55:15 -0700684void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
685 bool fboLayer, int alpha) {
686 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
687 bounds.getHeight() > mCaches.maxTextureSize ||
688 (fboLayer && clip.isEmpty())) {
689 mSnapshot->empty = fboLayer;
690 } else {
691 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
692 }
693}
694
Chris Craikd90144d2013-03-19 15:03:48 -0700695int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
696 int alpha, SkXfermode::Mode mode, int flags) {
697 const GLuint previousFbo = mSnapshot->fbo;
698 const int count = saveSnapshot(flags);
699
700 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
701 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
702 // operations will be able to store and restore the current clip and transform info, and
703 // quick rejection will be correct (for display lists)
704
705 Rect bounds(left, top, right, bottom);
706 Rect clip;
707 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700708 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700709
Chris Craik408eb122013-03-26 18:55:15 -0700710 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700711 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
712 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
713 }
714 }
715
716 return count;
717}
718
719
Romain Guy1c740bc2010-09-13 18:00:09 -0700720/**
721 * Layers are viewed by Skia are slightly different than layers in image editing
722 * programs (for instance.) When a layer is created, previously created layers
723 * and the frame buffer still receive every drawing command. For instance, if a
724 * layer is created and a shape intersecting the bounds of the layers and the
725 * framebuffer is draw, the shape will be drawn on both (unless the layer was
726 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
727 *
728 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
729 * texture. Unfortunately, this is inefficient as it requires every primitive to
730 * be drawn n + 1 times, where n is the number of active layers. In practice this
731 * means, for every primitive:
732 * - Switch active frame buffer
733 * - Change viewport, clip and projection matrix
734 * - Issue the drawing
735 *
736 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700737 * To avoid this, layers are implemented in a different way here, at least in the
738 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
739 * is set. When this flag is set we can redirect all drawing operations into a
740 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700741 *
742 * This implementation relies on the frame buffer being at least RGBA 8888. When
743 * a layer is created, only a texture is created, not an FBO. The content of the
744 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700745 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700746 * buffer and drawing continues as normal. This technique therefore treats the
747 * frame buffer as a scratch buffer for the layers.
748 *
749 * To compose the layers back onto the frame buffer, each layer texture
750 * (containing the original frame buffer data) is drawn as a simple quad over
751 * the frame buffer. The trick is that the quad is set as the composition
752 * destination in the blending equation, and the frame buffer becomes the source
753 * of the composition.
754 *
755 * Drawing layers with an alpha value requires an extra step before composition.
756 * An empty quad is drawn over the layer's region in the frame buffer. This quad
757 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
758 * quad is used to multiply the colors in the frame buffer. This is achieved by
759 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
760 * GL_ZERO, GL_SRC_ALPHA.
761 *
762 * Because glCopyTexImage2D() can be slow, an alternative implementation might
763 * be use to draw a single clipped layer. The implementation described above
764 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700765 *
766 * (1) The frame buffer is actually not cleared right away. To allow the GPU
767 * to potentially optimize series of calls to glCopyTexImage2D, the frame
768 * buffer is left untouched until the first drawing operation. Only when
769 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700770 */
Chet Haased48885a2012-08-28 17:43:28 -0700771bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
772 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700773 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700774 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700775
Romain Guyeb993562010-10-05 18:14:38 -0700776 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
777
Romain Guyf607bdc2010-09-10 19:20:06 -0700778 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700779 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700780 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700781 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700782 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700783
784 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700785 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700786 return false;
787 }
Romain Guyf18fd992010-07-08 11:45:51 -0700788
Romain Guya1d3c912011-12-13 14:55:06 -0800789 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700790 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700791 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700792 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700793 }
794
Romain Guy9ace8f52011-07-07 20:50:11 -0700795 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700796 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700797 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
798 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800799 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700800 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700801 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700802
Romain Guy8fb95422010-08-17 18:38:51 -0700803 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700804 mSnapshot->flags |= Snapshot::kFlagIsLayer;
805 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700806
Romain Guyeb993562010-10-05 18:14:38 -0700807 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700808 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700809 } else {
810 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700811 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800812 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700813 if (layer->isEmpty()) {
814 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700815 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700816 layer->getWidth(), layer->getHeight(), 0);
817 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800818 } else {
819 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700820 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800821 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700822
Romain Guy54be1cd2011-06-13 19:04:27 -0700823 // Enqueue the buffer coordinates to clear the corresponding region later
824 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700825 }
Romain Guyeb993562010-10-05 18:14:38 -0700826 }
Romain Guyf86ef572010-07-01 11:05:42 -0700827
Romain Guyd55a8612010-06-28 17:42:46 -0700828 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700829}
830
Chet Haased48885a2012-08-28 17:43:28 -0700831bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800832 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700833 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700834
Chet Haased48885a2012-08-28 17:43:28 -0700835 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800836 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
837 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700838 mSnapshot->fbo = layer->getFbo();
839 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
840 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
841 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
842 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700843 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700844
Romain Guy2b7028e2012-09-19 17:25:38 -0700845 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700846 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700847 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700848 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
849 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700850
851 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700852 if (layer->isEmpty()) {
853 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
854 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700855 }
856
857 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700858 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700859
Romain Guyf735c8e2013-01-31 17:45:55 -0800860 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700861
862 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700863 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800864 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700865 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700866 glClear(GL_COLOR_BUFFER_BIT);
867
868 dirtyClip();
869
870 // Change the ortho projection
871 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
872 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
873
874 return true;
875}
876
Romain Guy1c740bc2010-09-13 18:00:09 -0700877/**
878 * Read the documentation of createLayer() before doing anything in this method.
879 */
Romain Guy1d83e192010-08-17 11:37:00 -0700880void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
881 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000882 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700883 return;
884 }
885
Romain Guy8ce00302013-01-15 18:51:42 -0800886 Layer* layer = current->layer;
887 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700888 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700889
890 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700891 endTiling();
892
Romain Guye0aa84b2012-04-03 19:30:26 -0700893 // Detach the texture from the FBO
894 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800895
896 layer->removeFbo(false);
897
Romain Guyeb993562010-10-05 18:14:38 -0700898 // Unbind current FBO and restore previous one
899 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700900 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700901
902 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700903 }
904
Romain Guy9ace8f52011-07-07 20:50:11 -0700905 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700906 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700907 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700908 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700909 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700910 }
911
Romain Guy03750a02010-10-18 14:06:08 -0700912 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700913
Romain Guya1d3c912011-12-13 14:55:06 -0800914 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700915
Romain Guy5b3b3522010-10-27 18:57:51 -0700916 // When the layer is stored in an FBO, we can save a bit of fillrate by
917 // drawing only the dirty region
918 if (fboLayer) {
919 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700920 if (layer->getColorFilter()) {
921 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800922 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700923 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700924 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800925 resetColorFilter();
926 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700927 } else if (!rect.isEmpty()) {
928 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
929 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700930 }
Romain Guy8b55f372010-08-18 17:10:07 -0700931
Romain Guy746b7402010-10-26 16:27:31 -0700932 dirtyClip();
933
Romain Guyeb993562010-10-05 18:14:38 -0700934 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700935 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700936 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700937 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700938 }
939}
940
Romain Guyaa6c24c2011-04-28 18:40:04 -0700941void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craike83569c2013-03-20 16:57:09 -0700942 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700943
944 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700945 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700946 setupDrawWithTexture();
947 } else {
948 setupDrawWithExternalTexture();
949 }
950 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700951 setupDrawColor(alpha, alpha, alpha, alpha);
952 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700953 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700954 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700955 setupDrawPureColorUniforms();
956 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700957 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
958 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700959 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700960 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700961 }
Romain Guy3b753822013-03-05 10:27:35 -0800962 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700963 layer->getWidth() == (uint32_t) rect.getWidth() &&
964 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -0800965 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
966 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700967
Romain Guyd21b6e12011-11-30 20:21:23 -0800968 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700969 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
970 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800971 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700972 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
973 }
974 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700975 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
976
977 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
978
979 finishDrawTexture();
980}
981
Romain Guy5b3b3522010-10-27 18:57:51 -0700982void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700983 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700984 const Rect& texCoords = layer->texCoords;
985 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
986 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700987
Romain Guy9ace8f52011-07-07 20:50:11 -0700988 float x = rect.left;
989 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -0800990 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700991 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700992 layer->getHeight() == (uint32_t) rect.getHeight();
993
994 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700995 // When we're swapping, the layer is already in screen coordinates
996 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -0800997 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
998 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700999 }
1000
Romain Guyd21b6e12011-11-30 20:21:23 -08001001 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001002 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001003 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001004 }
1005
Chris Craike83569c2013-03-20 16:57:09 -07001006 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
1007 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001008 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001009 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001010 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1011 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001012
Romain Guyaa6c24c2011-04-28 18:40:04 -07001013 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1014 } else {
1015 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1016 drawTextureLayer(layer, rect);
1017 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1018 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001019}
1020
1021void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001022 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001023 layer->setRegionAsRect();
1024
Romain Guy40667672011-03-18 14:34:03 -07001025 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -07001026
Romain Guy5b3b3522010-10-27 18:57:51 -07001027 layer->region.clear();
1028 return;
1029 }
1030
Romain Guy8a3957d2011-09-07 17:55:15 -07001031 // TODO: See LayerRenderer.cpp::generateMesh() for important
1032 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -08001033 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001034 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001035 const android::Rect* rects;
1036 Region safeRegion;
1037 if (CC_LIKELY(hasRectToRectTransform())) {
1038 rects = layer->region.getArray(&count);
1039 } else {
1040 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1041 rects = safeRegion.getArray(&count);
1042 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001043
Chris Craike83569c2013-03-20 16:57:09 -07001044 const float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guy9ace8f52011-07-07 20:50:11 -07001045 const float texX = 1.0f / float(layer->getWidth());
1046 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001047 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001048
Romain Guy8ce00302013-01-15 18:51:42 -08001049 setupDraw();
1050
1051 // We must get (and therefore bind) the region mesh buffer
1052 // after we setup drawing in case we need to mess with the
1053 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001054 TextureVertex* mesh = mCaches.getRegionMesh();
1055 GLsizei numQuads = 0;
1056
Romain Guy7230a742011-01-10 22:26:16 -08001057 setupDrawWithTexture();
1058 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001059 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001060 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001061 setupDrawProgram();
1062 setupDrawDirtyRegionsDisabled();
1063 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001064 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001065 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001066 if (currentTransform().isPureTranslate()) {
1067 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1068 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001069
Romain Guyd21b6e12011-11-30 20:21:23 -08001070 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001071 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1072 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001073 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001074 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1075 }
Romain Guy15bc6432011-12-13 13:11:32 -08001076 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001077
1078 for (size_t i = 0; i < count; i++) {
1079 const android::Rect* r = &rects[i];
1080
1081 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001082 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001083 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001084 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001085
1086 // TODO: Reject quads outside of the clip
1087 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1088 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1089 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1090 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1091
1092 numQuads++;
1093
1094 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1095 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1096 numQuads = 0;
1097 mesh = mCaches.getRegionMesh();
1098 }
1099 }
1100
1101 if (numQuads > 0) {
1102 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1103 }
1104
Romain Guy7230a742011-01-10 22:26:16 -08001105 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001106
1107#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001108 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001109#endif
1110
1111 layer->region.clear();
1112 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001113}
1114
Romain Guy3a3133d2011-02-01 22:59:58 -08001115void OpenGLRenderer::drawRegionRects(const Region& region) {
1116#if DEBUG_LAYERS_AS_REGIONS
1117 size_t count;
1118 const android::Rect* rects = region.getArray(&count);
1119
1120 uint32_t colors[] = {
1121 0x7fff0000, 0x7f00ff00,
1122 0x7f0000ff, 0x7fff00ff,
1123 };
1124
1125 int offset = 0;
1126 int32_t top = rects[0].top;
1127
1128 for (size_t i = 0; i < count; i++) {
1129 if (top != rects[i].top) {
1130 offset ^= 0x2;
1131 top = rects[i].top;
1132 }
1133
1134 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1135 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1136 SkXfermode::kSrcOver_Mode);
1137 }
1138#endif
1139}
1140
Romain Guy8ce00302013-01-15 18:51:42 -08001141void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1142 SkXfermode::Mode mode, bool dirty) {
1143 int count = 0;
1144 Vector<float> rects;
1145
1146 SkRegion::Iterator it(region);
1147 while (!it.done()) {
1148 const SkIRect& r = it.rect();
1149 rects.push(r.fLeft);
1150 rects.push(r.fTop);
1151 rects.push(r.fRight);
1152 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001153 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001154 it.next();
1155 }
1156
Romain Guy3bbacf22013-02-06 16:51:04 -08001157 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001158}
1159
Romain Guy5b3b3522010-10-27 18:57:51 -07001160void OpenGLRenderer::dirtyLayer(const float left, const float top,
1161 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001162 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001163 Rect bounds(left, top, right, bottom);
1164 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001165 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001166 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001167}
1168
1169void OpenGLRenderer::dirtyLayer(const float left, const float top,
1170 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001171 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001172 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001173 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001174 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001175}
1176
1177void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001178 if (bounds.intersect(*mSnapshot->clipRect)) {
1179 bounds.snapToPixelBoundaries();
1180 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1181 if (!dirty.isEmpty()) {
1182 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001183 }
1184 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001185}
1186
Romain Guy54be1cd2011-06-13 19:04:27 -07001187void OpenGLRenderer::clearLayerRegions() {
1188 const size_t count = mLayers.size();
1189 if (count == 0) return;
1190
1191 if (!mSnapshot->isIgnored()) {
1192 // Doing several glScissor/glClear here can negatively impact
1193 // GPUs with a tiler architecture, instead we draw quads with
1194 // the Clear blending mode
1195
1196 // The list contains bounds that have already been clipped
1197 // against their initial clip rect, and the current clip
1198 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001199 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001200
1201 Vertex mesh[count * 6];
1202 Vertex* vertex = mesh;
1203
1204 for (uint32_t i = 0; i < count; i++) {
1205 Rect* bounds = mLayers.itemAt(i);
1206
1207 Vertex::set(vertex++, bounds->left, bounds->bottom);
1208 Vertex::set(vertex++, bounds->left, bounds->top);
1209 Vertex::set(vertex++, bounds->right, bounds->top);
1210 Vertex::set(vertex++, bounds->left, bounds->bottom);
1211 Vertex::set(vertex++, bounds->right, bounds->top);
1212 Vertex::set(vertex++, bounds->right, bounds->bottom);
1213
1214 delete bounds;
1215 }
Romain Guye67307c2013-02-11 18:01:20 -08001216 // We must clear the list of dirty rects before we
1217 // call setupDraw() to prevent stencil setup to do
1218 // the same thing again
1219 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001220
1221 setupDraw(false);
1222 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1223 setupDrawBlending(true, SkXfermode::kClear_Mode);
1224 setupDrawProgram();
1225 setupDrawPureColorUniforms();
1226 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001227 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001228
Romain Guy54be1cd2011-06-13 19:04:27 -07001229 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001230
1231 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001232 } else {
1233 for (uint32_t i = 0; i < count; i++) {
1234 delete mLayers.itemAt(i);
1235 }
Romain Guye67307c2013-02-11 18:01:20 -08001236 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001237 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001238}
1239
Romain Guybd6b79b2010-06-26 00:13:53 -07001240///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001241// State Deferral
1242///////////////////////////////////////////////////////////////////////////////
1243
Chris Craikff785832013-03-08 13:12:16 -08001244bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001245 const Rect& currentClip = *(mSnapshot->clipRect);
1246 const mat4& currentMatrix = *(mSnapshot->transform);
1247
Chris Craikff785832013-03-08 13:12:16 -08001248 if (stateDeferFlags & kStateDeferFlag_Draw) {
1249 // state has bounds initialized in local coordinates
1250 if (!state.mBounds.isEmpty()) {
1251 currentMatrix.mapRect(state.mBounds);
1252 if (!state.mBounds.intersect(currentClip)) {
1253 // quick rejected
1254 return true;
1255 }
1256 } else {
1257 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001258 }
Chris Craikff785832013-03-08 13:12:16 -08001259 state.mDrawModifiers = mDrawModifiers;
1260 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001261 }
1262
Chris Craikff785832013-03-08 13:12:16 -08001263 if (stateDeferFlags & kStateDeferFlag_Clip) {
1264 state.mClip.set(currentClip);
1265 } else {
1266 state.mClip.setEmpty();
1267 }
1268
1269 // transform always deferred
Chris Craikc3566d02013-02-04 16:16:33 -08001270 state.mMatrix.load(currentMatrix);
Chris Craikc3566d02013-02-04 16:16:33 -08001271 return false;
1272}
1273
Chris Craikff785832013-03-08 13:12:16 -08001274void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, int stateDeferFlags) {
Romain Guy3b753822013-03-05 10:27:35 -08001275 currentTransform().load(state.mMatrix);
Chris Craikc3566d02013-02-04 16:16:33 -08001276
Chris Craikff785832013-03-08 13:12:16 -08001277 if (stateDeferFlags & kStateDeferFlag_Draw) {
1278 mDrawModifiers = state.mDrawModifiers;
1279 mSnapshot->alpha = state.mAlpha;
1280 }
1281
Chris Craikd90144d2013-03-19 15:03:48 -07001282 if (!state.mClip.isEmpty()) {
Chris Craikff785832013-03-08 13:12:16 -08001283 mSnapshot->setClip(state.mClip.left, state.mClip.top, state.mClip.right, state.mClip.bottom);
1284 dirtyClip();
1285 }
Chris Craikc3566d02013-02-04 16:16:33 -08001286}
1287
1288///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001289// Transforms
1290///////////////////////////////////////////////////////////////////////////////
1291
1292void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy3b753822013-03-05 10:27:35 -08001293 currentTransform().translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001294}
1295
1296void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001297 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001298}
1299
1300void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001301 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001302}
1303
Romain Guy807daf72011-01-18 11:19:19 -08001304void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001305 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001306}
1307
Romain Guyf6a11b82010-06-23 17:47:49 -07001308void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001309 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001310 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001311 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001312 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001313 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001314}
1315
Chris Craikb98a0162013-02-21 11:30:22 -08001316bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001317 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001318}
1319
Romain Guyf6a11b82010-06-23 17:47:49 -07001320void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001321 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001322}
1323
1324void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001325 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001326 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001327 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001328 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001329}
1330
1331///////////////////////////////////////////////////////////////////////////////
1332// Clipping
1333///////////////////////////////////////////////////////////////////////////////
1334
Romain Guybb9524b2010-06-22 18:56:38 -07001335void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001336 Rect clip(*mSnapshot->clipRect);
1337 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001338
Romain Guy8a4ac612012-07-17 17:32:48 -07001339 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1340 clip.getWidth(), clip.getHeight())) {
1341 mDirtyClip = false;
1342 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001343}
1344
Romain Guy8ce00302013-01-15 18:51:42 -08001345void OpenGLRenderer::ensureStencilBuffer() {
1346 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1347 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1348 // just hope we have one when hasLayer() returns false.
1349 if (hasLayer()) {
1350 attachStencilBufferToLayer(mSnapshot->layer);
1351 }
1352}
1353
1354void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1355 // The layer's FBO is already bound when we reach this stage
1356 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001357 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1358 // is attached after we initiated tiling. We must turn it off,
1359 // attach the new render buffer then turn tiling back on
1360 endTiling();
1361
Romain Guy8d4aeb72013-02-12 16:08:55 -08001362 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001363 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001364 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001365
Romain Guyf735c8e2013-01-31 17:45:55 -08001366 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001367 }
1368}
1369
1370void OpenGLRenderer::setStencilFromClip() {
1371 if (!mCaches.debugOverdraw) {
1372 if (!mSnapshot->clipRegion->isEmpty()) {
1373 // NOTE: The order here is important, we must set dirtyClip to false
1374 // before any draw call to avoid calling back into this method
1375 mDirtyClip = false;
1376
1377 ensureStencilBuffer();
1378
1379 mCaches.stencil.enableWrite();
1380
1381 // Clear the stencil but first make sure we restrict drawing
1382 // to the region's bounds
1383 bool resetScissor = mCaches.enableScissor();
1384 if (resetScissor) {
1385 // The scissor was not set so we now need to update it
1386 setScissorFromClip();
1387 }
1388 mCaches.stencil.clear();
1389 if (resetScissor) mCaches.disableScissor();
1390
1391 // NOTE: We could use the region contour path to generate a smaller mesh
1392 // Since we are using the stencil we could use the red book path
1393 // drawing technique. It might increase bandwidth usage though.
1394
1395 // The last parameter is important: we are not drawing in the color buffer
1396 // so we don't want to dirty the current layer, if any
1397 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1398
1399 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001400
1401 // Draw the region used to generate the stencil if the appropriate debug
1402 // mode is enabled
1403 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1404 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1405 }
Romain Guy8ce00302013-01-15 18:51:42 -08001406 } else {
1407 mCaches.stencil.disable();
1408 }
1409 }
1410}
1411
Romain Guy9d5316e2010-06-24 19:30:36 -07001412const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001413 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001414}
1415
Romain Guy8a4ac612012-07-17 17:32:48 -07001416bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1417 if (mSnapshot->isIgnored()) {
1418 return true;
1419 }
1420
1421 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001422 currentTransform().mapRect(r);
Romain Guy8a4ac612012-07-17 17:32:48 -07001423 r.snapToPixelBoundaries();
1424
1425 Rect clipRect(*mSnapshot->clipRect);
1426 clipRect.snapToPixelBoundaries();
1427
1428 return !clipRect.intersects(r);
1429}
1430
Romain Guy35643dd2012-09-18 15:40:58 -07001431bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1432 Rect& transformed, Rect& clip) {
1433 if (mSnapshot->isIgnored()) {
1434 return true;
1435 }
1436
1437 transformed.set(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001438 currentTransform().mapRect(transformed);
Romain Guy35643dd2012-09-18 15:40:58 -07001439 transformed.snapToPixelBoundaries();
1440
1441 clip.set(*mSnapshot->clipRect);
1442 clip.snapToPixelBoundaries();
1443
1444 return !clip.intersects(transformed);
1445}
1446
Romain Guy672433d2013-01-04 19:05:13 -08001447bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1448 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001449 if (paint->getStyle() != SkPaint::kFill_Style) {
1450 float outset = paint->getStrokeWidth() * 0.5f;
1451 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1452 } else {
1453 return quickReject(left, top, right, bottom);
1454 }
1455}
1456
Romain Guyc7d53492010-06-25 13:41:57 -07001457bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001458 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001459 return true;
1460 }
1461
Romain Guy1d83e192010-08-17 11:37:00 -07001462 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001463 currentTransform().mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001464 r.snapToPixelBoundaries();
1465
1466 Rect clipRect(*mSnapshot->clipRect);
1467 clipRect.snapToPixelBoundaries();
1468
Romain Guy586cae32012-07-13 15:28:31 -07001469 bool rejected = !clipRect.intersects(r);
1470 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001471 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001472 }
1473
1474 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001475}
1476
Romain Guy8ce00302013-01-15 18:51:42 -08001477void OpenGLRenderer::debugClip() {
1478#if DEBUG_CLIP_REGIONS
1479 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1480 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1481 }
1482#endif
1483}
1484
Romain Guy079ba2c2010-07-16 14:12:24 -07001485bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001486 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001487 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1488 if (clipped) {
1489 dirtyClip();
1490 }
1491 return !mSnapshot->clipRect->isEmpty();
1492 }
1493
1494 SkPath path;
1495 path.addRect(left, top, right, bottom);
1496
1497 return clipPath(&path, op);
1498}
1499
1500bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1501 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001502 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001503
1504 SkPath transformed;
1505 path->transform(transform, &transformed);
1506
1507 SkRegion clip;
1508 if (!mSnapshot->clipRegion->isEmpty()) {
1509 clip.setRegion(*mSnapshot->clipRegion);
1510 } else {
1511 Rect* bounds = mSnapshot->clipRect;
1512 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1513 }
1514
1515 SkRegion region;
1516 region.setPath(transformed, clip);
1517
1518 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001519 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001520 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001521 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001522 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001523}
1524
Romain Guy735738c2012-12-03 12:34:51 -08001525bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001526 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1527 if (clipped) {
1528 dirtyClip();
1529 }
1530 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001531}
1532
Chet Haasea23eed82012-04-12 15:19:04 -07001533Rect* OpenGLRenderer::getClipRect() {
1534 return mSnapshot->clipRect;
1535}
1536
Romain Guyf6a11b82010-06-23 17:47:49 -07001537///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001538// Drawing commands
1539///////////////////////////////////////////////////////////////////////////////
1540
Romain Guy54be1cd2011-06-13 19:04:27 -07001541void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001542 // TODO: It would be best if we could do this before quickReject()
1543 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001544 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001545 // Make sure setScissor & setStencil happen at the beginning of
1546 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001547 if (mDirtyClip) {
1548 if (mCaches.scissorEnabled) {
1549 setScissorFromClip();
1550 }
Romain Guy8ce00302013-01-15 18:51:42 -08001551 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001552 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001553
Romain Guy70ca14e2010-12-13 18:24:33 -08001554 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001555
Romain Guy70ca14e2010-12-13 18:24:33 -08001556 mSetShaderColor = false;
1557 mColorSet = false;
1558 mColorA = mColorR = mColorG = mColorB = 0.0f;
1559 mTextureUnit = 0;
1560 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001561
1562 // Enable debug highlight when what we're about to draw is tested against
1563 // the stencil buffer and if stencil highlight debugging is on
1564 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1565 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1566 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001567}
1568
1569void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1570 mDescription.hasTexture = true;
1571 mDescription.hasAlpha8Texture = isAlpha8;
1572}
1573
Romain Guyff316ec2013-02-13 18:39:43 -08001574void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1575 mDescription.hasTexture = true;
1576 mDescription.hasColors = true;
1577 mDescription.hasAlpha8Texture = isAlpha8;
1578}
1579
Romain Guyaa6c24c2011-04-28 18:40:04 -07001580void OpenGLRenderer::setupDrawWithExternalTexture() {
1581 mDescription.hasExternalTexture = true;
1582}
1583
Romain Guy15bc6432011-12-13 13:11:32 -08001584void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001585 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001586}
1587
Chris Craik710f46d2012-09-17 17:25:49 -07001588void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001589 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001590}
1591
Romain Guyed6fcb02011-03-21 13:11:28 -07001592void OpenGLRenderer::setupDrawPoint(float pointSize) {
1593 mDescription.isPoint = true;
1594 mDescription.pointSize = pointSize;
1595}
1596
Romain Guy8d0d4782010-12-14 20:13:35 -08001597void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1598 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001599 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1600 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1601 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001602 mColorSet = true;
1603 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1604}
1605
Romain Guy86568192010-12-14 15:55:39 -08001606void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1607 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001608 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1609 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1610 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001611 mColorSet = true;
1612 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1613}
1614
Romain Guy41210632012-07-16 17:04:24 -07001615void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1616 mCaches.fontRenderer->describe(mDescription, paint);
1617}
1618
Romain Guy70ca14e2010-12-13 18:24:33 -08001619void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1620 mColorA = a;
1621 mColorR = r;
1622 mColorG = g;
1623 mColorB = b;
1624 mColorSet = true;
1625 mSetShaderColor = mDescription.setColor(r, g, b, a);
1626}
1627
1628void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001629 if (mDrawModifiers.mShader) {
1630 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001631 }
1632}
1633
1634void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001635 if (mDrawModifiers.mColorFilter) {
1636 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001637 }
1638}
1639
Romain Guyf09ef512011-05-27 11:43:46 -07001640void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1641 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1642 mColorA = 1.0f;
1643 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001644 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001645 }
1646}
1647
Romain Guy70ca14e2010-12-13 18:24:33 -08001648void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001649 // When the blending mode is kClear_Mode, we need to use a modulate color
1650 // argb=1,0,0,0
1651 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001652 bool blend = (mColorSet && mColorA < 1.0f) ||
1653 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1654 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001655}
1656
1657void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001658 // When the blending mode is kClear_Mode, we need to use a modulate color
1659 // argb=1,0,0,0
1660 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001661 blend |= (mColorSet && mColorA < 1.0f) ||
1662 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1663 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1664 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001665}
1666
1667void OpenGLRenderer::setupDrawProgram() {
1668 useProgram(mCaches.programCache.get(mDescription));
1669}
1670
1671void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1672 mTrackDirtyRegions = false;
1673}
1674
1675void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1676 bool ignoreTransform) {
1677 mModelView.loadTranslate(left, top, 0.0f);
1678 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001679 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1680 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001681 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001682 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001683 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1684 }
1685}
1686
Chet Haase8a5cc922011-04-26 07:28:09 -07001687void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001688 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001689}
1690
Romain Guy70ca14e2010-12-13 18:24:33 -08001691void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1692 bool ignoreTransform, bool ignoreModelView) {
1693 if (!ignoreModelView) {
1694 mModelView.loadTranslate(left, top, 0.0f);
1695 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001696 } else {
1697 mModelView.loadIdentity();
1698 }
Romain Guy86568192010-12-14 15:55:39 -08001699 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1700 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001701 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001702 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001703 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001704 }
1705 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001706 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001707 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1708 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001709}
1710
Romain Guyed6fcb02011-03-21 13:11:28 -07001711void OpenGLRenderer::setupDrawPointUniforms() {
1712 int slot = mCaches.currentProgram->getUniform("pointSize");
1713 glUniform1f(slot, mDescription.pointSize);
1714}
1715
Romain Guy70ca14e2010-12-13 18:24:33 -08001716void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001717 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001718 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1719 }
1720}
1721
Romain Guy86568192010-12-14 15:55:39 -08001722void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001723 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001724 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001725 }
1726}
1727
Romain Guy70ca14e2010-12-13 18:24:33 -08001728void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001729 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001730 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001731 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001732 }
Chris Craikc3566d02013-02-04 16:16:33 -08001733 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1734 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001735 }
1736}
1737
Romain Guy8d0d4782010-12-14 20:13:35 -08001738void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001739 if (mDrawModifiers.mShader) {
1740 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001741 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001742 }
1743}
1744
Romain Guy70ca14e2010-12-13 18:24:33 -08001745void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001746 if (mDrawModifiers.mColorFilter) {
1747 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001748 }
1749}
1750
Romain Guy41210632012-07-16 17:04:24 -07001751void OpenGLRenderer::setupDrawTextGammaUniforms() {
1752 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1753}
1754
Romain Guy70ca14e2010-12-13 18:24:33 -08001755void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001756 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001757 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001758 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001759}
1760
1761void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001762 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001763 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001764 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001765}
1766
Romain Guyaa6c24c2011-04-28 18:40:04 -07001767void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1768 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001769 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001770 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001771}
1772
Romain Guy8f0095c2011-05-02 17:24:22 -07001773void OpenGLRenderer::setupDrawTextureTransform() {
1774 mDescription.hasTextureTransform = true;
1775}
1776
1777void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001778 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1779 GL_FALSE, &transform.data[0]);
1780}
1781
Romain Guy70ca14e2010-12-13 18:24:33 -08001782void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001783 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001784 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001785 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001786 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001787 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001788 }
Romain Guyd71dd362011-12-12 19:03:35 -08001789
Chris Craikcb4d6002012-09-25 12:00:29 -07001790 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001791 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001792 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001793 }
1794
1795 mCaches.unbindIndicesBuffer();
1796}
1797
Romain Guyff316ec2013-02-13 18:39:43 -08001798void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1799 bool force = mCaches.unbindMeshBuffer();
1800 GLsizei stride = sizeof(ColorTextureVertex);
1801
1802 mCaches.bindPositionVertexPointer(force, vertices, stride);
1803 if (mCaches.currentProgram->texCoords >= 0) {
1804 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1805 }
1806 int slot = mCaches.currentProgram->getAttrib("colors");
1807 if (slot >= 0) {
1808 glEnableVertexAttribArray(slot);
1809 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1810 }
1811
1812 mCaches.unbindIndicesBuffer();
1813}
1814
Romain Guy15bc6432011-12-13 13:11:32 -08001815void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1816 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001817 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001818 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001819 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001820 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001821}
1822
Chet Haase5b0200b2011-04-13 17:58:08 -07001823void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001824 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001825 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001826 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001827}
1828
Romain Guy70ca14e2010-12-13 18:24:33 -08001829void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001830}
1831
1832///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001833// Drawing
1834///////////////////////////////////////////////////////////////////////////////
1835
Chris Craikff785832013-03-08 13:12:16 -08001836status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
1837 int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001838 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1839 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001840 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07001841 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chris Craikff785832013-03-08 13:12:16 -08001842 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
1843 displayList->replay(replayStruct, 0);
1844 return replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08001845 }
1846
1847 DeferredDisplayList deferredList;
Chris Craikff785832013-03-08 13:12:16 -08001848 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
1849 displayList->defer(deferStruct, 0);
1850 return deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08001851 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001852
Romain Guy65549432012-03-26 16:45:05 -07001853 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001854}
1855
Chris Craikc3566d02013-02-04 16:16:33 -08001856void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07001857 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08001858 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07001859 }
1860}
1861
Romain Guya168d732011-03-18 16:50:13 -07001862void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1863 int alpha;
1864 SkXfermode::Mode mode;
1865 getAlphaAndMode(paint, &alpha, &mode);
1866
Romain Guy886b2752013-01-04 12:26:18 -08001867 int color = paint != NULL ? paint->getColor() : 0;
1868
Romain Guya168d732011-03-18 16:50:13 -07001869 float x = left;
1870 float y = top;
1871
Romain Guy886b2752013-01-04 12:26:18 -08001872 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1873
Romain Guya168d732011-03-18 16:50:13 -07001874 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08001875 if (currentTransform().isPureTranslate()) {
1876 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
1877 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001878 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001879
1880 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001881 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001882 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001883 }
1884
Romain Guy886b2752013-01-04 12:26:18 -08001885 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1886 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1887 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001888}
1889
Chet Haase48659092012-05-31 15:21:51 -07001890status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001891 const float right = left + bitmap->width();
1892 const float bottom = top + bitmap->height();
1893
1894 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001895 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001896 }
1897
Romain Guya1d3c912011-12-13 14:55:06 -08001898 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001899 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001900 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001901 const AutoTexture autoCleanup(texture);
1902
Romain Guy211370f2012-02-01 16:10:55 -08001903 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001904 drawAlphaBitmap(texture, left, top, paint);
1905 } else {
1906 drawTextureRect(left, top, right, bottom, texture, paint);
1907 }
Chet Haase48659092012-05-31 15:21:51 -07001908
1909 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001910}
1911
Chet Haase48659092012-05-31 15:21:51 -07001912status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001913 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1914 const mat4 transform(*matrix);
1915 transform.mapRect(r);
1916
Romain Guy6926c722010-07-12 20:20:03 -07001917 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001918 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001919 }
1920
Romain Guya1d3c912011-12-13 14:55:06 -08001921 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001922 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001923 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001924 const AutoTexture autoCleanup(texture);
1925
Romain Guy5b3b3522010-10-27 18:57:51 -07001926 // This could be done in a cheaper way, all we need is pass the matrix
1927 // to the vertex shader. The save/restore is a bit overkill.
1928 save(SkCanvas::kMatrix_SaveFlag);
1929 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08001930 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1931 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
1932 } else {
1933 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1934 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001935 restore();
Chet Haase48659092012-05-31 15:21:51 -07001936
1937 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001938}
1939
Chet Haase48659092012-05-31 15:21:51 -07001940status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001941 const float right = left + bitmap->width();
1942 const float bottom = top + bitmap->height();
1943
1944 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001945 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001946 }
1947
1948 mCaches.activeTexture(0);
1949 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1950 const AutoTexture autoCleanup(texture);
1951
Romain Guy886b2752013-01-04 12:26:18 -08001952 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1953 drawAlphaBitmap(texture, left, top, paint);
1954 } else {
1955 drawTextureRect(left, top, right, bottom, texture, paint);
1956 }
Chet Haase48659092012-05-31 15:21:51 -07001957
1958 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001959}
1960
Chet Haase48659092012-05-31 15:21:51 -07001961status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001962 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001963 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001964 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001965 }
1966
Romain Guyb18d2d02011-02-10 15:52:54 -08001967 float left = FLT_MAX;
1968 float top = FLT_MAX;
1969 float right = FLT_MIN;
1970 float bottom = FLT_MIN;
1971
Romain Guya92bb4d2012-10-16 11:08:44 -07001972 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001973
Romain Guyff316ec2013-02-13 18:39:43 -08001974 ColorTextureVertex mesh[count];
1975 ColorTextureVertex* vertex = mesh;
1976
1977 bool cleanupColors = false;
1978 if (!colors) {
1979 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
1980 colors = new int[colorsCount];
1981 memset(colors, 0xff, colorsCount * sizeof(int));
1982 cleanupColors = true;
1983 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001984
Romain Guy5a7b4662011-01-20 19:09:30 -08001985 for (int32_t y = 0; y < meshHeight; y++) {
1986 for (int32_t x = 0; x < meshWidth; x++) {
1987 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1988
1989 float u1 = float(x) / meshWidth;
1990 float u2 = float(x + 1) / meshWidth;
1991 float v1 = float(y) / meshHeight;
1992 float v2 = float(y + 1) / meshHeight;
1993
1994 int ax = i + (meshWidth + 1) * 2;
1995 int ay = ax + 1;
1996 int bx = i;
1997 int by = bx + 1;
1998 int cx = i + 2;
1999 int cy = cx + 1;
2000 int dx = i + (meshWidth + 1) * 2 + 2;
2001 int dy = dx + 1;
2002
Romain Guyff316ec2013-02-13 18:39:43 -08002003 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2004 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2005 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002006
Romain Guyff316ec2013-02-13 18:39:43 -08002007 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2008 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2009 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002010
Romain Guya92bb4d2012-10-16 11:08:44 -07002011 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2012 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2013 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2014 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002015 }
2016 }
2017
Romain Guya92bb4d2012-10-16 11:08:44 -07002018 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002019 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002020 return DrawGlInfo::kStatusDone;
2021 }
2022
2023 mCaches.activeTexture(0);
2024 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guyff316ec2013-02-13 18:39:43 -08002025 if (!texture) {
2026 if (cleanupColors) delete[] colors;
2027 return DrawGlInfo::kStatusDone;
2028 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002029 const AutoTexture autoCleanup(texture);
2030
2031 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2032 texture->setFilter(FILTER(paint), true);
2033
2034 int alpha;
2035 SkXfermode::Mode mode;
2036 getAlphaAndMode(paint, &alpha, &mode);
2037
Romain Guyff316ec2013-02-13 18:39:43 -08002038 float a = alpha / 255.0f;
2039
Romain Guya92bb4d2012-10-16 11:08:44 -07002040 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002041 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002042 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002043
Romain Guyff316ec2013-02-13 18:39:43 -08002044 setupDraw();
2045 setupDrawWithTextureAndColor();
2046 setupDrawColor(a, a, a, a);
2047 setupDrawColorFilter();
2048 setupDrawBlending(true, mode, false);
2049 setupDrawProgram();
2050 setupDrawDirtyRegionsDisabled();
2051 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2052 setupDrawTexture(texture->id);
2053 setupDrawPureColorUniforms();
2054 setupDrawColorFilterUniforms();
2055 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2056
2057 glDrawArrays(GL_TRIANGLES, 0, count);
2058
2059 finishDrawTexture();
2060
2061 int slot = mCaches.currentProgram->getAttrib("colors");
2062 if (slot >= 0) {
2063 glDisableVertexAttribArray(slot);
2064 }
2065
2066 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002067
2068 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002069}
2070
Chet Haase48659092012-05-31 15:21:51 -07002071status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002072 float srcLeft, float srcTop, float srcRight, float srcBottom,
2073 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002074 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002075 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002076 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002077 }
2078
Romain Guya1d3c912011-12-13 14:55:06 -08002079 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002080 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002081 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002082 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002083
Romain Guy8ba548f2010-06-30 19:21:21 -07002084 const float width = texture->width;
2085 const float height = texture->height;
2086
Romain Guy68169722011-08-22 17:33:33 -07002087 const float u1 = fmax(0.0f, srcLeft / width);
2088 const float v1 = fmax(0.0f, srcTop / height);
2089 const float u2 = fmin(1.0f, srcRight / width);
2090 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07002091
Romain Guy03750a02010-10-18 14:06:08 -07002092 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002093 resetDrawTextureTexCoords(u1, v1, u2, v2);
2094
Romain Guy03750a02010-10-18 14:06:08 -07002095 int alpha;
2096 SkXfermode::Mode mode;
2097 getAlphaAndMode(paint, &alpha, &mode);
2098
Romain Guyd21b6e12011-11-30 20:21:23 -08002099 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2100
Romain Guy886b2752013-01-04 12:26:18 -08002101 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2102 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002103
Romain Guy886b2752013-01-04 12:26:18 -08002104 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2105 // Apply a scale transform on the canvas only when a shader is in use
2106 // Skia handles the ratio between the dst and src rects as a scale factor
2107 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002108 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002109 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002110
Romain Guy3b753822013-03-05 10:27:35 -08002111 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2112 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2113 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002114
2115 dstRight = x + (dstRight - dstLeft);
2116 dstBottom = y + (dstBottom - dstTop);
2117
2118 dstLeft = x;
2119 dstTop = y;
2120
2121 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2122 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002123 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002124 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002125 }
2126
2127 if (CC_UNLIKELY(useScaleTransform)) {
2128 save(SkCanvas::kMatrix_SaveFlag);
2129 translate(dstLeft, dstTop);
2130 scale(scaleX, scaleY);
2131
2132 dstLeft = 0.0f;
2133 dstTop = 0.0f;
2134
2135 dstRight = srcRight - srcLeft;
2136 dstBottom = srcBottom - srcTop;
2137 }
2138
2139 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2140 int color = paint ? paint->getColor() : 0;
2141 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2142 texture->id, paint != NULL, color, alpha, mode,
2143 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2144 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2145 } else {
2146 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2147 texture->id, alpha / 255.0f, mode, texture->blend,
2148 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2149 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2150 }
2151
2152 if (CC_UNLIKELY(useScaleTransform)) {
2153 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002154 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002155
2156 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002157
2158 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002159}
2160
Chet Haase48659092012-05-31 15:21:51 -07002161status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07002162 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07002163 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002164 int alpha;
2165 SkXfermode::Mode mode;
2166 getAlphaAndModeDirect(paint, &alpha, &mode);
2167
2168 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2169 left, top, right, bottom, alpha, mode);
2170}
2171
2172status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2173 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2174 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002175 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002176 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002177 }
2178
Romain Guybe6f9dc2012-07-16 12:41:17 -07002179 alpha *= mSnapshot->alpha;
2180
Romain Guy2728f962010-10-08 18:36:15 -07002181 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002182 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002183
Romain Guy211370f2012-02-01 16:10:55 -08002184 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002185 mCaches.activeTexture(0);
2186 Texture* texture = mCaches.textureCache.get(bitmap);
2187 if (!texture) return DrawGlInfo::kStatusDone;
2188 const AutoTexture autoCleanup(texture);
2189 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2190 texture->setFilter(GL_LINEAR, true);
2191
Romain Guy3b753822013-03-05 10:27:35 -08002192 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002193 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002194 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002195 const float offsetX = left + currentTransform().getTranslateX();
2196 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002197 const size_t count = mesh->quads.size();
2198 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002199 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002200 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002201 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2202 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2203 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002204 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002205 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002206 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002207 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002208 }
2209 }
2210
Romain Guy211370f2012-02-01 16:10:55 -08002211 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002212 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2213 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002214
2215 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2216 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2217 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2218 true, !mesh->hasEmptyQuads);
2219 } else {
2220 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2221 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2222 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2223 true, !mesh->hasEmptyQuads);
2224 }
Romain Guy054dc182010-10-15 17:55:25 -07002225 }
Chet Haase48659092012-05-31 15:21:51 -07002226
2227 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002228}
2229
Chris Craik65cd6122012-12-10 17:56:27 -08002230status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2231 bool useOffset) {
2232 if (!vertexBuffer.getSize()) {
2233 // no vertices to draw
2234 return DrawGlInfo::kStatusDone;
2235 }
2236
Chris Craikcb4d6002012-09-25 12:00:29 -07002237 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002238 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2239 bool isAA = paint->isAntiAlias();
2240
Chris Craik710f46d2012-09-17 17:25:49 -07002241 setupDraw();
2242 setupDrawNoTexture();
2243 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002244 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2245 setupDrawColorFilter();
2246 setupDrawShader();
2247 setupDrawBlending(isAA, mode);
2248 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002249 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002250 setupDrawColorUniforms();
2251 setupDrawColorFilterUniforms();
2252 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002253
Chris Craik710f46d2012-09-17 17:25:49 -07002254 void* vertices = vertexBuffer.getBuffer();
2255 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002256 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002257 mCaches.resetTexCoordsVertexPointer();
2258 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002259
Chris Craik710f46d2012-09-17 17:25:49 -07002260 int alphaSlot = -1;
2261 if (isAA) {
2262 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2263 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002264
Chris Craik710f46d2012-09-17 17:25:49 -07002265 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002266 glEnableVertexAttribArray(alphaSlot);
2267 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002268 }
Romain Guy04299382012-07-18 17:15:41 -07002269
Chris Craik710f46d2012-09-17 17:25:49 -07002270 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002271
Chris Craik710f46d2012-09-17 17:25:49 -07002272 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002273 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002274 }
Chris Craik65cd6122012-12-10 17:56:27 -08002275
2276 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002277}
2278
2279/**
Chris Craik65cd6122012-12-10 17:56:27 -08002280 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2281 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2282 * screen space in all directions. However, instead of using a fragment shader to compute the
2283 * translucency of the color from its position, we simply use a varying parameter to define how far
2284 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2285 *
2286 * Doesn't yet support joins, caps, or path effects.
2287 */
2288status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2289 VertexBuffer vertexBuffer;
2290 // TODO: try clipping large paths to viewport
2291 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2292
Romain Guyc46d07a2013-03-15 19:06:39 -07002293 if (hasLayer()) {
2294 SkRect bounds = path.getBounds();
2295 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2296 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2297 }
Chris Craik65cd6122012-12-10 17:56:27 -08002298
2299 return drawVertexBuffer(vertexBuffer, paint);
2300}
2301
2302/**
2303 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2304 * and additional geometry for defining an alpha slope perimeter.
2305 *
2306 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2307 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2308 * in-shader alpha region, but found it to be taxing on some GPUs.
2309 *
2310 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2311 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002312 */
Chet Haase48659092012-05-31 15:21:51 -07002313status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002314 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002315
Chris Craik65cd6122012-12-10 17:56:27 -08002316 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002317
Chris Craik65cd6122012-12-10 17:56:27 -08002318 VertexBuffer buffer;
2319 SkRect bounds;
2320 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002321
2322 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2323 return DrawGlInfo::kStatusDone;
2324 }
2325
Romain Guy3b753822013-03-05 10:27:35 -08002326 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002327
Chris Craik65cd6122012-12-10 17:56:27 -08002328 bool useOffset = !paint->isAntiAlias();
2329 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002330}
2331
Chet Haase48659092012-05-31 15:21:51 -07002332status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2333 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002334
2335 // TODO: The paint's cap style defines whether the points are square or circular
2336 // TODO: Handle AA for round points
2337
Chet Haase5b0200b2011-04-13 17:58:08 -07002338 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002339 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002340 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002341 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002342 if (isHairLine) {
2343 // Now that we know it's hairline, we can set the effective width, to be used later
2344 strokeWidth = 1.0f;
2345 }
2346 const float halfWidth = strokeWidth / 2;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002347
Romain Guyed6fcb02011-03-21 13:11:28 -07002348 int alpha;
2349 SkXfermode::Mode mode;
2350 getAlphaAndMode(paint, &alpha, &mode);
2351
2352 int verticesCount = count >> 1;
2353 int generatedVerticesCount = 0;
2354
2355 TextureVertex pointsData[verticesCount];
2356 TextureVertex* vertex = &pointsData[0];
2357
Romain Guy04299382012-07-18 17:15:41 -07002358 // TODO: We should optimize this method to not generate vertices for points
2359 // that lie outside of the clip.
2360 mCaches.enableScissor();
2361
Romain Guyed6fcb02011-03-21 13:11:28 -07002362 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002363 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002364 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002365 setupDrawColor(paint->getColor(), alpha);
2366 setupDrawColorFilter();
2367 setupDrawShader();
2368 setupDrawBlending(mode);
2369 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002370 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002371 setupDrawColorUniforms();
2372 setupDrawColorFilterUniforms();
2373 setupDrawPointUniforms();
2374 setupDrawShaderIdentityUniforms();
2375 setupDrawMesh(vertex);
2376
2377 for (int i = 0; i < count; i += 2) {
2378 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2379 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002380
Chet Haase8a5cc922011-04-26 07:28:09 -07002381 float left = points[i] - halfWidth;
2382 float right = points[i] + halfWidth;
2383 float top = points[i + 1] - halfWidth;
2384 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002385
Romain Guy3b753822013-03-05 10:27:35 -08002386 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyed6fcb02011-03-21 13:11:28 -07002387 }
2388
2389 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002390
2391 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002392}
2393
Chet Haase48659092012-05-31 15:21:51 -07002394status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002395 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002396 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002397
Romain Guyae88e5e2010-10-22 17:49:18 -07002398 Rect& clip(*mSnapshot->clipRect);
2399 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002400
Romain Guy3d58c032010-07-14 16:34:53 -07002401 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002402
2403 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002404}
Romain Guy9d5316e2010-06-24 19:30:36 -07002405
Chet Haase48659092012-05-31 15:21:51 -07002406status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2407 SkPaint* paint) {
2408 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002409 const AutoTexture autoCleanup(texture);
2410
2411 const float x = left + texture->left - texture->offset;
2412 const float y = top + texture->top - texture->offset;
2413
2414 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002415
2416 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002417}
2418
Chet Haase48659092012-05-31 15:21:51 -07002419status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002420 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002421 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2422 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002423 return DrawGlInfo::kStatusDone;
2424 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002425
Chris Craikcb4d6002012-09-25 12:00:29 -07002426 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002427 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002428 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002429 right - left, bottom - top, rx, ry, p);
2430 return drawShape(left, top, texture, p);
2431 }
2432
2433 SkPath path;
2434 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002435 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2436 float outset = p->getStrokeWidth() / 2;
2437 rect.outset(outset, outset);
2438 rx += outset;
2439 ry += outset;
2440 }
Chris Craik710f46d2012-09-17 17:25:49 -07002441 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002442 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002443}
2444
Chris Craik710f46d2012-09-17 17:25:49 -07002445status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002446 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002447 x + radius, y + radius, p) ||
2448 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002449 return DrawGlInfo::kStatusDone;
2450 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002451 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002452 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002453 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002454 return drawShape(x - radius, y - radius, texture, p);
2455 }
2456
2457 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002458 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2459 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2460 } else {
2461 path.addCircle(x, y, radius);
2462 }
Chris Craik65cd6122012-12-10 17:56:27 -08002463 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002464}
Romain Guy01d58e42011-01-19 21:54:02 -08002465
Chet Haase48659092012-05-31 15:21:51 -07002466status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002467 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002468 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2469 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002470 return DrawGlInfo::kStatusDone;
2471 }
Romain Guy01d58e42011-01-19 21:54:02 -08002472
Chris Craikcb4d6002012-09-25 12:00:29 -07002473 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002474 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002475 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002476 return drawShape(left, top, texture, p);
2477 }
2478
2479 SkPath path;
2480 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002481 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2482 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2483 }
Chris Craik710f46d2012-09-17 17:25:49 -07002484 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002485 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002486}
2487
Chet Haase48659092012-05-31 15:21:51 -07002488status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002489 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002490 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2491 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002492 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002493 }
2494
Chris Craik780c1282012-10-04 14:10:49 -07002495 if (fabs(sweepAngle) >= 360.0f) {
2496 return drawOval(left, top, right, bottom, p);
2497 }
2498
2499 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002500 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002501 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002502 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002503 startAngle, sweepAngle, useCenter, p);
2504 return drawShape(left, top, texture, p);
2505 }
2506
2507 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2508 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2509 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2510 }
2511
2512 SkPath path;
2513 if (useCenter) {
2514 path.moveTo(rect.centerX(), rect.centerY());
2515 }
2516 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2517 if (useCenter) {
2518 path.close();
2519 }
Chris Craik65cd6122012-12-10 17:56:27 -08002520 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002521}
2522
Romain Guycf8675e2012-10-02 12:32:25 -07002523// See SkPaintDefaults.h
2524#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2525
Chet Haase48659092012-05-31 15:21:51 -07002526status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002527 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2528 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002529 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002530 }
2531
Chris Craik710f46d2012-09-17 17:25:49 -07002532 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002533 // only fill style is supported by drawConvexPath, since others have to handle joins
2534 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2535 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2536 mCaches.activeTexture(0);
2537 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002538 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002539 return drawShape(left, top, texture, p);
2540 }
2541
2542 SkPath path;
2543 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2544 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2545 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2546 }
2547 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002548 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002549 }
2550
Romain Guy3b753822013-03-05 10:27:35 -08002551 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002552 SkPath path;
2553 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002554 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002555 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002556 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002557 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002558 }
Romain Guyc7d53492010-06-25 13:41:57 -07002559}
Romain Guy9d5316e2010-06-24 19:30:36 -07002560
Raph Levien416a8472012-07-19 22:48:17 -07002561void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2562 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2563 float x, float y) {
2564 mCaches.activeTexture(0);
2565
2566 // NOTE: The drop shadow will not perform gamma correction
2567 // if shader-based correction is enabled
2568 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2569 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002570 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Raph Levien416a8472012-07-19 22:48:17 -07002571 const AutoTexture autoCleanup(shadow);
2572
Chris Craikc3566d02013-02-04 16:16:33 -08002573 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2574 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002575
Chris Craikc3566d02013-02-04 16:16:33 -08002576 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2577 int shadowColor = mDrawModifiers.mShadowColor;
2578 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002579 shadowColor = 0xffffffff;
2580 }
2581
2582 setupDraw();
2583 setupDrawWithTexture(true);
2584 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2585 setupDrawColorFilter();
2586 setupDrawShader();
2587 setupDrawBlending(true, mode);
2588 setupDrawProgram();
2589 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2590 setupDrawTexture(shadow->id);
2591 setupDrawPureColorUniforms();
2592 setupDrawColorFilterUniforms();
2593 setupDrawShaderUniforms();
2594 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2595
2596 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2597}
2598
Romain Guy768bffc2013-02-27 13:50:45 -08002599bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2600 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2601 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2602}
2603
Romain Guy257ae352013-03-20 16:31:12 -07002604class TextSetupFunctor: public Functor {
2605public:
2606 TextSetupFunctor(OpenGLRenderer& renderer, float x, float y, bool pureTranslate,
2607 int alpha, SkXfermode::Mode mode, SkPaint* paint): Functor(),
2608 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate),
2609 alpha(alpha), mode(mode), paint(paint) {
2610 }
2611 ~TextSetupFunctor() { }
2612
2613 status_t operator ()(int what, void* data) {
2614 renderer.setupDraw();
2615 renderer.setupDrawTextGamma(paint);
2616 renderer.setupDrawDirtyRegionsDisabled();
2617 renderer.setupDrawWithTexture(true);
2618 renderer.setupDrawAlpha8Color(paint->getColor(), alpha);
2619 renderer.setupDrawColorFilter();
2620 renderer.setupDrawShader();
2621 renderer.setupDrawBlending(true, mode);
2622 renderer.setupDrawProgram();
2623 renderer.setupDrawModelView(x, y, x, y, pureTranslate, true);
2624 // Calling setupDrawTexture with the name 0 will enable the
2625 // uv attributes and increase the texture unit count
2626 // texture binding will be performed by the font renderer as
2627 // needed
2628 renderer.setupDrawTexture(0);
2629 renderer.setupDrawPureColorUniforms();
2630 renderer.setupDrawColorFilterUniforms();
2631 renderer.setupDrawShaderUniforms(pureTranslate);
2632 renderer.setupDrawTextGammaUniforms();
2633
2634 return NO_ERROR;
2635 }
2636
2637 OpenGLRenderer& renderer;
2638 float x;
2639 float y;
2640 bool pureTranslate;
2641 int alpha;
2642 SkXfermode::Mode mode;
2643 SkPaint* paint;
2644};
2645
Chet Haase48659092012-05-31 15:21:51 -07002646status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002647 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002648 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002649 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002650 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002651
Romain Guy671d6cf2012-01-18 12:39:17 -08002652 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002653 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002654 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002655 }
2656
2657 float x = 0.0f;
2658 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002659 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002660 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002661 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2662 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002663 }
2664
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002665 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002666 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002667
2668 int alpha;
2669 SkXfermode::Mode mode;
2670 getAlphaAndMode(paint, &alpha, &mode);
2671
Chris Craikc3566d02013-02-04 16:16:33 -08002672 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002673 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2674 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002675 }
2676
Romain Guy671d6cf2012-01-18 12:39:17 -08002677 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002678 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002679 if (pureTranslate && !linearFilter) {
2680 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2681 }
Romain Guy257ae352013-03-20 16:31:12 -07002682 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002683
2684 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2685 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2686
Romain Guy211370f2012-02-01 16:10:55 -08002687 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002688
Romain Guy257ae352013-03-20 16:31:12 -07002689 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002690 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002691 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002692 if (hasActiveLayer) {
2693 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002694 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002695 }
2696 dirtyLayerUnchecked(bounds, getRegion());
2697 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002698 }
Chet Haase48659092012-05-31 15:21:51 -07002699
2700 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002701}
2702
Romain Guy624234f2013-03-05 16:43:31 -08002703mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2704 mat4 fontTransform;
2705 if (CC_LIKELY(transform.isPureTranslate())) {
2706 fontTransform = mat4::identity();
2707 } else {
2708 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002709 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002710 } else {
2711 float sx, sy;
2712 currentTransform().decomposeScale(sx, sy);
2713 fontTransform.loadScale(sx, sy, 1.0f);
2714 }
2715 }
2716 return fontTransform;
2717}
2718
Romain Guyc2525952012-07-27 16:41:22 -07002719status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002720 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy768bffc2013-02-27 13:50:45 -08002721 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002722 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002723 }
2724
Chet Haasea1cff502012-02-21 13:43:44 -08002725 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002726 switch (paint->getTextAlign()) {
2727 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002728 x -= length / 2.0f;
2729 break;
2730 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002731 x -= length;
2732 break;
2733 default:
2734 break;
2735 }
2736
Romain Guycac5fd32011-12-01 20:08:50 -08002737 SkPaint::FontMetrics metrics;
2738 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002739 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002740 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002741 }
2742
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002743 const float oldX = x;
2744 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002745
2746 const mat4& transform = currentTransform();
2747 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002748
Romain Guy211370f2012-02-01 16:10:55 -08002749 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002750 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2751 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002752 }
2753
Romain Guy86568192010-12-14 15:55:39 -08002754 int alpha;
2755 SkXfermode::Mode mode;
2756 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002757
Romain Guyc74f45a2013-02-26 19:10:14 -08002758 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2759
Chris Craikc3566d02013-02-04 16:16:33 -08002760 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002761 fontRenderer.setFont(paint, mat4::identity());
2762 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2763 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002764 }
2765
Romain Guy19d4dd82013-03-04 11:14:26 -08002766 const bool hasActiveLayer = hasLayer();
2767
Romain Guy624234f2013-03-05 16:43:31 -08002768 // We only pass a partial transform to the font renderer. That partial
2769 // matrix defines how glyphs are rasterized. Typically we want glyphs
2770 // to be rasterized at their final size on screen, which means the partial
2771 // matrix needs to take the scale factor into account.
2772 // When a partial matrix is used to transform glyphs during rasterization,
2773 // the mesh is generated with the inverse transform (in the case of scale,
2774 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2775 // apply the full transform matrix at draw time in the vertex shader.
2776 // Applying the full matrix in the shader is the easiest way to handle
2777 // rotation and perspective and allows us to always generated quads in the
2778 // font renderer which greatly simplifies the code, clipping in particular.
2779 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002780 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002781
Romain Guy6620c6d2010-12-06 18:07:02 -08002782 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002783 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002784 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002785
Romain Guy624234f2013-03-05 16:43:31 -08002786 // TODO: Implement better clipping for scaled/rotated text
2787 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Romain Guy5b3b3522010-10-27 18:57:51 -07002788 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2789
Raph Levien996e57c2012-07-23 15:22:52 -07002790 bool status;
Romain Guy257ae352013-03-20 16:31:12 -07002791 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guya3dc55f2012-09-28 13:55:44 -07002792 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002793 SkPaint paintCopy(*paint);
2794 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2795 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002796 positions, hasActiveLayer ? &bounds : NULL, &functor);
Raph Levien996e57c2012-07-23 15:22:52 -07002797 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002798 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002799 positions, hasActiveLayer ? &bounds : NULL, &functor);
Raph Levien996e57c2012-07-23 15:22:52 -07002800 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002801
2802 if (status && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002803 if (!pureTranslate) {
2804 transform.mapRect(bounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002805 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002806 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002807 }
Romain Guy694b5192010-07-21 21:33:20 -07002808
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002809 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002810
2811 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002812}
2813
Chet Haase48659092012-05-31 15:21:51 -07002814status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002815 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002816 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002817 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002818 }
2819
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002820 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002821 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07002822 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002823
2824 int alpha;
2825 SkXfermode::Mode mode;
2826 getAlphaAndMode(paint, &alpha, &mode);
2827
Romain Guy03d58522012-02-24 17:54:07 -08002828 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002829 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002830 setupDrawDirtyRegionsDisabled();
2831 setupDrawWithTexture(true);
2832 setupDrawAlpha8Color(paint->getColor(), alpha);
2833 setupDrawColorFilter();
2834 setupDrawShader();
2835 setupDrawBlending(true, mode);
2836 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002837 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy257ae352013-03-20 16:31:12 -07002838 // Calling setupDrawTexture with the name 0 will enable the
2839 // uv attributes and increase the texture unit count
2840 // texture binding will be performed by the font renderer as
2841 // needed
2842 setupDrawTexture(0);
Romain Guy03d58522012-02-24 17:54:07 -08002843 setupDrawPureColorUniforms();
2844 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002845 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002846 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002847
Romain Guy97771732012-02-28 18:17:02 -08002848 const Rect* clip = &mSnapshot->getLocalClip();
2849 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 -08002850
Romain Guy97771732012-02-28 18:17:02 -08002851 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002852
2853 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2854 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002855 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08002856 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002857 dirtyLayerUnchecked(bounds, getRegion());
2858 }
Romain Guy97771732012-02-28 18:17:02 -08002859 }
Chet Haase48659092012-05-31 15:21:51 -07002860
2861 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002862}
2863
Chet Haase48659092012-05-31 15:21:51 -07002864status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2865 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002866
Romain Guya1d3c912011-12-13 14:55:06 -08002867 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002868
Romain Guyfb8b7632010-08-23 21:05:08 -07002869 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002870 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002871 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002872
Romain Guy8b55f372010-08-18 17:10:07 -07002873 const float x = texture->left - texture->offset;
2874 const float y = texture->top - texture->offset;
2875
Romain Guy01d58e42011-01-19 21:54:02 -08002876 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002877
2878 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002879}
2880
Chris Craika08f95c2013-03-15 17:24:33 -07002881status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002882 if (!layer) {
2883 return DrawGlInfo::kStatusDone;
2884 }
2885
Romain Guyb2e2f242012-10-17 18:18:35 -07002886 mat4* transform = NULL;
2887 if (layer->isTextureLayer()) {
2888 transform = &layer->getTransform();
2889 if (!transform->isIdentity()) {
2890 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08002891 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002892 }
2893 }
2894
Romain Guy35643dd2012-09-18 15:40:58 -07002895 Rect transformed;
2896 Rect clip;
2897 const bool rejected = quickRejectNoScissor(x, y,
2898 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2899
2900 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002901 if (transform && !transform->isIdentity()) {
2902 restore();
2903 }
Chet Haase48659092012-05-31 15:21:51 -07002904 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002905 }
2906
Romain Guy5bb3c732012-11-29 17:52:58 -08002907 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002908
Romain Guy87e2f7572012-09-24 11:37:12 -07002909 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002910 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002911
Romain Guy211370f2012-02-01 16:10:55 -08002912 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08002913 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
2914 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07002915
Romain Guyc88e3572011-01-22 00:32:12 -08002916 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002917 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002918 } else if (layer->mesh) {
Chris Craika08f95c2013-03-15 17:24:33 -07002919 const float a = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guyc88e3572011-01-22 00:32:12 -08002920 setupDraw();
2921 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002922 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002923 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002924 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002925 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002926 setupDrawPureColorUniforms();
2927 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002928 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08002929 if (CC_LIKELY(currentTransform().isPureTranslate())) {
2930 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2931 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002932
Romain Guyd21b6e12011-11-30 20:21:23 -08002933 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002934 setupDrawModelViewTranslate(tx, ty,
2935 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002936 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002937 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002938 setupDrawModelViewTranslate(x, y,
2939 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2940 }
Romain Guyc88e3572011-01-22 00:32:12 -08002941 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002942
Romain Guyc88e3572011-01-22 00:32:12 -08002943 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2944 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002945
Romain Guyc88e3572011-01-22 00:32:12 -08002946 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002947
2948#if DEBUG_LAYERS_AS_REGIONS
2949 drawRegionRects(layer->region);
2950#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002951 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002952
Chris Craikc3566d02013-02-04 16:16:33 -08002953 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07002954
Romain Guy5bb3c732012-11-29 17:52:58 -08002955 if (layer->debugDrawUpdate) {
2956 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002957 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2958 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2959 }
Romain Guyf219da52011-01-16 12:54:25 -08002960 }
Chet Haase48659092012-05-31 15:21:51 -07002961
Romain Guyb2e2f242012-10-17 18:18:35 -07002962 if (transform && !transform->isIdentity()) {
2963 restore();
2964 }
2965
Chet Haase48659092012-05-31 15:21:51 -07002966 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002967}
2968
Romain Guy6926c722010-07-12 20:20:03 -07002969///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002970// Shaders
2971///////////////////////////////////////////////////////////////////////////////
2972
2973void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08002974 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002975}
2976
Romain Guy06f96e22010-07-30 19:18:16 -07002977void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08002978 mDrawModifiers.mShader = shader;
2979 if (mDrawModifiers.mShader) {
2980 mDrawModifiers.mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002981 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002982}
2983
Romain Guyd27977d2010-07-14 19:18:51 -07002984///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002985// Color filters
2986///////////////////////////////////////////////////////////////////////////////
2987
2988void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08002989 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07002990}
2991
2992void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08002993 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07002994}
2995
2996///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002997// Drop shadow
2998///////////////////////////////////////////////////////////////////////////////
2999
3000void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003001 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003002}
3003
3004void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003005 mDrawModifiers.mHasShadow = true;
3006 mDrawModifiers.mShadowRadius = radius;
3007 mDrawModifiers.mShadowDx = dx;
3008 mDrawModifiers.mShadowDy = dy;
3009 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003010}
3011
3012///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003013// Draw filters
3014///////////////////////////////////////////////////////////////////////////////
3015
3016void OpenGLRenderer::resetPaintFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003017 mDrawModifiers.mHasDrawFilter = false;
Romain Guy5ff9df62012-01-23 17:09:05 -08003018}
3019
3020void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003021 mDrawModifiers.mHasDrawFilter = true;
3022 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3023 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003024}
3025
Chris Craika08f95c2013-03-15 17:24:33 -07003026SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003027 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003028 return paint;
3029 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003030
3031 uint32_t flags = paint->getFlags();
3032
3033 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003034 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3035 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003036
3037 return &mFilteredPaint;
3038}
3039
3040///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003041// Drawing implementation
3042///////////////////////////////////////////////////////////////////////////////
3043
Romain Guy01d58e42011-01-19 21:54:02 -08003044void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3045 float x, float y, SkPaint* paint) {
3046 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3047 return;
3048 }
3049
3050 int alpha;
3051 SkXfermode::Mode mode;
3052 getAlphaAndMode(paint, &alpha, &mode);
3053
3054 setupDraw();
3055 setupDrawWithTexture(true);
3056 setupDrawAlpha8Color(paint->getColor(), alpha);
3057 setupDrawColorFilter();
3058 setupDrawShader();
3059 setupDrawBlending(true, mode);
3060 setupDrawProgram();
3061 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3062 setupDrawTexture(texture->id);
3063 setupDrawPureColorUniforms();
3064 setupDrawColorFilterUniforms();
3065 setupDrawShaderUniforms();
3066 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3067
3068 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3069
3070 finishDrawTexture();
3071}
3072
Romain Guyf607bdc2010-09-10 19:20:06 -07003073// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003074#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3075#define kStdUnderline_Offset (1.0f / 9.0f)
3076#define kStdUnderline_Thickness (1.0f / 18.0f)
3077
3078void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3079 float x, float y, SkPaint* paint) {
3080 // Handle underline and strike-through
3081 uint32_t flags = paint->getFlags();
3082 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003083 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003084 float underlineWidth = length;
3085 // If length is > 0.0f, we already measured the text for the text alignment
3086 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003087 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003088 }
3089
Romain Guy211370f2012-02-01 16:10:55 -08003090 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003091 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003092 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003093
Raph Levien8b4072d2012-07-30 15:50:00 -07003094 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003095 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003096
Romain Guyf6834472011-01-23 13:32:12 -08003097 int linesCount = 0;
3098 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3099 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3100
3101 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003102 float points[pointsCount];
3103 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003104
3105 if (flags & SkPaint::kUnderlineText_Flag) {
3106 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003107 points[currentPoint++] = left;
3108 points[currentPoint++] = top;
3109 points[currentPoint++] = left + underlineWidth;
3110 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003111 }
3112
3113 if (flags & SkPaint::kStrikeThruText_Flag) {
3114 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003115 points[currentPoint++] = left;
3116 points[currentPoint++] = top;
3117 points[currentPoint++] = left + underlineWidth;
3118 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003119 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003120
Romain Guy726aeba2011-06-01 14:52:00 -07003121 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003122
Romain Guy726aeba2011-06-01 14:52:00 -07003123 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003124 }
3125 }
3126}
3127
Romain Guy672433d2013-01-04 19:05:13 -08003128status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3129 if (mSnapshot->isIgnored()) {
3130 return DrawGlInfo::kStatusDone;
3131 }
3132
Romain Guy735738c2012-12-03 12:34:51 -08003133 int color = paint->getColor();
3134 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003135 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003136 color |= 0x00ffffff;
3137 }
3138 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3139
3140 return drawColorRects(rects, count, color, mode);
3141}
3142
3143status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003144 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003145 if (count == 0) {
3146 return DrawGlInfo::kStatusDone;
3147 }
Romain Guy735738c2012-12-03 12:34:51 -08003148
Romain Guy672433d2013-01-04 19:05:13 -08003149 float left = FLT_MAX;
3150 float top = FLT_MAX;
3151 float right = FLT_MIN;
3152 float bottom = FLT_MIN;
3153
3154 int vertexCount = 0;
3155 Vertex mesh[count * 6];
3156 Vertex* vertex = mesh;
3157
Chris Craik2af46352012-11-26 18:30:17 -08003158 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003159 float l = rects[index + 0];
3160 float t = rects[index + 1];
3161 float r = rects[index + 2];
3162 float b = rects[index + 3];
3163
Romain Guy3b753822013-03-05 10:27:35 -08003164 Vertex::set(vertex++, l, b);
3165 Vertex::set(vertex++, l, t);
3166 Vertex::set(vertex++, r, t);
3167 Vertex::set(vertex++, l, b);
3168 Vertex::set(vertex++, r, t);
3169 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003170
Romain Guy3b753822013-03-05 10:27:35 -08003171 vertexCount += 6;
Romain Guy672433d2013-01-04 19:05:13 -08003172
Romain Guy3b753822013-03-05 10:27:35 -08003173 left = fminf(left, l);
3174 top = fminf(top, t);
3175 right = fmaxf(right, r);
3176 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003177 }
3178
Romain Guy3b753822013-03-05 10:27:35 -08003179 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003180 return DrawGlInfo::kStatusDone;
3181 }
Romain Guy672433d2013-01-04 19:05:13 -08003182
Romain Guy672433d2013-01-04 19:05:13 -08003183 setupDraw();
3184 setupDrawNoTexture();
3185 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3186 setupDrawShader();
3187 setupDrawColorFilter();
3188 setupDrawBlending(mode);
3189 setupDrawProgram();
3190 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003191 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003192 setupDrawColorUniforms();
3193 setupDrawShaderUniforms();
3194 setupDrawColorFilterUniforms();
3195 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3196
Romain Guy8ce00302013-01-15 18:51:42 -08003197 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003198 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003199 }
3200
3201 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3202
3203 return DrawGlInfo::kStatusDrew;
3204}
3205
Romain Guy026c5e162010-06-28 17:12:22 -07003206void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003207 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003208 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003209 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003210 color |= 0x00ffffff;
3211 }
3212
Romain Guy70ca14e2010-12-13 18:24:33 -08003213 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003214 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003215 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003216 setupDrawShader();
3217 setupDrawColorFilter();
3218 setupDrawBlending(mode);
3219 setupDrawProgram();
3220 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3221 setupDrawColorUniforms();
3222 setupDrawShaderUniforms(ignoreTransform);
3223 setupDrawColorFilterUniforms();
3224 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003225
Romain Guyc95c8d62010-09-17 15:31:32 -07003226 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3227}
3228
Romain Guy82ba8142010-07-09 13:25:56 -07003229void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003230 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003231 int alpha;
3232 SkXfermode::Mode mode;
3233 getAlphaAndMode(paint, &alpha, &mode);
3234
Romain Guyd21b6e12011-11-30 20:21:23 -08003235 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003236
Romain Guy3b753822013-03-05 10:27:35 -08003237 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3238 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3239 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003240
Romain Guyd21b6e12011-11-30 20:21:23 -08003241 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003242 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3243 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3244 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3245 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003246 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003247 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3248 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3249 GL_TRIANGLE_STRIP, gMeshCount);
3250 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003251}
3252
Romain Guybd6b79b2010-06-26 00:13:53 -07003253void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003254 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3255 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003256 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003257}
3258
3259void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003260 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003261 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003262 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003263
Romain Guy746b7402010-10-26 16:27:31 -07003264 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003265 setupDrawWithTexture();
3266 setupDrawColor(alpha, alpha, alpha, alpha);
3267 setupDrawColorFilter();
3268 setupDrawBlending(blend, mode, swapSrcDst);
3269 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003270 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003271 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003272 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003273 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003274 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003275 }
Romain Guy886b2752013-01-04 12:26:18 -08003276 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003277 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003278 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003279 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003280
Romain Guy6820ac82010-09-15 18:11:50 -07003281 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003282
3283 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003284}
3285
Romain Guy886b2752013-01-04 12:26:18 -08003286void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3287 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3288 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3289 bool ignoreTransform, bool dirty) {
3290
3291 setupDraw();
3292 setupDrawWithTexture(true);
3293 if (hasColor) {
3294 setupDrawAlpha8Color(color, alpha);
3295 }
3296 setupDrawColorFilter();
3297 setupDrawShader();
3298 setupDrawBlending(true, mode);
3299 setupDrawProgram();
3300 if (!dirty) setupDrawDirtyRegionsDisabled();
3301 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3302 setupDrawTexture(texture);
3303 setupDrawPureColorUniforms();
3304 setupDrawColorFilterUniforms();
3305 setupDrawShaderUniforms();
3306 setupDrawMesh(vertices, texCoords);
3307
3308 glDrawArrays(drawMode, 0, elementsCount);
3309
3310 finishDrawTexture();
3311}
3312
Romain Guya5aed0d2010-09-09 14:42:43 -07003313void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003314 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003315 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003316
Romain Guy82ba8142010-07-09 13:25:56 -07003317 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003318 // These blend modes are not supported by OpenGL directly and have
3319 // to be implemented using shaders. Since the shader will perform
3320 // the blending, turn blending off here
3321 // If the blend mode cannot be implemented using shaders, fall
3322 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003323 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003324 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003325 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003326 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003327
Romain Guy82bc7a72012-01-03 14:13:39 -08003328 if (mCaches.blend) {
3329 glDisable(GL_BLEND);
3330 mCaches.blend = false;
3331 }
3332
3333 return;
3334 } else {
3335 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003336 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003337 }
3338
3339 if (!mCaches.blend) {
3340 glEnable(GL_BLEND);
3341 }
3342
3343 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3344 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3345
3346 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3347 glBlendFunc(sourceMode, destMode);
3348 mCaches.lastSrcMode = sourceMode;
3349 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003350 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003351 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003352 glDisable(GL_BLEND);
3353 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003354 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003355}
3356
Romain Guy889f8d12010-07-29 14:37:42 -07003357bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003358 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003359 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003360 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003361 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003362 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003363 }
Romain Guy6926c722010-07-12 20:20:03 -07003364 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003365}
3366
Romain Guy026c5e162010-06-28 17:12:22 -07003367void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003368 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003369 TextureVertex::setUV(v++, u1, v1);
3370 TextureVertex::setUV(v++, u2, v1);
3371 TextureVertex::setUV(v++, u1, v2);
3372 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003373}
3374
Chet Haase5c13d892010-10-08 08:37:55 -07003375void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003376 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003377 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003378}
3379
Romain Guy9d5316e2010-06-24 19:30:36 -07003380}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003381}; // namespace android