blob: 22ec93b1a5e5eaa6db8d366a0bde536a5d824a2d [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 Guy11cb6422012-09-21 00:39:43 -0700279 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700280#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700281 GLenum status = GL_NO_ERROR;
282 while ((status = glGetError()) != GL_NO_ERROR) {
283 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
284 switch (status) {
285 case GL_INVALID_ENUM:
286 ALOGE(" GL_INVALID_ENUM");
287 break;
288 case GL_INVALID_VALUE:
289 ALOGE(" GL_INVALID_VALUE");
290 break;
291 case GL_INVALID_OPERATION:
292 ALOGE(" GL_INVALID_OPERATION");
293 break;
294 case GL_OUT_OF_MEMORY:
295 ALOGE(" Out of memory!");
296 break;
297 }
Romain Guya07105b2011-01-10 21:14:18 -0800298 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700299#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700300
Romain Guyc15008e2010-11-10 11:59:15 -0800301#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800302 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700303#else
304 if (mCaches.getDebugLevel() & kDebugMemory) {
305 mCaches.dumpMemoryUsage();
306 }
Romain Guyc15008e2010-11-10 11:59:15 -0800307#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700308 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700309}
310
Romain Guy6c319ca2011-01-11 14:29:25 -0800311void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700312 if (mCaches.currentProgram) {
313 if (mCaches.currentProgram->isInUse()) {
314 mCaches.currentProgram->remove();
315 mCaches.currentProgram = NULL;
316 }
317 }
Romain Guy50c0f092010-10-19 11:42:22 -0700318 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800319 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800320 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800321 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700322 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700323}
324
Romain Guy6c319ca2011-01-11 14:29:25 -0800325void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800326 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800327 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700328 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700329 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700330
Romain Guy3e263fa2011-12-12 16:47:48 -0800331 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
332
Chet Haase80250612012-08-15 13:46:54 -0700333 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700334 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800335 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700336 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700337
Romain Guya1d3c912011-12-13 14:55:06 -0800338 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700339
Romain Guy50c0f092010-10-19 11:42:22 -0700340 mCaches.blend = true;
341 glEnable(GL_BLEND);
342 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
343 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700344}
345
Romain Guy35643dd2012-09-18 15:40:58 -0700346void OpenGLRenderer::resumeAfterLayer() {
347 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
348 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
349 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700350 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700351
352 mCaches.resetScissor();
353 dirtyClip();
354}
355
Romain Guyba6be8a2012-04-23 18:22:09 -0700356void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700357 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700358}
359
360void OpenGLRenderer::attachFunctor(Functor* functor) {
361 mFunctors.add(functor);
362}
363
Romain Guy8f3b8e32012-03-27 16:33:45 -0700364status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
365 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700366 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700367
Romain Guyba6be8a2012-04-23 18:22:09 -0700368 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800369 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700370 SortedVector<Functor*> functors(mFunctors);
371 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700372
Romain Guyba6be8a2012-04-23 18:22:09 -0700373 DrawGlInfo info;
374 info.clipLeft = 0;
375 info.clipTop = 0;
376 info.clipRight = 0;
377 info.clipBottom = 0;
378 info.isLayer = false;
379 info.width = 0;
380 info.height = 0;
381 memset(info.transform, 0, sizeof(float) * 16);
382
383 for (size_t i = 0; i < count; i++) {
384 Functor* f = functors.itemAt(i);
385 result |= (*f)(DrawGlInfo::kModeProcess, &info);
386
Chris Craikc2c95432012-04-25 15:13:52 -0700387 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700388 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
389 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700390 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700391
Chris Craikc2c95432012-04-25 15:13:52 -0700392 if (result & DrawGlInfo::kStatusInvoke) {
393 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700394 }
395 }
Chris Craikd15321b2012-11-28 14:45:04 -0800396 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700397 }
398
399 return result;
400}
401
402status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800403 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700404 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700405
Romain Guy8a4ac612012-07-17 17:32:48 -0700406 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800407 if (mDirtyClip) {
408 setScissorFromClip();
409 }
Romain Guyd643bb52011-03-01 14:55:21 -0800410
Romain Guy80911b82011-03-16 15:30:12 -0700411 Rect clip(*mSnapshot->clipRect);
412 clip.snapToPixelBoundaries();
413
Romain Guyd643bb52011-03-01 14:55:21 -0800414 // Since we don't know what the functor will draw, let's dirty
415 // tne entire clip region
416 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800417 dirtyLayerUnchecked(clip, getRegion());
418 }
Romain Guyd643bb52011-03-01 14:55:21 -0800419
Romain Guy08aa2cb2011-03-17 11:06:57 -0700420 DrawGlInfo info;
421 info.clipLeft = clip.left;
422 info.clipTop = clip.top;
423 info.clipRight = clip.right;
424 info.clipBottom = clip.bottom;
425 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700426 info.width = getSnapshot()->viewport.getWidth();
427 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700428 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700429
Chet Haase48659092012-05-31 15:21:51 -0700430 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800431
Romain Guy8f3b8e32012-03-27 16:33:45 -0700432 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700433 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800434 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700435
Chris Craik65924a32012-04-05 17:52:11 -0700436 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700437 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700438 }
Romain Guycabfcc12011-03-07 18:06:46 -0800439 }
440
Chet Haasedaf98e92011-01-10 14:10:36 -0800441 resume();
Romain Guy65549432012-03-26 16:45:05 -0700442 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800443}
444
Romain Guyf6a11b82010-06-23 17:47:49 -0700445///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700446// Debug
447///////////////////////////////////////////////////////////////////////////////
448
Romain Guy0f667532013-03-01 14:31:04 -0800449void OpenGLRenderer::eventMark(const char* name) const {
450 mCaches.eventMark(0, name);
451}
452
Romain Guy87e2f7572012-09-24 11:37:12 -0700453void OpenGLRenderer::startMark(const char* name) const {
454 mCaches.startMark(0, name);
455}
456
457void OpenGLRenderer::endMark() const {
458 mCaches.endMark();
459}
460
461void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
462 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
463 if (clear) {
464 mCaches.disableScissor();
465 mCaches.stencil.clear();
466 }
467 if (enable) {
468 mCaches.stencil.enableDebugWrite();
469 } else {
470 mCaches.stencil.disable();
471 }
472 }
473}
474
475void OpenGLRenderer::renderOverdraw() {
476 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
477 const Rect* clip = mTilingSnapshot->clipRect;
478
479 mCaches.enableScissor();
480 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
481 clip->right - clip->left, clip->bottom - clip->top);
482
483 mCaches.stencil.enableDebugTest(2);
484 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
485 mCaches.stencil.enableDebugTest(3);
486 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
487 mCaches.stencil.enableDebugTest(4);
488 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
489 mCaches.stencil.enableDebugTest(4, true);
490 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
491 mCaches.stencil.disable();
492 }
493}
494
495///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700496// Layers
497///////////////////////////////////////////////////////////////////////////////
498
499bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
500 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
501 OpenGLRenderer* renderer = layer->renderer;
502 Rect& dirty = layer->dirtyRect;
503
Romain Guy7c450aa2012-09-21 19:15:00 -0700504 if (inFrame) {
505 endTiling();
506 debugOverdraw(false, false);
507 }
Romain Guy11cb6422012-09-21 00:39:43 -0700508
509 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
510 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
511 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
512 renderer->finish();
513
514 if (inFrame) {
515 resumeAfterLayer();
516 startTiling(mSnapshot);
517 }
518
519 dirty.setEmpty();
520 layer->deferredUpdateScheduled = false;
521 layer->renderer = NULL;
522 layer->displayList = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -0800523 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700524
525 return true;
526 }
527
528 return false;
529}
530
531void OpenGLRenderer::updateLayers() {
532 int count = mLayerUpdates.size();
533 if (count > 0) {
534 startMark("Layer Updates");
535
536 // Note: it is very important to update the layers in reverse order
537 for (int i = count - 1; i >= 0; i--) {
538 Layer* layer = mLayerUpdates.itemAt(i);
539 updateLayer(layer, false);
540 mCaches.resourceCache.decrementRefcount(layer);
541 }
542 mLayerUpdates.clear();
543
544 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
545 endMark();
546 }
547}
548
549void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
550 if (layer) {
551 mLayerUpdates.push_back(layer);
552 mCaches.resourceCache.incrementRefcount(layer);
553 }
554}
555
556void OpenGLRenderer::clearLayerUpdates() {
557 size_t count = mLayerUpdates.size();
558 if (count > 0) {
559 mCaches.resourceCache.lock();
560 for (size_t i = 0; i < count; i++) {
561 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
562 }
563 mCaches.resourceCache.unlock();
564 mLayerUpdates.clear();
565 }
566}
567
568///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700569// State management
570///////////////////////////////////////////////////////////////////////////////
571
Romain Guybb9524b2010-06-22 18:56:38 -0700572int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700573 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700574}
575
576int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700577 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700578}
579
580void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700581 if (mSaveCount > 1) {
582 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700583 }
Romain Guybb9524b2010-06-22 18:56:38 -0700584}
585
586void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700587 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700588
Romain Guy8fb95422010-08-17 18:38:51 -0700589 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700590 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700591 }
Romain Guybb9524b2010-06-22 18:56:38 -0700592}
593
Romain Guy8aef54f2010-09-01 15:13:49 -0700594int OpenGLRenderer::saveSnapshot(int flags) {
595 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700596 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700597}
598
599bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700600 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700601 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700602 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700603
Romain Guybd6b79b2010-06-26 00:13:53 -0700604 sp<Snapshot> current = mSnapshot;
605 sp<Snapshot> previous = mSnapshot->previous;
606
Romain Guyeb993562010-10-05 18:14:38 -0700607 if (restoreOrtho) {
608 Rect& r = previous->viewport;
609 glViewport(r.left, r.top, r.right, r.bottom);
610 mOrthoMatrix.load(current->orthoMatrix);
611 }
612
Romain Guy8b55f372010-08-18 17:10:07 -0700613 mSaveCount--;
614 mSnapshot = previous;
615
Romain Guy2542d192010-08-18 11:47:12 -0700616 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700617 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700618 }
Romain Guy2542d192010-08-18 11:47:12 -0700619
Romain Guy5ec99242010-11-03 16:19:08 -0700620 if (restoreLayer) {
621 composeLayer(current, previous);
622 }
623
Romain Guy2542d192010-08-18 11:47:12 -0700624 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700625}
626
Romain Guyf6a11b82010-06-23 17:47:49 -0700627///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700628// Layers
629///////////////////////////////////////////////////////////////////////////////
630
631int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700632 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700633 const GLuint previousFbo = mSnapshot->fbo;
634 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700635
Romain Guyaf636eb2010-12-09 17:47:21 -0800636 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700637 int alpha = 255;
638 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700639
Romain Guye45362c2010-11-03 19:58:32 -0700640 if (p) {
641 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700642 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700643 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700644 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700645 }
Romain Guyd55a8612010-06-28 17:42:46 -0700646
Chet Haased48885a2012-08-28 17:43:28 -0700647 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700648 }
Romain Guyd55a8612010-06-28 17:42:46 -0700649
650 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700651}
652
653int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
654 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700655 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700656 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700657 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700658 SkPaint paint;
659 paint.setAlpha(alpha);
660 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700661 }
Romain Guyd55a8612010-06-28 17:42:46 -0700662}
Romain Guybd6b79b2010-06-26 00:13:53 -0700663
Romain Guy1c740bc2010-09-13 18:00:09 -0700664/**
665 * Layers are viewed by Skia are slightly different than layers in image editing
666 * programs (for instance.) When a layer is created, previously created layers
667 * and the frame buffer still receive every drawing command. For instance, if a
668 * layer is created and a shape intersecting the bounds of the layers and the
669 * framebuffer is draw, the shape will be drawn on both (unless the layer was
670 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
671 *
672 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
673 * texture. Unfortunately, this is inefficient as it requires every primitive to
674 * be drawn n + 1 times, where n is the number of active layers. In practice this
675 * means, for every primitive:
676 * - Switch active frame buffer
677 * - Change viewport, clip and projection matrix
678 * - Issue the drawing
679 *
680 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700681 * To avoid this, layers are implemented in a different way here, at least in the
682 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
683 * is set. When this flag is set we can redirect all drawing operations into a
684 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700685 *
686 * This implementation relies on the frame buffer being at least RGBA 8888. When
687 * a layer is created, only a texture is created, not an FBO. The content of the
688 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700689 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700690 * buffer and drawing continues as normal. This technique therefore treats the
691 * frame buffer as a scratch buffer for the layers.
692 *
693 * To compose the layers back onto the frame buffer, each layer texture
694 * (containing the original frame buffer data) is drawn as a simple quad over
695 * the frame buffer. The trick is that the quad is set as the composition
696 * destination in the blending equation, and the frame buffer becomes the source
697 * of the composition.
698 *
699 * Drawing layers with an alpha value requires an extra step before composition.
700 * An empty quad is drawn over the layer's region in the frame buffer. This quad
701 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
702 * quad is used to multiply the colors in the frame buffer. This is achieved by
703 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
704 * GL_ZERO, GL_SRC_ALPHA.
705 *
706 * Because glCopyTexImage2D() can be slow, an alternative implementation might
707 * be use to draw a single clipped layer. The implementation described above
708 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700709 *
710 * (1) The frame buffer is actually not cleared right away. To allow the GPU
711 * to potentially optimize series of calls to glCopyTexImage2D, the frame
712 * buffer is left untouched until the first drawing operation. Only when
713 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700714 */
Chet Haased48885a2012-08-28 17:43:28 -0700715bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
716 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700717 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700718 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700719
Romain Guyeb993562010-10-05 18:14:38 -0700720 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
721
Romain Guyf607bdc2010-09-10 19:20:06 -0700722 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700723 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700724 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700725 Rect untransformedBounds(bounds);
Romain Guy3b753822013-03-05 10:27:35 -0800726 currentTransform().mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700727
Chet Haased48885a2012-08-28 17:43:28 -0700728 // Layers only make sense if they are in the framebuffer's bounds
729 if (bounds.intersect(*mSnapshot->clipRect)) {
730 // We cannot work with sub-pixels in this case
731 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700732
Chet Haased48885a2012-08-28 17:43:28 -0700733 // When the layer is not an FBO, we may use glCopyTexImage so we
734 // need to make sure the layer does not extend outside the bounds
735 // of the framebuffer
736 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700737 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700738 } else if (fboLayer) {
739 clip.set(bounds);
740 mat4 inverse;
Romain Guy3b753822013-03-05 10:27:35 -0800741 inverse.loadInverse(currentTransform());
Chet Haased48885a2012-08-28 17:43:28 -0700742 inverse.mapRect(clip);
743 clip.snapToPixelBoundaries();
744 if (clip.intersect(untransformedBounds)) {
745 clip.translate(-left, -top);
746 bounds.set(untransformedBounds);
747 } else {
748 clip.setEmpty();
749 }
Romain Guyad37cd32011-03-15 11:12:25 -0700750 }
Chet Haased48885a2012-08-28 17:43:28 -0700751 } else {
752 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700753 }
Romain Guybf434112010-09-16 14:40:17 -0700754
Romain Guy746b7402010-10-26 16:27:31 -0700755 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700756 bounds.getHeight() > mCaches.maxTextureSize ||
757 (fboLayer && clip.isEmpty())) {
758 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700759 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700760 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700761 }
762
763 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700764 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700765 return false;
766 }
Romain Guyf18fd992010-07-08 11:45:51 -0700767
Romain Guya1d3c912011-12-13 14:55:06 -0800768 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700769 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700770 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700771 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700772 }
773
Romain Guy9ace8f52011-07-07 20:50:11 -0700774 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700775 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700776 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
777 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800778 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700779 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700780 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700781
Romain Guy8fb95422010-08-17 18:38:51 -0700782 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700783 mSnapshot->flags |= Snapshot::kFlagIsLayer;
784 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700785
Romain Guyeb993562010-10-05 18:14:38 -0700786 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700787 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700788 } else {
789 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700790 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800791 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700792 if (layer->isEmpty()) {
793 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700794 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700795 layer->getWidth(), layer->getHeight(), 0);
796 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800797 } else {
798 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700799 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800800 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700801
Romain Guy54be1cd2011-06-13 19:04:27 -0700802 // Enqueue the buffer coordinates to clear the corresponding region later
803 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700804 }
Romain Guyeb993562010-10-05 18:14:38 -0700805 }
Romain Guyf86ef572010-07-01 11:05:42 -0700806
Romain Guyd55a8612010-06-28 17:42:46 -0700807 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700808}
809
Chet Haased48885a2012-08-28 17:43:28 -0700810bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800811 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700812 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700813
Chet Haased48885a2012-08-28 17:43:28 -0700814 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800815 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
816 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700817 mSnapshot->fbo = layer->getFbo();
818 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
819 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
820 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
821 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700822 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700823
Romain Guy2b7028e2012-09-19 17:25:38 -0700824 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700825 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700826 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700827 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
828 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700829
830 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700831 if (layer->isEmpty()) {
832 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
833 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700834 }
835
836 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700837 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700838
Romain Guyf735c8e2013-01-31 17:45:55 -0800839 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700840
841 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700842 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800843 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700844 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700845 glClear(GL_COLOR_BUFFER_BIT);
846
847 dirtyClip();
848
849 // Change the ortho projection
850 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
851 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
852
853 return true;
854}
855
Romain Guy1c740bc2010-09-13 18:00:09 -0700856/**
857 * Read the documentation of createLayer() before doing anything in this method.
858 */
Romain Guy1d83e192010-08-17 11:37:00 -0700859void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
860 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000861 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700862 return;
863 }
864
Romain Guy8ce00302013-01-15 18:51:42 -0800865 Layer* layer = current->layer;
866 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700867 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700868
869 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700870 endTiling();
871
Romain Guye0aa84b2012-04-03 19:30:26 -0700872 // Detach the texture from the FBO
873 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800874
875 layer->removeFbo(false);
876
Romain Guyeb993562010-10-05 18:14:38 -0700877 // Unbind current FBO and restore previous one
878 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700879 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700880
881 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700882 }
883
Romain Guy9ace8f52011-07-07 20:50:11 -0700884 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700885 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700886 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700887 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700888 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700889 }
890
Romain Guy03750a02010-10-18 14:06:08 -0700891 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700892
Romain Guya1d3c912011-12-13 14:55:06 -0800893 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700894
Romain Guy5b3b3522010-10-27 18:57:51 -0700895 // When the layer is stored in an FBO, we can save a bit of fillrate by
896 // drawing only the dirty region
897 if (fboLayer) {
898 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700899 if (layer->getColorFilter()) {
900 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800901 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700902 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700903 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800904 resetColorFilter();
905 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700906 } else if (!rect.isEmpty()) {
907 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
908 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700909 }
Romain Guy8b55f372010-08-18 17:10:07 -0700910
Romain Guy746b7402010-10-26 16:27:31 -0700911 dirtyClip();
912
Romain Guyeb993562010-10-05 18:14:38 -0700913 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700914 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700915 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700916 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700917 }
918}
919
Romain Guyaa6c24c2011-04-28 18:40:04 -0700920void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700921 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700922
923 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700924 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700925 setupDrawWithTexture();
926 } else {
927 setupDrawWithExternalTexture();
928 }
929 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700930 setupDrawColor(alpha, alpha, alpha, alpha);
931 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700932 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700933 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700934 setupDrawPureColorUniforms();
935 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700936 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
937 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700938 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700939 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700940 }
Romain Guy3b753822013-03-05 10:27:35 -0800941 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700942 layer->getWidth() == (uint32_t) rect.getWidth() &&
943 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -0800944 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
945 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700946
Romain Guyd21b6e12011-11-30 20:21:23 -0800947 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700948 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
949 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800950 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700951 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
952 }
953 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700954 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
955
956 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
957
958 finishDrawTexture();
959}
960
Romain Guy5b3b3522010-10-27 18:57:51 -0700961void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700962 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700963 const Rect& texCoords = layer->texCoords;
964 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
965 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700966
Romain Guy9ace8f52011-07-07 20:50:11 -0700967 float x = rect.left;
968 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -0800969 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700970 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700971 layer->getHeight() == (uint32_t) rect.getHeight();
972
973 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700974 // When we're swapping, the layer is already in screen coordinates
975 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -0800976 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
977 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700978 }
979
Romain Guyd21b6e12011-11-30 20:21:23 -0800980 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700981 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800982 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700983 }
984
985 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
986 layer->getTexture(), layer->getAlpha() / 255.0f,
987 layer->getMode(), layer->isBlend(),
988 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
989 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700990
Romain Guyaa6c24c2011-04-28 18:40:04 -0700991 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
992 } else {
993 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
994 drawTextureLayer(layer, rect);
995 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
996 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700997}
998
999void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001000 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001001 layer->setRegionAsRect();
1002
Romain Guy40667672011-03-18 14:34:03 -07001003 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -07001004
Romain Guy5b3b3522010-10-27 18:57:51 -07001005 layer->region.clear();
1006 return;
1007 }
1008
Romain Guy8a3957d2011-09-07 17:55:15 -07001009 // TODO: See LayerRenderer.cpp::generateMesh() for important
1010 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -08001011 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001012 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001013 const android::Rect* rects;
1014 Region safeRegion;
1015 if (CC_LIKELY(hasRectToRectTransform())) {
1016 rects = layer->region.getArray(&count);
1017 } else {
1018 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1019 rects = safeRegion.getArray(&count);
1020 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001021
Romain Guy9ace8f52011-07-07 20:50:11 -07001022 const float alpha = layer->getAlpha() / 255.0f;
1023 const float texX = 1.0f / float(layer->getWidth());
1024 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001025 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001026
Romain Guy8ce00302013-01-15 18:51:42 -08001027 setupDraw();
1028
1029 // We must get (and therefore bind) the region mesh buffer
1030 // after we setup drawing in case we need to mess with the
1031 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001032 TextureVertex* mesh = mCaches.getRegionMesh();
1033 GLsizei numQuads = 0;
1034
Romain Guy7230a742011-01-10 22:26:16 -08001035 setupDrawWithTexture();
1036 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001037 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001038 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001039 setupDrawProgram();
1040 setupDrawDirtyRegionsDisabled();
1041 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001042 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001043 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001044 if (currentTransform().isPureTranslate()) {
1045 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1046 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001047
Romain Guyd21b6e12011-11-30 20:21:23 -08001048 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001049 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1050 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001051 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001052 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1053 }
Romain Guy15bc6432011-12-13 13:11:32 -08001054 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001055
1056 for (size_t i = 0; i < count; i++) {
1057 const android::Rect* r = &rects[i];
1058
1059 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001060 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001061 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001062 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001063
1064 // TODO: Reject quads outside of the clip
1065 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1066 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1067 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1068 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1069
1070 numQuads++;
1071
1072 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1073 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1074 numQuads = 0;
1075 mesh = mCaches.getRegionMesh();
1076 }
1077 }
1078
1079 if (numQuads > 0) {
1080 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1081 }
1082
Romain Guy7230a742011-01-10 22:26:16 -08001083 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001084
1085#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001086 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001087#endif
1088
1089 layer->region.clear();
1090 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001091}
1092
Romain Guy3a3133d2011-02-01 22:59:58 -08001093void OpenGLRenderer::drawRegionRects(const Region& region) {
1094#if DEBUG_LAYERS_AS_REGIONS
1095 size_t count;
1096 const android::Rect* rects = region.getArray(&count);
1097
1098 uint32_t colors[] = {
1099 0x7fff0000, 0x7f00ff00,
1100 0x7f0000ff, 0x7fff00ff,
1101 };
1102
1103 int offset = 0;
1104 int32_t top = rects[0].top;
1105
1106 for (size_t i = 0; i < count; i++) {
1107 if (top != rects[i].top) {
1108 offset ^= 0x2;
1109 top = rects[i].top;
1110 }
1111
1112 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1113 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1114 SkXfermode::kSrcOver_Mode);
1115 }
1116#endif
1117}
1118
Romain Guy8ce00302013-01-15 18:51:42 -08001119void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1120 SkXfermode::Mode mode, bool dirty) {
1121 int count = 0;
1122 Vector<float> rects;
1123
1124 SkRegion::Iterator it(region);
1125 while (!it.done()) {
1126 const SkIRect& r = it.rect();
1127 rects.push(r.fLeft);
1128 rects.push(r.fTop);
1129 rects.push(r.fRight);
1130 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001131 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001132 it.next();
1133 }
1134
Romain Guy3bbacf22013-02-06 16:51:04 -08001135 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001136}
1137
Romain Guy5b3b3522010-10-27 18:57:51 -07001138void OpenGLRenderer::dirtyLayer(const float left, const float top,
1139 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001140 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001141 Rect bounds(left, top, right, bottom);
1142 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001143 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001144 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001145}
1146
1147void OpenGLRenderer::dirtyLayer(const float left, const float top,
1148 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001149 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001150 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001151 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001152 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001153}
1154
1155void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001156 if (bounds.intersect(*mSnapshot->clipRect)) {
1157 bounds.snapToPixelBoundaries();
1158 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1159 if (!dirty.isEmpty()) {
1160 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001161 }
1162 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001163}
1164
Romain Guy54be1cd2011-06-13 19:04:27 -07001165void OpenGLRenderer::clearLayerRegions() {
1166 const size_t count = mLayers.size();
1167 if (count == 0) return;
1168
1169 if (!mSnapshot->isIgnored()) {
1170 // Doing several glScissor/glClear here can negatively impact
1171 // GPUs with a tiler architecture, instead we draw quads with
1172 // the Clear blending mode
1173
1174 // The list contains bounds that have already been clipped
1175 // against their initial clip rect, and the current clip
1176 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001177 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001178
1179 Vertex mesh[count * 6];
1180 Vertex* vertex = mesh;
1181
1182 for (uint32_t i = 0; i < count; i++) {
1183 Rect* bounds = mLayers.itemAt(i);
1184
1185 Vertex::set(vertex++, bounds->left, bounds->bottom);
1186 Vertex::set(vertex++, bounds->left, bounds->top);
1187 Vertex::set(vertex++, bounds->right, bounds->top);
1188 Vertex::set(vertex++, bounds->left, bounds->bottom);
1189 Vertex::set(vertex++, bounds->right, bounds->top);
1190 Vertex::set(vertex++, bounds->right, bounds->bottom);
1191
1192 delete bounds;
1193 }
Romain Guye67307c2013-02-11 18:01:20 -08001194 // We must clear the list of dirty rects before we
1195 // call setupDraw() to prevent stencil setup to do
1196 // the same thing again
1197 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001198
1199 setupDraw(false);
1200 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1201 setupDrawBlending(true, SkXfermode::kClear_Mode);
1202 setupDrawProgram();
1203 setupDrawPureColorUniforms();
1204 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001205 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001206
Romain Guy54be1cd2011-06-13 19:04:27 -07001207 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001208
1209 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001210 } else {
1211 for (uint32_t i = 0; i < count; i++) {
1212 delete mLayers.itemAt(i);
1213 }
Romain Guye67307c2013-02-11 18:01:20 -08001214 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001215 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001216}
1217
Romain Guybd6b79b2010-06-26 00:13:53 -07001218///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001219// State Deferral
1220///////////////////////////////////////////////////////////////////////////////
1221
1222bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state) {
1223 const Rect& currentClip = *(mSnapshot->clipRect);
1224 const mat4& currentMatrix = *(mSnapshot->transform);
1225
1226 // state only has bounds initialized in local coordinates
1227 if (!state.mBounds.isEmpty()) {
1228 currentMatrix.mapRect(state.mBounds);
1229 if (!state.mBounds.intersect(currentClip)) {
1230 // quick rejected
1231 return true;
1232 }
Chris Craik5d116762013-02-19 17:49:31 -08001233 } else {
1234 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001235 }
1236
1237 state.mClip.set(currentClip);
1238 state.mMatrix.load(currentMatrix);
1239 state.mDrawModifiers = mDrawModifiers;
1240 return false;
1241}
1242
1243void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state) {
Romain Guy3b753822013-03-05 10:27:35 -08001244 currentTransform().load(state.mMatrix);
Chris Craikc3566d02013-02-04 16:16:33 -08001245
1246 // NOTE: a clip RECT will be saved and restored, but DeferredDisplayState doesn't support
1247 // complex clips. In the future, we should add support for deferral of operations clipped by
1248 // these. for now, we don't defer with complex clips (see OpenGLRenderer::disallowDeferral())
1249 mSnapshot->setClip(state.mClip.left, state.mClip.top, state.mClip.right, state.mClip.bottom);
1250 dirtyClip();
1251 mDrawModifiers = state.mDrawModifiers;
1252}
1253
1254///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001255// Transforms
1256///////////////////////////////////////////////////////////////////////////////
1257
1258void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy3b753822013-03-05 10:27:35 -08001259 currentTransform().translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001260}
1261
1262void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001263 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001264}
1265
1266void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001267 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001268}
1269
Romain Guy807daf72011-01-18 11:19:19 -08001270void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001271 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001272}
1273
Romain Guyf6a11b82010-06-23 17:47:49 -07001274void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001275 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001276 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001277 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001278 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001279 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001280}
1281
Chris Craikb98a0162013-02-21 11:30:22 -08001282bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001283 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001284}
1285
Romain Guyf6a11b82010-06-23 17:47:49 -07001286void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001287 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001288}
1289
1290void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001291 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001292 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001293 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001294 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001295}
1296
1297///////////////////////////////////////////////////////////////////////////////
1298// Clipping
1299///////////////////////////////////////////////////////////////////////////////
1300
Romain Guybb9524b2010-06-22 18:56:38 -07001301void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001302 Rect clip(*mSnapshot->clipRect);
1303 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001304
Romain Guy8a4ac612012-07-17 17:32:48 -07001305 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1306 clip.getWidth(), clip.getHeight())) {
1307 mDirtyClip = false;
1308 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001309}
1310
Romain Guy8ce00302013-01-15 18:51:42 -08001311void OpenGLRenderer::ensureStencilBuffer() {
1312 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1313 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1314 // just hope we have one when hasLayer() returns false.
1315 if (hasLayer()) {
1316 attachStencilBufferToLayer(mSnapshot->layer);
1317 }
1318}
1319
1320void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1321 // The layer's FBO is already bound when we reach this stage
1322 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001323 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1324 // is attached after we initiated tiling. We must turn it off,
1325 // attach the new render buffer then turn tiling back on
1326 endTiling();
1327
Romain Guy8d4aeb72013-02-12 16:08:55 -08001328 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001329 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001330 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001331
Romain Guyf735c8e2013-01-31 17:45:55 -08001332 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001333 }
1334}
1335
1336void OpenGLRenderer::setStencilFromClip() {
1337 if (!mCaches.debugOverdraw) {
1338 if (!mSnapshot->clipRegion->isEmpty()) {
1339 // NOTE: The order here is important, we must set dirtyClip to false
1340 // before any draw call to avoid calling back into this method
1341 mDirtyClip = false;
1342
1343 ensureStencilBuffer();
1344
1345 mCaches.stencil.enableWrite();
1346
1347 // Clear the stencil but first make sure we restrict drawing
1348 // to the region's bounds
1349 bool resetScissor = mCaches.enableScissor();
1350 if (resetScissor) {
1351 // The scissor was not set so we now need to update it
1352 setScissorFromClip();
1353 }
1354 mCaches.stencil.clear();
1355 if (resetScissor) mCaches.disableScissor();
1356
1357 // NOTE: We could use the region contour path to generate a smaller mesh
1358 // Since we are using the stencil we could use the red book path
1359 // drawing technique. It might increase bandwidth usage though.
1360
1361 // The last parameter is important: we are not drawing in the color buffer
1362 // so we don't want to dirty the current layer, if any
1363 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1364
1365 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001366
1367 // Draw the region used to generate the stencil if the appropriate debug
1368 // mode is enabled
1369 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1370 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1371 }
Romain Guy8ce00302013-01-15 18:51:42 -08001372 } else {
1373 mCaches.stencil.disable();
1374 }
1375 }
1376}
1377
Romain Guy9d5316e2010-06-24 19:30:36 -07001378const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001379 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001380}
1381
Romain Guy8a4ac612012-07-17 17:32:48 -07001382bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1383 if (mSnapshot->isIgnored()) {
1384 return true;
1385 }
1386
1387 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001388 currentTransform().mapRect(r);
Romain Guy8a4ac612012-07-17 17:32:48 -07001389 r.snapToPixelBoundaries();
1390
1391 Rect clipRect(*mSnapshot->clipRect);
1392 clipRect.snapToPixelBoundaries();
1393
1394 return !clipRect.intersects(r);
1395}
1396
Romain Guy35643dd2012-09-18 15:40:58 -07001397bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1398 Rect& transformed, Rect& clip) {
1399 if (mSnapshot->isIgnored()) {
1400 return true;
1401 }
1402
1403 transformed.set(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001404 currentTransform().mapRect(transformed);
Romain Guy35643dd2012-09-18 15:40:58 -07001405 transformed.snapToPixelBoundaries();
1406
1407 clip.set(*mSnapshot->clipRect);
1408 clip.snapToPixelBoundaries();
1409
1410 return !clip.intersects(transformed);
1411}
1412
Romain Guy672433d2013-01-04 19:05:13 -08001413bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1414 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001415 if (paint->getStyle() != SkPaint::kFill_Style) {
1416 float outset = paint->getStrokeWidth() * 0.5f;
1417 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1418 } else {
1419 return quickReject(left, top, right, bottom);
1420 }
1421}
1422
Romain Guyc7d53492010-06-25 13:41:57 -07001423bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001424 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001425 return true;
1426 }
1427
Romain Guy1d83e192010-08-17 11:37:00 -07001428 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001429 currentTransform().mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001430 r.snapToPixelBoundaries();
1431
1432 Rect clipRect(*mSnapshot->clipRect);
1433 clipRect.snapToPixelBoundaries();
1434
Romain Guy586cae32012-07-13 15:28:31 -07001435 bool rejected = !clipRect.intersects(r);
1436 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001437 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001438 }
1439
1440 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001441}
1442
Romain Guy8ce00302013-01-15 18:51:42 -08001443void OpenGLRenderer::debugClip() {
1444#if DEBUG_CLIP_REGIONS
1445 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1446 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1447 }
1448#endif
1449}
1450
Romain Guy079ba2c2010-07-16 14:12:24 -07001451bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001452 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001453 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1454 if (clipped) {
1455 dirtyClip();
1456 }
1457 return !mSnapshot->clipRect->isEmpty();
1458 }
1459
1460 SkPath path;
1461 path.addRect(left, top, right, bottom);
1462
1463 return clipPath(&path, op);
1464}
1465
1466bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1467 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001468 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001469
1470 SkPath transformed;
1471 path->transform(transform, &transformed);
1472
1473 SkRegion clip;
1474 if (!mSnapshot->clipRegion->isEmpty()) {
1475 clip.setRegion(*mSnapshot->clipRegion);
1476 } else {
1477 Rect* bounds = mSnapshot->clipRect;
1478 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1479 }
1480
1481 SkRegion region;
1482 region.setPath(transformed, clip);
1483
1484 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001485 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001486 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001487 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001488 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001489}
1490
Romain Guy735738c2012-12-03 12:34:51 -08001491bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001492 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1493 if (clipped) {
1494 dirtyClip();
1495 }
1496 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001497}
1498
Chet Haasea23eed82012-04-12 15:19:04 -07001499Rect* OpenGLRenderer::getClipRect() {
1500 return mSnapshot->clipRect;
1501}
1502
Romain Guyf6a11b82010-06-23 17:47:49 -07001503///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001504// Drawing commands
1505///////////////////////////////////////////////////////////////////////////////
1506
Romain Guy54be1cd2011-06-13 19:04:27 -07001507void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001508 // TODO: It would be best if we could do this before quickReject()
1509 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001510 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001511 // Make sure setScissor & setStencil happen at the beginning of
1512 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001513 if (mDirtyClip) {
1514 if (mCaches.scissorEnabled) {
1515 setScissorFromClip();
1516 }
Romain Guy8ce00302013-01-15 18:51:42 -08001517 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001518 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001519
Romain Guy70ca14e2010-12-13 18:24:33 -08001520 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001521
Romain Guy70ca14e2010-12-13 18:24:33 -08001522 mSetShaderColor = false;
1523 mColorSet = false;
1524 mColorA = mColorR = mColorG = mColorB = 0.0f;
1525 mTextureUnit = 0;
1526 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001527
1528 // Enable debug highlight when what we're about to draw is tested against
1529 // the stencil buffer and if stencil highlight debugging is on
1530 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1531 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1532 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001533}
1534
1535void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1536 mDescription.hasTexture = true;
1537 mDescription.hasAlpha8Texture = isAlpha8;
1538}
1539
Romain Guyff316ec2013-02-13 18:39:43 -08001540void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1541 mDescription.hasTexture = true;
1542 mDescription.hasColors = true;
1543 mDescription.hasAlpha8Texture = isAlpha8;
1544}
1545
Romain Guyaa6c24c2011-04-28 18:40:04 -07001546void OpenGLRenderer::setupDrawWithExternalTexture() {
1547 mDescription.hasExternalTexture = true;
1548}
1549
Romain Guy15bc6432011-12-13 13:11:32 -08001550void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001551 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001552}
1553
Chris Craik710f46d2012-09-17 17:25:49 -07001554void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001555 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001556}
1557
Romain Guyed6fcb02011-03-21 13:11:28 -07001558void OpenGLRenderer::setupDrawPoint(float pointSize) {
1559 mDescription.isPoint = true;
1560 mDescription.pointSize = pointSize;
1561}
1562
Romain Guy8d0d4782010-12-14 20:13:35 -08001563void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1564 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001565 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1566 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1567 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001568 mColorSet = true;
1569 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1570}
1571
Romain Guy86568192010-12-14 15:55:39 -08001572void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1573 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001574 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1575 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1576 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001577 mColorSet = true;
1578 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1579}
1580
Romain Guy41210632012-07-16 17:04:24 -07001581void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1582 mCaches.fontRenderer->describe(mDescription, paint);
1583}
1584
Romain Guy70ca14e2010-12-13 18:24:33 -08001585void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1586 mColorA = a;
1587 mColorR = r;
1588 mColorG = g;
1589 mColorB = b;
1590 mColorSet = true;
1591 mSetShaderColor = mDescription.setColor(r, g, b, a);
1592}
1593
1594void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001595 if (mDrawModifiers.mShader) {
1596 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001597 }
1598}
1599
1600void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001601 if (mDrawModifiers.mColorFilter) {
1602 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001603 }
1604}
1605
Romain Guyf09ef512011-05-27 11:43:46 -07001606void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1607 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1608 mColorA = 1.0f;
1609 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001610 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001611 }
1612}
1613
Romain Guy70ca14e2010-12-13 18:24:33 -08001614void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001615 // When the blending mode is kClear_Mode, we need to use a modulate color
1616 // argb=1,0,0,0
1617 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001618 bool blend = (mColorSet && mColorA < 1.0f) ||
1619 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1620 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001621}
1622
1623void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001624 // When the blending mode is kClear_Mode, we need to use a modulate color
1625 // argb=1,0,0,0
1626 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001627 blend |= (mColorSet && mColorA < 1.0f) ||
1628 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1629 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1630 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001631}
1632
1633void OpenGLRenderer::setupDrawProgram() {
1634 useProgram(mCaches.programCache.get(mDescription));
1635}
1636
1637void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1638 mTrackDirtyRegions = false;
1639}
1640
1641void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1642 bool ignoreTransform) {
1643 mModelView.loadTranslate(left, top, 0.0f);
1644 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001645 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1646 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001647 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001648 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001649 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1650 }
1651}
1652
Chet Haase8a5cc922011-04-26 07:28:09 -07001653void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001654 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001655}
1656
Romain Guy70ca14e2010-12-13 18:24:33 -08001657void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1658 bool ignoreTransform, bool ignoreModelView) {
1659 if (!ignoreModelView) {
1660 mModelView.loadTranslate(left, top, 0.0f);
1661 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001662 } else {
1663 mModelView.loadIdentity();
1664 }
Romain Guy86568192010-12-14 15:55:39 -08001665 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1666 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001667 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001668 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001669 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001670 }
1671 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001672 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001673 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1674 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001675}
1676
Romain Guyed6fcb02011-03-21 13:11:28 -07001677void OpenGLRenderer::setupDrawPointUniforms() {
1678 int slot = mCaches.currentProgram->getUniform("pointSize");
1679 glUniform1f(slot, mDescription.pointSize);
1680}
1681
Romain Guy70ca14e2010-12-13 18:24:33 -08001682void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001683 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001684 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1685 }
1686}
1687
Romain Guy86568192010-12-14 15:55:39 -08001688void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001689 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001690 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001691 }
1692}
1693
Romain Guy70ca14e2010-12-13 18:24:33 -08001694void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001695 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001696 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001697 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001698 }
Chris Craikc3566d02013-02-04 16:16:33 -08001699 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1700 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001701 }
1702}
1703
Romain Guy8d0d4782010-12-14 20:13:35 -08001704void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001705 if (mDrawModifiers.mShader) {
1706 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001707 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001708 }
1709}
1710
Romain Guy70ca14e2010-12-13 18:24:33 -08001711void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001712 if (mDrawModifiers.mColorFilter) {
1713 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001714 }
1715}
1716
Romain Guy41210632012-07-16 17:04:24 -07001717void OpenGLRenderer::setupDrawTextGammaUniforms() {
1718 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1719}
1720
Romain Guy70ca14e2010-12-13 18:24:33 -08001721void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001722 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001723 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001724 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001725}
1726
1727void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1728 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001729 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001730 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001731}
1732
Romain Guyaa6c24c2011-04-28 18:40:04 -07001733void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1734 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001735 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001736 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001737}
1738
Romain Guy8f0095c2011-05-02 17:24:22 -07001739void OpenGLRenderer::setupDrawTextureTransform() {
1740 mDescription.hasTextureTransform = true;
1741}
1742
1743void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001744 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1745 GL_FALSE, &transform.data[0]);
1746}
1747
Romain Guy70ca14e2010-12-13 18:24:33 -08001748void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001749 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001750 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001751 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001752 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001753 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001754 }
Romain Guyd71dd362011-12-12 19:03:35 -08001755
Chris Craikcb4d6002012-09-25 12:00:29 -07001756 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001757 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001758 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001759 }
1760
1761 mCaches.unbindIndicesBuffer();
1762}
1763
Romain Guyff316ec2013-02-13 18:39:43 -08001764void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1765 bool force = mCaches.unbindMeshBuffer();
1766 GLsizei stride = sizeof(ColorTextureVertex);
1767
1768 mCaches.bindPositionVertexPointer(force, vertices, stride);
1769 if (mCaches.currentProgram->texCoords >= 0) {
1770 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1771 }
1772 int slot = mCaches.currentProgram->getAttrib("colors");
1773 if (slot >= 0) {
1774 glEnableVertexAttribArray(slot);
1775 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1776 }
1777
1778 mCaches.unbindIndicesBuffer();
1779}
1780
Romain Guy15bc6432011-12-13 13:11:32 -08001781void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1782 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001783 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001784 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001785 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001786 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001787}
1788
Chet Haase5b0200b2011-04-13 17:58:08 -07001789void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001790 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001791 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001792 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001793}
1794
Romain Guy70ca14e2010-12-13 18:24:33 -08001795void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001796}
1797
1798///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001799// Drawing
1800///////////////////////////////////////////////////////////////////////////////
1801
Chris Craikc3566d02013-02-04 16:16:33 -08001802status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty, int32_t flags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001803 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1804 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001805 if (displayList && displayList->isRenderable()) {
Romain Guy0f667532013-03-01 14:31:04 -08001806 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chris Craikc3566d02013-02-04 16:16:33 -08001807 return displayList->replay(*this, dirty, flags, 0);
1808 }
1809
1810 DeferredDisplayList deferredList;
1811 return displayList->replay(*this, dirty, flags, 0, &deferredList);
Romain Guy0fe478e2010-11-08 12:08:41 -08001812 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001813
Romain Guy65549432012-03-26 16:45:05 -07001814 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001815}
1816
Chris Craikc3566d02013-02-04 16:16:33 -08001817void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07001818 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08001819 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07001820 }
1821}
1822
Romain Guya168d732011-03-18 16:50:13 -07001823void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1824 int alpha;
1825 SkXfermode::Mode mode;
1826 getAlphaAndMode(paint, &alpha, &mode);
1827
Romain Guy886b2752013-01-04 12:26:18 -08001828 int color = paint != NULL ? paint->getColor() : 0;
1829
Romain Guya168d732011-03-18 16:50:13 -07001830 float x = left;
1831 float y = top;
1832
Romain Guy886b2752013-01-04 12:26:18 -08001833 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1834
Romain Guya168d732011-03-18 16:50:13 -07001835 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08001836 if (currentTransform().isPureTranslate()) {
1837 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
1838 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001839 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001840
1841 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001842 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001843 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001844 }
1845
Romain Guy886b2752013-01-04 12:26:18 -08001846 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1847 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1848 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001849}
1850
Chet Haase48659092012-05-31 15:21:51 -07001851status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001852 const float right = left + bitmap->width();
1853 const float bottom = top + bitmap->height();
1854
1855 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001856 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001857 }
1858
Romain Guya1d3c912011-12-13 14:55:06 -08001859 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001860 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001861 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001862 const AutoTexture autoCleanup(texture);
1863
Romain Guy211370f2012-02-01 16:10:55 -08001864 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001865 drawAlphaBitmap(texture, left, top, paint);
1866 } else {
1867 drawTextureRect(left, top, right, bottom, texture, paint);
1868 }
Chet Haase48659092012-05-31 15:21:51 -07001869
1870 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001871}
1872
Chet Haase48659092012-05-31 15:21:51 -07001873status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001874 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1875 const mat4 transform(*matrix);
1876 transform.mapRect(r);
1877
Romain Guy6926c722010-07-12 20:20:03 -07001878 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001879 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001880 }
1881
Romain Guya1d3c912011-12-13 14:55:06 -08001882 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001883 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001884 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001885 const AutoTexture autoCleanup(texture);
1886
Romain Guy5b3b3522010-10-27 18:57:51 -07001887 // This could be done in a cheaper way, all we need is pass the matrix
1888 // to the vertex shader. The save/restore is a bit overkill.
1889 save(SkCanvas::kMatrix_SaveFlag);
1890 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08001891 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1892 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
1893 } else {
1894 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1895 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001896 restore();
Chet Haase48659092012-05-31 15:21:51 -07001897
1898 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001899}
1900
Chet Haase48659092012-05-31 15:21:51 -07001901status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001902 const float right = left + bitmap->width();
1903 const float bottom = top + bitmap->height();
1904
1905 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001906 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001907 }
1908
1909 mCaches.activeTexture(0);
1910 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1911 const AutoTexture autoCleanup(texture);
1912
Romain Guy886b2752013-01-04 12:26:18 -08001913 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1914 drawAlphaBitmap(texture, left, top, paint);
1915 } else {
1916 drawTextureRect(left, top, right, bottom, texture, paint);
1917 }
Chet Haase48659092012-05-31 15:21:51 -07001918
1919 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001920}
1921
Chet Haase48659092012-05-31 15:21:51 -07001922status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001923 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001924 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001925 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001926 }
1927
Romain Guyb18d2d02011-02-10 15:52:54 -08001928 float left = FLT_MAX;
1929 float top = FLT_MAX;
1930 float right = FLT_MIN;
1931 float bottom = FLT_MIN;
1932
Romain Guya92bb4d2012-10-16 11:08:44 -07001933 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001934
Romain Guyff316ec2013-02-13 18:39:43 -08001935 ColorTextureVertex mesh[count];
1936 ColorTextureVertex* vertex = mesh;
1937
1938 bool cleanupColors = false;
1939 if (!colors) {
1940 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
1941 colors = new int[colorsCount];
1942 memset(colors, 0xff, colorsCount * sizeof(int));
1943 cleanupColors = true;
1944 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001945
Romain Guy5a7b4662011-01-20 19:09:30 -08001946 for (int32_t y = 0; y < meshHeight; y++) {
1947 for (int32_t x = 0; x < meshWidth; x++) {
1948 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1949
1950 float u1 = float(x) / meshWidth;
1951 float u2 = float(x + 1) / meshWidth;
1952 float v1 = float(y) / meshHeight;
1953 float v2 = float(y + 1) / meshHeight;
1954
1955 int ax = i + (meshWidth + 1) * 2;
1956 int ay = ax + 1;
1957 int bx = i;
1958 int by = bx + 1;
1959 int cx = i + 2;
1960 int cy = cx + 1;
1961 int dx = i + (meshWidth + 1) * 2 + 2;
1962 int dy = dx + 1;
1963
Romain Guyff316ec2013-02-13 18:39:43 -08001964 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1965 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
1966 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08001967
Romain Guyff316ec2013-02-13 18:39:43 -08001968 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1969 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
1970 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08001971
Romain Guya92bb4d2012-10-16 11:08:44 -07001972 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1973 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1974 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1975 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001976 }
1977 }
1978
Romain Guya92bb4d2012-10-16 11:08:44 -07001979 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08001980 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07001981 return DrawGlInfo::kStatusDone;
1982 }
1983
1984 mCaches.activeTexture(0);
1985 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guyff316ec2013-02-13 18:39:43 -08001986 if (!texture) {
1987 if (cleanupColors) delete[] colors;
1988 return DrawGlInfo::kStatusDone;
1989 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001990 const AutoTexture autoCleanup(texture);
1991
1992 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1993 texture->setFilter(FILTER(paint), true);
1994
1995 int alpha;
1996 SkXfermode::Mode mode;
1997 getAlphaAndMode(paint, &alpha, &mode);
1998
Romain Guyff316ec2013-02-13 18:39:43 -08001999 float a = alpha / 255.0f;
2000
Romain Guya92bb4d2012-10-16 11:08:44 -07002001 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002002 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002003 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002004
Romain Guyff316ec2013-02-13 18:39:43 -08002005 setupDraw();
2006 setupDrawWithTextureAndColor();
2007 setupDrawColor(a, a, a, a);
2008 setupDrawColorFilter();
2009 setupDrawBlending(true, mode, false);
2010 setupDrawProgram();
2011 setupDrawDirtyRegionsDisabled();
2012 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2013 setupDrawTexture(texture->id);
2014 setupDrawPureColorUniforms();
2015 setupDrawColorFilterUniforms();
2016 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2017
2018 glDrawArrays(GL_TRIANGLES, 0, count);
2019
2020 finishDrawTexture();
2021
2022 int slot = mCaches.currentProgram->getAttrib("colors");
2023 if (slot >= 0) {
2024 glDisableVertexAttribArray(slot);
2025 }
2026
2027 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002028
2029 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002030}
2031
Chet Haase48659092012-05-31 15:21:51 -07002032status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002033 float srcLeft, float srcTop, float srcRight, float srcBottom,
2034 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002035 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002036 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002037 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002038 }
2039
Romain Guya1d3c912011-12-13 14:55:06 -08002040 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002041 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002042 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002043 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002044
Romain Guy8ba548f2010-06-30 19:21:21 -07002045 const float width = texture->width;
2046 const float height = texture->height;
2047
Romain Guy68169722011-08-22 17:33:33 -07002048 const float u1 = fmax(0.0f, srcLeft / width);
2049 const float v1 = fmax(0.0f, srcTop / height);
2050 const float u2 = fmin(1.0f, srcRight / width);
2051 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07002052
Romain Guy03750a02010-10-18 14:06:08 -07002053 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002054 resetDrawTextureTexCoords(u1, v1, u2, v2);
2055
Romain Guy03750a02010-10-18 14:06:08 -07002056 int alpha;
2057 SkXfermode::Mode mode;
2058 getAlphaAndMode(paint, &alpha, &mode);
2059
Romain Guyd21b6e12011-11-30 20:21:23 -08002060 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2061
Romain Guy886b2752013-01-04 12:26:18 -08002062 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2063 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002064
Romain Guy886b2752013-01-04 12:26:18 -08002065 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2066 // Apply a scale transform on the canvas only when a shader is in use
2067 // Skia handles the ratio between the dst and src rects as a scale factor
2068 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002069 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002070 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002071
Romain Guy3b753822013-03-05 10:27:35 -08002072 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2073 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2074 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002075
2076 dstRight = x + (dstRight - dstLeft);
2077 dstBottom = y + (dstBottom - dstTop);
2078
2079 dstLeft = x;
2080 dstTop = y;
2081
2082 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2083 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002084 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002085 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002086 }
2087
2088 if (CC_UNLIKELY(useScaleTransform)) {
2089 save(SkCanvas::kMatrix_SaveFlag);
2090 translate(dstLeft, dstTop);
2091 scale(scaleX, scaleY);
2092
2093 dstLeft = 0.0f;
2094 dstTop = 0.0f;
2095
2096 dstRight = srcRight - srcLeft;
2097 dstBottom = srcBottom - srcTop;
2098 }
2099
2100 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2101 int color = paint ? paint->getColor() : 0;
2102 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2103 texture->id, paint != NULL, color, alpha, mode,
2104 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2105 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2106 } else {
2107 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2108 texture->id, alpha / 255.0f, mode, texture->blend,
2109 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2110 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2111 }
2112
2113 if (CC_UNLIKELY(useScaleTransform)) {
2114 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002115 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002116
2117 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002118
2119 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002120}
2121
Chet Haase48659092012-05-31 15:21:51 -07002122status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07002123 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07002124 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002125 int alpha;
2126 SkXfermode::Mode mode;
2127 getAlphaAndModeDirect(paint, &alpha, &mode);
2128
2129 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2130 left, top, right, bottom, alpha, mode);
2131}
2132
2133status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2134 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2135 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002136 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002137 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002138 }
2139
Romain Guybe6f9dc2012-07-16 12:41:17 -07002140 alpha *= mSnapshot->alpha;
2141
Romain Guy2728f962010-10-08 18:36:15 -07002142 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002143 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002144
Romain Guy211370f2012-02-01 16:10:55 -08002145 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002146 mCaches.activeTexture(0);
2147 Texture* texture = mCaches.textureCache.get(bitmap);
2148 if (!texture) return DrawGlInfo::kStatusDone;
2149 const AutoTexture autoCleanup(texture);
2150 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2151 texture->setFilter(GL_LINEAR, true);
2152
Romain Guy3b753822013-03-05 10:27:35 -08002153 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002154 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002155 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002156 const float offsetX = left + currentTransform().getTranslateX();
2157 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002158 const size_t count = mesh->quads.size();
2159 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002160 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002161 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002162 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2163 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2164 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002165 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002166 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002167 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002168 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002169 }
2170 }
2171
Romain Guy211370f2012-02-01 16:10:55 -08002172 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002173 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2174 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002175
2176 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2177 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2178 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2179 true, !mesh->hasEmptyQuads);
2180 } else {
2181 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2182 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2183 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2184 true, !mesh->hasEmptyQuads);
2185 }
Romain Guy054dc182010-10-15 17:55:25 -07002186 }
Chet Haase48659092012-05-31 15:21:51 -07002187
2188 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002189}
2190
Chris Craik65cd6122012-12-10 17:56:27 -08002191status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2192 bool useOffset) {
2193 if (!vertexBuffer.getSize()) {
2194 // no vertices to draw
2195 return DrawGlInfo::kStatusDone;
2196 }
2197
Chris Craikcb4d6002012-09-25 12:00:29 -07002198 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002199 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2200 bool isAA = paint->isAntiAlias();
2201
Chris Craik710f46d2012-09-17 17:25:49 -07002202 setupDraw();
2203 setupDrawNoTexture();
2204 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002205 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2206 setupDrawColorFilter();
2207 setupDrawShader();
2208 setupDrawBlending(isAA, mode);
2209 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002210 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002211 setupDrawColorUniforms();
2212 setupDrawColorFilterUniforms();
2213 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002214
Chris Craik710f46d2012-09-17 17:25:49 -07002215 void* vertices = vertexBuffer.getBuffer();
2216 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002217 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002218 mCaches.resetTexCoordsVertexPointer();
2219 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002220
Chris Craik710f46d2012-09-17 17:25:49 -07002221 int alphaSlot = -1;
2222 if (isAA) {
2223 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2224 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002225
Chris Craik710f46d2012-09-17 17:25:49 -07002226 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002227 glEnableVertexAttribArray(alphaSlot);
2228 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002229 }
Romain Guy04299382012-07-18 17:15:41 -07002230
Chris Craik710f46d2012-09-17 17:25:49 -07002231 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002232
Chris Craik710f46d2012-09-17 17:25:49 -07002233 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002234 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002235 }
Chris Craik65cd6122012-12-10 17:56:27 -08002236
2237 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002238}
2239
2240/**
Chris Craik65cd6122012-12-10 17:56:27 -08002241 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2242 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2243 * screen space in all directions. However, instead of using a fragment shader to compute the
2244 * translucency of the color from its position, we simply use a varying parameter to define how far
2245 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2246 *
2247 * Doesn't yet support joins, caps, or path effects.
2248 */
2249status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2250 VertexBuffer vertexBuffer;
2251 // TODO: try clipping large paths to viewport
2252 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2253
2254 SkRect bounds = path.getBounds();
2255 PathTessellator::expandBoundsForStroke(bounds, paint, false);
Romain Guy3b753822013-03-05 10:27:35 -08002256 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chris Craik65cd6122012-12-10 17:56:27 -08002257
2258 return drawVertexBuffer(vertexBuffer, paint);
2259}
2260
2261/**
2262 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2263 * and additional geometry for defining an alpha slope perimeter.
2264 *
2265 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2266 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2267 * in-shader alpha region, but found it to be taxing on some GPUs.
2268 *
2269 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2270 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002271 */
Chet Haase48659092012-05-31 15:21:51 -07002272status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002273 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002274
Chris Craik65cd6122012-12-10 17:56:27 -08002275 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002276
Chris Craik65cd6122012-12-10 17:56:27 -08002277 VertexBuffer buffer;
2278 SkRect bounds;
2279 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002280
2281 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2282 return DrawGlInfo::kStatusDone;
2283 }
2284
Romain Guy3b753822013-03-05 10:27:35 -08002285 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002286
Chris Craik65cd6122012-12-10 17:56:27 -08002287 bool useOffset = !paint->isAntiAlias();
2288 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002289}
2290
Chet Haase48659092012-05-31 15:21:51 -07002291status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2292 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002293
2294 // TODO: The paint's cap style defines whether the points are square or circular
2295 // TODO: Handle AA for round points
2296
Chet Haase5b0200b2011-04-13 17:58:08 -07002297 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002298 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002299 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002300 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002301 if (isHairLine) {
2302 // Now that we know it's hairline, we can set the effective width, to be used later
2303 strokeWidth = 1.0f;
2304 }
2305 const float halfWidth = strokeWidth / 2;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002306
Romain Guyed6fcb02011-03-21 13:11:28 -07002307 int alpha;
2308 SkXfermode::Mode mode;
2309 getAlphaAndMode(paint, &alpha, &mode);
2310
2311 int verticesCount = count >> 1;
2312 int generatedVerticesCount = 0;
2313
2314 TextureVertex pointsData[verticesCount];
2315 TextureVertex* vertex = &pointsData[0];
2316
Romain Guy04299382012-07-18 17:15:41 -07002317 // TODO: We should optimize this method to not generate vertices for points
2318 // that lie outside of the clip.
2319 mCaches.enableScissor();
2320
Romain Guyed6fcb02011-03-21 13:11:28 -07002321 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002322 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002323 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002324 setupDrawColor(paint->getColor(), alpha);
2325 setupDrawColorFilter();
2326 setupDrawShader();
2327 setupDrawBlending(mode);
2328 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002329 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002330 setupDrawColorUniforms();
2331 setupDrawColorFilterUniforms();
2332 setupDrawPointUniforms();
2333 setupDrawShaderIdentityUniforms();
2334 setupDrawMesh(vertex);
2335
2336 for (int i = 0; i < count; i += 2) {
2337 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2338 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002339
Chet Haase8a5cc922011-04-26 07:28:09 -07002340 float left = points[i] - halfWidth;
2341 float right = points[i] + halfWidth;
2342 float top = points[i + 1] - halfWidth;
2343 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002344
Romain Guy3b753822013-03-05 10:27:35 -08002345 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyed6fcb02011-03-21 13:11:28 -07002346 }
2347
2348 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002349
2350 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002351}
2352
Chet Haase48659092012-05-31 15:21:51 -07002353status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002354 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002355 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002356
Romain Guyae88e5e2010-10-22 17:49:18 -07002357 Rect& clip(*mSnapshot->clipRect);
2358 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002359
Romain Guy3d58c032010-07-14 16:34:53 -07002360 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002361
2362 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002363}
Romain Guy9d5316e2010-06-24 19:30:36 -07002364
Chet Haase48659092012-05-31 15:21:51 -07002365status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2366 SkPaint* paint) {
2367 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002368 const AutoTexture autoCleanup(texture);
2369
2370 const float x = left + texture->left - texture->offset;
2371 const float y = top + texture->top - texture->offset;
2372
2373 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002374
2375 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002376}
2377
Chet Haase48659092012-05-31 15:21:51 -07002378status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002379 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002380 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002381 return DrawGlInfo::kStatusDone;
2382 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002383
Chris Craikcb4d6002012-09-25 12:00:29 -07002384 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002385 mCaches.activeTexture(0);
2386 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2387 right - left, bottom - top, rx, ry, p);
2388 return drawShape(left, top, texture, p);
2389 }
2390
2391 SkPath path;
2392 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002393 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2394 float outset = p->getStrokeWidth() / 2;
2395 rect.outset(outset, outset);
2396 rx += outset;
2397 ry += outset;
2398 }
Chris Craik710f46d2012-09-17 17:25:49 -07002399 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002400 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002401}
2402
Chris Craik710f46d2012-09-17 17:25:49 -07002403status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002404 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2405 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002406 return DrawGlInfo::kStatusDone;
2407 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002408 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002409 mCaches.activeTexture(0);
2410 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2411 return drawShape(x - radius, y - radius, texture, p);
2412 }
2413
2414 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002415 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2416 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2417 } else {
2418 path.addCircle(x, y, radius);
2419 }
Chris Craik65cd6122012-12-10 17:56:27 -08002420 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002421}
Romain Guy01d58e42011-01-19 21:54:02 -08002422
Chet Haase48659092012-05-31 15:21:51 -07002423status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002424 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002425 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002426 return DrawGlInfo::kStatusDone;
2427 }
Romain Guy01d58e42011-01-19 21:54:02 -08002428
Chris Craikcb4d6002012-09-25 12:00:29 -07002429 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002430 mCaches.activeTexture(0);
2431 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2432 return drawShape(left, top, texture, p);
2433 }
2434
2435 SkPath path;
2436 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002437 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2438 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2439 }
Chris Craik710f46d2012-09-17 17:25:49 -07002440 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002441 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002442}
2443
Chet Haase48659092012-05-31 15:21:51 -07002444status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002445 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
2446 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
2447 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002448 }
2449
Chris Craik780c1282012-10-04 14:10:49 -07002450 if (fabs(sweepAngle) >= 360.0f) {
2451 return drawOval(left, top, right, bottom, p);
2452 }
2453
2454 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002455 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002456 mCaches.activeTexture(0);
2457 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2458 startAngle, sweepAngle, useCenter, p);
2459 return drawShape(left, top, texture, p);
2460 }
2461
2462 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2463 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2464 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2465 }
2466
2467 SkPath path;
2468 if (useCenter) {
2469 path.moveTo(rect.centerX(), rect.centerY());
2470 }
2471 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2472 if (useCenter) {
2473 path.close();
2474 }
Chris Craik65cd6122012-12-10 17:56:27 -08002475 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002476}
2477
Romain Guycf8675e2012-10-02 12:32:25 -07002478// See SkPaintDefaults.h
2479#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2480
Chet Haase48659092012-05-31 15:21:51 -07002481status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002482 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002483 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002484 }
2485
Chris Craik710f46d2012-09-17 17:25:49 -07002486 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002487 // only fill style is supported by drawConvexPath, since others have to handle joins
2488 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2489 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2490 mCaches.activeTexture(0);
2491 const PathTexture* texture =
2492 mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2493 return drawShape(left, top, texture, p);
2494 }
2495
2496 SkPath path;
2497 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2498 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2499 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2500 }
2501 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002502 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002503 }
2504
Romain Guy3b753822013-03-05 10:27:35 -08002505 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002506 SkPath path;
2507 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002508 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002509 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002510 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002511 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002512 }
Romain Guyc7d53492010-06-25 13:41:57 -07002513}
Romain Guy9d5316e2010-06-24 19:30:36 -07002514
Raph Levien416a8472012-07-19 22:48:17 -07002515void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2516 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2517 float x, float y) {
2518 mCaches.activeTexture(0);
2519
2520 // NOTE: The drop shadow will not perform gamma correction
2521 // if shader-based correction is enabled
2522 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2523 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002524 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Raph Levien416a8472012-07-19 22:48:17 -07002525 const AutoTexture autoCleanup(shadow);
2526
Chris Craikc3566d02013-02-04 16:16:33 -08002527 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2528 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002529
Chris Craikc3566d02013-02-04 16:16:33 -08002530 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2531 int shadowColor = mDrawModifiers.mShadowColor;
2532 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002533 shadowColor = 0xffffffff;
2534 }
2535
2536 setupDraw();
2537 setupDrawWithTexture(true);
2538 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2539 setupDrawColorFilter();
2540 setupDrawShader();
2541 setupDrawBlending(true, mode);
2542 setupDrawProgram();
2543 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2544 setupDrawTexture(shadow->id);
2545 setupDrawPureColorUniforms();
2546 setupDrawColorFilterUniforms();
2547 setupDrawShaderUniforms();
2548 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2549
2550 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2551}
2552
Romain Guy768bffc2013-02-27 13:50:45 -08002553bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2554 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2555 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2556}
2557
Chet Haase48659092012-05-31 15:21:51 -07002558status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002559 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002560 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002561 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002562 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002563
Romain Guy671d6cf2012-01-18 12:39:17 -08002564 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002565 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002566 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002567 }
2568
2569 float x = 0.0f;
2570 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002571 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002572 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002573 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2574 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002575 }
2576
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002577 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002578 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002579
2580 int alpha;
2581 SkXfermode::Mode mode;
2582 getAlphaAndMode(paint, &alpha, &mode);
2583
Chris Craikc3566d02013-02-04 16:16:33 -08002584 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002585 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2586 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002587 }
2588
Romain Guy671d6cf2012-01-18 12:39:17 -08002589 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002590 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002591 if (pureTranslate && !linearFilter) {
2592 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2593 }
2594
2595 mCaches.activeTexture(0);
2596 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002597 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002598 setupDrawDirtyRegionsDisabled();
2599 setupDrawWithTexture(true);
2600 setupDrawAlpha8Color(paint->getColor(), alpha);
2601 setupDrawColorFilter();
2602 setupDrawShader();
2603 setupDrawBlending(true, mode);
2604 setupDrawProgram();
2605 setupDrawModelView(x, y, x, y, pureTranslate, true);
2606 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2607 setupDrawPureColorUniforms();
2608 setupDrawColorFilterUniforms();
2609 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002610 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002611
2612 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2613 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2614
Romain Guy211370f2012-02-01 16:10:55 -08002615 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002616
2617 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2618 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002619 if (hasActiveLayer) {
2620 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002621 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002622 }
2623 dirtyLayerUnchecked(bounds, getRegion());
2624 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002625 }
Chet Haase48659092012-05-31 15:21:51 -07002626
2627 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002628}
2629
Romain Guy624234f2013-03-05 16:43:31 -08002630mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2631 mat4 fontTransform;
2632 if (CC_LIKELY(transform.isPureTranslate())) {
2633 fontTransform = mat4::identity();
2634 } else {
2635 if (CC_UNLIKELY(transform.isPerspective())) {
2636 // When the below condition is true, we are rendering text with a
2637 // perspective transform inside a layer (either an inline layer
2638 // created by Canvas.saveLayer() or a hardware layer.)
2639 if (hasLayer() || getTargetFbo() != 0) {
2640 float sx, sy;
2641 currentTransform().decomposeScale(sx, sy);
2642 fontTransform.loadScale(sx, sy, 1.0f);
2643 } else {
2644 fontTransform = mat4::identity();
2645 }
2646 } else {
2647 float sx, sy;
2648 currentTransform().decomposeScale(sx, sy);
2649 fontTransform.loadScale(sx, sy, 1.0f);
2650 }
2651 }
2652 return fontTransform;
2653}
2654
Romain Guyc2525952012-07-27 16:41:22 -07002655status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002656 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy768bffc2013-02-27 13:50:45 -08002657 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002658 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002659 }
2660
Chet Haasea1cff502012-02-21 13:43:44 -08002661 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002662 switch (paint->getTextAlign()) {
2663 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002664 x -= length / 2.0f;
2665 break;
2666 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002667 x -= length;
2668 break;
2669 default:
2670 break;
2671 }
2672
Romain Guycac5fd32011-12-01 20:08:50 -08002673 SkPaint::FontMetrics metrics;
2674 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002675 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002676 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002677 }
2678
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002679 const float oldX = x;
2680 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002681
2682 const mat4& transform = currentTransform();
2683 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002684
Romain Guy211370f2012-02-01 16:10:55 -08002685 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002686 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2687 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002688 }
2689
Romain Guy86568192010-12-14 15:55:39 -08002690 int alpha;
2691 SkXfermode::Mode mode;
2692 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002693
Romain Guyc74f45a2013-02-26 19:10:14 -08002694 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2695
Chris Craikc3566d02013-02-04 16:16:33 -08002696 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002697 fontRenderer.setFont(paint, mat4::identity());
2698 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2699 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002700 }
2701
Romain Guy19d4dd82013-03-04 11:14:26 -08002702 const bool hasActiveLayer = hasLayer();
2703
Romain Guy624234f2013-03-05 16:43:31 -08002704 // We only pass a partial transform to the font renderer. That partial
2705 // matrix defines how glyphs are rasterized. Typically we want glyphs
2706 // to be rasterized at their final size on screen, which means the partial
2707 // matrix needs to take the scale factor into account.
2708 // When a partial matrix is used to transform glyphs during rasterization,
2709 // the mesh is generated with the inverse transform (in the case of scale,
2710 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2711 // apply the full transform matrix at draw time in the vertex shader.
2712 // Applying the full matrix in the shader is the easiest way to handle
2713 // rotation and perspective and allows us to always generated quads in the
2714 // font renderer which greatly simplifies the code, clipping in particular.
2715 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002716 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002717
Romain Guy6620c6d2010-12-06 18:07:02 -08002718 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002719 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -07002720
Romain Guy16c88082012-06-11 16:03:47 -07002721 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002722 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002723 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002724 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002725 setupDrawDirtyRegionsDisabled();
2726 setupDrawWithTexture(true);
2727 setupDrawAlpha8Color(paint->getColor(), alpha);
2728 setupDrawColorFilter();
2729 setupDrawShader();
2730 setupDrawBlending(true, mode);
2731 setupDrawProgram();
Romain Guy624234f2013-03-05 16:43:31 -08002732 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002733 // See comment above; the font renderer must use texture unit 0
2734 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002735 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2736 setupDrawPureColorUniforms();
2737 setupDrawColorFilterUniforms();
Romain Guy624234f2013-03-05 16:43:31 -08002738 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002739 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002740
Romain Guy624234f2013-03-05 16:43:31 -08002741 // TODO: Implement better clipping for scaled/rotated text
2742 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Romain Guy5b3b3522010-10-27 18:57:51 -07002743 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2744
Raph Levien996e57c2012-07-23 15:22:52 -07002745 bool status;
Romain Guya3dc55f2012-09-28 13:55:44 -07002746 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002747 SkPaint paintCopy(*paint);
2748 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2749 status = fontRenderer.renderPosText(&paintCopy, 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 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002752 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002753 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002754 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002755
2756 if (status && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002757 if (!pureTranslate) {
2758 transform.mapRect(bounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002759 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002760 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002761 }
Romain Guy694b5192010-07-21 21:33:20 -07002762
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002763 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002764
2765 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002766}
2767
Chet Haase48659092012-05-31 15:21:51 -07002768status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002769 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002770 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002771 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002772 }
2773
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002774 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002775 fontRenderer.setFont(paint, mat4::identity());
Romain Guy03d58522012-02-24 17:54:07 -08002776
2777 int alpha;
2778 SkXfermode::Mode mode;
2779 getAlphaAndMode(paint, &alpha, &mode);
2780
2781 mCaches.activeTexture(0);
2782 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002783 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002784 setupDrawDirtyRegionsDisabled();
2785 setupDrawWithTexture(true);
2786 setupDrawAlpha8Color(paint->getColor(), alpha);
2787 setupDrawColorFilter();
2788 setupDrawShader();
2789 setupDrawBlending(true, mode);
2790 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002791 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002792 setupDrawTexture(fontRenderer.getTexture(true));
2793 setupDrawPureColorUniforms();
2794 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002795 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002796 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002797
Romain Guy97771732012-02-28 18:17:02 -08002798 const Rect* clip = &mSnapshot->getLocalClip();
2799 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 -08002800
Romain Guy97771732012-02-28 18:17:02 -08002801 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002802
2803 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2804 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002805 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08002806 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002807 dirtyLayerUnchecked(bounds, getRegion());
2808 }
Romain Guy97771732012-02-28 18:17:02 -08002809 }
Chet Haase48659092012-05-31 15:21:51 -07002810
2811 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002812}
2813
Chet Haase48659092012-05-31 15:21:51 -07002814status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2815 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002816
Romain Guya1d3c912011-12-13 14:55:06 -08002817 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002818
Romain Guyfb8b7632010-08-23 21:05:08 -07002819 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002820 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002821 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002822
Romain Guy8b55f372010-08-18 17:10:07 -07002823 const float x = texture->left - texture->offset;
2824 const float y = texture->top - texture->offset;
2825
Romain Guy01d58e42011-01-19 21:54:02 -08002826 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002827
2828 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002829}
2830
Chet Haase48659092012-05-31 15:21:51 -07002831status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002832 if (!layer) {
2833 return DrawGlInfo::kStatusDone;
2834 }
2835
Romain Guyb2e2f242012-10-17 18:18:35 -07002836 mat4* transform = NULL;
2837 if (layer->isTextureLayer()) {
2838 transform = &layer->getTransform();
2839 if (!transform->isIdentity()) {
2840 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08002841 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002842 }
2843 }
2844
Romain Guy35643dd2012-09-18 15:40:58 -07002845 Rect transformed;
2846 Rect clip;
2847 const bool rejected = quickRejectNoScissor(x, y,
2848 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2849
2850 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002851 if (transform && !transform->isIdentity()) {
2852 restore();
2853 }
Chet Haase48659092012-05-31 15:21:51 -07002854 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002855 }
2856
Romain Guy5bb3c732012-11-29 17:52:58 -08002857 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002858
Romain Guy87e2f7572012-09-24 11:37:12 -07002859 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002860 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002861
Romain Guy211370f2012-02-01 16:10:55 -08002862 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08002863 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
2864 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07002865
Romain Guyc88e3572011-01-22 00:32:12 -08002866 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002867 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002868 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002869 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002870 setupDraw();
2871 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002872 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002873 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002874 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002875 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002876 setupDrawPureColorUniforms();
2877 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002878 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08002879 if (CC_LIKELY(currentTransform().isPureTranslate())) {
2880 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2881 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002882
Romain Guyd21b6e12011-11-30 20:21:23 -08002883 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002884 setupDrawModelViewTranslate(tx, ty,
2885 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002886 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002887 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002888 setupDrawModelViewTranslate(x, y,
2889 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2890 }
Romain Guyc88e3572011-01-22 00:32:12 -08002891 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002892
Romain Guyc88e3572011-01-22 00:32:12 -08002893 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2894 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002895
Romain Guyc88e3572011-01-22 00:32:12 -08002896 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002897
2898#if DEBUG_LAYERS_AS_REGIONS
2899 drawRegionRects(layer->region);
2900#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002901 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002902
Chris Craikc3566d02013-02-04 16:16:33 -08002903 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07002904
Romain Guy5bb3c732012-11-29 17:52:58 -08002905 if (layer->debugDrawUpdate) {
2906 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002907 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2908 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2909 }
Romain Guyf219da52011-01-16 12:54:25 -08002910 }
Chet Haase48659092012-05-31 15:21:51 -07002911
Romain Guyb2e2f242012-10-17 18:18:35 -07002912 if (transform && !transform->isIdentity()) {
2913 restore();
2914 }
2915
Chet Haase48659092012-05-31 15:21:51 -07002916 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002917}
2918
Romain Guy6926c722010-07-12 20:20:03 -07002919///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002920// Shaders
2921///////////////////////////////////////////////////////////////////////////////
2922
2923void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08002924 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002925}
2926
Romain Guy06f96e22010-07-30 19:18:16 -07002927void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08002928 mDrawModifiers.mShader = shader;
2929 if (mDrawModifiers.mShader) {
2930 mDrawModifiers.mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002931 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002932}
2933
Romain Guyd27977d2010-07-14 19:18:51 -07002934///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002935// Color filters
2936///////////////////////////////////////////////////////////////////////////////
2937
2938void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08002939 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07002940}
2941
2942void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08002943 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07002944}
2945
2946///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002947// Drop shadow
2948///////////////////////////////////////////////////////////////////////////////
2949
2950void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08002951 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07002952}
2953
2954void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08002955 mDrawModifiers.mHasShadow = true;
2956 mDrawModifiers.mShadowRadius = radius;
2957 mDrawModifiers.mShadowDx = dx;
2958 mDrawModifiers.mShadowDy = dy;
2959 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07002960}
2961
2962///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002963// Draw filters
2964///////////////////////////////////////////////////////////////////////////////
2965
2966void OpenGLRenderer::resetPaintFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08002967 mDrawModifiers.mHasDrawFilter = false;
Romain Guy5ff9df62012-01-23 17:09:05 -08002968}
2969
2970void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08002971 mDrawModifiers.mHasDrawFilter = true;
2972 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2973 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08002974}
2975
Romain Guy758724f2013-02-27 11:53:12 -08002976SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint, bool alwaysCopy) {
2977 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
2978 if (CC_UNLIKELY(alwaysCopy)) {
2979 mFilteredPaint = *paint;
2980 return &mFilteredPaint;
2981 }
2982 return paint;
2983 }
Romain Guy5ff9df62012-01-23 17:09:05 -08002984
2985 uint32_t flags = paint->getFlags();
2986
2987 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08002988 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
2989 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08002990
2991 return &mFilteredPaint;
2992}
2993
2994///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002995// Drawing implementation
2996///////////////////////////////////////////////////////////////////////////////
2997
Romain Guy01d58e42011-01-19 21:54:02 -08002998void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2999 float x, float y, SkPaint* paint) {
3000 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3001 return;
3002 }
3003
3004 int alpha;
3005 SkXfermode::Mode mode;
3006 getAlphaAndMode(paint, &alpha, &mode);
3007
3008 setupDraw();
3009 setupDrawWithTexture(true);
3010 setupDrawAlpha8Color(paint->getColor(), alpha);
3011 setupDrawColorFilter();
3012 setupDrawShader();
3013 setupDrawBlending(true, mode);
3014 setupDrawProgram();
3015 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3016 setupDrawTexture(texture->id);
3017 setupDrawPureColorUniforms();
3018 setupDrawColorFilterUniforms();
3019 setupDrawShaderUniforms();
3020 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3021
3022 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3023
3024 finishDrawTexture();
3025}
3026
Romain Guyf607bdc2010-09-10 19:20:06 -07003027// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003028#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3029#define kStdUnderline_Offset (1.0f / 9.0f)
3030#define kStdUnderline_Thickness (1.0f / 18.0f)
3031
3032void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3033 float x, float y, SkPaint* paint) {
3034 // Handle underline and strike-through
3035 uint32_t flags = paint->getFlags();
3036 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003037 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003038 float underlineWidth = length;
3039 // If length is > 0.0f, we already measured the text for the text alignment
3040 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003041 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003042 }
3043
Romain Guy211370f2012-02-01 16:10:55 -08003044 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003045 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003046 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003047
Raph Levien8b4072d2012-07-30 15:50:00 -07003048 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003049 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003050
Romain Guyf6834472011-01-23 13:32:12 -08003051 int linesCount = 0;
3052 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3053 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3054
3055 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003056 float points[pointsCount];
3057 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003058
3059 if (flags & SkPaint::kUnderlineText_Flag) {
3060 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003061 points[currentPoint++] = left;
3062 points[currentPoint++] = top;
3063 points[currentPoint++] = left + underlineWidth;
3064 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003065 }
3066
3067 if (flags & SkPaint::kStrikeThruText_Flag) {
3068 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003069 points[currentPoint++] = left;
3070 points[currentPoint++] = top;
3071 points[currentPoint++] = left + underlineWidth;
3072 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003073 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003074
Romain Guy726aeba2011-06-01 14:52:00 -07003075 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003076
Romain Guy726aeba2011-06-01 14:52:00 -07003077 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003078 }
3079 }
3080}
3081
Romain Guy672433d2013-01-04 19:05:13 -08003082status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3083 if (mSnapshot->isIgnored()) {
3084 return DrawGlInfo::kStatusDone;
3085 }
3086
Romain Guy735738c2012-12-03 12:34:51 -08003087 int color = paint->getColor();
3088 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003089 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003090 color |= 0x00ffffff;
3091 }
3092 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3093
3094 return drawColorRects(rects, count, color, mode);
3095}
3096
3097status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003098 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003099 if (count == 0) {
3100 return DrawGlInfo::kStatusDone;
3101 }
Romain Guy735738c2012-12-03 12:34:51 -08003102
Romain Guy672433d2013-01-04 19:05:13 -08003103 float left = FLT_MAX;
3104 float top = FLT_MAX;
3105 float right = FLT_MIN;
3106 float bottom = FLT_MIN;
3107
3108 int vertexCount = 0;
3109 Vertex mesh[count * 6];
3110 Vertex* vertex = mesh;
3111
Chris Craik2af46352012-11-26 18:30:17 -08003112 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003113 float l = rects[index + 0];
3114 float t = rects[index + 1];
3115 float r = rects[index + 2];
3116 float b = rects[index + 3];
3117
Romain Guy3b753822013-03-05 10:27:35 -08003118 Vertex::set(vertex++, l, b);
3119 Vertex::set(vertex++, l, t);
3120 Vertex::set(vertex++, r, t);
3121 Vertex::set(vertex++, l, b);
3122 Vertex::set(vertex++, r, t);
3123 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003124
Romain Guy3b753822013-03-05 10:27:35 -08003125 vertexCount += 6;
Romain Guy672433d2013-01-04 19:05:13 -08003126
Romain Guy3b753822013-03-05 10:27:35 -08003127 left = fminf(left, l);
3128 top = fminf(top, t);
3129 right = fmaxf(right, r);
3130 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003131 }
3132
Romain Guy3b753822013-03-05 10:27:35 -08003133 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003134 return DrawGlInfo::kStatusDone;
3135 }
Romain Guy672433d2013-01-04 19:05:13 -08003136
Romain Guy672433d2013-01-04 19:05:13 -08003137 setupDraw();
3138 setupDrawNoTexture();
3139 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3140 setupDrawShader();
3141 setupDrawColorFilter();
3142 setupDrawBlending(mode);
3143 setupDrawProgram();
3144 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003145 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003146 setupDrawColorUniforms();
3147 setupDrawShaderUniforms();
3148 setupDrawColorFilterUniforms();
3149 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3150
Romain Guy8ce00302013-01-15 18:51:42 -08003151 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003152 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003153 }
3154
3155 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3156
3157 return DrawGlInfo::kStatusDrew;
3158}
3159
Romain Guy026c5e162010-06-28 17:12:22 -07003160void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003161 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003162 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003163 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003164 color |= 0x00ffffff;
3165 }
3166
Romain Guy70ca14e2010-12-13 18:24:33 -08003167 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003168 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003169 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003170 setupDrawShader();
3171 setupDrawColorFilter();
3172 setupDrawBlending(mode);
3173 setupDrawProgram();
3174 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3175 setupDrawColorUniforms();
3176 setupDrawShaderUniforms(ignoreTransform);
3177 setupDrawColorFilterUniforms();
3178 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003179
Romain Guyc95c8d62010-09-17 15:31:32 -07003180 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3181}
3182
Romain Guy82ba8142010-07-09 13:25:56 -07003183void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003184 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003185 int alpha;
3186 SkXfermode::Mode mode;
3187 getAlphaAndMode(paint, &alpha, &mode);
3188
Romain Guyd21b6e12011-11-30 20:21:23 -08003189 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003190
Romain Guy3b753822013-03-05 10:27:35 -08003191 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3192 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3193 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003194
Romain Guyd21b6e12011-11-30 20:21:23 -08003195 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003196 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3197 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3198 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3199 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003200 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003201 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3202 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3203 GL_TRIANGLE_STRIP, gMeshCount);
3204 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003205}
3206
Romain Guybd6b79b2010-06-26 00:13:53 -07003207void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003208 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3209 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003210 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003211}
3212
3213void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003214 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003215 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003216 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003217
Romain Guy746b7402010-10-26 16:27:31 -07003218 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003219 setupDrawWithTexture();
3220 setupDrawColor(alpha, alpha, alpha, alpha);
3221 setupDrawColorFilter();
3222 setupDrawBlending(blend, mode, swapSrcDst);
3223 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003224 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003225 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003226 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003227 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003228 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003229 }
Romain Guy886b2752013-01-04 12:26:18 -08003230 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003231 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003232 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003233 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003234
Romain Guy6820ac82010-09-15 18:11:50 -07003235 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003236
3237 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003238}
3239
Romain Guy886b2752013-01-04 12:26:18 -08003240void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3241 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3242 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3243 bool ignoreTransform, bool dirty) {
3244
3245 setupDraw();
3246 setupDrawWithTexture(true);
3247 if (hasColor) {
3248 setupDrawAlpha8Color(color, alpha);
3249 }
3250 setupDrawColorFilter();
3251 setupDrawShader();
3252 setupDrawBlending(true, mode);
3253 setupDrawProgram();
3254 if (!dirty) setupDrawDirtyRegionsDisabled();
3255 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3256 setupDrawTexture(texture);
3257 setupDrawPureColorUniforms();
3258 setupDrawColorFilterUniforms();
3259 setupDrawShaderUniforms();
3260 setupDrawMesh(vertices, texCoords);
3261
3262 glDrawArrays(drawMode, 0, elementsCount);
3263
3264 finishDrawTexture();
3265}
3266
Romain Guya5aed0d2010-09-09 14:42:43 -07003267void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003268 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003269 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003270
Romain Guy82ba8142010-07-09 13:25:56 -07003271 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003272 // These blend modes are not supported by OpenGL directly and have
3273 // to be implemented using shaders. Since the shader will perform
3274 // the blending, turn blending off here
3275 // If the blend mode cannot be implemented using shaders, fall
3276 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003277 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003278 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003279 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003280 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003281
Romain Guy82bc7a72012-01-03 14:13:39 -08003282 if (mCaches.blend) {
3283 glDisable(GL_BLEND);
3284 mCaches.blend = false;
3285 }
3286
3287 return;
3288 } else {
3289 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003290 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003291 }
3292
3293 if (!mCaches.blend) {
3294 glEnable(GL_BLEND);
3295 }
3296
3297 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3298 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3299
3300 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3301 glBlendFunc(sourceMode, destMode);
3302 mCaches.lastSrcMode = sourceMode;
3303 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003304 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003305 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003306 glDisable(GL_BLEND);
3307 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003308 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003309}
3310
Romain Guy889f8d12010-07-29 14:37:42 -07003311bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003312 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003313 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003314 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003315 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003316 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003317 }
Romain Guy6926c722010-07-12 20:20:03 -07003318 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003319}
3320
Romain Guy026c5e162010-06-28 17:12:22 -07003321void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003322 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003323 TextureVertex::setUV(v++, u1, v1);
3324 TextureVertex::setUV(v++, u2, v1);
3325 TextureVertex::setUV(v++, u1, v2);
3326 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003327}
3328
Chet Haase5c13d892010-10-08 08:37:55 -07003329void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003330 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003331 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003332}
3333
Romain Guy9d5316e2010-06-24 19:30:36 -07003334}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003335}; // namespace android