blob: f0d25e1e18abe713f1870d1ebf89daa3e3651ddb [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 Guyff316ec2013-02-13 18:39:43 -0800320 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700321 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700322}
323
Romain Guy6c319ca2011-01-11 14:29:25 -0800324void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800325 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800326 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700327 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700328 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700329
Romain Guy3e263fa2011-12-12 16:47:48 -0800330 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
331
Chet Haase80250612012-08-15 13:46:54 -0700332 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700333 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800334 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700335 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700336
Romain Guya1d3c912011-12-13 14:55:06 -0800337 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700338
Romain Guy50c0f092010-10-19 11:42:22 -0700339 mCaches.blend = true;
340 glEnable(GL_BLEND);
341 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
342 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700343}
344
Romain Guy35643dd2012-09-18 15:40:58 -0700345void OpenGLRenderer::resumeAfterLayer() {
346 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
347 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
348 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700349 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700350
351 mCaches.resetScissor();
352 dirtyClip();
353}
354
Romain Guyba6be8a2012-04-23 18:22:09 -0700355void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700356 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700357}
358
359void OpenGLRenderer::attachFunctor(Functor* functor) {
360 mFunctors.add(functor);
361}
362
Romain Guy8f3b8e32012-03-27 16:33:45 -0700363status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
364 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700365 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700366
Romain Guyba6be8a2012-04-23 18:22:09 -0700367 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800368 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700369 SortedVector<Functor*> functors(mFunctors);
370 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700371
Romain Guyba6be8a2012-04-23 18:22:09 -0700372 DrawGlInfo info;
373 info.clipLeft = 0;
374 info.clipTop = 0;
375 info.clipRight = 0;
376 info.clipBottom = 0;
377 info.isLayer = false;
378 info.width = 0;
379 info.height = 0;
380 memset(info.transform, 0, sizeof(float) * 16);
381
382 for (size_t i = 0; i < count; i++) {
383 Functor* f = functors.itemAt(i);
384 result |= (*f)(DrawGlInfo::kModeProcess, &info);
385
Chris Craikc2c95432012-04-25 15:13:52 -0700386 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700387 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
388 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700389 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700390
Chris Craikc2c95432012-04-25 15:13:52 -0700391 if (result & DrawGlInfo::kStatusInvoke) {
392 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700393 }
394 }
Chris Craikd15321b2012-11-28 14:45:04 -0800395 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700396 }
397
398 return result;
399}
400
401status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800402 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700403 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700404
Romain Guy8a4ac612012-07-17 17:32:48 -0700405 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800406 if (mDirtyClip) {
407 setScissorFromClip();
408 }
Romain Guyd643bb52011-03-01 14:55:21 -0800409
Romain Guy80911b82011-03-16 15:30:12 -0700410 Rect clip(*mSnapshot->clipRect);
411 clip.snapToPixelBoundaries();
412
Romain Guyd643bb52011-03-01 14:55:21 -0800413 // Since we don't know what the functor will draw, let's dirty
414 // tne entire clip region
415 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800416 dirtyLayerUnchecked(clip, getRegion());
417 }
Romain Guyd643bb52011-03-01 14:55:21 -0800418
Romain Guy08aa2cb2011-03-17 11:06:57 -0700419 DrawGlInfo info;
420 info.clipLeft = clip.left;
421 info.clipTop = clip.top;
422 info.clipRight = clip.right;
423 info.clipBottom = clip.bottom;
424 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700425 info.width = getSnapshot()->viewport.getWidth();
426 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700427 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700428
Chet Haase48659092012-05-31 15:21:51 -0700429 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800430
Romain Guy8f3b8e32012-03-27 16:33:45 -0700431 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700432 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800433 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700434
Chris Craik65924a32012-04-05 17:52:11 -0700435 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700436 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700437 }
Romain Guycabfcc12011-03-07 18:06:46 -0800438 }
439
Chet Haasedaf98e92011-01-10 14:10:36 -0800440 resume();
Romain Guy65549432012-03-26 16:45:05 -0700441 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800442}
443
Romain Guyf6a11b82010-06-23 17:47:49 -0700444///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700445// Debug
446///////////////////////////////////////////////////////////////////////////////
447
448void OpenGLRenderer::startMark(const char* name) const {
449 mCaches.startMark(0, name);
450}
451
452void OpenGLRenderer::endMark() const {
453 mCaches.endMark();
454}
455
456void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
457 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
458 if (clear) {
459 mCaches.disableScissor();
460 mCaches.stencil.clear();
461 }
462 if (enable) {
463 mCaches.stencil.enableDebugWrite();
464 } else {
465 mCaches.stencil.disable();
466 }
467 }
468}
469
470void OpenGLRenderer::renderOverdraw() {
471 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
472 const Rect* clip = mTilingSnapshot->clipRect;
473
474 mCaches.enableScissor();
475 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
476 clip->right - clip->left, clip->bottom - clip->top);
477
478 mCaches.stencil.enableDebugTest(2);
479 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
480 mCaches.stencil.enableDebugTest(3);
481 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
482 mCaches.stencil.enableDebugTest(4);
483 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
484 mCaches.stencil.enableDebugTest(4, true);
485 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
486 mCaches.stencil.disable();
487 }
488}
489
490///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700491// Layers
492///////////////////////////////////////////////////////////////////////////////
493
494bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
495 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
496 OpenGLRenderer* renderer = layer->renderer;
497 Rect& dirty = layer->dirtyRect;
498
Romain Guy7c450aa2012-09-21 19:15:00 -0700499 if (inFrame) {
500 endTiling();
501 debugOverdraw(false, false);
502 }
Romain Guy11cb6422012-09-21 00:39:43 -0700503
504 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
505 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
506 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
507 renderer->finish();
508
509 if (inFrame) {
510 resumeAfterLayer();
511 startTiling(mSnapshot);
512 }
513
514 dirty.setEmpty();
515 layer->deferredUpdateScheduled = false;
516 layer->renderer = NULL;
517 layer->displayList = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -0800518 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700519
520 return true;
521 }
522
523 return false;
524}
525
526void OpenGLRenderer::updateLayers() {
527 int count = mLayerUpdates.size();
528 if (count > 0) {
529 startMark("Layer Updates");
530
531 // Note: it is very important to update the layers in reverse order
532 for (int i = count - 1; i >= 0; i--) {
533 Layer* layer = mLayerUpdates.itemAt(i);
534 updateLayer(layer, false);
535 mCaches.resourceCache.decrementRefcount(layer);
536 }
537 mLayerUpdates.clear();
538
539 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
540 endMark();
541 }
542}
543
544void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
545 if (layer) {
546 mLayerUpdates.push_back(layer);
547 mCaches.resourceCache.incrementRefcount(layer);
548 }
549}
550
551void OpenGLRenderer::clearLayerUpdates() {
552 size_t count = mLayerUpdates.size();
553 if (count > 0) {
554 mCaches.resourceCache.lock();
555 for (size_t i = 0; i < count; i++) {
556 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
557 }
558 mCaches.resourceCache.unlock();
559 mLayerUpdates.clear();
560 }
561}
562
563///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700564// State management
565///////////////////////////////////////////////////////////////////////////////
566
Romain Guybb9524b2010-06-22 18:56:38 -0700567int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700568 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700569}
570
571int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700572 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700573}
574
575void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700576 if (mSaveCount > 1) {
577 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700578 }
Romain Guybb9524b2010-06-22 18:56:38 -0700579}
580
581void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700582 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700583
Romain Guy8fb95422010-08-17 18:38:51 -0700584 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700585 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700586 }
Romain Guybb9524b2010-06-22 18:56:38 -0700587}
588
Romain Guy8aef54f2010-09-01 15:13:49 -0700589int OpenGLRenderer::saveSnapshot(int flags) {
590 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700591 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700592}
593
594bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700595 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700596 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700597 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700598
Romain Guybd6b79b2010-06-26 00:13:53 -0700599 sp<Snapshot> current = mSnapshot;
600 sp<Snapshot> previous = mSnapshot->previous;
601
Romain Guyeb993562010-10-05 18:14:38 -0700602 if (restoreOrtho) {
603 Rect& r = previous->viewport;
604 glViewport(r.left, r.top, r.right, r.bottom);
605 mOrthoMatrix.load(current->orthoMatrix);
606 }
607
Romain Guy8b55f372010-08-18 17:10:07 -0700608 mSaveCount--;
609 mSnapshot = previous;
610
Romain Guy2542d192010-08-18 11:47:12 -0700611 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700612 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700613 }
Romain Guy2542d192010-08-18 11:47:12 -0700614
Romain Guy5ec99242010-11-03 16:19:08 -0700615 if (restoreLayer) {
616 composeLayer(current, previous);
617 }
618
Romain Guy2542d192010-08-18 11:47:12 -0700619 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700620}
621
Romain Guyf6a11b82010-06-23 17:47:49 -0700622///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700623// Layers
624///////////////////////////////////////////////////////////////////////////////
625
626int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700627 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700628 const GLuint previousFbo = mSnapshot->fbo;
629 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700630
Romain Guyaf636eb2010-12-09 17:47:21 -0800631 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700632 int alpha = 255;
633 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700634
Romain Guye45362c2010-11-03 19:58:32 -0700635 if (p) {
636 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700637 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700638 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700639 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700640 }
Romain Guyd55a8612010-06-28 17:42:46 -0700641
Chet Haased48885a2012-08-28 17:43:28 -0700642 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700643 }
Romain Guyd55a8612010-06-28 17:42:46 -0700644
645 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700646}
647
648int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
649 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700650 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700651 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700652 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700653 SkPaint paint;
654 paint.setAlpha(alpha);
655 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700656 }
Romain Guyd55a8612010-06-28 17:42:46 -0700657}
Romain Guybd6b79b2010-06-26 00:13:53 -0700658
Romain Guy1c740bc2010-09-13 18:00:09 -0700659/**
660 * Layers are viewed by Skia are slightly different than layers in image editing
661 * programs (for instance.) When a layer is created, previously created layers
662 * and the frame buffer still receive every drawing command. For instance, if a
663 * layer is created and a shape intersecting the bounds of the layers and the
664 * framebuffer is draw, the shape will be drawn on both (unless the layer was
665 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
666 *
667 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
668 * texture. Unfortunately, this is inefficient as it requires every primitive to
669 * be drawn n + 1 times, where n is the number of active layers. In practice this
670 * means, for every primitive:
671 * - Switch active frame buffer
672 * - Change viewport, clip and projection matrix
673 * - Issue the drawing
674 *
675 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700676 * To avoid this, layers are implemented in a different way here, at least in the
677 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
678 * is set. When this flag is set we can redirect all drawing operations into a
679 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700680 *
681 * This implementation relies on the frame buffer being at least RGBA 8888. When
682 * a layer is created, only a texture is created, not an FBO. The content of the
683 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700684 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700685 * buffer and drawing continues as normal. This technique therefore treats the
686 * frame buffer as a scratch buffer for the layers.
687 *
688 * To compose the layers back onto the frame buffer, each layer texture
689 * (containing the original frame buffer data) is drawn as a simple quad over
690 * the frame buffer. The trick is that the quad is set as the composition
691 * destination in the blending equation, and the frame buffer becomes the source
692 * of the composition.
693 *
694 * Drawing layers with an alpha value requires an extra step before composition.
695 * An empty quad is drawn over the layer's region in the frame buffer. This quad
696 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
697 * quad is used to multiply the colors in the frame buffer. This is achieved by
698 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
699 * GL_ZERO, GL_SRC_ALPHA.
700 *
701 * Because glCopyTexImage2D() can be slow, an alternative implementation might
702 * be use to draw a single clipped layer. The implementation described above
703 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700704 *
705 * (1) The frame buffer is actually not cleared right away. To allow the GPU
706 * to potentially optimize series of calls to glCopyTexImage2D, the frame
707 * buffer is left untouched until the first drawing operation. Only when
708 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700709 */
Chet Haased48885a2012-08-28 17:43:28 -0700710bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
711 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700712 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700713 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700714
Romain Guyeb993562010-10-05 18:14:38 -0700715 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
716
Romain Guyf607bdc2010-09-10 19:20:06 -0700717 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700718 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700719 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700720 Rect untransformedBounds(bounds);
721 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700722
Chet Haased48885a2012-08-28 17:43:28 -0700723 // Layers only make sense if they are in the framebuffer's bounds
724 if (bounds.intersect(*mSnapshot->clipRect)) {
725 // We cannot work with sub-pixels in this case
726 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700727
Chet Haased48885a2012-08-28 17:43:28 -0700728 // When the layer is not an FBO, we may use glCopyTexImage so we
729 // need to make sure the layer does not extend outside the bounds
730 // of the framebuffer
731 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700732 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700733 } else if (fboLayer) {
734 clip.set(bounds);
735 mat4 inverse;
736 inverse.loadInverse(*mSnapshot->transform);
737 inverse.mapRect(clip);
738 clip.snapToPixelBoundaries();
739 if (clip.intersect(untransformedBounds)) {
740 clip.translate(-left, -top);
741 bounds.set(untransformedBounds);
742 } else {
743 clip.setEmpty();
744 }
Romain Guyad37cd32011-03-15 11:12:25 -0700745 }
Chet Haased48885a2012-08-28 17:43:28 -0700746 } else {
747 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700748 }
Romain Guybf434112010-09-16 14:40:17 -0700749
Romain Guy746b7402010-10-26 16:27:31 -0700750 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700751 bounds.getHeight() > mCaches.maxTextureSize ||
752 (fboLayer && clip.isEmpty())) {
753 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700754 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700755 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700756 }
757
758 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700759 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700760 return false;
761 }
Romain Guyf18fd992010-07-08 11:45:51 -0700762
Romain Guya1d3c912011-12-13 14:55:06 -0800763 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700764 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700765 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700766 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700767 }
768
Romain Guy9ace8f52011-07-07 20:50:11 -0700769 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700770 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700771 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
772 bounds.getWidth() / float(layer->getWidth()), 0.0f);
773 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700774 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700775 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700776
Romain Guy8fb95422010-08-17 18:38:51 -0700777 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700778 mSnapshot->flags |= Snapshot::kFlagIsLayer;
779 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700780
Romain Guyeb993562010-10-05 18:14:38 -0700781 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700782 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700783 } else {
784 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700785 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800786 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700787 if (layer->isEmpty()) {
788 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700789 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700790 layer->getWidth(), layer->getHeight(), 0);
791 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800792 } else {
793 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700794 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800795 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700796
Romain Guy54be1cd2011-06-13 19:04:27 -0700797 // Enqueue the buffer coordinates to clear the corresponding region later
798 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700799 }
Romain Guyeb993562010-10-05 18:14:38 -0700800 }
Romain Guyf86ef572010-07-01 11:05:42 -0700801
Romain Guyd55a8612010-06-28 17:42:46 -0700802 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700803}
804
Chet Haased48885a2012-08-28 17:43:28 -0700805bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800806 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700807 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700808
Chet Haased48885a2012-08-28 17:43:28 -0700809 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800810 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
811 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700812 mSnapshot->fbo = layer->getFbo();
813 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
814 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
815 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
816 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700817 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700818
Romain Guy2b7028e2012-09-19 17:25:38 -0700819 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700820 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700821 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700822 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
823 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700824
825 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700826 if (layer->isEmpty()) {
827 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
828 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700829 }
830
831 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700832 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700833
Romain Guyf735c8e2013-01-31 17:45:55 -0800834 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700835
836 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700837 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800838 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700839 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700840 glClear(GL_COLOR_BUFFER_BIT);
841
842 dirtyClip();
843
844 // Change the ortho projection
845 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
846 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
847
848 return true;
849}
850
Romain Guy1c740bc2010-09-13 18:00:09 -0700851/**
852 * Read the documentation of createLayer() before doing anything in this method.
853 */
Romain Guy1d83e192010-08-17 11:37:00 -0700854void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
855 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000856 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700857 return;
858 }
859
Romain Guy8ce00302013-01-15 18:51:42 -0800860 Layer* layer = current->layer;
861 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700862 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700863
864 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700865 endTiling();
866
Romain Guye0aa84b2012-04-03 19:30:26 -0700867 // Detach the texture from the FBO
868 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800869
870 layer->removeFbo(false);
871
Romain Guyeb993562010-10-05 18:14:38 -0700872 // Unbind current FBO and restore previous one
873 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700874 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700875
876 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700877 }
878
Romain Guy9ace8f52011-07-07 20:50:11 -0700879 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700880 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700881 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700882 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700883 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700884 }
885
Romain Guy03750a02010-10-18 14:06:08 -0700886 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700887
Romain Guya1d3c912011-12-13 14:55:06 -0800888 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700889
Romain Guy5b3b3522010-10-27 18:57:51 -0700890 // When the layer is stored in an FBO, we can save a bit of fillrate by
891 // drawing only the dirty region
892 if (fboLayer) {
893 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700894 if (layer->getColorFilter()) {
895 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800896 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700897 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700898 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800899 resetColorFilter();
900 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700901 } else if (!rect.isEmpty()) {
902 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
903 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700904 }
Romain Guy8b55f372010-08-18 17:10:07 -0700905
Romain Guy746b7402010-10-26 16:27:31 -0700906 dirtyClip();
907
Romain Guyeb993562010-10-05 18:14:38 -0700908 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700909 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700910 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700911 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700912 }
913}
914
Romain Guyaa6c24c2011-04-28 18:40:04 -0700915void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700916 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700917
918 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700919 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700920 setupDrawWithTexture();
921 } else {
922 setupDrawWithExternalTexture();
923 }
924 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700925 setupDrawColor(alpha, alpha, alpha, alpha);
926 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700927 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700928 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700929 setupDrawPureColorUniforms();
930 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700931 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
932 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700933 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700934 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700935 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700936 if (mSnapshot->transform->isPureTranslate() &&
937 layer->getWidth() == (uint32_t) rect.getWidth() &&
938 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700939 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
940 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
941
Romain Guyd21b6e12011-11-30 20:21:23 -0800942 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700943 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
944 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800945 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700946 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
947 }
948 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700949 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
950
951 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
952
953 finishDrawTexture();
954}
955
Romain Guy5b3b3522010-10-27 18:57:51 -0700956void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700957 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700958 const Rect& texCoords = layer->texCoords;
959 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
960 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700961
Romain Guy9ace8f52011-07-07 20:50:11 -0700962 float x = rect.left;
963 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700964 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700965 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700966 layer->getHeight() == (uint32_t) rect.getHeight();
967
968 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700969 // When we're swapping, the layer is already in screen coordinates
970 if (!swap) {
971 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
972 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
973 }
974
Romain Guyd21b6e12011-11-30 20:21:23 -0800975 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700976 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800977 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700978 }
979
980 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
981 layer->getTexture(), layer->getAlpha() / 255.0f,
982 layer->getMode(), layer->isBlend(),
983 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
984 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700985
Romain Guyaa6c24c2011-04-28 18:40:04 -0700986 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
987 } else {
988 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
989 drawTextureLayer(layer, rect);
990 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
991 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700992}
993
994void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700995 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700996 layer->setRegionAsRect();
997
Romain Guy40667672011-03-18 14:34:03 -0700998 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700999
Romain Guy5b3b3522010-10-27 18:57:51 -07001000 layer->region.clear();
1001 return;
1002 }
1003
Romain Guy8a3957d2011-09-07 17:55:15 -07001004 // TODO: See LayerRenderer.cpp::generateMesh() for important
1005 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -08001006 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001007 size_t count;
1008 const android::Rect* rects = layer->region.getArray(&count);
1009
Romain Guy9ace8f52011-07-07 20:50:11 -07001010 const float alpha = layer->getAlpha() / 255.0f;
1011 const float texX = 1.0f / float(layer->getWidth());
1012 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001013 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001014
Romain Guy8ce00302013-01-15 18:51:42 -08001015 setupDraw();
1016
1017 // We must get (and therefore bind) the region mesh buffer
1018 // after we setup drawing in case we need to mess with the
1019 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001020 TextureVertex* mesh = mCaches.getRegionMesh();
1021 GLsizei numQuads = 0;
1022
Romain Guy7230a742011-01-10 22:26:16 -08001023 setupDrawWithTexture();
1024 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001025 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001026 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001027 setupDrawProgram();
1028 setupDrawDirtyRegionsDisabled();
1029 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001030 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001031 setupDrawTexture(layer->getTexture());
1032 if (mSnapshot->transform->isPureTranslate()) {
1033 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
1034 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
1035
Romain Guyd21b6e12011-11-30 20:21:23 -08001036 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001037 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1038 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001039 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001040 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1041 }
Romain Guy15bc6432011-12-13 13:11:32 -08001042 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001043
1044 for (size_t i = 0; i < count; i++) {
1045 const android::Rect* r = &rects[i];
1046
1047 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001048 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001049 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001050 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001051
1052 // TODO: Reject quads outside of the clip
1053 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1054 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1055 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1056 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1057
1058 numQuads++;
1059
1060 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1061 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1062 numQuads = 0;
1063 mesh = mCaches.getRegionMesh();
1064 }
1065 }
1066
1067 if (numQuads > 0) {
1068 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1069 }
1070
Romain Guy7230a742011-01-10 22:26:16 -08001071 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001072
1073#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001074 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001075#endif
1076
1077 layer->region.clear();
1078 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001079}
1080
Romain Guy3a3133d2011-02-01 22:59:58 -08001081void OpenGLRenderer::drawRegionRects(const Region& region) {
1082#if DEBUG_LAYERS_AS_REGIONS
1083 size_t count;
1084 const android::Rect* rects = region.getArray(&count);
1085
1086 uint32_t colors[] = {
1087 0x7fff0000, 0x7f00ff00,
1088 0x7f0000ff, 0x7fff00ff,
1089 };
1090
1091 int offset = 0;
1092 int32_t top = rects[0].top;
1093
1094 for (size_t i = 0; i < count; i++) {
1095 if (top != rects[i].top) {
1096 offset ^= 0x2;
1097 top = rects[i].top;
1098 }
1099
1100 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1101 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1102 SkXfermode::kSrcOver_Mode);
1103 }
1104#endif
1105}
1106
Romain Guy8ce00302013-01-15 18:51:42 -08001107void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1108 SkXfermode::Mode mode, bool dirty) {
1109 int count = 0;
1110 Vector<float> rects;
1111
1112 SkRegion::Iterator it(region);
1113 while (!it.done()) {
1114 const SkIRect& r = it.rect();
1115 rects.push(r.fLeft);
1116 rects.push(r.fTop);
1117 rects.push(r.fRight);
1118 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001119 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001120 it.next();
1121 }
1122
Romain Guy3bbacf22013-02-06 16:51:04 -08001123 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001124}
1125
Romain Guy5b3b3522010-10-27 18:57:51 -07001126void OpenGLRenderer::dirtyLayer(const float left, const float top,
1127 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001128 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001129 Rect bounds(left, top, right, bottom);
1130 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001131 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001132 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001133}
1134
1135void OpenGLRenderer::dirtyLayer(const float left, const float top,
1136 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001137 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001138 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001139 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001140 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001141}
1142
1143void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001144 if (bounds.intersect(*mSnapshot->clipRect)) {
1145 bounds.snapToPixelBoundaries();
1146 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1147 if (!dirty.isEmpty()) {
1148 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001149 }
1150 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001151}
1152
Romain Guy54be1cd2011-06-13 19:04:27 -07001153void OpenGLRenderer::clearLayerRegions() {
1154 const size_t count = mLayers.size();
1155 if (count == 0) return;
1156
1157 if (!mSnapshot->isIgnored()) {
1158 // Doing several glScissor/glClear here can negatively impact
1159 // GPUs with a tiler architecture, instead we draw quads with
1160 // the Clear blending mode
1161
1162 // The list contains bounds that have already been clipped
1163 // against their initial clip rect, and the current clip
1164 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001165 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001166
1167 Vertex mesh[count * 6];
1168 Vertex* vertex = mesh;
1169
1170 for (uint32_t i = 0; i < count; i++) {
1171 Rect* bounds = mLayers.itemAt(i);
1172
1173 Vertex::set(vertex++, bounds->left, bounds->bottom);
1174 Vertex::set(vertex++, bounds->left, bounds->top);
1175 Vertex::set(vertex++, bounds->right, bounds->top);
1176 Vertex::set(vertex++, bounds->left, bounds->bottom);
1177 Vertex::set(vertex++, bounds->right, bounds->top);
1178 Vertex::set(vertex++, bounds->right, bounds->bottom);
1179
1180 delete bounds;
1181 }
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 Guy8d4aeb72013-02-12 16:08:55 -08001276 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001277 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001278 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001279
Romain Guyf735c8e2013-01-31 17:45:55 -08001280 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001281 }
1282}
1283
1284void OpenGLRenderer::setStencilFromClip() {
1285 if (!mCaches.debugOverdraw) {
1286 if (!mSnapshot->clipRegion->isEmpty()) {
1287 // NOTE: The order here is important, we must set dirtyClip to false
1288 // before any draw call to avoid calling back into this method
1289 mDirtyClip = false;
1290
1291 ensureStencilBuffer();
1292
1293 mCaches.stencil.enableWrite();
1294
1295 // Clear the stencil but first make sure we restrict drawing
1296 // to the region's bounds
1297 bool resetScissor = mCaches.enableScissor();
1298 if (resetScissor) {
1299 // The scissor was not set so we now need to update it
1300 setScissorFromClip();
1301 }
1302 mCaches.stencil.clear();
1303 if (resetScissor) mCaches.disableScissor();
1304
1305 // NOTE: We could use the region contour path to generate a smaller mesh
1306 // Since we are using the stencil we could use the red book path
1307 // drawing technique. It might increase bandwidth usage though.
1308
1309 // The last parameter is important: we are not drawing in the color buffer
1310 // so we don't want to dirty the current layer, if any
1311 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1312
1313 mCaches.stencil.enableTest();
1314 } else {
1315 mCaches.stencil.disable();
1316 }
1317 }
1318}
1319
Romain Guy9d5316e2010-06-24 19:30:36 -07001320const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001321 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001322}
1323
Romain Guy8a4ac612012-07-17 17:32:48 -07001324bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1325 if (mSnapshot->isIgnored()) {
1326 return true;
1327 }
1328
1329 Rect r(left, top, right, bottom);
1330 mSnapshot->transform->mapRect(r);
1331 r.snapToPixelBoundaries();
1332
1333 Rect clipRect(*mSnapshot->clipRect);
1334 clipRect.snapToPixelBoundaries();
1335
1336 return !clipRect.intersects(r);
1337}
1338
Romain Guy35643dd2012-09-18 15:40:58 -07001339bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1340 Rect& transformed, Rect& clip) {
1341 if (mSnapshot->isIgnored()) {
1342 return true;
1343 }
1344
1345 transformed.set(left, top, right, bottom);
1346 mSnapshot->transform->mapRect(transformed);
1347 transformed.snapToPixelBoundaries();
1348
1349 clip.set(*mSnapshot->clipRect);
1350 clip.snapToPixelBoundaries();
1351
1352 return !clip.intersects(transformed);
1353}
1354
Romain Guy672433d2013-01-04 19:05:13 -08001355bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1356 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001357 if (paint->getStyle() != SkPaint::kFill_Style) {
1358 float outset = paint->getStrokeWidth() * 0.5f;
1359 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1360 } else {
1361 return quickReject(left, top, right, bottom);
1362 }
1363}
1364
Romain Guyc7d53492010-06-25 13:41:57 -07001365bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001366 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001367 return true;
1368 }
1369
Romain Guy1d83e192010-08-17 11:37:00 -07001370 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001371 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001372 r.snapToPixelBoundaries();
1373
1374 Rect clipRect(*mSnapshot->clipRect);
1375 clipRect.snapToPixelBoundaries();
1376
Romain Guy586cae32012-07-13 15:28:31 -07001377 bool rejected = !clipRect.intersects(r);
1378 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001379 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001380 }
1381
1382 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001383}
1384
Romain Guy8ce00302013-01-15 18:51:42 -08001385void OpenGLRenderer::debugClip() {
1386#if DEBUG_CLIP_REGIONS
1387 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1388 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1389 }
1390#endif
1391}
1392
Romain Guy079ba2c2010-07-16 14:12:24 -07001393bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001394 if (CC_LIKELY(mSnapshot->transform->rectToRect())) {
1395 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1396 if (clipped) {
1397 dirtyClip();
1398 }
1399 return !mSnapshot->clipRect->isEmpty();
1400 }
1401
1402 SkPath path;
1403 path.addRect(left, top, right, bottom);
1404
1405 return clipPath(&path, op);
1406}
1407
1408bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1409 SkMatrix transform;
1410 mSnapshot->transform->copyTo(transform);
1411
1412 SkPath transformed;
1413 path->transform(transform, &transformed);
1414
1415 SkRegion clip;
1416 if (!mSnapshot->clipRegion->isEmpty()) {
1417 clip.setRegion(*mSnapshot->clipRegion);
1418 } else {
1419 Rect* bounds = mSnapshot->clipRect;
1420 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1421 }
1422
1423 SkRegion region;
1424 region.setPath(transformed, clip);
1425
1426 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001427 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001428 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001429 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001430 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001431}
1432
Romain Guy735738c2012-12-03 12:34:51 -08001433bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001434 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1435 if (clipped) {
1436 dirtyClip();
1437 }
1438 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001439}
1440
Chet Haasea23eed82012-04-12 15:19:04 -07001441Rect* OpenGLRenderer::getClipRect() {
1442 return mSnapshot->clipRect;
1443}
1444
Romain Guyf6a11b82010-06-23 17:47:49 -07001445///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001446// Drawing commands
1447///////////////////////////////////////////////////////////////////////////////
1448
Romain Guy54be1cd2011-06-13 19:04:27 -07001449void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001450 // TODO: It would be best if we could do this before quickReject()
1451 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001452 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001453 // Make sure setScissor & setStencil happen at the beginning of
1454 // this method
Romain Guy70ca14e2010-12-13 18:24:33 -08001455 if (mDirtyClip) {
1456 setScissorFromClip();
Romain Guy8ce00302013-01-15 18:51:42 -08001457 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001458 }
1459 mDescription.reset();
1460 mSetShaderColor = false;
1461 mColorSet = false;
1462 mColorA = mColorR = mColorG = mColorB = 0.0f;
1463 mTextureUnit = 0;
1464 mTrackDirtyRegions = true;
1465}
1466
1467void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1468 mDescription.hasTexture = true;
1469 mDescription.hasAlpha8Texture = isAlpha8;
1470}
1471
Romain Guyff316ec2013-02-13 18:39:43 -08001472void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1473 mDescription.hasTexture = true;
1474 mDescription.hasColors = true;
1475 mDescription.hasAlpha8Texture = isAlpha8;
1476}
1477
Romain Guyaa6c24c2011-04-28 18:40:04 -07001478void OpenGLRenderer::setupDrawWithExternalTexture() {
1479 mDescription.hasExternalTexture = true;
1480}
1481
Romain Guy15bc6432011-12-13 13:11:32 -08001482void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001483 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001484}
1485
Chris Craik710f46d2012-09-17 17:25:49 -07001486void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001487 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001488}
1489
Romain Guyed6fcb02011-03-21 13:11:28 -07001490void OpenGLRenderer::setupDrawPoint(float pointSize) {
1491 mDescription.isPoint = true;
1492 mDescription.pointSize = pointSize;
1493}
1494
Romain Guy8d0d4782010-12-14 20:13:35 -08001495void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1496 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001497 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1498 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1499 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001500 mColorSet = true;
1501 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1502}
1503
Romain Guy86568192010-12-14 15:55:39 -08001504void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1505 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001506 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1507 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1508 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001509 mColorSet = true;
1510 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1511}
1512
Romain Guy41210632012-07-16 17:04:24 -07001513void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1514 mCaches.fontRenderer->describe(mDescription, paint);
1515}
1516
Romain Guy70ca14e2010-12-13 18:24:33 -08001517void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1518 mColorA = a;
1519 mColorR = r;
1520 mColorG = g;
1521 mColorB = b;
1522 mColorSet = true;
1523 mSetShaderColor = mDescription.setColor(r, g, b, a);
1524}
1525
1526void OpenGLRenderer::setupDrawShader() {
1527 if (mShader) {
Romain Guy3bbacf22013-02-06 16:51:04 -08001528 mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001529 }
1530}
1531
1532void OpenGLRenderer::setupDrawColorFilter() {
1533 if (mColorFilter) {
Romain Guy3bbacf22013-02-06 16:51:04 -08001534 mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001535 }
1536}
1537
Romain Guyf09ef512011-05-27 11:43:46 -07001538void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1539 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1540 mColorA = 1.0f;
1541 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001542 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001543 }
1544}
1545
Romain Guy70ca14e2010-12-13 18:24:33 -08001546void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001547 // When the blending mode is kClear_Mode, we need to use a modulate color
1548 // argb=1,0,0,0
1549 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001550 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1551 mDescription, swapSrcDst);
1552}
1553
1554void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001555 // When the blending mode is kClear_Mode, we need to use a modulate color
1556 // argb=1,0,0,0
1557 accountForClear(mode);
Romain Guye83221c2012-09-24 16:01:35 -07001558 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()) ||
1559 (mColorFilter && mColorFilter->blend()), mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001560}
1561
1562void OpenGLRenderer::setupDrawProgram() {
1563 useProgram(mCaches.programCache.get(mDescription));
1564}
1565
1566void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1567 mTrackDirtyRegions = false;
1568}
1569
1570void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1571 bool ignoreTransform) {
1572 mModelView.loadTranslate(left, top, 0.0f);
1573 if (!ignoreTransform) {
1574 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1575 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1576 } else {
1577 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1578 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1579 }
1580}
1581
Chet Haase8a5cc922011-04-26 07:28:09 -07001582void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1583 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001584}
1585
Romain Guy70ca14e2010-12-13 18:24:33 -08001586void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1587 bool ignoreTransform, bool ignoreModelView) {
1588 if (!ignoreModelView) {
1589 mModelView.loadTranslate(left, top, 0.0f);
1590 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001591 } else {
1592 mModelView.loadIdentity();
1593 }
Romain Guy86568192010-12-14 15:55:39 -08001594 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1595 if (!ignoreTransform) {
1596 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1597 if (mTrackDirtyRegions && dirty) {
1598 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1599 }
1600 } else {
1601 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1602 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1603 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001604}
1605
Romain Guyed6fcb02011-03-21 13:11:28 -07001606void OpenGLRenderer::setupDrawPointUniforms() {
1607 int slot = mCaches.currentProgram->getUniform("pointSize");
1608 glUniform1f(slot, mDescription.pointSize);
1609}
1610
Romain Guy70ca14e2010-12-13 18:24:33 -08001611void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001612 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001613 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1614 }
1615}
1616
Romain Guy86568192010-12-14 15:55:39 -08001617void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001618 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001619 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001620 }
1621}
1622
Romain Guy70ca14e2010-12-13 18:24:33 -08001623void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1624 if (mShader) {
1625 if (ignoreTransform) {
1626 mModelView.loadInverse(*mSnapshot->transform);
1627 }
1628 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1629 }
1630}
1631
Romain Guy8d0d4782010-12-14 20:13:35 -08001632void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1633 if (mShader) {
1634 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1635 }
1636}
1637
Romain Guy70ca14e2010-12-13 18:24:33 -08001638void OpenGLRenderer::setupDrawColorFilterUniforms() {
1639 if (mColorFilter) {
1640 mColorFilter->setupProgram(mCaches.currentProgram);
1641 }
1642}
1643
Romain Guy41210632012-07-16 17:04:24 -07001644void OpenGLRenderer::setupDrawTextGammaUniforms() {
1645 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1646}
1647
Romain Guy70ca14e2010-12-13 18:24:33 -08001648void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001649 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001650 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001651 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001652}
1653
1654void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1655 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001656 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001657 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001658}
1659
Romain Guyaa6c24c2011-04-28 18:40:04 -07001660void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1661 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001662 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001663 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001664}
1665
Romain Guy8f0095c2011-05-02 17:24:22 -07001666void OpenGLRenderer::setupDrawTextureTransform() {
1667 mDescription.hasTextureTransform = true;
1668}
1669
1670void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001671 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1672 GL_FALSE, &transform.data[0]);
1673}
1674
Romain Guy70ca14e2010-12-13 18:24:33 -08001675void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001676 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001677 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001678 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001679 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001680 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001681 }
Romain Guyd71dd362011-12-12 19:03:35 -08001682
Chris Craikcb4d6002012-09-25 12:00:29 -07001683 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001684 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001685 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001686 }
1687
1688 mCaches.unbindIndicesBuffer();
1689}
1690
Romain Guyff316ec2013-02-13 18:39:43 -08001691void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1692 bool force = mCaches.unbindMeshBuffer();
1693 GLsizei stride = sizeof(ColorTextureVertex);
1694
1695 mCaches.bindPositionVertexPointer(force, vertices, stride);
1696 if (mCaches.currentProgram->texCoords >= 0) {
1697 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1698 }
1699 int slot = mCaches.currentProgram->getAttrib("colors");
1700 if (slot >= 0) {
1701 glEnableVertexAttribArray(slot);
1702 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1703 }
1704
1705 mCaches.unbindIndicesBuffer();
1706}
1707
Romain Guy15bc6432011-12-13 13:11:32 -08001708void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1709 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001710 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001711 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001712 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001713 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001714}
1715
Chet Haase5b0200b2011-04-13 17:58:08 -07001716void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001717 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001718 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001719 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001720}
1721
Romain Guy70ca14e2010-12-13 18:24:33 -08001722void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001723}
1724
1725///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001726// Drawing
1727///////////////////////////////////////////////////////////////////////////////
1728
Chet Haase1271e2c2012-04-20 09:54:27 -07001729status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001730 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001731
Romain Guy0fe478e2010-11-08 12:08:41 -08001732 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1733 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001734 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001735 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001736 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001737
Romain Guy65549432012-03-26 16:45:05 -07001738 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001739}
1740
Chet Haaseed30fd82011-04-22 16:18:45 -07001741void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1742 if (displayList) {
Chris Craik2af46352012-11-26 18:30:17 -08001743 displayList->output(level);
Chet Haaseed30fd82011-04-22 16:18:45 -07001744 }
1745}
1746
Romain Guya168d732011-03-18 16:50:13 -07001747void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1748 int alpha;
1749 SkXfermode::Mode mode;
1750 getAlphaAndMode(paint, &alpha, &mode);
1751
Romain Guy886b2752013-01-04 12:26:18 -08001752 int color = paint != NULL ? paint->getColor() : 0;
1753
Romain Guya168d732011-03-18 16:50:13 -07001754 float x = left;
1755 float y = top;
1756
Romain Guy886b2752013-01-04 12:26:18 -08001757 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1758
Romain Guya168d732011-03-18 16:50:13 -07001759 bool ignoreTransform = false;
1760 if (mSnapshot->transform->isPureTranslate()) {
1761 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1762 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1763 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001764
1765 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001766 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001767 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001768 }
1769
Romain Guy886b2752013-01-04 12:26:18 -08001770 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1771 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1772 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001773}
1774
Chet Haase48659092012-05-31 15:21:51 -07001775status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001776 const float right = left + bitmap->width();
1777 const float bottom = top + bitmap->height();
1778
1779 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001780 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001781 }
1782
Romain Guya1d3c912011-12-13 14:55:06 -08001783 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001784 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001785 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001786 const AutoTexture autoCleanup(texture);
1787
Romain Guy211370f2012-02-01 16:10:55 -08001788 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001789 drawAlphaBitmap(texture, left, top, paint);
1790 } else {
1791 drawTextureRect(left, top, right, bottom, texture, paint);
1792 }
Chet Haase48659092012-05-31 15:21:51 -07001793
1794 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001795}
1796
Chet Haase48659092012-05-31 15:21:51 -07001797status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001798 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1799 const mat4 transform(*matrix);
1800 transform.mapRect(r);
1801
Romain Guy6926c722010-07-12 20:20:03 -07001802 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001803 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001804 }
1805
Romain Guya1d3c912011-12-13 14:55:06 -08001806 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001807 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001808 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001809 const AutoTexture autoCleanup(texture);
1810
Romain Guy5b3b3522010-10-27 18:57:51 -07001811 // This could be done in a cheaper way, all we need is pass the matrix
1812 // to the vertex shader. The save/restore is a bit overkill.
1813 save(SkCanvas::kMatrix_SaveFlag);
1814 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08001815 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1816 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
1817 } else {
1818 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1819 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001820 restore();
Chet Haase48659092012-05-31 15:21:51 -07001821
1822 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001823}
1824
Chet Haase48659092012-05-31 15:21:51 -07001825status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001826 const float right = left + bitmap->width();
1827 const float bottom = top + bitmap->height();
1828
1829 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001830 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001831 }
1832
1833 mCaches.activeTexture(0);
1834 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1835 const AutoTexture autoCleanup(texture);
1836
Romain Guy886b2752013-01-04 12:26:18 -08001837 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1838 drawAlphaBitmap(texture, left, top, paint);
1839 } else {
1840 drawTextureRect(left, top, right, bottom, texture, paint);
1841 }
Chet Haase48659092012-05-31 15:21:51 -07001842
1843 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001844}
1845
Chet Haase48659092012-05-31 15:21:51 -07001846status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001847 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001848 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001849 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001850 }
1851
Romain Guyb18d2d02011-02-10 15:52:54 -08001852 float left = FLT_MAX;
1853 float top = FLT_MAX;
1854 float right = FLT_MIN;
1855 float bottom = FLT_MIN;
1856
Romain Guya92bb4d2012-10-16 11:08:44 -07001857 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001858
Romain Guyff316ec2013-02-13 18:39:43 -08001859 ColorTextureVertex mesh[count];
1860 ColorTextureVertex* vertex = mesh;
1861
1862 bool cleanupColors = false;
1863 if (!colors) {
1864 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
1865 colors = new int[colorsCount];
1866 memset(colors, 0xff, colorsCount * sizeof(int));
1867 cleanupColors = true;
1868 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001869
Romain Guy5a7b4662011-01-20 19:09:30 -08001870 for (int32_t y = 0; y < meshHeight; y++) {
1871 for (int32_t x = 0; x < meshWidth; x++) {
1872 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1873
1874 float u1 = float(x) / meshWidth;
1875 float u2 = float(x + 1) / meshWidth;
1876 float v1 = float(y) / meshHeight;
1877 float v2 = float(y + 1) / meshHeight;
1878
1879 int ax = i + (meshWidth + 1) * 2;
1880 int ay = ax + 1;
1881 int bx = i;
1882 int by = bx + 1;
1883 int cx = i + 2;
1884 int cy = cx + 1;
1885 int dx = i + (meshWidth + 1) * 2 + 2;
1886 int dy = dx + 1;
1887
Romain Guyff316ec2013-02-13 18:39:43 -08001888 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1889 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
1890 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08001891
Romain Guyff316ec2013-02-13 18:39:43 -08001892 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1893 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
1894 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08001895
Romain Guya92bb4d2012-10-16 11:08:44 -07001896 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1897 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1898 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1899 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001900 }
1901 }
1902
Romain Guya92bb4d2012-10-16 11:08:44 -07001903 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08001904 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07001905 return DrawGlInfo::kStatusDone;
1906 }
1907
1908 mCaches.activeTexture(0);
1909 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guyff316ec2013-02-13 18:39:43 -08001910 if (!texture) {
1911 if (cleanupColors) delete[] colors;
1912 return DrawGlInfo::kStatusDone;
1913 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001914 const AutoTexture autoCleanup(texture);
1915
1916 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1917 texture->setFilter(FILTER(paint), true);
1918
1919 int alpha;
1920 SkXfermode::Mode mode;
1921 getAlphaAndMode(paint, &alpha, &mode);
1922
Romain Guyff316ec2013-02-13 18:39:43 -08001923 float a = alpha / 255.0f;
1924
Romain Guya92bb4d2012-10-16 11:08:44 -07001925 if (hasLayer()) {
Romain Guyb18d2d02011-02-10 15:52:54 -08001926 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1927 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001928
Romain Guyff316ec2013-02-13 18:39:43 -08001929 setupDraw();
1930 setupDrawWithTextureAndColor();
1931 setupDrawColor(a, a, a, a);
1932 setupDrawColorFilter();
1933 setupDrawBlending(true, mode, false);
1934 setupDrawProgram();
1935 setupDrawDirtyRegionsDisabled();
1936 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
1937 setupDrawTexture(texture->id);
1938 setupDrawPureColorUniforms();
1939 setupDrawColorFilterUniforms();
1940 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
1941
1942 glDrawArrays(GL_TRIANGLES, 0, count);
1943
1944 finishDrawTexture();
1945
1946 int slot = mCaches.currentProgram->getAttrib("colors");
1947 if (slot >= 0) {
1948 glDisableVertexAttribArray(slot);
1949 }
1950
1951 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07001952
1953 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001954}
1955
Chet Haase48659092012-05-31 15:21:51 -07001956status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001957 float srcLeft, float srcTop, float srcRight, float srcBottom,
1958 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001959 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001960 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001961 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001962 }
1963
Romain Guya1d3c912011-12-13 14:55:06 -08001964 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001965 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001966 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001967 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001968
Romain Guy8ba548f2010-06-30 19:21:21 -07001969 const float width = texture->width;
1970 const float height = texture->height;
1971
Romain Guy68169722011-08-22 17:33:33 -07001972 const float u1 = fmax(0.0f, srcLeft / width);
1973 const float v1 = fmax(0.0f, srcTop / height);
1974 const float u2 = fmin(1.0f, srcRight / width);
1975 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001976
Romain Guy03750a02010-10-18 14:06:08 -07001977 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001978 resetDrawTextureTexCoords(u1, v1, u2, v2);
1979
Romain Guy03750a02010-10-18 14:06:08 -07001980 int alpha;
1981 SkXfermode::Mode mode;
1982 getAlphaAndMode(paint, &alpha, &mode);
1983
Romain Guyd21b6e12011-11-30 20:21:23 -08001984 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1985
Romain Guy886b2752013-01-04 12:26:18 -08001986 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
1987 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08001988
Romain Guy886b2752013-01-04 12:26:18 -08001989 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
1990 // Apply a scale transform on the canvas only when a shader is in use
1991 // Skia handles the ratio between the dst and src rects as a scale factor
1992 // when a shader is set
1993 bool useScaleTransform = mShader && scaled;
1994 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07001995
Romain Guy886b2752013-01-04 12:26:18 -08001996 if (CC_LIKELY(mSnapshot->transform->isPureTranslate() && !useScaleTransform)) {
1997 float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1998 float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1999
2000 dstRight = x + (dstRight - dstLeft);
2001 dstBottom = y + (dstBottom - dstTop);
2002
2003 dstLeft = x;
2004 dstTop = y;
2005
2006 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2007 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002008 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002009 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002010 }
2011
2012 if (CC_UNLIKELY(useScaleTransform)) {
2013 save(SkCanvas::kMatrix_SaveFlag);
2014 translate(dstLeft, dstTop);
2015 scale(scaleX, scaleY);
2016
2017 dstLeft = 0.0f;
2018 dstTop = 0.0f;
2019
2020 dstRight = srcRight - srcLeft;
2021 dstBottom = srcBottom - srcTop;
2022 }
2023
2024 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2025 int color = paint ? paint->getColor() : 0;
2026 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2027 texture->id, paint != NULL, color, alpha, mode,
2028 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2029 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2030 } else {
2031 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2032 texture->id, alpha / 255.0f, mode, texture->blend,
2033 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2034 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2035 }
2036
2037 if (CC_UNLIKELY(useScaleTransform)) {
2038 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002039 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002040
2041 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002042
2043 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002044}
2045
Chet Haase48659092012-05-31 15:21:51 -07002046status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07002047 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07002048 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002049 int alpha;
2050 SkXfermode::Mode mode;
2051 getAlphaAndModeDirect(paint, &alpha, &mode);
2052
2053 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2054 left, top, right, bottom, alpha, mode);
2055}
2056
2057status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2058 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2059 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002060 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002061 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002062 }
2063
Romain Guybe6f9dc2012-07-16 12:41:17 -07002064 alpha *= mSnapshot->alpha;
2065
Romain Guya1d3c912011-12-13 14:55:06 -08002066 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002067 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002068 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002069 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08002070 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2071 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07002072
Romain Guy2728f962010-10-08 18:36:15 -07002073 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002074 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002075
Romain Guy211370f2012-02-01 16:10:55 -08002076 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002077 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002078 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002079 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002080 const float offsetX = left + mSnapshot->transform->getTranslateX();
2081 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002082 const size_t count = mesh->quads.size();
2083 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002084 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002085 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002086 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2087 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2088 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002089 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002090 dirtyLayer(left + bounds.left, top + bounds.top,
2091 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08002092 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002093 }
2094 }
2095
Romain Guy211370f2012-02-01 16:10:55 -08002096 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002097 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2098 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2099
2100 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2101 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2102 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2103 true, !mesh->hasEmptyQuads);
2104 } else {
2105 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2106 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2107 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2108 true, !mesh->hasEmptyQuads);
2109 }
Romain Guy054dc182010-10-15 17:55:25 -07002110 }
Chet Haase48659092012-05-31 15:21:51 -07002111
2112 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002113}
2114
Chris Craik65cd6122012-12-10 17:56:27 -08002115status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2116 bool useOffset) {
2117 if (!vertexBuffer.getSize()) {
2118 // no vertices to draw
2119 return DrawGlInfo::kStatusDone;
2120 }
2121
Chris Craikcb4d6002012-09-25 12:00:29 -07002122 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002123 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2124 bool isAA = paint->isAntiAlias();
2125
Chris Craik710f46d2012-09-17 17:25:49 -07002126 setupDraw();
2127 setupDrawNoTexture();
2128 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002129 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2130 setupDrawColorFilter();
2131 setupDrawShader();
2132 setupDrawBlending(isAA, mode);
2133 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002134 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002135 setupDrawColorUniforms();
2136 setupDrawColorFilterUniforms();
2137 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002138
Chris Craik710f46d2012-09-17 17:25:49 -07002139 void* vertices = vertexBuffer.getBuffer();
2140 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002141 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002142 mCaches.resetTexCoordsVertexPointer();
2143 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002144
Chris Craik710f46d2012-09-17 17:25:49 -07002145 int alphaSlot = -1;
2146 if (isAA) {
2147 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2148 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002149
Chris Craik710f46d2012-09-17 17:25:49 -07002150 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002151 glEnableVertexAttribArray(alphaSlot);
2152 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002153 }
Romain Guy04299382012-07-18 17:15:41 -07002154
Chris Craik710f46d2012-09-17 17:25:49 -07002155 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002156
Chris Craik710f46d2012-09-17 17:25:49 -07002157 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002158 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002159 }
Chris Craik65cd6122012-12-10 17:56:27 -08002160
2161 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002162}
2163
2164/**
Chris Craik65cd6122012-12-10 17:56:27 -08002165 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2166 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2167 * screen space in all directions. However, instead of using a fragment shader to compute the
2168 * translucency of the color from its position, we simply use a varying parameter to define how far
2169 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2170 *
2171 * Doesn't yet support joins, caps, or path effects.
2172 */
2173status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2174 VertexBuffer vertexBuffer;
2175 // TODO: try clipping large paths to viewport
2176 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2177
2178 SkRect bounds = path.getBounds();
2179 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2180 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
2181
2182 return drawVertexBuffer(vertexBuffer, paint);
2183}
2184
2185/**
2186 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2187 * and additional geometry for defining an alpha slope perimeter.
2188 *
2189 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2190 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2191 * in-shader alpha region, but found it to be taxing on some GPUs.
2192 *
2193 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2194 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002195 */
Chet Haase48659092012-05-31 15:21:51 -07002196status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002197 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002198
Chris Craik65cd6122012-12-10 17:56:27 -08002199 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002200
Chris Craik65cd6122012-12-10 17:56:27 -08002201 VertexBuffer buffer;
2202 SkRect bounds;
2203 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002204
2205 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2206 return DrawGlInfo::kStatusDone;
2207 }
2208
Chris Craik65cd6122012-12-10 17:56:27 -08002209 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy7b631422012-04-04 11:38:54 -07002210
Chris Craik65cd6122012-12-10 17:56:27 -08002211 bool useOffset = !paint->isAntiAlias();
2212 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002213}
2214
Chet Haase48659092012-05-31 15:21:51 -07002215status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2216 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002217
2218 // TODO: The paint's cap style defines whether the points are square or circular
2219 // TODO: Handle AA for round points
2220
Chet Haase5b0200b2011-04-13 17:58:08 -07002221 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002222 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002223 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002224 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002225 if (isHairLine) {
2226 // Now that we know it's hairline, we can set the effective width, to be used later
2227 strokeWidth = 1.0f;
2228 }
2229 const float halfWidth = strokeWidth / 2;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002230
Romain Guyed6fcb02011-03-21 13:11:28 -07002231 int alpha;
2232 SkXfermode::Mode mode;
2233 getAlphaAndMode(paint, &alpha, &mode);
2234
2235 int verticesCount = count >> 1;
2236 int generatedVerticesCount = 0;
2237
2238 TextureVertex pointsData[verticesCount];
2239 TextureVertex* vertex = &pointsData[0];
2240
Romain Guy04299382012-07-18 17:15:41 -07002241 // TODO: We should optimize this method to not generate vertices for points
2242 // that lie outside of the clip.
2243 mCaches.enableScissor();
2244
Romain Guyed6fcb02011-03-21 13:11:28 -07002245 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002246 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002247 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002248 setupDrawColor(paint->getColor(), alpha);
2249 setupDrawColorFilter();
2250 setupDrawShader();
2251 setupDrawBlending(mode);
2252 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002253 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002254 setupDrawColorUniforms();
2255 setupDrawColorFilterUniforms();
2256 setupDrawPointUniforms();
2257 setupDrawShaderIdentityUniforms();
2258 setupDrawMesh(vertex);
2259
2260 for (int i = 0; i < count; i += 2) {
2261 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2262 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002263
Chet Haase8a5cc922011-04-26 07:28:09 -07002264 float left = points[i] - halfWidth;
2265 float right = points[i] + halfWidth;
2266 float top = points[i + 1] - halfWidth;
2267 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002268
Chet Haase8a5cc922011-04-26 07:28:09 -07002269 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002270 }
2271
2272 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002273
2274 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002275}
2276
Chet Haase48659092012-05-31 15:21:51 -07002277status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002278 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002279 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002280
Romain Guyae88e5e2010-10-22 17:49:18 -07002281 Rect& clip(*mSnapshot->clipRect);
2282 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002283
Romain Guy3d58c032010-07-14 16:34:53 -07002284 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002285
2286 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002287}
Romain Guy9d5316e2010-06-24 19:30:36 -07002288
Chet Haase48659092012-05-31 15:21:51 -07002289status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2290 SkPaint* paint) {
2291 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002292 const AutoTexture autoCleanup(texture);
2293
2294 const float x = left + texture->left - texture->offset;
2295 const float y = top + texture->top - texture->offset;
2296
2297 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002298
2299 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002300}
2301
Chet Haase48659092012-05-31 15:21:51 -07002302status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002303 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002304 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002305 return DrawGlInfo::kStatusDone;
2306 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002307
Chris Craikcb4d6002012-09-25 12:00:29 -07002308 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002309 mCaches.activeTexture(0);
2310 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2311 right - left, bottom - top, rx, ry, p);
2312 return drawShape(left, top, texture, p);
2313 }
2314
2315 SkPath path;
2316 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002317 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2318 float outset = p->getStrokeWidth() / 2;
2319 rect.outset(outset, outset);
2320 rx += outset;
2321 ry += outset;
2322 }
Chris Craik710f46d2012-09-17 17:25:49 -07002323 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002324 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002325}
2326
Chris Craik710f46d2012-09-17 17:25:49 -07002327status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002328 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2329 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002330 return DrawGlInfo::kStatusDone;
2331 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002332 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002333 mCaches.activeTexture(0);
2334 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2335 return drawShape(x - radius, y - radius, texture, p);
2336 }
2337
2338 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002339 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2340 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2341 } else {
2342 path.addCircle(x, y, radius);
2343 }
Chris Craik65cd6122012-12-10 17:56:27 -08002344 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002345}
Romain Guy01d58e42011-01-19 21:54:02 -08002346
Chet Haase48659092012-05-31 15:21:51 -07002347status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002348 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002349 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002350 return DrawGlInfo::kStatusDone;
2351 }
Romain Guy01d58e42011-01-19 21:54:02 -08002352
Chris Craikcb4d6002012-09-25 12:00:29 -07002353 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002354 mCaches.activeTexture(0);
2355 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2356 return drawShape(left, top, texture, p);
2357 }
2358
2359 SkPath path;
2360 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002361 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2362 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2363 }
Chris Craik710f46d2012-09-17 17:25:49 -07002364 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002365 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002366}
2367
Chet Haase48659092012-05-31 15:21:51 -07002368status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002369 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
2370 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
2371 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002372 }
2373
Chris Craik780c1282012-10-04 14:10:49 -07002374 if (fabs(sweepAngle) >= 360.0f) {
2375 return drawOval(left, top, right, bottom, p);
2376 }
2377
2378 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002379 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002380 mCaches.activeTexture(0);
2381 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2382 startAngle, sweepAngle, useCenter, p);
2383 return drawShape(left, top, texture, p);
2384 }
2385
2386 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2387 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2388 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2389 }
2390
2391 SkPath path;
2392 if (useCenter) {
2393 path.moveTo(rect.centerX(), rect.centerY());
2394 }
2395 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2396 if (useCenter) {
2397 path.close();
2398 }
Chris Craik65cd6122012-12-10 17:56:27 -08002399 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002400}
2401
Romain Guycf8675e2012-10-02 12:32:25 -07002402// See SkPaintDefaults.h
2403#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2404
Chet Haase48659092012-05-31 15:21:51 -07002405status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002406 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002407 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002408 }
2409
Chris Craik710f46d2012-09-17 17:25:49 -07002410 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002411 // only fill style is supported by drawConvexPath, since others have to handle joins
2412 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2413 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2414 mCaches.activeTexture(0);
2415 const PathTexture* texture =
2416 mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2417 return drawShape(left, top, texture, p);
2418 }
2419
2420 SkPath path;
2421 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2422 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2423 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2424 }
2425 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002426 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002427 }
2428
Romain Guy181d0a62011-06-09 18:52:38 -07002429 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002430 SkPath path;
2431 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002432 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002433 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002434 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002435 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002436 }
Romain Guyc7d53492010-06-25 13:41:57 -07002437}
Romain Guy9d5316e2010-06-24 19:30:36 -07002438
Raph Levien416a8472012-07-19 22:48:17 -07002439void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2440 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2441 float x, float y) {
2442 mCaches.activeTexture(0);
2443
2444 // NOTE: The drop shadow will not perform gamma correction
2445 // if shader-based correction is enabled
2446 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2447 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2448 paint, text, bytesCount, count, mShadowRadius, positions);
2449 const AutoTexture autoCleanup(shadow);
2450
2451 const float sx = x - shadow->left + mShadowDx;
2452 const float sy = y - shadow->top + mShadowDy;
2453
2454 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2455 int shadowColor = mShadowColor;
2456 if (mShader) {
2457 shadowColor = 0xffffffff;
2458 }
2459
2460 setupDraw();
2461 setupDrawWithTexture(true);
2462 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2463 setupDrawColorFilter();
2464 setupDrawShader();
2465 setupDrawBlending(true, mode);
2466 setupDrawProgram();
2467 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2468 setupDrawTexture(shadow->id);
2469 setupDrawPureColorUniforms();
2470 setupDrawColorFilterUniforms();
2471 setupDrawShaderUniforms();
2472 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2473
2474 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2475}
2476
Chet Haase48659092012-05-31 15:21:51 -07002477status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002478 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002479 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002480 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002481 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002482 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002483
Romain Guy671d6cf2012-01-18 12:39:17 -08002484 // NOTE: Skia does not support perspective transform on drawPosText yet
2485 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002486 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002487 }
2488
2489 float x = 0.0f;
2490 float y = 0.0f;
2491 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2492 if (pureTranslate) {
2493 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2494 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2495 }
2496
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002497 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002498 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy671d6cf2012-01-18 12:39:17 -08002499
2500 int alpha;
2501 SkXfermode::Mode mode;
2502 getAlphaAndMode(paint, &alpha, &mode);
2503
Raph Levien416a8472012-07-19 22:48:17 -07002504 if (CC_UNLIKELY(mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002505 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2506 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002507 }
2508
Romain Guy671d6cf2012-01-18 12:39:17 -08002509 // Pick the appropriate texture filtering
2510 bool linearFilter = mSnapshot->transform->changesBounds();
2511 if (pureTranslate && !linearFilter) {
2512 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2513 }
2514
2515 mCaches.activeTexture(0);
2516 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002517 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002518 setupDrawDirtyRegionsDisabled();
2519 setupDrawWithTexture(true);
2520 setupDrawAlpha8Color(paint->getColor(), alpha);
2521 setupDrawColorFilter();
2522 setupDrawShader();
2523 setupDrawBlending(true, mode);
2524 setupDrawProgram();
2525 setupDrawModelView(x, y, x, y, pureTranslate, true);
2526 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2527 setupDrawPureColorUniforms();
2528 setupDrawColorFilterUniforms();
2529 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002530 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002531
2532 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2533 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2534
Romain Guy211370f2012-02-01 16:10:55 -08002535 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002536
2537 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2538 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002539 if (hasActiveLayer) {
2540 if (!pureTranslate) {
2541 mSnapshot->transform->mapRect(bounds);
2542 }
2543 dirtyLayerUnchecked(bounds, getRegion());
2544 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002545 }
Chet Haase48659092012-05-31 15:21:51 -07002546
2547 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002548}
2549
Romain Guyc2525952012-07-27 16:41:22 -07002550status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002551 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002552 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002553 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002554 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002555 }
2556
Chet Haasea1cff502012-02-21 13:43:44 -08002557 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002558 switch (paint->getTextAlign()) {
2559 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002560 x -= length / 2.0f;
2561 break;
2562 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002563 x -= length;
2564 break;
2565 default:
2566 break;
2567 }
2568
Romain Guycac5fd32011-12-01 20:08:50 -08002569 SkPaint::FontMetrics metrics;
2570 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002571 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002572 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002573 }
2574
Romain Guye3a9b242013-01-08 11:15:30 -08002575#if DEBUG_GLYPHS
2576 ALOGD("OpenGLRenderer drawText() with FontID=%d",
2577 SkTypeface::UniqueID(paint->getTypeface()));
2578#endif
2579
2580 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2581 fontRenderer.setFont(paint, *mSnapshot->transform);
2582
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002583 const float oldX = x;
2584 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002585 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002586 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002587 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2588 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2589 }
2590
Romain Guy86568192010-12-14 15:55:39 -08002591 int alpha;
2592 SkXfermode::Mode mode;
2593 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002594
Romain Guy211370f2012-02-01 16:10:55 -08002595 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002596 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2597 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002598 }
2599
Romain Guy6620c6d2010-12-06 18:07:02 -08002600 // Pick the appropriate texture filtering
2601 bool linearFilter = mSnapshot->transform->changesBounds();
2602 if (pureTranslate && !linearFilter) {
2603 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2604 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002605
Romain Guy16c88082012-06-11 16:03:47 -07002606 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002607 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002608 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002609 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002610 setupDrawDirtyRegionsDisabled();
2611 setupDrawWithTexture(true);
2612 setupDrawAlpha8Color(paint->getColor(), alpha);
2613 setupDrawColorFilter();
2614 setupDrawShader();
2615 setupDrawBlending(true, mode);
2616 setupDrawProgram();
2617 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002618 // See comment above; the font renderer must use texture unit 0
2619 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002620 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2621 setupDrawPureColorUniforms();
2622 setupDrawColorFilterUniforms();
2623 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002624 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002625
Romain Guya3dc55f2012-09-28 13:55:44 -07002626 const Rect* clip = pureTranslate ? mSnapshot->clipRect :
2627 (mSnapshot->hasPerspectiveTransform() ? NULL : &mSnapshot->getLocalClip());
Romain Guy5b3b3522010-10-27 18:57:51 -07002628 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2629
Romain Guy211370f2012-02-01 16:10:55 -08002630 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002631
Raph Levien996e57c2012-07-23 15:22:52 -07002632 bool status;
Romain Guya3dc55f2012-09-28 13:55:44 -07002633 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002634 SkPaint paintCopy(*paint);
2635 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2636 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002637 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002638 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002639 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002640 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002641 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002642
2643 if (status && hasActiveLayer) {
2644 if (!pureTranslate) {
2645 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002646 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002647 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002648 }
Romain Guy694b5192010-07-21 21:33:20 -07002649
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002650 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002651
2652 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002653}
2654
Chet Haase48659092012-05-31 15:21:51 -07002655status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002656 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002657 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2658 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002659 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002660 }
2661
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002662 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002663 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy03d58522012-02-24 17:54:07 -08002664
2665 int alpha;
2666 SkXfermode::Mode mode;
2667 getAlphaAndMode(paint, &alpha, &mode);
2668
2669 mCaches.activeTexture(0);
2670 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002671 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002672 setupDrawDirtyRegionsDisabled();
2673 setupDrawWithTexture(true);
2674 setupDrawAlpha8Color(paint->getColor(), alpha);
2675 setupDrawColorFilter();
2676 setupDrawShader();
2677 setupDrawBlending(true, mode);
2678 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002679 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002680 setupDrawTexture(fontRenderer.getTexture(true));
2681 setupDrawPureColorUniforms();
2682 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002683 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002684 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002685
Romain Guy97771732012-02-28 18:17:02 -08002686 const Rect* clip = &mSnapshot->getLocalClip();
2687 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 -08002688
Romain Guy97771732012-02-28 18:17:02 -08002689 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002690
2691 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2692 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002693 if (hasActiveLayer) {
2694 mSnapshot->transform->mapRect(bounds);
2695 dirtyLayerUnchecked(bounds, getRegion());
2696 }
Romain Guy97771732012-02-28 18:17:02 -08002697 }
Chet Haase48659092012-05-31 15:21:51 -07002698
2699 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002700}
2701
Chet Haase48659092012-05-31 15:21:51 -07002702status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2703 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002704
Romain Guya1d3c912011-12-13 14:55:06 -08002705 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002706
Romain Guyfb8b7632010-08-23 21:05:08 -07002707 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002708 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002709 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002710
Romain Guy8b55f372010-08-18 17:10:07 -07002711 const float x = texture->left - texture->offset;
2712 const float y = texture->top - texture->offset;
2713
Romain Guy01d58e42011-01-19 21:54:02 -08002714 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002715
2716 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002717}
2718
Chet Haase48659092012-05-31 15:21:51 -07002719status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002720 if (!layer) {
2721 return DrawGlInfo::kStatusDone;
2722 }
2723
Romain Guyb2e2f242012-10-17 18:18:35 -07002724 mat4* transform = NULL;
2725 if (layer->isTextureLayer()) {
2726 transform = &layer->getTransform();
2727 if (!transform->isIdentity()) {
2728 save(0);
2729 mSnapshot->transform->multiply(*transform);
2730 }
2731 }
2732
Romain Guy35643dd2012-09-18 15:40:58 -07002733 Rect transformed;
2734 Rect clip;
2735 const bool rejected = quickRejectNoScissor(x, y,
2736 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2737
2738 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002739 if (transform && !transform->isIdentity()) {
2740 restore();
2741 }
Chet Haase48659092012-05-31 15:21:51 -07002742 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002743 }
2744
Romain Guy5bb3c732012-11-29 17:52:58 -08002745 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002746
Romain Guy87e2f7572012-09-24 11:37:12 -07002747 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002748 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002749
Romain Guy211370f2012-02-01 16:10:55 -08002750 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guye529ece2012-09-26 11:23:17 -07002751 SkiaColorFilter* oldFilter = mColorFilter;
2752 mColorFilter = layer->getColorFilter();
2753
Romain Guyc88e3572011-01-22 00:32:12 -08002754 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002755 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002756 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002757 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002758 setupDraw();
2759 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002760 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002761 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002762 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002763 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002764 setupDrawPureColorUniforms();
2765 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002766 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002767 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002768 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2769 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002770
Romain Guyd21b6e12011-11-30 20:21:23 -08002771 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002772 setupDrawModelViewTranslate(tx, ty,
2773 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002774 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002775 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002776 setupDrawModelViewTranslate(x, y,
2777 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2778 }
Romain Guyc88e3572011-01-22 00:32:12 -08002779 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002780
Romain Guyc88e3572011-01-22 00:32:12 -08002781 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2782 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002783
Romain Guyc88e3572011-01-22 00:32:12 -08002784 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002785
2786#if DEBUG_LAYERS_AS_REGIONS
2787 drawRegionRects(layer->region);
2788#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002789 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002790
Romain Guye529ece2012-09-26 11:23:17 -07002791 mColorFilter = oldFilter;
2792
Romain Guy5bb3c732012-11-29 17:52:58 -08002793 if (layer->debugDrawUpdate) {
2794 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002795 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2796 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2797 }
Romain Guyf219da52011-01-16 12:54:25 -08002798 }
Chet Haase48659092012-05-31 15:21:51 -07002799
Romain Guyb2e2f242012-10-17 18:18:35 -07002800 if (transform && !transform->isIdentity()) {
2801 restore();
2802 }
2803
Chet Haase48659092012-05-31 15:21:51 -07002804 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002805}
2806
Romain Guy6926c722010-07-12 20:20:03 -07002807///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002808// Shaders
2809///////////////////////////////////////////////////////////////////////////////
2810
2811void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002812 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002813}
2814
Romain Guy06f96e22010-07-30 19:18:16 -07002815void OpenGLRenderer::setupShader(SkiaShader* shader) {
2816 mShader = shader;
2817 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002818 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002819 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002820}
2821
Romain Guyd27977d2010-07-14 19:18:51 -07002822///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002823// Color filters
2824///////////////////////////////////////////////////////////////////////////////
2825
2826void OpenGLRenderer::resetColorFilter() {
2827 mColorFilter = NULL;
2828}
2829
2830void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2831 mColorFilter = filter;
2832}
2833
2834///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002835// Drop shadow
2836///////////////////////////////////////////////////////////////////////////////
2837
2838void OpenGLRenderer::resetShadow() {
2839 mHasShadow = false;
2840}
2841
2842void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2843 mHasShadow = true;
2844 mShadowRadius = radius;
2845 mShadowDx = dx;
2846 mShadowDy = dy;
2847 mShadowColor = color;
2848}
2849
2850///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002851// Draw filters
2852///////////////////////////////////////////////////////////////////////////////
2853
2854void OpenGLRenderer::resetPaintFilter() {
2855 mHasDrawFilter = false;
2856}
2857
2858void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2859 mHasDrawFilter = true;
2860 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2861 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2862}
2863
2864SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002865 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002866
2867 uint32_t flags = paint->getFlags();
2868
2869 mFilteredPaint = *paint;
2870 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2871
2872 return &mFilteredPaint;
2873}
2874
2875///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002876// Drawing implementation
2877///////////////////////////////////////////////////////////////////////////////
2878
Romain Guy01d58e42011-01-19 21:54:02 -08002879void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2880 float x, float y, SkPaint* paint) {
2881 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2882 return;
2883 }
2884
2885 int alpha;
2886 SkXfermode::Mode mode;
2887 getAlphaAndMode(paint, &alpha, &mode);
2888
2889 setupDraw();
2890 setupDrawWithTexture(true);
2891 setupDrawAlpha8Color(paint->getColor(), alpha);
2892 setupDrawColorFilter();
2893 setupDrawShader();
2894 setupDrawBlending(true, mode);
2895 setupDrawProgram();
2896 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2897 setupDrawTexture(texture->id);
2898 setupDrawPureColorUniforms();
2899 setupDrawColorFilterUniforms();
2900 setupDrawShaderUniforms();
2901 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2902
2903 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2904
2905 finishDrawTexture();
2906}
2907
Romain Guyf607bdc2010-09-10 19:20:06 -07002908// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002909#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2910#define kStdUnderline_Offset (1.0f / 9.0f)
2911#define kStdUnderline_Thickness (1.0f / 18.0f)
2912
2913void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2914 float x, float y, SkPaint* paint) {
2915 // Handle underline and strike-through
2916 uint32_t flags = paint->getFlags();
2917 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002918 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002919 float underlineWidth = length;
2920 // If length is > 0.0f, we already measured the text for the text alignment
2921 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002922 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002923 }
2924
Romain Guy211370f2012-02-01 16:10:55 -08002925 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002926 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002927 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002928
Raph Levien8b4072d2012-07-30 15:50:00 -07002929 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002930 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002931
Romain Guyf6834472011-01-23 13:32:12 -08002932 int linesCount = 0;
2933 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2934 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2935
2936 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002937 float points[pointsCount];
2938 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002939
2940 if (flags & SkPaint::kUnderlineText_Flag) {
2941 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002942 points[currentPoint++] = left;
2943 points[currentPoint++] = top;
2944 points[currentPoint++] = left + underlineWidth;
2945 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002946 }
2947
2948 if (flags & SkPaint::kStrikeThruText_Flag) {
2949 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002950 points[currentPoint++] = left;
2951 points[currentPoint++] = top;
2952 points[currentPoint++] = left + underlineWidth;
2953 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002954 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002955
Romain Guy726aeba2011-06-01 14:52:00 -07002956 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002957
Romain Guy726aeba2011-06-01 14:52:00 -07002958 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002959 }
2960 }
2961}
2962
Romain Guy672433d2013-01-04 19:05:13 -08002963status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
2964 if (mSnapshot->isIgnored()) {
2965 return DrawGlInfo::kStatusDone;
2966 }
2967
Romain Guy735738c2012-12-03 12:34:51 -08002968 int color = paint->getColor();
2969 // If a shader is set, preserve only the alpha
2970 if (mShader) {
2971 color |= 0x00ffffff;
2972 }
2973 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2974
2975 return drawColorRects(rects, count, color, mode);
2976}
2977
2978status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08002979 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy735738c2012-12-03 12:34:51 -08002980
Romain Guy672433d2013-01-04 19:05:13 -08002981 float left = FLT_MAX;
2982 float top = FLT_MAX;
2983 float right = FLT_MIN;
2984 float bottom = FLT_MIN;
2985
2986 int vertexCount = 0;
2987 Vertex mesh[count * 6];
2988 Vertex* vertex = mesh;
2989
Chris Craik2af46352012-11-26 18:30:17 -08002990 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08002991 float l = rects[index + 0];
2992 float t = rects[index + 1];
2993 float r = rects[index + 2];
2994 float b = rects[index + 3];
2995
Romain Guy8ce00302013-01-15 18:51:42 -08002996 if (ignoreTransform || !quickRejectNoScissor(left, top, right, bottom)) {
Romain Guy672433d2013-01-04 19:05:13 -08002997 Vertex::set(vertex++, l, b);
2998 Vertex::set(vertex++, l, t);
2999 Vertex::set(vertex++, r, t);
3000 Vertex::set(vertex++, l, b);
3001 Vertex::set(vertex++, r, t);
3002 Vertex::set(vertex++, r, b);
3003
3004 vertexCount += 6;
3005
3006 left = fminf(left, l);
3007 top = fminf(top, t);
3008 right = fmaxf(right, r);
3009 bottom = fmaxf(bottom, b);
3010 }
3011 }
3012
Romain Guy3bbacf22013-02-06 16:51:04 -08003013 if (count == 0 || (clip && quickReject(left, top, right, bottom))) {
Romain Guya362c692013-02-04 13:50:16 -08003014 return DrawGlInfo::kStatusDone;
3015 }
Romain Guy672433d2013-01-04 19:05:13 -08003016
Romain Guy672433d2013-01-04 19:05:13 -08003017 setupDraw();
3018 setupDrawNoTexture();
3019 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3020 setupDrawShader();
3021 setupDrawColorFilter();
3022 setupDrawBlending(mode);
3023 setupDrawProgram();
3024 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003025 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003026 setupDrawColorUniforms();
3027 setupDrawShaderUniforms();
3028 setupDrawColorFilterUniforms();
3029 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3030
Romain Guy8ce00302013-01-15 18:51:42 -08003031 if (dirty && hasLayer()) {
Romain Guy672433d2013-01-04 19:05:13 -08003032 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
3033 }
3034
3035 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3036
3037 return DrawGlInfo::kStatusDrew;
3038}
3039
Romain Guy026c5e162010-06-28 17:12:22 -07003040void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003041 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003042 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07003043 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003044 color |= 0x00ffffff;
3045 }
3046
Romain Guy70ca14e2010-12-13 18:24:33 -08003047 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003048 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003049 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003050 setupDrawShader();
3051 setupDrawColorFilter();
3052 setupDrawBlending(mode);
3053 setupDrawProgram();
3054 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3055 setupDrawColorUniforms();
3056 setupDrawShaderUniforms(ignoreTransform);
3057 setupDrawColorFilterUniforms();
3058 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003059
Romain Guyc95c8d62010-09-17 15:31:32 -07003060 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3061}
3062
Romain Guy82ba8142010-07-09 13:25:56 -07003063void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003064 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003065 int alpha;
3066 SkXfermode::Mode mode;
3067 getAlphaAndMode(paint, &alpha, &mode);
3068
Romain Guyd21b6e12011-11-30 20:21:23 -08003069 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003070
Romain Guy211370f2012-02-01 16:10:55 -08003071 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08003072 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
3073 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
3074
Romain Guyd21b6e12011-11-30 20:21:23 -08003075 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003076 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3077 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3078 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3079 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003080 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003081 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3082 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3083 GL_TRIANGLE_STRIP, gMeshCount);
3084 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003085}
3086
Romain Guybd6b79b2010-06-26 00:13:53 -07003087void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003088 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3089 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003090 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003091}
3092
3093void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003094 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003095 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003096 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003097
Romain Guy746b7402010-10-26 16:27:31 -07003098 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003099 setupDrawWithTexture();
3100 setupDrawColor(alpha, alpha, alpha, alpha);
3101 setupDrawColorFilter();
3102 setupDrawBlending(blend, mode, swapSrcDst);
3103 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003104 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003105 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003106 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003107 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003108 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003109 }
Romain Guy886b2752013-01-04 12:26:18 -08003110 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003111 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003112 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003113 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003114
Romain Guy6820ac82010-09-15 18:11:50 -07003115 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003116
3117 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003118}
3119
Romain Guy886b2752013-01-04 12:26:18 -08003120void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3121 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3122 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3123 bool ignoreTransform, bool dirty) {
3124
3125 setupDraw();
3126 setupDrawWithTexture(true);
3127 if (hasColor) {
3128 setupDrawAlpha8Color(color, alpha);
3129 }
3130 setupDrawColorFilter();
3131 setupDrawShader();
3132 setupDrawBlending(true, mode);
3133 setupDrawProgram();
3134 if (!dirty) setupDrawDirtyRegionsDisabled();
3135 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3136 setupDrawTexture(texture);
3137 setupDrawPureColorUniforms();
3138 setupDrawColorFilterUniforms();
3139 setupDrawShaderUniforms();
3140 setupDrawMesh(vertices, texCoords);
3141
3142 glDrawArrays(drawMode, 0, elementsCount);
3143
3144 finishDrawTexture();
3145}
3146
Romain Guya5aed0d2010-09-09 14:42:43 -07003147void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003148 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003149 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003150
Romain Guy82ba8142010-07-09 13:25:56 -07003151 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003152 // These blend modes are not supported by OpenGL directly and have
3153 // to be implemented using shaders. Since the shader will perform
3154 // the blending, turn blending off here
3155 // If the blend mode cannot be implemented using shaders, fall
3156 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003157 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003158 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003159 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003160 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003161
Romain Guy82bc7a72012-01-03 14:13:39 -08003162 if (mCaches.blend) {
3163 glDisable(GL_BLEND);
3164 mCaches.blend = false;
3165 }
3166
3167 return;
3168 } else {
3169 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003170 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003171 }
3172
3173 if (!mCaches.blend) {
3174 glEnable(GL_BLEND);
3175 }
3176
3177 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3178 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3179
3180 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3181 glBlendFunc(sourceMode, destMode);
3182 mCaches.lastSrcMode = sourceMode;
3183 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003184 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003185 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003186 glDisable(GL_BLEND);
3187 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003188 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003189}
3190
Romain Guy889f8d12010-07-29 14:37:42 -07003191bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003192 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003193 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003194 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003195 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003196 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003197 }
Romain Guy6926c722010-07-12 20:20:03 -07003198 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003199}
3200
Romain Guy026c5e162010-06-28 17:12:22 -07003201void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003202 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003203 TextureVertex::setUV(v++, u1, v1);
3204 TextureVertex::setUV(v++, u2, v1);
3205 TextureVertex::setUV(v++, u1, v2);
3206 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003207}
3208
Chet Haase5c13d892010-10-08 08:37:55 -07003209void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003210 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003211 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003212}
3213
Romain Guy9d5316e2010-06-24 19:30:36 -07003214}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003215}; // namespace android