blob: 7fe0a69274e4bd968b00026bfd4fe0a9f906aa29 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy03d58522012-02-24 17:54:07 -080024#include <SkPathMeasure.h>
Romain Guy694b5192010-07-21 21:33:20 -070025#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070028#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Romain Guy08aa2cb2011-03-17 11:06:57 -070030#include <private/hwui/DrawGlInfo.h>
31
Romain Guy5b3b3522010-10-27 18:57:51 -070032#include <ui/Rect.h>
33
Romain Guy85bf02f2010-06-22 13:11:24 -070034#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080035#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080036#include "DisplayListRenderer.h"
Chris Craik65cd6122012-12-10 17:56:27 -080037#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070038#include "Properties.h"
Romain Guya957eea2010-12-08 18:34:42 -080039#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070040
41namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070042namespace uirenderer {
43
44///////////////////////////////////////////////////////////////////////////////
45// Defines
46///////////////////////////////////////////////////////////////////////////////
47
Romain Guy759ea802010-09-16 20:49:46 -070048#define RAD_TO_DEG (180.0f / 3.14159265f)
49#define MIN_ANGLE 0.001f
50
Romain Guyf8773082012-07-12 18:01:00 -070051#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070052
Romain Guy713e1bb2012-10-16 18:44:09 -070053#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080054
Romain Guy9d5316e2010-06-24 19:30:36 -070055///////////////////////////////////////////////////////////////////////////////
56// Globals
57///////////////////////////////////////////////////////////////////////////////
58
Romain Guy889f8d12010-07-29 14:37:42 -070059/**
60 * Structure mapping Skia xfermodes to OpenGL blending factors.
61 */
62struct Blender {
63 SkXfermode::Mode mode;
64 GLenum src;
65 GLenum dst;
66}; // struct Blender
67
Romain Guy026c5e162010-06-28 17:12:22 -070068// In this array, the index of each Blender equals the value of the first
69// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
70static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070071 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
73 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
74 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
75 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
76 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
78 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
79 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
82 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050084 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070085 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070086};
Romain Guye4d01122010-06-16 18:44:05 -070087
Romain Guy87a76572010-09-13 18:11:21 -070088// This array contains the swapped version of each SkXfermode. For instance
89// this array's SrcOver blending mode is actually DstOver. You can refer to
90// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070091static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070092 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
94 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
95 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
96 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
98 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
101 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
102 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
104 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500105 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700106 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700107};
108
Romain Guyf6a11b82010-06-23 17:47:49 -0700109///////////////////////////////////////////////////////////////////////////////
110// Constructors/destructor
111///////////////////////////////////////////////////////////////////////////////
112
Romain Guy3bbacf22013-02-06 16:51:04 -0800113OpenGLRenderer::OpenGLRenderer():
114 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craikc3566d02013-02-04 16:16:33 -0800115 mDrawModifiers.mShader = NULL;
116 mDrawModifiers.mColorFilter = NULL;
117 mDrawModifiers.mHasShadow = false;
118 mDrawModifiers.mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700119
Romain Guyac670c02010-07-27 17:39:27 -0700120 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
121
Romain Guyae5575b2010-07-29 18:48:04 -0700122 mFirstSnapshot = new Snapshot;
Romain Guy87e2f7572012-09-24 11:37:12 -0700123
124 mScissorOptimizationDisabled = false;
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);
Romain Guy41308e22012-10-22 20:02:43 -0700196 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700197
Romain Guy11cb6422012-09-21 00:39:43 -0700198 updateLayers();
199
Romain Guydcfc8362013-01-03 13:08:57 -0800200 discardFramebuffer(left, top, right, bottom);
Romain Guy45e4c3d2012-09-11 17:17:07 -0700201
Romain Guyddf74372012-05-22 14:07:07 -0700202 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800203
Romain Guy54c1a642012-09-27 17:55:46 -0700204 // Functors break the tiling extension in pretty spectacular ways
205 // This ensures we don't use tiling when a functor is going to be
206 // invoked during the frame
207 mSuppressTiling = mCaches.hasRegisteredFunctors();
208
Romain Guy2b7028e2012-09-19 17:25:38 -0700209 mTilingSnapshot = mSnapshot;
Romain Guy57b52682012-09-20 17:38:46 -0700210 startTiling(mTilingSnapshot, 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) {
255 Rect* clip = mTilingSnapshot->clipRect;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800256 if (s->flags & Snapshot::kFlagFboTarget) {
257 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) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800409 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700410 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700411
Romain Guy8a4ac612012-07-17 17:32:48 -0700412 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800413 if (mDirtyClip) {
414 setScissorFromClip();
415 }
Romain Guyd643bb52011-03-01 14:55:21 -0800416
Romain Guy80911b82011-03-16 15:30:12 -0700417 Rect clip(*mSnapshot->clipRect);
418 clip.snapToPixelBoundaries();
419
Romain Guyd643bb52011-03-01 14:55:21 -0800420 // Since we don't know what the functor will draw, let's dirty
421 // tne entire clip region
422 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800423 dirtyLayerUnchecked(clip, getRegion());
424 }
Romain Guyd643bb52011-03-01 14:55:21 -0800425
Romain Guy08aa2cb2011-03-17 11:06:57 -0700426 DrawGlInfo info;
427 info.clipLeft = clip.left;
428 info.clipTop = clip.top;
429 info.clipRight = clip.right;
430 info.clipBottom = clip.bottom;
431 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700432 info.width = getSnapshot()->viewport.getWidth();
433 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700434 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700435
Chet Haase48659092012-05-31 15:21:51 -0700436 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800437
Romain Guy8f3b8e32012-03-27 16:33:45 -0700438 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700439 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800440 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700441
Chris Craik65924a32012-04-05 17:52:11 -0700442 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700443 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700444 }
Romain Guycabfcc12011-03-07 18:06:46 -0800445 }
446
Chet Haasedaf98e92011-01-10 14:10:36 -0800447 resume();
Romain Guy65549432012-03-26 16:45:05 -0700448 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800449}
450
Romain Guyf6a11b82010-06-23 17:47:49 -0700451///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700452// Debug
453///////////////////////////////////////////////////////////////////////////////
454
Romain Guy0f667532013-03-01 14:31:04 -0800455void OpenGLRenderer::eventMark(const char* name) const {
456 mCaches.eventMark(0, name);
457}
458
Romain Guy87e2f7572012-09-24 11:37:12 -0700459void OpenGLRenderer::startMark(const char* name) const {
460 mCaches.startMark(0, name);
461}
462
463void OpenGLRenderer::endMark() const {
464 mCaches.endMark();
465}
466
467void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
468 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
469 if (clear) {
470 mCaches.disableScissor();
471 mCaches.stencil.clear();
472 }
473 if (enable) {
474 mCaches.stencil.enableDebugWrite();
475 } else {
476 mCaches.stencil.disable();
477 }
478 }
479}
480
481void OpenGLRenderer::renderOverdraw() {
482 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
483 const Rect* clip = mTilingSnapshot->clipRect;
484
485 mCaches.enableScissor();
486 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
487 clip->right - clip->left, clip->bottom - clip->top);
488
489 mCaches.stencil.enableDebugTest(2);
490 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
491 mCaches.stencil.enableDebugTest(3);
492 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
493 mCaches.stencil.enableDebugTest(4);
494 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
495 mCaches.stencil.enableDebugTest(4, true);
496 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
497 mCaches.stencil.disable();
498 }
499}
500
501///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700502// Layers
503///////////////////////////////////////////////////////////////////////////////
504
505bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
506 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
507 OpenGLRenderer* renderer = layer->renderer;
508 Rect& dirty = layer->dirtyRect;
509
Romain Guy7c450aa2012-09-21 19:15:00 -0700510 if (inFrame) {
511 endTiling();
512 debugOverdraw(false, false);
513 }
Romain Guy11cb6422012-09-21 00:39:43 -0700514
515 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
516 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
517 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
518 renderer->finish();
519
520 if (inFrame) {
521 resumeAfterLayer();
522 startTiling(mSnapshot);
523 }
524
525 dirty.setEmpty();
526 layer->deferredUpdateScheduled = false;
527 layer->renderer = NULL;
528 layer->displayList = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -0800529 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700530
531 return true;
532 }
533
534 return false;
535}
536
537void OpenGLRenderer::updateLayers() {
538 int count = mLayerUpdates.size();
539 if (count > 0) {
540 startMark("Layer Updates");
541
542 // Note: it is very important to update the layers in reverse order
543 for (int i = count - 1; i >= 0; i--) {
544 Layer* layer = mLayerUpdates.itemAt(i);
545 updateLayer(layer, false);
546 mCaches.resourceCache.decrementRefcount(layer);
547 }
548 mLayerUpdates.clear();
549
550 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
551 endMark();
552 }
553}
554
555void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
556 if (layer) {
557 mLayerUpdates.push_back(layer);
558 mCaches.resourceCache.incrementRefcount(layer);
559 }
560}
561
562void OpenGLRenderer::clearLayerUpdates() {
563 size_t count = mLayerUpdates.size();
564 if (count > 0) {
565 mCaches.resourceCache.lock();
566 for (size_t i = 0; i < count; i++) {
567 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
568 }
569 mCaches.resourceCache.unlock();
570 mLayerUpdates.clear();
571 }
572}
573
574///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700575// State management
576///////////////////////////////////////////////////////////////////////////////
577
Romain Guybb9524b2010-06-22 18:56:38 -0700578int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700579 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700580}
581
582int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700583 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700584}
585
586void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700587 if (mSaveCount > 1) {
588 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700589 }
Romain Guybb9524b2010-06-22 18:56:38 -0700590}
591
592void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700593 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700594
Romain Guy8fb95422010-08-17 18:38:51 -0700595 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700596 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700597 }
Romain Guybb9524b2010-06-22 18:56:38 -0700598}
599
Romain Guy8aef54f2010-09-01 15:13:49 -0700600int OpenGLRenderer::saveSnapshot(int flags) {
601 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700602 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700603}
604
605bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700606 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700607 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700608 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700609
Romain Guybd6b79b2010-06-26 00:13:53 -0700610 sp<Snapshot> current = mSnapshot;
611 sp<Snapshot> previous = mSnapshot->previous;
612
Romain Guyeb993562010-10-05 18:14:38 -0700613 if (restoreOrtho) {
614 Rect& r = previous->viewport;
615 glViewport(r.left, r.top, r.right, r.bottom);
616 mOrthoMatrix.load(current->orthoMatrix);
617 }
618
Romain Guy8b55f372010-08-18 17:10:07 -0700619 mSaveCount--;
620 mSnapshot = previous;
621
Romain Guy2542d192010-08-18 11:47:12 -0700622 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700623 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700624 }
Romain Guy2542d192010-08-18 11:47:12 -0700625
Romain Guy5ec99242010-11-03 16:19:08 -0700626 if (restoreLayer) {
627 composeLayer(current, previous);
628 }
629
Romain Guy2542d192010-08-18 11:47:12 -0700630 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700631}
632
Romain Guyf6a11b82010-06-23 17:47:49 -0700633///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700634// Layers
635///////////////////////////////////////////////////////////////////////////////
636
637int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700638 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700639 const GLuint previousFbo = mSnapshot->fbo;
640 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700641
Romain Guyaf636eb2010-12-09 17:47:21 -0800642 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700643 int alpha = 255;
644 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700645
Romain Guye45362c2010-11-03 19:58:32 -0700646 if (p) {
647 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700648 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700649 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700650 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700651 }
Romain Guyd55a8612010-06-28 17:42:46 -0700652
Chet Haased48885a2012-08-28 17:43:28 -0700653 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700654 }
Romain Guyd55a8612010-06-28 17:42:46 -0700655
656 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700657}
658
659int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
660 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700661 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700662 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700663 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700664 SkPaint paint;
665 paint.setAlpha(alpha);
666 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700667 }
Romain Guyd55a8612010-06-28 17:42:46 -0700668}
Romain Guybd6b79b2010-06-26 00:13:53 -0700669
Romain Guy1c740bc2010-09-13 18:00:09 -0700670/**
671 * Layers are viewed by Skia are slightly different than layers in image editing
672 * programs (for instance.) When a layer is created, previously created layers
673 * and the frame buffer still receive every drawing command. For instance, if a
674 * layer is created and a shape intersecting the bounds of the layers and the
675 * framebuffer is draw, the shape will be drawn on both (unless the layer was
676 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
677 *
678 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
679 * texture. Unfortunately, this is inefficient as it requires every primitive to
680 * be drawn n + 1 times, where n is the number of active layers. In practice this
681 * means, for every primitive:
682 * - Switch active frame buffer
683 * - Change viewport, clip and projection matrix
684 * - Issue the drawing
685 *
686 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700687 * To avoid this, layers are implemented in a different way here, at least in the
688 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
689 * is set. When this flag is set we can redirect all drawing operations into a
690 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700691 *
692 * This implementation relies on the frame buffer being at least RGBA 8888. When
693 * a layer is created, only a texture is created, not an FBO. The content of the
694 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700695 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700696 * buffer and drawing continues as normal. This technique therefore treats the
697 * frame buffer as a scratch buffer for the layers.
698 *
699 * To compose the layers back onto the frame buffer, each layer texture
700 * (containing the original frame buffer data) is drawn as a simple quad over
701 * the frame buffer. The trick is that the quad is set as the composition
702 * destination in the blending equation, and the frame buffer becomes the source
703 * of the composition.
704 *
705 * Drawing layers with an alpha value requires an extra step before composition.
706 * An empty quad is drawn over the layer's region in the frame buffer. This quad
707 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
708 * quad is used to multiply the colors in the frame buffer. This is achieved by
709 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
710 * GL_ZERO, GL_SRC_ALPHA.
711 *
712 * Because glCopyTexImage2D() can be slow, an alternative implementation might
713 * be use to draw a single clipped layer. The implementation described above
714 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700715 *
716 * (1) The frame buffer is actually not cleared right away. To allow the GPU
717 * to potentially optimize series of calls to glCopyTexImage2D, the frame
718 * buffer is left untouched until the first drawing operation. Only when
719 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700720 */
Chet Haased48885a2012-08-28 17:43:28 -0700721bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
722 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700723 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700724 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700725
Romain Guyeb993562010-10-05 18:14:38 -0700726 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
727
Romain Guyf607bdc2010-09-10 19:20:06 -0700728 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700729 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700730 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700731 Rect untransformedBounds(bounds);
Romain Guy3b753822013-03-05 10:27:35 -0800732 currentTransform().mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700733
Chet Haased48885a2012-08-28 17:43:28 -0700734 // Layers only make sense if they are in the framebuffer's bounds
735 if (bounds.intersect(*mSnapshot->clipRect)) {
736 // We cannot work with sub-pixels in this case
737 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700738
Chet Haased48885a2012-08-28 17:43:28 -0700739 // When the layer is not an FBO, we may use glCopyTexImage so we
740 // need to make sure the layer does not extend outside the bounds
741 // of the framebuffer
742 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700743 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700744 } else if (fboLayer) {
745 clip.set(bounds);
746 mat4 inverse;
Romain Guy3b753822013-03-05 10:27:35 -0800747 inverse.loadInverse(currentTransform());
Chet Haased48885a2012-08-28 17:43:28 -0700748 inverse.mapRect(clip);
749 clip.snapToPixelBoundaries();
750 if (clip.intersect(untransformedBounds)) {
751 clip.translate(-left, -top);
752 bounds.set(untransformedBounds);
753 } else {
754 clip.setEmpty();
755 }
Romain Guyad37cd32011-03-15 11:12:25 -0700756 }
Chet Haased48885a2012-08-28 17:43:28 -0700757 } else {
758 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700759 }
Romain Guybf434112010-09-16 14:40:17 -0700760
Romain Guy746b7402010-10-26 16:27:31 -0700761 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700762 bounds.getHeight() > mCaches.maxTextureSize ||
763 (fboLayer && clip.isEmpty())) {
764 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700765 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700766 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700767 }
768
769 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700770 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700771 return false;
772 }
Romain Guyf18fd992010-07-08 11:45:51 -0700773
Romain Guya1d3c912011-12-13 14:55:06 -0800774 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700775 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700776 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700777 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700778 }
779
Romain Guy9ace8f52011-07-07 20:50:11 -0700780 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700781 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700782 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
783 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800784 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700785 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700786 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700787
Romain Guy8fb95422010-08-17 18:38:51 -0700788 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700789 mSnapshot->flags |= Snapshot::kFlagIsLayer;
790 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700791
Romain Guyeb993562010-10-05 18:14:38 -0700792 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700793 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700794 } else {
795 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700796 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800797 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700798 if (layer->isEmpty()) {
799 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700800 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700801 layer->getWidth(), layer->getHeight(), 0);
802 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800803 } else {
804 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700805 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800806 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700807
Romain Guy54be1cd2011-06-13 19:04:27 -0700808 // Enqueue the buffer coordinates to clear the corresponding region later
809 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700810 }
Romain Guyeb993562010-10-05 18:14:38 -0700811 }
Romain Guyf86ef572010-07-01 11:05:42 -0700812
Romain Guyd55a8612010-06-28 17:42:46 -0700813 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700814}
815
Chet Haased48885a2012-08-28 17:43:28 -0700816bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800817 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700818 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700819
Chet Haased48885a2012-08-28 17:43:28 -0700820 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800821 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
822 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700823 mSnapshot->fbo = layer->getFbo();
824 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
825 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
826 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
827 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700828 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700829
Romain Guy2b7028e2012-09-19 17:25:38 -0700830 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700831 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700832 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700833 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
834 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700835
836 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700837 if (layer->isEmpty()) {
838 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
839 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700840 }
841
842 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700843 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700844
Romain Guyf735c8e2013-01-31 17:45:55 -0800845 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700846
847 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700848 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800849 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700850 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700851 glClear(GL_COLOR_BUFFER_BIT);
852
853 dirtyClip();
854
855 // Change the ortho projection
856 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
857 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
858
859 return true;
860}
861
Romain Guy1c740bc2010-09-13 18:00:09 -0700862/**
863 * Read the documentation of createLayer() before doing anything in this method.
864 */
Romain Guy1d83e192010-08-17 11:37:00 -0700865void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
866 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000867 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700868 return;
869 }
870
Romain Guy8ce00302013-01-15 18:51:42 -0800871 Layer* layer = current->layer;
872 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700873 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700874
875 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700876 endTiling();
877
Romain Guye0aa84b2012-04-03 19:30:26 -0700878 // Detach the texture from the FBO
879 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800880
881 layer->removeFbo(false);
882
Romain Guyeb993562010-10-05 18:14:38 -0700883 // Unbind current FBO and restore previous one
884 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700885 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700886
887 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700888 }
889
Romain Guy9ace8f52011-07-07 20:50:11 -0700890 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700891 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700892 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700893 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700894 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700895 }
896
Romain Guy03750a02010-10-18 14:06:08 -0700897 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700898
Romain Guya1d3c912011-12-13 14:55:06 -0800899 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700900
Romain Guy5b3b3522010-10-27 18:57:51 -0700901 // When the layer is stored in an FBO, we can save a bit of fillrate by
902 // drawing only the dirty region
903 if (fboLayer) {
904 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700905 if (layer->getColorFilter()) {
906 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800907 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700908 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700909 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800910 resetColorFilter();
911 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700912 } else if (!rect.isEmpty()) {
913 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
914 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700915 }
Romain Guy8b55f372010-08-18 17:10:07 -0700916
Romain Guy746b7402010-10-26 16:27:31 -0700917 dirtyClip();
918
Romain Guyeb993562010-10-05 18:14:38 -0700919 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700920 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700921 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700922 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700923 }
924}
925
Romain Guyaa6c24c2011-04-28 18:40:04 -0700926void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700927 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700928
929 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700930 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700931 setupDrawWithTexture();
932 } else {
933 setupDrawWithExternalTexture();
934 }
935 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700936 setupDrawColor(alpha, alpha, alpha, alpha);
937 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700938 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700939 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700940 setupDrawPureColorUniforms();
941 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700942 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
943 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700944 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700945 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700946 }
Romain Guy3b753822013-03-05 10:27:35 -0800947 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700948 layer->getWidth() == (uint32_t) rect.getWidth() &&
949 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -0800950 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
951 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700952
Romain Guyd21b6e12011-11-30 20:21:23 -0800953 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700954 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
955 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800956 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700957 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
958 }
959 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700960 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
961
962 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
963
964 finishDrawTexture();
965}
966
Romain Guy5b3b3522010-10-27 18:57:51 -0700967void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700968 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700969 const Rect& texCoords = layer->texCoords;
970 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
971 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700972
Romain Guy9ace8f52011-07-07 20:50:11 -0700973 float x = rect.left;
974 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -0800975 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700976 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700977 layer->getHeight() == (uint32_t) rect.getHeight();
978
979 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700980 // When we're swapping, the layer is already in screen coordinates
981 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -0800982 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
983 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700984 }
985
Romain Guyd21b6e12011-11-30 20:21:23 -0800986 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700987 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800988 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700989 }
990
991 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
992 layer->getTexture(), layer->getAlpha() / 255.0f,
993 layer->getMode(), layer->isBlend(),
994 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
995 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700996
Romain Guyaa6c24c2011-04-28 18:40:04 -0700997 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
998 } else {
999 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1000 drawTextureLayer(layer, rect);
1001 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1002 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001003}
1004
1005void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001006 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001007 layer->setRegionAsRect();
1008
Romain Guy40667672011-03-18 14:34:03 -07001009 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -07001010
Romain Guy5b3b3522010-10-27 18:57:51 -07001011 layer->region.clear();
1012 return;
1013 }
1014
Romain Guy8a3957d2011-09-07 17:55:15 -07001015 // TODO: See LayerRenderer.cpp::generateMesh() for important
1016 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -08001017 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001018 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001019 const android::Rect* rects;
1020 Region safeRegion;
1021 if (CC_LIKELY(hasRectToRectTransform())) {
1022 rects = layer->region.getArray(&count);
1023 } else {
1024 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1025 rects = safeRegion.getArray(&count);
1026 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001027
Romain Guy9ace8f52011-07-07 20:50:11 -07001028 const float alpha = layer->getAlpha() / 255.0f;
1029 const float texX = 1.0f / float(layer->getWidth());
1030 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001031 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001032
Romain Guy8ce00302013-01-15 18:51:42 -08001033 setupDraw();
1034
1035 // We must get (and therefore bind) the region mesh buffer
1036 // after we setup drawing in case we need to mess with the
1037 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001038 TextureVertex* mesh = mCaches.getRegionMesh();
1039 GLsizei numQuads = 0;
1040
Romain Guy7230a742011-01-10 22:26:16 -08001041 setupDrawWithTexture();
1042 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001043 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001044 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001045 setupDrawProgram();
1046 setupDrawDirtyRegionsDisabled();
1047 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001048 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001049 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001050 if (currentTransform().isPureTranslate()) {
1051 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1052 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001053
Romain Guyd21b6e12011-11-30 20:21:23 -08001054 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001055 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1056 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001057 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001058 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1059 }
Romain Guy15bc6432011-12-13 13:11:32 -08001060 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001061
1062 for (size_t i = 0; i < count; i++) {
1063 const android::Rect* r = &rects[i];
1064
1065 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001066 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001067 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001068 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001069
1070 // TODO: Reject quads outside of the clip
1071 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1072 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1073 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1074 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1075
1076 numQuads++;
1077
1078 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1079 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1080 numQuads = 0;
1081 mesh = mCaches.getRegionMesh();
1082 }
1083 }
1084
1085 if (numQuads > 0) {
1086 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1087 }
1088
Romain Guy7230a742011-01-10 22:26:16 -08001089 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001090
1091#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001092 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001093#endif
1094
1095 layer->region.clear();
1096 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001097}
1098
Romain Guy3a3133d2011-02-01 22:59:58 -08001099void OpenGLRenderer::drawRegionRects(const Region& region) {
1100#if DEBUG_LAYERS_AS_REGIONS
1101 size_t count;
1102 const android::Rect* rects = region.getArray(&count);
1103
1104 uint32_t colors[] = {
1105 0x7fff0000, 0x7f00ff00,
1106 0x7f0000ff, 0x7fff00ff,
1107 };
1108
1109 int offset = 0;
1110 int32_t top = rects[0].top;
1111
1112 for (size_t i = 0; i < count; i++) {
1113 if (top != rects[i].top) {
1114 offset ^= 0x2;
1115 top = rects[i].top;
1116 }
1117
1118 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1119 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1120 SkXfermode::kSrcOver_Mode);
1121 }
1122#endif
1123}
1124
Romain Guy8ce00302013-01-15 18:51:42 -08001125void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1126 SkXfermode::Mode mode, bool dirty) {
1127 int count = 0;
1128 Vector<float> rects;
1129
1130 SkRegion::Iterator it(region);
1131 while (!it.done()) {
1132 const SkIRect& r = it.rect();
1133 rects.push(r.fLeft);
1134 rects.push(r.fTop);
1135 rects.push(r.fRight);
1136 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001137 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001138 it.next();
1139 }
1140
Romain Guy3bbacf22013-02-06 16:51:04 -08001141 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001142}
1143
Romain Guy5b3b3522010-10-27 18:57:51 -07001144void OpenGLRenderer::dirtyLayer(const float left, const float top,
1145 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001146 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001147 Rect bounds(left, top, right, bottom);
1148 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001149 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001150 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001151}
1152
1153void OpenGLRenderer::dirtyLayer(const float left, const float top,
1154 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001155 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001156 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001157 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001158 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001159}
1160
1161void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001162 if (bounds.intersect(*mSnapshot->clipRect)) {
1163 bounds.snapToPixelBoundaries();
1164 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1165 if (!dirty.isEmpty()) {
1166 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001167 }
1168 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001169}
1170
Romain Guy54be1cd2011-06-13 19:04:27 -07001171void OpenGLRenderer::clearLayerRegions() {
1172 const size_t count = mLayers.size();
1173 if (count == 0) return;
1174
1175 if (!mSnapshot->isIgnored()) {
1176 // Doing several glScissor/glClear here can negatively impact
1177 // GPUs with a tiler architecture, instead we draw quads with
1178 // the Clear blending mode
1179
1180 // The list contains bounds that have already been clipped
1181 // against their initial clip rect, and the current clip
1182 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001183 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001184
1185 Vertex mesh[count * 6];
1186 Vertex* vertex = mesh;
1187
1188 for (uint32_t i = 0; i < count; i++) {
1189 Rect* bounds = mLayers.itemAt(i);
1190
1191 Vertex::set(vertex++, bounds->left, bounds->bottom);
1192 Vertex::set(vertex++, bounds->left, bounds->top);
1193 Vertex::set(vertex++, bounds->right, bounds->top);
1194 Vertex::set(vertex++, bounds->left, bounds->bottom);
1195 Vertex::set(vertex++, bounds->right, bounds->top);
1196 Vertex::set(vertex++, bounds->right, bounds->bottom);
1197
1198 delete bounds;
1199 }
Romain Guye67307c2013-02-11 18:01:20 -08001200 // We must clear the list of dirty rects before we
1201 // call setupDraw() to prevent stencil setup to do
1202 // the same thing again
1203 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001204
1205 setupDraw(false);
1206 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1207 setupDrawBlending(true, SkXfermode::kClear_Mode);
1208 setupDrawProgram();
1209 setupDrawPureColorUniforms();
1210 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001211 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001212
Romain Guy54be1cd2011-06-13 19:04:27 -07001213 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001214
1215 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001216 } else {
1217 for (uint32_t i = 0; i < count; i++) {
1218 delete mLayers.itemAt(i);
1219 }
Romain Guye67307c2013-02-11 18:01:20 -08001220 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001221 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001222}
1223
Romain Guybd6b79b2010-06-26 00:13:53 -07001224///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001225// State Deferral
1226///////////////////////////////////////////////////////////////////////////////
1227
1228bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state) {
1229 const Rect& currentClip = *(mSnapshot->clipRect);
1230 const mat4& currentMatrix = *(mSnapshot->transform);
1231
1232 // state only has bounds initialized in local coordinates
1233 if (!state.mBounds.isEmpty()) {
1234 currentMatrix.mapRect(state.mBounds);
1235 if (!state.mBounds.intersect(currentClip)) {
1236 // quick rejected
1237 return true;
1238 }
Chris Craik5d116762013-02-19 17:49:31 -08001239 } else {
1240 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001241 }
1242
1243 state.mClip.set(currentClip);
1244 state.mMatrix.load(currentMatrix);
1245 state.mDrawModifiers = mDrawModifiers;
1246 return false;
1247}
1248
1249void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state) {
Romain Guy3b753822013-03-05 10:27:35 -08001250 currentTransform().load(state.mMatrix);
Chris Craikc3566d02013-02-04 16:16:33 -08001251
1252 // NOTE: a clip RECT will be saved and restored, but DeferredDisplayState doesn't support
1253 // complex clips. In the future, we should add support for deferral of operations clipped by
1254 // these. for now, we don't defer with complex clips (see OpenGLRenderer::disallowDeferral())
1255 mSnapshot->setClip(state.mClip.left, state.mClip.top, state.mClip.right, state.mClip.bottom);
1256 dirtyClip();
1257 mDrawModifiers = state.mDrawModifiers;
1258}
1259
1260///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001261// Transforms
1262///////////////////////////////////////////////////////////////////////////////
1263
1264void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy3b753822013-03-05 10:27:35 -08001265 currentTransform().translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001266}
1267
1268void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001269 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001270}
1271
1272void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001273 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001274}
1275
Romain Guy807daf72011-01-18 11:19:19 -08001276void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001277 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001278}
1279
Romain Guyf6a11b82010-06-23 17:47:49 -07001280void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001281 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001282 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001283 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001284 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001285 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001286}
1287
Chris Craikb98a0162013-02-21 11:30:22 -08001288bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001289 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001290}
1291
Romain Guyf6a11b82010-06-23 17:47:49 -07001292void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001293 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001294}
1295
1296void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001297 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001298 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001299 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001300 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001301}
1302
1303///////////////////////////////////////////////////////////////////////////////
1304// Clipping
1305///////////////////////////////////////////////////////////////////////////////
1306
Romain Guybb9524b2010-06-22 18:56:38 -07001307void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001308 Rect clip(*mSnapshot->clipRect);
1309 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001310
Romain Guy8a4ac612012-07-17 17:32:48 -07001311 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1312 clip.getWidth(), clip.getHeight())) {
1313 mDirtyClip = false;
1314 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001315}
1316
Romain Guy8ce00302013-01-15 18:51:42 -08001317void OpenGLRenderer::ensureStencilBuffer() {
1318 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1319 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1320 // just hope we have one when hasLayer() returns false.
1321 if (hasLayer()) {
1322 attachStencilBufferToLayer(mSnapshot->layer);
1323 }
1324}
1325
1326void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1327 // The layer's FBO is already bound when we reach this stage
1328 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001329 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1330 // is attached after we initiated tiling. We must turn it off,
1331 // attach the new render buffer then turn tiling back on
1332 endTiling();
1333
Romain Guy8d4aeb72013-02-12 16:08:55 -08001334 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001335 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001336 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001337
Romain Guyf735c8e2013-01-31 17:45:55 -08001338 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001339 }
1340}
1341
1342void OpenGLRenderer::setStencilFromClip() {
1343 if (!mCaches.debugOverdraw) {
1344 if (!mSnapshot->clipRegion->isEmpty()) {
1345 // NOTE: The order here is important, we must set dirtyClip to false
1346 // before any draw call to avoid calling back into this method
1347 mDirtyClip = false;
1348
1349 ensureStencilBuffer();
1350
1351 mCaches.stencil.enableWrite();
1352
1353 // Clear the stencil but first make sure we restrict drawing
1354 // to the region's bounds
1355 bool resetScissor = mCaches.enableScissor();
1356 if (resetScissor) {
1357 // The scissor was not set so we now need to update it
1358 setScissorFromClip();
1359 }
1360 mCaches.stencil.clear();
1361 if (resetScissor) mCaches.disableScissor();
1362
1363 // NOTE: We could use the region contour path to generate a smaller mesh
1364 // Since we are using the stencil we could use the red book path
1365 // drawing technique. It might increase bandwidth usage though.
1366
1367 // The last parameter is important: we are not drawing in the color buffer
1368 // so we don't want to dirty the current layer, if any
1369 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1370
1371 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001372
1373 // Draw the region used to generate the stencil if the appropriate debug
1374 // mode is enabled
1375 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1376 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1377 }
Romain Guy8ce00302013-01-15 18:51:42 -08001378 } else {
1379 mCaches.stencil.disable();
1380 }
1381 }
1382}
1383
Romain Guy9d5316e2010-06-24 19:30:36 -07001384const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001385 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001386}
1387
Romain Guy8a4ac612012-07-17 17:32:48 -07001388bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1389 if (mSnapshot->isIgnored()) {
1390 return true;
1391 }
1392
1393 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001394 currentTransform().mapRect(r);
Romain Guy8a4ac612012-07-17 17:32:48 -07001395 r.snapToPixelBoundaries();
1396
1397 Rect clipRect(*mSnapshot->clipRect);
1398 clipRect.snapToPixelBoundaries();
1399
1400 return !clipRect.intersects(r);
1401}
1402
Romain Guy35643dd2012-09-18 15:40:58 -07001403bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1404 Rect& transformed, Rect& clip) {
1405 if (mSnapshot->isIgnored()) {
1406 return true;
1407 }
1408
1409 transformed.set(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001410 currentTransform().mapRect(transformed);
Romain Guy35643dd2012-09-18 15:40:58 -07001411 transformed.snapToPixelBoundaries();
1412
1413 clip.set(*mSnapshot->clipRect);
1414 clip.snapToPixelBoundaries();
1415
1416 return !clip.intersects(transformed);
1417}
1418
Romain Guy672433d2013-01-04 19:05:13 -08001419bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1420 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001421 if (paint->getStyle() != SkPaint::kFill_Style) {
1422 float outset = paint->getStrokeWidth() * 0.5f;
1423 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1424 } else {
1425 return quickReject(left, top, right, bottom);
1426 }
1427}
1428
Romain Guyc7d53492010-06-25 13:41:57 -07001429bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001430 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001431 return true;
1432 }
1433
Romain Guy1d83e192010-08-17 11:37:00 -07001434 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001435 currentTransform().mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001436 r.snapToPixelBoundaries();
1437
1438 Rect clipRect(*mSnapshot->clipRect);
1439 clipRect.snapToPixelBoundaries();
1440
Romain Guy586cae32012-07-13 15:28:31 -07001441 bool rejected = !clipRect.intersects(r);
1442 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001443 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001444 }
1445
1446 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001447}
1448
Romain Guy8ce00302013-01-15 18:51:42 -08001449void OpenGLRenderer::debugClip() {
1450#if DEBUG_CLIP_REGIONS
1451 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1452 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1453 }
1454#endif
1455}
1456
Romain Guy079ba2c2010-07-16 14:12:24 -07001457bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001458 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001459 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1460 if (clipped) {
1461 dirtyClip();
1462 }
1463 return !mSnapshot->clipRect->isEmpty();
1464 }
1465
1466 SkPath path;
1467 path.addRect(left, top, right, bottom);
1468
1469 return clipPath(&path, op);
1470}
1471
1472bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1473 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001474 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001475
1476 SkPath transformed;
1477 path->transform(transform, &transformed);
1478
1479 SkRegion clip;
1480 if (!mSnapshot->clipRegion->isEmpty()) {
1481 clip.setRegion(*mSnapshot->clipRegion);
1482 } else {
1483 Rect* bounds = mSnapshot->clipRect;
1484 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1485 }
1486
1487 SkRegion region;
1488 region.setPath(transformed, clip);
1489
1490 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001491 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001492 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001493 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001494 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001495}
1496
Romain Guy735738c2012-12-03 12:34:51 -08001497bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001498 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1499 if (clipped) {
1500 dirtyClip();
1501 }
1502 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001503}
1504
Chet Haasea23eed82012-04-12 15:19:04 -07001505Rect* OpenGLRenderer::getClipRect() {
1506 return mSnapshot->clipRect;
1507}
1508
Romain Guyf6a11b82010-06-23 17:47:49 -07001509///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001510// Drawing commands
1511///////////////////////////////////////////////////////////////////////////////
1512
Romain Guy54be1cd2011-06-13 19:04:27 -07001513void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001514 // TODO: It would be best if we could do this before quickReject()
1515 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001516 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001517 // Make sure setScissor & setStencil happen at the beginning of
1518 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001519 if (mDirtyClip) {
1520 if (mCaches.scissorEnabled) {
1521 setScissorFromClip();
1522 }
Romain Guy8ce00302013-01-15 18:51:42 -08001523 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001524 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001525
Romain Guy70ca14e2010-12-13 18:24:33 -08001526 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001527
Romain Guy70ca14e2010-12-13 18:24:33 -08001528 mSetShaderColor = false;
1529 mColorSet = false;
1530 mColorA = mColorR = mColorG = mColorB = 0.0f;
1531 mTextureUnit = 0;
1532 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001533
1534 // Enable debug highlight when what we're about to draw is tested against
1535 // the stencil buffer and if stencil highlight debugging is on
1536 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1537 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1538 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001539}
1540
1541void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1542 mDescription.hasTexture = true;
1543 mDescription.hasAlpha8Texture = isAlpha8;
1544}
1545
Romain Guyff316ec2013-02-13 18:39:43 -08001546void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1547 mDescription.hasTexture = true;
1548 mDescription.hasColors = true;
1549 mDescription.hasAlpha8Texture = isAlpha8;
1550}
1551
Romain Guyaa6c24c2011-04-28 18:40:04 -07001552void OpenGLRenderer::setupDrawWithExternalTexture() {
1553 mDescription.hasExternalTexture = true;
1554}
1555
Romain Guy15bc6432011-12-13 13:11:32 -08001556void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001557 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001558}
1559
Chris Craik710f46d2012-09-17 17:25:49 -07001560void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001561 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001562}
1563
Romain Guyed6fcb02011-03-21 13:11:28 -07001564void OpenGLRenderer::setupDrawPoint(float pointSize) {
1565 mDescription.isPoint = true;
1566 mDescription.pointSize = pointSize;
1567}
1568
Romain Guy8d0d4782010-12-14 20:13:35 -08001569void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1570 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001571 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1572 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1573 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001574 mColorSet = true;
1575 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1576}
1577
Romain Guy86568192010-12-14 15:55:39 -08001578void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1579 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001580 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1581 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1582 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001583 mColorSet = true;
1584 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1585}
1586
Romain Guy41210632012-07-16 17:04:24 -07001587void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1588 mCaches.fontRenderer->describe(mDescription, paint);
1589}
1590
Romain Guy70ca14e2010-12-13 18:24:33 -08001591void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1592 mColorA = a;
1593 mColorR = r;
1594 mColorG = g;
1595 mColorB = b;
1596 mColorSet = true;
1597 mSetShaderColor = mDescription.setColor(r, g, b, a);
1598}
1599
1600void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001601 if (mDrawModifiers.mShader) {
1602 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001603 }
1604}
1605
1606void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001607 if (mDrawModifiers.mColorFilter) {
1608 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001609 }
1610}
1611
Romain Guyf09ef512011-05-27 11:43:46 -07001612void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1613 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1614 mColorA = 1.0f;
1615 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001616 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001617 }
1618}
1619
Romain Guy70ca14e2010-12-13 18:24:33 -08001620void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001621 // When the blending mode is kClear_Mode, we need to use a modulate color
1622 // argb=1,0,0,0
1623 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001624 bool blend = (mColorSet && mColorA < 1.0f) ||
1625 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1626 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001627}
1628
1629void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001630 // When the blending mode is kClear_Mode, we need to use a modulate color
1631 // argb=1,0,0,0
1632 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001633 blend |= (mColorSet && mColorA < 1.0f) ||
1634 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1635 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1636 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001637}
1638
1639void OpenGLRenderer::setupDrawProgram() {
1640 useProgram(mCaches.programCache.get(mDescription));
1641}
1642
1643void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1644 mTrackDirtyRegions = false;
1645}
1646
1647void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1648 bool ignoreTransform) {
1649 mModelView.loadTranslate(left, top, 0.0f);
1650 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001651 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1652 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001653 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001654 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001655 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1656 }
1657}
1658
Chet Haase8a5cc922011-04-26 07:28:09 -07001659void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001660 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001661}
1662
Romain Guy70ca14e2010-12-13 18:24:33 -08001663void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1664 bool ignoreTransform, bool ignoreModelView) {
1665 if (!ignoreModelView) {
1666 mModelView.loadTranslate(left, top, 0.0f);
1667 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001668 } else {
1669 mModelView.loadIdentity();
1670 }
Romain Guy86568192010-12-14 15:55:39 -08001671 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1672 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001673 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001674 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001675 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001676 }
1677 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001678 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001679 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1680 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001681}
1682
Romain Guyed6fcb02011-03-21 13:11:28 -07001683void OpenGLRenderer::setupDrawPointUniforms() {
1684 int slot = mCaches.currentProgram->getUniform("pointSize");
1685 glUniform1f(slot, mDescription.pointSize);
1686}
1687
Romain Guy70ca14e2010-12-13 18:24:33 -08001688void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001689 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001690 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1691 }
1692}
1693
Romain Guy86568192010-12-14 15:55:39 -08001694void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001695 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001696 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001697 }
1698}
1699
Romain Guy70ca14e2010-12-13 18:24:33 -08001700void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001701 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001702 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001703 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001704 }
Chris Craikc3566d02013-02-04 16:16:33 -08001705 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1706 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001707 }
1708}
1709
Romain Guy8d0d4782010-12-14 20:13:35 -08001710void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001711 if (mDrawModifiers.mShader) {
1712 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001713 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001714 }
1715}
1716
Romain Guy70ca14e2010-12-13 18:24:33 -08001717void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001718 if (mDrawModifiers.mColorFilter) {
1719 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001720 }
1721}
1722
Romain Guy41210632012-07-16 17:04:24 -07001723void OpenGLRenderer::setupDrawTextGammaUniforms() {
1724 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1725}
1726
Romain Guy70ca14e2010-12-13 18:24:33 -08001727void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001728 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001729 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001730 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001731}
1732
1733void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1734 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001735 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001736 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001737}
1738
Romain Guyaa6c24c2011-04-28 18:40:04 -07001739void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1740 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001741 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001742 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001743}
1744
Romain Guy8f0095c2011-05-02 17:24:22 -07001745void OpenGLRenderer::setupDrawTextureTransform() {
1746 mDescription.hasTextureTransform = true;
1747}
1748
1749void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001750 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1751 GL_FALSE, &transform.data[0]);
1752}
1753
Romain Guy70ca14e2010-12-13 18:24:33 -08001754void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001755 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001756 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001757 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001758 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001759 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001760 }
Romain Guyd71dd362011-12-12 19:03:35 -08001761
Chris Craikcb4d6002012-09-25 12:00:29 -07001762 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001763 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001764 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001765 }
1766
1767 mCaches.unbindIndicesBuffer();
1768}
1769
Romain Guyff316ec2013-02-13 18:39:43 -08001770void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1771 bool force = mCaches.unbindMeshBuffer();
1772 GLsizei stride = sizeof(ColorTextureVertex);
1773
1774 mCaches.bindPositionVertexPointer(force, vertices, stride);
1775 if (mCaches.currentProgram->texCoords >= 0) {
1776 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1777 }
1778 int slot = mCaches.currentProgram->getAttrib("colors");
1779 if (slot >= 0) {
1780 glEnableVertexAttribArray(slot);
1781 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1782 }
1783
1784 mCaches.unbindIndicesBuffer();
1785}
1786
Romain Guy15bc6432011-12-13 13:11:32 -08001787void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1788 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001789 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001790 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001791 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001792 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001793}
1794
Chet Haase5b0200b2011-04-13 17:58:08 -07001795void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001796 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001797 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001798 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001799}
1800
Romain Guy70ca14e2010-12-13 18:24:33 -08001801void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001802}
1803
1804///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001805// Drawing
1806///////////////////////////////////////////////////////////////////////////////
1807
Chris Craikc3566d02013-02-04 16:16:33 -08001808status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty, int32_t flags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001809 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1810 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001811 if (displayList && displayList->isRenderable()) {
Romain Guy0f667532013-03-01 14:31:04 -08001812 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chris Craikc3566d02013-02-04 16:16:33 -08001813 return displayList->replay(*this, dirty, flags, 0);
1814 }
1815
1816 DeferredDisplayList deferredList;
1817 return displayList->replay(*this, dirty, flags, 0, &deferredList);
Romain Guy0fe478e2010-11-08 12:08:41 -08001818 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001819
Romain Guy65549432012-03-26 16:45:05 -07001820 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001821}
1822
Chris Craikc3566d02013-02-04 16:16:33 -08001823void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07001824 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08001825 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07001826 }
1827}
1828
Romain Guya168d732011-03-18 16:50:13 -07001829void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1830 int alpha;
1831 SkXfermode::Mode mode;
1832 getAlphaAndMode(paint, &alpha, &mode);
1833
Romain Guy886b2752013-01-04 12:26:18 -08001834 int color = paint != NULL ? paint->getColor() : 0;
1835
Romain Guya168d732011-03-18 16:50:13 -07001836 float x = left;
1837 float y = top;
1838
Romain Guy886b2752013-01-04 12:26:18 -08001839 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1840
Romain Guya168d732011-03-18 16:50:13 -07001841 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08001842 if (currentTransform().isPureTranslate()) {
1843 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
1844 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001845 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001846
1847 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001848 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001849 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001850 }
1851
Romain Guy886b2752013-01-04 12:26:18 -08001852 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1853 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1854 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001855}
1856
Chet Haase48659092012-05-31 15:21:51 -07001857status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001858 const float right = left + bitmap->width();
1859 const float bottom = top + bitmap->height();
1860
1861 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001862 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001863 }
1864
Romain Guya1d3c912011-12-13 14:55:06 -08001865 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001866 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001867 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001868 const AutoTexture autoCleanup(texture);
1869
Romain Guy211370f2012-02-01 16:10:55 -08001870 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001871 drawAlphaBitmap(texture, left, top, paint);
1872 } else {
1873 drawTextureRect(left, top, right, bottom, texture, paint);
1874 }
Chet Haase48659092012-05-31 15:21:51 -07001875
1876 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001877}
1878
Chet Haase48659092012-05-31 15:21:51 -07001879status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001880 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1881 const mat4 transform(*matrix);
1882 transform.mapRect(r);
1883
Romain Guy6926c722010-07-12 20:20:03 -07001884 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001885 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001886 }
1887
Romain Guya1d3c912011-12-13 14:55:06 -08001888 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001889 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001890 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001891 const AutoTexture autoCleanup(texture);
1892
Romain Guy5b3b3522010-10-27 18:57:51 -07001893 // This could be done in a cheaper way, all we need is pass the matrix
1894 // to the vertex shader. The save/restore is a bit overkill.
1895 save(SkCanvas::kMatrix_SaveFlag);
1896 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08001897 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1898 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
1899 } else {
1900 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1901 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001902 restore();
Chet Haase48659092012-05-31 15:21:51 -07001903
1904 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001905}
1906
Chet Haase48659092012-05-31 15:21:51 -07001907status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001908 const float right = left + bitmap->width();
1909 const float bottom = top + bitmap->height();
1910
1911 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001912 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001913 }
1914
1915 mCaches.activeTexture(0);
1916 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1917 const AutoTexture autoCleanup(texture);
1918
Romain Guy886b2752013-01-04 12:26:18 -08001919 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1920 drawAlphaBitmap(texture, left, top, paint);
1921 } else {
1922 drawTextureRect(left, top, right, bottom, texture, paint);
1923 }
Chet Haase48659092012-05-31 15:21:51 -07001924
1925 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001926}
1927
Chet Haase48659092012-05-31 15:21:51 -07001928status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001929 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001930 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001931 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001932 }
1933
Romain Guyb18d2d02011-02-10 15:52:54 -08001934 float left = FLT_MAX;
1935 float top = FLT_MAX;
1936 float right = FLT_MIN;
1937 float bottom = FLT_MIN;
1938
Romain Guya92bb4d2012-10-16 11:08:44 -07001939 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001940
Romain Guyff316ec2013-02-13 18:39:43 -08001941 ColorTextureVertex mesh[count];
1942 ColorTextureVertex* vertex = mesh;
1943
1944 bool cleanupColors = false;
1945 if (!colors) {
1946 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
1947 colors = new int[colorsCount];
1948 memset(colors, 0xff, colorsCount * sizeof(int));
1949 cleanupColors = true;
1950 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001951
Romain Guy5a7b4662011-01-20 19:09:30 -08001952 for (int32_t y = 0; y < meshHeight; y++) {
1953 for (int32_t x = 0; x < meshWidth; x++) {
1954 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1955
1956 float u1 = float(x) / meshWidth;
1957 float u2 = float(x + 1) / meshWidth;
1958 float v1 = float(y) / meshHeight;
1959 float v2 = float(y + 1) / meshHeight;
1960
1961 int ax = i + (meshWidth + 1) * 2;
1962 int ay = ax + 1;
1963 int bx = i;
1964 int by = bx + 1;
1965 int cx = i + 2;
1966 int cy = cx + 1;
1967 int dx = i + (meshWidth + 1) * 2 + 2;
1968 int dy = dx + 1;
1969
Romain Guyff316ec2013-02-13 18:39:43 -08001970 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1971 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
1972 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08001973
Romain Guyff316ec2013-02-13 18:39:43 -08001974 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1975 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
1976 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08001977
Romain Guya92bb4d2012-10-16 11:08:44 -07001978 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1979 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1980 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1981 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001982 }
1983 }
1984
Romain Guya92bb4d2012-10-16 11:08:44 -07001985 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08001986 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07001987 return DrawGlInfo::kStatusDone;
1988 }
1989
1990 mCaches.activeTexture(0);
1991 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guyff316ec2013-02-13 18:39:43 -08001992 if (!texture) {
1993 if (cleanupColors) delete[] colors;
1994 return DrawGlInfo::kStatusDone;
1995 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001996 const AutoTexture autoCleanup(texture);
1997
1998 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1999 texture->setFilter(FILTER(paint), true);
2000
2001 int alpha;
2002 SkXfermode::Mode mode;
2003 getAlphaAndMode(paint, &alpha, &mode);
2004
Romain Guyff316ec2013-02-13 18:39:43 -08002005 float a = alpha / 255.0f;
2006
Romain Guya92bb4d2012-10-16 11:08:44 -07002007 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002008 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002009 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002010
Romain Guyff316ec2013-02-13 18:39:43 -08002011 setupDraw();
2012 setupDrawWithTextureAndColor();
2013 setupDrawColor(a, a, a, a);
2014 setupDrawColorFilter();
2015 setupDrawBlending(true, mode, false);
2016 setupDrawProgram();
2017 setupDrawDirtyRegionsDisabled();
2018 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2019 setupDrawTexture(texture->id);
2020 setupDrawPureColorUniforms();
2021 setupDrawColorFilterUniforms();
2022 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2023
2024 glDrawArrays(GL_TRIANGLES, 0, count);
2025
2026 finishDrawTexture();
2027
2028 int slot = mCaches.currentProgram->getAttrib("colors");
2029 if (slot >= 0) {
2030 glDisableVertexAttribArray(slot);
2031 }
2032
2033 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002034
2035 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002036}
2037
Chet Haase48659092012-05-31 15:21:51 -07002038status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002039 float srcLeft, float srcTop, float srcRight, float srcBottom,
2040 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002041 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002042 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002043 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002044 }
2045
Romain Guya1d3c912011-12-13 14:55:06 -08002046 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002047 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002048 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002049 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002050
Romain Guy8ba548f2010-06-30 19:21:21 -07002051 const float width = texture->width;
2052 const float height = texture->height;
2053
Romain Guy68169722011-08-22 17:33:33 -07002054 const float u1 = fmax(0.0f, srcLeft / width);
2055 const float v1 = fmax(0.0f, srcTop / height);
2056 const float u2 = fmin(1.0f, srcRight / width);
2057 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07002058
Romain Guy03750a02010-10-18 14:06:08 -07002059 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002060 resetDrawTextureTexCoords(u1, v1, u2, v2);
2061
Romain Guy03750a02010-10-18 14:06:08 -07002062 int alpha;
2063 SkXfermode::Mode mode;
2064 getAlphaAndMode(paint, &alpha, &mode);
2065
Romain Guyd21b6e12011-11-30 20:21:23 -08002066 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2067
Romain Guy886b2752013-01-04 12:26:18 -08002068 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2069 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002070
Romain Guy886b2752013-01-04 12:26:18 -08002071 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2072 // Apply a scale transform on the canvas only when a shader is in use
2073 // Skia handles the ratio between the dst and src rects as a scale factor
2074 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002075 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002076 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002077
Romain Guy3b753822013-03-05 10:27:35 -08002078 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2079 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2080 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002081
2082 dstRight = x + (dstRight - dstLeft);
2083 dstBottom = y + (dstBottom - dstTop);
2084
2085 dstLeft = x;
2086 dstTop = y;
2087
2088 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2089 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002090 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002091 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002092 }
2093
2094 if (CC_UNLIKELY(useScaleTransform)) {
2095 save(SkCanvas::kMatrix_SaveFlag);
2096 translate(dstLeft, dstTop);
2097 scale(scaleX, scaleY);
2098
2099 dstLeft = 0.0f;
2100 dstTop = 0.0f;
2101
2102 dstRight = srcRight - srcLeft;
2103 dstBottom = srcBottom - srcTop;
2104 }
2105
2106 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2107 int color = paint ? paint->getColor() : 0;
2108 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2109 texture->id, paint != NULL, color, alpha, mode,
2110 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2111 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2112 } else {
2113 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2114 texture->id, alpha / 255.0f, mode, texture->blend,
2115 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2116 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2117 }
2118
2119 if (CC_UNLIKELY(useScaleTransform)) {
2120 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002121 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002122
2123 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002124
2125 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002126}
2127
Chet Haase48659092012-05-31 15:21:51 -07002128status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07002129 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07002130 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002131 int alpha;
2132 SkXfermode::Mode mode;
2133 getAlphaAndModeDirect(paint, &alpha, &mode);
2134
2135 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2136 left, top, right, bottom, alpha, mode);
2137}
2138
2139status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2140 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2141 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002142 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002143 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002144 }
2145
Romain Guybe6f9dc2012-07-16 12:41:17 -07002146 alpha *= mSnapshot->alpha;
2147
Romain Guy2728f962010-10-08 18:36:15 -07002148 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002149 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002150
Romain Guy211370f2012-02-01 16:10:55 -08002151 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002152 mCaches.activeTexture(0);
2153 Texture* texture = mCaches.textureCache.get(bitmap);
2154 if (!texture) return DrawGlInfo::kStatusDone;
2155 const AutoTexture autoCleanup(texture);
2156 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2157 texture->setFilter(GL_LINEAR, true);
2158
Romain Guy3b753822013-03-05 10:27:35 -08002159 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002160 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002161 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002162 const float offsetX = left + currentTransform().getTranslateX();
2163 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002164 const size_t count = mesh->quads.size();
2165 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002166 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002167 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002168 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2169 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2170 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002171 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002172 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002173 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002174 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002175 }
2176 }
2177
Romain Guy211370f2012-02-01 16:10:55 -08002178 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002179 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2180 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002181
2182 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2183 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2184 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2185 true, !mesh->hasEmptyQuads);
2186 } else {
2187 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2188 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2189 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2190 true, !mesh->hasEmptyQuads);
2191 }
Romain Guy054dc182010-10-15 17:55:25 -07002192 }
Chet Haase48659092012-05-31 15:21:51 -07002193
2194 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002195}
2196
Chris Craik65cd6122012-12-10 17:56:27 -08002197status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2198 bool useOffset) {
2199 if (!vertexBuffer.getSize()) {
2200 // no vertices to draw
2201 return DrawGlInfo::kStatusDone;
2202 }
2203
Chris Craikcb4d6002012-09-25 12:00:29 -07002204 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002205 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2206 bool isAA = paint->isAntiAlias();
2207
Chris Craik710f46d2012-09-17 17:25:49 -07002208 setupDraw();
2209 setupDrawNoTexture();
2210 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002211 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2212 setupDrawColorFilter();
2213 setupDrawShader();
2214 setupDrawBlending(isAA, mode);
2215 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002216 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002217 setupDrawColorUniforms();
2218 setupDrawColorFilterUniforms();
2219 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002220
Chris Craik710f46d2012-09-17 17:25:49 -07002221 void* vertices = vertexBuffer.getBuffer();
2222 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002223 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002224 mCaches.resetTexCoordsVertexPointer();
2225 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002226
Chris Craik710f46d2012-09-17 17:25:49 -07002227 int alphaSlot = -1;
2228 if (isAA) {
2229 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2230 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002231
Chris Craik710f46d2012-09-17 17:25:49 -07002232 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002233 glEnableVertexAttribArray(alphaSlot);
2234 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002235 }
Romain Guy04299382012-07-18 17:15:41 -07002236
Chris Craik710f46d2012-09-17 17:25:49 -07002237 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002238
Chris Craik710f46d2012-09-17 17:25:49 -07002239 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002240 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002241 }
Chris Craik65cd6122012-12-10 17:56:27 -08002242
2243 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002244}
2245
2246/**
Chris Craik65cd6122012-12-10 17:56:27 -08002247 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2248 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2249 * screen space in all directions. However, instead of using a fragment shader to compute the
2250 * translucency of the color from its position, we simply use a varying parameter to define how far
2251 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2252 *
2253 * Doesn't yet support joins, caps, or path effects.
2254 */
2255status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2256 VertexBuffer vertexBuffer;
2257 // TODO: try clipping large paths to viewport
2258 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2259
2260 SkRect bounds = path.getBounds();
2261 PathTessellator::expandBoundsForStroke(bounds, paint, false);
Romain Guy3b753822013-03-05 10:27:35 -08002262 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chris Craik65cd6122012-12-10 17:56:27 -08002263
2264 return drawVertexBuffer(vertexBuffer, paint);
2265}
2266
2267/**
2268 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2269 * and additional geometry for defining an alpha slope perimeter.
2270 *
2271 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2272 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2273 * in-shader alpha region, but found it to be taxing on some GPUs.
2274 *
2275 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2276 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002277 */
Chet Haase48659092012-05-31 15:21:51 -07002278status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002279 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002280
Chris Craik65cd6122012-12-10 17:56:27 -08002281 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002282
Chris Craik65cd6122012-12-10 17:56:27 -08002283 VertexBuffer buffer;
2284 SkRect bounds;
2285 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002286
2287 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2288 return DrawGlInfo::kStatusDone;
2289 }
2290
Romain Guy3b753822013-03-05 10:27:35 -08002291 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002292
Chris Craik65cd6122012-12-10 17:56:27 -08002293 bool useOffset = !paint->isAntiAlias();
2294 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002295}
2296
Chet Haase48659092012-05-31 15:21:51 -07002297status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2298 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002299
2300 // TODO: The paint's cap style defines whether the points are square or circular
2301 // TODO: Handle AA for round points
2302
Chet Haase5b0200b2011-04-13 17:58:08 -07002303 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002304 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002305 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002306 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002307 if (isHairLine) {
2308 // Now that we know it's hairline, we can set the effective width, to be used later
2309 strokeWidth = 1.0f;
2310 }
2311 const float halfWidth = strokeWidth / 2;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002312
Romain Guyed6fcb02011-03-21 13:11:28 -07002313 int alpha;
2314 SkXfermode::Mode mode;
2315 getAlphaAndMode(paint, &alpha, &mode);
2316
2317 int verticesCount = count >> 1;
2318 int generatedVerticesCount = 0;
2319
2320 TextureVertex pointsData[verticesCount];
2321 TextureVertex* vertex = &pointsData[0];
2322
Romain Guy04299382012-07-18 17:15:41 -07002323 // TODO: We should optimize this method to not generate vertices for points
2324 // that lie outside of the clip.
2325 mCaches.enableScissor();
2326
Romain Guyed6fcb02011-03-21 13:11:28 -07002327 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002328 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002329 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002330 setupDrawColor(paint->getColor(), alpha);
2331 setupDrawColorFilter();
2332 setupDrawShader();
2333 setupDrawBlending(mode);
2334 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002335 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002336 setupDrawColorUniforms();
2337 setupDrawColorFilterUniforms();
2338 setupDrawPointUniforms();
2339 setupDrawShaderIdentityUniforms();
2340 setupDrawMesh(vertex);
2341
2342 for (int i = 0; i < count; i += 2) {
2343 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2344 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002345
Chet Haase8a5cc922011-04-26 07:28:09 -07002346 float left = points[i] - halfWidth;
2347 float right = points[i] + halfWidth;
2348 float top = points[i + 1] - halfWidth;
2349 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002350
Romain Guy3b753822013-03-05 10:27:35 -08002351 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyed6fcb02011-03-21 13:11:28 -07002352 }
2353
2354 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002355
2356 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002357}
2358
Chet Haase48659092012-05-31 15:21:51 -07002359status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002360 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002361 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002362
Romain Guyae88e5e2010-10-22 17:49:18 -07002363 Rect& clip(*mSnapshot->clipRect);
2364 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002365
Romain Guy3d58c032010-07-14 16:34:53 -07002366 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002367
2368 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002369}
Romain Guy9d5316e2010-06-24 19:30:36 -07002370
Chet Haase48659092012-05-31 15:21:51 -07002371status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2372 SkPaint* paint) {
2373 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002374 const AutoTexture autoCleanup(texture);
2375
2376 const float x = left + texture->left - texture->offset;
2377 const float y = top + texture->top - texture->offset;
2378
2379 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002380
2381 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002382}
2383
Chet Haase48659092012-05-31 15:21:51 -07002384status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002385 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002386 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002387 return DrawGlInfo::kStatusDone;
2388 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002389
Chris Craikcb4d6002012-09-25 12:00:29 -07002390 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002391 mCaches.activeTexture(0);
2392 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2393 right - left, bottom - top, rx, ry, p);
2394 return drawShape(left, top, texture, p);
2395 }
2396
2397 SkPath path;
2398 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002399 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2400 float outset = p->getStrokeWidth() / 2;
2401 rect.outset(outset, outset);
2402 rx += outset;
2403 ry += outset;
2404 }
Chris Craik710f46d2012-09-17 17:25:49 -07002405 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002406 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002407}
2408
Chris Craik710f46d2012-09-17 17:25:49 -07002409status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002410 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2411 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002412 return DrawGlInfo::kStatusDone;
2413 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002414 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002415 mCaches.activeTexture(0);
2416 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2417 return drawShape(x - radius, y - radius, texture, p);
2418 }
2419
2420 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002421 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2422 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2423 } else {
2424 path.addCircle(x, y, radius);
2425 }
Chris Craik65cd6122012-12-10 17:56:27 -08002426 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002427}
Romain Guy01d58e42011-01-19 21:54:02 -08002428
Chet Haase48659092012-05-31 15:21:51 -07002429status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002430 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002431 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002432 return DrawGlInfo::kStatusDone;
2433 }
Romain Guy01d58e42011-01-19 21:54:02 -08002434
Chris Craikcb4d6002012-09-25 12:00:29 -07002435 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002436 mCaches.activeTexture(0);
2437 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2438 return drawShape(left, top, texture, p);
2439 }
2440
2441 SkPath path;
2442 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002443 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2444 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2445 }
Chris Craik710f46d2012-09-17 17:25:49 -07002446 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002447 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002448}
2449
Chet Haase48659092012-05-31 15:21:51 -07002450status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002451 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
2452 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
2453 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002454 }
2455
Chris Craik780c1282012-10-04 14:10:49 -07002456 if (fabs(sweepAngle) >= 360.0f) {
2457 return drawOval(left, top, right, bottom, p);
2458 }
2459
2460 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002461 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002462 mCaches.activeTexture(0);
2463 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2464 startAngle, sweepAngle, useCenter, p);
2465 return drawShape(left, top, texture, p);
2466 }
2467
2468 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2469 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2470 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2471 }
2472
2473 SkPath path;
2474 if (useCenter) {
2475 path.moveTo(rect.centerX(), rect.centerY());
2476 }
2477 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2478 if (useCenter) {
2479 path.close();
2480 }
Chris Craik65cd6122012-12-10 17:56:27 -08002481 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002482}
2483
Romain Guycf8675e2012-10-02 12:32:25 -07002484// See SkPaintDefaults.h
2485#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2486
Chet Haase48659092012-05-31 15:21:51 -07002487status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002488 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002489 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002490 }
2491
Chris Craik710f46d2012-09-17 17:25:49 -07002492 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002493 // only fill style is supported by drawConvexPath, since others have to handle joins
2494 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2495 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2496 mCaches.activeTexture(0);
2497 const PathTexture* texture =
2498 mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2499 return drawShape(left, top, texture, p);
2500 }
2501
2502 SkPath path;
2503 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2504 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2505 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2506 }
2507 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002508 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002509 }
2510
Romain Guy3b753822013-03-05 10:27:35 -08002511 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002512 SkPath path;
2513 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002514 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002515 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002516 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002517 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002518 }
Romain Guyc7d53492010-06-25 13:41:57 -07002519}
Romain Guy9d5316e2010-06-24 19:30:36 -07002520
Raph Levien416a8472012-07-19 22:48:17 -07002521void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2522 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2523 float x, float y) {
2524 mCaches.activeTexture(0);
2525
2526 // NOTE: The drop shadow will not perform gamma correction
2527 // if shader-based correction is enabled
2528 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2529 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002530 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Raph Levien416a8472012-07-19 22:48:17 -07002531 const AutoTexture autoCleanup(shadow);
2532
Chris Craikc3566d02013-02-04 16:16:33 -08002533 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2534 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002535
Chris Craikc3566d02013-02-04 16:16:33 -08002536 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2537 int shadowColor = mDrawModifiers.mShadowColor;
2538 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002539 shadowColor = 0xffffffff;
2540 }
2541
2542 setupDraw();
2543 setupDrawWithTexture(true);
2544 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2545 setupDrawColorFilter();
2546 setupDrawShader();
2547 setupDrawBlending(true, mode);
2548 setupDrawProgram();
2549 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2550 setupDrawTexture(shadow->id);
2551 setupDrawPureColorUniforms();
2552 setupDrawColorFilterUniforms();
2553 setupDrawShaderUniforms();
2554 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2555
2556 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2557}
2558
Romain Guy768bffc2013-02-27 13:50:45 -08002559bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2560 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2561 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2562}
2563
Chet Haase48659092012-05-31 15:21:51 -07002564status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002565 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002566 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002567 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002568 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002569
Romain Guy671d6cf2012-01-18 12:39:17 -08002570 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002571 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002572 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002573 }
2574
2575 float x = 0.0f;
2576 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002577 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002578 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002579 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2580 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002581 }
2582
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002583 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002584 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002585
2586 int alpha;
2587 SkXfermode::Mode mode;
2588 getAlphaAndMode(paint, &alpha, &mode);
2589
Chris Craikc3566d02013-02-04 16:16:33 -08002590 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002591 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2592 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002593 }
2594
Romain Guy671d6cf2012-01-18 12:39:17 -08002595 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002596 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002597 if (pureTranslate && !linearFilter) {
2598 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2599 }
2600
2601 mCaches.activeTexture(0);
2602 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002603 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002604 setupDrawDirtyRegionsDisabled();
2605 setupDrawWithTexture(true);
2606 setupDrawAlpha8Color(paint->getColor(), alpha);
2607 setupDrawColorFilter();
2608 setupDrawShader();
2609 setupDrawBlending(true, mode);
2610 setupDrawProgram();
2611 setupDrawModelView(x, y, x, y, pureTranslate, true);
2612 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2613 setupDrawPureColorUniforms();
2614 setupDrawColorFilterUniforms();
2615 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002616 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002617
2618 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2619 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2620
Romain Guy211370f2012-02-01 16:10:55 -08002621 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002622
2623 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2624 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002625 if (hasActiveLayer) {
2626 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002627 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002628 }
2629 dirtyLayerUnchecked(bounds, getRegion());
2630 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002631 }
Chet Haase48659092012-05-31 15:21:51 -07002632
2633 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002634}
2635
Romain Guy624234f2013-03-05 16:43:31 -08002636mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2637 mat4 fontTransform;
2638 if (CC_LIKELY(transform.isPureTranslate())) {
2639 fontTransform = mat4::identity();
2640 } else {
2641 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002642 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002643 } else {
2644 float sx, sy;
2645 currentTransform().decomposeScale(sx, sy);
2646 fontTransform.loadScale(sx, sy, 1.0f);
2647 }
2648 }
2649 return fontTransform;
2650}
2651
Romain Guyc2525952012-07-27 16:41:22 -07002652status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002653 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy768bffc2013-02-27 13:50:45 -08002654 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002655 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002656 }
2657
Chet Haasea1cff502012-02-21 13:43:44 -08002658 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002659 switch (paint->getTextAlign()) {
2660 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002661 x -= length / 2.0f;
2662 break;
2663 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002664 x -= length;
2665 break;
2666 default:
2667 break;
2668 }
2669
Romain Guycac5fd32011-12-01 20:08:50 -08002670 SkPaint::FontMetrics metrics;
2671 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002672 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002673 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002674 }
2675
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002676 const float oldX = x;
2677 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002678
2679 const mat4& transform = currentTransform();
2680 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002681
Romain Guy211370f2012-02-01 16:10:55 -08002682 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002683 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2684 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002685 }
2686
Romain Guy86568192010-12-14 15:55:39 -08002687 int alpha;
2688 SkXfermode::Mode mode;
2689 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002690
Romain Guyc74f45a2013-02-26 19:10:14 -08002691 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2692
Chris Craikc3566d02013-02-04 16:16:33 -08002693 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002694 fontRenderer.setFont(paint, mat4::identity());
2695 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2696 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002697 }
2698
Romain Guy19d4dd82013-03-04 11:14:26 -08002699 const bool hasActiveLayer = hasLayer();
2700
Romain Guy624234f2013-03-05 16:43:31 -08002701 // We only pass a partial transform to the font renderer. That partial
2702 // matrix defines how glyphs are rasterized. Typically we want glyphs
2703 // to be rasterized at their final size on screen, which means the partial
2704 // matrix needs to take the scale factor into account.
2705 // When a partial matrix is used to transform glyphs during rasterization,
2706 // the mesh is generated with the inverse transform (in the case of scale,
2707 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2708 // apply the full transform matrix at draw time in the vertex shader.
2709 // Applying the full matrix in the shader is the easiest way to handle
2710 // rotation and perspective and allows us to always generated quads in the
2711 // font renderer which greatly simplifies the code, clipping in particular.
2712 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002713 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002714
Romain Guy6620c6d2010-12-06 18:07:02 -08002715 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002716 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -07002717
Romain Guy16c88082012-06-11 16:03:47 -07002718 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002719 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002720 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002721 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002722 setupDrawDirtyRegionsDisabled();
2723 setupDrawWithTexture(true);
2724 setupDrawAlpha8Color(paint->getColor(), alpha);
2725 setupDrawColorFilter();
2726 setupDrawShader();
2727 setupDrawBlending(true, mode);
2728 setupDrawProgram();
Romain Guy624234f2013-03-05 16:43:31 -08002729 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002730 // See comment above; the font renderer must use texture unit 0
2731 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002732 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2733 setupDrawPureColorUniforms();
2734 setupDrawColorFilterUniforms();
Romain Guy624234f2013-03-05 16:43:31 -08002735 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002736 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002737
Romain Guy624234f2013-03-05 16:43:31 -08002738 // TODO: Implement better clipping for scaled/rotated text
2739 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Romain Guy5b3b3522010-10-27 18:57:51 -07002740 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2741
Raph Levien996e57c2012-07-23 15:22:52 -07002742 bool status;
Romain Guya3dc55f2012-09-28 13:55:44 -07002743 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002744 SkPaint paintCopy(*paint);
2745 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2746 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002747 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002748 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002749 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002750 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002751 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002752
2753 if (status && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002754 if (!pureTranslate) {
2755 transform.mapRect(bounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002756 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002757 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002758 }
Romain Guy694b5192010-07-21 21:33:20 -07002759
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002760 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002761
2762 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002763}
2764
Chet Haase48659092012-05-31 15:21:51 -07002765status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002766 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002767 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002768 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002769 }
2770
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002771 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002772 fontRenderer.setFont(paint, mat4::identity());
Romain Guy03d58522012-02-24 17:54:07 -08002773
2774 int alpha;
2775 SkXfermode::Mode mode;
2776 getAlphaAndMode(paint, &alpha, &mode);
2777
2778 mCaches.activeTexture(0);
2779 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002780 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002781 setupDrawDirtyRegionsDisabled();
2782 setupDrawWithTexture(true);
2783 setupDrawAlpha8Color(paint->getColor(), alpha);
2784 setupDrawColorFilter();
2785 setupDrawShader();
2786 setupDrawBlending(true, mode);
2787 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002788 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002789 setupDrawTexture(fontRenderer.getTexture(true));
2790 setupDrawPureColorUniforms();
2791 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002792 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002793 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002794
Romain Guy97771732012-02-28 18:17:02 -08002795 const Rect* clip = &mSnapshot->getLocalClip();
2796 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 -08002797
Romain Guy97771732012-02-28 18:17:02 -08002798 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002799
2800 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2801 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002802 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08002803 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002804 dirtyLayerUnchecked(bounds, getRegion());
2805 }
Romain Guy97771732012-02-28 18:17:02 -08002806 }
Chet Haase48659092012-05-31 15:21:51 -07002807
2808 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002809}
2810
Chet Haase48659092012-05-31 15:21:51 -07002811status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2812 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002813
Romain Guya1d3c912011-12-13 14:55:06 -08002814 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002815
Romain Guyfb8b7632010-08-23 21:05:08 -07002816 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002817 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002818 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002819
Romain Guy8b55f372010-08-18 17:10:07 -07002820 const float x = texture->left - texture->offset;
2821 const float y = texture->top - texture->offset;
2822
Romain Guy01d58e42011-01-19 21:54:02 -08002823 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002824
2825 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002826}
2827
Chet Haase48659092012-05-31 15:21:51 -07002828status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002829 if (!layer) {
2830 return DrawGlInfo::kStatusDone;
2831 }
2832
Romain Guyb2e2f242012-10-17 18:18:35 -07002833 mat4* transform = NULL;
2834 if (layer->isTextureLayer()) {
2835 transform = &layer->getTransform();
2836 if (!transform->isIdentity()) {
2837 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08002838 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002839 }
2840 }
2841
Romain Guy35643dd2012-09-18 15:40:58 -07002842 Rect transformed;
2843 Rect clip;
2844 const bool rejected = quickRejectNoScissor(x, y,
2845 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2846
2847 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002848 if (transform && !transform->isIdentity()) {
2849 restore();
2850 }
Chet Haase48659092012-05-31 15:21:51 -07002851 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002852 }
2853
Romain Guy5bb3c732012-11-29 17:52:58 -08002854 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002855
Romain Guy87e2f7572012-09-24 11:37:12 -07002856 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002857 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002858
Romain Guy211370f2012-02-01 16:10:55 -08002859 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08002860 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
2861 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07002862
Romain Guyc88e3572011-01-22 00:32:12 -08002863 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002864 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002865 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002866 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002867 setupDraw();
2868 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002869 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002870 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002871 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002872 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002873 setupDrawPureColorUniforms();
2874 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002875 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08002876 if (CC_LIKELY(currentTransform().isPureTranslate())) {
2877 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2878 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002879
Romain Guyd21b6e12011-11-30 20:21:23 -08002880 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002881 setupDrawModelViewTranslate(tx, ty,
2882 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002883 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002884 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002885 setupDrawModelViewTranslate(x, y,
2886 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2887 }
Romain Guyc88e3572011-01-22 00:32:12 -08002888 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002889
Romain Guyc88e3572011-01-22 00:32:12 -08002890 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2891 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002892
Romain Guyc88e3572011-01-22 00:32:12 -08002893 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002894
2895#if DEBUG_LAYERS_AS_REGIONS
2896 drawRegionRects(layer->region);
2897#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002898 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002899
Chris Craikc3566d02013-02-04 16:16:33 -08002900 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07002901
Romain Guy5bb3c732012-11-29 17:52:58 -08002902 if (layer->debugDrawUpdate) {
2903 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002904 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2905 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2906 }
Romain Guyf219da52011-01-16 12:54:25 -08002907 }
Chet Haase48659092012-05-31 15:21:51 -07002908
Romain Guyb2e2f242012-10-17 18:18:35 -07002909 if (transform && !transform->isIdentity()) {
2910 restore();
2911 }
2912
Chet Haase48659092012-05-31 15:21:51 -07002913 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002914}
2915
Romain Guy6926c722010-07-12 20:20:03 -07002916///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002917// Shaders
2918///////////////////////////////////////////////////////////////////////////////
2919
2920void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08002921 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002922}
2923
Romain Guy06f96e22010-07-30 19:18:16 -07002924void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08002925 mDrawModifiers.mShader = shader;
2926 if (mDrawModifiers.mShader) {
2927 mDrawModifiers.mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002928 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002929}
2930
Romain Guyd27977d2010-07-14 19:18:51 -07002931///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002932// Color filters
2933///////////////////////////////////////////////////////////////////////////////
2934
2935void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08002936 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07002937}
2938
2939void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08002940 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07002941}
2942
2943///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002944// Drop shadow
2945///////////////////////////////////////////////////////////////////////////////
2946
2947void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08002948 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07002949}
2950
2951void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08002952 mDrawModifiers.mHasShadow = true;
2953 mDrawModifiers.mShadowRadius = radius;
2954 mDrawModifiers.mShadowDx = dx;
2955 mDrawModifiers.mShadowDy = dy;
2956 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07002957}
2958
2959///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002960// Draw filters
2961///////////////////////////////////////////////////////////////////////////////
2962
2963void OpenGLRenderer::resetPaintFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08002964 mDrawModifiers.mHasDrawFilter = false;
Romain Guy5ff9df62012-01-23 17:09:05 -08002965}
2966
2967void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08002968 mDrawModifiers.mHasDrawFilter = true;
2969 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2970 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08002971}
2972
Romain Guy758724f2013-02-27 11:53:12 -08002973SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint, bool alwaysCopy) {
2974 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
2975 if (CC_UNLIKELY(alwaysCopy)) {
2976 mFilteredPaint = *paint;
2977 return &mFilteredPaint;
2978 }
2979 return paint;
2980 }
Romain Guy5ff9df62012-01-23 17:09:05 -08002981
2982 uint32_t flags = paint->getFlags();
2983
2984 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08002985 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
2986 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08002987
2988 return &mFilteredPaint;
2989}
2990
2991///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002992// Drawing implementation
2993///////////////////////////////////////////////////////////////////////////////
2994
Romain Guy01d58e42011-01-19 21:54:02 -08002995void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2996 float x, float y, SkPaint* paint) {
2997 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2998 return;
2999 }
3000
3001 int alpha;
3002 SkXfermode::Mode mode;
3003 getAlphaAndMode(paint, &alpha, &mode);
3004
3005 setupDraw();
3006 setupDrawWithTexture(true);
3007 setupDrawAlpha8Color(paint->getColor(), alpha);
3008 setupDrawColorFilter();
3009 setupDrawShader();
3010 setupDrawBlending(true, mode);
3011 setupDrawProgram();
3012 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3013 setupDrawTexture(texture->id);
3014 setupDrawPureColorUniforms();
3015 setupDrawColorFilterUniforms();
3016 setupDrawShaderUniforms();
3017 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3018
3019 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3020
3021 finishDrawTexture();
3022}
3023
Romain Guyf607bdc2010-09-10 19:20:06 -07003024// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003025#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3026#define kStdUnderline_Offset (1.0f / 9.0f)
3027#define kStdUnderline_Thickness (1.0f / 18.0f)
3028
3029void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3030 float x, float y, SkPaint* paint) {
3031 // Handle underline and strike-through
3032 uint32_t flags = paint->getFlags();
3033 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003034 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003035 float underlineWidth = length;
3036 // If length is > 0.0f, we already measured the text for the text alignment
3037 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003038 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003039 }
3040
Romain Guy211370f2012-02-01 16:10:55 -08003041 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003042 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003043 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003044
Raph Levien8b4072d2012-07-30 15:50:00 -07003045 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003046 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003047
Romain Guyf6834472011-01-23 13:32:12 -08003048 int linesCount = 0;
3049 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3050 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3051
3052 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003053 float points[pointsCount];
3054 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003055
3056 if (flags & SkPaint::kUnderlineText_Flag) {
3057 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003058 points[currentPoint++] = left;
3059 points[currentPoint++] = top;
3060 points[currentPoint++] = left + underlineWidth;
3061 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003062 }
3063
3064 if (flags & SkPaint::kStrikeThruText_Flag) {
3065 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003066 points[currentPoint++] = left;
3067 points[currentPoint++] = top;
3068 points[currentPoint++] = left + underlineWidth;
3069 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003070 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003071
Romain Guy726aeba2011-06-01 14:52:00 -07003072 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003073
Romain Guy726aeba2011-06-01 14:52:00 -07003074 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003075 }
3076 }
3077}
3078
Romain Guy672433d2013-01-04 19:05:13 -08003079status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3080 if (mSnapshot->isIgnored()) {
3081 return DrawGlInfo::kStatusDone;
3082 }
3083
Romain Guy735738c2012-12-03 12:34:51 -08003084 int color = paint->getColor();
3085 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003086 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003087 color |= 0x00ffffff;
3088 }
3089 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3090
3091 return drawColorRects(rects, count, color, mode);
3092}
3093
3094status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003095 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003096 if (count == 0) {
3097 return DrawGlInfo::kStatusDone;
3098 }
Romain Guy735738c2012-12-03 12:34:51 -08003099
Romain Guy672433d2013-01-04 19:05:13 -08003100 float left = FLT_MAX;
3101 float top = FLT_MAX;
3102 float right = FLT_MIN;
3103 float bottom = FLT_MIN;
3104
3105 int vertexCount = 0;
3106 Vertex mesh[count * 6];
3107 Vertex* vertex = mesh;
3108
Chris Craik2af46352012-11-26 18:30:17 -08003109 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003110 float l = rects[index + 0];
3111 float t = rects[index + 1];
3112 float r = rects[index + 2];
3113 float b = rects[index + 3];
3114
Romain Guy3b753822013-03-05 10:27:35 -08003115 Vertex::set(vertex++, l, b);
3116 Vertex::set(vertex++, l, t);
3117 Vertex::set(vertex++, r, t);
3118 Vertex::set(vertex++, l, b);
3119 Vertex::set(vertex++, r, t);
3120 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003121
Romain Guy3b753822013-03-05 10:27:35 -08003122 vertexCount += 6;
Romain Guy672433d2013-01-04 19:05:13 -08003123
Romain Guy3b753822013-03-05 10:27:35 -08003124 left = fminf(left, l);
3125 top = fminf(top, t);
3126 right = fmaxf(right, r);
3127 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003128 }
3129
Romain Guy3b753822013-03-05 10:27:35 -08003130 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003131 return DrawGlInfo::kStatusDone;
3132 }
Romain Guy672433d2013-01-04 19:05:13 -08003133
Romain Guy672433d2013-01-04 19:05:13 -08003134 setupDraw();
3135 setupDrawNoTexture();
3136 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3137 setupDrawShader();
3138 setupDrawColorFilter();
3139 setupDrawBlending(mode);
3140 setupDrawProgram();
3141 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003142 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003143 setupDrawColorUniforms();
3144 setupDrawShaderUniforms();
3145 setupDrawColorFilterUniforms();
3146 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3147
Romain Guy8ce00302013-01-15 18:51:42 -08003148 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003149 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003150 }
3151
3152 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3153
3154 return DrawGlInfo::kStatusDrew;
3155}
3156
Romain Guy026c5e162010-06-28 17:12:22 -07003157void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003158 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003159 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003160 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003161 color |= 0x00ffffff;
3162 }
3163
Romain Guy70ca14e2010-12-13 18:24:33 -08003164 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003165 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003166 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003167 setupDrawShader();
3168 setupDrawColorFilter();
3169 setupDrawBlending(mode);
3170 setupDrawProgram();
3171 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3172 setupDrawColorUniforms();
3173 setupDrawShaderUniforms(ignoreTransform);
3174 setupDrawColorFilterUniforms();
3175 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003176
Romain Guyc95c8d62010-09-17 15:31:32 -07003177 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3178}
3179
Romain Guy82ba8142010-07-09 13:25:56 -07003180void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003181 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003182 int alpha;
3183 SkXfermode::Mode mode;
3184 getAlphaAndMode(paint, &alpha, &mode);
3185
Romain Guyd21b6e12011-11-30 20:21:23 -08003186 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003187
Romain Guy3b753822013-03-05 10:27:35 -08003188 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3189 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3190 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003191
Romain Guyd21b6e12011-11-30 20:21:23 -08003192 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003193 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3194 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3195 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3196 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003197 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003198 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3199 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3200 GL_TRIANGLE_STRIP, gMeshCount);
3201 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003202}
3203
Romain Guybd6b79b2010-06-26 00:13:53 -07003204void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003205 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3206 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003207 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003208}
3209
3210void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003211 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003212 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003213 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003214
Romain Guy746b7402010-10-26 16:27:31 -07003215 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003216 setupDrawWithTexture();
3217 setupDrawColor(alpha, alpha, alpha, alpha);
3218 setupDrawColorFilter();
3219 setupDrawBlending(blend, mode, swapSrcDst);
3220 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003221 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003222 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003223 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003224 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003225 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003226 }
Romain Guy886b2752013-01-04 12:26:18 -08003227 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003228 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003229 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003230 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003231
Romain Guy6820ac82010-09-15 18:11:50 -07003232 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003233
3234 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003235}
3236
Romain Guy886b2752013-01-04 12:26:18 -08003237void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3238 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3239 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3240 bool ignoreTransform, bool dirty) {
3241
3242 setupDraw();
3243 setupDrawWithTexture(true);
3244 if (hasColor) {
3245 setupDrawAlpha8Color(color, alpha);
3246 }
3247 setupDrawColorFilter();
3248 setupDrawShader();
3249 setupDrawBlending(true, mode);
3250 setupDrawProgram();
3251 if (!dirty) setupDrawDirtyRegionsDisabled();
3252 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3253 setupDrawTexture(texture);
3254 setupDrawPureColorUniforms();
3255 setupDrawColorFilterUniforms();
3256 setupDrawShaderUniforms();
3257 setupDrawMesh(vertices, texCoords);
3258
3259 glDrawArrays(drawMode, 0, elementsCount);
3260
3261 finishDrawTexture();
3262}
3263
Romain Guya5aed0d2010-09-09 14:42:43 -07003264void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003265 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003266 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003267
Romain Guy82ba8142010-07-09 13:25:56 -07003268 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003269 // These blend modes are not supported by OpenGL directly and have
3270 // to be implemented using shaders. Since the shader will perform
3271 // the blending, turn blending off here
3272 // If the blend mode cannot be implemented using shaders, fall
3273 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003274 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003275 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003276 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003277 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003278
Romain Guy82bc7a72012-01-03 14:13:39 -08003279 if (mCaches.blend) {
3280 glDisable(GL_BLEND);
3281 mCaches.blend = false;
3282 }
3283
3284 return;
3285 } else {
3286 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003287 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003288 }
3289
3290 if (!mCaches.blend) {
3291 glEnable(GL_BLEND);
3292 }
3293
3294 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3295 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3296
3297 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3298 glBlendFunc(sourceMode, destMode);
3299 mCaches.lastSrcMode = sourceMode;
3300 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003301 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003302 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003303 glDisable(GL_BLEND);
3304 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003305 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003306}
3307
Romain Guy889f8d12010-07-29 14:37:42 -07003308bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003309 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003310 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003311 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003312 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003313 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003314 }
Romain Guy6926c722010-07-12 20:20:03 -07003315 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003316}
3317
Romain Guy026c5e162010-06-28 17:12:22 -07003318void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003319 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003320 TextureVertex::setUV(v++, u1, v1);
3321 TextureVertex::setUV(v++, u2, v1);
3322 TextureVertex::setUV(v++, u1, v2);
3323 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003324}
3325
Chet Haase5c13d892010-10-08 08:37:55 -07003326void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003327 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003328 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003329}
3330
Romain Guy9d5316e2010-06-24 19:30:36 -07003331}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003332}; // namespace android