blob: 62f268dab2912bd3dbf5da71d5a665b1180e6430 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy03d58522012-02-24 17:54:07 -080024#include <SkPathMeasure.h>
Romain Guy694b5192010-07-21 21:33:20 -070025#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070028#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Romain Guy08aa2cb2011-03-17 11:06:57 -070030#include <private/hwui/DrawGlInfo.h>
31
Romain Guy5b3b3522010-10-27 18:57:51 -070032#include <ui/Rect.h>
33
Romain Guy85bf02f2010-06-22 13:11:24 -070034#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Chris Craik65cd6122012-12-10 17:56:27 -080036#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070037#include "Properties.h"
Romain Guya957eea2010-12-08 18:34:42 -080038#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070039
40namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070041namespace uirenderer {
42
43///////////////////////////////////////////////////////////////////////////////
44// Defines
45///////////////////////////////////////////////////////////////////////////////
46
Romain Guy759ea802010-09-16 20:49:46 -070047#define RAD_TO_DEG (180.0f / 3.14159265f)
48#define MIN_ANGLE 0.001f
49
Romain Guyf8773082012-07-12 18:01:00 -070050#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070051
Romain Guy713e1bb2012-10-16 18:44:09 -070052#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080053
Romain Guy9d5316e2010-06-24 19:30:36 -070054///////////////////////////////////////////////////////////////////////////////
55// Globals
56///////////////////////////////////////////////////////////////////////////////
57
Romain Guy889f8d12010-07-29 14:37:42 -070058/**
59 * Structure mapping Skia xfermodes to OpenGL blending factors.
60 */
61struct Blender {
62 SkXfermode::Mode mode;
63 GLenum src;
64 GLenum dst;
65}; // struct Blender
66
Romain Guy026c5e162010-06-28 17:12:22 -070067// In this array, the index of each Blender equals the value of the first
68// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
69static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070070 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
71 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
72 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
73 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
74 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
75 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
77 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
78 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
81 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
82 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050083 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070084 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070085};
Romain Guye4d01122010-06-16 18:44:05 -070086
Romain Guy87a76572010-09-13 18:11:21 -070087// This array contains the swapped version of each SkXfermode. For instance
88// this array's SrcOver blending mode is actually DstOver. You can refer to
89// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070090static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070091 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
92 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
93 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
94 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
95 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
96 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
97 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
100 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
101 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500104 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700105 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700106};
107
Romain Guyf6a11b82010-06-23 17:47:49 -0700108///////////////////////////////////////////////////////////////////////////////
109// Constructors/destructor
110///////////////////////////////////////////////////////////////////////////////
111
Romain Guy3bbacf22013-02-06 16:51:04 -0800112OpenGLRenderer::OpenGLRenderer():
113 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700114 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700115 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700116 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800117 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700118
Romain Guyac670c02010-07-27 17:39:27 -0700119 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
120
Romain Guyae5575b2010-07-29 18:48:04 -0700121 mFirstSnapshot = new Snapshot;
Romain Guy87e2f7572012-09-24 11:37:12 -0700122
123 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700124}
125
Romain Guy85bf02f2010-06-22 13:11:24 -0700126OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700127 // The context has already been destroyed at this point, do not call
128 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700129}
130
Romain Guy87e2f7572012-09-24 11:37:12 -0700131void OpenGLRenderer::initProperties() {
132 char property[PROPERTY_VALUE_MAX];
133 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
134 mScissorOptimizationDisabled = !strcasecmp(property, "true");
135 INIT_LOGD(" Scissor optimization %s",
136 mScissorOptimizationDisabled ? "disabled" : "enabled");
137 } else {
138 INIT_LOGD(" Scissor optimization enabled");
139 }
Romain Guy13631f32012-01-30 17:41:55 -0800140}
141
142///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700143// Setup
144///////////////////////////////////////////////////////////////////////////////
145
Romain Guyef359272013-01-31 19:07:29 -0800146void OpenGLRenderer::setName(const char* name) {
147 if (name) {
148 mName.setTo(name);
149 } else {
150 mName.clear();
151 }
152}
153
154const char* OpenGLRenderer::getName() const {
155 return mName.string();
156}
157
Romain Guy49c5fc02012-05-15 11:10:01 -0700158bool OpenGLRenderer::isDeferred() {
159 return false;
160}
161
Romain Guy85bf02f2010-06-22 13:11:24 -0700162void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700163 initViewport(width, height);
164
165 glDisable(GL_DITHER);
166 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
167
168 glEnableVertexAttribArray(Program::kBindingPosition);
169}
170
171void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700172 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700173
174 mWidth = width;
175 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700176
177 mFirstSnapshot->height = height;
178 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700179}
180
Romain Guy7c25aab2012-10-18 15:05:02 -0700181status_t OpenGLRenderer::prepare(bool opaque) {
Chet Haase44b2fe32012-06-06 19:03:58 -0700182 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800183}
184
Romain Guyc3fedaf2013-01-29 17:26:25 -0800185status_t OpenGLRenderer::prepareDirty(float left, float top,
186 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800187 mCaches.clearGarbage();
188
Romain Guy8aef54f2010-09-01 15:13:49 -0700189 mSnapshot = new Snapshot(mFirstSnapshot,
190 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800191 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700192 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700193
Romain Guy7d7b5492011-01-24 16:33:45 -0800194 mSnapshot->setClip(left, top, right, bottom);
Romain Guy41308e22012-10-22 20:02:43 -0700195 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700196
Romain Guy11cb6422012-09-21 00:39:43 -0700197 updateLayers();
198
Romain Guydcfc8362013-01-03 13:08:57 -0800199 discardFramebuffer(left, top, right, bottom);
Romain Guy45e4c3d2012-09-11 17:17:07 -0700200
Romain Guyddf74372012-05-22 14:07:07 -0700201 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800202
Romain Guy54c1a642012-09-27 17:55:46 -0700203 // Functors break the tiling extension in pretty spectacular ways
204 // This ensures we don't use tiling when a functor is going to be
205 // invoked during the frame
206 mSuppressTiling = mCaches.hasRegisteredFunctors();
207
Romain Guy2b7028e2012-09-19 17:25:38 -0700208 mTilingSnapshot = mSnapshot;
Romain Guy57b52682012-09-20 17:38:46 -0700209 startTiling(mTilingSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700210
Romain Guy7c450aa2012-09-21 19:15:00 -0700211 debugOverdraw(true, true);
212
Romain Guy7c25aab2012-10-18 15:05:02 -0700213 return clear(left, top, right, bottom, opaque);
214}
215
Romain Guydcfc8362013-01-03 13:08:57 -0800216void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
217 // If we know that we are going to redraw the entire framebuffer,
218 // perform a discard to let the driver know we don't need to preserve
219 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800220 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800221 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800222 const bool isFbo = getTargetFbo() == 0;
223 const GLenum attachments[] = {
224 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
225 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800226 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
227 }
228}
229
Romain Guy7c25aab2012-10-18 15:05:02 -0700230status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700231 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700232 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700233 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700234 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700235 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700236 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700237
Romain Guy7c25aab2012-10-18 15:05:02 -0700238 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700239 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700240}
241
242void OpenGLRenderer::syncState() {
243 glViewport(0, 0, mWidth, mHeight);
244
245 if (mCaches.blend) {
246 glEnable(GL_BLEND);
247 } else {
248 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700249 }
Romain Guybb9524b2010-06-22 18:56:38 -0700250}
251
Romain Guy57b52682012-09-20 17:38:46 -0700252void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700253 if (!mSuppressTiling) {
254 Rect* clip = mTilingSnapshot->clipRect;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800255 if (s->flags & Snapshot::kFlagFboTarget) {
256 clip = &s->layer->clipRect;
Romain Guy54c1a642012-09-27 17:55:46 -0700257 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700258
Romain Guyc3fedaf2013-01-29 17:26:25 -0800259 startTiling(*clip, s->height, opaque);
260 }
261}
262
263void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
264 if (!mSuppressTiling) {
265 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
266 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700267 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700268}
269
270void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700271 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700272}
273
Romain Guyb025b9c2010-09-16 14:16:48 -0700274void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700275 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700276 endTiling();
277
Romain Guy11cb6422012-09-21 00:39:43 -0700278 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700279#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700280 GLenum status = GL_NO_ERROR;
281 while ((status = glGetError()) != GL_NO_ERROR) {
282 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
283 switch (status) {
284 case GL_INVALID_ENUM:
285 ALOGE(" GL_INVALID_ENUM");
286 break;
287 case GL_INVALID_VALUE:
288 ALOGE(" GL_INVALID_VALUE");
289 break;
290 case GL_INVALID_OPERATION:
291 ALOGE(" GL_INVALID_OPERATION");
292 break;
293 case GL_OUT_OF_MEMORY:
294 ALOGE(" Out of memory!");
295 break;
296 }
Romain Guya07105b2011-01-10 21:14:18 -0800297 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700298#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700299
Romain Guyc15008e2010-11-10 11:59:15 -0800300#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800301 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700302#else
303 if (mCaches.getDebugLevel() & kDebugMemory) {
304 mCaches.dumpMemoryUsage();
305 }
Romain Guyc15008e2010-11-10 11:59:15 -0800306#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700307 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700308}
309
Romain Guy6c319ca2011-01-11 14:29:25 -0800310void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700311 if (mCaches.currentProgram) {
312 if (mCaches.currentProgram->isInUse()) {
313 mCaches.currentProgram->remove();
314 mCaches.currentProgram = NULL;
315 }
316 }
Romain Guy50c0f092010-10-19 11:42:22 -0700317 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800318 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800319 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800320 mCaches.disbaleTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700321 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700322}
323
Romain Guy6c319ca2011-01-11 14:29:25 -0800324void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800325 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800326 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700327 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700328 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700329
Romain Guy3e263fa2011-12-12 16:47:48 -0800330 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
331
Chet Haase80250612012-08-15 13:46:54 -0700332 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700333 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800334 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700335 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700336
Romain Guya1d3c912011-12-13 14:55:06 -0800337 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700338
Romain Guy50c0f092010-10-19 11:42:22 -0700339 mCaches.blend = true;
340 glEnable(GL_BLEND);
341 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
342 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700343}
344
Romain Guy35643dd2012-09-18 15:40:58 -0700345void OpenGLRenderer::resumeAfterLayer() {
346 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
347 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
348 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700349 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700350
351 mCaches.resetScissor();
352 dirtyClip();
353}
354
Romain Guyba6be8a2012-04-23 18:22:09 -0700355void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700356 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700357}
358
359void OpenGLRenderer::attachFunctor(Functor* functor) {
360 mFunctors.add(functor);
361}
362
Romain Guy8f3b8e32012-03-27 16:33:45 -0700363status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
364 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700365 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700366
Romain Guyba6be8a2012-04-23 18:22:09 -0700367 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800368 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700369 SortedVector<Functor*> functors(mFunctors);
370 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700371
Romain Guyba6be8a2012-04-23 18:22:09 -0700372 DrawGlInfo info;
373 info.clipLeft = 0;
374 info.clipTop = 0;
375 info.clipRight = 0;
376 info.clipBottom = 0;
377 info.isLayer = false;
378 info.width = 0;
379 info.height = 0;
380 memset(info.transform, 0, sizeof(float) * 16);
381
382 for (size_t i = 0; i < count; i++) {
383 Functor* f = functors.itemAt(i);
384 result |= (*f)(DrawGlInfo::kModeProcess, &info);
385
Chris Craikc2c95432012-04-25 15:13:52 -0700386 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700387 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
388 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700389 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700390
Chris Craikc2c95432012-04-25 15:13:52 -0700391 if (result & DrawGlInfo::kStatusInvoke) {
392 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700393 }
394 }
Chris Craikd15321b2012-11-28 14:45:04 -0800395 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700396 }
397
398 return result;
399}
400
401status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800402 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700403 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700404
Romain Guy8a4ac612012-07-17 17:32:48 -0700405 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800406 if (mDirtyClip) {
407 setScissorFromClip();
408 }
Romain Guyd643bb52011-03-01 14:55:21 -0800409
Romain Guy80911b82011-03-16 15:30:12 -0700410 Rect clip(*mSnapshot->clipRect);
411 clip.snapToPixelBoundaries();
412
Romain Guyd643bb52011-03-01 14:55:21 -0800413 // Since we don't know what the functor will draw, let's dirty
414 // tne entire clip region
415 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800416 dirtyLayerUnchecked(clip, getRegion());
417 }
Romain Guyd643bb52011-03-01 14:55:21 -0800418
Romain Guy08aa2cb2011-03-17 11:06:57 -0700419 DrawGlInfo info;
420 info.clipLeft = clip.left;
421 info.clipTop = clip.top;
422 info.clipRight = clip.right;
423 info.clipBottom = clip.bottom;
424 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700425 info.width = getSnapshot()->viewport.getWidth();
426 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700427 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700428
Chet Haase48659092012-05-31 15:21:51 -0700429 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800430
Romain Guy8f3b8e32012-03-27 16:33:45 -0700431 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700432 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800433 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700434
Chris Craik65924a32012-04-05 17:52:11 -0700435 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700436 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700437 }
Romain Guycabfcc12011-03-07 18:06:46 -0800438 }
439
Chet Haasedaf98e92011-01-10 14:10:36 -0800440 resume();
Romain Guy65549432012-03-26 16:45:05 -0700441 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800442}
443
Romain Guyf6a11b82010-06-23 17:47:49 -0700444///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700445// Debug
446///////////////////////////////////////////////////////////////////////////////
447
448void OpenGLRenderer::startMark(const char* name) const {
449 mCaches.startMark(0, name);
450}
451
452void OpenGLRenderer::endMark() const {
453 mCaches.endMark();
454}
455
456void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
457 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
458 if (clear) {
459 mCaches.disableScissor();
460 mCaches.stencil.clear();
461 }
462 if (enable) {
463 mCaches.stencil.enableDebugWrite();
464 } else {
465 mCaches.stencil.disable();
466 }
467 }
468}
469
470void OpenGLRenderer::renderOverdraw() {
471 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
472 const Rect* clip = mTilingSnapshot->clipRect;
473
474 mCaches.enableScissor();
475 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
476 clip->right - clip->left, clip->bottom - clip->top);
477
478 mCaches.stencil.enableDebugTest(2);
479 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
480 mCaches.stencil.enableDebugTest(3);
481 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
482 mCaches.stencil.enableDebugTest(4);
483 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
484 mCaches.stencil.enableDebugTest(4, true);
485 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
486 mCaches.stencil.disable();
487 }
488}
489
490///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700491// Layers
492///////////////////////////////////////////////////////////////////////////////
493
494bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
495 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
496 OpenGLRenderer* renderer = layer->renderer;
497 Rect& dirty = layer->dirtyRect;
498
Romain Guy7c450aa2012-09-21 19:15:00 -0700499 if (inFrame) {
500 endTiling();
501 debugOverdraw(false, false);
502 }
Romain Guy11cb6422012-09-21 00:39:43 -0700503
504 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
505 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
506 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
507 renderer->finish();
508
509 if (inFrame) {
510 resumeAfterLayer();
511 startTiling(mSnapshot);
512 }
513
514 dirty.setEmpty();
515 layer->deferredUpdateScheduled = false;
516 layer->renderer = NULL;
517 layer->displayList = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -0800518 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700519
520 return true;
521 }
522
523 return false;
524}
525
526void OpenGLRenderer::updateLayers() {
527 int count = mLayerUpdates.size();
528 if (count > 0) {
529 startMark("Layer Updates");
530
531 // Note: it is very important to update the layers in reverse order
532 for (int i = count - 1; i >= 0; i--) {
533 Layer* layer = mLayerUpdates.itemAt(i);
534 updateLayer(layer, false);
535 mCaches.resourceCache.decrementRefcount(layer);
536 }
537 mLayerUpdates.clear();
538
539 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
540 endMark();
541 }
542}
543
544void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
545 if (layer) {
546 mLayerUpdates.push_back(layer);
547 mCaches.resourceCache.incrementRefcount(layer);
548 }
549}
550
551void OpenGLRenderer::clearLayerUpdates() {
552 size_t count = mLayerUpdates.size();
553 if (count > 0) {
554 mCaches.resourceCache.lock();
555 for (size_t i = 0; i < count; i++) {
556 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
557 }
558 mCaches.resourceCache.unlock();
559 mLayerUpdates.clear();
560 }
561}
562
563///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700564// State management
565///////////////////////////////////////////////////////////////////////////////
566
Romain Guybb9524b2010-06-22 18:56:38 -0700567int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700568 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700569}
570
571int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700572 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700573}
574
575void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700576 if (mSaveCount > 1) {
577 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700578 }
Romain Guybb9524b2010-06-22 18:56:38 -0700579}
580
581void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700582 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700583
Romain Guy8fb95422010-08-17 18:38:51 -0700584 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700585 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700586 }
Romain Guybb9524b2010-06-22 18:56:38 -0700587}
588
Romain Guy8aef54f2010-09-01 15:13:49 -0700589int OpenGLRenderer::saveSnapshot(int flags) {
590 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700591 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700592}
593
594bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700595 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700596 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700597 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700598
Romain Guybd6b79b2010-06-26 00:13:53 -0700599 sp<Snapshot> current = mSnapshot;
600 sp<Snapshot> previous = mSnapshot->previous;
601
Romain Guyeb993562010-10-05 18:14:38 -0700602 if (restoreOrtho) {
603 Rect& r = previous->viewport;
604 glViewport(r.left, r.top, r.right, r.bottom);
605 mOrthoMatrix.load(current->orthoMatrix);
606 }
607
Romain Guy8b55f372010-08-18 17:10:07 -0700608 mSaveCount--;
609 mSnapshot = previous;
610
Romain Guy2542d192010-08-18 11:47:12 -0700611 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700612 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700613 }
Romain Guy2542d192010-08-18 11:47:12 -0700614
Romain Guy5ec99242010-11-03 16:19:08 -0700615 if (restoreLayer) {
616 composeLayer(current, previous);
617 }
618
Romain Guy2542d192010-08-18 11:47:12 -0700619 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700620}
621
Romain Guyf6a11b82010-06-23 17:47:49 -0700622///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700623// Layers
624///////////////////////////////////////////////////////////////////////////////
625
626int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700627 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700628 const GLuint previousFbo = mSnapshot->fbo;
629 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700630
Romain Guyaf636eb2010-12-09 17:47:21 -0800631 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700632 int alpha = 255;
633 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700634
Romain Guye45362c2010-11-03 19:58:32 -0700635 if (p) {
636 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700637 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700638 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700639 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700640 }
Romain Guyd55a8612010-06-28 17:42:46 -0700641
Chet Haased48885a2012-08-28 17:43:28 -0700642 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700643 }
Romain Guyd55a8612010-06-28 17:42:46 -0700644
645 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700646}
647
648int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
649 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700650 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700651 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700652 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700653 SkPaint paint;
654 paint.setAlpha(alpha);
655 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700656 }
Romain Guyd55a8612010-06-28 17:42:46 -0700657}
Romain Guybd6b79b2010-06-26 00:13:53 -0700658
Romain Guy1c740bc2010-09-13 18:00:09 -0700659/**
660 * Layers are viewed by Skia are slightly different than layers in image editing
661 * programs (for instance.) When a layer is created, previously created layers
662 * and the frame buffer still receive every drawing command. For instance, if a
663 * layer is created and a shape intersecting the bounds of the layers and the
664 * framebuffer is draw, the shape will be drawn on both (unless the layer was
665 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
666 *
667 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
668 * texture. Unfortunately, this is inefficient as it requires every primitive to
669 * be drawn n + 1 times, where n is the number of active layers. In practice this
670 * means, for every primitive:
671 * - Switch active frame buffer
672 * - Change viewport, clip and projection matrix
673 * - Issue the drawing
674 *
675 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700676 * To avoid this, layers are implemented in a different way here, at least in the
677 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
678 * is set. When this flag is set we can redirect all drawing operations into a
679 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700680 *
681 * This implementation relies on the frame buffer being at least RGBA 8888. When
682 * a layer is created, only a texture is created, not an FBO. The content of the
683 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700684 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700685 * buffer and drawing continues as normal. This technique therefore treats the
686 * frame buffer as a scratch buffer for the layers.
687 *
688 * To compose the layers back onto the frame buffer, each layer texture
689 * (containing the original frame buffer data) is drawn as a simple quad over
690 * the frame buffer. The trick is that the quad is set as the composition
691 * destination in the blending equation, and the frame buffer becomes the source
692 * of the composition.
693 *
694 * Drawing layers with an alpha value requires an extra step before composition.
695 * An empty quad is drawn over the layer's region in the frame buffer. This quad
696 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
697 * quad is used to multiply the colors in the frame buffer. This is achieved by
698 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
699 * GL_ZERO, GL_SRC_ALPHA.
700 *
701 * Because glCopyTexImage2D() can be slow, an alternative implementation might
702 * be use to draw a single clipped layer. The implementation described above
703 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700704 *
705 * (1) The frame buffer is actually not cleared right away. To allow the GPU
706 * to potentially optimize series of calls to glCopyTexImage2D, the frame
707 * buffer is left untouched until the first drawing operation. Only when
708 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700709 */
Chet Haased48885a2012-08-28 17:43:28 -0700710bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
711 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700712 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700713 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700714
Romain Guyeb993562010-10-05 18:14:38 -0700715 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
716
Romain Guyf607bdc2010-09-10 19:20:06 -0700717 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700718 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700719 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700720 Rect untransformedBounds(bounds);
721 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700722
Chet Haased48885a2012-08-28 17:43:28 -0700723 // Layers only make sense if they are in the framebuffer's bounds
724 if (bounds.intersect(*mSnapshot->clipRect)) {
725 // We cannot work with sub-pixels in this case
726 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700727
Chet Haased48885a2012-08-28 17:43:28 -0700728 // When the layer is not an FBO, we may use glCopyTexImage so we
729 // need to make sure the layer does not extend outside the bounds
730 // of the framebuffer
731 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700732 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700733 } else if (fboLayer) {
734 clip.set(bounds);
735 mat4 inverse;
736 inverse.loadInverse(*mSnapshot->transform);
737 inverse.mapRect(clip);
738 clip.snapToPixelBoundaries();
739 if (clip.intersect(untransformedBounds)) {
740 clip.translate(-left, -top);
741 bounds.set(untransformedBounds);
742 } else {
743 clip.setEmpty();
744 }
Romain Guyad37cd32011-03-15 11:12:25 -0700745 }
Chet Haased48885a2012-08-28 17:43:28 -0700746 } else {
747 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700748 }
Romain Guybf434112010-09-16 14:40:17 -0700749
Romain Guy746b7402010-10-26 16:27:31 -0700750 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700751 bounds.getHeight() > mCaches.maxTextureSize ||
752 (fboLayer && clip.isEmpty())) {
753 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700754 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700755 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700756 }
757
758 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700759 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700760 return false;
761 }
Romain Guyf18fd992010-07-08 11:45:51 -0700762
Romain Guya1d3c912011-12-13 14:55:06 -0800763 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700764 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain 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 }
Romain Guye67307c2013-02-11 18:01:20 -08001182 // We must clear the list of dirty rects before we
1183 // call setupDraw() to prevent stencil setup to do
1184 // the same thing again
1185 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001186
1187 setupDraw(false);
1188 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1189 setupDrawBlending(true, SkXfermode::kClear_Mode);
1190 setupDrawProgram();
1191 setupDrawPureColorUniforms();
1192 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001193 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001194
Romain Guy54be1cd2011-06-13 19:04:27 -07001195 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001196
1197 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001198 } else {
1199 for (uint32_t i = 0; i < count; i++) {
1200 delete mLayers.itemAt(i);
1201 }
Romain Guye67307c2013-02-11 18:01:20 -08001202 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001203 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001204}
1205
Romain Guybd6b79b2010-06-26 00:13:53 -07001206///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001207// Transforms
1208///////////////////////////////////////////////////////////////////////////////
1209
1210void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001211 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001212}
1213
1214void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001215 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001216}
1217
1218void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001219 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001220}
1221
Romain Guy807daf72011-01-18 11:19:19 -08001222void OpenGLRenderer::skew(float sx, float sy) {
1223 mSnapshot->transform->skew(sx, sy);
1224}
1225
Romain Guyf6a11b82010-06-23 17:47:49 -07001226void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001227 if (matrix) {
1228 mSnapshot->transform->load(*matrix);
1229 } else {
1230 mSnapshot->transform->loadIdentity();
1231 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001232}
1233
1234void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001235 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001236}
1237
1238void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001239 SkMatrix transform;
1240 mSnapshot->transform->copyTo(transform);
1241 transform.preConcat(*matrix);
1242 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001243}
1244
1245///////////////////////////////////////////////////////////////////////////////
1246// Clipping
1247///////////////////////////////////////////////////////////////////////////////
1248
Romain Guybb9524b2010-06-22 18:56:38 -07001249void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001250 Rect clip(*mSnapshot->clipRect);
1251 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001252
Romain Guy8a4ac612012-07-17 17:32:48 -07001253 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1254 clip.getWidth(), clip.getHeight())) {
1255 mDirtyClip = false;
1256 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001257}
1258
Romain Guy8ce00302013-01-15 18:51:42 -08001259void OpenGLRenderer::ensureStencilBuffer() {
1260 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1261 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1262 // just hope we have one when hasLayer() returns false.
1263 if (hasLayer()) {
1264 attachStencilBufferToLayer(mSnapshot->layer);
1265 }
1266}
1267
1268void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1269 // The layer's FBO is already bound when we reach this stage
1270 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001271 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1272 // is attached after we initiated tiling. We must turn it off,
1273 // attach the new render buffer then turn tiling back on
1274 endTiling();
1275
Romain Guy3bbacf22013-02-06 16:51:04 -08001276 RenderBuffer* buffer = new RenderBuffer(
1277 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
1278 buffer->bind();
1279 buffer->allocate();
Romain Guy2055aba2013-01-18 16:42:51 -08001280
Romain Guy8ce00302013-01-15 18:51:42 -08001281 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001282
Romain Guyf735c8e2013-01-31 17:45:55 -08001283 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001284 }
1285}
1286
1287void OpenGLRenderer::setStencilFromClip() {
1288 if (!mCaches.debugOverdraw) {
1289 if (!mSnapshot->clipRegion->isEmpty()) {
1290 // NOTE: The order here is important, we must set dirtyClip to false
1291 // before any draw call to avoid calling back into this method
1292 mDirtyClip = false;
1293
1294 ensureStencilBuffer();
1295
1296 mCaches.stencil.enableWrite();
1297
1298 // Clear the stencil but first make sure we restrict drawing
1299 // to the region's bounds
1300 bool resetScissor = mCaches.enableScissor();
1301 if (resetScissor) {
1302 // The scissor was not set so we now need to update it
1303 setScissorFromClip();
1304 }
1305 mCaches.stencil.clear();
1306 if (resetScissor) mCaches.disableScissor();
1307
1308 // NOTE: We could use the region contour path to generate a smaller mesh
1309 // Since we are using the stencil we could use the red book path
1310 // drawing technique. It might increase bandwidth usage though.
1311
1312 // The last parameter is important: we are not drawing in the color buffer
1313 // so we don't want to dirty the current layer, if any
1314 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1315
1316 mCaches.stencil.enableTest();
1317 } else {
1318 mCaches.stencil.disable();
1319 }
1320 }
1321}
1322
Romain Guy9d5316e2010-06-24 19:30:36 -07001323const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001324 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001325}
1326
Romain Guy8a4ac612012-07-17 17:32:48 -07001327bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1328 if (mSnapshot->isIgnored()) {
1329 return true;
1330 }
1331
1332 Rect r(left, top, right, bottom);
1333 mSnapshot->transform->mapRect(r);
1334 r.snapToPixelBoundaries();
1335
1336 Rect clipRect(*mSnapshot->clipRect);
1337 clipRect.snapToPixelBoundaries();
1338
1339 return !clipRect.intersects(r);
1340}
1341
Romain Guy35643dd2012-09-18 15:40:58 -07001342bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1343 Rect& transformed, Rect& clip) {
1344 if (mSnapshot->isIgnored()) {
1345 return true;
1346 }
1347
1348 transformed.set(left, top, right, bottom);
1349 mSnapshot->transform->mapRect(transformed);
1350 transformed.snapToPixelBoundaries();
1351
1352 clip.set(*mSnapshot->clipRect);
1353 clip.snapToPixelBoundaries();
1354
1355 return !clip.intersects(transformed);
1356}
1357
Romain Guy672433d2013-01-04 19:05:13 -08001358bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1359 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001360 if (paint->getStyle() != SkPaint::kFill_Style) {
1361 float outset = paint->getStrokeWidth() * 0.5f;
1362 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1363 } else {
1364 return quickReject(left, top, right, bottom);
1365 }
1366}
1367
Romain Guyc7d53492010-06-25 13:41:57 -07001368bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001369 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001370 return true;
1371 }
1372
Romain Guy1d83e192010-08-17 11:37:00 -07001373 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001374 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001375 r.snapToPixelBoundaries();
1376
1377 Rect clipRect(*mSnapshot->clipRect);
1378 clipRect.snapToPixelBoundaries();
1379
Romain Guy586cae32012-07-13 15:28:31 -07001380 bool rejected = !clipRect.intersects(r);
1381 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001382 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001383 }
1384
1385 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001386}
1387
Romain Guy8ce00302013-01-15 18:51:42 -08001388void OpenGLRenderer::debugClip() {
1389#if DEBUG_CLIP_REGIONS
1390 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1391 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1392 }
1393#endif
1394}
1395
Romain Guy079ba2c2010-07-16 14:12:24 -07001396bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001397 if (CC_LIKELY(mSnapshot->transform->rectToRect())) {
1398 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1399 if (clipped) {
1400 dirtyClip();
1401 }
1402 return !mSnapshot->clipRect->isEmpty();
1403 }
1404
1405 SkPath path;
1406 path.addRect(left, top, right, bottom);
1407
1408 return clipPath(&path, op);
1409}
1410
1411bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1412 SkMatrix transform;
1413 mSnapshot->transform->copyTo(transform);
1414
1415 SkPath transformed;
1416 path->transform(transform, &transformed);
1417
1418 SkRegion clip;
1419 if (!mSnapshot->clipRegion->isEmpty()) {
1420 clip.setRegion(*mSnapshot->clipRegion);
1421 } else {
1422 Rect* bounds = mSnapshot->clipRect;
1423 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1424 }
1425
1426 SkRegion region;
1427 region.setPath(transformed, clip);
1428
1429 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001430 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001431 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001432 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001433 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001434}
1435
Romain Guy735738c2012-12-03 12:34:51 -08001436bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001437 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1438 if (clipped) {
1439 dirtyClip();
1440 }
1441 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001442}
1443
Chet Haasea23eed82012-04-12 15:19:04 -07001444Rect* OpenGLRenderer::getClipRect() {
1445 return mSnapshot->clipRect;
1446}
1447
Romain Guyf6a11b82010-06-23 17:47:49 -07001448///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001449// Drawing commands
1450///////////////////////////////////////////////////////////////////////////////
1451
Romain Guy54be1cd2011-06-13 19:04:27 -07001452void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001453 // TODO: It would be best if we could do this before quickReject()
1454 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001455 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001456 // Make sure setScissor & setStencil happen at the beginning of
1457 // this method
Romain Guy70ca14e2010-12-13 18:24:33 -08001458 if (mDirtyClip) {
1459 setScissorFromClip();
Romain Guy8ce00302013-01-15 18:51:42 -08001460 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001461 }
1462 mDescription.reset();
1463 mSetShaderColor = false;
1464 mColorSet = false;
1465 mColorA = mColorR = mColorG = mColorB = 0.0f;
1466 mTextureUnit = 0;
1467 mTrackDirtyRegions = true;
1468}
1469
1470void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1471 mDescription.hasTexture = true;
1472 mDescription.hasAlpha8Texture = isAlpha8;
1473}
1474
Romain Guyaa6c24c2011-04-28 18:40:04 -07001475void OpenGLRenderer::setupDrawWithExternalTexture() {
1476 mDescription.hasExternalTexture = true;
1477}
1478
Romain Guy15bc6432011-12-13 13:11:32 -08001479void OpenGLRenderer::setupDrawNoTexture() {
1480 mCaches.disbaleTexCoordsVertexArray();
1481}
1482
Chris Craik710f46d2012-09-17 17:25:49 -07001483void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001484 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001485}
1486
Romain Guyed6fcb02011-03-21 13:11:28 -07001487void OpenGLRenderer::setupDrawPoint(float pointSize) {
1488 mDescription.isPoint = true;
1489 mDescription.pointSize = pointSize;
1490}
1491
Romain Guy8d0d4782010-12-14 20:13:35 -08001492void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1493 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001494 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1495 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1496 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001497 mColorSet = true;
1498 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1499}
1500
Romain Guy86568192010-12-14 15:55:39 -08001501void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1502 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001503 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1504 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1505 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001506 mColorSet = true;
1507 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1508}
1509
Romain Guy41210632012-07-16 17:04:24 -07001510void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1511 mCaches.fontRenderer->describe(mDescription, paint);
1512}
1513
Romain Guy70ca14e2010-12-13 18:24:33 -08001514void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1515 mColorA = a;
1516 mColorR = r;
1517 mColorG = g;
1518 mColorB = b;
1519 mColorSet = true;
1520 mSetShaderColor = mDescription.setColor(r, g, b, a);
1521}
1522
1523void OpenGLRenderer::setupDrawShader() {
1524 if (mShader) {
Romain Guy3bbacf22013-02-06 16:51:04 -08001525 mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001526 }
1527}
1528
1529void OpenGLRenderer::setupDrawColorFilter() {
1530 if (mColorFilter) {
Romain Guy3bbacf22013-02-06 16:51:04 -08001531 mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001532 }
1533}
1534
Romain Guyf09ef512011-05-27 11:43:46 -07001535void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1536 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1537 mColorA = 1.0f;
1538 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001539 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001540 }
1541}
1542
Romain Guy70ca14e2010-12-13 18:24:33 -08001543void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001544 // When the blending mode is kClear_Mode, we need to use a modulate color
1545 // argb=1,0,0,0
1546 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001547 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1548 mDescription, swapSrcDst);
1549}
1550
1551void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001552 // When the blending mode is kClear_Mode, we need to use a modulate color
1553 // argb=1,0,0,0
1554 accountForClear(mode);
Romain Guye83221c2012-09-24 16:01:35 -07001555 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()) ||
1556 (mColorFilter && mColorFilter->blend()), mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001557}
1558
1559void OpenGLRenderer::setupDrawProgram() {
1560 useProgram(mCaches.programCache.get(mDescription));
1561}
1562
1563void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1564 mTrackDirtyRegions = false;
1565}
1566
1567void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1568 bool ignoreTransform) {
1569 mModelView.loadTranslate(left, top, 0.0f);
1570 if (!ignoreTransform) {
1571 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1572 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1573 } else {
1574 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1575 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1576 }
1577}
1578
Chet Haase8a5cc922011-04-26 07:28:09 -07001579void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1580 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001581}
1582
Romain Guy70ca14e2010-12-13 18:24:33 -08001583void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1584 bool ignoreTransform, bool ignoreModelView) {
1585 if (!ignoreModelView) {
1586 mModelView.loadTranslate(left, top, 0.0f);
1587 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001588 } else {
1589 mModelView.loadIdentity();
1590 }
Romain Guy86568192010-12-14 15:55:39 -08001591 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1592 if (!ignoreTransform) {
1593 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1594 if (mTrackDirtyRegions && dirty) {
1595 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1596 }
1597 } else {
1598 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1599 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1600 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001601}
1602
Romain Guyed6fcb02011-03-21 13:11:28 -07001603void OpenGLRenderer::setupDrawPointUniforms() {
1604 int slot = mCaches.currentProgram->getUniform("pointSize");
1605 glUniform1f(slot, mDescription.pointSize);
1606}
1607
Romain Guy70ca14e2010-12-13 18:24:33 -08001608void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001609 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001610 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1611 }
1612}
1613
Romain Guy86568192010-12-14 15:55:39 -08001614void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001615 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001616 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001617 }
1618}
1619
Romain Guy70ca14e2010-12-13 18:24:33 -08001620void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1621 if (mShader) {
1622 if (ignoreTransform) {
1623 mModelView.loadInverse(*mSnapshot->transform);
1624 }
1625 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1626 }
1627}
1628
Romain Guy8d0d4782010-12-14 20:13:35 -08001629void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1630 if (mShader) {
1631 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1632 }
1633}
1634
Romain Guy70ca14e2010-12-13 18:24:33 -08001635void OpenGLRenderer::setupDrawColorFilterUniforms() {
1636 if (mColorFilter) {
1637 mColorFilter->setupProgram(mCaches.currentProgram);
1638 }
1639}
1640
Romain Guy41210632012-07-16 17:04:24 -07001641void OpenGLRenderer::setupDrawTextGammaUniforms() {
1642 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1643}
1644
Romain Guy70ca14e2010-12-13 18:24:33 -08001645void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001646 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001647 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001648 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001649}
1650
1651void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1652 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001653 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001654 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001655}
1656
Romain Guyaa6c24c2011-04-28 18:40:04 -07001657void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1658 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001659 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001660 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001661}
1662
Romain Guy8f0095c2011-05-02 17:24:22 -07001663void OpenGLRenderer::setupDrawTextureTransform() {
1664 mDescription.hasTextureTransform = true;
1665}
1666
1667void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001668 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1669 GL_FALSE, &transform.data[0]);
1670}
1671
Romain Guy70ca14e2010-12-13 18:24:33 -08001672void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001673 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001674 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001675 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001676 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001677 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001678 }
Romain Guyd71dd362011-12-12 19:03:35 -08001679
Chris Craikcb4d6002012-09-25 12:00:29 -07001680 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001681 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001682 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001683 }
1684
1685 mCaches.unbindIndicesBuffer();
1686}
1687
1688void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1689 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001690 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001691 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001692 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001693 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001694}
1695
Chet Haase5b0200b2011-04-13 17:58:08 -07001696void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001697 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001698 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001699 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001700}
1701
Romain Guy70ca14e2010-12-13 18:24:33 -08001702void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001703}
1704
1705///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001706// Drawing
1707///////////////////////////////////////////////////////////////////////////////
1708
Chet Haase1271e2c2012-04-20 09:54:27 -07001709status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001710 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001711
Romain Guy0fe478e2010-11-08 12:08:41 -08001712 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1713 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001714 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001715 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001716 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001717
Romain Guy65549432012-03-26 16:45:05 -07001718 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001719}
1720
Chet Haaseed30fd82011-04-22 16:18:45 -07001721void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1722 if (displayList) {
Chris Craik2af46352012-11-26 18:30:17 -08001723 displayList->output(level);
Chet Haaseed30fd82011-04-22 16:18:45 -07001724 }
1725}
1726
Romain Guya168d732011-03-18 16:50:13 -07001727void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1728 int alpha;
1729 SkXfermode::Mode mode;
1730 getAlphaAndMode(paint, &alpha, &mode);
1731
Romain Guy886b2752013-01-04 12:26:18 -08001732 int color = paint != NULL ? paint->getColor() : 0;
1733
Romain Guya168d732011-03-18 16:50:13 -07001734 float x = left;
1735 float y = top;
1736
Romain Guy886b2752013-01-04 12:26:18 -08001737 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1738
Romain Guya168d732011-03-18 16:50:13 -07001739 bool ignoreTransform = false;
1740 if (mSnapshot->transform->isPureTranslate()) {
1741 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1742 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1743 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001744
1745 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001746 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001747 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001748 }
1749
Romain Guy886b2752013-01-04 12:26:18 -08001750 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1751 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1752 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001753}
1754
Chet Haase48659092012-05-31 15:21:51 -07001755status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001756 const float right = left + bitmap->width();
1757 const float bottom = top + bitmap->height();
1758
1759 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001760 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001761 }
1762
Romain Guya1d3c912011-12-13 14:55:06 -08001763 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001764 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001765 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001766 const AutoTexture autoCleanup(texture);
1767
Romain Guy211370f2012-02-01 16:10:55 -08001768 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001769 drawAlphaBitmap(texture, left, top, paint);
1770 } else {
1771 drawTextureRect(left, top, right, bottom, texture, paint);
1772 }
Chet Haase48659092012-05-31 15:21:51 -07001773
1774 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001775}
1776
Chet Haase48659092012-05-31 15:21:51 -07001777status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001778 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1779 const mat4 transform(*matrix);
1780 transform.mapRect(r);
1781
Romain Guy6926c722010-07-12 20:20:03 -07001782 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001783 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001784 }
1785
Romain Guya1d3c912011-12-13 14:55:06 -08001786 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001787 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001788 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001789 const AutoTexture autoCleanup(texture);
1790
Romain Guy5b3b3522010-10-27 18:57:51 -07001791 // This could be done in a cheaper way, all we need is pass the matrix
1792 // to the vertex shader. The save/restore is a bit overkill.
1793 save(SkCanvas::kMatrix_SaveFlag);
1794 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08001795 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1796 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
1797 } else {
1798 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1799 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001800 restore();
Chet Haase48659092012-05-31 15:21:51 -07001801
1802 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001803}
1804
Chet Haase48659092012-05-31 15:21:51 -07001805status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001806 const float right = left + bitmap->width();
1807 const float bottom = top + bitmap->height();
1808
1809 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001810 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001811 }
1812
1813 mCaches.activeTexture(0);
1814 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1815 const AutoTexture autoCleanup(texture);
1816
Romain Guy886b2752013-01-04 12:26:18 -08001817 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1818 drawAlphaBitmap(texture, left, top, paint);
1819 } else {
1820 drawTextureRect(left, top, right, bottom, texture, paint);
1821 }
Chet Haase48659092012-05-31 15:21:51 -07001822
1823 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001824}
1825
Chet Haase48659092012-05-31 15:21:51 -07001826status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001827 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001828 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001829 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001830 }
1831
Romain Guyb18d2d02011-02-10 15:52:54 -08001832 float left = FLT_MAX;
1833 float top = FLT_MAX;
1834 float right = FLT_MIN;
1835 float bottom = FLT_MIN;
1836
Romain Guya92bb4d2012-10-16 11:08:44 -07001837 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001838
Romain Guya566b7c2011-01-23 16:36:11 -08001839 // TODO: Support the colors array
1840 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001841 TextureVertex* vertex = mesh;
Romain Guya92bb4d2012-10-16 11:08:44 -07001842
Romain Guy5a7b4662011-01-20 19:09:30 -08001843 for (int32_t y = 0; y < meshHeight; y++) {
1844 for (int32_t x = 0; x < meshWidth; x++) {
1845 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1846
1847 float u1 = float(x) / meshWidth;
1848 float u2 = float(x + 1) / meshWidth;
1849 float v1 = float(y) / meshHeight;
1850 float v2 = float(y + 1) / meshHeight;
1851
1852 int ax = i + (meshWidth + 1) * 2;
1853 int ay = ax + 1;
1854 int bx = i;
1855 int by = bx + 1;
1856 int cx = i + 2;
1857 int cy = cx + 1;
1858 int dx = i + (meshWidth + 1) * 2 + 2;
1859 int dy = dx + 1;
1860
1861 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1862 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1863 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1864
1865 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1866 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1867 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001868
Romain Guya92bb4d2012-10-16 11:08:44 -07001869 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1870 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1871 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1872 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001873 }
1874 }
1875
Romain Guya92bb4d2012-10-16 11:08:44 -07001876 if (quickReject(left, top, right, bottom)) {
1877 return DrawGlInfo::kStatusDone;
1878 }
1879
1880 mCaches.activeTexture(0);
1881 Texture* texture = mCaches.textureCache.get(bitmap);
1882 if (!texture) return DrawGlInfo::kStatusDone;
1883 const AutoTexture autoCleanup(texture);
1884
1885 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1886 texture->setFilter(FILTER(paint), true);
1887
1888 int alpha;
1889 SkXfermode::Mode mode;
1890 getAlphaAndMode(paint, &alpha, &mode);
1891
1892 if (hasLayer()) {
Romain Guyb18d2d02011-02-10 15:52:54 -08001893 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1894 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001895
Romain Guy5a7b4662011-01-20 19:09:30 -08001896 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1897 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001898 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001899
1900 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001901}
1902
Chet Haase48659092012-05-31 15:21:51 -07001903status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001904 float srcLeft, float srcTop, float srcRight, float srcBottom,
1905 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001906 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001907 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001908 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001909 }
1910
Romain Guya1d3c912011-12-13 14:55:06 -08001911 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001912 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001913 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001914 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001915
Romain Guy8ba548f2010-06-30 19:21:21 -07001916 const float width = texture->width;
1917 const float height = texture->height;
1918
Romain Guy68169722011-08-22 17:33:33 -07001919 const float u1 = fmax(0.0f, srcLeft / width);
1920 const float v1 = fmax(0.0f, srcTop / height);
1921 const float u2 = fmin(1.0f, srcRight / width);
1922 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001923
Romain Guy03750a02010-10-18 14:06:08 -07001924 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001925 resetDrawTextureTexCoords(u1, v1, u2, v2);
1926
Romain Guy03750a02010-10-18 14:06:08 -07001927 int alpha;
1928 SkXfermode::Mode mode;
1929 getAlphaAndMode(paint, &alpha, &mode);
1930
Romain Guyd21b6e12011-11-30 20:21:23 -08001931 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1932
Romain Guy886b2752013-01-04 12:26:18 -08001933 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
1934 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08001935
Romain Guy886b2752013-01-04 12:26:18 -08001936 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
1937 // Apply a scale transform on the canvas only when a shader is in use
1938 // Skia handles the ratio between the dst and src rects as a scale factor
1939 // when a shader is set
1940 bool useScaleTransform = mShader && scaled;
1941 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07001942
Romain Guy886b2752013-01-04 12:26:18 -08001943 if (CC_LIKELY(mSnapshot->transform->isPureTranslate() && !useScaleTransform)) {
1944 float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1945 float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1946
1947 dstRight = x + (dstRight - dstLeft);
1948 dstBottom = y + (dstBottom - dstTop);
1949
1950 dstLeft = x;
1951 dstTop = y;
1952
1953 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
1954 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08001955 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001956 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08001957 }
1958
1959 if (CC_UNLIKELY(useScaleTransform)) {
1960 save(SkCanvas::kMatrix_SaveFlag);
1961 translate(dstLeft, dstTop);
1962 scale(scaleX, scaleY);
1963
1964 dstLeft = 0.0f;
1965 dstTop = 0.0f;
1966
1967 dstRight = srcRight - srcLeft;
1968 dstBottom = srcBottom - srcTop;
1969 }
1970
1971 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1972 int color = paint ? paint->getColor() : 0;
1973 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
1974 texture->id, paint != NULL, color, alpha, mode,
1975 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1976 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
1977 } else {
1978 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
1979 texture->id, alpha / 255.0f, mode, texture->blend,
1980 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1981 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
1982 }
1983
1984 if (CC_UNLIKELY(useScaleTransform)) {
1985 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08001986 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001987
1988 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001989
1990 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001991}
1992
Chet Haase48659092012-05-31 15:21:51 -07001993status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001994 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001995 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001996 int alpha;
1997 SkXfermode::Mode mode;
1998 getAlphaAndModeDirect(paint, &alpha, &mode);
1999
2000 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2001 left, top, right, bottom, alpha, mode);
2002}
2003
2004status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2005 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2006 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002007 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002008 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002009 }
2010
Romain Guybe6f9dc2012-07-16 12:41:17 -07002011 alpha *= mSnapshot->alpha;
2012
Romain Guya1d3c912011-12-13 14:55:06 -08002013 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002014 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002015 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002016 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08002017 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2018 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07002019
Romain Guy2728f962010-10-08 18:36:15 -07002020 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002021 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002022
Romain Guy211370f2012-02-01 16:10:55 -08002023 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002024 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002025 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002026 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002027 const float offsetX = left + mSnapshot->transform->getTranslateX();
2028 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002029 const size_t count = mesh->quads.size();
2030 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002031 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002032 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002033 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2034 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2035 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002036 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002037 dirtyLayer(left + bounds.left, top + bounds.top,
2038 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08002039 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002040 }
2041 }
2042
Romain Guy211370f2012-02-01 16:10:55 -08002043 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002044 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2045 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2046
2047 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2048 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2049 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2050 true, !mesh->hasEmptyQuads);
2051 } else {
2052 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2053 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2054 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2055 true, !mesh->hasEmptyQuads);
2056 }
Romain Guy054dc182010-10-15 17:55:25 -07002057 }
Chet Haase48659092012-05-31 15:21:51 -07002058
2059 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002060}
2061
Chris Craik65cd6122012-12-10 17:56:27 -08002062status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2063 bool useOffset) {
2064 if (!vertexBuffer.getSize()) {
2065 // no vertices to draw
2066 return DrawGlInfo::kStatusDone;
2067 }
2068
Chris Craikcb4d6002012-09-25 12:00:29 -07002069 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002070 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2071 bool isAA = paint->isAntiAlias();
2072
Chris Craik710f46d2012-09-17 17:25:49 -07002073 setupDraw();
2074 setupDrawNoTexture();
2075 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002076 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2077 setupDrawColorFilter();
2078 setupDrawShader();
2079 setupDrawBlending(isAA, mode);
2080 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002081 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002082 setupDrawColorUniforms();
2083 setupDrawColorFilterUniforms();
2084 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002085
Chris Craik710f46d2012-09-17 17:25:49 -07002086 void* vertices = vertexBuffer.getBuffer();
2087 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002088 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002089 mCaches.resetTexCoordsVertexPointer();
2090 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002091
Chris Craik710f46d2012-09-17 17:25:49 -07002092 int alphaSlot = -1;
2093 if (isAA) {
2094 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2095 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002096
Chris Craik710f46d2012-09-17 17:25:49 -07002097 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002098 glEnableVertexAttribArray(alphaSlot);
2099 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002100 }
Romain Guy04299382012-07-18 17:15:41 -07002101
Chris Craik710f46d2012-09-17 17:25:49 -07002102 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002103
Chris Craik710f46d2012-09-17 17:25:49 -07002104 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002105 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002106 }
Chris Craik65cd6122012-12-10 17:56:27 -08002107
2108 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002109}
2110
2111/**
Chris Craik65cd6122012-12-10 17:56:27 -08002112 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2113 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2114 * screen space in all directions. However, instead of using a fragment shader to compute the
2115 * translucency of the color from its position, we simply use a varying parameter to define how far
2116 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2117 *
2118 * Doesn't yet support joins, caps, or path effects.
2119 */
2120status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2121 VertexBuffer vertexBuffer;
2122 // TODO: try clipping large paths to viewport
2123 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2124
2125 SkRect bounds = path.getBounds();
2126 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2127 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
2128
2129 return drawVertexBuffer(vertexBuffer, paint);
2130}
2131
2132/**
2133 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2134 * and additional geometry for defining an alpha slope perimeter.
2135 *
2136 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2137 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2138 * in-shader alpha region, but found it to be taxing on some GPUs.
2139 *
2140 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2141 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002142 */
Chet Haase48659092012-05-31 15:21:51 -07002143status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002144 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002145
Chris Craik65cd6122012-12-10 17:56:27 -08002146 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002147
Chris Craik65cd6122012-12-10 17:56:27 -08002148 VertexBuffer buffer;
2149 SkRect bounds;
2150 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002151
2152 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2153 return DrawGlInfo::kStatusDone;
2154 }
2155
Chris Craik65cd6122012-12-10 17:56:27 -08002156 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy7b631422012-04-04 11:38:54 -07002157
Chris Craik65cd6122012-12-10 17:56:27 -08002158 bool useOffset = !paint->isAntiAlias();
2159 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002160}
2161
Chet Haase48659092012-05-31 15:21:51 -07002162status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2163 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002164
2165 // TODO: The paint's cap style defines whether the points are square or circular
2166 // TODO: Handle AA for round points
2167
Chet Haase5b0200b2011-04-13 17:58:08 -07002168 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002169 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002170 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002171 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002172 if (isHairLine) {
2173 // Now that we know it's hairline, we can set the effective width, to be used later
2174 strokeWidth = 1.0f;
2175 }
2176 const float halfWidth = strokeWidth / 2;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002177
Romain Guyed6fcb02011-03-21 13:11:28 -07002178 int alpha;
2179 SkXfermode::Mode mode;
2180 getAlphaAndMode(paint, &alpha, &mode);
2181
2182 int verticesCount = count >> 1;
2183 int generatedVerticesCount = 0;
2184
2185 TextureVertex pointsData[verticesCount];
2186 TextureVertex* vertex = &pointsData[0];
2187
Romain Guy04299382012-07-18 17:15:41 -07002188 // TODO: We should optimize this method to not generate vertices for points
2189 // that lie outside of the clip.
2190 mCaches.enableScissor();
2191
Romain Guyed6fcb02011-03-21 13:11:28 -07002192 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002193 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002194 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002195 setupDrawColor(paint->getColor(), alpha);
2196 setupDrawColorFilter();
2197 setupDrawShader();
2198 setupDrawBlending(mode);
2199 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002200 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002201 setupDrawColorUniforms();
2202 setupDrawColorFilterUniforms();
2203 setupDrawPointUniforms();
2204 setupDrawShaderIdentityUniforms();
2205 setupDrawMesh(vertex);
2206
2207 for (int i = 0; i < count; i += 2) {
2208 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2209 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002210
Chet Haase8a5cc922011-04-26 07:28:09 -07002211 float left = points[i] - halfWidth;
2212 float right = points[i] + halfWidth;
2213 float top = points[i + 1] - halfWidth;
2214 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002215
Chet Haase8a5cc922011-04-26 07:28:09 -07002216 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002217 }
2218
2219 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002220
2221 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002222}
2223
Chet Haase48659092012-05-31 15:21:51 -07002224status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002225 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002226 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002227
Romain Guyae88e5e2010-10-22 17:49:18 -07002228 Rect& clip(*mSnapshot->clipRect);
2229 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002230
Romain Guy3d58c032010-07-14 16:34:53 -07002231 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002232
2233 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002234}
Romain Guy9d5316e2010-06-24 19:30:36 -07002235
Chet Haase48659092012-05-31 15:21:51 -07002236status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2237 SkPaint* paint) {
2238 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002239 const AutoTexture autoCleanup(texture);
2240
2241 const float x = left + texture->left - texture->offset;
2242 const float y = top + texture->top - texture->offset;
2243
2244 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002245
2246 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002247}
2248
Chet Haase48659092012-05-31 15:21:51 -07002249status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002250 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002251 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002252 return DrawGlInfo::kStatusDone;
2253 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002254
Chris Craikcb4d6002012-09-25 12:00:29 -07002255 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002256 mCaches.activeTexture(0);
2257 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2258 right - left, bottom - top, rx, ry, p);
2259 return drawShape(left, top, texture, p);
2260 }
2261
2262 SkPath path;
2263 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002264 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2265 float outset = p->getStrokeWidth() / 2;
2266 rect.outset(outset, outset);
2267 rx += outset;
2268 ry += outset;
2269 }
Chris Craik710f46d2012-09-17 17:25:49 -07002270 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002271 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002272}
2273
Chris Craik710f46d2012-09-17 17:25:49 -07002274status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002275 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2276 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002277 return DrawGlInfo::kStatusDone;
2278 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002279 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002280 mCaches.activeTexture(0);
2281 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2282 return drawShape(x - radius, y - radius, texture, p);
2283 }
2284
2285 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002286 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2287 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2288 } else {
2289 path.addCircle(x, y, radius);
2290 }
Chris Craik65cd6122012-12-10 17:56:27 -08002291 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002292}
Romain Guy01d58e42011-01-19 21:54:02 -08002293
Chet Haase48659092012-05-31 15:21:51 -07002294status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002295 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002296 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002297 return DrawGlInfo::kStatusDone;
2298 }
Romain Guy01d58e42011-01-19 21:54:02 -08002299
Chris Craikcb4d6002012-09-25 12:00:29 -07002300 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002301 mCaches.activeTexture(0);
2302 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2303 return drawShape(left, top, texture, p);
2304 }
2305
2306 SkPath path;
2307 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002308 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2309 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2310 }
Chris Craik710f46d2012-09-17 17:25:49 -07002311 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002312 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002313}
2314
Chet Haase48659092012-05-31 15:21:51 -07002315status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002316 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
2317 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
2318 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002319 }
2320
Chris Craik780c1282012-10-04 14:10:49 -07002321 if (fabs(sweepAngle) >= 360.0f) {
2322 return drawOval(left, top, right, bottom, p);
2323 }
2324
2325 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002326 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002327 mCaches.activeTexture(0);
2328 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2329 startAngle, sweepAngle, useCenter, p);
2330 return drawShape(left, top, texture, p);
2331 }
2332
2333 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2334 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2335 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2336 }
2337
2338 SkPath path;
2339 if (useCenter) {
2340 path.moveTo(rect.centerX(), rect.centerY());
2341 }
2342 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2343 if (useCenter) {
2344 path.close();
2345 }
Chris Craik65cd6122012-12-10 17:56:27 -08002346 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002347}
2348
Romain Guycf8675e2012-10-02 12:32:25 -07002349// See SkPaintDefaults.h
2350#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2351
Chet Haase48659092012-05-31 15:21:51 -07002352status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002353 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002354 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002355 }
2356
Chris Craik710f46d2012-09-17 17:25:49 -07002357 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002358 // only fill style is supported by drawConvexPath, since others have to handle joins
2359 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2360 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2361 mCaches.activeTexture(0);
2362 const PathTexture* texture =
2363 mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2364 return drawShape(left, top, texture, p);
2365 }
2366
2367 SkPath path;
2368 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2369 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2370 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2371 }
2372 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002373 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002374 }
2375
Romain Guy181d0a62011-06-09 18:52:38 -07002376 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002377 SkPath path;
2378 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002379 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002380 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002381 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002382 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002383 }
Romain Guyc7d53492010-06-25 13:41:57 -07002384}
Romain Guy9d5316e2010-06-24 19:30:36 -07002385
Raph Levien416a8472012-07-19 22:48:17 -07002386void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2387 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2388 float x, float y) {
2389 mCaches.activeTexture(0);
2390
2391 // NOTE: The drop shadow will not perform gamma correction
2392 // if shader-based correction is enabled
2393 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2394 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2395 paint, text, bytesCount, count, mShadowRadius, positions);
2396 const AutoTexture autoCleanup(shadow);
2397
2398 const float sx = x - shadow->left + mShadowDx;
2399 const float sy = y - shadow->top + mShadowDy;
2400
2401 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2402 int shadowColor = mShadowColor;
2403 if (mShader) {
2404 shadowColor = 0xffffffff;
2405 }
2406
2407 setupDraw();
2408 setupDrawWithTexture(true);
2409 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2410 setupDrawColorFilter();
2411 setupDrawShader();
2412 setupDrawBlending(true, mode);
2413 setupDrawProgram();
2414 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2415 setupDrawTexture(shadow->id);
2416 setupDrawPureColorUniforms();
2417 setupDrawColorFilterUniforms();
2418 setupDrawShaderUniforms();
2419 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2420
2421 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2422}
2423
Chet Haase48659092012-05-31 15:21:51 -07002424status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002425 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002426 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002427 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002428 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002429 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002430
Romain Guy671d6cf2012-01-18 12:39:17 -08002431 // NOTE: Skia does not support perspective transform on drawPosText yet
2432 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002433 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002434 }
2435
2436 float x = 0.0f;
2437 float y = 0.0f;
2438 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2439 if (pureTranslate) {
2440 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2441 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2442 }
2443
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002444 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002445 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy671d6cf2012-01-18 12:39:17 -08002446
2447 int alpha;
2448 SkXfermode::Mode mode;
2449 getAlphaAndMode(paint, &alpha, &mode);
2450
Raph Levien416a8472012-07-19 22:48:17 -07002451 if (CC_UNLIKELY(mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002452 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2453 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002454 }
2455
Romain Guy671d6cf2012-01-18 12:39:17 -08002456 // Pick the appropriate texture filtering
2457 bool linearFilter = mSnapshot->transform->changesBounds();
2458 if (pureTranslate && !linearFilter) {
2459 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2460 }
2461
2462 mCaches.activeTexture(0);
2463 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002464 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002465 setupDrawDirtyRegionsDisabled();
2466 setupDrawWithTexture(true);
2467 setupDrawAlpha8Color(paint->getColor(), alpha);
2468 setupDrawColorFilter();
2469 setupDrawShader();
2470 setupDrawBlending(true, mode);
2471 setupDrawProgram();
2472 setupDrawModelView(x, y, x, y, pureTranslate, true);
2473 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2474 setupDrawPureColorUniforms();
2475 setupDrawColorFilterUniforms();
2476 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002477 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002478
2479 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2480 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2481
Romain Guy211370f2012-02-01 16:10:55 -08002482 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002483
2484 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2485 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002486 if (hasActiveLayer) {
2487 if (!pureTranslate) {
2488 mSnapshot->transform->mapRect(bounds);
2489 }
2490 dirtyLayerUnchecked(bounds, getRegion());
2491 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002492 }
Chet Haase48659092012-05-31 15:21:51 -07002493
2494 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002495}
2496
Romain Guyc2525952012-07-27 16:41:22 -07002497status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002498 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002499 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002500 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002501 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002502 }
2503
Chet Haasea1cff502012-02-21 13:43:44 -08002504 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002505 switch (paint->getTextAlign()) {
2506 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002507 x -= length / 2.0f;
2508 break;
2509 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002510 x -= length;
2511 break;
2512 default:
2513 break;
2514 }
2515
Romain Guycac5fd32011-12-01 20:08:50 -08002516 SkPaint::FontMetrics metrics;
2517 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002518 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002519 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002520 }
2521
Romain Guye3a9b242013-01-08 11:15:30 -08002522#if DEBUG_GLYPHS
2523 ALOGD("OpenGLRenderer drawText() with FontID=%d",
2524 SkTypeface::UniqueID(paint->getTypeface()));
2525#endif
2526
2527 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2528 fontRenderer.setFont(paint, *mSnapshot->transform);
2529
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002530 const float oldX = x;
2531 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002532 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002533 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002534 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2535 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2536 }
2537
Romain Guy86568192010-12-14 15:55:39 -08002538 int alpha;
2539 SkXfermode::Mode mode;
2540 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002541
Romain Guy211370f2012-02-01 16:10:55 -08002542 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002543 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2544 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002545 }
2546
Romain Guy6620c6d2010-12-06 18:07:02 -08002547 // Pick the appropriate texture filtering
2548 bool linearFilter = mSnapshot->transform->changesBounds();
2549 if (pureTranslate && !linearFilter) {
2550 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2551 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002552
Romain Guy16c88082012-06-11 16:03:47 -07002553 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002554 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002555 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002556 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002557 setupDrawDirtyRegionsDisabled();
2558 setupDrawWithTexture(true);
2559 setupDrawAlpha8Color(paint->getColor(), alpha);
2560 setupDrawColorFilter();
2561 setupDrawShader();
2562 setupDrawBlending(true, mode);
2563 setupDrawProgram();
2564 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002565 // See comment above; the font renderer must use texture unit 0
2566 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002567 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2568 setupDrawPureColorUniforms();
2569 setupDrawColorFilterUniforms();
2570 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002571 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002572
Romain Guya3dc55f2012-09-28 13:55:44 -07002573 const Rect* clip = pureTranslate ? mSnapshot->clipRect :
2574 (mSnapshot->hasPerspectiveTransform() ? NULL : &mSnapshot->getLocalClip());
Romain Guy5b3b3522010-10-27 18:57:51 -07002575 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2576
Romain Guy211370f2012-02-01 16:10:55 -08002577 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002578
Raph Levien996e57c2012-07-23 15:22:52 -07002579 bool status;
Romain Guya3dc55f2012-09-28 13:55:44 -07002580 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002581 SkPaint paintCopy(*paint);
2582 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2583 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002584 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002585 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002586 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002587 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002588 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002589
2590 if (status && hasActiveLayer) {
2591 if (!pureTranslate) {
2592 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002593 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002594 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002595 }
Romain Guy694b5192010-07-21 21:33:20 -07002596
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002597 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002598
2599 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002600}
2601
Chet Haase48659092012-05-31 15:21:51 -07002602status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002603 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002604 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2605 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002606 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002607 }
2608
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002609 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002610 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy03d58522012-02-24 17:54:07 -08002611
2612 int alpha;
2613 SkXfermode::Mode mode;
2614 getAlphaAndMode(paint, &alpha, &mode);
2615
2616 mCaches.activeTexture(0);
2617 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002618 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002619 setupDrawDirtyRegionsDisabled();
2620 setupDrawWithTexture(true);
2621 setupDrawAlpha8Color(paint->getColor(), alpha);
2622 setupDrawColorFilter();
2623 setupDrawShader();
2624 setupDrawBlending(true, mode);
2625 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002626 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002627 setupDrawTexture(fontRenderer.getTexture(true));
2628 setupDrawPureColorUniforms();
2629 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002630 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002631 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002632
Romain Guy97771732012-02-28 18:17:02 -08002633 const Rect* clip = &mSnapshot->getLocalClip();
2634 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 -08002635
Romain Guy97771732012-02-28 18:17:02 -08002636 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002637
2638 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2639 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002640 if (hasActiveLayer) {
2641 mSnapshot->transform->mapRect(bounds);
2642 dirtyLayerUnchecked(bounds, getRegion());
2643 }
Romain Guy97771732012-02-28 18:17:02 -08002644 }
Chet Haase48659092012-05-31 15:21:51 -07002645
2646 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002647}
2648
Chet Haase48659092012-05-31 15:21:51 -07002649status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2650 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002651
Romain Guya1d3c912011-12-13 14:55:06 -08002652 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002653
Romain Guyfb8b7632010-08-23 21:05:08 -07002654 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002655 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002656 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002657
Romain Guy8b55f372010-08-18 17:10:07 -07002658 const float x = texture->left - texture->offset;
2659 const float y = texture->top - texture->offset;
2660
Romain Guy01d58e42011-01-19 21:54:02 -08002661 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002662
2663 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002664}
2665
Chet Haase48659092012-05-31 15:21:51 -07002666status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002667 if (!layer) {
2668 return DrawGlInfo::kStatusDone;
2669 }
2670
Romain Guyb2e2f242012-10-17 18:18:35 -07002671 mat4* transform = NULL;
2672 if (layer->isTextureLayer()) {
2673 transform = &layer->getTransform();
2674 if (!transform->isIdentity()) {
2675 save(0);
2676 mSnapshot->transform->multiply(*transform);
2677 }
2678 }
2679
Romain Guy35643dd2012-09-18 15:40:58 -07002680 Rect transformed;
2681 Rect clip;
2682 const bool rejected = quickRejectNoScissor(x, y,
2683 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2684
2685 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002686 if (transform && !transform->isIdentity()) {
2687 restore();
2688 }
Chet Haase48659092012-05-31 15:21:51 -07002689 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002690 }
2691
Romain Guy5bb3c732012-11-29 17:52:58 -08002692 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002693
Romain Guy87e2f7572012-09-24 11:37:12 -07002694 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002695 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002696
Romain Guy211370f2012-02-01 16:10:55 -08002697 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guye529ece2012-09-26 11:23:17 -07002698 SkiaColorFilter* oldFilter = mColorFilter;
2699 mColorFilter = layer->getColorFilter();
2700
Romain Guyc88e3572011-01-22 00:32:12 -08002701 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002702 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002703 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002704 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002705 setupDraw();
2706 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002707 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002708 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002709 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002710 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002711 setupDrawPureColorUniforms();
2712 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002713 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002714 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002715 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2716 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002717
Romain Guyd21b6e12011-11-30 20:21:23 -08002718 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002719 setupDrawModelViewTranslate(tx, ty,
2720 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002721 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002722 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002723 setupDrawModelViewTranslate(x, y,
2724 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2725 }
Romain Guyc88e3572011-01-22 00:32:12 -08002726 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002727
Romain Guyc88e3572011-01-22 00:32:12 -08002728 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2729 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002730
Romain Guyc88e3572011-01-22 00:32:12 -08002731 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002732
2733#if DEBUG_LAYERS_AS_REGIONS
2734 drawRegionRects(layer->region);
2735#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002736 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002737
Romain Guye529ece2012-09-26 11:23:17 -07002738 mColorFilter = oldFilter;
2739
Romain Guy5bb3c732012-11-29 17:52:58 -08002740 if (layer->debugDrawUpdate) {
2741 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002742 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2743 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2744 }
Romain Guyf219da52011-01-16 12:54:25 -08002745 }
Chet Haase48659092012-05-31 15:21:51 -07002746
Romain Guyb2e2f242012-10-17 18:18:35 -07002747 if (transform && !transform->isIdentity()) {
2748 restore();
2749 }
2750
Chet Haase48659092012-05-31 15:21:51 -07002751 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002752}
2753
Romain Guy6926c722010-07-12 20:20:03 -07002754///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002755// Shaders
2756///////////////////////////////////////////////////////////////////////////////
2757
2758void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002759 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002760}
2761
Romain Guy06f96e22010-07-30 19:18:16 -07002762void OpenGLRenderer::setupShader(SkiaShader* shader) {
2763 mShader = shader;
2764 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002765 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002766 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002767}
2768
Romain Guyd27977d2010-07-14 19:18:51 -07002769///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002770// Color filters
2771///////////////////////////////////////////////////////////////////////////////
2772
2773void OpenGLRenderer::resetColorFilter() {
2774 mColorFilter = NULL;
2775}
2776
2777void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2778 mColorFilter = filter;
2779}
2780
2781///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002782// Drop shadow
2783///////////////////////////////////////////////////////////////////////////////
2784
2785void OpenGLRenderer::resetShadow() {
2786 mHasShadow = false;
2787}
2788
2789void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2790 mHasShadow = true;
2791 mShadowRadius = radius;
2792 mShadowDx = dx;
2793 mShadowDy = dy;
2794 mShadowColor = color;
2795}
2796
2797///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002798// Draw filters
2799///////////////////////////////////////////////////////////////////////////////
2800
2801void OpenGLRenderer::resetPaintFilter() {
2802 mHasDrawFilter = false;
2803}
2804
2805void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2806 mHasDrawFilter = true;
2807 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2808 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2809}
2810
2811SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002812 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002813
2814 uint32_t flags = paint->getFlags();
2815
2816 mFilteredPaint = *paint;
2817 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2818
2819 return &mFilteredPaint;
2820}
2821
2822///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002823// Drawing implementation
2824///////////////////////////////////////////////////////////////////////////////
2825
Romain Guy01d58e42011-01-19 21:54:02 -08002826void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2827 float x, float y, SkPaint* paint) {
2828 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2829 return;
2830 }
2831
2832 int alpha;
2833 SkXfermode::Mode mode;
2834 getAlphaAndMode(paint, &alpha, &mode);
2835
2836 setupDraw();
2837 setupDrawWithTexture(true);
2838 setupDrawAlpha8Color(paint->getColor(), alpha);
2839 setupDrawColorFilter();
2840 setupDrawShader();
2841 setupDrawBlending(true, mode);
2842 setupDrawProgram();
2843 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2844 setupDrawTexture(texture->id);
2845 setupDrawPureColorUniforms();
2846 setupDrawColorFilterUniforms();
2847 setupDrawShaderUniforms();
2848 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2849
2850 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2851
2852 finishDrawTexture();
2853}
2854
Romain Guyf607bdc2010-09-10 19:20:06 -07002855// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002856#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2857#define kStdUnderline_Offset (1.0f / 9.0f)
2858#define kStdUnderline_Thickness (1.0f / 18.0f)
2859
2860void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2861 float x, float y, SkPaint* paint) {
2862 // Handle underline and strike-through
2863 uint32_t flags = paint->getFlags();
2864 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002865 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002866 float underlineWidth = length;
2867 // If length is > 0.0f, we already measured the text for the text alignment
2868 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002869 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002870 }
2871
Romain Guy211370f2012-02-01 16:10:55 -08002872 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002873 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002874 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002875
Raph Levien8b4072d2012-07-30 15:50:00 -07002876 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002877 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002878
Romain Guyf6834472011-01-23 13:32:12 -08002879 int linesCount = 0;
2880 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2881 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2882
2883 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002884 float points[pointsCount];
2885 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002886
2887 if (flags & SkPaint::kUnderlineText_Flag) {
2888 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002889 points[currentPoint++] = left;
2890 points[currentPoint++] = top;
2891 points[currentPoint++] = left + underlineWidth;
2892 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002893 }
2894
2895 if (flags & SkPaint::kStrikeThruText_Flag) {
2896 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002897 points[currentPoint++] = left;
2898 points[currentPoint++] = top;
2899 points[currentPoint++] = left + underlineWidth;
2900 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002901 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002902
Romain Guy726aeba2011-06-01 14:52:00 -07002903 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002904
Romain Guy726aeba2011-06-01 14:52:00 -07002905 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002906 }
2907 }
2908}
2909
Romain Guy672433d2013-01-04 19:05:13 -08002910status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
2911 if (mSnapshot->isIgnored()) {
2912 return DrawGlInfo::kStatusDone;
2913 }
2914
Romain Guy735738c2012-12-03 12:34:51 -08002915 int color = paint->getColor();
2916 // If a shader is set, preserve only the alpha
2917 if (mShader) {
2918 color |= 0x00ffffff;
2919 }
2920 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2921
2922 return drawColorRects(rects, count, color, mode);
2923}
2924
2925status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08002926 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy735738c2012-12-03 12:34:51 -08002927
Romain Guy672433d2013-01-04 19:05:13 -08002928 float left = FLT_MAX;
2929 float top = FLT_MAX;
2930 float right = FLT_MIN;
2931 float bottom = FLT_MIN;
2932
2933 int vertexCount = 0;
2934 Vertex mesh[count * 6];
2935 Vertex* vertex = mesh;
2936
Chris Craik2af46352012-11-26 18:30:17 -08002937 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08002938 float l = rects[index + 0];
2939 float t = rects[index + 1];
2940 float r = rects[index + 2];
2941 float b = rects[index + 3];
2942
Romain Guy8ce00302013-01-15 18:51:42 -08002943 if (ignoreTransform || !quickRejectNoScissor(left, top, right, bottom)) {
Romain Guy672433d2013-01-04 19:05:13 -08002944 Vertex::set(vertex++, l, b);
2945 Vertex::set(vertex++, l, t);
2946 Vertex::set(vertex++, r, t);
2947 Vertex::set(vertex++, l, b);
2948 Vertex::set(vertex++, r, t);
2949 Vertex::set(vertex++, r, b);
2950
2951 vertexCount += 6;
2952
2953 left = fminf(left, l);
2954 top = fminf(top, t);
2955 right = fmaxf(right, r);
2956 bottom = fmaxf(bottom, b);
2957 }
2958 }
2959
Romain Guy3bbacf22013-02-06 16:51:04 -08002960 if (count == 0 || (clip && quickReject(left, top, right, bottom))) {
Romain Guya362c692013-02-04 13:50:16 -08002961 return DrawGlInfo::kStatusDone;
2962 }
Romain Guy672433d2013-01-04 19:05:13 -08002963
Romain Guy672433d2013-01-04 19:05:13 -08002964 setupDraw();
2965 setupDrawNoTexture();
2966 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2967 setupDrawShader();
2968 setupDrawColorFilter();
2969 setupDrawBlending(mode);
2970 setupDrawProgram();
2971 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08002972 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08002973 setupDrawColorUniforms();
2974 setupDrawShaderUniforms();
2975 setupDrawColorFilterUniforms();
2976 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
2977
Romain Guy8ce00302013-01-15 18:51:42 -08002978 if (dirty && hasLayer()) {
Romain Guy672433d2013-01-04 19:05:13 -08002979 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
2980 }
2981
2982 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
2983
2984 return DrawGlInfo::kStatusDrew;
2985}
2986
Romain Guy026c5e162010-06-28 17:12:22 -07002987void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002988 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002989 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002990 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002991 color |= 0x00ffffff;
2992 }
2993
Romain Guy70ca14e2010-12-13 18:24:33 -08002994 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002995 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002996 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002997 setupDrawShader();
2998 setupDrawColorFilter();
2999 setupDrawBlending(mode);
3000 setupDrawProgram();
3001 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3002 setupDrawColorUniforms();
3003 setupDrawShaderUniforms(ignoreTransform);
3004 setupDrawColorFilterUniforms();
3005 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003006
Romain Guyc95c8d62010-09-17 15:31:32 -07003007 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3008}
3009
Romain Guy82ba8142010-07-09 13:25:56 -07003010void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003011 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003012 int alpha;
3013 SkXfermode::Mode mode;
3014 getAlphaAndMode(paint, &alpha, &mode);
3015
Romain Guyd21b6e12011-11-30 20:21:23 -08003016 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003017
Romain Guy211370f2012-02-01 16:10:55 -08003018 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08003019 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
3020 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
3021
Romain Guyd21b6e12011-11-30 20:21:23 -08003022 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003023 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3024 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3025 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3026 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003027 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003028 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3029 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3030 GL_TRIANGLE_STRIP, gMeshCount);
3031 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003032}
3033
Romain Guybd6b79b2010-06-26 00:13:53 -07003034void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003035 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3036 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003037 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003038}
3039
3040void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003041 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003042 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003043 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003044
Romain Guy746b7402010-10-26 16:27:31 -07003045 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003046 setupDrawWithTexture();
3047 setupDrawColor(alpha, alpha, alpha, alpha);
3048 setupDrawColorFilter();
3049 setupDrawBlending(blend, mode, swapSrcDst);
3050 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003051 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003052 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003053 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003054 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003055 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003056 }
Romain Guy886b2752013-01-04 12:26:18 -08003057 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003058 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003059 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003060 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003061
Romain Guy6820ac82010-09-15 18:11:50 -07003062 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003063
3064 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003065}
3066
Romain Guy886b2752013-01-04 12:26:18 -08003067void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3068 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3069 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3070 bool ignoreTransform, bool dirty) {
3071
3072 setupDraw();
3073 setupDrawWithTexture(true);
3074 if (hasColor) {
3075 setupDrawAlpha8Color(color, alpha);
3076 }
3077 setupDrawColorFilter();
3078 setupDrawShader();
3079 setupDrawBlending(true, mode);
3080 setupDrawProgram();
3081 if (!dirty) setupDrawDirtyRegionsDisabled();
3082 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3083 setupDrawTexture(texture);
3084 setupDrawPureColorUniforms();
3085 setupDrawColorFilterUniforms();
3086 setupDrawShaderUniforms();
3087 setupDrawMesh(vertices, texCoords);
3088
3089 glDrawArrays(drawMode, 0, elementsCount);
3090
3091 finishDrawTexture();
3092}
3093
Romain Guya5aed0d2010-09-09 14:42:43 -07003094void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003095 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003096 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003097
Romain Guy82ba8142010-07-09 13:25:56 -07003098 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003099 // These blend modes are not supported by OpenGL directly and have
3100 // to be implemented using shaders. Since the shader will perform
3101 // the blending, turn blending off here
3102 // If the blend mode cannot be implemented using shaders, fall
3103 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003104 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003105 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003106 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003107 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003108
Romain Guy82bc7a72012-01-03 14:13:39 -08003109 if (mCaches.blend) {
3110 glDisable(GL_BLEND);
3111 mCaches.blend = false;
3112 }
3113
3114 return;
3115 } else {
3116 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003117 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003118 }
3119
3120 if (!mCaches.blend) {
3121 glEnable(GL_BLEND);
3122 }
3123
3124 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3125 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3126
3127 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3128 glBlendFunc(sourceMode, destMode);
3129 mCaches.lastSrcMode = sourceMode;
3130 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003131 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003132 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003133 glDisable(GL_BLEND);
3134 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003135 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003136}
3137
Romain Guy889f8d12010-07-29 14:37:42 -07003138bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003139 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003140 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003141 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003142 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003143 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003144 }
Romain Guy6926c722010-07-12 20:20:03 -07003145 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003146}
3147
Romain Guy026c5e162010-06-28 17:12:22 -07003148void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003149 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003150 TextureVertex::setUV(v++, u1, v1);
3151 TextureVertex::setUV(v++, u2, v1);
3152 TextureVertex::setUV(v++, u1, v2);
3153 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003154}
3155
Chet Haase5c13d892010-10-08 08:37:55 -07003156void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003157 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003158 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003159}
3160
Romain Guy9d5316e2010-06-24 19:30:36 -07003161}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003162}; // namespace android