blob: 3cec87d6d229b209ad0788ca54dfde3ca6b83cd1 [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 Craik710f46d2012-09-17 17:25:49 -070036#include "PathRenderer.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 },
83 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
84 { 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 },
104 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
105 { 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 Guydda570202010-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 Guydda570202010-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
Chris Craik710f46d2012-09-17 17:25:49 -07001484void OpenGLRenderer::setupDrawVertexShape() {
1485 mDescription.isVertexShape = true;
Chris Craik6ebdc112012-08-31 18:24:33 -07001486}
1487
Romain Guyed6fcb02011-03-21 13:11:28 -07001488void OpenGLRenderer::setupDrawPoint(float pointSize) {
1489 mDescription.isPoint = true;
1490 mDescription.pointSize = pointSize;
1491}
1492
Romain Guy8d0d4782010-12-14 20:13:35 -08001493void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1494 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001495 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1496 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1497 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001498 mColorSet = true;
1499 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1500}
1501
Romain Guy86568192010-12-14 15:55:39 -08001502void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1503 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001504 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1505 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1506 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001507 mColorSet = true;
1508 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1509}
1510
Romain Guy41210632012-07-16 17:04:24 -07001511void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1512 mCaches.fontRenderer->describe(mDescription, paint);
1513}
1514
Romain Guy70ca14e2010-12-13 18:24:33 -08001515void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1516 mColorA = a;
1517 mColorR = r;
1518 mColorG = g;
1519 mColorB = b;
1520 mColorSet = true;
1521 mSetShaderColor = mDescription.setColor(r, g, b, a);
1522}
1523
1524void OpenGLRenderer::setupDrawShader() {
1525 if (mShader) {
Romain Guy3bbacf22013-02-06 16:51:04 -08001526 mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001527 }
1528}
1529
1530void OpenGLRenderer::setupDrawColorFilter() {
1531 if (mColorFilter) {
Romain Guy3bbacf22013-02-06 16:51:04 -08001532 mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001533 }
1534}
1535
Romain Guyf09ef512011-05-27 11:43:46 -07001536void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1537 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1538 mColorA = 1.0f;
1539 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001540 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001541 }
1542}
1543
Romain Guy70ca14e2010-12-13 18:24:33 -08001544void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001545 // When the blending mode is kClear_Mode, we need to use a modulate color
1546 // argb=1,0,0,0
1547 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001548 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1549 mDescription, swapSrcDst);
1550}
1551
1552void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001553 // When the blending mode is kClear_Mode, we need to use a modulate color
1554 // argb=1,0,0,0
1555 accountForClear(mode);
Romain Guye83221c2012-09-24 16:01:35 -07001556 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()) ||
1557 (mColorFilter && mColorFilter->blend()), mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001558}
1559
1560void OpenGLRenderer::setupDrawProgram() {
1561 useProgram(mCaches.programCache.get(mDescription));
1562}
1563
1564void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1565 mTrackDirtyRegions = false;
1566}
1567
1568void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1569 bool ignoreTransform) {
1570 mModelView.loadTranslate(left, top, 0.0f);
1571 if (!ignoreTransform) {
1572 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1573 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1574 } else {
1575 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1576 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1577 }
1578}
1579
Chet Haase8a5cc922011-04-26 07:28:09 -07001580void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1581 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001582}
1583
Romain Guy70ca14e2010-12-13 18:24:33 -08001584void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1585 bool ignoreTransform, bool ignoreModelView) {
1586 if (!ignoreModelView) {
1587 mModelView.loadTranslate(left, top, 0.0f);
1588 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001589 } else {
1590 mModelView.loadIdentity();
1591 }
Romain Guy86568192010-12-14 15:55:39 -08001592 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1593 if (!ignoreTransform) {
1594 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1595 if (mTrackDirtyRegions && dirty) {
1596 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1597 }
1598 } else {
1599 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1600 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1601 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001602}
1603
Romain Guyed6fcb02011-03-21 13:11:28 -07001604void OpenGLRenderer::setupDrawPointUniforms() {
1605 int slot = mCaches.currentProgram->getUniform("pointSize");
1606 glUniform1f(slot, mDescription.pointSize);
1607}
1608
Romain Guy70ca14e2010-12-13 18:24:33 -08001609void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001610 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001611 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1612 }
1613}
1614
Romain Guy86568192010-12-14 15:55:39 -08001615void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001616 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001617 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001618 }
1619}
1620
Romain Guy70ca14e2010-12-13 18:24:33 -08001621void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1622 if (mShader) {
1623 if (ignoreTransform) {
1624 mModelView.loadInverse(*mSnapshot->transform);
1625 }
1626 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1627 }
1628}
1629
Romain Guy8d0d4782010-12-14 20:13:35 -08001630void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1631 if (mShader) {
1632 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1633 }
1634}
1635
Romain Guy70ca14e2010-12-13 18:24:33 -08001636void OpenGLRenderer::setupDrawColorFilterUniforms() {
1637 if (mColorFilter) {
1638 mColorFilter->setupProgram(mCaches.currentProgram);
1639 }
1640}
1641
Romain Guy41210632012-07-16 17:04:24 -07001642void OpenGLRenderer::setupDrawTextGammaUniforms() {
1643 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1644}
1645
Romain Guy70ca14e2010-12-13 18:24:33 -08001646void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001647 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001648 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001649 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001650}
1651
1652void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1653 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001654 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001655 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001656}
1657
Romain Guyaa6c24c2011-04-28 18:40:04 -07001658void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1659 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001660 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001661 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001662}
1663
Romain Guy8f0095c2011-05-02 17:24:22 -07001664void OpenGLRenderer::setupDrawTextureTransform() {
1665 mDescription.hasTextureTransform = true;
1666}
1667
1668void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001669 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1670 GL_FALSE, &transform.data[0]);
1671}
1672
Romain Guy70ca14e2010-12-13 18:24:33 -08001673void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001674 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001675 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001676 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001677 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001678 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001679 }
Romain Guyd71dd362011-12-12 19:03:35 -08001680
Chris Craikcb4d6002012-09-25 12:00:29 -07001681 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001682 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001683 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001684 }
1685
1686 mCaches.unbindIndicesBuffer();
1687}
1688
1689void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1690 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001691 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001692 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001693 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001694 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001695}
1696
Chet Haase5b0200b2011-04-13 17:58:08 -07001697void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001698 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001699 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001700 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001701}
1702
1703/**
1704 * Sets up the shader to draw an AA line. We draw AA lines with quads, where there is an
Chet Haase99585ad2011-05-02 15:00:16 -07001705 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1706 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1707 * attributes (one per vertex) are values from zero to one that tells the fragment
1708 * shader where the fragment is in relation to the line width/length overall; these values are
1709 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1710 * region of the line.
1711 * Note that we only pass down the width values in this setup function. The length coordinates
1712 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001713 */
Chet Haase99585ad2011-05-02 15:00:16 -07001714void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001715 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001716 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001717 mCaches.bindPositionVertexPointer(force, vertices, gAAVertexStride);
Romain Guyf3a910b42011-12-12 20:35:21 -08001718 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001719 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001720
Romain Guy7b631422012-04-04 11:38:54 -07001721 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001722 glEnableVertexAttribArray(widthSlot);
1723 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001724
Romain Guy7b631422012-04-04 11:38:54 -07001725 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001726 glEnableVertexAttribArray(lengthSlot);
1727 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001728
Chet Haase5b0200b2011-04-13 17:58:08 -07001729 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001730 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001731}
1732
1733void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1734 glDisableVertexAttribArray(widthSlot);
1735 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001736}
1737
Romain Guy70ca14e2010-12-13 18:24:33 -08001738void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001739}
1740
1741///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001742// Drawing
1743///////////////////////////////////////////////////////////////////////////////
1744
Chet Haase1271e2c2012-04-20 09:54:27 -07001745status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001746 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001747
Romain Guy0fe478e2010-11-08 12:08:41 -08001748 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1749 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001750 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001751 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001752 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001753
Romain Guy65549432012-03-26 16:45:05 -07001754 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001755}
1756
Chet Haaseed30fd82011-04-22 16:18:45 -07001757void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1758 if (displayList) {
Chris Craik2af46352012-11-26 18:30:17 -08001759 displayList->output(level);
Chet Haaseed30fd82011-04-22 16:18:45 -07001760 }
1761}
1762
Romain Guya168d732011-03-18 16:50:13 -07001763void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1764 int alpha;
1765 SkXfermode::Mode mode;
1766 getAlphaAndMode(paint, &alpha, &mode);
1767
Romain Guy886b2752013-01-04 12:26:18 -08001768 int color = paint != NULL ? paint->getColor() : 0;
1769
Romain Guya168d732011-03-18 16:50:13 -07001770 float x = left;
1771 float y = top;
1772
Romain Guy886b2752013-01-04 12:26:18 -08001773 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1774
Romain Guya168d732011-03-18 16:50:13 -07001775 bool ignoreTransform = false;
1776 if (mSnapshot->transform->isPureTranslate()) {
1777 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1778 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1779 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001780
1781 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001782 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001783 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001784 }
1785
Romain Guy886b2752013-01-04 12:26:18 -08001786 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1787 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1788 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001789}
1790
Chet Haase48659092012-05-31 15:21:51 -07001791status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c72e2010-07-12 20:20:03 -07001792 const float right = left + bitmap->width();
1793 const float bottom = top + bitmap->height();
1794
1795 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001796 return DrawGlInfo::kStatusDone;
Romain Guy6926c72e2010-07-12 20:20:03 -07001797 }
1798
Romain Guya1d3c912011-12-13 14:55:06 -08001799 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001800 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001801 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001802 const AutoTexture autoCleanup(texture);
1803
Romain Guy211370f2012-02-01 16:10:55 -08001804 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001805 drawAlphaBitmap(texture, left, top, paint);
1806 } else {
1807 drawTextureRect(left, top, right, bottom, texture, paint);
1808 }
Chet Haase48659092012-05-31 15:21:51 -07001809
1810 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001811}
1812
Chet Haase48659092012-05-31 15:21:51 -07001813status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001814 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1815 const mat4 transform(*matrix);
1816 transform.mapRect(r);
1817
Romain Guy6926c72e2010-07-12 20:20:03 -07001818 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001819 return DrawGlInfo::kStatusDone;
Romain Guy6926c72e2010-07-12 20:20:03 -07001820 }
1821
Romain Guya1d3c912011-12-13 14:55:06 -08001822 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001823 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001824 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001825 const AutoTexture autoCleanup(texture);
1826
Romain Guy5b3b3522010-10-27 18:57:51 -07001827 // This could be done in a cheaper way, all we need is pass the matrix
1828 // to the vertex shader. The save/restore is a bit overkill.
1829 save(SkCanvas::kMatrix_SaveFlag);
1830 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08001831 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1832 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
1833 } else {
1834 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1835 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001836 restore();
Chet Haase48659092012-05-31 15:21:51 -07001837
1838 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001839}
1840
Chet Haase48659092012-05-31 15:21:51 -07001841status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001842 const float right = left + bitmap->width();
1843 const float bottom = top + bitmap->height();
1844
1845 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001846 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001847 }
1848
1849 mCaches.activeTexture(0);
1850 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1851 const AutoTexture autoCleanup(texture);
1852
Romain Guy886b2752013-01-04 12:26:18 -08001853 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1854 drawAlphaBitmap(texture, left, top, paint);
1855 } else {
1856 drawTextureRect(left, top, right, bottom, texture, paint);
1857 }
Chet Haase48659092012-05-31 15:21:51 -07001858
1859 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001860}
1861
Chet Haase48659092012-05-31 15:21:51 -07001862status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001863 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001864 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001865 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001866 }
1867
Romain Guyb18d2d02011-02-10 15:52:54 -08001868 float left = FLT_MAX;
1869 float top = FLT_MAX;
1870 float right = FLT_MIN;
1871 float bottom = FLT_MIN;
1872
Romain Guya92bb4d2012-10-16 11:08:44 -07001873 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001874
Romain Guya566b7c2011-01-23 16:36:11 -08001875 // TODO: Support the colors array
1876 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001877 TextureVertex* vertex = mesh;
Romain Guya92bb4d2012-10-16 11:08:44 -07001878
Romain Guy5a7b4662011-01-20 19:09:30 -08001879 for (int32_t y = 0; y < meshHeight; y++) {
1880 for (int32_t x = 0; x < meshWidth; x++) {
1881 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1882
1883 float u1 = float(x) / meshWidth;
1884 float u2 = float(x + 1) / meshWidth;
1885 float v1 = float(y) / meshHeight;
1886 float v2 = float(y + 1) / meshHeight;
1887
1888 int ax = i + (meshWidth + 1) * 2;
1889 int ay = ax + 1;
1890 int bx = i;
1891 int by = bx + 1;
1892 int cx = i + 2;
1893 int cy = cx + 1;
1894 int dx = i + (meshWidth + 1) * 2 + 2;
1895 int dy = dx + 1;
1896
1897 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1898 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1899 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1900
1901 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1902 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1903 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001904
Romain Guya92bb4d2012-10-16 11:08:44 -07001905 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1906 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1907 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1908 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001909 }
1910 }
1911
Romain Guya92bb4d2012-10-16 11:08:44 -07001912 if (quickReject(left, top, right, bottom)) {
1913 return DrawGlInfo::kStatusDone;
1914 }
1915
1916 mCaches.activeTexture(0);
1917 Texture* texture = mCaches.textureCache.get(bitmap);
1918 if (!texture) return DrawGlInfo::kStatusDone;
1919 const AutoTexture autoCleanup(texture);
1920
1921 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1922 texture->setFilter(FILTER(paint), true);
1923
1924 int alpha;
1925 SkXfermode::Mode mode;
1926 getAlphaAndMode(paint, &alpha, &mode);
1927
1928 if (hasLayer()) {
Romain Guyb18d2d02011-02-10 15:52:54 -08001929 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1930 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001931
Romain Guy5a7b4662011-01-20 19:09:30 -08001932 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1933 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001934 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001935
1936 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001937}
1938
Chet Haase48659092012-05-31 15:21:51 -07001939status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001940 float srcLeft, float srcTop, float srcRight, float srcBottom,
1941 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001942 SkPaint* paint) {
Romain Guy6926c72e2010-07-12 20:20:03 -07001943 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001944 return DrawGlInfo::kStatusDone;
Romain Guy6926c72e2010-07-12 20:20:03 -07001945 }
1946
Romain Guya1d3c912011-12-13 14:55:06 -08001947 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001948 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001949 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001950 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001951
Romain Guy8ba548f2010-06-30 19:21:21 -07001952 const float width = texture->width;
1953 const float height = texture->height;
1954
Romain Guy68169722011-08-22 17:33:33 -07001955 const float u1 = fmax(0.0f, srcLeft / width);
1956 const float v1 = fmax(0.0f, srcTop / height);
1957 const float u2 = fmin(1.0f, srcRight / width);
1958 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001959
Romain Guy03750a02010-10-18 14:06:08 -07001960 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001961 resetDrawTextureTexCoords(u1, v1, u2, v2);
1962
Romain Guy03750a02010-10-18 14:06:08 -07001963 int alpha;
1964 SkXfermode::Mode mode;
1965 getAlphaAndMode(paint, &alpha, &mode);
1966
Romain Guyd21b6e12011-11-30 20:21:23 -08001967 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1968
Romain Guy886b2752013-01-04 12:26:18 -08001969 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
1970 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08001971
Romain Guy886b2752013-01-04 12:26:18 -08001972 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
1973 // Apply a scale transform on the canvas only when a shader is in use
1974 // Skia handles the ratio between the dst and src rects as a scale factor
1975 // when a shader is set
1976 bool useScaleTransform = mShader && scaled;
1977 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07001978
Romain Guy886b2752013-01-04 12:26:18 -08001979 if (CC_LIKELY(mSnapshot->transform->isPureTranslate() && !useScaleTransform)) {
1980 float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1981 float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1982
1983 dstRight = x + (dstRight - dstLeft);
1984 dstBottom = y + (dstBottom - dstTop);
1985
1986 dstLeft = x;
1987 dstTop = y;
1988
1989 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
1990 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08001991 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001992 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08001993 }
1994
1995 if (CC_UNLIKELY(useScaleTransform)) {
1996 save(SkCanvas::kMatrix_SaveFlag);
1997 translate(dstLeft, dstTop);
1998 scale(scaleX, scaleY);
1999
2000 dstLeft = 0.0f;
2001 dstTop = 0.0f;
2002
2003 dstRight = srcRight - srcLeft;
2004 dstBottom = srcBottom - srcTop;
2005 }
2006
2007 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2008 int color = paint ? paint->getColor() : 0;
2009 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2010 texture->id, paint != NULL, color, alpha, mode,
2011 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2012 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2013 } else {
2014 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2015 texture->id, alpha / 255.0f, mode, texture->blend,
2016 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2017 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2018 }
2019
2020 if (CC_UNLIKELY(useScaleTransform)) {
2021 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002022 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002023
2024 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002025
2026 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002027}
2028
Chet Haase48659092012-05-31 15:21:51 -07002029status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07002030 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07002031 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002032 int alpha;
2033 SkXfermode::Mode mode;
2034 getAlphaAndModeDirect(paint, &alpha, &mode);
2035
2036 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2037 left, top, right, bottom, alpha, mode);
2038}
2039
2040status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2041 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2042 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c72e2010-07-12 20:20:03 -07002043 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002044 return DrawGlInfo::kStatusDone;
Romain Guy6926c72e2010-07-12 20:20:03 -07002045 }
2046
Romain Guybe6f9dc2012-07-16 12:41:17 -07002047 alpha *= mSnapshot->alpha;
2048
Romain Guya1d3c912011-12-13 14:55:06 -08002049 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002050 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002051 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002052 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08002053 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2054 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07002055
Romain Guy2728f962010-10-08 18:36:15 -07002056 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002057 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002058
Romain Guy211370f2012-02-01 16:10:55 -08002059 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002060 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002061 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002062 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002063 const float offsetX = left + mSnapshot->transform->getTranslateX();
2064 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002065 const size_t count = mesh->quads.size();
2066 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002067 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002068 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002069 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2070 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2071 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002072 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002073 dirtyLayer(left + bounds.left, top + bounds.top,
2074 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08002075 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002076 }
2077 }
2078
Romain Guy211370f2012-02-01 16:10:55 -08002079 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002080 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2081 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2082
2083 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2084 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2085 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2086 true, !mesh->hasEmptyQuads);
2087 } else {
2088 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2089 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2090 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2091 true, !mesh->hasEmptyQuads);
2092 }
Romain Guy054dc182010-10-15 17:55:25 -07002093 }
Chet Haase48659092012-05-31 15:21:51 -07002094
2095 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002096}
2097
Chet Haase99ecdc42011-05-06 12:06:34 -07002098/**
Chris Craikcb4d6002012-09-25 12:00:29 -07002099 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2100 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2101 * screen space in all directions. However, instead of using a fragment shader to compute the
2102 * translucency of the color from its position, we simply use a varying parameter to define how far
2103 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2104 *
2105 * Doesn't yet support joins, caps, or path effects.
Chet Haase858aa932011-05-12 09:06:00 -07002106 */
Chris Craikcb4d6002012-09-25 12:00:29 -07002107void OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2108 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002109 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2110 bool isAA = paint->isAntiAlias();
2111
Chris Craik710f46d2012-09-17 17:25:49 -07002112 VertexBuffer vertexBuffer;
2113 // TODO: try clipping large paths to viewport
Chris Craikcb4d6002012-09-25 12:00:29 -07002114 PathRenderer::convexPathVertices(path, paint, mSnapshot->transform, vertexBuffer);
Romain Guy04299382012-07-18 17:15:41 -07002115
Chris Craikbf09ffb2012-10-01 13:50:37 -07002116 if (!vertexBuffer.getSize()) {
2117 // no vertices to draw
2118 return;
2119 }
2120
Chris Craik710f46d2012-09-17 17:25:49 -07002121 setupDraw();
2122 setupDrawNoTexture();
2123 if (isAA) setupDrawAA();
2124 setupDrawVertexShape();
2125 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2126 setupDrawColorFilter();
2127 setupDrawShader();
2128 setupDrawBlending(isAA, mode);
2129 setupDrawProgram();
Chris Craikcb4d6002012-09-25 12:00:29 -07002130 setupDrawModelViewIdentity();
Chris Craik710f46d2012-09-17 17:25:49 -07002131 setupDrawColorUniforms();
2132 setupDrawColorFilterUniforms();
2133 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002134
Chris Craik710f46d2012-09-17 17:25:49 -07002135 void* vertices = vertexBuffer.getBuffer();
2136 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002137 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002138 mCaches.resetTexCoordsVertexPointer();
2139 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002140
Chris Craik710f46d2012-09-17 17:25:49 -07002141 int alphaSlot = -1;
2142 if (isAA) {
2143 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2144 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002145
Chris Craik710f46d2012-09-17 17:25:49 -07002146 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002147 glEnableVertexAttribArray(alphaSlot);
2148 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002149 }
Romain Guy04299382012-07-18 17:15:41 -07002150
Chris Craikcb4d6002012-09-25 12:00:29 -07002151 SkRect bounds = PathRenderer::computePathBounds(path, paint);
Chris Craik710f46d2012-09-17 17:25:49 -07002152 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy04299382012-07-18 17:15:41 -07002153
Chris Craik710f46d2012-09-17 17:25:49 -07002154 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002155
Chris Craik710f46d2012-09-17 17:25:49 -07002156 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002157 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002158 }
Chet Haase858aa932011-05-12 09:06:00 -07002159}
2160
2161/**
Chet Haase99ecdc42011-05-06 12:06:34 -07002162 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
2163 * rules for those lines produces some unexpected results, and may vary between hardware devices.
2164 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
2165 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
2166 * of the line. Hairlines are more involved because we need to account for transform scaling
2167 * to end up with a one-pixel-wide line in screen space..
2168 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
2169 * in combination with values that we calculate and pass down in this method. The basic approach
2170 * is that the quad we create contains both the core line area plus a bounding area in which
2171 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
2172 * proportion of the width and the length of a given segment is represented by the boundary
2173 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
2174 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
2175 * on the inside). This ends up giving the result we want, with pixels that are completely
2176 * 'inside' the line area being filled opaquely and the other pixels being filled according to
2177 * how far into the boundary region they are, which is determined by shader interpolation.
2178 */
Chet Haase48659092012-05-31 15:21:51 -07002179status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
2180 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002181
2182 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07002183 // We use half the stroke width here because we're going to position the quad
2184 // corner vertices half of the width away from the line endpoints
2185 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002186 // A stroke width of 0 has a special meaning in Skia:
2187 // it draws a line 1 px wide regardless of current transform
2188 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07002189
Chet Haase99ecdc42011-05-06 12:06:34 -07002190 float inverseScaleX = 1.0f;
2191 float inverseScaleY = 1.0f;
2192 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07002193
Chet Haase8a5cc922011-04-26 07:28:09 -07002194 int alpha;
2195 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07002196
Chet Haase8a5cc922011-04-26 07:28:09 -07002197 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002198 int verticesCount = count;
2199 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07002200 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07002201 verticesCount += (count - 4);
2202 }
2203
2204 if (isHairLine || isAA) {
2205 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
2206 // the line on the screen should always be one pixel wide regardless of scale. For
2207 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08002208 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07002209 Matrix4 *mat = mSnapshot->transform;
2210 float m00 = mat->data[Matrix4::kScaleX];
2211 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07002212 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07002213 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07002214
Romain Guy211370f2012-02-01 16:10:55 -08002215 float scaleX = sqrtf(m00 * m00 + m01 * m01);
2216 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07002217
Chet Haase99ecdc42011-05-06 12:06:34 -07002218 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
2219 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07002220
Chet Haase99ecdc42011-05-06 12:06:34 -07002221 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
2222 scaled = true;
2223 }
2224 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002225 }
Chet Haase8a5cc922011-04-26 07:28:09 -07002226
2227 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07002228
2229 mCaches.enableScissor();
2230
Chet Haase8a5cc922011-04-26 07:28:09 -07002231 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002232 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002233 if (isAA) {
Chris Craik710f46d2012-09-17 17:25:49 -07002234 setupDrawAA();
Chet Haase8a5cc922011-04-26 07:28:09 -07002235 }
2236 setupDrawColor(paint->getColor(), alpha);
2237 setupDrawColorFilter();
2238 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08002239 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07002240 setupDrawProgram();
Chris Craikb30cb102012-10-05 19:11:37 -07002241 setupDrawModelViewIdentity(true);
Chet Haase8a5cc922011-04-26 07:28:09 -07002242 setupDrawColorUniforms();
2243 setupDrawColorFilterUniforms();
2244 setupDrawShaderIdentityUniforms();
2245
2246 if (isHairLine) {
2247 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07002248 halfStrokeWidth = isAA? 1 : .5;
2249 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002250 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07002251 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07002252 }
Romain Guy7b631422012-04-04 11:38:54 -07002253
2254 int widthSlot;
2255 int lengthSlot;
2256
Chet Haase5b0200b2011-04-13 17:58:08 -07002257 Vertex lines[verticesCount];
2258 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002259
Chet Haase99585ad2011-05-02 15:00:16 -07002260 AAVertex wLines[verticesCount];
2261 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002262
Romain Guy211370f2012-02-01 16:10:55 -08002263 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002264 setupDrawVertices(vertices);
2265 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002266 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2267 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002268 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002269 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2270 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002271 // We will need to calculate the actual width proportion on each segment for
2272 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002273 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002274 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2275 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002276 }
2277
Chet Haase99ecdc42011-05-06 12:06:34 -07002278 AAVertex* prevAAVertex = NULL;
2279 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002280
Chet Haase99585ad2011-05-02 15:00:16 -07002281 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002282 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002283
Chet Haase5b0200b2011-04-13 17:58:08 -07002284 for (int i = 0; i < count; i += 4) {
2285 // a = start point, b = end point
2286 vec2 a(points[i], points[i + 1]);
2287 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002288
Chet Haase99585ad2011-05-02 15:00:16 -07002289 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002290 float boundaryLengthProportion = 0;
2291 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002292
Chet Haase5b0200b2011-04-13 17:58:08 -07002293 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002294 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002295 float x = n.x;
2296 n.x = -n.y;
2297 n.y = x;
2298
Chet Haase8a5cc922011-04-26 07:28:09 -07002299 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002300 if (isAA) {
2301 float wideningFactor;
2302 if (fabs(n.x) >= fabs(n.y)) {
2303 wideningFactor = fabs(1.0f / n.x);
2304 } else {
2305 wideningFactor = fabs(1.0f / n.y);
2306 }
2307 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002308 }
Romain Guy7b631422012-04-04 11:38:54 -07002309
Chet Haase99ecdc42011-05-06 12:06:34 -07002310 if (scaled) {
2311 n.x *= inverseScaleX;
2312 n.y *= inverseScaleY;
2313 }
2314 } else if (scaled) {
2315 // Extend n by .5 pixel on each side, post-transform
2316 vec2 extendedN = n.copyNormalized();
2317 extendedN /= 2;
2318 extendedN.x *= inverseScaleX;
2319 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002320
Chet Haase99ecdc42011-05-06 12:06:34 -07002321 float extendedNLength = extendedN.length();
2322 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002323 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002324 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002325 }
Romain Guy7b631422012-04-04 11:38:54 -07002326
Chet Haase99585ad2011-05-02 15:00:16 -07002327 // aa lines expand the endpoint vertices to encompass the AA boundary
2328 if (isAA) {
2329 vec2 abVector = (b - a);
2330 length = abVector.length();
2331 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002332
Chet Haase99ecdc42011-05-06 12:06:34 -07002333 if (scaled) {
2334 abVector.x *= inverseScaleX;
2335 abVector.y *= inverseScaleY;
2336 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002337 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002338 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002339 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002340 }
Romain Guy7b631422012-04-04 11:38:54 -07002341
Chet Haase99ecdc42011-05-06 12:06:34 -07002342 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002343 a -= abVector;
2344 b += abVector;
2345 }
2346
Chet Haase5b0200b2011-04-13 17:58:08 -07002347 // Four corners of the rectangle defining a thick line
2348 vec2 p1 = a - n;
2349 vec2 p2 = a + n;
2350 vec2 p3 = b + n;
2351 vec2 p4 = b - n;
2352
Chet Haase99585ad2011-05-02 15:00:16 -07002353
Chet Haase5b0200b2011-04-13 17:58:08 -07002354 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2355 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2356 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2357 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2358
Romain Guy04299382012-07-18 17:15:41 -07002359 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002360 if (!isAA) {
2361 if (prevVertex != NULL) {
2362 // Issue two repeat vertices to create degenerate triangles to bridge
2363 // between the previous line and the new one. This is necessary because
2364 // we are creating a single triangle_strip which will contain
2365 // potentially discontinuous line segments.
2366 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2367 Vertex::set(vertices++, p1.x, p1.y);
2368 generatedVerticesCount += 2;
2369 }
Romain Guy7b631422012-04-04 11:38:54 -07002370
Chet Haase5b0200b2011-04-13 17:58:08 -07002371 Vertex::set(vertices++, p1.x, p1.y);
2372 Vertex::set(vertices++, p2.x, p2.y);
2373 Vertex::set(vertices++, p4.x, p4.y);
2374 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002375
Chet Haase5b0200b2011-04-13 17:58:08 -07002376 prevVertex = vertices - 1;
2377 generatedVerticesCount += 4;
2378 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002379 if (!isHairLine && scaled) {
2380 // Must set width proportions per-segment for scaled non-hairlines to use the
2381 // correct AA boundary dimensions
2382 if (boundaryWidthSlot < 0) {
2383 boundaryWidthSlot =
2384 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002385 }
Romain Guy7b631422012-04-04 11:38:54 -07002386
Chet Haase99ecdc42011-05-06 12:06:34 -07002387 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002388 }
Romain Guy7b631422012-04-04 11:38:54 -07002389
Chet Haase99585ad2011-05-02 15:00:16 -07002390 if (boundaryLengthSlot < 0) {
2391 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002392 }
Romain Guy7b631422012-04-04 11:38:54 -07002393
Chet Haase99ecdc42011-05-06 12:06:34 -07002394 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002395
Chet Haase5b0200b2011-04-13 17:58:08 -07002396 if (prevAAVertex != NULL) {
2397 // Issue two repeat vertices to create degenerate triangles to bridge
2398 // between the previous line and the new one. This is necessary because
2399 // we are creating a single triangle_strip which will contain
2400 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002401 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2402 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2403 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002404 generatedVerticesCount += 2;
2405 }
Romain Guy7b631422012-04-04 11:38:54 -07002406
Chet Haase99585ad2011-05-02 15:00:16 -07002407 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2408 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2409 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2410 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002411
Chet Haase5b0200b2011-04-13 17:58:08 -07002412 prevAAVertex = aaVertices - 1;
2413 generatedVerticesCount += 4;
2414 }
Romain Guy7b631422012-04-04 11:38:54 -07002415
Chet Haase99585ad2011-05-02 15:00:16 -07002416 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2417 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2418 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002419 }
2420 }
Romain Guy7b631422012-04-04 11:38:54 -07002421
Chet Haase5b0200b2011-04-13 17:58:08 -07002422 if (generatedVerticesCount > 0) {
2423 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2424 }
Romain Guy7b631422012-04-04 11:38:54 -07002425
2426 if (isAA) {
2427 finishDrawAALine(widthSlot, lengthSlot);
2428 }
Chet Haase48659092012-05-31 15:21:51 -07002429
2430 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002431}
2432
Chet Haase48659092012-05-31 15:21:51 -07002433status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2434 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002435
2436 // TODO: The paint's cap style defines whether the points are square or circular
2437 // TODO: Handle AA for round points
2438
Chet Haase5b0200b2011-04-13 17:58:08 -07002439 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002440 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002441 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002442 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002443 if (isHairLine) {
2444 // Now that we know it's hairline, we can set the effective width, to be used later
2445 strokeWidth = 1.0f;
2446 }
2447 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002448 int alpha;
2449 SkXfermode::Mode mode;
2450 getAlphaAndMode(paint, &alpha, &mode);
2451
2452 int verticesCount = count >> 1;
2453 int generatedVerticesCount = 0;
2454
2455 TextureVertex pointsData[verticesCount];
2456 TextureVertex* vertex = &pointsData[0];
2457
Romain Guy04299382012-07-18 17:15:41 -07002458 // TODO: We should optimize this method to not generate vertices for points
2459 // that lie outside of the clip.
2460 mCaches.enableScissor();
2461
Romain Guyed6fcb02011-03-21 13:11:28 -07002462 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002463 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002464 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002465 setupDrawColor(paint->getColor(), alpha);
2466 setupDrawColorFilter();
2467 setupDrawShader();
2468 setupDrawBlending(mode);
2469 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002470 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002471 setupDrawColorUniforms();
2472 setupDrawColorFilterUniforms();
2473 setupDrawPointUniforms();
2474 setupDrawShaderIdentityUniforms();
2475 setupDrawMesh(vertex);
2476
2477 for (int i = 0; i < count; i += 2) {
2478 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2479 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002480
Chet Haase8a5cc922011-04-26 07:28:09 -07002481 float left = points[i] - halfWidth;
2482 float right = points[i] + halfWidth;
2483 float top = points[i + 1] - halfWidth;
2484 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002485
Chet Haase8a5cc922011-04-26 07:28:09 -07002486 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002487 }
2488
2489 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002490
2491 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002492}
2493
Chet Haase48659092012-05-31 15:21:51 -07002494status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002495 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002496 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002497
Romain Guyae88e5e2010-10-22 17:49:18 -07002498 Rect& clip(*mSnapshot->clipRect);
2499 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002500
Romain Guy3d58c032010-07-14 16:34:53 -07002501 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002502
2503 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002504}
Romain Guy9d5316e2010-06-24 19:30:36 -07002505
Chet Haase48659092012-05-31 15:21:51 -07002506status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2507 SkPaint* paint) {
2508 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002509 const AutoTexture autoCleanup(texture);
2510
2511 const float x = left + texture->left - texture->offset;
2512 const float y = top + texture->top - texture->offset;
2513
2514 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002515
2516 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002517}
2518
Chet Haase48659092012-05-31 15:21:51 -07002519status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002520 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002521 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002522 return DrawGlInfo::kStatusDone;
2523 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002524
Chris Craikcb4d6002012-09-25 12:00:29 -07002525 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002526 mCaches.activeTexture(0);
2527 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2528 right - left, bottom - top, rx, ry, p);
2529 return drawShape(left, top, texture, p);
2530 }
2531
2532 SkPath path;
2533 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002534 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2535 float outset = p->getStrokeWidth() / 2;
2536 rect.outset(outset, outset);
2537 rx += outset;
2538 ry += outset;
2539 }
Chris Craik710f46d2012-09-17 17:25:49 -07002540 path.addRoundRect(rect, rx, ry);
Chris Craikcb4d6002012-09-25 12:00:29 -07002541 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002542
2543 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002544}
2545
Chris Craik710f46d2012-09-17 17:25:49 -07002546status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002547 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2548 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002549 return DrawGlInfo::kStatusDone;
2550 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002551 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002552 mCaches.activeTexture(0);
2553 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2554 return drawShape(x - radius, y - radius, texture, p);
2555 }
2556
2557 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002558 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2559 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2560 } else {
2561 path.addCircle(x, y, radius);
2562 }
2563 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002564
2565 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002566}
Romain Guy01d58e42011-01-19 21:54:02 -08002567
Chet Haase48659092012-05-31 15:21:51 -07002568status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002569 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002570 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002571 return DrawGlInfo::kStatusDone;
2572 }
Romain Guy01d58e42011-01-19 21:54:02 -08002573
Chris Craikcb4d6002012-09-25 12:00:29 -07002574 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002575 mCaches.activeTexture(0);
2576 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2577 return drawShape(left, top, texture, p);
2578 }
2579
2580 SkPath path;
2581 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002582 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2583 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2584 }
Chris Craik710f46d2012-09-17 17:25:49 -07002585 path.addOval(rect);
Chris Craikcb4d6002012-09-25 12:00:29 -07002586 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002587
2588 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002589}
2590
Chet Haase48659092012-05-31 15:21:51 -07002591status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002592 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
2593 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
2594 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002595 }
2596
Chris Craik780c1282012-10-04 14:10:49 -07002597 if (fabs(sweepAngle) >= 360.0f) {
2598 return drawOval(left, top, right, bottom, p);
2599 }
2600
2601 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Romain Guye3a9b242013-01-08 11:15:30 -08002602 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 ||
2603 p->getStrokeCap() != SkPaint::kButt_Cap || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002604 mCaches.activeTexture(0);
2605 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2606 startAngle, sweepAngle, useCenter, p);
2607 return drawShape(left, top, texture, p);
2608 }
2609
2610 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2611 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2612 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2613 }
2614
2615 SkPath path;
2616 if (useCenter) {
2617 path.moveTo(rect.centerX(), rect.centerY());
2618 }
2619 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2620 if (useCenter) {
2621 path.close();
2622 }
2623 drawConvexPath(path, p);
2624
2625 return DrawGlInfo::kStatusDrew;
Romain Guy8b2f5262011-01-23 16:15:02 -08002626}
2627
Romain Guycf8675e2012-10-02 12:32:25 -07002628// See SkPaintDefaults.h
2629#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2630
Chet Haase48659092012-05-31 15:21:51 -07002631status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002632 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002633 return DrawGlInfo::kStatusDone;
Romain Guy6926c72e2010-07-12 20:20:03 -07002634 }
2635
Chris Craik710f46d2012-09-17 17:25:49 -07002636 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002637 // only fill style is supported by drawConvexPath, since others have to handle joins
2638 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2639 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2640 mCaches.activeTexture(0);
2641 const PathTexture* texture =
2642 mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2643 return drawShape(left, top, texture, p);
2644 }
2645
2646 SkPath path;
2647 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2648 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2649 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2650 }
2651 path.addRect(rect);
2652 drawConvexPath(path, p);
2653
2654 return DrawGlInfo::kStatusDrew;
Romain Guy026c5e162010-06-28 17:12:22 -07002655 }
2656
Romain Guy181d0a62011-06-09 18:52:38 -07002657 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002658 SkPath path;
2659 path.addRect(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002660 drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002661 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002662 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chet Haase858aa932011-05-12 09:06:00 -07002663 }
Chet Haase48659092012-05-31 15:21:51 -07002664
2665 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002666}
Romain Guy9d5316e2010-06-24 19:30:36 -07002667
Raph Levien416a8472012-07-19 22:48:17 -07002668void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2669 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2670 float x, float y) {
2671 mCaches.activeTexture(0);
2672
2673 // NOTE: The drop shadow will not perform gamma correction
2674 // if shader-based correction is enabled
2675 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2676 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2677 paint, text, bytesCount, count, mShadowRadius, positions);
2678 const AutoTexture autoCleanup(shadow);
2679
2680 const float sx = x - shadow->left + mShadowDx;
2681 const float sy = y - shadow->top + mShadowDy;
2682
2683 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2684 int shadowColor = mShadowColor;
2685 if (mShader) {
2686 shadowColor = 0xffffffff;
2687 }
2688
2689 setupDraw();
2690 setupDrawWithTexture(true);
2691 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2692 setupDrawColorFilter();
2693 setupDrawShader();
2694 setupDrawBlending(true, mode);
2695 setupDrawProgram();
2696 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2697 setupDrawTexture(shadow->id);
2698 setupDrawPureColorUniforms();
2699 setupDrawColorFilterUniforms();
2700 setupDrawShaderUniforms();
2701 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2702
2703 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2704}
2705
Chet Haase48659092012-05-31 15:21:51 -07002706status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002707 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002708 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002709 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002710 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002711 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002712
Romain Guy671d6cf2012-01-18 12:39:17 -08002713 // NOTE: Skia does not support perspective transform on drawPosText yet
2714 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002715 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002716 }
2717
2718 float x = 0.0f;
2719 float y = 0.0f;
2720 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2721 if (pureTranslate) {
2722 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2723 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2724 }
2725
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002726 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002727 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy671d6cf2012-01-18 12:39:17 -08002728
2729 int alpha;
2730 SkXfermode::Mode mode;
2731 getAlphaAndMode(paint, &alpha, &mode);
2732
Raph Levien416a8472012-07-19 22:48:17 -07002733 if (CC_UNLIKELY(mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002734 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2735 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002736 }
2737
Romain Guy671d6cf2012-01-18 12:39:17 -08002738 // Pick the appropriate texture filtering
2739 bool linearFilter = mSnapshot->transform->changesBounds();
2740 if (pureTranslate && !linearFilter) {
2741 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2742 }
2743
2744 mCaches.activeTexture(0);
2745 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002746 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002747 setupDrawDirtyRegionsDisabled();
2748 setupDrawWithTexture(true);
2749 setupDrawAlpha8Color(paint->getColor(), alpha);
2750 setupDrawColorFilter();
2751 setupDrawShader();
2752 setupDrawBlending(true, mode);
2753 setupDrawProgram();
2754 setupDrawModelView(x, y, x, y, pureTranslate, true);
2755 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2756 setupDrawPureColorUniforms();
2757 setupDrawColorFilterUniforms();
2758 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002759 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002760
2761 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2762 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2763
Romain Guy211370f2012-02-01 16:10:55 -08002764 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002765
2766 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2767 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002768 if (hasActiveLayer) {
2769 if (!pureTranslate) {
2770 mSnapshot->transform->mapRect(bounds);
2771 }
2772 dirtyLayerUnchecked(bounds, getRegion());
2773 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002774 }
Chet Haase48659092012-05-31 15:21:51 -07002775
2776 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002777}
2778
Romain Guyc2525952012-07-27 16:41:22 -07002779status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002780 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002781 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002782 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002783 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002784 }
2785
Chet Haasea1cff502012-02-21 13:43:44 -08002786 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002787 switch (paint->getTextAlign()) {
2788 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002789 x -= length / 2.0f;
2790 break;
2791 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002792 x -= length;
2793 break;
2794 default:
2795 break;
2796 }
2797
Romain Guycac5fd32011-12-01 20:08:50 -08002798 SkPaint::FontMetrics metrics;
2799 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002800 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002801 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002802 }
2803
Romain Guye3a9b242013-01-08 11:15:30 -08002804#if DEBUG_GLYPHS
2805 ALOGD("OpenGLRenderer drawText() with FontID=%d",
2806 SkTypeface::UniqueID(paint->getTypeface()));
2807#endif
2808
2809 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2810 fontRenderer.setFont(paint, *mSnapshot->transform);
2811
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002812 const float oldX = x;
2813 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002814 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002815 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002816 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2817 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2818 }
2819
Romain Guy86568192010-12-14 15:55:39 -08002820 int alpha;
2821 SkXfermode::Mode mode;
2822 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002823
Romain Guy211370f2012-02-01 16:10:55 -08002824 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002825 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2826 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002827 }
2828
Romain Guy6620c6d2010-12-06 18:07:02 -08002829 // Pick the appropriate texture filtering
2830 bool linearFilter = mSnapshot->transform->changesBounds();
2831 if (pureTranslate && !linearFilter) {
2832 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2833 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002834
Romain Guy16c88082012-06-11 16:03:47 -07002835 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002836 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002837 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002838 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002839 setupDrawDirtyRegionsDisabled();
2840 setupDrawWithTexture(true);
2841 setupDrawAlpha8Color(paint->getColor(), alpha);
2842 setupDrawColorFilter();
2843 setupDrawShader();
2844 setupDrawBlending(true, mode);
2845 setupDrawProgram();
2846 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002847 // See comment above; the font renderer must use texture unit 0
2848 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002849 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2850 setupDrawPureColorUniforms();
2851 setupDrawColorFilterUniforms();
2852 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002853 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002854
Romain Guya3dc55f2012-09-28 13:55:44 -07002855 const Rect* clip = pureTranslate ? mSnapshot->clipRect :
2856 (mSnapshot->hasPerspectiveTransform() ? NULL : &mSnapshot->getLocalClip());
Romain Guy5b3b3522010-10-27 18:57:51 -07002857 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2858
Romain Guy211370f2012-02-01 16:10:55 -08002859 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002860
Raph Levien996e57c2012-07-23 15:22:52 -07002861 bool status;
Romain Guya3dc55f2012-09-28 13:55:44 -07002862 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002863 SkPaint paintCopy(*paint);
2864 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2865 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002866 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002867 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002868 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002869 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002870 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002871
2872 if (status && hasActiveLayer) {
2873 if (!pureTranslate) {
2874 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002875 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002876 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002877 }
Romain Guy694b5192010-07-21 21:33:20 -07002878
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002879 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002880
2881 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002882}
2883
Chet Haase48659092012-05-31 15:21:51 -07002884status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002885 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002886 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2887 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002888 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002889 }
2890
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002891 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002892 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy03d58522012-02-24 17:54:07 -08002893
2894 int alpha;
2895 SkXfermode::Mode mode;
2896 getAlphaAndMode(paint, &alpha, &mode);
2897
2898 mCaches.activeTexture(0);
2899 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002900 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002901 setupDrawDirtyRegionsDisabled();
2902 setupDrawWithTexture(true);
2903 setupDrawAlpha8Color(paint->getColor(), alpha);
2904 setupDrawColorFilter();
2905 setupDrawShader();
2906 setupDrawBlending(true, mode);
2907 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002908 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002909 setupDrawTexture(fontRenderer.getTexture(true));
2910 setupDrawPureColorUniforms();
2911 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002912 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002913 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002914
Romain Guy97771732012-02-28 18:17:02 -08002915 const Rect* clip = &mSnapshot->getLocalClip();
2916 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 -08002917
Romain Guy97771732012-02-28 18:17:02 -08002918 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002919
2920 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2921 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002922 if (hasActiveLayer) {
2923 mSnapshot->transform->mapRect(bounds);
2924 dirtyLayerUnchecked(bounds, getRegion());
2925 }
Romain Guy97771732012-02-28 18:17:02 -08002926 }
Chet Haase48659092012-05-31 15:21:51 -07002927
2928 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002929}
2930
Chet Haase48659092012-05-31 15:21:51 -07002931status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2932 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002933
Romain Guya1d3c912011-12-13 14:55:06 -08002934 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002935
Romain Guyfb8b7632010-08-23 21:05:08 -07002936 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002937 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002938 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002939
Romain Guy8b55f372010-08-18 17:10:07 -07002940 const float x = texture->left - texture->offset;
2941 const float y = texture->top - texture->offset;
2942
Romain Guy01d58e42011-01-19 21:54:02 -08002943 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002944
2945 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002946}
2947
Chet Haase48659092012-05-31 15:21:51 -07002948status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002949 if (!layer) {
2950 return DrawGlInfo::kStatusDone;
2951 }
2952
Romain Guyb2e2f242012-10-17 18:18:35 -07002953 mat4* transform = NULL;
2954 if (layer->isTextureLayer()) {
2955 transform = &layer->getTransform();
2956 if (!transform->isIdentity()) {
2957 save(0);
2958 mSnapshot->transform->multiply(*transform);
2959 }
2960 }
2961
Romain Guy35643dd2012-09-18 15:40:58 -07002962 Rect transformed;
2963 Rect clip;
2964 const bool rejected = quickRejectNoScissor(x, y,
2965 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2966
2967 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002968 if (transform && !transform->isIdentity()) {
2969 restore();
2970 }
Chet Haase48659092012-05-31 15:21:51 -07002971 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002972 }
2973
Romain Guy5bb3c732012-11-29 17:52:58 -08002974 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002975
Romain Guy87e2f7572012-09-24 11:37:12 -07002976 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002977 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002978
Romain Guy211370f2012-02-01 16:10:55 -08002979 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guye529ece2012-09-26 11:23:17 -07002980 SkiaColorFilter* oldFilter = mColorFilter;
2981 mColorFilter = layer->getColorFilter();
2982
Romain Guyc88e3572011-01-22 00:32:12 -08002983 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002984 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002985 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002986 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002987 setupDraw();
2988 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002989 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002990 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002991 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002992 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002993 setupDrawPureColorUniforms();
2994 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002995 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002996 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002997 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2998 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002999
Romain Guyd21b6e12011-11-30 20:21:23 -08003000 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003001 setupDrawModelViewTranslate(tx, ty,
3002 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003003 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003004 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003005 setupDrawModelViewTranslate(x, y,
3006 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3007 }
Romain Guyc88e3572011-01-22 00:32:12 -08003008 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08003009
Romain Guyc88e3572011-01-22 00:32:12 -08003010 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
3011 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08003012
Romain Guyc88e3572011-01-22 00:32:12 -08003013 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08003014
3015#if DEBUG_LAYERS_AS_REGIONS
3016 drawRegionRects(layer->region);
3017#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003018 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003019
Romain Guye529ece2012-09-26 11:23:17 -07003020 mColorFilter = oldFilter;
3021
Romain Guy5bb3c732012-11-29 17:52:58 -08003022 if (layer->debugDrawUpdate) {
3023 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003024 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3025 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3026 }
Romain Guyf219da52011-01-16 12:54:25 -08003027 }
Chet Haase48659092012-05-31 15:21:51 -07003028
Romain Guyb2e2f242012-10-17 18:18:35 -07003029 if (transform && !transform->isIdentity()) {
3030 restore();
3031 }
3032
Chet Haase48659092012-05-31 15:21:51 -07003033 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003034}
3035
Romain Guy6926c72e2010-07-12 20:20:03 -07003036///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003037// Shaders
3038///////////////////////////////////////////////////////////////////////////////
3039
3040void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07003041 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003042}
3043
Romain Guy06f96e22010-07-30 19:18:16 -07003044void OpenGLRenderer::setupShader(SkiaShader* shader) {
3045 mShader = shader;
3046 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003047 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07003048 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003049}
3050
Romain Guyd27977d2010-07-14 19:18:51 -07003051///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003052// Color filters
3053///////////////////////////////////////////////////////////////////////////////
3054
3055void OpenGLRenderer::resetColorFilter() {
3056 mColorFilter = NULL;
3057}
3058
3059void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
3060 mColorFilter = filter;
3061}
3062
3063///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003064// Drop shadow
3065///////////////////////////////////////////////////////////////////////////////
3066
3067void OpenGLRenderer::resetShadow() {
3068 mHasShadow = false;
3069}
3070
3071void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
3072 mHasShadow = true;
3073 mShadowRadius = radius;
3074 mShadowDx = dx;
3075 mShadowDy = dy;
3076 mShadowColor = color;
3077}
3078
3079///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003080// Draw filters
3081///////////////////////////////////////////////////////////////////////////////
3082
3083void OpenGLRenderer::resetPaintFilter() {
3084 mHasDrawFilter = false;
3085}
3086
3087void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
3088 mHasDrawFilter = true;
3089 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3090 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
3091}
3092
3093SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08003094 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08003095
3096 uint32_t flags = paint->getFlags();
3097
3098 mFilteredPaint = *paint;
3099 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
3100
3101 return &mFilteredPaint;
3102}
3103
3104///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c72e2010-07-12 20:20:03 -07003105// Drawing implementation
3106///////////////////////////////////////////////////////////////////////////////
3107
Romain Guy01d58e42011-01-19 21:54:02 -08003108void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3109 float x, float y, SkPaint* paint) {
3110 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3111 return;
3112 }
3113
3114 int alpha;
3115 SkXfermode::Mode mode;
3116 getAlphaAndMode(paint, &alpha, &mode);
3117
3118 setupDraw();
3119 setupDrawWithTexture(true);
3120 setupDrawAlpha8Color(paint->getColor(), alpha);
3121 setupDrawColorFilter();
3122 setupDrawShader();
3123 setupDrawBlending(true, mode);
3124 setupDrawProgram();
3125 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3126 setupDrawTexture(texture->id);
3127 setupDrawPureColorUniforms();
3128 setupDrawColorFilterUniforms();
3129 setupDrawShaderUniforms();
3130 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3131
3132 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3133
3134 finishDrawTexture();
3135}
3136
Romain Guyf607bdc2010-09-10 19:20:06 -07003137// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003138#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3139#define kStdUnderline_Offset (1.0f / 9.0f)
3140#define kStdUnderline_Thickness (1.0f / 18.0f)
3141
3142void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3143 float x, float y, SkPaint* paint) {
3144 // Handle underline and strike-through
3145 uint32_t flags = paint->getFlags();
3146 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003147 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003148 float underlineWidth = length;
3149 // If length is > 0.0f, we already measured the text for the text alignment
3150 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003151 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003152 }
3153
Romain Guy211370f2012-02-01 16:10:55 -08003154 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003155 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003156 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003157
Raph Levien8b4072d2012-07-30 15:50:00 -07003158 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003159 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003160
Romain Guyf6834472011-01-23 13:32:12 -08003161 int linesCount = 0;
3162 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3163 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3164
3165 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003166 float points[pointsCount];
3167 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003168
3169 if (flags & SkPaint::kUnderlineText_Flag) {
3170 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003171 points[currentPoint++] = left;
3172 points[currentPoint++] = top;
3173 points[currentPoint++] = left + underlineWidth;
3174 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003175 }
3176
3177 if (flags & SkPaint::kStrikeThruText_Flag) {
3178 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003179 points[currentPoint++] = left;
3180 points[currentPoint++] = top;
3181 points[currentPoint++] = left + underlineWidth;
3182 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003183 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003184
Romain Guy726aeba2011-06-01 14:52:00 -07003185 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003186
Romain Guy726aeba2011-06-01 14:52:00 -07003187 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003188 }
3189 }
3190}
3191
Romain Guy672433d2013-01-04 19:05:13 -08003192status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3193 if (mSnapshot->isIgnored()) {
3194 return DrawGlInfo::kStatusDone;
3195 }
3196
Romain Guy735738c2012-12-03 12:34:51 -08003197 int color = paint->getColor();
3198 // If a shader is set, preserve only the alpha
3199 if (mShader) {
3200 color |= 0x00ffffff;
3201 }
3202 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3203
3204 return drawColorRects(rects, count, color, mode);
3205}
3206
3207status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003208 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy735738c2012-12-03 12:34:51 -08003209
Romain Guy672433d2013-01-04 19:05:13 -08003210 float left = FLT_MAX;
3211 float top = FLT_MAX;
3212 float right = FLT_MIN;
3213 float bottom = FLT_MIN;
3214
3215 int vertexCount = 0;
3216 Vertex mesh[count * 6];
3217 Vertex* vertex = mesh;
3218
Chris Craik2af46352012-11-26 18:30:17 -08003219 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003220 float l = rects[index + 0];
3221 float t = rects[index + 1];
3222 float r = rects[index + 2];
3223 float b = rects[index + 3];
3224
Romain Guy8ce00302013-01-15 18:51:42 -08003225 if (ignoreTransform || !quickRejectNoScissor(left, top, right, bottom)) {
Romain Guy672433d2013-01-04 19:05:13 -08003226 Vertex::set(vertex++, l, b);
3227 Vertex::set(vertex++, l, t);
3228 Vertex::set(vertex++, r, t);
3229 Vertex::set(vertex++, l, b);
3230 Vertex::set(vertex++, r, t);
3231 Vertex::set(vertex++, r, b);
3232
3233 vertexCount += 6;
3234
3235 left = fminf(left, l);
3236 top = fminf(top, t);
3237 right = fmaxf(right, r);
3238 bottom = fmaxf(bottom, b);
3239 }
3240 }
3241
Romain Guy3bbacf22013-02-06 16:51:04 -08003242 if (count == 0 || (clip && quickReject(left, top, right, bottom))) {
Romain Guya362c692013-02-04 13:50:16 -08003243 return DrawGlInfo::kStatusDone;
3244 }
Romain Guy672433d2013-01-04 19:05:13 -08003245
Romain Guy672433d2013-01-04 19:05:13 -08003246 setupDraw();
3247 setupDrawNoTexture();
3248 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3249 setupDrawShader();
3250 setupDrawColorFilter();
3251 setupDrawBlending(mode);
3252 setupDrawProgram();
3253 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003254 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003255 setupDrawColorUniforms();
3256 setupDrawShaderUniforms();
3257 setupDrawColorFilterUniforms();
3258 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3259
Romain Guy8ce00302013-01-15 18:51:42 -08003260 if (dirty && hasLayer()) {
Romain Guy672433d2013-01-04 19:05:13 -08003261 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
3262 }
3263
3264 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3265
3266 return DrawGlInfo::kStatusDrew;
3267}
3268
Romain Guy026c5e162010-06-28 17:12:22 -07003269void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003270 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003271 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07003272 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003273 color |= 0x00ffffff;
3274 }
3275
Romain Guy70ca14e2010-12-13 18:24:33 -08003276 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003277 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003278 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003279 setupDrawShader();
3280 setupDrawColorFilter();
3281 setupDrawBlending(mode);
3282 setupDrawProgram();
3283 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3284 setupDrawColorUniforms();
3285 setupDrawShaderUniforms(ignoreTransform);
3286 setupDrawColorFilterUniforms();
3287 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003288
Romain Guyc95c8d62010-09-17 15:31:32 -07003289 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3290}
3291
Romain Guy82ba8142010-07-09 13:25:56 -07003292void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003293 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003294 int alpha;
3295 SkXfermode::Mode mode;
3296 getAlphaAndMode(paint, &alpha, &mode);
3297
Romain Guyd21b6e12011-11-30 20:21:23 -08003298 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003299
Romain Guy211370f2012-02-01 16:10:55 -08003300 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08003301 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
3302 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
3303
Romain Guyd21b6e12011-11-30 20:21:23 -08003304 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003305 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3306 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3307 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3308 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003309 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003310 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3311 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3312 GL_TRIANGLE_STRIP, gMeshCount);
3313 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003314}
3315
Romain Guybd6b79b2010-06-26 00:13:53 -07003316void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003317 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3318 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003319 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003320}
3321
3322void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003323 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003324 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003325 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003326
Romain Guy746b7402010-10-26 16:27:31 -07003327 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003328 setupDrawWithTexture();
3329 setupDrawColor(alpha, alpha, alpha, alpha);
3330 setupDrawColorFilter();
3331 setupDrawBlending(blend, mode, swapSrcDst);
3332 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003333 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003334 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003335 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003336 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003337 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003338 }
Romain Guy886b2752013-01-04 12:26:18 -08003339 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003340 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003341 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003342 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003343
Romain Guy6820ac82010-09-15 18:11:50 -07003344 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003345
3346 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003347}
3348
Romain Guy886b2752013-01-04 12:26:18 -08003349void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3350 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3351 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3352 bool ignoreTransform, bool dirty) {
3353
3354 setupDraw();
3355 setupDrawWithTexture(true);
3356 if (hasColor) {
3357 setupDrawAlpha8Color(color, alpha);
3358 }
3359 setupDrawColorFilter();
3360 setupDrawShader();
3361 setupDrawBlending(true, mode);
3362 setupDrawProgram();
3363 if (!dirty) setupDrawDirtyRegionsDisabled();
3364 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3365 setupDrawTexture(texture);
3366 setupDrawPureColorUniforms();
3367 setupDrawColorFilterUniforms();
3368 setupDrawShaderUniforms();
3369 setupDrawMesh(vertices, texCoords);
3370
3371 glDrawArrays(drawMode, 0, elementsCount);
3372
3373 finishDrawTexture();
3374}
3375
Romain Guya5aed0d2010-09-09 14:42:43 -07003376void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003377 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003378 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003379
Romain Guy82ba8142010-07-09 13:25:56 -07003380 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003381 // These blend modes are not supported by OpenGL directly and have
3382 // to be implemented using shaders. Since the shader will perform
3383 // the blending, turn blending off here
3384 // If the blend mode cannot be implemented using shaders, fall
3385 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003386 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003387 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003388 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003389 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003390
Romain Guy82bc7a72012-01-03 14:13:39 -08003391 if (mCaches.blend) {
3392 glDisable(GL_BLEND);
3393 mCaches.blend = false;
3394 }
3395
3396 return;
3397 } else {
3398 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003399 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003400 }
3401
3402 if (!mCaches.blend) {
3403 glEnable(GL_BLEND);
3404 }
3405
3406 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3407 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3408
3409 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3410 glBlendFunc(sourceMode, destMode);
3411 mCaches.lastSrcMode = sourceMode;
3412 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003413 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003414 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003415 glDisable(GL_BLEND);
3416 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003417 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003418}
3419
Romain Guy889f8d12010-07-29 14:37:42 -07003420bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003421 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003422 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003423 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003424 mCaches.currentProgram = program;
Romain Guy6926c72e2010-07-12 20:20:03 -07003425 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003426 }
Romain Guy6926c72e2010-07-12 20:20:03 -07003427 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003428}
3429
Romain Guy026c5e162010-06-28 17:12:22 -07003430void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003431 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003432 TextureVertex::setUV(v++, u1, v1);
3433 TextureVertex::setUV(v++, u2, v1);
3434 TextureVertex::setUV(v++, u1, v2);
3435 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003436}
3437
Chet Haase5c13d892010-10-08 08:37:55 -07003438void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003439 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003440 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003441}
3442
Romain Guy9d5316e2010-06-24 19:30:36 -07003443}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003444}; // namespace android