blob: 08e2332df80bc51522b7a3ccaeec292f19eb432b [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"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Chris Craik65cd6122012-12-10 17:56:27 -080036#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070037#include "Properties.h"
Romain Guya957eea2010-12-08 18:34:42 -080038#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070039
40namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070041namespace uirenderer {
42
43///////////////////////////////////////////////////////////////////////////////
44// Defines
45///////////////////////////////////////////////////////////////////////////////
46
Romain Guy759ea802010-09-16 20:49:46 -070047#define RAD_TO_DEG (180.0f / 3.14159265f)
48#define MIN_ANGLE 0.001f
49
Romain Guyf8773082012-07-12 18:01:00 -070050#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070051
Romain Guy713e1bb2012-10-16 18:44:09 -070052#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080053
Romain Guy9d5316e2010-06-24 19:30:36 -070054///////////////////////////////////////////////////////////////////////////////
55// Globals
56///////////////////////////////////////////////////////////////////////////////
57
Romain Guy889f8d12010-07-29 14:37:42 -070058/**
59 * Structure mapping Skia xfermodes to OpenGL blending factors.
60 */
61struct Blender {
62 SkXfermode::Mode mode;
63 GLenum src;
64 GLenum dst;
65}; // struct Blender
66
Romain Guy026c5e162010-06-28 17:12:22 -070067// In this array, the index of each Blender equals the value of the first
68// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
69static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070070 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
71 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
72 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
73 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
74 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
75 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
77 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
78 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
81 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
82 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050083 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070084 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070085};
Romain Guye4d01122010-06-16 18:44:05 -070086
Romain Guy87a76572010-09-13 18:11:21 -070087// This array contains the swapped version of each SkXfermode. For instance
88// this array's SrcOver blending mode is actually DstOver. You can refer to
89// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070090static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070091 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
92 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
93 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
94 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
95 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
96 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
97 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
100 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
101 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500104 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700105 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700106};
107
Romain Guyf6a11b82010-06-23 17:47:49 -0700108///////////////////////////////////////////////////////////////////////////////
109// Constructors/destructor
110///////////////////////////////////////////////////////////////////////////////
111
Romain Guy3bbacf22013-02-06 16:51:04 -0800112OpenGLRenderer::OpenGLRenderer():
113 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700114 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700115 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700116 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800117 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700118
Romain Guyac670c02010-07-27 17:39:27 -0700119 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
120
Romain Guyae5575b2010-07-29 18:48:04 -0700121 mFirstSnapshot = new Snapshot;
Romain Guy87e2f7572012-09-24 11:37:12 -0700122
123 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700124}
125
Romain Guy85bf02f2010-06-22 13:11:24 -0700126OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700127 // The context has already been destroyed at this point, do not call
128 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700129}
130
Romain Guy87e2f7572012-09-24 11:37:12 -0700131void OpenGLRenderer::initProperties() {
132 char property[PROPERTY_VALUE_MAX];
133 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
134 mScissorOptimizationDisabled = !strcasecmp(property, "true");
135 INIT_LOGD(" Scissor optimization %s",
136 mScissorOptimizationDisabled ? "disabled" : "enabled");
137 } else {
138 INIT_LOGD(" Scissor optimization enabled");
139 }
Romain Guy13631f32012-01-30 17:41:55 -0800140}
141
142///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700143// Setup
144///////////////////////////////////////////////////////////////////////////////
145
Romain Guyef359272013-01-31 19:07:29 -0800146void OpenGLRenderer::setName(const char* name) {
147 if (name) {
148 mName.setTo(name);
149 } else {
150 mName.clear();
151 }
152}
153
154const char* OpenGLRenderer::getName() const {
155 return mName.string();
156}
157
Romain Guy49c5fc02012-05-15 11:10:01 -0700158bool OpenGLRenderer::isDeferred() {
159 return false;
160}
161
Romain Guy85bf02f2010-06-22 13:11:24 -0700162void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700163 initViewport(width, height);
164
165 glDisable(GL_DITHER);
166 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
167
168 glEnableVertexAttribArray(Program::kBindingPosition);
169}
170
171void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700172 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700173
174 mWidth = width;
175 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700176
177 mFirstSnapshot->height = height;
178 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700179}
180
Romain Guy7c25aab2012-10-18 15:05:02 -0700181status_t OpenGLRenderer::prepare(bool opaque) {
Chet Haase44b2fe32012-06-06 19:03:58 -0700182 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800183}
184
Romain Guyc3fedaf2013-01-29 17:26:25 -0800185status_t OpenGLRenderer::prepareDirty(float left, float top,
186 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800187 mCaches.clearGarbage();
188
Romain Guy8aef54f2010-09-01 15:13:49 -0700189 mSnapshot = new Snapshot(mFirstSnapshot,
190 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800191 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700192 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700193
Romain Guy7d7b5492011-01-24 16:33:45 -0800194 mSnapshot->setClip(left, top, right, bottom);
Romain Guy41308e22012-10-22 20:02:43 -0700195 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700196
Romain Guy11cb6422012-09-21 00:39:43 -0700197 updateLayers();
198
Romain Guydcfc8362013-01-03 13:08:57 -0800199 discardFramebuffer(left, top, right, bottom);
Romain Guy45e4c3d2012-09-11 17:17:07 -0700200
Romain Guyddf74372012-05-22 14:07:07 -0700201 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800202
Romain Guy54c1a642012-09-27 17:55:46 -0700203 // Functors break the tiling extension in pretty spectacular ways
204 // This ensures we don't use tiling when a functor is going to be
205 // invoked during the frame
206 mSuppressTiling = mCaches.hasRegisteredFunctors();
207
Romain Guy2b7028e2012-09-19 17:25:38 -0700208 mTilingSnapshot = mSnapshot;
Romain Guy57b52682012-09-20 17:38:46 -0700209 startTiling(mTilingSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700210
Romain Guy7c450aa2012-09-21 19:15:00 -0700211 debugOverdraw(true, true);
212
Romain Guy7c25aab2012-10-18 15:05:02 -0700213 return clear(left, top, right, bottom, opaque);
214}
215
Romain Guydcfc8362013-01-03 13:08:57 -0800216void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
217 // If we know that we are going to redraw the entire framebuffer,
218 // perform a discard to let the driver know we don't need to preserve
219 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800220 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800221 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800222 const bool isFbo = getTargetFbo() == 0;
223 const GLenum attachments[] = {
224 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
225 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800226 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
227 }
228}
229
Romain Guy7c25aab2012-10-18 15:05:02 -0700230status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700231 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700232 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700233 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700234 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700235 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700236 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700237
Romain Guy7c25aab2012-10-18 15:05:02 -0700238 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700239 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700240}
241
242void OpenGLRenderer::syncState() {
243 glViewport(0, 0, mWidth, mHeight);
244
245 if (mCaches.blend) {
246 glEnable(GL_BLEND);
247 } else {
248 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700249 }
Romain Guybb9524b2010-06-22 18:56:38 -0700250}
251
Romain Guy57b52682012-09-20 17:38:46 -0700252void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700253 if (!mSuppressTiling) {
254 Rect* clip = mTilingSnapshot->clipRect;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800255 if (s->flags & Snapshot::kFlagFboTarget) {
256 clip = &s->layer->clipRect;
Romain Guy54c1a642012-09-27 17:55:46 -0700257 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700258
Romain Guyc3fedaf2013-01-29 17:26:25 -0800259 startTiling(*clip, s->height, opaque);
260 }
261}
262
263void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
264 if (!mSuppressTiling) {
265 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
266 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700267 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700268}
269
270void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700271 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700272}
273
Romain Guyb025b9c2010-09-16 14:16:48 -0700274void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700275 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700276 endTiling();
277
Romain Guy11cb6422012-09-21 00:39:43 -0700278 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700279#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700280 GLenum status = GL_NO_ERROR;
281 while ((status = glGetError()) != GL_NO_ERROR) {
282 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
283 switch (status) {
284 case GL_INVALID_ENUM:
285 ALOGE(" GL_INVALID_ENUM");
286 break;
287 case GL_INVALID_VALUE:
288 ALOGE(" GL_INVALID_VALUE");
289 break;
290 case GL_INVALID_OPERATION:
291 ALOGE(" GL_INVALID_OPERATION");
292 break;
293 case GL_OUT_OF_MEMORY:
294 ALOGE(" Out of memory!");
295 break;
296 }
Romain Guya07105b2011-01-10 21:14:18 -0800297 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700298#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700299
Romain Guyc15008e2010-11-10 11:59:15 -0800300#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800301 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700302#else
303 if (mCaches.getDebugLevel() & kDebugMemory) {
304 mCaches.dumpMemoryUsage();
305 }
Romain Guyc15008e2010-11-10 11:59:15 -0800306#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700307 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700308}
309
Romain Guy6c319ca2011-01-11 14:29:25 -0800310void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700311 if (mCaches.currentProgram) {
312 if (mCaches.currentProgram->isInUse()) {
313 mCaches.currentProgram->remove();
314 mCaches.currentProgram = NULL;
315 }
316 }
Romain Guy50c0f092010-10-19 11:42:22 -0700317 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800318 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800319 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800320 mCaches.disbaleTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700321 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700322}
323
Romain Guy6c319ca2011-01-11 14:29:25 -0800324void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800325 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800326 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700327 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700328 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700329
Romain Guy3e263fa2011-12-12 16:47:48 -0800330 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
331
Chet Haase80250612012-08-15 13:46:54 -0700332 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700333 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800334 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700335 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700336
Romain Guya1d3c912011-12-13 14:55:06 -0800337 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700338
Romain Guy50c0f092010-10-19 11:42:22 -0700339 mCaches.blend = true;
340 glEnable(GL_BLEND);
341 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
342 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700343}
344
Romain Guy35643dd2012-09-18 15:40:58 -0700345void OpenGLRenderer::resumeAfterLayer() {
346 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
347 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
348 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700349 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700350
351 mCaches.resetScissor();
352 dirtyClip();
353}
354
Romain Guyba6be8a2012-04-23 18:22:09 -0700355void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700356 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700357}
358
359void OpenGLRenderer::attachFunctor(Functor* functor) {
360 mFunctors.add(functor);
361}
362
Romain Guy8f3b8e32012-03-27 16:33:45 -0700363status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
364 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700365 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700366
Romain Guyba6be8a2012-04-23 18:22:09 -0700367 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800368 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700369 SortedVector<Functor*> functors(mFunctors);
370 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700371
Romain Guyba6be8a2012-04-23 18:22:09 -0700372 DrawGlInfo info;
373 info.clipLeft = 0;
374 info.clipTop = 0;
375 info.clipRight = 0;
376 info.clipBottom = 0;
377 info.isLayer = false;
378 info.width = 0;
379 info.height = 0;
380 memset(info.transform, 0, sizeof(float) * 16);
381
382 for (size_t i = 0; i < count; i++) {
383 Functor* f = functors.itemAt(i);
384 result |= (*f)(DrawGlInfo::kModeProcess, &info);
385
Chris Craikc2c95432012-04-25 15:13:52 -0700386 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700387 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
388 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700389 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700390
Chris Craikc2c95432012-04-25 15:13:52 -0700391 if (result & DrawGlInfo::kStatusInvoke) {
392 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700393 }
394 }
Chris Craikd15321b2012-11-28 14:45:04 -0800395 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700396 }
397
398 return result;
399}
400
401status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800402 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700403 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700404
Romain Guy8a4ac612012-07-17 17:32:48 -0700405 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800406 if (mDirtyClip) {
407 setScissorFromClip();
408 }
Romain Guyd643bb52011-03-01 14:55:21 -0800409
Romain Guy80911b82011-03-16 15:30:12 -0700410 Rect clip(*mSnapshot->clipRect);
411 clip.snapToPixelBoundaries();
412
Romain Guyd643bb52011-03-01 14:55:21 -0800413 // Since we don't know what the functor will draw, let's dirty
414 // tne entire clip region
415 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800416 dirtyLayerUnchecked(clip, getRegion());
417 }
Romain Guyd643bb52011-03-01 14:55:21 -0800418
Romain Guy08aa2cb2011-03-17 11:06:57 -0700419 DrawGlInfo info;
420 info.clipLeft = clip.left;
421 info.clipTop = clip.top;
422 info.clipRight = clip.right;
423 info.clipBottom = clip.bottom;
424 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700425 info.width = getSnapshot()->viewport.getWidth();
426 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700427 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700428
Chet Haase48659092012-05-31 15:21:51 -0700429 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800430
Romain Guy8f3b8e32012-03-27 16:33:45 -0700431 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700432 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800433 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700434
Chris Craik65924a32012-04-05 17:52:11 -0700435 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700436 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700437 }
Romain Guycabfcc12011-03-07 18:06:46 -0800438 }
439
Chet Haasedaf98e92011-01-10 14:10:36 -0800440 resume();
Romain Guy65549432012-03-26 16:45:05 -0700441 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800442}
443
Romain Guyf6a11b82010-06-23 17:47:49 -0700444///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700445// Debug
446///////////////////////////////////////////////////////////////////////////////
447
448void OpenGLRenderer::startMark(const char* name) const {
449 mCaches.startMark(0, name);
450}
451
452void OpenGLRenderer::endMark() const {
453 mCaches.endMark();
454}
455
456void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
457 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
458 if (clear) {
459 mCaches.disableScissor();
460 mCaches.stencil.clear();
461 }
462 if (enable) {
463 mCaches.stencil.enableDebugWrite();
464 } else {
465 mCaches.stencil.disable();
466 }
467 }
468}
469
470void OpenGLRenderer::renderOverdraw() {
471 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
472 const Rect* clip = mTilingSnapshot->clipRect;
473
474 mCaches.enableScissor();
475 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
476 clip->right - clip->left, clip->bottom - clip->top);
477
478 mCaches.stencil.enableDebugTest(2);
479 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
480 mCaches.stencil.enableDebugTest(3);
481 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
482 mCaches.stencil.enableDebugTest(4);
483 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
484 mCaches.stencil.enableDebugTest(4, true);
485 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
486 mCaches.stencil.disable();
487 }
488}
489
490///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700491// Layers
492///////////////////////////////////////////////////////////////////////////////
493
494bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
495 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
496 OpenGLRenderer* renderer = layer->renderer;
497 Rect& dirty = layer->dirtyRect;
498
Romain Guy7c450aa2012-09-21 19:15:00 -0700499 if (inFrame) {
500 endTiling();
501 debugOverdraw(false, false);
502 }
Romain Guy11cb6422012-09-21 00:39:43 -0700503
504 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
505 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
506 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
507 renderer->finish();
508
509 if (inFrame) {
510 resumeAfterLayer();
511 startTiling(mSnapshot);
512 }
513
514 dirty.setEmpty();
515 layer->deferredUpdateScheduled = false;
516 layer->renderer = NULL;
517 layer->displayList = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -0800518 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700519
520 return true;
521 }
522
523 return false;
524}
525
526void OpenGLRenderer::updateLayers() {
527 int count = mLayerUpdates.size();
528 if (count > 0) {
529 startMark("Layer Updates");
530
531 // Note: it is very important to update the layers in reverse order
532 for (int i = count - 1; i >= 0; i--) {
533 Layer* layer = mLayerUpdates.itemAt(i);
534 updateLayer(layer, false);
535 mCaches.resourceCache.decrementRefcount(layer);
536 }
537 mLayerUpdates.clear();
538
539 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
540 endMark();
541 }
542}
543
544void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
545 if (layer) {
546 mLayerUpdates.push_back(layer);
547 mCaches.resourceCache.incrementRefcount(layer);
548 }
549}
550
551void OpenGLRenderer::clearLayerUpdates() {
552 size_t count = mLayerUpdates.size();
553 if (count > 0) {
554 mCaches.resourceCache.lock();
555 for (size_t i = 0; i < count; i++) {
556 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
557 }
558 mCaches.resourceCache.unlock();
559 mLayerUpdates.clear();
560 }
561}
562
563///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700564// State management
565///////////////////////////////////////////////////////////////////////////////
566
Romain Guybb9524b2010-06-22 18:56:38 -0700567int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700568 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700569}
570
571int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700572 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700573}
574
575void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700576 if (mSaveCount > 1) {
577 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700578 }
Romain Guybb9524b2010-06-22 18:56:38 -0700579}
580
581void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700582 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700583
Romain Guy8fb95422010-08-17 18:38:51 -0700584 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700585 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700586 }
Romain Guybb9524b2010-06-22 18:56:38 -0700587}
588
Romain Guy8aef54f2010-09-01 15:13:49 -0700589int OpenGLRenderer::saveSnapshot(int flags) {
590 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700591 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700592}
593
594bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700595 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700596 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700597 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700598
Romain Guybd6b79b2010-06-26 00:13:53 -0700599 sp<Snapshot> current = mSnapshot;
600 sp<Snapshot> previous = mSnapshot->previous;
601
Romain Guyeb993562010-10-05 18:14:38 -0700602 if (restoreOrtho) {
603 Rect& r = previous->viewport;
604 glViewport(r.left, r.top, r.right, r.bottom);
605 mOrthoMatrix.load(current->orthoMatrix);
606 }
607
Romain Guy8b55f372010-08-18 17:10:07 -0700608 mSaveCount--;
609 mSnapshot = previous;
610
Romain Guy2542d192010-08-18 11:47:12 -0700611 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700612 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700613 }
Romain Guy2542d192010-08-18 11:47:12 -0700614
Romain Guy5ec99242010-11-03 16:19:08 -0700615 if (restoreLayer) {
616 composeLayer(current, previous);
617 }
618
Romain Guy2542d192010-08-18 11:47:12 -0700619 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700620}
621
Romain Guyf6a11b82010-06-23 17:47:49 -0700622///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700623// Layers
624///////////////////////////////////////////////////////////////////////////////
625
626int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700627 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700628 const GLuint previousFbo = mSnapshot->fbo;
629 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700630
Romain Guyaf636eb2010-12-09 17:47:21 -0800631 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700632 int alpha = 255;
633 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700634
Romain Guye45362c2010-11-03 19:58:32 -0700635 if (p) {
636 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700637 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700638 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700639 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700640 }
Romain Guyd55a8612010-06-28 17:42:46 -0700641
Chet Haased48885a2012-08-28 17:43:28 -0700642 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700643 }
Romain Guyd55a8612010-06-28 17:42:46 -0700644
645 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700646}
647
648int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
649 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700650 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700651 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700652 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700653 SkPaint paint;
654 paint.setAlpha(alpha);
655 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700656 }
Romain Guyd55a8612010-06-28 17:42:46 -0700657}
Romain Guybd6b79b2010-06-26 00:13:53 -0700658
Romain Guy1c740bc2010-09-13 18:00:09 -0700659/**
660 * Layers are viewed by Skia are slightly different than layers in image editing
661 * programs (for instance.) When a layer is created, previously created layers
662 * and the frame buffer still receive every drawing command. For instance, if a
663 * layer is created and a shape intersecting the bounds of the layers and the
664 * framebuffer is draw, the shape will be drawn on both (unless the layer was
665 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
666 *
667 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
668 * texture. Unfortunately, this is inefficient as it requires every primitive to
669 * be drawn n + 1 times, where n is the number of active layers. In practice this
670 * means, for every primitive:
671 * - Switch active frame buffer
672 * - Change viewport, clip and projection matrix
673 * - Issue the drawing
674 *
675 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700676 * To avoid this, layers are implemented in a different way here, at least in the
677 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
678 * is set. When this flag is set we can redirect all drawing operations into a
679 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700680 *
681 * This implementation relies on the frame buffer being at least RGBA 8888. When
682 * a layer is created, only a texture is created, not an FBO. The content of the
683 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700684 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700685 * buffer and drawing continues as normal. This technique therefore treats the
686 * frame buffer as a scratch buffer for the layers.
687 *
688 * To compose the layers back onto the frame buffer, each layer texture
689 * (containing the original frame buffer data) is drawn as a simple quad over
690 * the frame buffer. The trick is that the quad is set as the composition
691 * destination in the blending equation, and the frame buffer becomes the source
692 * of the composition.
693 *
694 * Drawing layers with an alpha value requires an extra step before composition.
695 * An empty quad is drawn over the layer's region in the frame buffer. This quad
696 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
697 * quad is used to multiply the colors in the frame buffer. This is achieved by
698 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
699 * GL_ZERO, GL_SRC_ALPHA.
700 *
701 * Because glCopyTexImage2D() can be slow, an alternative implementation might
702 * be use to draw a single clipped layer. The implementation described above
703 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700704 *
705 * (1) The frame buffer is actually not cleared right away. To allow the GPU
706 * to potentially optimize series of calls to glCopyTexImage2D, the frame
707 * buffer is left untouched until the first drawing operation. Only when
708 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700709 */
Chet Haased48885a2012-08-28 17:43:28 -0700710bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
711 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700712 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700713 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700714
Romain Guyeb993562010-10-05 18:14:38 -0700715 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
716
Romain Guyf607bdc2010-09-10 19:20:06 -0700717 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700718 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700719 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700720 Rect untransformedBounds(bounds);
721 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700722
Chet Haased48885a2012-08-28 17:43:28 -0700723 // Layers only make sense if they are in the framebuffer's bounds
724 if (bounds.intersect(*mSnapshot->clipRect)) {
725 // We cannot work with sub-pixels in this case
726 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700727
Chet Haased48885a2012-08-28 17:43:28 -0700728 // When the layer is not an FBO, we may use glCopyTexImage so we
729 // need to make sure the layer does not extend outside the bounds
730 // of the framebuffer
731 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700732 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700733 } else if (fboLayer) {
734 clip.set(bounds);
735 mat4 inverse;
736 inverse.loadInverse(*mSnapshot->transform);
737 inverse.mapRect(clip);
738 clip.snapToPixelBoundaries();
739 if (clip.intersect(untransformedBounds)) {
740 clip.translate(-left, -top);
741 bounds.set(untransformedBounds);
742 } else {
743 clip.setEmpty();
744 }
Romain Guyad37cd32011-03-15 11:12:25 -0700745 }
Chet Haased48885a2012-08-28 17:43:28 -0700746 } else {
747 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700748 }
Romain Guybf434112010-09-16 14:40:17 -0700749
Romain Guy746b7402010-10-26 16:27:31 -0700750 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700751 bounds.getHeight() > mCaches.maxTextureSize ||
752 (fboLayer && clip.isEmpty())) {
753 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700754 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700755 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700756 }
757
758 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700759 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700760 return false;
761 }
Romain Guyf18fd992010-07-08 11:45:51 -0700762
Romain Guya1d3c912011-12-13 14:55:06 -0800763 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700764 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700765 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700766 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700767 }
768
Romain Guy9ace8f52011-07-07 20:50:11 -0700769 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700770 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700771 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
772 bounds.getWidth() / float(layer->getWidth()), 0.0f);
773 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700774 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700775 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700776
Romain Guy8fb95422010-08-17 18:38:51 -0700777 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700778 mSnapshot->flags |= Snapshot::kFlagIsLayer;
779 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700780
Romain Guyeb993562010-10-05 18:14:38 -0700781 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700782 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700783 } else {
784 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700785 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800786 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700787 if (layer->isEmpty()) {
788 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700789 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700790 layer->getWidth(), layer->getHeight(), 0);
791 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800792 } else {
793 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700794 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800795 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700796
Romain Guy54be1cd2011-06-13 19:04:27 -0700797 // Enqueue the buffer coordinates to clear the corresponding region later
798 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700799 }
Romain Guyeb993562010-10-05 18:14:38 -0700800 }
Romain Guyf86ef572010-07-01 11:05:42 -0700801
Romain Guyd55a8612010-06-28 17:42:46 -0700802 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700803}
804
Chet Haased48885a2012-08-28 17:43:28 -0700805bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800806 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700807 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700808
Chet Haased48885a2012-08-28 17:43:28 -0700809 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800810 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
811 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700812 mSnapshot->fbo = layer->getFbo();
813 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
814 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
815 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
816 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700817 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700818
Romain Guy2b7028e2012-09-19 17:25:38 -0700819 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700820 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700821 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700822 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
823 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700824
825 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700826 if (layer->isEmpty()) {
827 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
828 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700829 }
830
831 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700832 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700833
Romain Guyf735c8e2013-01-31 17:45:55 -0800834 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700835
836 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700837 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800838 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700839 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700840 glClear(GL_COLOR_BUFFER_BIT);
841
842 dirtyClip();
843
844 // Change the ortho projection
845 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
846 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
847
848 return true;
849}
850
Romain Guy1c740bc2010-09-13 18:00:09 -0700851/**
852 * Read the documentation of createLayer() before doing anything in this method.
853 */
Romain Guy1d83e192010-08-17 11:37:00 -0700854void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
855 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000856 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700857 return;
858 }
859
Romain Guy8ce00302013-01-15 18:51:42 -0800860 Layer* layer = current->layer;
861 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700862 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700863
864 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700865 endTiling();
866
Romain Guye0aa84b2012-04-03 19:30:26 -0700867 // Detach the texture from the FBO
868 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800869
870 layer->removeFbo(false);
871
Romain Guyeb993562010-10-05 18:14:38 -0700872 // Unbind current FBO and restore previous one
873 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700874 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700875
876 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700877 }
878
Romain Guy9ace8f52011-07-07 20:50:11 -0700879 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700880 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700881 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700882 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700883 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700884 }
885
Romain Guy03750a02010-10-18 14:06:08 -0700886 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700887
Romain Guya1d3c912011-12-13 14:55:06 -0800888 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700889
Romain Guy5b3b3522010-10-27 18:57:51 -0700890 // When the layer is stored in an FBO, we can save a bit of fillrate by
891 // drawing only the dirty region
892 if (fboLayer) {
893 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700894 if (layer->getColorFilter()) {
895 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800896 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700897 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700898 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800899 resetColorFilter();
900 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700901 } else if (!rect.isEmpty()) {
902 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
903 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700904 }
Romain Guy8b55f372010-08-18 17:10:07 -0700905
Romain Guy746b7402010-10-26 16:27:31 -0700906 dirtyClip();
907
Romain Guyeb993562010-10-05 18:14:38 -0700908 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700909 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700910 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700911 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700912 }
913}
914
Romain Guyaa6c24c2011-04-28 18:40:04 -0700915void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700916 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700917
918 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700919 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700920 setupDrawWithTexture();
921 } else {
922 setupDrawWithExternalTexture();
923 }
924 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700925 setupDrawColor(alpha, alpha, alpha, alpha);
926 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700927 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700928 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700929 setupDrawPureColorUniforms();
930 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700931 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
932 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700933 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700934 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700935 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700936 if (mSnapshot->transform->isPureTranslate() &&
937 layer->getWidth() == (uint32_t) rect.getWidth() &&
938 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700939 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
940 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
941
Romain Guyd21b6e12011-11-30 20:21:23 -0800942 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700943 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
944 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800945 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700946 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
947 }
948 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700949 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
950
951 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
952
953 finishDrawTexture();
954}
955
Romain Guy5b3b3522010-10-27 18:57:51 -0700956void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700957 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700958 const Rect& texCoords = layer->texCoords;
959 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
960 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700961
Romain Guy9ace8f52011-07-07 20:50:11 -0700962 float x = rect.left;
963 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700964 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700965 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700966 layer->getHeight() == (uint32_t) rect.getHeight();
967
968 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700969 // When we're swapping, the layer is already in screen coordinates
970 if (!swap) {
971 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
972 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
973 }
974
Romain Guyd21b6e12011-11-30 20:21:23 -0800975 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700976 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800977 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700978 }
979
980 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
981 layer->getTexture(), layer->getAlpha() / 255.0f,
982 layer->getMode(), layer->isBlend(),
983 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
984 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700985
Romain Guyaa6c24c2011-04-28 18:40:04 -0700986 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
987 } else {
988 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
989 drawTextureLayer(layer, rect);
990 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
991 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700992}
993
994void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700995 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700996 layer->setRegionAsRect();
997
Romain Guy40667672011-03-18 14:34:03 -0700998 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700999
Romain Guy5b3b3522010-10-27 18:57:51 -07001000 layer->region.clear();
1001 return;
1002 }
1003
Romain Guy8a3957d2011-09-07 17:55:15 -07001004 // TODO: See LayerRenderer.cpp::generateMesh() for important
1005 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -08001006 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001007 size_t count;
1008 const android::Rect* rects = layer->region.getArray(&count);
1009
Romain Guy9ace8f52011-07-07 20:50:11 -07001010 const float alpha = layer->getAlpha() / 255.0f;
1011 const float texX = 1.0f / float(layer->getWidth());
1012 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001013 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001014
Romain Guy8ce00302013-01-15 18:51:42 -08001015 setupDraw();
1016
1017 // We must get (and therefore bind) the region mesh buffer
1018 // after we setup drawing in case we need to mess with the
1019 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001020 TextureVertex* mesh = mCaches.getRegionMesh();
1021 GLsizei numQuads = 0;
1022
Romain Guy7230a742011-01-10 22:26:16 -08001023 setupDrawWithTexture();
1024 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001025 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001026 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001027 setupDrawProgram();
1028 setupDrawDirtyRegionsDisabled();
1029 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001030 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001031 setupDrawTexture(layer->getTexture());
1032 if (mSnapshot->transform->isPureTranslate()) {
1033 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
1034 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
1035
Romain Guyd21b6e12011-11-30 20:21:23 -08001036 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001037 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1038 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001039 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001040 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1041 }
Romain Guy15bc6432011-12-13 13:11:32 -08001042 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001043
1044 for (size_t i = 0; i < count; i++) {
1045 const android::Rect* r = &rects[i];
1046
1047 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001048 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001049 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001050 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001051
1052 // TODO: Reject quads outside of the clip
1053 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1054 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1055 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1056 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1057
1058 numQuads++;
1059
1060 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1061 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1062 numQuads = 0;
1063 mesh = mCaches.getRegionMesh();
1064 }
1065 }
1066
1067 if (numQuads > 0) {
1068 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1069 }
1070
Romain Guy7230a742011-01-10 22:26:16 -08001071 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001072
1073#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001074 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001075#endif
1076
1077 layer->region.clear();
1078 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001079}
1080
Romain Guy3a3133d2011-02-01 22:59:58 -08001081void OpenGLRenderer::drawRegionRects(const Region& region) {
1082#if DEBUG_LAYERS_AS_REGIONS
1083 size_t count;
1084 const android::Rect* rects = region.getArray(&count);
1085
1086 uint32_t colors[] = {
1087 0x7fff0000, 0x7f00ff00,
1088 0x7f0000ff, 0x7fff00ff,
1089 };
1090
1091 int offset = 0;
1092 int32_t top = rects[0].top;
1093
1094 for (size_t i = 0; i < count; i++) {
1095 if (top != rects[i].top) {
1096 offset ^= 0x2;
1097 top = rects[i].top;
1098 }
1099
1100 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1101 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1102 SkXfermode::kSrcOver_Mode);
1103 }
1104#endif
1105}
1106
Romain Guy8ce00302013-01-15 18:51:42 -08001107void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1108 SkXfermode::Mode mode, bool dirty) {
1109 int count = 0;
1110 Vector<float> rects;
1111
1112 SkRegion::Iterator it(region);
1113 while (!it.done()) {
1114 const SkIRect& r = it.rect();
1115 rects.push(r.fLeft);
1116 rects.push(r.fTop);
1117 rects.push(r.fRight);
1118 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001119 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001120 it.next();
1121 }
1122
Romain Guy3bbacf22013-02-06 16:51:04 -08001123 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001124}
1125
Romain Guy5b3b3522010-10-27 18:57:51 -07001126void OpenGLRenderer::dirtyLayer(const float left, const float top,
1127 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001128 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001129 Rect bounds(left, top, right, bottom);
1130 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001131 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001132 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001133}
1134
1135void OpenGLRenderer::dirtyLayer(const float left, const float top,
1136 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001137 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001138 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001139 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001140 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001141}
1142
1143void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001144 if (bounds.intersect(*mSnapshot->clipRect)) {
1145 bounds.snapToPixelBoundaries();
1146 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1147 if (!dirty.isEmpty()) {
1148 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001149 }
1150 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001151}
1152
Romain Guy54be1cd2011-06-13 19:04:27 -07001153void OpenGLRenderer::clearLayerRegions() {
1154 const size_t count = mLayers.size();
1155 if (count == 0) return;
1156
1157 if (!mSnapshot->isIgnored()) {
1158 // Doing several glScissor/glClear here can negatively impact
1159 // GPUs with a tiler architecture, instead we draw quads with
1160 // the Clear blending mode
1161
1162 // The list contains bounds that have already been clipped
1163 // against their initial clip rect, and the current clip
1164 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001165 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001166
1167 Vertex mesh[count * 6];
1168 Vertex* vertex = mesh;
1169
1170 for (uint32_t i = 0; i < count; i++) {
1171 Rect* bounds = mLayers.itemAt(i);
1172
1173 Vertex::set(vertex++, bounds->left, bounds->bottom);
1174 Vertex::set(vertex++, bounds->left, bounds->top);
1175 Vertex::set(vertex++, bounds->right, bounds->top);
1176 Vertex::set(vertex++, bounds->left, bounds->bottom);
1177 Vertex::set(vertex++, bounds->right, bounds->top);
1178 Vertex::set(vertex++, bounds->right, bounds->bottom);
1179
1180 delete bounds;
1181 }
1182
1183 setupDraw(false);
1184 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1185 setupDrawBlending(true, SkXfermode::kClear_Mode);
1186 setupDrawProgram();
1187 setupDrawPureColorUniforms();
1188 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001189 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001190
Romain Guy54be1cd2011-06-13 19:04:27 -07001191 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001192
1193 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001194 } else {
1195 for (uint32_t i = 0; i < count; i++) {
1196 delete mLayers.itemAt(i);
1197 }
1198 }
1199
1200 mLayers.clear();
1201}
1202
Romain Guybd6b79b2010-06-26 00:13:53 -07001203///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001204// Transforms
1205///////////////////////////////////////////////////////////////////////////////
1206
1207void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001208 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001209}
1210
1211void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001212 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001213}
1214
1215void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001216 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001217}
1218
Romain Guy807daf72011-01-18 11:19:19 -08001219void OpenGLRenderer::skew(float sx, float sy) {
1220 mSnapshot->transform->skew(sx, sy);
1221}
1222
Romain Guyf6a11b82010-06-23 17:47:49 -07001223void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001224 if (matrix) {
1225 mSnapshot->transform->load(*matrix);
1226 } else {
1227 mSnapshot->transform->loadIdentity();
1228 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001229}
1230
1231void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001232 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001233}
1234
1235void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001236 SkMatrix transform;
1237 mSnapshot->transform->copyTo(transform);
1238 transform.preConcat(*matrix);
1239 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001240}
1241
1242///////////////////////////////////////////////////////////////////////////////
1243// Clipping
1244///////////////////////////////////////////////////////////////////////////////
1245
Romain Guybb9524b2010-06-22 18:56:38 -07001246void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001247 Rect clip(*mSnapshot->clipRect);
1248 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001249
Romain Guy8a4ac612012-07-17 17:32:48 -07001250 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1251 clip.getWidth(), clip.getHeight())) {
1252 mDirtyClip = false;
1253 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001254}
1255
Romain Guy8ce00302013-01-15 18:51:42 -08001256void OpenGLRenderer::ensureStencilBuffer() {
1257 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1258 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1259 // just hope we have one when hasLayer() returns false.
1260 if (hasLayer()) {
1261 attachStencilBufferToLayer(mSnapshot->layer);
1262 }
1263}
1264
1265void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1266 // The layer's FBO is already bound when we reach this stage
1267 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001268 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1269 // is attached after we initiated tiling. We must turn it off,
1270 // attach the new render buffer then turn tiling back on
1271 endTiling();
1272
Romain Guy3bbacf22013-02-06 16:51:04 -08001273 RenderBuffer* buffer = new RenderBuffer(
1274 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
1275 buffer->bind();
1276 buffer->allocate();
Romain Guy2055aba2013-01-18 16:42:51 -08001277
Romain Guy8ce00302013-01-15 18:51:42 -08001278 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001279
Romain Guyf735c8e2013-01-31 17:45:55 -08001280 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001281 }
1282}
1283
1284void OpenGLRenderer::setStencilFromClip() {
1285 if (!mCaches.debugOverdraw) {
1286 if (!mSnapshot->clipRegion->isEmpty()) {
1287 // NOTE: The order here is important, we must set dirtyClip to false
1288 // before any draw call to avoid calling back into this method
1289 mDirtyClip = false;
1290
1291 ensureStencilBuffer();
1292
1293 mCaches.stencil.enableWrite();
1294
1295 // Clear the stencil but first make sure we restrict drawing
1296 // to the region's bounds
1297 bool resetScissor = mCaches.enableScissor();
1298 if (resetScissor) {
1299 // The scissor was not set so we now need to update it
1300 setScissorFromClip();
1301 }
1302 mCaches.stencil.clear();
1303 if (resetScissor) mCaches.disableScissor();
1304
1305 // NOTE: We could use the region contour path to generate a smaller mesh
1306 // Since we are using the stencil we could use the red book path
1307 // drawing technique. It might increase bandwidth usage though.
1308
1309 // The last parameter is important: we are not drawing in the color buffer
1310 // so we don't want to dirty the current layer, if any
1311 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1312
1313 mCaches.stencil.enableTest();
1314 } else {
1315 mCaches.stencil.disable();
1316 }
1317 }
1318}
1319
Romain Guy9d5316e2010-06-24 19:30:36 -07001320const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001321 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001322}
1323
Romain Guy8a4ac612012-07-17 17:32:48 -07001324bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1325 if (mSnapshot->isIgnored()) {
1326 return true;
1327 }
1328
1329 Rect r(left, top, right, bottom);
1330 mSnapshot->transform->mapRect(r);
1331 r.snapToPixelBoundaries();
1332
1333 Rect clipRect(*mSnapshot->clipRect);
1334 clipRect.snapToPixelBoundaries();
1335
1336 return !clipRect.intersects(r);
1337}
1338
Romain Guy35643dd2012-09-18 15:40:58 -07001339bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1340 Rect& transformed, Rect& clip) {
1341 if (mSnapshot->isIgnored()) {
1342 return true;
1343 }
1344
1345 transformed.set(left, top, right, bottom);
1346 mSnapshot->transform->mapRect(transformed);
1347 transformed.snapToPixelBoundaries();
1348
1349 clip.set(*mSnapshot->clipRect);
1350 clip.snapToPixelBoundaries();
1351
1352 return !clip.intersects(transformed);
1353}
1354
Romain Guy672433d2013-01-04 19:05:13 -08001355bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1356 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001357 if (paint->getStyle() != SkPaint::kFill_Style) {
1358 float outset = paint->getStrokeWidth() * 0.5f;
1359 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1360 } else {
1361 return quickReject(left, top, right, bottom);
1362 }
1363}
1364
Romain Guyc7d53492010-06-25 13:41:57 -07001365bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001366 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001367 return true;
1368 }
1369
Romain Guy1d83e192010-08-17 11:37:00 -07001370 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001371 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001372 r.snapToPixelBoundaries();
1373
1374 Rect clipRect(*mSnapshot->clipRect);
1375 clipRect.snapToPixelBoundaries();
1376
Romain Guy586cae32012-07-13 15:28:31 -07001377 bool rejected = !clipRect.intersects(r);
1378 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001379 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001380 }
1381
1382 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001383}
1384
Romain Guy8ce00302013-01-15 18:51:42 -08001385void OpenGLRenderer::debugClip() {
1386#if DEBUG_CLIP_REGIONS
1387 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1388 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1389 }
1390#endif
1391}
1392
Romain Guy079ba2c2010-07-16 14:12:24 -07001393bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001394 if (CC_LIKELY(mSnapshot->transform->rectToRect())) {
1395 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1396 if (clipped) {
1397 dirtyClip();
1398 }
1399 return !mSnapshot->clipRect->isEmpty();
1400 }
1401
1402 SkPath path;
1403 path.addRect(left, top, right, bottom);
1404
1405 return clipPath(&path, op);
1406}
1407
1408bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1409 SkMatrix transform;
1410 mSnapshot->transform->copyTo(transform);
1411
1412 SkPath transformed;
1413 path->transform(transform, &transformed);
1414
1415 SkRegion clip;
1416 if (!mSnapshot->clipRegion->isEmpty()) {
1417 clip.setRegion(*mSnapshot->clipRegion);
1418 } else {
1419 Rect* bounds = mSnapshot->clipRect;
1420 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1421 }
1422
1423 SkRegion region;
1424 region.setPath(transformed, clip);
1425
1426 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001427 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001428 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001429 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001430 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001431}
1432
Romain Guy735738c2012-12-03 12:34:51 -08001433bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001434 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1435 if (clipped) {
1436 dirtyClip();
1437 }
1438 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001439}
1440
Chet Haasea23eed82012-04-12 15:19:04 -07001441Rect* OpenGLRenderer::getClipRect() {
1442 return mSnapshot->clipRect;
1443}
1444
Romain Guyf6a11b82010-06-23 17:47:49 -07001445///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001446// Drawing commands
1447///////////////////////////////////////////////////////////////////////////////
1448
Romain Guy54be1cd2011-06-13 19:04:27 -07001449void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001450 // TODO: It would be best if we could do this before quickReject()
1451 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001452 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001453 // Make sure setScissor & setStencil happen at the beginning of
1454 // this method
Romain Guy70ca14e2010-12-13 18:24:33 -08001455 if (mDirtyClip) {
1456 setScissorFromClip();
Romain Guy8ce00302013-01-15 18:51:42 -08001457 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001458 }
1459 mDescription.reset();
1460 mSetShaderColor = false;
1461 mColorSet = false;
1462 mColorA = mColorR = mColorG = mColorB = 0.0f;
1463 mTextureUnit = 0;
1464 mTrackDirtyRegions = true;
1465}
1466
1467void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1468 mDescription.hasTexture = true;
1469 mDescription.hasAlpha8Texture = isAlpha8;
1470}
1471
Romain Guyaa6c24c2011-04-28 18:40:04 -07001472void OpenGLRenderer::setupDrawWithExternalTexture() {
1473 mDescription.hasExternalTexture = true;
1474}
1475
Romain Guy15bc6432011-12-13 13:11:32 -08001476void OpenGLRenderer::setupDrawNoTexture() {
1477 mCaches.disbaleTexCoordsVertexArray();
1478}
1479
Chris Craik710f46d2012-09-17 17:25:49 -07001480void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001481 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001482}
1483
Romain Guyed6fcb02011-03-21 13:11:28 -07001484void OpenGLRenderer::setupDrawPoint(float pointSize) {
1485 mDescription.isPoint = true;
1486 mDescription.pointSize = pointSize;
1487}
1488
Romain Guy8d0d4782010-12-14 20:13:35 -08001489void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1490 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001491 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1492 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1493 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001494 mColorSet = true;
1495 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1496}
1497
Romain Guy86568192010-12-14 15:55:39 -08001498void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1499 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001500 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1501 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1502 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001503 mColorSet = true;
1504 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1505}
1506
Romain Guy41210632012-07-16 17:04:24 -07001507void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1508 mCaches.fontRenderer->describe(mDescription, paint);
1509}
1510
Romain Guy70ca14e2010-12-13 18:24:33 -08001511void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1512 mColorA = a;
1513 mColorR = r;
1514 mColorG = g;
1515 mColorB = b;
1516 mColorSet = true;
1517 mSetShaderColor = mDescription.setColor(r, g, b, a);
1518}
1519
1520void OpenGLRenderer::setupDrawShader() {
1521 if (mShader) {
Romain Guy3bbacf22013-02-06 16:51:04 -08001522 mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001523 }
1524}
1525
1526void OpenGLRenderer::setupDrawColorFilter() {
1527 if (mColorFilter) {
Romain Guy3bbacf22013-02-06 16:51:04 -08001528 mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001529 }
1530}
1531
Romain Guyf09ef512011-05-27 11:43:46 -07001532void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1533 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1534 mColorA = 1.0f;
1535 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001536 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001537 }
1538}
1539
Romain Guy70ca14e2010-12-13 18:24:33 -08001540void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001541 // When the blending mode is kClear_Mode, we need to use a modulate color
1542 // argb=1,0,0,0
1543 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001544 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1545 mDescription, swapSrcDst);
1546}
1547
1548void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001549 // When the blending mode is kClear_Mode, we need to use a modulate color
1550 // argb=1,0,0,0
1551 accountForClear(mode);
Romain Guye83221c2012-09-24 16:01:35 -07001552 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()) ||
1553 (mColorFilter && mColorFilter->blend()), mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001554}
1555
1556void OpenGLRenderer::setupDrawProgram() {
1557 useProgram(mCaches.programCache.get(mDescription));
1558}
1559
1560void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1561 mTrackDirtyRegions = false;
1562}
1563
1564void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1565 bool ignoreTransform) {
1566 mModelView.loadTranslate(left, top, 0.0f);
1567 if (!ignoreTransform) {
1568 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1569 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1570 } else {
1571 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1572 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1573 }
1574}
1575
Chet Haase8a5cc922011-04-26 07:28:09 -07001576void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1577 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001578}
1579
Romain Guy70ca14e2010-12-13 18:24:33 -08001580void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1581 bool ignoreTransform, bool ignoreModelView) {
1582 if (!ignoreModelView) {
1583 mModelView.loadTranslate(left, top, 0.0f);
1584 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001585 } else {
1586 mModelView.loadIdentity();
1587 }
Romain Guy86568192010-12-14 15:55:39 -08001588 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1589 if (!ignoreTransform) {
1590 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1591 if (mTrackDirtyRegions && dirty) {
1592 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1593 }
1594 } else {
1595 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1596 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1597 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001598}
1599
Romain Guyed6fcb02011-03-21 13:11:28 -07001600void OpenGLRenderer::setupDrawPointUniforms() {
1601 int slot = mCaches.currentProgram->getUniform("pointSize");
1602 glUniform1f(slot, mDescription.pointSize);
1603}
1604
Romain Guy70ca14e2010-12-13 18:24:33 -08001605void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001606 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001607 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1608 }
1609}
1610
Romain Guy86568192010-12-14 15:55:39 -08001611void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001612 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001613 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001614 }
1615}
1616
Romain Guy70ca14e2010-12-13 18:24:33 -08001617void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1618 if (mShader) {
1619 if (ignoreTransform) {
1620 mModelView.loadInverse(*mSnapshot->transform);
1621 }
1622 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1623 }
1624}
1625
Romain Guy8d0d4782010-12-14 20:13:35 -08001626void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1627 if (mShader) {
1628 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1629 }
1630}
1631
Romain Guy70ca14e2010-12-13 18:24:33 -08001632void OpenGLRenderer::setupDrawColorFilterUniforms() {
1633 if (mColorFilter) {
1634 mColorFilter->setupProgram(mCaches.currentProgram);
1635 }
1636}
1637
Romain Guy41210632012-07-16 17:04:24 -07001638void OpenGLRenderer::setupDrawTextGammaUniforms() {
1639 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1640}
1641
Romain Guy70ca14e2010-12-13 18:24:33 -08001642void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001643 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001644 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001645 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001646}
1647
1648void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1649 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001650 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001651 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001652}
1653
Romain Guyaa6c24c2011-04-28 18:40:04 -07001654void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1655 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001656 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001657 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001658}
1659
Romain Guy8f0095c2011-05-02 17:24:22 -07001660void OpenGLRenderer::setupDrawTextureTransform() {
1661 mDescription.hasTextureTransform = true;
1662}
1663
1664void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001665 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1666 GL_FALSE, &transform.data[0]);
1667}
1668
Romain Guy70ca14e2010-12-13 18:24:33 -08001669void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001670 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001671 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001672 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001673 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001674 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001675 }
Romain Guyd71dd362011-12-12 19:03:35 -08001676
Chris Craikcb4d6002012-09-25 12:00:29 -07001677 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001678 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001679 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001680 }
1681
1682 mCaches.unbindIndicesBuffer();
1683}
1684
1685void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1686 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001687 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001688 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001689 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001690 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001691}
1692
Chet Haase5b0200b2011-04-13 17:58:08 -07001693void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001694 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001695 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001696 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001697}
1698
Romain Guy70ca14e2010-12-13 18:24:33 -08001699void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001700}
1701
1702///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001703// Drawing
1704///////////////////////////////////////////////////////////////////////////////
1705
Chet Haase1271e2c2012-04-20 09:54:27 -07001706status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001707 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001708
Romain Guy0fe478e2010-11-08 12:08:41 -08001709 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1710 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001711 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001712 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001713 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001714
Romain Guy65549432012-03-26 16:45:05 -07001715 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001716}
1717
Chet Haaseed30fd82011-04-22 16:18:45 -07001718void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1719 if (displayList) {
Chris Craik2af46352012-11-26 18:30:17 -08001720 displayList->output(level);
Chet Haaseed30fd82011-04-22 16:18:45 -07001721 }
1722}
1723
Romain Guya168d732011-03-18 16:50:13 -07001724void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1725 int alpha;
1726 SkXfermode::Mode mode;
1727 getAlphaAndMode(paint, &alpha, &mode);
1728
Romain Guy886b2752013-01-04 12:26:18 -08001729 int color = paint != NULL ? paint->getColor() : 0;
1730
Romain Guya168d732011-03-18 16:50:13 -07001731 float x = left;
1732 float y = top;
1733
Romain Guy886b2752013-01-04 12:26:18 -08001734 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1735
Romain Guya168d732011-03-18 16:50:13 -07001736 bool ignoreTransform = false;
1737 if (mSnapshot->transform->isPureTranslate()) {
1738 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1739 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1740 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001741
1742 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001743 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001744 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001745 }
1746
Romain Guy886b2752013-01-04 12:26:18 -08001747 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1748 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1749 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001750}
1751
Chet Haase48659092012-05-31 15:21:51 -07001752status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001753 const float right = left + bitmap->width();
1754 const float bottom = top + bitmap->height();
1755
1756 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001757 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001758 }
1759
Romain Guya1d3c912011-12-13 14:55:06 -08001760 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001761 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001762 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001763 const AutoTexture autoCleanup(texture);
1764
Romain Guy211370f2012-02-01 16:10:55 -08001765 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001766 drawAlphaBitmap(texture, left, top, paint);
1767 } else {
1768 drawTextureRect(left, top, right, bottom, texture, paint);
1769 }
Chet Haase48659092012-05-31 15:21:51 -07001770
1771 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001772}
1773
Chet Haase48659092012-05-31 15:21:51 -07001774status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001775 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1776 const mat4 transform(*matrix);
1777 transform.mapRect(r);
1778
Romain Guy6926c722010-07-12 20:20:03 -07001779 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001780 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001781 }
1782
Romain Guya1d3c912011-12-13 14:55:06 -08001783 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001784 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001785 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001786 const AutoTexture autoCleanup(texture);
1787
Romain Guy5b3b3522010-10-27 18:57:51 -07001788 // This could be done in a cheaper way, all we need is pass the matrix
1789 // to the vertex shader. The save/restore is a bit overkill.
1790 save(SkCanvas::kMatrix_SaveFlag);
1791 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08001792 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1793 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
1794 } else {
1795 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1796 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001797 restore();
Chet Haase48659092012-05-31 15:21:51 -07001798
1799 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001800}
1801
Chet Haase48659092012-05-31 15:21:51 -07001802status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001803 const float right = left + bitmap->width();
1804 const float bottom = top + bitmap->height();
1805
1806 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001807 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001808 }
1809
1810 mCaches.activeTexture(0);
1811 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1812 const AutoTexture autoCleanup(texture);
1813
Romain Guy886b2752013-01-04 12:26:18 -08001814 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1815 drawAlphaBitmap(texture, left, top, paint);
1816 } else {
1817 drawTextureRect(left, top, right, bottom, texture, paint);
1818 }
Chet Haase48659092012-05-31 15:21:51 -07001819
1820 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001821}
1822
Chet Haase48659092012-05-31 15:21:51 -07001823status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001824 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001825 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001826 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001827 }
1828
Romain Guyb18d2d02011-02-10 15:52:54 -08001829 float left = FLT_MAX;
1830 float top = FLT_MAX;
1831 float right = FLT_MIN;
1832 float bottom = FLT_MIN;
1833
Romain Guya92bb4d2012-10-16 11:08:44 -07001834 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001835
Romain Guya566b7c2011-01-23 16:36:11 -08001836 // TODO: Support the colors array
1837 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001838 TextureVertex* vertex = mesh;
Romain Guya92bb4d2012-10-16 11:08:44 -07001839
Romain Guy5a7b4662011-01-20 19:09:30 -08001840 for (int32_t y = 0; y < meshHeight; y++) {
1841 for (int32_t x = 0; x < meshWidth; x++) {
1842 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1843
1844 float u1 = float(x) / meshWidth;
1845 float u2 = float(x + 1) / meshWidth;
1846 float v1 = float(y) / meshHeight;
1847 float v2 = float(y + 1) / meshHeight;
1848
1849 int ax = i + (meshWidth + 1) * 2;
1850 int ay = ax + 1;
1851 int bx = i;
1852 int by = bx + 1;
1853 int cx = i + 2;
1854 int cy = cx + 1;
1855 int dx = i + (meshWidth + 1) * 2 + 2;
1856 int dy = dx + 1;
1857
1858 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1859 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1860 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1861
1862 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1863 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1864 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001865
Romain Guya92bb4d2012-10-16 11:08:44 -07001866 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1867 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1868 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1869 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001870 }
1871 }
1872
Romain Guya92bb4d2012-10-16 11:08:44 -07001873 if (quickReject(left, top, right, bottom)) {
1874 return DrawGlInfo::kStatusDone;
1875 }
1876
1877 mCaches.activeTexture(0);
1878 Texture* texture = mCaches.textureCache.get(bitmap);
1879 if (!texture) return DrawGlInfo::kStatusDone;
1880 const AutoTexture autoCleanup(texture);
1881
1882 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1883 texture->setFilter(FILTER(paint), true);
1884
1885 int alpha;
1886 SkXfermode::Mode mode;
1887 getAlphaAndMode(paint, &alpha, &mode);
1888
1889 if (hasLayer()) {
Romain Guyb18d2d02011-02-10 15:52:54 -08001890 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1891 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001892
Romain Guy5a7b4662011-01-20 19:09:30 -08001893 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1894 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001895 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001896
1897 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001898}
1899
Chet Haase48659092012-05-31 15:21:51 -07001900status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001901 float srcLeft, float srcTop, float srcRight, float srcBottom,
1902 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001903 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001904 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001905 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001906 }
1907
Romain Guya1d3c912011-12-13 14:55:06 -08001908 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001909 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001910 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001911 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001912
Romain Guy8ba548f2010-06-30 19:21:21 -07001913 const float width = texture->width;
1914 const float height = texture->height;
1915
Romain Guy68169722011-08-22 17:33:33 -07001916 const float u1 = fmax(0.0f, srcLeft / width);
1917 const float v1 = fmax(0.0f, srcTop / height);
1918 const float u2 = fmin(1.0f, srcRight / width);
1919 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001920
Romain Guy03750a02010-10-18 14:06:08 -07001921 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001922 resetDrawTextureTexCoords(u1, v1, u2, v2);
1923
Romain Guy03750a02010-10-18 14:06:08 -07001924 int alpha;
1925 SkXfermode::Mode mode;
1926 getAlphaAndMode(paint, &alpha, &mode);
1927
Romain Guyd21b6e12011-11-30 20:21:23 -08001928 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1929
Romain Guy886b2752013-01-04 12:26:18 -08001930 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
1931 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08001932
Romain Guy886b2752013-01-04 12:26:18 -08001933 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
1934 // Apply a scale transform on the canvas only when a shader is in use
1935 // Skia handles the ratio between the dst and src rects as a scale factor
1936 // when a shader is set
1937 bool useScaleTransform = mShader && scaled;
1938 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07001939
Romain Guy886b2752013-01-04 12:26:18 -08001940 if (CC_LIKELY(mSnapshot->transform->isPureTranslate() && !useScaleTransform)) {
1941 float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1942 float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1943
1944 dstRight = x + (dstRight - dstLeft);
1945 dstBottom = y + (dstBottom - dstTop);
1946
1947 dstLeft = x;
1948 dstTop = y;
1949
1950 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
1951 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08001952 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001953 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08001954 }
1955
1956 if (CC_UNLIKELY(useScaleTransform)) {
1957 save(SkCanvas::kMatrix_SaveFlag);
1958 translate(dstLeft, dstTop);
1959 scale(scaleX, scaleY);
1960
1961 dstLeft = 0.0f;
1962 dstTop = 0.0f;
1963
1964 dstRight = srcRight - srcLeft;
1965 dstBottom = srcBottom - srcTop;
1966 }
1967
1968 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1969 int color = paint ? paint->getColor() : 0;
1970 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
1971 texture->id, paint != NULL, color, alpha, mode,
1972 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1973 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
1974 } else {
1975 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
1976 texture->id, alpha / 255.0f, mode, texture->blend,
1977 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1978 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
1979 }
1980
1981 if (CC_UNLIKELY(useScaleTransform)) {
1982 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08001983 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001984
1985 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001986
1987 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001988}
1989
Chet Haase48659092012-05-31 15:21:51 -07001990status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001991 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001992 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001993 int alpha;
1994 SkXfermode::Mode mode;
1995 getAlphaAndModeDirect(paint, &alpha, &mode);
1996
1997 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1998 left, top, right, bottom, alpha, mode);
1999}
2000
2001status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2002 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2003 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002004 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002005 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002006 }
2007
Romain Guybe6f9dc2012-07-16 12:41:17 -07002008 alpha *= mSnapshot->alpha;
2009
Romain Guya1d3c912011-12-13 14:55:06 -08002010 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002011 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002012 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002013 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08002014 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2015 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07002016
Romain Guy2728f962010-10-08 18:36:15 -07002017 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002018 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002019
Romain Guy211370f2012-02-01 16:10:55 -08002020 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002021 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002022 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002023 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002024 const float offsetX = left + mSnapshot->transform->getTranslateX();
2025 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002026 const size_t count = mesh->quads.size();
2027 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002028 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002029 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002030 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2031 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2032 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002033 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002034 dirtyLayer(left + bounds.left, top + bounds.top,
2035 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08002036 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002037 }
2038 }
2039
Romain Guy211370f2012-02-01 16:10:55 -08002040 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002041 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2042 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2043
2044 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2045 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2046 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2047 true, !mesh->hasEmptyQuads);
2048 } else {
2049 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2050 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2051 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2052 true, !mesh->hasEmptyQuads);
2053 }
Romain Guy054dc182010-10-15 17:55:25 -07002054 }
Chet Haase48659092012-05-31 15:21:51 -07002055
2056 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002057}
2058
Chris Craik65cd6122012-12-10 17:56:27 -08002059status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2060 bool useOffset) {
2061 if (!vertexBuffer.getSize()) {
2062 // no vertices to draw
2063 return DrawGlInfo::kStatusDone;
2064 }
2065
Chris Craikcb4d6002012-09-25 12:00:29 -07002066 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002067 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2068 bool isAA = paint->isAntiAlias();
2069
Chris Craik710f46d2012-09-17 17:25:49 -07002070 setupDraw();
2071 setupDrawNoTexture();
2072 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002073 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2074 setupDrawColorFilter();
2075 setupDrawShader();
2076 setupDrawBlending(isAA, mode);
2077 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002078 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002079 setupDrawColorUniforms();
2080 setupDrawColorFilterUniforms();
2081 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002082
Chris Craik710f46d2012-09-17 17:25:49 -07002083 void* vertices = vertexBuffer.getBuffer();
2084 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002085 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002086 mCaches.resetTexCoordsVertexPointer();
2087 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002088
Chris Craik710f46d2012-09-17 17:25:49 -07002089 int alphaSlot = -1;
2090 if (isAA) {
2091 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2092 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002093
Chris Craik710f46d2012-09-17 17:25:49 -07002094 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002095 glEnableVertexAttribArray(alphaSlot);
2096 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002097 }
Romain Guy04299382012-07-18 17:15:41 -07002098
Chris Craik710f46d2012-09-17 17:25:49 -07002099 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002100
Chris Craik710f46d2012-09-17 17:25:49 -07002101 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002102 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002103 }
Chris Craik65cd6122012-12-10 17:56:27 -08002104
2105 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002106}
2107
2108/**
Chris Craik65cd6122012-12-10 17:56:27 -08002109 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2110 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2111 * screen space in all directions. However, instead of using a fragment shader to compute the
2112 * translucency of the color from its position, we simply use a varying parameter to define how far
2113 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2114 *
2115 * Doesn't yet support joins, caps, or path effects.
2116 */
2117status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2118 VertexBuffer vertexBuffer;
2119 // TODO: try clipping large paths to viewport
2120 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2121
2122 SkRect bounds = path.getBounds();
2123 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2124 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
2125
2126 return drawVertexBuffer(vertexBuffer, paint);
2127}
2128
2129/**
2130 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2131 * and additional geometry for defining an alpha slope perimeter.
2132 *
2133 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2134 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2135 * in-shader alpha region, but found it to be taxing on some GPUs.
2136 *
2137 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2138 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002139 */
Chet Haase48659092012-05-31 15:21:51 -07002140status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002141 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002142
Chris Craik65cd6122012-12-10 17:56:27 -08002143 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002144
Chris Craik65cd6122012-12-10 17:56:27 -08002145 VertexBuffer buffer;
2146 SkRect bounds;
2147 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002148
2149 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2150 return DrawGlInfo::kStatusDone;
2151 }
2152
Chris Craik65cd6122012-12-10 17:56:27 -08002153 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy7b631422012-04-04 11:38:54 -07002154
Chris Craik65cd6122012-12-10 17:56:27 -08002155 bool useOffset = !paint->isAntiAlias();
2156 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002157}
2158
Chet Haase48659092012-05-31 15:21:51 -07002159status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2160 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002161
2162 // TODO: The paint's cap style defines whether the points are square or circular
2163 // TODO: Handle AA for round points
2164
Chet Haase5b0200b2011-04-13 17:58:08 -07002165 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002166 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002167 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002168 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002169 if (isHairLine) {
2170 // Now that we know it's hairline, we can set the effective width, to be used later
2171 strokeWidth = 1.0f;
2172 }
2173 const float halfWidth = strokeWidth / 2;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002174
Romain Guyed6fcb02011-03-21 13:11:28 -07002175 int alpha;
2176 SkXfermode::Mode mode;
2177 getAlphaAndMode(paint, &alpha, &mode);
2178
2179 int verticesCount = count >> 1;
2180 int generatedVerticesCount = 0;
2181
2182 TextureVertex pointsData[verticesCount];
2183 TextureVertex* vertex = &pointsData[0];
2184
Romain Guy04299382012-07-18 17:15:41 -07002185 // TODO: We should optimize this method to not generate vertices for points
2186 // that lie outside of the clip.
2187 mCaches.enableScissor();
2188
Romain Guyed6fcb02011-03-21 13:11:28 -07002189 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002190 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002191 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002192 setupDrawColor(paint->getColor(), alpha);
2193 setupDrawColorFilter();
2194 setupDrawShader();
2195 setupDrawBlending(mode);
2196 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002197 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002198 setupDrawColorUniforms();
2199 setupDrawColorFilterUniforms();
2200 setupDrawPointUniforms();
2201 setupDrawShaderIdentityUniforms();
2202 setupDrawMesh(vertex);
2203
2204 for (int i = 0; i < count; i += 2) {
2205 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2206 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002207
Chet Haase8a5cc922011-04-26 07:28:09 -07002208 float left = points[i] - halfWidth;
2209 float right = points[i] + halfWidth;
2210 float top = points[i + 1] - halfWidth;
2211 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002212
Chet Haase8a5cc922011-04-26 07:28:09 -07002213 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002214 }
2215
2216 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002217
2218 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002219}
2220
Chet Haase48659092012-05-31 15:21:51 -07002221status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002222 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002223 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002224
Romain Guyae88e5e2010-10-22 17:49:18 -07002225 Rect& clip(*mSnapshot->clipRect);
2226 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002227
Romain Guy3d58c032010-07-14 16:34:53 -07002228 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002229
2230 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002231}
Romain Guy9d5316e2010-06-24 19:30:36 -07002232
Chet Haase48659092012-05-31 15:21:51 -07002233status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2234 SkPaint* paint) {
2235 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002236 const AutoTexture autoCleanup(texture);
2237
2238 const float x = left + texture->left - texture->offset;
2239 const float y = top + texture->top - texture->offset;
2240
2241 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002242
2243 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002244}
2245
Chet Haase48659092012-05-31 15:21:51 -07002246status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002247 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002248 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002249 return DrawGlInfo::kStatusDone;
2250 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002251
Chris Craikcb4d6002012-09-25 12:00:29 -07002252 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002253 mCaches.activeTexture(0);
2254 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2255 right - left, bottom - top, rx, ry, p);
2256 return drawShape(left, top, texture, p);
2257 }
2258
2259 SkPath path;
2260 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002261 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2262 float outset = p->getStrokeWidth() / 2;
2263 rect.outset(outset, outset);
2264 rx += outset;
2265 ry += outset;
2266 }
Chris Craik710f46d2012-09-17 17:25:49 -07002267 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002268 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002269}
2270
Chris Craik710f46d2012-09-17 17:25:49 -07002271status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002272 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2273 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002274 return DrawGlInfo::kStatusDone;
2275 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002276 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002277 mCaches.activeTexture(0);
2278 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2279 return drawShape(x - radius, y - radius, texture, p);
2280 }
2281
2282 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002283 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2284 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2285 } else {
2286 path.addCircle(x, y, radius);
2287 }
Chris Craik65cd6122012-12-10 17:56:27 -08002288 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002289}
Romain Guy01d58e42011-01-19 21:54:02 -08002290
Chet Haase48659092012-05-31 15:21:51 -07002291status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002292 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002293 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002294 return DrawGlInfo::kStatusDone;
2295 }
Romain Guy01d58e42011-01-19 21:54:02 -08002296
Chris Craikcb4d6002012-09-25 12:00:29 -07002297 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002298 mCaches.activeTexture(0);
2299 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2300 return drawShape(left, top, texture, p);
2301 }
2302
2303 SkPath path;
2304 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002305 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2306 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2307 }
Chris Craik710f46d2012-09-17 17:25:49 -07002308 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002309 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002310}
2311
Chet Haase48659092012-05-31 15:21:51 -07002312status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002313 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
2314 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
2315 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002316 }
2317
Chris Craik780c1282012-10-04 14:10:49 -07002318 if (fabs(sweepAngle) >= 360.0f) {
2319 return drawOval(left, top, right, bottom, p);
2320 }
2321
2322 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002323 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002324 mCaches.activeTexture(0);
2325 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2326 startAngle, sweepAngle, useCenter, p);
2327 return drawShape(left, top, texture, p);
2328 }
2329
2330 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2331 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2332 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2333 }
2334
2335 SkPath path;
2336 if (useCenter) {
2337 path.moveTo(rect.centerX(), rect.centerY());
2338 }
2339 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2340 if (useCenter) {
2341 path.close();
2342 }
Chris Craik65cd6122012-12-10 17:56:27 -08002343 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002344}
2345
Romain Guycf8675e2012-10-02 12:32:25 -07002346// See SkPaintDefaults.h
2347#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2348
Chet Haase48659092012-05-31 15:21:51 -07002349status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002350 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002351 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002352 }
2353
Chris Craik710f46d2012-09-17 17:25:49 -07002354 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002355 // only fill style is supported by drawConvexPath, since others have to handle joins
2356 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2357 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2358 mCaches.activeTexture(0);
2359 const PathTexture* texture =
2360 mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2361 return drawShape(left, top, texture, p);
2362 }
2363
2364 SkPath path;
2365 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2366 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2367 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2368 }
2369 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002370 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002371 }
2372
Romain Guy181d0a62011-06-09 18:52:38 -07002373 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002374 SkPath path;
2375 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002376 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002377 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002378 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002379 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002380 }
Romain Guyc7d53492010-06-25 13:41:57 -07002381}
Romain Guy9d5316e2010-06-24 19:30:36 -07002382
Raph Levien416a8472012-07-19 22:48:17 -07002383void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2384 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2385 float x, float y) {
2386 mCaches.activeTexture(0);
2387
2388 // NOTE: The drop shadow will not perform gamma correction
2389 // if shader-based correction is enabled
2390 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2391 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2392 paint, text, bytesCount, count, mShadowRadius, positions);
2393 const AutoTexture autoCleanup(shadow);
2394
2395 const float sx = x - shadow->left + mShadowDx;
2396 const float sy = y - shadow->top + mShadowDy;
2397
2398 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2399 int shadowColor = mShadowColor;
2400 if (mShader) {
2401 shadowColor = 0xffffffff;
2402 }
2403
2404 setupDraw();
2405 setupDrawWithTexture(true);
2406 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2407 setupDrawColorFilter();
2408 setupDrawShader();
2409 setupDrawBlending(true, mode);
2410 setupDrawProgram();
2411 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2412 setupDrawTexture(shadow->id);
2413 setupDrawPureColorUniforms();
2414 setupDrawColorFilterUniforms();
2415 setupDrawShaderUniforms();
2416 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2417
2418 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2419}
2420
Chet Haase48659092012-05-31 15:21:51 -07002421status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002422 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002423 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002424 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002425 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002426 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002427
Romain Guy671d6cf2012-01-18 12:39:17 -08002428 // NOTE: Skia does not support perspective transform on drawPosText yet
2429 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002430 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002431 }
2432
2433 float x = 0.0f;
2434 float y = 0.0f;
2435 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2436 if (pureTranslate) {
2437 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2438 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2439 }
2440
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002441 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002442 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy671d6cf2012-01-18 12:39:17 -08002443
2444 int alpha;
2445 SkXfermode::Mode mode;
2446 getAlphaAndMode(paint, &alpha, &mode);
2447
Raph Levien416a8472012-07-19 22:48:17 -07002448 if (CC_UNLIKELY(mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002449 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2450 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002451 }
2452
Romain Guy671d6cf2012-01-18 12:39:17 -08002453 // Pick the appropriate texture filtering
2454 bool linearFilter = mSnapshot->transform->changesBounds();
2455 if (pureTranslate && !linearFilter) {
2456 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2457 }
2458
2459 mCaches.activeTexture(0);
2460 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002461 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002462 setupDrawDirtyRegionsDisabled();
2463 setupDrawWithTexture(true);
2464 setupDrawAlpha8Color(paint->getColor(), alpha);
2465 setupDrawColorFilter();
2466 setupDrawShader();
2467 setupDrawBlending(true, mode);
2468 setupDrawProgram();
2469 setupDrawModelView(x, y, x, y, pureTranslate, true);
2470 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2471 setupDrawPureColorUniforms();
2472 setupDrawColorFilterUniforms();
2473 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002474 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002475
2476 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2477 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2478
Romain Guy211370f2012-02-01 16:10:55 -08002479 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002480
2481 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2482 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002483 if (hasActiveLayer) {
2484 if (!pureTranslate) {
2485 mSnapshot->transform->mapRect(bounds);
2486 }
2487 dirtyLayerUnchecked(bounds, getRegion());
2488 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002489 }
Chet Haase48659092012-05-31 15:21:51 -07002490
2491 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002492}
2493
Romain Guyc2525952012-07-27 16:41:22 -07002494status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002495 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002496 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002497 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002498 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002499 }
2500
Chet Haasea1cff502012-02-21 13:43:44 -08002501 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002502 switch (paint->getTextAlign()) {
2503 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002504 x -= length / 2.0f;
2505 break;
2506 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002507 x -= length;
2508 break;
2509 default:
2510 break;
2511 }
2512
Romain Guycac5fd32011-12-01 20:08:50 -08002513 SkPaint::FontMetrics metrics;
2514 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002515 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002516 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002517 }
2518
Romain Guye3a9b242013-01-08 11:15:30 -08002519#if DEBUG_GLYPHS
2520 ALOGD("OpenGLRenderer drawText() with FontID=%d",
2521 SkTypeface::UniqueID(paint->getTypeface()));
2522#endif
2523
2524 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2525 fontRenderer.setFont(paint, *mSnapshot->transform);
2526
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002527 const float oldX = x;
2528 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002529 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002530 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002531 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2532 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2533 }
2534
Romain Guy86568192010-12-14 15:55:39 -08002535 int alpha;
2536 SkXfermode::Mode mode;
2537 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002538
Romain Guy211370f2012-02-01 16:10:55 -08002539 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002540 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2541 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002542 }
2543
Romain Guy6620c6d2010-12-06 18:07:02 -08002544 // Pick the appropriate texture filtering
2545 bool linearFilter = mSnapshot->transform->changesBounds();
2546 if (pureTranslate && !linearFilter) {
2547 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2548 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002549
Romain Guy16c88082012-06-11 16:03:47 -07002550 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002551 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002552 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002553 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002554 setupDrawDirtyRegionsDisabled();
2555 setupDrawWithTexture(true);
2556 setupDrawAlpha8Color(paint->getColor(), alpha);
2557 setupDrawColorFilter();
2558 setupDrawShader();
2559 setupDrawBlending(true, mode);
2560 setupDrawProgram();
2561 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002562 // See comment above; the font renderer must use texture unit 0
2563 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002564 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2565 setupDrawPureColorUniforms();
2566 setupDrawColorFilterUniforms();
2567 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002568 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002569
Romain Guya3dc55f2012-09-28 13:55:44 -07002570 const Rect* clip = pureTranslate ? mSnapshot->clipRect :
2571 (mSnapshot->hasPerspectiveTransform() ? NULL : &mSnapshot->getLocalClip());
Romain Guy5b3b3522010-10-27 18:57:51 -07002572 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2573
Romain Guy211370f2012-02-01 16:10:55 -08002574 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002575
Raph Levien996e57c2012-07-23 15:22:52 -07002576 bool status;
Romain Guya3dc55f2012-09-28 13:55:44 -07002577 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002578 SkPaint paintCopy(*paint);
2579 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2580 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002581 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002582 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002583 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002584 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002585 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002586
2587 if (status && hasActiveLayer) {
2588 if (!pureTranslate) {
2589 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002590 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002591 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002592 }
Romain Guy694b5192010-07-21 21:33:20 -07002593
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002594 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002595
2596 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002597}
2598
Chet Haase48659092012-05-31 15:21:51 -07002599status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002600 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002601 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2602 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002603 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002604 }
2605
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002606 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002607 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy03d58522012-02-24 17:54:07 -08002608
2609 int alpha;
2610 SkXfermode::Mode mode;
2611 getAlphaAndMode(paint, &alpha, &mode);
2612
2613 mCaches.activeTexture(0);
2614 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002615 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002616 setupDrawDirtyRegionsDisabled();
2617 setupDrawWithTexture(true);
2618 setupDrawAlpha8Color(paint->getColor(), alpha);
2619 setupDrawColorFilter();
2620 setupDrawShader();
2621 setupDrawBlending(true, mode);
2622 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002623 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002624 setupDrawTexture(fontRenderer.getTexture(true));
2625 setupDrawPureColorUniforms();
2626 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002627 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002628 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002629
Romain Guy97771732012-02-28 18:17:02 -08002630 const Rect* clip = &mSnapshot->getLocalClip();
2631 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 -08002632
Romain Guy97771732012-02-28 18:17:02 -08002633 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002634
2635 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2636 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002637 if (hasActiveLayer) {
2638 mSnapshot->transform->mapRect(bounds);
2639 dirtyLayerUnchecked(bounds, getRegion());
2640 }
Romain Guy97771732012-02-28 18:17:02 -08002641 }
Chet Haase48659092012-05-31 15:21:51 -07002642
2643 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002644}
2645
Chet Haase48659092012-05-31 15:21:51 -07002646status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2647 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002648
Romain Guya1d3c912011-12-13 14:55:06 -08002649 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002650
Romain Guyfb8b7632010-08-23 21:05:08 -07002651 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002652 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002653 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002654
Romain Guy8b55f372010-08-18 17:10:07 -07002655 const float x = texture->left - texture->offset;
2656 const float y = texture->top - texture->offset;
2657
Romain Guy01d58e42011-01-19 21:54:02 -08002658 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002659
2660 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002661}
2662
Chet Haase48659092012-05-31 15:21:51 -07002663status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002664 if (!layer) {
2665 return DrawGlInfo::kStatusDone;
2666 }
2667
Romain Guyb2e2f242012-10-17 18:18:35 -07002668 mat4* transform = NULL;
2669 if (layer->isTextureLayer()) {
2670 transform = &layer->getTransform();
2671 if (!transform->isIdentity()) {
2672 save(0);
2673 mSnapshot->transform->multiply(*transform);
2674 }
2675 }
2676
Romain Guy35643dd2012-09-18 15:40:58 -07002677 Rect transformed;
2678 Rect clip;
2679 const bool rejected = quickRejectNoScissor(x, y,
2680 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2681
2682 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002683 if (transform && !transform->isIdentity()) {
2684 restore();
2685 }
Chet Haase48659092012-05-31 15:21:51 -07002686 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002687 }
2688
Romain Guy5bb3c732012-11-29 17:52:58 -08002689 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002690
Romain Guy87e2f7572012-09-24 11:37:12 -07002691 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002692 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002693
Romain Guy211370f2012-02-01 16:10:55 -08002694 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guye529ece2012-09-26 11:23:17 -07002695 SkiaColorFilter* oldFilter = mColorFilter;
2696 mColorFilter = layer->getColorFilter();
2697
Romain Guyc88e3572011-01-22 00:32:12 -08002698 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002699 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002700 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002701 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002702 setupDraw();
2703 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002704 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002705 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002706 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002707 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002708 setupDrawPureColorUniforms();
2709 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002710 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002711 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002712 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2713 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002714
Romain Guyd21b6e12011-11-30 20:21:23 -08002715 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002716 setupDrawModelViewTranslate(tx, ty,
2717 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002718 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002719 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002720 setupDrawModelViewTranslate(x, y,
2721 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2722 }
Romain Guyc88e3572011-01-22 00:32:12 -08002723 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002724
Romain Guyc88e3572011-01-22 00:32:12 -08002725 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2726 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002727
Romain Guyc88e3572011-01-22 00:32:12 -08002728 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002729
2730#if DEBUG_LAYERS_AS_REGIONS
2731 drawRegionRects(layer->region);
2732#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002733 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002734
Romain Guye529ece2012-09-26 11:23:17 -07002735 mColorFilter = oldFilter;
2736
Romain Guy5bb3c732012-11-29 17:52:58 -08002737 if (layer->debugDrawUpdate) {
2738 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002739 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2740 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2741 }
Romain Guyf219da52011-01-16 12:54:25 -08002742 }
Chet Haase48659092012-05-31 15:21:51 -07002743
Romain Guyb2e2f242012-10-17 18:18:35 -07002744 if (transform && !transform->isIdentity()) {
2745 restore();
2746 }
2747
Chet Haase48659092012-05-31 15:21:51 -07002748 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002749}
2750
Romain Guy6926c722010-07-12 20:20:03 -07002751///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002752// Shaders
2753///////////////////////////////////////////////////////////////////////////////
2754
2755void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002756 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002757}
2758
Romain Guy06f96e22010-07-30 19:18:16 -07002759void OpenGLRenderer::setupShader(SkiaShader* shader) {
2760 mShader = shader;
2761 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002762 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002763 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002764}
2765
Romain Guyd27977d2010-07-14 19:18:51 -07002766///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002767// Color filters
2768///////////////////////////////////////////////////////////////////////////////
2769
2770void OpenGLRenderer::resetColorFilter() {
2771 mColorFilter = NULL;
2772}
2773
2774void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2775 mColorFilter = filter;
2776}
2777
2778///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002779// Drop shadow
2780///////////////////////////////////////////////////////////////////////////////
2781
2782void OpenGLRenderer::resetShadow() {
2783 mHasShadow = false;
2784}
2785
2786void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2787 mHasShadow = true;
2788 mShadowRadius = radius;
2789 mShadowDx = dx;
2790 mShadowDy = dy;
2791 mShadowColor = color;
2792}
2793
2794///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002795// Draw filters
2796///////////////////////////////////////////////////////////////////////////////
2797
2798void OpenGLRenderer::resetPaintFilter() {
2799 mHasDrawFilter = false;
2800}
2801
2802void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2803 mHasDrawFilter = true;
2804 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2805 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2806}
2807
2808SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002809 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002810
2811 uint32_t flags = paint->getFlags();
2812
2813 mFilteredPaint = *paint;
2814 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2815
2816 return &mFilteredPaint;
2817}
2818
2819///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002820// Drawing implementation
2821///////////////////////////////////////////////////////////////////////////////
2822
Romain Guy01d58e42011-01-19 21:54:02 -08002823void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2824 float x, float y, SkPaint* paint) {
2825 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2826 return;
2827 }
2828
2829 int alpha;
2830 SkXfermode::Mode mode;
2831 getAlphaAndMode(paint, &alpha, &mode);
2832
2833 setupDraw();
2834 setupDrawWithTexture(true);
2835 setupDrawAlpha8Color(paint->getColor(), alpha);
2836 setupDrawColorFilter();
2837 setupDrawShader();
2838 setupDrawBlending(true, mode);
2839 setupDrawProgram();
2840 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2841 setupDrawTexture(texture->id);
2842 setupDrawPureColorUniforms();
2843 setupDrawColorFilterUniforms();
2844 setupDrawShaderUniforms();
2845 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2846
2847 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2848
2849 finishDrawTexture();
2850}
2851
Romain Guyf607bdc2010-09-10 19:20:06 -07002852// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002853#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2854#define kStdUnderline_Offset (1.0f / 9.0f)
2855#define kStdUnderline_Thickness (1.0f / 18.0f)
2856
2857void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2858 float x, float y, SkPaint* paint) {
2859 // Handle underline and strike-through
2860 uint32_t flags = paint->getFlags();
2861 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002862 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002863 float underlineWidth = length;
2864 // If length is > 0.0f, we already measured the text for the text alignment
2865 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002866 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002867 }
2868
Romain Guy211370f2012-02-01 16:10:55 -08002869 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002870 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002871 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002872
Raph Levien8b4072d2012-07-30 15:50:00 -07002873 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002874 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002875
Romain Guyf6834472011-01-23 13:32:12 -08002876 int linesCount = 0;
2877 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2878 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2879
2880 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002881 float points[pointsCount];
2882 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002883
2884 if (flags & SkPaint::kUnderlineText_Flag) {
2885 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002886 points[currentPoint++] = left;
2887 points[currentPoint++] = top;
2888 points[currentPoint++] = left + underlineWidth;
2889 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002890 }
2891
2892 if (flags & SkPaint::kStrikeThruText_Flag) {
2893 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002894 points[currentPoint++] = left;
2895 points[currentPoint++] = top;
2896 points[currentPoint++] = left + underlineWidth;
2897 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002898 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002899
Romain Guy726aeba2011-06-01 14:52:00 -07002900 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002901
Romain Guy726aeba2011-06-01 14:52:00 -07002902 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002903 }
2904 }
2905}
2906
Romain Guy672433d2013-01-04 19:05:13 -08002907status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
2908 if (mSnapshot->isIgnored()) {
2909 return DrawGlInfo::kStatusDone;
2910 }
2911
Romain Guy735738c2012-12-03 12:34:51 -08002912 int color = paint->getColor();
2913 // If a shader is set, preserve only the alpha
2914 if (mShader) {
2915 color |= 0x00ffffff;
2916 }
2917 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2918
2919 return drawColorRects(rects, count, color, mode);
2920}
2921
2922status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08002923 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy735738c2012-12-03 12:34:51 -08002924
Romain Guy672433d2013-01-04 19:05:13 -08002925 float left = FLT_MAX;
2926 float top = FLT_MAX;
2927 float right = FLT_MIN;
2928 float bottom = FLT_MIN;
2929
2930 int vertexCount = 0;
2931 Vertex mesh[count * 6];
2932 Vertex* vertex = mesh;
2933
Chris Craik2af46352012-11-26 18:30:17 -08002934 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08002935 float l = rects[index + 0];
2936 float t = rects[index + 1];
2937 float r = rects[index + 2];
2938 float b = rects[index + 3];
2939
Romain Guy8ce00302013-01-15 18:51:42 -08002940 if (ignoreTransform || !quickRejectNoScissor(left, top, right, bottom)) {
Romain Guy672433d2013-01-04 19:05:13 -08002941 Vertex::set(vertex++, l, b);
2942 Vertex::set(vertex++, l, t);
2943 Vertex::set(vertex++, r, t);
2944 Vertex::set(vertex++, l, b);
2945 Vertex::set(vertex++, r, t);
2946 Vertex::set(vertex++, r, b);
2947
2948 vertexCount += 6;
2949
2950 left = fminf(left, l);
2951 top = fminf(top, t);
2952 right = fmaxf(right, r);
2953 bottom = fmaxf(bottom, b);
2954 }
2955 }
2956
Romain Guy3bbacf22013-02-06 16:51:04 -08002957 if (count == 0 || (clip && quickReject(left, top, right, bottom))) {
Romain Guya362c692013-02-04 13:50:16 -08002958 return DrawGlInfo::kStatusDone;
2959 }
Romain Guy672433d2013-01-04 19:05:13 -08002960
Romain Guy672433d2013-01-04 19:05:13 -08002961 setupDraw();
2962 setupDrawNoTexture();
2963 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2964 setupDrawShader();
2965 setupDrawColorFilter();
2966 setupDrawBlending(mode);
2967 setupDrawProgram();
2968 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08002969 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08002970 setupDrawColorUniforms();
2971 setupDrawShaderUniforms();
2972 setupDrawColorFilterUniforms();
2973 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
2974
Romain Guy8ce00302013-01-15 18:51:42 -08002975 if (dirty && hasLayer()) {
Romain Guy672433d2013-01-04 19:05:13 -08002976 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
2977 }
2978
2979 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
2980
2981 return DrawGlInfo::kStatusDrew;
2982}
2983
Romain Guy026c5e162010-06-28 17:12:22 -07002984void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002985 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002986 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002987 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002988 color |= 0x00ffffff;
2989 }
2990
Romain Guy70ca14e2010-12-13 18:24:33 -08002991 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002992 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002993 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002994 setupDrawShader();
2995 setupDrawColorFilter();
2996 setupDrawBlending(mode);
2997 setupDrawProgram();
2998 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2999 setupDrawColorUniforms();
3000 setupDrawShaderUniforms(ignoreTransform);
3001 setupDrawColorFilterUniforms();
3002 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003003
Romain Guyc95c8d62010-09-17 15:31:32 -07003004 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3005}
3006
Romain Guy82ba8142010-07-09 13:25:56 -07003007void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003008 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003009 int alpha;
3010 SkXfermode::Mode mode;
3011 getAlphaAndMode(paint, &alpha, &mode);
3012
Romain Guyd21b6e12011-11-30 20:21:23 -08003013 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003014
Romain Guy211370f2012-02-01 16:10:55 -08003015 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08003016 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
3017 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
3018
Romain Guyd21b6e12011-11-30 20:21:23 -08003019 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003020 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3021 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3022 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3023 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003024 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003025 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3026 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3027 GL_TRIANGLE_STRIP, gMeshCount);
3028 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003029}
3030
Romain Guybd6b79b2010-06-26 00:13:53 -07003031void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003032 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3033 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003034 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003035}
3036
3037void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003038 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003039 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003040 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003041
Romain Guy746b7402010-10-26 16:27:31 -07003042 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003043 setupDrawWithTexture();
3044 setupDrawColor(alpha, alpha, alpha, alpha);
3045 setupDrawColorFilter();
3046 setupDrawBlending(blend, mode, swapSrcDst);
3047 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003048 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003049 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003050 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003051 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003052 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003053 }
Romain Guy886b2752013-01-04 12:26:18 -08003054 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003055 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003056 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003057 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003058
Romain Guy6820ac82010-09-15 18:11:50 -07003059 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003060
3061 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003062}
3063
Romain Guy886b2752013-01-04 12:26:18 -08003064void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3065 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3066 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3067 bool ignoreTransform, bool dirty) {
3068
3069 setupDraw();
3070 setupDrawWithTexture(true);
3071 if (hasColor) {
3072 setupDrawAlpha8Color(color, alpha);
3073 }
3074 setupDrawColorFilter();
3075 setupDrawShader();
3076 setupDrawBlending(true, mode);
3077 setupDrawProgram();
3078 if (!dirty) setupDrawDirtyRegionsDisabled();
3079 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3080 setupDrawTexture(texture);
3081 setupDrawPureColorUniforms();
3082 setupDrawColorFilterUniforms();
3083 setupDrawShaderUniforms();
3084 setupDrawMesh(vertices, texCoords);
3085
3086 glDrawArrays(drawMode, 0, elementsCount);
3087
3088 finishDrawTexture();
3089}
3090
Romain Guya5aed0d2010-09-09 14:42:43 -07003091void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003092 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003093 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003094
Romain Guy82ba8142010-07-09 13:25:56 -07003095 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003096 // These blend modes are not supported by OpenGL directly and have
3097 // to be implemented using shaders. Since the shader will perform
3098 // the blending, turn blending off here
3099 // If the blend mode cannot be implemented using shaders, fall
3100 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003101 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003102 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003103 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003104 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003105
Romain Guy82bc7a72012-01-03 14:13:39 -08003106 if (mCaches.blend) {
3107 glDisable(GL_BLEND);
3108 mCaches.blend = false;
3109 }
3110
3111 return;
3112 } else {
3113 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003114 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003115 }
3116
3117 if (!mCaches.blend) {
3118 glEnable(GL_BLEND);
3119 }
3120
3121 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3122 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3123
3124 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3125 glBlendFunc(sourceMode, destMode);
3126 mCaches.lastSrcMode = sourceMode;
3127 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003128 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003129 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003130 glDisable(GL_BLEND);
3131 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003132 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003133}
3134
Romain Guy889f8d12010-07-29 14:37:42 -07003135bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003136 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003137 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003138 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003139 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003140 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003141 }
Romain Guy6926c722010-07-12 20:20:03 -07003142 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003143}
3144
Romain Guy026c5e162010-06-28 17:12:22 -07003145void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003146 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003147 TextureVertex::setUV(v++, u1, v1);
3148 TextureVertex::setUV(v++, u2, v1);
3149 TextureVertex::setUV(v++, u1, v2);
3150 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003151}
3152
Chet Haase5c13d892010-10-08 08:37:55 -07003153void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003154 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003155 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003156}
3157
Romain Guy9d5316e2010-06-24 19:30:36 -07003158}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003159}; // namespace android