blob: ea4e73b2bb065c935320ff17fc4b5a6f0f70ef49 [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 Craikd0afeac2013-03-15 18:43:11 -0700115 resetDrawModifiers();
Romain Guy026c5e162010-06-28 17:12:22 -0700116
Romain Guyac670c02010-07-27 17:39:27 -0700117 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
118
Romain Guyae5575b2010-07-29 18:48:04 -0700119 mFirstSnapshot = new Snapshot;
Romain Guy87e2f7572012-09-24 11:37:12 -0700120
121 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700122}
123
Romain Guy85bf02f2010-06-22 13:11:24 -0700124OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700125 // The context has already been destroyed at this point, do not call
126 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700127}
128
Romain Guy87e2f7572012-09-24 11:37:12 -0700129void OpenGLRenderer::initProperties() {
130 char property[PROPERTY_VALUE_MAX];
131 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
132 mScissorOptimizationDisabled = !strcasecmp(property, "true");
133 INIT_LOGD(" Scissor optimization %s",
134 mScissorOptimizationDisabled ? "disabled" : "enabled");
135 } else {
136 INIT_LOGD(" Scissor optimization enabled");
137 }
Romain Guy13631f32012-01-30 17:41:55 -0800138}
139
140///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700141// Setup
142///////////////////////////////////////////////////////////////////////////////
143
Romain Guyef359272013-01-31 19:07:29 -0800144void OpenGLRenderer::setName(const char* name) {
145 if (name) {
146 mName.setTo(name);
147 } else {
148 mName.clear();
149 }
150}
151
152const char* OpenGLRenderer::getName() const {
153 return mName.string();
154}
155
Romain Guy49c5fc02012-05-15 11:10:01 -0700156bool OpenGLRenderer::isDeferred() {
157 return false;
158}
159
Romain Guy85bf02f2010-06-22 13:11:24 -0700160void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700161 initViewport(width, height);
162
163 glDisable(GL_DITHER);
164 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
165
166 glEnableVertexAttribArray(Program::kBindingPosition);
167}
168
169void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700170 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700171
172 mWidth = width;
173 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700174
175 mFirstSnapshot->height = height;
176 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700177}
178
Romain Guy7c25aab2012-10-18 15:05:02 -0700179status_t OpenGLRenderer::prepare(bool opaque) {
Chet Haase44b2fe32012-06-06 19:03:58 -0700180 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800181}
182
Romain Guyc3fedaf2013-01-29 17:26:25 -0800183status_t OpenGLRenderer::prepareDirty(float left, float top,
184 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800185 mCaches.clearGarbage();
186
Romain Guy8aef54f2010-09-01 15:13:49 -0700187 mSnapshot = new Snapshot(mFirstSnapshot,
188 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800189 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700190 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700191
Romain Guy7d7b5492011-01-24 16:33:45 -0800192 mSnapshot->setClip(left, top, right, bottom);
Romain Guy41308e22012-10-22 20:02:43 -0700193 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700194
Romain Guy11cb6422012-09-21 00:39:43 -0700195 updateLayers();
196
Romain Guydcfc8362013-01-03 13:08:57 -0800197 discardFramebuffer(left, top, right, bottom);
Romain Guy45e4c3d2012-09-11 17:17:07 -0700198
Romain Guyddf74372012-05-22 14:07:07 -0700199 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800200
Romain Guy54c1a642012-09-27 17:55:46 -0700201 // Functors break the tiling extension in pretty spectacular ways
202 // This ensures we don't use tiling when a functor is going to be
203 // invoked during the frame
204 mSuppressTiling = mCaches.hasRegisteredFunctors();
205
Romain Guy2b7028e2012-09-19 17:25:38 -0700206 mTilingSnapshot = mSnapshot;
Romain Guy57b52682012-09-20 17:38:46 -0700207 startTiling(mTilingSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700208
Romain Guy7c450aa2012-09-21 19:15:00 -0700209 debugOverdraw(true, true);
210
Romain Guy7c25aab2012-10-18 15:05:02 -0700211 return clear(left, top, right, bottom, opaque);
212}
213
Romain Guydcfc8362013-01-03 13:08:57 -0800214void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
215 // If we know that we are going to redraw the entire framebuffer,
216 // perform a discard to let the driver know we don't need to preserve
217 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800218 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800219 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800220 const bool isFbo = getTargetFbo() == 0;
221 const GLenum attachments[] = {
222 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
223 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800224 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
225 }
226}
227
Romain Guy7c25aab2012-10-18 15:05:02 -0700228status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700229 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700230 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700231 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700232 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700233 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700234 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700235
Romain Guy7c25aab2012-10-18 15:05:02 -0700236 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700237 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700238}
239
240void OpenGLRenderer::syncState() {
241 glViewport(0, 0, mWidth, mHeight);
242
243 if (mCaches.blend) {
244 glEnable(GL_BLEND);
245 } else {
246 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700247 }
Romain Guybb9524b2010-06-22 18:56:38 -0700248}
249
Romain Guy57b52682012-09-20 17:38:46 -0700250void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700251 if (!mSuppressTiling) {
252 Rect* clip = mTilingSnapshot->clipRect;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800253 if (s->flags & Snapshot::kFlagFboTarget) {
254 clip = &s->layer->clipRect;
Romain Guy54c1a642012-09-27 17:55:46 -0700255 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700256
Romain Guyc3fedaf2013-01-29 17:26:25 -0800257 startTiling(*clip, s->height, opaque);
258 }
259}
260
261void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
262 if (!mSuppressTiling) {
263 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800264 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700265 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700266}
267
268void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700269 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700270}
271
Romain Guyb025b9c2010-09-16 14:16:48 -0700272void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700273 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700274 endTiling();
275
Romain Guyca89e2a2013-03-08 17:44:20 -0800276 // When finish() is invoked on FBO 0 we've reached the end
277 // of the current frame
278 if (getTargetFbo() == 0) {
279 mCaches.pathCache.trim();
280 }
281
Romain Guy11cb6422012-09-21 00:39:43 -0700282 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700283#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700284 GLenum status = GL_NO_ERROR;
285 while ((status = glGetError()) != GL_NO_ERROR) {
286 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
287 switch (status) {
288 case GL_INVALID_ENUM:
289 ALOGE(" GL_INVALID_ENUM");
290 break;
291 case GL_INVALID_VALUE:
292 ALOGE(" GL_INVALID_VALUE");
293 break;
294 case GL_INVALID_OPERATION:
295 ALOGE(" GL_INVALID_OPERATION");
296 break;
297 case GL_OUT_OF_MEMORY:
298 ALOGE(" Out of memory!");
299 break;
300 }
Romain Guya07105b2011-01-10 21:14:18 -0800301 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700302#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700303
Romain Guyc15008e2010-11-10 11:59:15 -0800304#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800305 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700306#else
307 if (mCaches.getDebugLevel() & kDebugMemory) {
308 mCaches.dumpMemoryUsage();
309 }
Romain Guyc15008e2010-11-10 11:59:15 -0800310#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700311 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700312}
313
Romain Guy6c319ca2011-01-11 14:29:25 -0800314void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700315 if (mCaches.currentProgram) {
316 if (mCaches.currentProgram->isInUse()) {
317 mCaches.currentProgram->remove();
318 mCaches.currentProgram = NULL;
319 }
320 }
Romain Guy50c0f092010-10-19 11:42:22 -0700321 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800322 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800323 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800324 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700325 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700326}
327
Romain Guy6c319ca2011-01-11 14:29:25 -0800328void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800329 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800330 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700331 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700332 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700333
Romain Guy3e263fa2011-12-12 16:47:48 -0800334 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
335
Chet Haase80250612012-08-15 13:46:54 -0700336 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700337 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800338 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700339 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700340
Romain Guya1d3c912011-12-13 14:55:06 -0800341 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700342
Romain Guy50c0f092010-10-19 11:42:22 -0700343 mCaches.blend = true;
344 glEnable(GL_BLEND);
345 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
346 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700347}
348
Romain Guy35643dd2012-09-18 15:40:58 -0700349void OpenGLRenderer::resumeAfterLayer() {
350 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
351 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
352 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700353 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700354
355 mCaches.resetScissor();
356 dirtyClip();
357}
358
Romain Guyba6be8a2012-04-23 18:22:09 -0700359void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700360 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700361}
362
363void OpenGLRenderer::attachFunctor(Functor* functor) {
364 mFunctors.add(functor);
365}
366
Romain Guy8f3b8e32012-03-27 16:33:45 -0700367status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
368 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700369 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700370
Romain Guyba6be8a2012-04-23 18:22:09 -0700371 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800372 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700373 SortedVector<Functor*> functors(mFunctors);
374 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700375
Romain Guyba6be8a2012-04-23 18:22:09 -0700376 DrawGlInfo info;
377 info.clipLeft = 0;
378 info.clipTop = 0;
379 info.clipRight = 0;
380 info.clipBottom = 0;
381 info.isLayer = false;
382 info.width = 0;
383 info.height = 0;
384 memset(info.transform, 0, sizeof(float) * 16);
385
386 for (size_t i = 0; i < count; i++) {
387 Functor* f = functors.itemAt(i);
388 result |= (*f)(DrawGlInfo::kModeProcess, &info);
389
Chris Craikc2c95432012-04-25 15:13:52 -0700390 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700391 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
392 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700393 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700394
Chris Craikc2c95432012-04-25 15:13:52 -0700395 if (result & DrawGlInfo::kStatusInvoke) {
396 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700397 }
398 }
Chris Craikd15321b2012-11-28 14:45:04 -0800399 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700400 }
401
402 return result;
403}
404
405status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800406 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700407 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700408
Romain Guy8a4ac612012-07-17 17:32:48 -0700409 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800410 if (mDirtyClip) {
411 setScissorFromClip();
412 }
Romain Guyd643bb52011-03-01 14:55:21 -0800413
Romain Guy80911b82011-03-16 15:30:12 -0700414 Rect clip(*mSnapshot->clipRect);
415 clip.snapToPixelBoundaries();
416
Romain Guyd643bb52011-03-01 14:55:21 -0800417 // Since we don't know what the functor will draw, let's dirty
418 // tne entire clip region
419 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800420 dirtyLayerUnchecked(clip, getRegion());
421 }
Romain Guyd643bb52011-03-01 14:55:21 -0800422
Romain Guy08aa2cb2011-03-17 11:06:57 -0700423 DrawGlInfo info;
424 info.clipLeft = clip.left;
425 info.clipTop = clip.top;
426 info.clipRight = clip.right;
427 info.clipBottom = clip.bottom;
428 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700429 info.width = getSnapshot()->viewport.getWidth();
430 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700431 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700432
Chet Haase48659092012-05-31 15:21:51 -0700433 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800434
Romain Guy8f3b8e32012-03-27 16:33:45 -0700435 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700436 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800437 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700438
Chris Craik65924a32012-04-05 17:52:11 -0700439 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700440 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700441 }
Romain Guycabfcc12011-03-07 18:06:46 -0800442 }
443
Chet Haasedaf98e92011-01-10 14:10:36 -0800444 resume();
Romain Guy65549432012-03-26 16:45:05 -0700445 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800446}
447
Romain Guyf6a11b82010-06-23 17:47:49 -0700448///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700449// Debug
450///////////////////////////////////////////////////////////////////////////////
451
Romain Guy0f667532013-03-01 14:31:04 -0800452void OpenGLRenderer::eventMark(const char* name) const {
453 mCaches.eventMark(0, name);
454}
455
Romain Guy87e2f7572012-09-24 11:37:12 -0700456void OpenGLRenderer::startMark(const char* name) const {
457 mCaches.startMark(0, name);
458}
459
460void OpenGLRenderer::endMark() const {
461 mCaches.endMark();
462}
463
464void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
465 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
466 if (clear) {
467 mCaches.disableScissor();
468 mCaches.stencil.clear();
469 }
470 if (enable) {
471 mCaches.stencil.enableDebugWrite();
472 } else {
473 mCaches.stencil.disable();
474 }
475 }
476}
477
478void OpenGLRenderer::renderOverdraw() {
479 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
480 const Rect* clip = mTilingSnapshot->clipRect;
481
482 mCaches.enableScissor();
483 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
484 clip->right - clip->left, clip->bottom - clip->top);
485
486 mCaches.stencil.enableDebugTest(2);
487 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
488 mCaches.stencil.enableDebugTest(3);
489 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
490 mCaches.stencil.enableDebugTest(4);
491 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
492 mCaches.stencil.enableDebugTest(4, true);
493 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
494 mCaches.stencil.disable();
495 }
496}
497
498///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700499// Layers
500///////////////////////////////////////////////////////////////////////////////
501
502bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
503 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
504 OpenGLRenderer* renderer = layer->renderer;
505 Rect& dirty = layer->dirtyRect;
506
Romain Guy7c450aa2012-09-21 19:15:00 -0700507 if (inFrame) {
508 endTiling();
509 debugOverdraw(false, false);
510 }
Romain Guy11cb6422012-09-21 00:39:43 -0700511
512 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
513 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
514 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
515 renderer->finish();
516
517 if (inFrame) {
518 resumeAfterLayer();
519 startTiling(mSnapshot);
520 }
521
522 dirty.setEmpty();
523 layer->deferredUpdateScheduled = false;
524 layer->renderer = NULL;
525 layer->displayList = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -0800526 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700527
528 return true;
529 }
530
531 return false;
532}
533
534void OpenGLRenderer::updateLayers() {
535 int count = mLayerUpdates.size();
536 if (count > 0) {
537 startMark("Layer Updates");
538
539 // Note: it is very important to update the layers in reverse order
540 for (int i = count - 1; i >= 0; i--) {
541 Layer* layer = mLayerUpdates.itemAt(i);
542 updateLayer(layer, false);
543 mCaches.resourceCache.decrementRefcount(layer);
544 }
545 mLayerUpdates.clear();
546
547 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
548 endMark();
549 }
550}
551
552void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
553 if (layer) {
554 mLayerUpdates.push_back(layer);
555 mCaches.resourceCache.incrementRefcount(layer);
556 }
557}
558
559void OpenGLRenderer::clearLayerUpdates() {
560 size_t count = mLayerUpdates.size();
561 if (count > 0) {
562 mCaches.resourceCache.lock();
563 for (size_t i = 0; i < count; i++) {
564 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
565 }
566 mCaches.resourceCache.unlock();
567 mLayerUpdates.clear();
568 }
569}
570
571///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700572// State management
573///////////////////////////////////////////////////////////////////////////////
574
Romain Guybb9524b2010-06-22 18:56:38 -0700575int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700576 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700577}
578
579int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700580 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700581}
582
583void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700584 if (mSaveCount > 1) {
585 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700586 }
Romain Guybb9524b2010-06-22 18:56:38 -0700587}
588
589void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700590 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700591
Romain Guy8fb95422010-08-17 18:38:51 -0700592 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700593 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700594 }
Romain Guybb9524b2010-06-22 18:56:38 -0700595}
596
Romain Guy8aef54f2010-09-01 15:13:49 -0700597int OpenGLRenderer::saveSnapshot(int flags) {
598 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700599 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700600}
601
602bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700603 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700604 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700605 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700606
Romain Guybd6b79b2010-06-26 00:13:53 -0700607 sp<Snapshot> current = mSnapshot;
608 sp<Snapshot> previous = mSnapshot->previous;
609
Romain Guyeb993562010-10-05 18:14:38 -0700610 if (restoreOrtho) {
611 Rect& r = previous->viewport;
612 glViewport(r.left, r.top, r.right, r.bottom);
613 mOrthoMatrix.load(current->orthoMatrix);
614 }
615
Romain Guy8b55f372010-08-18 17:10:07 -0700616 mSaveCount--;
617 mSnapshot = previous;
618
Romain Guy2542d192010-08-18 11:47:12 -0700619 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700620 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700621 }
Romain Guy2542d192010-08-18 11:47:12 -0700622
Romain Guy5ec99242010-11-03 16:19:08 -0700623 if (restoreLayer) {
624 composeLayer(current, previous);
625 }
626
Romain Guy2542d192010-08-18 11:47:12 -0700627 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700628}
629
Romain Guyf6a11b82010-06-23 17:47:49 -0700630///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700631// Layers
632///////////////////////////////////////////////////////////////////////////////
633
634int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800635 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700636 const GLuint previousFbo = mSnapshot->fbo;
637 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700638
Romain Guyaf636eb2010-12-09 17:47:21 -0800639 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700640 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700641 }
Romain Guyd55a8612010-06-28 17:42:46 -0700642
643 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700644}
645
Romain Guy1c740bc2010-09-13 18:00:09 -0700646/**
647 * Layers are viewed by Skia are slightly different than layers in image editing
648 * programs (for instance.) When a layer is created, previously created layers
649 * and the frame buffer still receive every drawing command. For instance, if a
650 * layer is created and a shape intersecting the bounds of the layers and the
651 * framebuffer is draw, the shape will be drawn on both (unless the layer was
652 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
653 *
654 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
655 * texture. Unfortunately, this is inefficient as it requires every primitive to
656 * be drawn n + 1 times, where n is the number of active layers. In practice this
657 * means, for every primitive:
658 * - Switch active frame buffer
659 * - Change viewport, clip and projection matrix
660 * - Issue the drawing
661 *
662 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700663 * To avoid this, layers are implemented in a different way here, at least in the
664 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
665 * is set. When this flag is set we can redirect all drawing operations into a
666 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700667 *
668 * This implementation relies on the frame buffer being at least RGBA 8888. When
669 * a layer is created, only a texture is created, not an FBO. The content of the
670 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700671 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700672 * buffer and drawing continues as normal. This technique therefore treats the
673 * frame buffer as a scratch buffer for the layers.
674 *
675 * To compose the layers back onto the frame buffer, each layer texture
676 * (containing the original frame buffer data) is drawn as a simple quad over
677 * the frame buffer. The trick is that the quad is set as the composition
678 * destination in the blending equation, and the frame buffer becomes the source
679 * of the composition.
680 *
681 * Drawing layers with an alpha value requires an extra step before composition.
682 * An empty quad is drawn over the layer's region in the frame buffer. This quad
683 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
684 * quad is used to multiply the colors in the frame buffer. This is achieved by
685 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
686 * GL_ZERO, GL_SRC_ALPHA.
687 *
688 * Because glCopyTexImage2D() can be slow, an alternative implementation might
689 * be use to draw a single clipped layer. The implementation described above
690 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700691 *
692 * (1) The frame buffer is actually not cleared right away. To allow the GPU
693 * to potentially optimize series of calls to glCopyTexImage2D, the frame
694 * buffer is left untouched until the first drawing operation. Only when
695 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700696 */
Chet Haased48885a2012-08-28 17:43:28 -0700697bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
698 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700699 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700700 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700701
Romain Guyeb993562010-10-05 18:14:38 -0700702 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
703
Romain Guyf607bdc2010-09-10 19:20:06 -0700704 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700705 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700706 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700707 Rect untransformedBounds(bounds);
Romain Guy3b753822013-03-05 10:27:35 -0800708 currentTransform().mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700709
Chet Haased48885a2012-08-28 17:43:28 -0700710 // Layers only make sense if they are in the framebuffer's bounds
711 if (bounds.intersect(*mSnapshot->clipRect)) {
712 // We cannot work with sub-pixels in this case
713 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700714
Chet Haased48885a2012-08-28 17:43:28 -0700715 // When the layer is not an FBO, we may use glCopyTexImage so we
716 // need to make sure the layer does not extend outside the bounds
717 // of the framebuffer
718 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700719 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700720 } else if (fboLayer) {
721 clip.set(bounds);
722 mat4 inverse;
Romain Guy3b753822013-03-05 10:27:35 -0800723 inverse.loadInverse(currentTransform());
Chet Haased48885a2012-08-28 17:43:28 -0700724 inverse.mapRect(clip);
725 clip.snapToPixelBoundaries();
726 if (clip.intersect(untransformedBounds)) {
727 clip.translate(-left, -top);
728 bounds.set(untransformedBounds);
729 } else {
730 clip.setEmpty();
731 }
Romain Guyad37cd32011-03-15 11:12:25 -0700732 }
Chet Haased48885a2012-08-28 17:43:28 -0700733 } else {
734 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700735 }
Romain Guybf434112010-09-16 14:40:17 -0700736
Romain Guy746b7402010-10-26 16:27:31 -0700737 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700738 bounds.getHeight() > mCaches.maxTextureSize ||
739 (fboLayer && clip.isEmpty())) {
740 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700741 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700742 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700743 }
744
745 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700746 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700747 return false;
748 }
Romain Guyf18fd992010-07-08 11:45:51 -0700749
Romain Guya1d3c912011-12-13 14:55:06 -0800750 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700751 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700752 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700753 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700754 }
755
Romain Guy9ace8f52011-07-07 20:50:11 -0700756 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700757 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700758 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
759 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800760 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700761 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700762 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700763
Romain Guy8fb95422010-08-17 18:38:51 -0700764 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700765 mSnapshot->flags |= Snapshot::kFlagIsLayer;
766 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700767
Romain Guyeb993562010-10-05 18:14:38 -0700768 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700769 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700770 } else {
771 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700772 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800773 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700774 if (layer->isEmpty()) {
775 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700776 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700777 layer->getWidth(), layer->getHeight(), 0);
778 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800779 } else {
780 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700781 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800782 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700783
Romain Guy54be1cd2011-06-13 19:04:27 -0700784 // Enqueue the buffer coordinates to clear the corresponding region later
785 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700786 }
Romain Guyeb993562010-10-05 18:14:38 -0700787 }
Romain Guyf86ef572010-07-01 11:05:42 -0700788
Romain Guyd55a8612010-06-28 17:42:46 -0700789 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700790}
791
Chet Haased48885a2012-08-28 17:43:28 -0700792bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800793 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700794 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700795
Chet Haased48885a2012-08-28 17:43:28 -0700796 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800797 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
798 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700799 mSnapshot->fbo = layer->getFbo();
800 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
801 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
802 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
803 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700804 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700805
Romain Guy2b7028e2012-09-19 17:25:38 -0700806 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700807 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700808 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700809 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
810 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700811
812 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700813 if (layer->isEmpty()) {
814 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
815 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700816 }
817
818 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700819 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700820
Romain Guyf735c8e2013-01-31 17:45:55 -0800821 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700822
823 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700824 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800825 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700826 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700827 glClear(GL_COLOR_BUFFER_BIT);
828
829 dirtyClip();
830
831 // Change the ortho projection
832 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
833 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
834
835 return true;
836}
837
Romain Guy1c740bc2010-09-13 18:00:09 -0700838/**
839 * Read the documentation of createLayer() before doing anything in this method.
840 */
Romain Guy1d83e192010-08-17 11:37:00 -0700841void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
842 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000843 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700844 return;
845 }
846
Romain Guy8ce00302013-01-15 18:51:42 -0800847 Layer* layer = current->layer;
848 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700849 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700850
851 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700852 endTiling();
853
Romain Guye0aa84b2012-04-03 19:30:26 -0700854 // Detach the texture from the FBO
855 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800856
857 layer->removeFbo(false);
858
Romain Guyeb993562010-10-05 18:14:38 -0700859 // Unbind current FBO and restore previous one
860 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700861 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700862
863 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700864 }
865
Romain Guy9ace8f52011-07-07 20:50:11 -0700866 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700867 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700868 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700869 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700870 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700871 }
872
Romain Guy03750a02010-10-18 14:06:08 -0700873 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700874
Romain Guya1d3c912011-12-13 14:55:06 -0800875 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700876
Romain Guy5b3b3522010-10-27 18:57:51 -0700877 // When the layer is stored in an FBO, we can save a bit of fillrate by
878 // drawing only the dirty region
879 if (fboLayer) {
880 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700881 if (layer->getColorFilter()) {
882 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800883 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700884 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700885 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800886 resetColorFilter();
887 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700888 } else if (!rect.isEmpty()) {
889 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
890 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700891 }
Romain Guy8b55f372010-08-18 17:10:07 -0700892
Romain Guy746b7402010-10-26 16:27:31 -0700893 dirtyClip();
894
Romain Guyeb993562010-10-05 18:14:38 -0700895 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700896 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700897 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700898 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700899 }
900}
901
Romain Guyaa6c24c2011-04-28 18:40:04 -0700902void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craike83569c2013-03-20 16:57:09 -0700903 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700904
905 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700906 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700907 setupDrawWithTexture();
908 } else {
909 setupDrawWithExternalTexture();
910 }
911 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700912 setupDrawColor(alpha, alpha, alpha, alpha);
913 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700914 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700915 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700916 setupDrawPureColorUniforms();
917 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700918 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
919 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700920 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700921 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700922 }
Romain Guy3b753822013-03-05 10:27:35 -0800923 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700924 layer->getWidth() == (uint32_t) rect.getWidth() &&
925 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -0800926 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
927 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700928
Romain Guyd21b6e12011-11-30 20:21:23 -0800929 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700930 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
931 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800932 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700933 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
934 }
935 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700936 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
937
938 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
939
940 finishDrawTexture();
941}
942
Romain Guy5b3b3522010-10-27 18:57:51 -0700943void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700944 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700945 const Rect& texCoords = layer->texCoords;
946 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
947 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700948
Romain Guy9ace8f52011-07-07 20:50:11 -0700949 float x = rect.left;
950 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -0800951 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700952 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700953 layer->getHeight() == (uint32_t) rect.getHeight();
954
955 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700956 // When we're swapping, the layer is already in screen coordinates
957 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -0800958 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
959 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700960 }
961
Romain Guyd21b6e12011-11-30 20:21:23 -0800962 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700963 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800964 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700965 }
966
Chris Craike83569c2013-03-20 16:57:09 -0700967 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
968 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700969 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -0700970 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -0700971 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
972 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700973
Romain Guyaa6c24c2011-04-28 18:40:04 -0700974 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
975 } else {
976 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
977 drawTextureLayer(layer, rect);
978 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
979 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700980}
981
982void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700983 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700984 layer->setRegionAsRect();
985
Romain Guy40667672011-03-18 14:34:03 -0700986 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700987
Romain Guy5b3b3522010-10-27 18:57:51 -0700988 layer->region.clear();
989 return;
990 }
991
Romain Guy8a3957d2011-09-07 17:55:15 -0700992 // TODO: See LayerRenderer.cpp::generateMesh() for important
993 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800994 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700995 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -0800996 const android::Rect* rects;
997 Region safeRegion;
998 if (CC_LIKELY(hasRectToRectTransform())) {
999 rects = layer->region.getArray(&count);
1000 } else {
1001 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1002 rects = safeRegion.getArray(&count);
1003 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001004
Chris Craike83569c2013-03-20 16:57:09 -07001005 const float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guy9ace8f52011-07-07 20:50:11 -07001006 const float texX = 1.0f / float(layer->getWidth());
1007 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001008 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001009
Romain Guy8ce00302013-01-15 18:51:42 -08001010 setupDraw();
1011
1012 // We must get (and therefore bind) the region mesh buffer
1013 // after we setup drawing in case we need to mess with the
1014 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001015 TextureVertex* mesh = mCaches.getRegionMesh();
1016 GLsizei numQuads = 0;
1017
Romain Guy7230a742011-01-10 22:26:16 -08001018 setupDrawWithTexture();
1019 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001020 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001021 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001022 setupDrawProgram();
1023 setupDrawDirtyRegionsDisabled();
1024 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001025 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001026 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001027 if (currentTransform().isPureTranslate()) {
1028 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1029 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001030
Romain Guyd21b6e12011-11-30 20:21:23 -08001031 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001032 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1033 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001034 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001035 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1036 }
Romain Guy15bc6432011-12-13 13:11:32 -08001037 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001038
1039 for (size_t i = 0; i < count; i++) {
1040 const android::Rect* r = &rects[i];
1041
1042 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001043 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001044 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001045 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001046
1047 // TODO: Reject quads outside of the clip
1048 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1049 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1050 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1051 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1052
1053 numQuads++;
1054
1055 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1056 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1057 numQuads = 0;
1058 mesh = mCaches.getRegionMesh();
1059 }
1060 }
1061
1062 if (numQuads > 0) {
1063 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1064 }
1065
Romain Guy7230a742011-01-10 22:26:16 -08001066 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001067
1068#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001069 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001070#endif
1071
1072 layer->region.clear();
1073 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001074}
1075
Romain Guy3a3133d2011-02-01 22:59:58 -08001076void OpenGLRenderer::drawRegionRects(const Region& region) {
1077#if DEBUG_LAYERS_AS_REGIONS
1078 size_t count;
1079 const android::Rect* rects = region.getArray(&count);
1080
1081 uint32_t colors[] = {
1082 0x7fff0000, 0x7f00ff00,
1083 0x7f0000ff, 0x7fff00ff,
1084 };
1085
1086 int offset = 0;
1087 int32_t top = rects[0].top;
1088
1089 for (size_t i = 0; i < count; i++) {
1090 if (top != rects[i].top) {
1091 offset ^= 0x2;
1092 top = rects[i].top;
1093 }
1094
1095 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1096 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1097 SkXfermode::kSrcOver_Mode);
1098 }
1099#endif
1100}
1101
Romain Guy8ce00302013-01-15 18:51:42 -08001102void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1103 SkXfermode::Mode mode, bool dirty) {
1104 int count = 0;
1105 Vector<float> rects;
1106
1107 SkRegion::Iterator it(region);
1108 while (!it.done()) {
1109 const SkIRect& r = it.rect();
1110 rects.push(r.fLeft);
1111 rects.push(r.fTop);
1112 rects.push(r.fRight);
1113 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001114 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001115 it.next();
1116 }
1117
Romain Guy3bbacf22013-02-06 16:51:04 -08001118 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001119}
1120
Romain Guy5b3b3522010-10-27 18:57:51 -07001121void OpenGLRenderer::dirtyLayer(const float left, const float top,
1122 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001123 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001124 Rect bounds(left, top, right, bottom);
1125 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001126 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001127 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001128}
1129
1130void OpenGLRenderer::dirtyLayer(const float left, const float top,
1131 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001132 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001133 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001134 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001135 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001136}
1137
1138void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001139 if (bounds.intersect(*mSnapshot->clipRect)) {
1140 bounds.snapToPixelBoundaries();
1141 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1142 if (!dirty.isEmpty()) {
1143 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001144 }
1145 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001146}
1147
Romain Guy54be1cd2011-06-13 19:04:27 -07001148void OpenGLRenderer::clearLayerRegions() {
1149 const size_t count = mLayers.size();
1150 if (count == 0) return;
1151
1152 if (!mSnapshot->isIgnored()) {
1153 // Doing several glScissor/glClear here can negatively impact
1154 // GPUs with a tiler architecture, instead we draw quads with
1155 // the Clear blending mode
1156
1157 // The list contains bounds that have already been clipped
1158 // against their initial clip rect, and the current clip
1159 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001160 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001161
1162 Vertex mesh[count * 6];
1163 Vertex* vertex = mesh;
1164
1165 for (uint32_t i = 0; i < count; i++) {
1166 Rect* bounds = mLayers.itemAt(i);
1167
1168 Vertex::set(vertex++, bounds->left, bounds->bottom);
1169 Vertex::set(vertex++, bounds->left, bounds->top);
1170 Vertex::set(vertex++, bounds->right, bounds->top);
1171 Vertex::set(vertex++, bounds->left, bounds->bottom);
1172 Vertex::set(vertex++, bounds->right, bounds->top);
1173 Vertex::set(vertex++, bounds->right, bounds->bottom);
1174
1175 delete bounds;
1176 }
Romain Guye67307c2013-02-11 18:01:20 -08001177 // We must clear the list of dirty rects before we
1178 // call setupDraw() to prevent stencil setup to do
1179 // the same thing again
1180 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001181
1182 setupDraw(false);
1183 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1184 setupDrawBlending(true, SkXfermode::kClear_Mode);
1185 setupDrawProgram();
1186 setupDrawPureColorUniforms();
1187 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001188 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001189
Romain Guy54be1cd2011-06-13 19:04:27 -07001190 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001191
1192 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001193 } else {
1194 for (uint32_t i = 0; i < count; i++) {
1195 delete mLayers.itemAt(i);
1196 }
Romain Guye67307c2013-02-11 18:01:20 -08001197 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001198 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001199}
1200
Romain Guybd6b79b2010-06-26 00:13:53 -07001201///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001202// State Deferral
1203///////////////////////////////////////////////////////////////////////////////
1204
Chris Craikd0afeac2013-03-15 18:43:11 -07001205void OpenGLRenderer::resetDrawModifiers() {
1206 mDrawModifiers.mShader = NULL;
1207 mDrawModifiers.mColorFilter = NULL;
1208 mDrawModifiers.mHasShadow = false;
1209 mDrawModifiers.mHasDrawFilter = false;
1210}
1211
Chris Craikff785832013-03-08 13:12:16 -08001212bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001213 const Rect& currentClip = *(mSnapshot->clipRect);
1214 const mat4& currentMatrix = *(mSnapshot->transform);
1215
Chris Craikff785832013-03-08 13:12:16 -08001216 if (stateDeferFlags & kStateDeferFlag_Draw) {
1217 // state has bounds initialized in local coordinates
1218 if (!state.mBounds.isEmpty()) {
1219 currentMatrix.mapRect(state.mBounds);
1220 if (!state.mBounds.intersect(currentClip)) {
1221 // quick rejected
1222 return true;
1223 }
1224 } else {
1225 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001226 }
Chris Craikff785832013-03-08 13:12:16 -08001227 state.mDrawModifiers = mDrawModifiers;
1228 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001229 }
1230
Chris Craikff785832013-03-08 13:12:16 -08001231 if (stateDeferFlags & kStateDeferFlag_Clip) {
1232 state.mClip.set(currentClip);
1233 } else {
1234 state.mClip.setEmpty();
1235 }
1236
1237 // transform always deferred
Chris Craikc3566d02013-02-04 16:16:33 -08001238 state.mMatrix.load(currentMatrix);
Chris Craikc3566d02013-02-04 16:16:33 -08001239 return false;
1240}
1241
Chris Craikff785832013-03-08 13:12:16 -08001242void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, int stateDeferFlags) {
Romain Guy3b753822013-03-05 10:27:35 -08001243 currentTransform().load(state.mMatrix);
Chris Craikc3566d02013-02-04 16:16:33 -08001244
Chris Craikff785832013-03-08 13:12:16 -08001245 if (stateDeferFlags & kStateDeferFlag_Draw) {
1246 mDrawModifiers = state.mDrawModifiers;
1247 mSnapshot->alpha = state.mAlpha;
1248 }
1249
1250 if (!state.mClip.isEmpty()) { //stateDeferFlags & kStateDeferFlag_Clip) {
1251 mSnapshot->setClip(state.mClip.left, state.mClip.top, state.mClip.right, state.mClip.bottom);
1252 dirtyClip();
1253 }
Chris Craikc3566d02013-02-04 16:16:33 -08001254}
1255
1256///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001257// Transforms
1258///////////////////////////////////////////////////////////////////////////////
1259
1260void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy3b753822013-03-05 10:27:35 -08001261 currentTransform().translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001262}
1263
1264void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001265 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001266}
1267
1268void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001269 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001270}
1271
Romain Guy807daf72011-01-18 11:19:19 -08001272void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001273 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001274}
1275
Romain Guyf6a11b82010-06-23 17:47:49 -07001276void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001277 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001278 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001279 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001280 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001281 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001282}
1283
Chris Craikb98a0162013-02-21 11:30:22 -08001284bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001285 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001286}
1287
Romain Guyf6a11b82010-06-23 17:47:49 -07001288void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001289 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001290}
1291
1292void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001293 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001294 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001295 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001296 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001297}
1298
1299///////////////////////////////////////////////////////////////////////////////
1300// Clipping
1301///////////////////////////////////////////////////////////////////////////////
1302
Romain Guybb9524b2010-06-22 18:56:38 -07001303void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001304 Rect clip(*mSnapshot->clipRect);
1305 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001306
Romain Guy8a4ac612012-07-17 17:32:48 -07001307 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1308 clip.getWidth(), clip.getHeight())) {
1309 mDirtyClip = false;
1310 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001311}
1312
Romain Guy8ce00302013-01-15 18:51:42 -08001313void OpenGLRenderer::ensureStencilBuffer() {
1314 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1315 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1316 // just hope we have one when hasLayer() returns false.
1317 if (hasLayer()) {
1318 attachStencilBufferToLayer(mSnapshot->layer);
1319 }
1320}
1321
1322void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1323 // The layer's FBO is already bound when we reach this stage
1324 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001325 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1326 // is attached after we initiated tiling. We must turn it off,
1327 // attach the new render buffer then turn tiling back on
1328 endTiling();
1329
Romain Guy8d4aeb72013-02-12 16:08:55 -08001330 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001331 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001332 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001333
Romain Guyf735c8e2013-01-31 17:45:55 -08001334 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001335 }
1336}
1337
1338void OpenGLRenderer::setStencilFromClip() {
1339 if (!mCaches.debugOverdraw) {
1340 if (!mSnapshot->clipRegion->isEmpty()) {
1341 // NOTE: The order here is important, we must set dirtyClip to false
1342 // before any draw call to avoid calling back into this method
1343 mDirtyClip = false;
1344
1345 ensureStencilBuffer();
1346
1347 mCaches.stencil.enableWrite();
1348
1349 // Clear the stencil but first make sure we restrict drawing
1350 // to the region's bounds
1351 bool resetScissor = mCaches.enableScissor();
1352 if (resetScissor) {
1353 // The scissor was not set so we now need to update it
1354 setScissorFromClip();
1355 }
1356 mCaches.stencil.clear();
1357 if (resetScissor) mCaches.disableScissor();
1358
1359 // NOTE: We could use the region contour path to generate a smaller mesh
1360 // Since we are using the stencil we could use the red book path
1361 // drawing technique. It might increase bandwidth usage though.
1362
1363 // The last parameter is important: we are not drawing in the color buffer
1364 // so we don't want to dirty the current layer, if any
1365 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1366
1367 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001368
1369 // Draw the region used to generate the stencil if the appropriate debug
1370 // mode is enabled
1371 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1372 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1373 }
Romain Guy8ce00302013-01-15 18:51:42 -08001374 } else {
1375 mCaches.stencil.disable();
1376 }
1377 }
1378}
1379
Romain Guy9d5316e2010-06-24 19:30:36 -07001380const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001381 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001382}
1383
Romain Guy8a4ac612012-07-17 17:32:48 -07001384bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1385 if (mSnapshot->isIgnored()) {
1386 return true;
1387 }
1388
1389 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001390 currentTransform().mapRect(r);
Romain Guy8a4ac612012-07-17 17:32:48 -07001391 r.snapToPixelBoundaries();
1392
1393 Rect clipRect(*mSnapshot->clipRect);
1394 clipRect.snapToPixelBoundaries();
1395
1396 return !clipRect.intersects(r);
1397}
1398
Romain Guy35643dd2012-09-18 15:40:58 -07001399bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1400 Rect& transformed, Rect& clip) {
1401 if (mSnapshot->isIgnored()) {
1402 return true;
1403 }
1404
1405 transformed.set(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001406 currentTransform().mapRect(transformed);
Romain Guy35643dd2012-09-18 15:40:58 -07001407 transformed.snapToPixelBoundaries();
1408
1409 clip.set(*mSnapshot->clipRect);
1410 clip.snapToPixelBoundaries();
1411
1412 return !clip.intersects(transformed);
1413}
1414
Romain Guy672433d2013-01-04 19:05:13 -08001415bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1416 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001417 if (paint->getStyle() != SkPaint::kFill_Style) {
1418 float outset = paint->getStrokeWidth() * 0.5f;
1419 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1420 } else {
1421 return quickReject(left, top, right, bottom);
1422 }
1423}
1424
Romain Guyc7d53492010-06-25 13:41:57 -07001425bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001426 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001427 return true;
1428 }
1429
Romain Guy1d83e192010-08-17 11:37:00 -07001430 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001431 currentTransform().mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001432 r.snapToPixelBoundaries();
1433
1434 Rect clipRect(*mSnapshot->clipRect);
1435 clipRect.snapToPixelBoundaries();
1436
Romain Guy586cae32012-07-13 15:28:31 -07001437 bool rejected = !clipRect.intersects(r);
1438 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001439 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001440 }
1441
1442 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001443}
1444
Romain Guy8ce00302013-01-15 18:51:42 -08001445void OpenGLRenderer::debugClip() {
1446#if DEBUG_CLIP_REGIONS
1447 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1448 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1449 }
1450#endif
1451}
1452
Romain Guy079ba2c2010-07-16 14:12:24 -07001453bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001454 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001455 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1456 if (clipped) {
1457 dirtyClip();
1458 }
1459 return !mSnapshot->clipRect->isEmpty();
1460 }
1461
1462 SkPath path;
1463 path.addRect(left, top, right, bottom);
1464
1465 return clipPath(&path, op);
1466}
1467
1468bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1469 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001470 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001471
1472 SkPath transformed;
1473 path->transform(transform, &transformed);
1474
1475 SkRegion clip;
1476 if (!mSnapshot->clipRegion->isEmpty()) {
1477 clip.setRegion(*mSnapshot->clipRegion);
1478 } else {
1479 Rect* bounds = mSnapshot->clipRect;
1480 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1481 }
1482
1483 SkRegion region;
1484 region.setPath(transformed, clip);
1485
1486 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001487 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001488 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001489 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001490 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001491}
1492
Romain Guy735738c2012-12-03 12:34:51 -08001493bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001494 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1495 if (clipped) {
1496 dirtyClip();
1497 }
1498 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001499}
1500
Chet Haasea23eed82012-04-12 15:19:04 -07001501Rect* OpenGLRenderer::getClipRect() {
1502 return mSnapshot->clipRect;
1503}
1504
Romain Guyf6a11b82010-06-23 17:47:49 -07001505///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001506// Drawing commands
1507///////////////////////////////////////////////////////////////////////////////
1508
Romain Guy54be1cd2011-06-13 19:04:27 -07001509void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001510 // TODO: It would be best if we could do this before quickReject()
1511 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001512 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001513 // Make sure setScissor & setStencil happen at the beginning of
1514 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001515 if (mDirtyClip) {
1516 if (mCaches.scissorEnabled) {
1517 setScissorFromClip();
1518 }
Romain Guy8ce00302013-01-15 18:51:42 -08001519 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001520 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001521
Romain Guy70ca14e2010-12-13 18:24:33 -08001522 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001523
Romain Guy70ca14e2010-12-13 18:24:33 -08001524 mSetShaderColor = false;
1525 mColorSet = false;
1526 mColorA = mColorR = mColorG = mColorB = 0.0f;
1527 mTextureUnit = 0;
1528 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001529
1530 // Enable debug highlight when what we're about to draw is tested against
1531 // the stencil buffer and if stencil highlight debugging is on
1532 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1533 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1534 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001535}
1536
1537void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1538 mDescription.hasTexture = true;
1539 mDescription.hasAlpha8Texture = isAlpha8;
1540}
1541
Romain Guyff316ec2013-02-13 18:39:43 -08001542void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1543 mDescription.hasTexture = true;
1544 mDescription.hasColors = true;
1545 mDescription.hasAlpha8Texture = isAlpha8;
1546}
1547
Romain Guyaa6c24c2011-04-28 18:40:04 -07001548void OpenGLRenderer::setupDrawWithExternalTexture() {
1549 mDescription.hasExternalTexture = true;
1550}
1551
Romain Guy15bc6432011-12-13 13:11:32 -08001552void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001553 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001554}
1555
Chris Craik710f46d2012-09-17 17:25:49 -07001556void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001557 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001558}
1559
Romain Guyed6fcb02011-03-21 13:11:28 -07001560void OpenGLRenderer::setupDrawPoint(float pointSize) {
1561 mDescription.isPoint = true;
1562 mDescription.pointSize = pointSize;
1563}
1564
Romain Guy8d0d4782010-12-14 20:13:35 -08001565void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1566 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001567 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1568 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1569 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001570 mColorSet = true;
1571 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1572}
1573
Romain Guy86568192010-12-14 15:55:39 -08001574void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1575 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001576 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1577 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1578 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001579 mColorSet = true;
1580 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1581}
1582
Romain Guy41210632012-07-16 17:04:24 -07001583void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1584 mCaches.fontRenderer->describe(mDescription, paint);
1585}
1586
Romain Guy70ca14e2010-12-13 18:24:33 -08001587void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1588 mColorA = a;
1589 mColorR = r;
1590 mColorG = g;
1591 mColorB = b;
1592 mColorSet = true;
1593 mSetShaderColor = mDescription.setColor(r, g, b, a);
1594}
1595
1596void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001597 if (mDrawModifiers.mShader) {
1598 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001599 }
1600}
1601
1602void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001603 if (mDrawModifiers.mColorFilter) {
1604 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001605 }
1606}
1607
Romain Guyf09ef512011-05-27 11:43:46 -07001608void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1609 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1610 mColorA = 1.0f;
1611 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001612 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001613 }
1614}
1615
Romain Guy70ca14e2010-12-13 18:24:33 -08001616void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001617 // When the blending mode is kClear_Mode, we need to use a modulate color
1618 // argb=1,0,0,0
1619 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001620 bool blend = (mColorSet && mColorA < 1.0f) ||
1621 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1622 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001623}
1624
1625void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001626 // When the blending mode is kClear_Mode, we need to use a modulate color
1627 // argb=1,0,0,0
1628 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001629 blend |= (mColorSet && mColorA < 1.0f) ||
1630 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1631 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1632 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001633}
1634
1635void OpenGLRenderer::setupDrawProgram() {
1636 useProgram(mCaches.programCache.get(mDescription));
1637}
1638
1639void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1640 mTrackDirtyRegions = false;
1641}
1642
1643void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1644 bool ignoreTransform) {
1645 mModelView.loadTranslate(left, top, 0.0f);
1646 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001647 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1648 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001649 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001650 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001651 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1652 }
1653}
1654
Chet Haase8a5cc922011-04-26 07:28:09 -07001655void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001656 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001657}
1658
Romain Guy70ca14e2010-12-13 18:24:33 -08001659void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1660 bool ignoreTransform, bool ignoreModelView) {
1661 if (!ignoreModelView) {
1662 mModelView.loadTranslate(left, top, 0.0f);
1663 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001664 } else {
1665 mModelView.loadIdentity();
1666 }
Romain Guy86568192010-12-14 15:55:39 -08001667 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1668 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001669 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001670 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001671 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001672 }
1673 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001674 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001675 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1676 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001677}
1678
Romain Guyed6fcb02011-03-21 13:11:28 -07001679void OpenGLRenderer::setupDrawPointUniforms() {
1680 int slot = mCaches.currentProgram->getUniform("pointSize");
1681 glUniform1f(slot, mDescription.pointSize);
1682}
1683
Romain Guy70ca14e2010-12-13 18:24:33 -08001684void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001685 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001686 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1687 }
1688}
1689
Romain Guy86568192010-12-14 15:55:39 -08001690void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001691 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001692 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001693 }
1694}
1695
Romain Guy70ca14e2010-12-13 18:24:33 -08001696void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001697 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001698 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001699 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001700 }
Chris Craikc3566d02013-02-04 16:16:33 -08001701 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1702 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001703 }
1704}
1705
Romain Guy8d0d4782010-12-14 20:13:35 -08001706void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001707 if (mDrawModifiers.mShader) {
1708 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001709 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001710 }
1711}
1712
Romain Guy70ca14e2010-12-13 18:24:33 -08001713void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001714 if (mDrawModifiers.mColorFilter) {
1715 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001716 }
1717}
1718
Romain Guy41210632012-07-16 17:04:24 -07001719void OpenGLRenderer::setupDrawTextGammaUniforms() {
1720 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1721}
1722
Romain Guy70ca14e2010-12-13 18:24:33 -08001723void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001724 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001725 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001726 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001727}
1728
1729void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1730 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001731 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001732 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001733}
1734
Romain Guyaa6c24c2011-04-28 18:40:04 -07001735void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1736 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001737 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001738 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001739}
1740
Romain Guy8f0095c2011-05-02 17:24:22 -07001741void OpenGLRenderer::setupDrawTextureTransform() {
1742 mDescription.hasTextureTransform = true;
1743}
1744
1745void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001746 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1747 GL_FALSE, &transform.data[0]);
1748}
1749
Romain Guy70ca14e2010-12-13 18:24:33 -08001750void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001751 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001752 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001753 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001754 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001755 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001756 }
Romain Guyd71dd362011-12-12 19:03:35 -08001757
Chris Craikcb4d6002012-09-25 12:00:29 -07001758 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001759 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001760 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001761 }
1762
1763 mCaches.unbindIndicesBuffer();
1764}
1765
Romain Guyff316ec2013-02-13 18:39:43 -08001766void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1767 bool force = mCaches.unbindMeshBuffer();
1768 GLsizei stride = sizeof(ColorTextureVertex);
1769
1770 mCaches.bindPositionVertexPointer(force, vertices, stride);
1771 if (mCaches.currentProgram->texCoords >= 0) {
1772 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1773 }
1774 int slot = mCaches.currentProgram->getAttrib("colors");
1775 if (slot >= 0) {
1776 glEnableVertexAttribArray(slot);
1777 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1778 }
1779
1780 mCaches.unbindIndicesBuffer();
1781}
1782
Romain Guy15bc6432011-12-13 13:11:32 -08001783void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1784 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001785 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001786 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001787 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001788 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001789}
1790
Chet Haase5b0200b2011-04-13 17:58:08 -07001791void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001792 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001793 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001794 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001795}
1796
Romain Guy70ca14e2010-12-13 18:24:33 -08001797void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001798}
1799
1800///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001801// Drawing
1802///////////////////////////////////////////////////////////////////////////////
1803
Chris Craikff785832013-03-08 13:12:16 -08001804status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
1805 int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001806 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1807 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001808 if (displayList && displayList->isRenderable()) {
Chris Craikcada41a2013-03-18 17:00:18 -07001809 if (true || CC_UNLIKELY(mCaches.drawDeferDisabled)) { // NOTE: temporary workaround
Chris Craikff785832013-03-08 13:12:16 -08001810 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
1811 displayList->replay(replayStruct, 0);
1812 return replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08001813 }
1814
1815 DeferredDisplayList deferredList;
Chris Craikff785832013-03-08 13:12:16 -08001816 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
1817 displayList->defer(deferStruct, 0);
1818 return deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08001819 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001820
Romain Guy65549432012-03-26 16:45:05 -07001821 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001822}
1823
Chris Craikc3566d02013-02-04 16:16:33 -08001824void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07001825 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08001826 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07001827 }
1828}
1829
Romain Guya168d732011-03-18 16:50:13 -07001830void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1831 int alpha;
1832 SkXfermode::Mode mode;
1833 getAlphaAndMode(paint, &alpha, &mode);
1834
Romain Guy886b2752013-01-04 12:26:18 -08001835 int color = paint != NULL ? paint->getColor() : 0;
1836
Romain Guya168d732011-03-18 16:50:13 -07001837 float x = left;
1838 float y = top;
1839
Romain Guy886b2752013-01-04 12:26:18 -08001840 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1841
Romain Guya168d732011-03-18 16:50:13 -07001842 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08001843 if (currentTransform().isPureTranslate()) {
1844 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
1845 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001846 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001847
1848 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001849 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001850 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001851 }
1852
Romain Guy886b2752013-01-04 12:26:18 -08001853 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1854 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1855 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001856}
1857
Chet Haase48659092012-05-31 15:21:51 -07001858status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001859 const float right = left + bitmap->width();
1860 const float bottom = top + bitmap->height();
1861
1862 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001863 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001864 }
1865
Romain Guya1d3c912011-12-13 14:55:06 -08001866 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001867 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001868 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001869 const AutoTexture autoCleanup(texture);
1870
Romain Guy211370f2012-02-01 16:10:55 -08001871 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001872 drawAlphaBitmap(texture, left, top, paint);
1873 } else {
1874 drawTextureRect(left, top, right, bottom, texture, paint);
1875 }
Chet Haase48659092012-05-31 15:21:51 -07001876
1877 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001878}
1879
Chet Haase48659092012-05-31 15:21:51 -07001880status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001881 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1882 const mat4 transform(*matrix);
1883 transform.mapRect(r);
1884
Romain Guy6926c722010-07-12 20:20:03 -07001885 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001886 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001887 }
1888
Romain Guya1d3c912011-12-13 14:55:06 -08001889 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001890 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001891 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001892 const AutoTexture autoCleanup(texture);
1893
Romain Guy5b3b3522010-10-27 18:57:51 -07001894 // This could be done in a cheaper way, all we need is pass the matrix
1895 // to the vertex shader. The save/restore is a bit overkill.
1896 save(SkCanvas::kMatrix_SaveFlag);
1897 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08001898 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1899 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
1900 } else {
1901 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1902 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001903 restore();
Chet Haase48659092012-05-31 15:21:51 -07001904
1905 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001906}
1907
Chet Haase48659092012-05-31 15:21:51 -07001908status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001909 const float right = left + bitmap->width();
1910 const float bottom = top + bitmap->height();
1911
1912 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001913 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001914 }
1915
1916 mCaches.activeTexture(0);
1917 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1918 const AutoTexture autoCleanup(texture);
1919
Romain Guy886b2752013-01-04 12:26:18 -08001920 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1921 drawAlphaBitmap(texture, left, top, paint);
1922 } else {
1923 drawTextureRect(left, top, right, bottom, texture, paint);
1924 }
Chet Haase48659092012-05-31 15:21:51 -07001925
1926 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001927}
1928
Chet Haase48659092012-05-31 15:21:51 -07001929status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001930 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001931 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001932 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001933 }
1934
Romain Guyb18d2d02011-02-10 15:52:54 -08001935 float left = FLT_MAX;
1936 float top = FLT_MAX;
1937 float right = FLT_MIN;
1938 float bottom = FLT_MIN;
1939
Romain Guya92bb4d2012-10-16 11:08:44 -07001940 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001941
Romain Guyff316ec2013-02-13 18:39:43 -08001942 ColorTextureVertex mesh[count];
1943 ColorTextureVertex* vertex = mesh;
1944
1945 bool cleanupColors = false;
1946 if (!colors) {
1947 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
1948 colors = new int[colorsCount];
1949 memset(colors, 0xff, colorsCount * sizeof(int));
1950 cleanupColors = true;
1951 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001952
Romain Guy5a7b4662011-01-20 19:09:30 -08001953 for (int32_t y = 0; y < meshHeight; y++) {
1954 for (int32_t x = 0; x < meshWidth; x++) {
1955 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1956
1957 float u1 = float(x) / meshWidth;
1958 float u2 = float(x + 1) / meshWidth;
1959 float v1 = float(y) / meshHeight;
1960 float v2 = float(y + 1) / meshHeight;
1961
1962 int ax = i + (meshWidth + 1) * 2;
1963 int ay = ax + 1;
1964 int bx = i;
1965 int by = bx + 1;
1966 int cx = i + 2;
1967 int cy = cx + 1;
1968 int dx = i + (meshWidth + 1) * 2 + 2;
1969 int dy = dx + 1;
1970
Romain Guyff316ec2013-02-13 18:39:43 -08001971 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1972 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
1973 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08001974
Romain Guyff316ec2013-02-13 18:39:43 -08001975 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1976 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
1977 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08001978
Romain Guya92bb4d2012-10-16 11:08:44 -07001979 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1980 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1981 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1982 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001983 }
1984 }
1985
Romain Guya92bb4d2012-10-16 11:08:44 -07001986 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08001987 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07001988 return DrawGlInfo::kStatusDone;
1989 }
1990
1991 mCaches.activeTexture(0);
1992 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guyff316ec2013-02-13 18:39:43 -08001993 if (!texture) {
1994 if (cleanupColors) delete[] colors;
1995 return DrawGlInfo::kStatusDone;
1996 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001997 const AutoTexture autoCleanup(texture);
1998
1999 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2000 texture->setFilter(FILTER(paint), true);
2001
2002 int alpha;
2003 SkXfermode::Mode mode;
2004 getAlphaAndMode(paint, &alpha, &mode);
2005
Romain Guyff316ec2013-02-13 18:39:43 -08002006 float a = alpha / 255.0f;
2007
Romain Guya92bb4d2012-10-16 11:08:44 -07002008 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002009 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002010 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002011
Romain Guyff316ec2013-02-13 18:39:43 -08002012 setupDraw();
2013 setupDrawWithTextureAndColor();
2014 setupDrawColor(a, a, a, a);
2015 setupDrawColorFilter();
2016 setupDrawBlending(true, mode, false);
2017 setupDrawProgram();
2018 setupDrawDirtyRegionsDisabled();
2019 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2020 setupDrawTexture(texture->id);
2021 setupDrawPureColorUniforms();
2022 setupDrawColorFilterUniforms();
2023 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2024
2025 glDrawArrays(GL_TRIANGLES, 0, count);
2026
2027 finishDrawTexture();
2028
2029 int slot = mCaches.currentProgram->getAttrib("colors");
2030 if (slot >= 0) {
2031 glDisableVertexAttribArray(slot);
2032 }
2033
2034 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002035
2036 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002037}
2038
Chet Haase48659092012-05-31 15:21:51 -07002039status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002040 float srcLeft, float srcTop, float srcRight, float srcBottom,
2041 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002042 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002043 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002044 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002045 }
2046
Romain Guya1d3c912011-12-13 14:55:06 -08002047 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002048 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002049 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002050 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002051
Romain Guy8ba548f2010-06-30 19:21:21 -07002052 const float width = texture->width;
2053 const float height = texture->height;
2054
Romain Guy68169722011-08-22 17:33:33 -07002055 const float u1 = fmax(0.0f, srcLeft / width);
2056 const float v1 = fmax(0.0f, srcTop / height);
2057 const float u2 = fmin(1.0f, srcRight / width);
2058 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07002059
Romain Guy03750a02010-10-18 14:06:08 -07002060 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002061 resetDrawTextureTexCoords(u1, v1, u2, v2);
2062
Romain Guy03750a02010-10-18 14:06:08 -07002063 int alpha;
2064 SkXfermode::Mode mode;
2065 getAlphaAndMode(paint, &alpha, &mode);
2066
Romain Guyd21b6e12011-11-30 20:21:23 -08002067 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2068
Romain Guy886b2752013-01-04 12:26:18 -08002069 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2070 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002071
Romain Guy886b2752013-01-04 12:26:18 -08002072 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2073 // Apply a scale transform on the canvas only when a shader is in use
2074 // Skia handles the ratio between the dst and src rects as a scale factor
2075 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002076 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002077 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002078
Romain Guy3b753822013-03-05 10:27:35 -08002079 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2080 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2081 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002082
2083 dstRight = x + (dstRight - dstLeft);
2084 dstBottom = y + (dstBottom - dstTop);
2085
2086 dstLeft = x;
2087 dstTop = y;
2088
2089 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2090 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002091 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002092 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002093 }
2094
2095 if (CC_UNLIKELY(useScaleTransform)) {
2096 save(SkCanvas::kMatrix_SaveFlag);
2097 translate(dstLeft, dstTop);
2098 scale(scaleX, scaleY);
2099
2100 dstLeft = 0.0f;
2101 dstTop = 0.0f;
2102
2103 dstRight = srcRight - srcLeft;
2104 dstBottom = srcBottom - srcTop;
2105 }
2106
2107 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2108 int color = paint ? paint->getColor() : 0;
2109 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2110 texture->id, paint != NULL, color, alpha, mode,
2111 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2112 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2113 } else {
2114 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2115 texture->id, alpha / 255.0f, mode, texture->blend,
2116 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2117 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2118 }
2119
2120 if (CC_UNLIKELY(useScaleTransform)) {
2121 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002122 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002123
2124 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002125
2126 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002127}
2128
Chet Haase48659092012-05-31 15:21:51 -07002129status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07002130 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07002131 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002132 int alpha;
2133 SkXfermode::Mode mode;
2134 getAlphaAndModeDirect(paint, &alpha, &mode);
2135
2136 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2137 left, top, right, bottom, alpha, mode);
2138}
2139
2140status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2141 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2142 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002143 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002144 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002145 }
2146
Romain Guybe6f9dc2012-07-16 12:41:17 -07002147 alpha *= mSnapshot->alpha;
2148
Romain Guy2728f962010-10-08 18:36:15 -07002149 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002150 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002151
Romain Guy211370f2012-02-01 16:10:55 -08002152 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002153 mCaches.activeTexture(0);
2154 Texture* texture = mCaches.textureCache.get(bitmap);
2155 if (!texture) return DrawGlInfo::kStatusDone;
2156 const AutoTexture autoCleanup(texture);
2157 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2158 texture->setFilter(GL_LINEAR, true);
2159
Romain Guy3b753822013-03-05 10:27:35 -08002160 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002161 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002162 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002163 const float offsetX = left + currentTransform().getTranslateX();
2164 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002165 const size_t count = mesh->quads.size();
2166 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002167 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002168 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002169 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2170 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2171 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002172 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002173 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002174 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002175 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002176 }
2177 }
2178
Romain Guy211370f2012-02-01 16:10:55 -08002179 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002180 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2181 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002182
2183 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2184 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2185 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2186 true, !mesh->hasEmptyQuads);
2187 } else {
2188 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2189 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2190 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2191 true, !mesh->hasEmptyQuads);
2192 }
Romain Guy054dc182010-10-15 17:55:25 -07002193 }
Chet Haase48659092012-05-31 15:21:51 -07002194
2195 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002196}
2197
Chris Craik65cd6122012-12-10 17:56:27 -08002198status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2199 bool useOffset) {
2200 if (!vertexBuffer.getSize()) {
2201 // no vertices to draw
2202 return DrawGlInfo::kStatusDone;
2203 }
2204
Chris Craikcb4d6002012-09-25 12:00:29 -07002205 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002206 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2207 bool isAA = paint->isAntiAlias();
2208
Chris Craik710f46d2012-09-17 17:25:49 -07002209 setupDraw();
2210 setupDrawNoTexture();
2211 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002212 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2213 setupDrawColorFilter();
2214 setupDrawShader();
2215 setupDrawBlending(isAA, mode);
2216 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002217 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002218 setupDrawColorUniforms();
2219 setupDrawColorFilterUniforms();
2220 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002221
Chris Craik710f46d2012-09-17 17:25:49 -07002222 void* vertices = vertexBuffer.getBuffer();
2223 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002224 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002225 mCaches.resetTexCoordsVertexPointer();
2226 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002227
Chris Craik710f46d2012-09-17 17:25:49 -07002228 int alphaSlot = -1;
2229 if (isAA) {
2230 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2231 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002232
Chris Craik710f46d2012-09-17 17:25:49 -07002233 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002234 glEnableVertexAttribArray(alphaSlot);
2235 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002236 }
Romain Guy04299382012-07-18 17:15:41 -07002237
Chris Craik710f46d2012-09-17 17:25:49 -07002238 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002239
Chris Craik710f46d2012-09-17 17:25:49 -07002240 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002241 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002242 }
Chris Craik65cd6122012-12-10 17:56:27 -08002243
2244 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002245}
2246
2247/**
Chris Craik65cd6122012-12-10 17:56:27 -08002248 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2249 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2250 * screen space in all directions. However, instead of using a fragment shader to compute the
2251 * translucency of the color from its position, we simply use a varying parameter to define how far
2252 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2253 *
2254 * Doesn't yet support joins, caps, or path effects.
2255 */
2256status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2257 VertexBuffer vertexBuffer;
2258 // TODO: try clipping large paths to viewport
2259 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2260
Romain Guyc46d07a2013-03-15 19:06:39 -07002261 if (hasLayer()) {
2262 SkRect bounds = path.getBounds();
2263 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2264 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2265 }
Chris Craik65cd6122012-12-10 17:56:27 -08002266
2267 return drawVertexBuffer(vertexBuffer, paint);
2268}
2269
2270/**
2271 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2272 * and additional geometry for defining an alpha slope perimeter.
2273 *
2274 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2275 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2276 * in-shader alpha region, but found it to be taxing on some GPUs.
2277 *
2278 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2279 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002280 */
Chet Haase48659092012-05-31 15:21:51 -07002281status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002282 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002283
Chris Craik65cd6122012-12-10 17:56:27 -08002284 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002285
Chris Craik65cd6122012-12-10 17:56:27 -08002286 VertexBuffer buffer;
2287 SkRect bounds;
2288 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002289
2290 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2291 return DrawGlInfo::kStatusDone;
2292 }
2293
Romain Guy3b753822013-03-05 10:27:35 -08002294 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002295
Chris Craik65cd6122012-12-10 17:56:27 -08002296 bool useOffset = !paint->isAntiAlias();
2297 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002298}
2299
Chet Haase48659092012-05-31 15:21:51 -07002300status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2301 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002302
2303 // TODO: The paint's cap style defines whether the points are square or circular
2304 // TODO: Handle AA for round points
2305
Chet Haase5b0200b2011-04-13 17:58:08 -07002306 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002307 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002308 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002309 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002310 if (isHairLine) {
2311 // Now that we know it's hairline, we can set the effective width, to be used later
2312 strokeWidth = 1.0f;
2313 }
2314 const float halfWidth = strokeWidth / 2;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002315
Romain Guyed6fcb02011-03-21 13:11:28 -07002316 int alpha;
2317 SkXfermode::Mode mode;
2318 getAlphaAndMode(paint, &alpha, &mode);
2319
2320 int verticesCount = count >> 1;
2321 int generatedVerticesCount = 0;
2322
2323 TextureVertex pointsData[verticesCount];
2324 TextureVertex* vertex = &pointsData[0];
2325
Romain Guy04299382012-07-18 17:15:41 -07002326 // TODO: We should optimize this method to not generate vertices for points
2327 // that lie outside of the clip.
2328 mCaches.enableScissor();
2329
Romain Guyed6fcb02011-03-21 13:11:28 -07002330 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002331 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002332 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002333 setupDrawColor(paint->getColor(), alpha);
2334 setupDrawColorFilter();
2335 setupDrawShader();
2336 setupDrawBlending(mode);
2337 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002338 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002339 setupDrawColorUniforms();
2340 setupDrawColorFilterUniforms();
2341 setupDrawPointUniforms();
2342 setupDrawShaderIdentityUniforms();
2343 setupDrawMesh(vertex);
2344
2345 for (int i = 0; i < count; i += 2) {
2346 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2347 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002348
Chet Haase8a5cc922011-04-26 07:28:09 -07002349 float left = points[i] - halfWidth;
2350 float right = points[i] + halfWidth;
2351 float top = points[i + 1] - halfWidth;
2352 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002353
Romain Guy3b753822013-03-05 10:27:35 -08002354 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyed6fcb02011-03-21 13:11:28 -07002355 }
2356
2357 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002358
2359 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002360}
2361
Chet Haase48659092012-05-31 15:21:51 -07002362status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002363 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002364 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002365
Romain Guyae88e5e2010-10-22 17:49:18 -07002366 Rect& clip(*mSnapshot->clipRect);
2367 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002368
Romain Guy3d58c032010-07-14 16:34:53 -07002369 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002370
2371 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002372}
Romain Guy9d5316e2010-06-24 19:30:36 -07002373
Chet Haase48659092012-05-31 15:21:51 -07002374status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2375 SkPaint* paint) {
2376 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002377 const AutoTexture autoCleanup(texture);
2378
2379 const float x = left + texture->left - texture->offset;
2380 const float y = top + texture->top - texture->offset;
2381
2382 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002383
2384 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002385}
2386
Chet Haase48659092012-05-31 15:21:51 -07002387status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002388 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002389 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002390 return DrawGlInfo::kStatusDone;
2391 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002392
Chris Craikcb4d6002012-09-25 12:00:29 -07002393 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002394 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002395 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002396 right - left, bottom - top, rx, ry, p);
2397 return drawShape(left, top, texture, p);
2398 }
2399
2400 SkPath path;
2401 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002402 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2403 float outset = p->getStrokeWidth() / 2;
2404 rect.outset(outset, outset);
2405 rx += outset;
2406 ry += outset;
2407 }
Chris Craik710f46d2012-09-17 17:25:49 -07002408 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002409 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002410}
2411
Chris Craik710f46d2012-09-17 17:25:49 -07002412status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002413 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2414 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002415 return DrawGlInfo::kStatusDone;
2416 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002417 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002418 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002419 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002420 return drawShape(x - radius, y - radius, texture, p);
2421 }
2422
2423 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002424 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2425 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2426 } else {
2427 path.addCircle(x, y, radius);
2428 }
Chris Craik65cd6122012-12-10 17:56:27 -08002429 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002430}
Romain Guy01d58e42011-01-19 21:54:02 -08002431
Chet Haase48659092012-05-31 15:21:51 -07002432status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002433 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002434 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002435 return DrawGlInfo::kStatusDone;
2436 }
Romain Guy01d58e42011-01-19 21:54:02 -08002437
Chris Craikcb4d6002012-09-25 12:00:29 -07002438 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002439 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002440 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002441 return drawShape(left, top, texture, p);
2442 }
2443
2444 SkPath path;
2445 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002446 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2447 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2448 }
Chris Craik710f46d2012-09-17 17:25:49 -07002449 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002450 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002451}
2452
Chet Haase48659092012-05-31 15:21:51 -07002453status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002454 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
2455 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
2456 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002457 }
2458
Chris Craik780c1282012-10-04 14:10:49 -07002459 if (fabs(sweepAngle) >= 360.0f) {
2460 return drawOval(left, top, right, bottom, p);
2461 }
2462
2463 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002464 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002465 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002466 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002467 startAngle, sweepAngle, useCenter, p);
2468 return drawShape(left, top, texture, p);
2469 }
2470
2471 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2472 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2473 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2474 }
2475
2476 SkPath path;
2477 if (useCenter) {
2478 path.moveTo(rect.centerX(), rect.centerY());
2479 }
2480 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2481 if (useCenter) {
2482 path.close();
2483 }
Chris Craik65cd6122012-12-10 17:56:27 -08002484 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002485}
2486
Romain Guycf8675e2012-10-02 12:32:25 -07002487// See SkPaintDefaults.h
2488#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2489
Chet Haase48659092012-05-31 15:21:51 -07002490status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002491 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002492 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002493 }
2494
Chris Craik710f46d2012-09-17 17:25:49 -07002495 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002496 // only fill style is supported by drawConvexPath, since others have to handle joins
2497 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2498 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2499 mCaches.activeTexture(0);
2500 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002501 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002502 return drawShape(left, top, texture, p);
2503 }
2504
2505 SkPath path;
2506 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2507 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2508 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2509 }
2510 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002511 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002512 }
2513
Romain Guy3b753822013-03-05 10:27:35 -08002514 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002515 SkPath path;
2516 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002517 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002518 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002519 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002520 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002521 }
Romain Guyc7d53492010-06-25 13:41:57 -07002522}
Romain Guy9d5316e2010-06-24 19:30:36 -07002523
Raph Levien416a8472012-07-19 22:48:17 -07002524void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2525 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2526 float x, float y) {
2527 mCaches.activeTexture(0);
2528
2529 // NOTE: The drop shadow will not perform gamma correction
2530 // if shader-based correction is enabled
2531 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2532 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002533 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Raph Levien416a8472012-07-19 22:48:17 -07002534 const AutoTexture autoCleanup(shadow);
2535
Chris Craikc3566d02013-02-04 16:16:33 -08002536 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2537 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002538
Chris Craikc3566d02013-02-04 16:16:33 -08002539 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2540 int shadowColor = mDrawModifiers.mShadowColor;
2541 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002542 shadowColor = 0xffffffff;
2543 }
2544
2545 setupDraw();
2546 setupDrawWithTexture(true);
2547 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2548 setupDrawColorFilter();
2549 setupDrawShader();
2550 setupDrawBlending(true, mode);
2551 setupDrawProgram();
2552 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2553 setupDrawTexture(shadow->id);
2554 setupDrawPureColorUniforms();
2555 setupDrawColorFilterUniforms();
2556 setupDrawShaderUniforms();
2557 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2558
2559 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2560}
2561
Romain Guy768bffc2013-02-27 13:50:45 -08002562bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2563 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2564 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2565}
2566
Chet Haase48659092012-05-31 15:21:51 -07002567status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002568 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002569 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002570 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002571 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002572
Romain Guy671d6cf2012-01-18 12:39:17 -08002573 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002574 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002575 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002576 }
2577
2578 float x = 0.0f;
2579 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002580 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002581 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002582 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2583 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002584 }
2585
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002586 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002587 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002588
2589 int alpha;
2590 SkXfermode::Mode mode;
2591 getAlphaAndMode(paint, &alpha, &mode);
2592
Chris Craikc3566d02013-02-04 16:16:33 -08002593 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002594 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2595 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002596 }
2597
Romain Guy671d6cf2012-01-18 12:39:17 -08002598 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002599 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002600 if (pureTranslate && !linearFilter) {
2601 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2602 }
2603
2604 mCaches.activeTexture(0);
2605 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002606 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002607 setupDrawDirtyRegionsDisabled();
2608 setupDrawWithTexture(true);
2609 setupDrawAlpha8Color(paint->getColor(), alpha);
2610 setupDrawColorFilter();
2611 setupDrawShader();
2612 setupDrawBlending(true, mode);
2613 setupDrawProgram();
2614 setupDrawModelView(x, y, x, y, pureTranslate, true);
2615 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2616 setupDrawPureColorUniforms();
2617 setupDrawColorFilterUniforms();
2618 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002619 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002620
2621 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2622 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2623
Romain Guy211370f2012-02-01 16:10:55 -08002624 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002625
2626 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2627 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002628 if (hasActiveLayer) {
2629 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002630 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002631 }
2632 dirtyLayerUnchecked(bounds, getRegion());
2633 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002634 }
Chet Haase48659092012-05-31 15:21:51 -07002635
2636 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002637}
2638
Romain Guy624234f2013-03-05 16:43:31 -08002639mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2640 mat4 fontTransform;
2641 if (CC_LIKELY(transform.isPureTranslate())) {
2642 fontTransform = mat4::identity();
2643 } else {
2644 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002645 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002646 } 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
Chris Craika08f95c2013-03-15 17:24:33 -07002831status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
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) {
Chris Craika08f95c2013-03-15 17:24:33 -07002869 const float a = layer->getAlpha() / 255.0f * mSnapshot->alpha;
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
Chris Craika08f95c2013-03-15 17:24:33 -07002976SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08002977 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08002978 return paint;
2979 }
Romain Guy5ff9df62012-01-23 17:09:05 -08002980
2981 uint32_t flags = paint->getFlags();
2982
2983 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08002984 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
2985 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08002986
2987 return &mFilteredPaint;
2988}
2989
2990///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002991// Drawing implementation
2992///////////////////////////////////////////////////////////////////////////////
2993
Romain Guy01d58e42011-01-19 21:54:02 -08002994void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2995 float x, float y, SkPaint* paint) {
2996 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2997 return;
2998 }
2999
3000 int alpha;
3001 SkXfermode::Mode mode;
3002 getAlphaAndMode(paint, &alpha, &mode);
3003
3004 setupDraw();
3005 setupDrawWithTexture(true);
3006 setupDrawAlpha8Color(paint->getColor(), alpha);
3007 setupDrawColorFilter();
3008 setupDrawShader();
3009 setupDrawBlending(true, mode);
3010 setupDrawProgram();
3011 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3012 setupDrawTexture(texture->id);
3013 setupDrawPureColorUniforms();
3014 setupDrawColorFilterUniforms();
3015 setupDrawShaderUniforms();
3016 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3017
3018 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3019
3020 finishDrawTexture();
3021}
3022
Romain Guyf607bdc2010-09-10 19:20:06 -07003023// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003024#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3025#define kStdUnderline_Offset (1.0f / 9.0f)
3026#define kStdUnderline_Thickness (1.0f / 18.0f)
3027
3028void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3029 float x, float y, SkPaint* paint) {
3030 // Handle underline and strike-through
3031 uint32_t flags = paint->getFlags();
3032 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003033 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003034 float underlineWidth = length;
3035 // If length is > 0.0f, we already measured the text for the text alignment
3036 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003037 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003038 }
3039
Romain Guy211370f2012-02-01 16:10:55 -08003040 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003041 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003042 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003043
Raph Levien8b4072d2012-07-30 15:50:00 -07003044 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003045 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003046
Romain Guyf6834472011-01-23 13:32:12 -08003047 int linesCount = 0;
3048 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3049 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3050
3051 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003052 float points[pointsCount];
3053 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003054
3055 if (flags & SkPaint::kUnderlineText_Flag) {
3056 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003057 points[currentPoint++] = left;
3058 points[currentPoint++] = top;
3059 points[currentPoint++] = left + underlineWidth;
3060 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003061 }
3062
3063 if (flags & SkPaint::kStrikeThruText_Flag) {
3064 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003065 points[currentPoint++] = left;
3066 points[currentPoint++] = top;
3067 points[currentPoint++] = left + underlineWidth;
3068 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003069 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003070
Romain Guy726aeba2011-06-01 14:52:00 -07003071 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003072
Romain Guy726aeba2011-06-01 14:52:00 -07003073 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003074 }
3075 }
3076}
3077
Romain Guy672433d2013-01-04 19:05:13 -08003078status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3079 if (mSnapshot->isIgnored()) {
3080 return DrawGlInfo::kStatusDone;
3081 }
3082
Romain Guy735738c2012-12-03 12:34:51 -08003083 int color = paint->getColor();
3084 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003085 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003086 color |= 0x00ffffff;
3087 }
3088 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3089
3090 return drawColorRects(rects, count, color, mode);
3091}
3092
3093status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003094 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003095 if (count == 0) {
3096 return DrawGlInfo::kStatusDone;
3097 }
Romain Guy735738c2012-12-03 12:34:51 -08003098
Romain Guy672433d2013-01-04 19:05:13 -08003099 float left = FLT_MAX;
3100 float top = FLT_MAX;
3101 float right = FLT_MIN;
3102 float bottom = FLT_MIN;
3103
3104 int vertexCount = 0;
3105 Vertex mesh[count * 6];
3106 Vertex* vertex = mesh;
3107
Chris Craik2af46352012-11-26 18:30:17 -08003108 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003109 float l = rects[index + 0];
3110 float t = rects[index + 1];
3111 float r = rects[index + 2];
3112 float b = rects[index + 3];
3113
Romain Guy3b753822013-03-05 10:27:35 -08003114 Vertex::set(vertex++, l, b);
3115 Vertex::set(vertex++, l, t);
3116 Vertex::set(vertex++, r, t);
3117 Vertex::set(vertex++, l, b);
3118 Vertex::set(vertex++, r, t);
3119 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003120
Romain Guy3b753822013-03-05 10:27:35 -08003121 vertexCount += 6;
Romain Guy672433d2013-01-04 19:05:13 -08003122
Romain Guy3b753822013-03-05 10:27:35 -08003123 left = fminf(left, l);
3124 top = fminf(top, t);
3125 right = fmaxf(right, r);
3126 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003127 }
3128
Romain Guy3b753822013-03-05 10:27:35 -08003129 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003130 return DrawGlInfo::kStatusDone;
3131 }
Romain Guy672433d2013-01-04 19:05:13 -08003132
Romain Guy672433d2013-01-04 19:05:13 -08003133 setupDraw();
3134 setupDrawNoTexture();
3135 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3136 setupDrawShader();
3137 setupDrawColorFilter();
3138 setupDrawBlending(mode);
3139 setupDrawProgram();
3140 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003141 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003142 setupDrawColorUniforms();
3143 setupDrawShaderUniforms();
3144 setupDrawColorFilterUniforms();
3145 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3146
Romain Guy8ce00302013-01-15 18:51:42 -08003147 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003148 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003149 }
3150
3151 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3152
3153 return DrawGlInfo::kStatusDrew;
3154}
3155
Romain Guy026c5e162010-06-28 17:12:22 -07003156void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003157 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003158 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003159 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003160 color |= 0x00ffffff;
3161 }
3162
Romain Guy70ca14e2010-12-13 18:24:33 -08003163 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003164 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003165 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003166 setupDrawShader();
3167 setupDrawColorFilter();
3168 setupDrawBlending(mode);
3169 setupDrawProgram();
3170 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3171 setupDrawColorUniforms();
3172 setupDrawShaderUniforms(ignoreTransform);
3173 setupDrawColorFilterUniforms();
3174 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003175
Romain Guyc95c8d62010-09-17 15:31:32 -07003176 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3177}
3178
Romain Guy82ba8142010-07-09 13:25:56 -07003179void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003180 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003181 int alpha;
3182 SkXfermode::Mode mode;
3183 getAlphaAndMode(paint, &alpha, &mode);
3184
Romain Guyd21b6e12011-11-30 20:21:23 -08003185 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003186
Romain Guy3b753822013-03-05 10:27:35 -08003187 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3188 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3189 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003190
Romain Guyd21b6e12011-11-30 20:21:23 -08003191 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003192 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3193 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3194 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3195 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003196 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003197 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3198 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3199 GL_TRIANGLE_STRIP, gMeshCount);
3200 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003201}
3202
Romain Guybd6b79b2010-06-26 00:13:53 -07003203void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003204 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3205 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003206 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003207}
3208
3209void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003210 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003211 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003212 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003213
Romain Guy746b7402010-10-26 16:27:31 -07003214 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003215 setupDrawWithTexture();
3216 setupDrawColor(alpha, alpha, alpha, alpha);
3217 setupDrawColorFilter();
3218 setupDrawBlending(blend, mode, swapSrcDst);
3219 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003220 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003221 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003222 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003223 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003224 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003225 }
Romain Guy886b2752013-01-04 12:26:18 -08003226 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003227 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003228 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003229 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003230
Romain Guy6820ac82010-09-15 18:11:50 -07003231 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003232
3233 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003234}
3235
Romain Guy886b2752013-01-04 12:26:18 -08003236void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3237 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3238 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3239 bool ignoreTransform, bool dirty) {
3240
3241 setupDraw();
3242 setupDrawWithTexture(true);
3243 if (hasColor) {
3244 setupDrawAlpha8Color(color, alpha);
3245 }
3246 setupDrawColorFilter();
3247 setupDrawShader();
3248 setupDrawBlending(true, mode);
3249 setupDrawProgram();
3250 if (!dirty) setupDrawDirtyRegionsDisabled();
3251 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3252 setupDrawTexture(texture);
3253 setupDrawPureColorUniforms();
3254 setupDrawColorFilterUniforms();
3255 setupDrawShaderUniforms();
3256 setupDrawMesh(vertices, texCoords);
3257
3258 glDrawArrays(drawMode, 0, elementsCount);
3259
3260 finishDrawTexture();
3261}
3262
Romain Guya5aed0d2010-09-09 14:42:43 -07003263void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003264 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003265 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003266
Romain Guy82ba8142010-07-09 13:25:56 -07003267 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003268 // These blend modes are not supported by OpenGL directly and have
3269 // to be implemented using shaders. Since the shader will perform
3270 // the blending, turn blending off here
3271 // If the blend mode cannot be implemented using shaders, fall
3272 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003273 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003274 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003275 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003276 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003277
Romain Guy82bc7a72012-01-03 14:13:39 -08003278 if (mCaches.blend) {
3279 glDisable(GL_BLEND);
3280 mCaches.blend = false;
3281 }
3282
3283 return;
3284 } else {
3285 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003286 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003287 }
3288
3289 if (!mCaches.blend) {
3290 glEnable(GL_BLEND);
3291 }
3292
3293 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3294 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3295
3296 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3297 glBlendFunc(sourceMode, destMode);
3298 mCaches.lastSrcMode = sourceMode;
3299 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003300 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003301 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003302 glDisable(GL_BLEND);
3303 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003304 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003305}
3306
Romain Guy889f8d12010-07-29 14:37:42 -07003307bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003308 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003309 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003310 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003311 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003312 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003313 }
Romain Guy6926c722010-07-12 20:20:03 -07003314 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003315}
3316
Romain Guy026c5e162010-06-28 17:12:22 -07003317void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003318 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003319 TextureVertex::setUV(v++, u1, v1);
3320 TextureVertex::setUV(v++, u2, v1);
3321 TextureVertex::setUV(v++, u1, v2);
3322 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003323}
3324
Chet Haase5c13d892010-10-08 08:37:55 -07003325void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003326 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003327 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003328}
3329
Romain Guy9d5316e2010-06-24 19:30:36 -07003330}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003331}; // namespace android