blob: bc30738113d47961f126ee424b42f133367445a8 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy03d58522012-02-24 17:54:07 -080024#include <SkPathMeasure.h>
Romain Guy694b5192010-07-21 21:33:20 -070025#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070028#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Romain Guy08aa2cb2011-03-17 11:06:57 -070030#include <private/hwui/DrawGlInfo.h>
31
Romain Guy5b3b3522010-10-27 18:57:51 -070032#include <ui/Rect.h>
33
Romain Guy85bf02f2010-06-22 13:11:24 -070034#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Chris Craik710f46d2012-09-17 17:25:49 -070036#include "PathRenderer.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070037#include "Properties.h"
Romain Guya957eea2010-12-08 18:34:42 -080038#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070039
40namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070041namespace uirenderer {
42
43///////////////////////////////////////////////////////////////////////////////
44// Defines
45///////////////////////////////////////////////////////////////////////////////
46
Romain Guy759ea802010-09-16 20:49:46 -070047#define RAD_TO_DEG (180.0f / 3.14159265f)
48#define MIN_ANGLE 0.001f
49
Romain Guyf8773082012-07-12 18:01:00 -070050#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070051
Romain Guy713e1bb2012-10-16 18:44:09 -070052#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080053
Romain Guy9d5316e2010-06-24 19:30:36 -070054///////////////////////////////////////////////////////////////////////////////
55// Globals
56///////////////////////////////////////////////////////////////////////////////
57
Romain Guy889f8d12010-07-29 14:37:42 -070058/**
59 * Structure mapping Skia xfermodes to OpenGL blending factors.
60 */
61struct Blender {
62 SkXfermode::Mode mode;
63 GLenum src;
64 GLenum dst;
65}; // struct Blender
66
Romain Guy026c5e162010-06-28 17:12:22 -070067// In this array, the index of each Blender equals the value of the first
68// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
69static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070070 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
71 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
72 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
73 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
74 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
75 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
77 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
78 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
81 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
82 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
83 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
84 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070085};
Romain Guye4d01122010-06-16 18:44:05 -070086
Romain Guy87a76572010-09-13 18:11:21 -070087// This array contains the swapped version of each SkXfermode. For instance
88// this array's SrcOver blending mode is actually DstOver. You can refer to
89// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070090static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070091 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
92 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
93 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
94 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
95 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
96 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
97 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
100 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
101 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
104 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
105 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700106};
107
Romain Guyf6a11b82010-06-23 17:47:49 -0700108///////////////////////////////////////////////////////////////////////////////
109// Constructors/destructor
110///////////////////////////////////////////////////////////////////////////////
111
Romain Guyfb8b7632010-08-23 21:05:08 -0700112OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700113 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700114 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700115 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800116 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700117
Romain Guyac670c02010-07-27 17:39:27 -0700118 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
119
Romain Guyae5575b2010-07-29 18:48:04 -0700120 mFirstSnapshot = new Snapshot;
Romain Guy87e2f7572012-09-24 11:37:12 -0700121
122 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700123}
124
Romain Guy85bf02f2010-06-22 13:11:24 -0700125OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700126 // The context has already been destroyed at this point, do not call
127 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700128}
129
Romain Guy87e2f7572012-09-24 11:37:12 -0700130void OpenGLRenderer::initProperties() {
131 char property[PROPERTY_VALUE_MAX];
132 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
133 mScissorOptimizationDisabled = !strcasecmp(property, "true");
134 INIT_LOGD(" Scissor optimization %s",
135 mScissorOptimizationDisabled ? "disabled" : "enabled");
136 } else {
137 INIT_LOGD(" Scissor optimization enabled");
138 }
Romain Guy13631f32012-01-30 17:41:55 -0800139}
140
141///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700142// Setup
143///////////////////////////////////////////////////////////////////////////////
144
Romain Guy49c5fc02012-05-15 11:10:01 -0700145bool OpenGLRenderer::isDeferred() {
146 return false;
147}
148
Romain Guy85bf02f2010-06-22 13:11:24 -0700149void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700150 initViewport(width, height);
151
152 glDisable(GL_DITHER);
153 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
154
155 glEnableVertexAttribArray(Program::kBindingPosition);
156}
157
158void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700159 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700160
161 mWidth = width;
162 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700163
164 mFirstSnapshot->height = height;
165 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700166}
167
Romain Guy7c25aab2012-10-18 15:05:02 -0700168status_t OpenGLRenderer::prepare(bool opaque) {
Chet Haase44b2fe32012-06-06 19:03:58 -0700169 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800170}
171
Romain Guy7c25aab2012-10-18 15:05:02 -0700172status_t OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom,
173 bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800174 mCaches.clearGarbage();
175
Romain Guy8aef54f2010-09-01 15:13:49 -0700176 mSnapshot = new Snapshot(mFirstSnapshot,
177 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800178 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700179 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700180
Romain Guy7d7b5492011-01-24 16:33:45 -0800181 mSnapshot->setClip(left, top, right, bottom);
Romain Guy41308e22012-10-22 20:02:43 -0700182 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700183
Romain Guy11cb6422012-09-21 00:39:43 -0700184 updateLayers();
185
Romain Guy45e4c3d2012-09-11 17:17:07 -0700186 // If we know that we are going to redraw the entire framebuffer,
187 // perform a discard to let the driver know we don't need to preserve
188 // the back buffer for this frame.
189 if (mCaches.extensions.hasDiscardFramebuffer() &&
190 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
191 const GLenum attachments[] = { getTargetFbo() == 0 ? GL_COLOR_EXT : GL_COLOR_ATTACHMENT0 };
192 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
193 }
194
Romain Guyddf74372012-05-22 14:07:07 -0700195 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800196
Romain Guy54c1a642012-09-27 17:55:46 -0700197 // Functors break the tiling extension in pretty spectacular ways
198 // This ensures we don't use tiling when a functor is going to be
199 // invoked during the frame
200 mSuppressTiling = mCaches.hasRegisteredFunctors();
201
Romain Guy2b7028e2012-09-19 17:25:38 -0700202 mTilingSnapshot = mSnapshot;
Romain Guy57b52682012-09-20 17:38:46 -0700203 startTiling(mTilingSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700204
Romain Guy7c450aa2012-09-21 19:15:00 -0700205 debugOverdraw(true, true);
206
Romain Guy7c25aab2012-10-18 15:05:02 -0700207 return clear(left, top, right, bottom, opaque);
208}
209
210status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700211 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700212 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700213 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700214 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700215 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700216 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700217
Romain Guy7c25aab2012-10-18 15:05:02 -0700218 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700219 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700220}
221
222void OpenGLRenderer::syncState() {
223 glViewport(0, 0, mWidth, mHeight);
224
225 if (mCaches.blend) {
226 glEnable(GL_BLEND);
227 } else {
228 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700229 }
Romain Guybb9524b2010-06-22 18:56:38 -0700230}
231
Romain Guy57b52682012-09-20 17:38:46 -0700232void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700233 if (!mSuppressTiling) {
234 Rect* clip = mTilingSnapshot->clipRect;
235 if (s->flags & Snapshot::kFlagIsFboLayer) {
236 clip = s->clipRect;
237 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700238
Romain Guy54c1a642012-09-27 17:55:46 -0700239 mCaches.startTiling(clip->left, s->height - clip->bottom,
240 clip->right - clip->left, clip->bottom - clip->top, opaque);
241 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700242}
243
244void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700245 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700246}
247
Romain Guyb025b9c2010-09-16 14:16:48 -0700248void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700249 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700250 endTiling();
251
Romain Guy11cb6422012-09-21 00:39:43 -0700252 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700253#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700254 GLenum status = GL_NO_ERROR;
255 while ((status = glGetError()) != GL_NO_ERROR) {
256 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
257 switch (status) {
258 case GL_INVALID_ENUM:
259 ALOGE(" GL_INVALID_ENUM");
260 break;
261 case GL_INVALID_VALUE:
262 ALOGE(" GL_INVALID_VALUE");
263 break;
264 case GL_INVALID_OPERATION:
265 ALOGE(" GL_INVALID_OPERATION");
266 break;
267 case GL_OUT_OF_MEMORY:
268 ALOGE(" Out of memory!");
269 break;
270 }
Romain Guya07105b2011-01-10 21:14:18 -0800271 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700272#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700273
Romain Guyc15008e2010-11-10 11:59:15 -0800274#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800275 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700276#else
277 if (mCaches.getDebugLevel() & kDebugMemory) {
278 mCaches.dumpMemoryUsage();
279 }
Romain Guyc15008e2010-11-10 11:59:15 -0800280#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700281 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700282}
283
Romain Guy6c319ca2011-01-11 14:29:25 -0800284void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700285 if (mCaches.currentProgram) {
286 if (mCaches.currentProgram->isInUse()) {
287 mCaches.currentProgram->remove();
288 mCaches.currentProgram = NULL;
289 }
290 }
Romain Guy50c0f092010-10-19 11:42:22 -0700291 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800292 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800293 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800294 mCaches.disbaleTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700295 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700296}
297
Romain Guy6c319ca2011-01-11 14:29:25 -0800298void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800299 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800300 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700301 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700302 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700303
Romain Guy3e263fa2011-12-12 16:47:48 -0800304 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
305
Chet Haase80250612012-08-15 13:46:54 -0700306 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700307 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800308 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700309 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700310
Romain Guya1d3c912011-12-13 14:55:06 -0800311 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700312
Romain Guy50c0f092010-10-19 11:42:22 -0700313 mCaches.blend = true;
314 glEnable(GL_BLEND);
315 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
316 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700317}
318
Romain Guy35643dd2012-09-18 15:40:58 -0700319void OpenGLRenderer::resumeAfterLayer() {
320 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
321 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
322 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700323 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700324
325 mCaches.resetScissor();
326 dirtyClip();
327}
328
Romain Guyba6be8a2012-04-23 18:22:09 -0700329void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700330 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700331}
332
333void OpenGLRenderer::attachFunctor(Functor* functor) {
334 mFunctors.add(functor);
335}
336
Romain Guy8f3b8e32012-03-27 16:33:45 -0700337status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
338 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700339 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700340
Romain Guyba6be8a2012-04-23 18:22:09 -0700341 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800342 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700343 SortedVector<Functor*> functors(mFunctors);
344 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700345
Romain Guyba6be8a2012-04-23 18:22:09 -0700346 DrawGlInfo info;
347 info.clipLeft = 0;
348 info.clipTop = 0;
349 info.clipRight = 0;
350 info.clipBottom = 0;
351 info.isLayer = false;
352 info.width = 0;
353 info.height = 0;
354 memset(info.transform, 0, sizeof(float) * 16);
355
356 for (size_t i = 0; i < count; i++) {
357 Functor* f = functors.itemAt(i);
358 result |= (*f)(DrawGlInfo::kModeProcess, &info);
359
Chris Craikc2c95432012-04-25 15:13:52 -0700360 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700361 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
362 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700363 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700364
Chris Craikc2c95432012-04-25 15:13:52 -0700365 if (result & DrawGlInfo::kStatusInvoke) {
366 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700367 }
368 }
Chris Craikd15321b2012-11-28 14:45:04 -0800369 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700370 }
371
372 return result;
373}
374
375status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800376 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700377 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700378
Romain Guy8a4ac612012-07-17 17:32:48 -0700379 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800380 if (mDirtyClip) {
381 setScissorFromClip();
382 }
Romain Guyd643bb52011-03-01 14:55:21 -0800383
Romain Guy80911b82011-03-16 15:30:12 -0700384 Rect clip(*mSnapshot->clipRect);
385 clip.snapToPixelBoundaries();
386
Romain Guyd643bb52011-03-01 14:55:21 -0800387 // Since we don't know what the functor will draw, let's dirty
388 // tne entire clip region
389 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800390 dirtyLayerUnchecked(clip, getRegion());
391 }
Romain Guyd643bb52011-03-01 14:55:21 -0800392
Romain Guy08aa2cb2011-03-17 11:06:57 -0700393 DrawGlInfo info;
394 info.clipLeft = clip.left;
395 info.clipTop = clip.top;
396 info.clipRight = clip.right;
397 info.clipBottom = clip.bottom;
398 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700399 info.width = getSnapshot()->viewport.getWidth();
400 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700401 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700402
Chet Haase48659092012-05-31 15:21:51 -0700403 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800404
Romain Guy8f3b8e32012-03-27 16:33:45 -0700405 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700406 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800407 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700408
Chris Craik65924a32012-04-05 17:52:11 -0700409 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700410 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700411 }
Romain Guycabfcc12011-03-07 18:06:46 -0800412 }
413
Chet Haasedaf98e92011-01-10 14:10:36 -0800414 resume();
Romain Guy65549432012-03-26 16:45:05 -0700415 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800416}
417
Romain Guyf6a11b82010-06-23 17:47:49 -0700418///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700419// Debug
420///////////////////////////////////////////////////////////////////////////////
421
422void OpenGLRenderer::startMark(const char* name) const {
423 mCaches.startMark(0, name);
424}
425
426void OpenGLRenderer::endMark() const {
427 mCaches.endMark();
428}
429
430void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
431 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
432 if (clear) {
433 mCaches.disableScissor();
434 mCaches.stencil.clear();
435 }
436 if (enable) {
437 mCaches.stencil.enableDebugWrite();
438 } else {
439 mCaches.stencil.disable();
440 }
441 }
442}
443
444void OpenGLRenderer::renderOverdraw() {
445 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
446 const Rect* clip = mTilingSnapshot->clipRect;
447
448 mCaches.enableScissor();
449 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
450 clip->right - clip->left, clip->bottom - clip->top);
451
452 mCaches.stencil.enableDebugTest(2);
453 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
454 mCaches.stencil.enableDebugTest(3);
455 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
456 mCaches.stencil.enableDebugTest(4);
457 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
458 mCaches.stencil.enableDebugTest(4, true);
459 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
460 mCaches.stencil.disable();
461 }
462}
463
464///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700465// Layers
466///////////////////////////////////////////////////////////////////////////////
467
468bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
469 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
470 OpenGLRenderer* renderer = layer->renderer;
471 Rect& dirty = layer->dirtyRect;
472
Romain Guy7c450aa2012-09-21 19:15:00 -0700473 if (inFrame) {
474 endTiling();
475 debugOverdraw(false, false);
476 }
Romain Guy11cb6422012-09-21 00:39:43 -0700477
478 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
479 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
480 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
481 renderer->finish();
482
483 if (inFrame) {
484 resumeAfterLayer();
485 startTiling(mSnapshot);
486 }
487
488 dirty.setEmpty();
489 layer->deferredUpdateScheduled = false;
490 layer->renderer = NULL;
491 layer->displayList = NULL;
492
493 return true;
494 }
495
496 return false;
497}
498
499void OpenGLRenderer::updateLayers() {
500 int count = mLayerUpdates.size();
501 if (count > 0) {
502 startMark("Layer Updates");
503
504 // Note: it is very important to update the layers in reverse order
505 for (int i = count - 1; i >= 0; i--) {
506 Layer* layer = mLayerUpdates.itemAt(i);
507 updateLayer(layer, false);
508 mCaches.resourceCache.decrementRefcount(layer);
509 }
510 mLayerUpdates.clear();
511
512 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
513 endMark();
514 }
515}
516
517void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
518 if (layer) {
519 mLayerUpdates.push_back(layer);
520 mCaches.resourceCache.incrementRefcount(layer);
521 }
522}
523
524void OpenGLRenderer::clearLayerUpdates() {
525 size_t count = mLayerUpdates.size();
526 if (count > 0) {
527 mCaches.resourceCache.lock();
528 for (size_t i = 0; i < count; i++) {
529 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
530 }
531 mCaches.resourceCache.unlock();
532 mLayerUpdates.clear();
533 }
534}
535
536///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700537// State management
538///////////////////////////////////////////////////////////////////////////////
539
Romain Guybb9524b2010-06-22 18:56:38 -0700540int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700541 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700542}
543
544int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700545 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700546}
547
548void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700549 if (mSaveCount > 1) {
550 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700551 }
Romain Guybb9524b2010-06-22 18:56:38 -0700552}
553
554void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700555 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700556
Romain Guy8fb95422010-08-17 18:38:51 -0700557 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700558 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700559 }
Romain Guybb9524b2010-06-22 18:56:38 -0700560}
561
Romain Guy8aef54f2010-09-01 15:13:49 -0700562int OpenGLRenderer::saveSnapshot(int flags) {
563 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700564 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700565}
566
567bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700568 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700569 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700570 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700571
Romain Guybd6b79b2010-06-26 00:13:53 -0700572 sp<Snapshot> current = mSnapshot;
573 sp<Snapshot> previous = mSnapshot->previous;
574
Romain Guyeb993562010-10-05 18:14:38 -0700575 if (restoreOrtho) {
576 Rect& r = previous->viewport;
577 glViewport(r.left, r.top, r.right, r.bottom);
578 mOrthoMatrix.load(current->orthoMatrix);
579 }
580
Romain Guy8b55f372010-08-18 17:10:07 -0700581 mSaveCount--;
582 mSnapshot = previous;
583
Romain Guy2542d192010-08-18 11:47:12 -0700584 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700585 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700586 }
Romain Guy2542d192010-08-18 11:47:12 -0700587
Romain Guy5ec99242010-11-03 16:19:08 -0700588 if (restoreLayer) {
589 composeLayer(current, previous);
590 }
591
Romain Guy2542d192010-08-18 11:47:12 -0700592 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700593}
594
Romain Guyf6a11b82010-06-23 17:47:49 -0700595///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700596// Layers
597///////////////////////////////////////////////////////////////////////////////
598
599int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700600 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700601 const GLuint previousFbo = mSnapshot->fbo;
602 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700603
Romain Guyaf636eb2010-12-09 17:47:21 -0800604 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700605 int alpha = 255;
606 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700607
Romain Guye45362c2010-11-03 19:58:32 -0700608 if (p) {
609 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700610 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700611 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700612 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700613 }
Romain Guyd55a8612010-06-28 17:42:46 -0700614
Chet Haased48885a2012-08-28 17:43:28 -0700615 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700616 }
Romain Guyd55a8612010-06-28 17:42:46 -0700617
618 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700619}
620
621int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
622 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700623 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700624 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700625 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700626 SkPaint paint;
627 paint.setAlpha(alpha);
628 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700629 }
Romain Guyd55a8612010-06-28 17:42:46 -0700630}
Romain Guybd6b79b2010-06-26 00:13:53 -0700631
Romain Guy1c740bc2010-09-13 18:00:09 -0700632/**
633 * Layers are viewed by Skia are slightly different than layers in image editing
634 * programs (for instance.) When a layer is created, previously created layers
635 * and the frame buffer still receive every drawing command. For instance, if a
636 * layer is created and a shape intersecting the bounds of the layers and the
637 * framebuffer is draw, the shape will be drawn on both (unless the layer was
638 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
639 *
640 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
641 * texture. Unfortunately, this is inefficient as it requires every primitive to
642 * be drawn n + 1 times, where n is the number of active layers. In practice this
643 * means, for every primitive:
644 * - Switch active frame buffer
645 * - Change viewport, clip and projection matrix
646 * - Issue the drawing
647 *
648 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700649 * To avoid this, layers are implemented in a different way here, at least in the
650 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
651 * is set. When this flag is set we can redirect all drawing operations into a
652 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700653 *
654 * This implementation relies on the frame buffer being at least RGBA 8888. When
655 * a layer is created, only a texture is created, not an FBO. The content of the
656 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700657 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700658 * buffer and drawing continues as normal. This technique therefore treats the
659 * frame buffer as a scratch buffer for the layers.
660 *
661 * To compose the layers back onto the frame buffer, each layer texture
662 * (containing the original frame buffer data) is drawn as a simple quad over
663 * the frame buffer. The trick is that the quad is set as the composition
664 * destination in the blending equation, and the frame buffer becomes the source
665 * of the composition.
666 *
667 * Drawing layers with an alpha value requires an extra step before composition.
668 * An empty quad is drawn over the layer's region in the frame buffer. This quad
669 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
670 * quad is used to multiply the colors in the frame buffer. This is achieved by
671 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
672 * GL_ZERO, GL_SRC_ALPHA.
673 *
674 * Because glCopyTexImage2D() can be slow, an alternative implementation might
675 * be use to draw a single clipped layer. The implementation described above
676 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700677 *
678 * (1) The frame buffer is actually not cleared right away. To allow the GPU
679 * to potentially optimize series of calls to glCopyTexImage2D, the frame
680 * buffer is left untouched until the first drawing operation. Only when
681 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700682 */
Chet Haased48885a2012-08-28 17:43:28 -0700683bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
684 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700685 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700686 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700687
Romain Guyeb993562010-10-05 18:14:38 -0700688 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
689
Romain Guyf607bdc2010-09-10 19:20:06 -0700690 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700691 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700692 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700693 Rect untransformedBounds(bounds);
694 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700695
Chet Haased48885a2012-08-28 17:43:28 -0700696 // Layers only make sense if they are in the framebuffer's bounds
697 if (bounds.intersect(*mSnapshot->clipRect)) {
698 // We cannot work with sub-pixels in this case
699 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700700
Chet Haased48885a2012-08-28 17:43:28 -0700701 // When the layer is not an FBO, we may use glCopyTexImage so we
702 // need to make sure the layer does not extend outside the bounds
703 // of the framebuffer
704 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700705 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700706 } else if (fboLayer) {
707 clip.set(bounds);
708 mat4 inverse;
709 inverse.loadInverse(*mSnapshot->transform);
710 inverse.mapRect(clip);
711 clip.snapToPixelBoundaries();
712 if (clip.intersect(untransformedBounds)) {
713 clip.translate(-left, -top);
714 bounds.set(untransformedBounds);
715 } else {
716 clip.setEmpty();
717 }
Romain Guyad37cd32011-03-15 11:12:25 -0700718 }
Chet Haased48885a2012-08-28 17:43:28 -0700719 } else {
720 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700721 }
Romain Guybf434112010-09-16 14:40:17 -0700722
Romain Guy746b7402010-10-26 16:27:31 -0700723 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700724 bounds.getHeight() > mCaches.maxTextureSize ||
725 (fboLayer && clip.isEmpty())) {
726 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700727 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700728 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700729 }
730
731 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700732 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700733 return false;
734 }
Romain Guyf18fd992010-07-08 11:45:51 -0700735
Romain Guya1d3c912011-12-13 14:55:06 -0800736 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700737 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700738 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700739 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700740 }
741
Romain Guy9ace8f52011-07-07 20:50:11 -0700742 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700743 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700744 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
745 bounds.getWidth() / float(layer->getWidth()), 0.0f);
746 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700747 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700748 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700749
Romain Guy8fb95422010-08-17 18:38:51 -0700750 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700751 mSnapshot->flags |= Snapshot::kFlagIsLayer;
752 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700753
Romain Guyeb993562010-10-05 18:14:38 -0700754 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700755 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700756 } else {
757 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700758 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800759 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700760 if (layer->isEmpty()) {
761 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700762 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700763 layer->getWidth(), layer->getHeight(), 0);
764 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800765 } else {
766 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700767 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800768 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700769
Romain Guy54be1cd2011-06-13 19:04:27 -0700770 // Enqueue the buffer coordinates to clear the corresponding region later
771 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700772 }
Romain Guyeb993562010-10-05 18:14:38 -0700773 }
Romain Guyf86ef572010-07-01 11:05:42 -0700774
Romain Guyd55a8612010-06-28 17:42:46 -0700775 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700776}
777
Chet Haased48885a2012-08-28 17:43:28 -0700778bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700779 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700780
Chet Haased48885a2012-08-28 17:43:28 -0700781 mSnapshot->region = &mSnapshot->layer->region;
782 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700783
Chet Haased48885a2012-08-28 17:43:28 -0700784 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
785 mSnapshot->fbo = layer->getFbo();
786 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
787 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
788 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
789 mSnapshot->height = bounds.getHeight();
790 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
791 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700792
Romain Guy2b7028e2012-09-19 17:25:38 -0700793 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700794 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700795 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700796 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
797 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700798
799 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700800 if (layer->isEmpty()) {
801 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
802 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700803 }
804
805 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700806 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700807
Romain Guy2b7028e2012-09-19 17:25:38 -0700808 startTiling(mSnapshot);
Romain Guy5b3b3522010-10-27 18:57:51 -0700809
810 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700811 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800812 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700813 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700814 glClear(GL_COLOR_BUFFER_BIT);
815
816 dirtyClip();
817
818 // Change the ortho projection
819 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
820 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
821
822 return true;
823}
824
Romain Guy1c740bc2010-09-13 18:00:09 -0700825/**
826 * Read the documentation of createLayer() before doing anything in this method.
827 */
Romain Guy1d83e192010-08-17 11:37:00 -0700828void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
829 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000830 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700831 return;
832 }
833
Romain Guy5b3b3522010-10-27 18:57:51 -0700834 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700835
836 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700837 endTiling();
838
Romain Guye0aa84b2012-04-03 19:30:26 -0700839 // Detach the texture from the FBO
840 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guyeb993562010-10-05 18:14:38 -0700841 // Unbind current FBO and restore previous one
842 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700843 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700844
845 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700846 }
847
Romain Guy1d83e192010-08-17 11:37:00 -0700848 Layer* layer = current->layer;
849 const Rect& rect = layer->layer;
850
Romain Guy9ace8f52011-07-07 20:50:11 -0700851 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700852 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700853 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700854 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700855 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700856 }
857
Romain Guy03750a02010-10-18 14:06:08 -0700858 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700859
Romain Guya1d3c912011-12-13 14:55:06 -0800860 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700861
Romain Guy5b3b3522010-10-27 18:57:51 -0700862 // When the layer is stored in an FBO, we can save a bit of fillrate by
863 // drawing only the dirty region
864 if (fboLayer) {
865 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700866 if (layer->getColorFilter()) {
867 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800868 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700869 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700870 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800871 resetColorFilter();
872 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700873 } else if (!rect.isEmpty()) {
874 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
875 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700876 }
Romain Guy8b55f372010-08-18 17:10:07 -0700877
Romain Guyeb993562010-10-05 18:14:38 -0700878 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800879 // Note: No need to use glDiscardFramebufferEXT() since we never
880 // create/compose layers that are not on screen with this
881 // code path
882 // See LayerRenderer::destroyLayer(Layer*)
883
Romain Guyeb993562010-10-05 18:14:38 -0700884 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
885 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700886 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700887 }
888
Romain Guy746b7402010-10-26 16:27:31 -0700889 dirtyClip();
890
Romain Guyeb993562010-10-05 18:14:38 -0700891 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700892 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700893 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700894 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700895 }
896}
897
Romain Guyaa6c24c2011-04-28 18:40:04 -0700898void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700899 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700900
901 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700902 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700903 setupDrawWithTexture();
904 } else {
905 setupDrawWithExternalTexture();
906 }
907 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700908 setupDrawColor(alpha, alpha, alpha, alpha);
909 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700910 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700911 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700912 setupDrawPureColorUniforms();
913 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700914 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
915 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700916 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700917 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700918 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700919 if (mSnapshot->transform->isPureTranslate() &&
920 layer->getWidth() == (uint32_t) rect.getWidth() &&
921 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700922 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
923 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
924
Romain Guyd21b6e12011-11-30 20:21:23 -0800925 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700926 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
927 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800928 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700929 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
930 }
931 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700932 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
933
934 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
935
936 finishDrawTexture();
937}
938
Romain Guy5b3b3522010-10-27 18:57:51 -0700939void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700940 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700941 const Rect& texCoords = layer->texCoords;
942 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
943 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700944
Romain Guy9ace8f52011-07-07 20:50:11 -0700945 float x = rect.left;
946 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700947 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700948 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700949 layer->getHeight() == (uint32_t) rect.getHeight();
950
951 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700952 // When we're swapping, the layer is already in screen coordinates
953 if (!swap) {
954 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
955 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
956 }
957
Romain Guyd21b6e12011-11-30 20:21:23 -0800958 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700959 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800960 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700961 }
962
963 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
964 layer->getTexture(), layer->getAlpha() / 255.0f,
965 layer->getMode(), layer->isBlend(),
966 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
967 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700968
Romain Guyaa6c24c2011-04-28 18:40:04 -0700969 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
970 } else {
971 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
972 drawTextureLayer(layer, rect);
973 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
974 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700975}
976
977void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700978 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700979 layer->setRegionAsRect();
980
Romain Guy40667672011-03-18 14:34:03 -0700981 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700982
Romain Guy5b3b3522010-10-27 18:57:51 -0700983 layer->region.clear();
984 return;
985 }
986
Romain Guy8a3957d2011-09-07 17:55:15 -0700987 // TODO: See LayerRenderer.cpp::generateMesh() for important
988 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800989 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700990 size_t count;
991 const android::Rect* rects = layer->region.getArray(&count);
992
Romain Guy9ace8f52011-07-07 20:50:11 -0700993 const float alpha = layer->getAlpha() / 255.0f;
994 const float texX = 1.0f / float(layer->getWidth());
995 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800996 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700997
998 TextureVertex* mesh = mCaches.getRegionMesh();
999 GLsizei numQuads = 0;
1000
Romain Guy7230a742011-01-10 22:26:16 -08001001 setupDraw();
1002 setupDrawWithTexture();
1003 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001004 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001005 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001006 setupDrawProgram();
1007 setupDrawDirtyRegionsDisabled();
1008 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001009 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001010 setupDrawTexture(layer->getTexture());
1011 if (mSnapshot->transform->isPureTranslate()) {
1012 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
1013 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
1014
Romain Guyd21b6e12011-11-30 20:21:23 -08001015 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001016 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1017 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001018 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001019 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1020 }
Romain Guy15bc6432011-12-13 13:11:32 -08001021 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001022
1023 for (size_t i = 0; i < count; i++) {
1024 const android::Rect* r = &rects[i];
1025
1026 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001027 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001028 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001029 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001030
1031 // TODO: Reject quads outside of the clip
1032 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1033 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1034 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1035 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1036
1037 numQuads++;
1038
1039 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1040 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1041 numQuads = 0;
1042 mesh = mCaches.getRegionMesh();
1043 }
1044 }
1045
1046 if (numQuads > 0) {
1047 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1048 }
1049
Romain Guy7230a742011-01-10 22:26:16 -08001050 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001051
1052#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001053 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001054#endif
1055
1056 layer->region.clear();
1057 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001058}
1059
Romain Guy3a3133d2011-02-01 22:59:58 -08001060void OpenGLRenderer::drawRegionRects(const Region& region) {
1061#if DEBUG_LAYERS_AS_REGIONS
1062 size_t count;
1063 const android::Rect* rects = region.getArray(&count);
1064
1065 uint32_t colors[] = {
1066 0x7fff0000, 0x7f00ff00,
1067 0x7f0000ff, 0x7fff00ff,
1068 };
1069
1070 int offset = 0;
1071 int32_t top = rects[0].top;
1072
1073 for (size_t i = 0; i < count; i++) {
1074 if (top != rects[i].top) {
1075 offset ^= 0x2;
1076 top = rects[i].top;
1077 }
1078
1079 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1080 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1081 SkXfermode::kSrcOver_Mode);
1082 }
1083#endif
1084}
1085
Romain Guy5b3b3522010-10-27 18:57:51 -07001086void OpenGLRenderer::dirtyLayer(const float left, const float top,
1087 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001088 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001089 Rect bounds(left, top, right, bottom);
1090 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001091 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001092 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001093}
1094
1095void OpenGLRenderer::dirtyLayer(const float left, const float top,
1096 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001097 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001098 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001099 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001100 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001101}
1102
1103void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001104 if (bounds.intersect(*mSnapshot->clipRect)) {
1105 bounds.snapToPixelBoundaries();
1106 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1107 if (!dirty.isEmpty()) {
1108 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001109 }
1110 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001111}
1112
Romain Guy54be1cd2011-06-13 19:04:27 -07001113void OpenGLRenderer::clearLayerRegions() {
1114 const size_t count = mLayers.size();
1115 if (count == 0) return;
1116
1117 if (!mSnapshot->isIgnored()) {
1118 // Doing several glScissor/glClear here can negatively impact
1119 // GPUs with a tiler architecture, instead we draw quads with
1120 // the Clear blending mode
1121
1122 // The list contains bounds that have already been clipped
1123 // against their initial clip rect, and the current clip
1124 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001125 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001126
1127 Vertex mesh[count * 6];
1128 Vertex* vertex = mesh;
1129
1130 for (uint32_t i = 0; i < count; i++) {
1131 Rect* bounds = mLayers.itemAt(i);
1132
1133 Vertex::set(vertex++, bounds->left, bounds->bottom);
1134 Vertex::set(vertex++, bounds->left, bounds->top);
1135 Vertex::set(vertex++, bounds->right, bounds->top);
1136 Vertex::set(vertex++, bounds->left, bounds->bottom);
1137 Vertex::set(vertex++, bounds->right, bounds->top);
1138 Vertex::set(vertex++, bounds->right, bounds->bottom);
1139
1140 delete bounds;
1141 }
1142
1143 setupDraw(false);
1144 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1145 setupDrawBlending(true, SkXfermode::kClear_Mode);
1146 setupDrawProgram();
1147 setupDrawPureColorUniforms();
1148 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001149 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001150
Romain Guy54be1cd2011-06-13 19:04:27 -07001151 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001152
1153 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001154 } else {
1155 for (uint32_t i = 0; i < count; i++) {
1156 delete mLayers.itemAt(i);
1157 }
1158 }
1159
1160 mLayers.clear();
1161}
1162
Romain Guybd6b79b2010-06-26 00:13:53 -07001163///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001164// Transforms
1165///////////////////////////////////////////////////////////////////////////////
1166
1167void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001168 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001169}
1170
1171void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001172 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001173}
1174
1175void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001176 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001177}
1178
Romain Guy807daf72011-01-18 11:19:19 -08001179void OpenGLRenderer::skew(float sx, float sy) {
1180 mSnapshot->transform->skew(sx, sy);
1181}
1182
Romain Guyf6a11b82010-06-23 17:47:49 -07001183void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001184 if (matrix) {
1185 mSnapshot->transform->load(*matrix);
1186 } else {
1187 mSnapshot->transform->loadIdentity();
1188 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001189}
1190
1191void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001192 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001193}
1194
1195void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001196 SkMatrix transform;
1197 mSnapshot->transform->copyTo(transform);
1198 transform.preConcat(*matrix);
1199 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001200}
1201
1202///////////////////////////////////////////////////////////////////////////////
1203// Clipping
1204///////////////////////////////////////////////////////////////////////////////
1205
Romain Guybb9524b2010-06-22 18:56:38 -07001206void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001207 Rect clip(*mSnapshot->clipRect);
1208 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001209
Romain Guy8a4ac612012-07-17 17:32:48 -07001210 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1211 clip.getWidth(), clip.getHeight())) {
1212 mDirtyClip = false;
1213 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001214}
1215
1216const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001217 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001218}
1219
Romain Guy8a4ac612012-07-17 17:32:48 -07001220bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1221 if (mSnapshot->isIgnored()) {
1222 return true;
1223 }
1224
1225 Rect r(left, top, right, bottom);
1226 mSnapshot->transform->mapRect(r);
1227 r.snapToPixelBoundaries();
1228
1229 Rect clipRect(*mSnapshot->clipRect);
1230 clipRect.snapToPixelBoundaries();
1231
1232 return !clipRect.intersects(r);
1233}
1234
Romain Guy35643dd2012-09-18 15:40:58 -07001235bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1236 Rect& transformed, Rect& clip) {
1237 if (mSnapshot->isIgnored()) {
1238 return true;
1239 }
1240
1241 transformed.set(left, top, right, bottom);
1242 mSnapshot->transform->mapRect(transformed);
1243 transformed.snapToPixelBoundaries();
1244
1245 clip.set(*mSnapshot->clipRect);
1246 clip.snapToPixelBoundaries();
1247
1248 return !clip.intersects(transformed);
1249}
1250
Chris Craikcb4d6002012-09-25 12:00:29 -07001251bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom, SkPaint* paint) {
1252 if (paint->getStyle() != SkPaint::kFill_Style) {
1253 float outset = paint->getStrokeWidth() * 0.5f;
1254 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1255 } else {
1256 return quickReject(left, top, right, bottom);
1257 }
1258}
1259
Romain Guyc7d53492010-06-25 13:41:57 -07001260bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001261 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001262 return true;
1263 }
1264
Romain Guy1d83e192010-08-17 11:37:00 -07001265 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001266 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001267 r.snapToPixelBoundaries();
1268
1269 Rect clipRect(*mSnapshot->clipRect);
1270 clipRect.snapToPixelBoundaries();
1271
Romain Guy586cae32012-07-13 15:28:31 -07001272 bool rejected = !clipRect.intersects(r);
1273 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001274 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001275 }
1276
1277 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001278}
1279
Romain Guy079ba2c2010-07-16 14:12:24 -07001280bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1281 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001282 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001283 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001284 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001285 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001286}
1287
Chet Haasea23eed82012-04-12 15:19:04 -07001288Rect* OpenGLRenderer::getClipRect() {
1289 return mSnapshot->clipRect;
1290}
1291
Romain Guyf6a11b82010-06-23 17:47:49 -07001292///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001293// Drawing commands
1294///////////////////////////////////////////////////////////////////////////////
1295
Romain Guy54be1cd2011-06-13 19:04:27 -07001296void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001297 // TODO: It would be best if we could do this before quickReject()
1298 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001299 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001300 if (mDirtyClip) {
1301 setScissorFromClip();
1302 }
1303 mDescription.reset();
1304 mSetShaderColor = false;
1305 mColorSet = false;
1306 mColorA = mColorR = mColorG = mColorB = 0.0f;
1307 mTextureUnit = 0;
1308 mTrackDirtyRegions = true;
1309}
1310
1311void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1312 mDescription.hasTexture = true;
1313 mDescription.hasAlpha8Texture = isAlpha8;
1314}
1315
Romain Guyaa6c24c2011-04-28 18:40:04 -07001316void OpenGLRenderer::setupDrawWithExternalTexture() {
1317 mDescription.hasExternalTexture = true;
1318}
1319
Romain Guy15bc6432011-12-13 13:11:32 -08001320void OpenGLRenderer::setupDrawNoTexture() {
1321 mCaches.disbaleTexCoordsVertexArray();
1322}
1323
Chris Craik710f46d2012-09-17 17:25:49 -07001324void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001325 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001326}
1327
Chris Craik710f46d2012-09-17 17:25:49 -07001328void OpenGLRenderer::setupDrawVertexShape() {
1329 mDescription.isVertexShape = true;
Chris Craik6ebdc112012-08-31 18:24:33 -07001330}
1331
Romain Guyed6fcb02011-03-21 13:11:28 -07001332void OpenGLRenderer::setupDrawPoint(float pointSize) {
1333 mDescription.isPoint = true;
1334 mDescription.pointSize = pointSize;
1335}
1336
Romain Guy70ca14e2010-12-13 18:24:33 -08001337void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001338 setupDrawColor(color, (color >> 24) & 0xFF);
1339}
1340
1341void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1342 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001343 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1344 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001345 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001346 mColorR = a * ((color >> 16) & 0xFF);
1347 mColorG = a * ((color >> 8) & 0xFF);
1348 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001349 mColorSet = true;
1350 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1351}
1352
Romain Guy86568192010-12-14 15:55:39 -08001353void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1354 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001355 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1356 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001357 const float a = mColorA / 255.0f;
1358 mColorR = a * ((color >> 16) & 0xFF);
1359 mColorG = a * ((color >> 8) & 0xFF);
1360 mColorB = a * ((color ) & 0xFF);
1361 mColorSet = true;
1362 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1363}
1364
Romain Guy41210632012-07-16 17:04:24 -07001365void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1366 mCaches.fontRenderer->describe(mDescription, paint);
1367}
1368
Romain Guy70ca14e2010-12-13 18:24:33 -08001369void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1370 mColorA = a;
1371 mColorR = r;
1372 mColorG = g;
1373 mColorB = b;
1374 mColorSet = true;
1375 mSetShaderColor = mDescription.setColor(r, g, b, a);
1376}
1377
1378void OpenGLRenderer::setupDrawShader() {
1379 if (mShader) {
1380 mShader->describe(mDescription, mCaches.extensions);
1381 }
1382}
1383
1384void OpenGLRenderer::setupDrawColorFilter() {
1385 if (mColorFilter) {
1386 mColorFilter->describe(mDescription, mCaches.extensions);
1387 }
1388}
1389
Romain Guyf09ef512011-05-27 11:43:46 -07001390void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1391 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1392 mColorA = 1.0f;
1393 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001394 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001395 }
1396}
1397
Romain Guy70ca14e2010-12-13 18:24:33 -08001398void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001399 // When the blending mode is kClear_Mode, we need to use a modulate color
1400 // argb=1,0,0,0
1401 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001402 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1403 mDescription, swapSrcDst);
1404}
1405
1406void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001407 // When the blending mode is kClear_Mode, we need to use a modulate color
1408 // argb=1,0,0,0
1409 accountForClear(mode);
Romain Guye83221c2012-09-24 16:01:35 -07001410 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()) ||
1411 (mColorFilter && mColorFilter->blend()), mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001412}
1413
1414void OpenGLRenderer::setupDrawProgram() {
1415 useProgram(mCaches.programCache.get(mDescription));
1416}
1417
1418void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1419 mTrackDirtyRegions = false;
1420}
1421
1422void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1423 bool ignoreTransform) {
1424 mModelView.loadTranslate(left, top, 0.0f);
1425 if (!ignoreTransform) {
1426 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1427 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1428 } else {
1429 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1430 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1431 }
1432}
1433
Chet Haase8a5cc922011-04-26 07:28:09 -07001434void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1435 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001436}
1437
Romain Guy70ca14e2010-12-13 18:24:33 -08001438void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1439 bool ignoreTransform, bool ignoreModelView) {
1440 if (!ignoreModelView) {
1441 mModelView.loadTranslate(left, top, 0.0f);
1442 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001443 } else {
1444 mModelView.loadIdentity();
1445 }
Romain Guy86568192010-12-14 15:55:39 -08001446 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1447 if (!ignoreTransform) {
1448 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1449 if (mTrackDirtyRegions && dirty) {
1450 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1451 }
1452 } else {
1453 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1454 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1455 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001456}
1457
Romain Guyed6fcb02011-03-21 13:11:28 -07001458void OpenGLRenderer::setupDrawPointUniforms() {
1459 int slot = mCaches.currentProgram->getUniform("pointSize");
1460 glUniform1f(slot, mDescription.pointSize);
1461}
1462
Romain Guy70ca14e2010-12-13 18:24:33 -08001463void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001464 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001465 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1466 }
1467}
1468
Romain Guy86568192010-12-14 15:55:39 -08001469void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001470 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001471 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001472 }
1473}
1474
Romain Guy70ca14e2010-12-13 18:24:33 -08001475void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1476 if (mShader) {
1477 if (ignoreTransform) {
1478 mModelView.loadInverse(*mSnapshot->transform);
1479 }
1480 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1481 }
1482}
1483
Romain Guy8d0d4782010-12-14 20:13:35 -08001484void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1485 if (mShader) {
1486 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1487 }
1488}
1489
Romain Guy70ca14e2010-12-13 18:24:33 -08001490void OpenGLRenderer::setupDrawColorFilterUniforms() {
1491 if (mColorFilter) {
1492 mColorFilter->setupProgram(mCaches.currentProgram);
1493 }
1494}
1495
Romain Guy41210632012-07-16 17:04:24 -07001496void OpenGLRenderer::setupDrawTextGammaUniforms() {
1497 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1498}
1499
Romain Guy70ca14e2010-12-13 18:24:33 -08001500void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001501 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001502 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001503 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001504}
1505
1506void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1507 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001508 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001509 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001510}
1511
Romain Guyaa6c24c2011-04-28 18:40:04 -07001512void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1513 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001514 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001515 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001516}
1517
Romain Guy8f0095c2011-05-02 17:24:22 -07001518void OpenGLRenderer::setupDrawTextureTransform() {
1519 mDescription.hasTextureTransform = true;
1520}
1521
1522void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001523 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1524 GL_FALSE, &transform.data[0]);
1525}
1526
Romain Guy70ca14e2010-12-13 18:24:33 -08001527void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001528 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001529 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001530 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001531 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001532 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001533 }
Romain Guyd71dd362011-12-12 19:03:35 -08001534
Chris Craikcb4d6002012-09-25 12:00:29 -07001535 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001536 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001537 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001538 }
1539
1540 mCaches.unbindIndicesBuffer();
1541}
1542
1543void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1544 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001545 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001546 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001547 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001548 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001549}
1550
Chet Haase5b0200b2011-04-13 17:58:08 -07001551void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001552 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001553 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001554 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001555}
1556
1557/**
1558 * Sets up the shader to draw an AA line. We draw AA lines with quads, where there is an
Chet Haase99585ad2011-05-02 15:00:16 -07001559 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1560 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1561 * attributes (one per vertex) are values from zero to one that tells the fragment
1562 * shader where the fragment is in relation to the line width/length overall; these values are
1563 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1564 * region of the line.
1565 * Note that we only pass down the width values in this setup function. The length coordinates
1566 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001567 */
Chet Haase99585ad2011-05-02 15:00:16 -07001568void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001569 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001570 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001571 mCaches.bindPositionVertexPointer(force, vertices, gAAVertexStride);
Romain Guyf3a910b42011-12-12 20:35:21 -08001572 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001573 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001574
Romain Guy7b631422012-04-04 11:38:54 -07001575 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001576 glEnableVertexAttribArray(widthSlot);
1577 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001578
Romain Guy7b631422012-04-04 11:38:54 -07001579 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001580 glEnableVertexAttribArray(lengthSlot);
1581 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001582
Chet Haase5b0200b2011-04-13 17:58:08 -07001583 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001584 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001585}
1586
1587void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1588 glDisableVertexAttribArray(widthSlot);
1589 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001590}
1591
Romain Guy70ca14e2010-12-13 18:24:33 -08001592void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001593}
1594
1595///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001596// Drawing
1597///////////////////////////////////////////////////////////////////////////////
1598
Chet Haase1271e2c2012-04-20 09:54:27 -07001599status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001600 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001601
Romain Guy0fe478e2010-11-08 12:08:41 -08001602 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1603 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001604 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001605 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001606 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001607
Romain Guy65549432012-03-26 16:45:05 -07001608 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001609}
1610
Chet Haaseed30fd82011-04-22 16:18:45 -07001611void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1612 if (displayList) {
1613 displayList->output(*this, level);
1614 }
1615}
1616
Romain Guya168d732011-03-18 16:50:13 -07001617void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1618 int alpha;
1619 SkXfermode::Mode mode;
1620 getAlphaAndMode(paint, &alpha, &mode);
1621
Romain Guya168d732011-03-18 16:50:13 -07001622 float x = left;
1623 float y = top;
1624
Romain Guye3c26852011-07-25 16:36:01 -07001625 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001626 bool ignoreTransform = false;
1627 if (mSnapshot->transform->isPureTranslate()) {
1628 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1629 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1630 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001631 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001632 } else {
1633 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001634 }
1635
1636 setupDraw();
1637 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001638 if (paint) {
1639 setupDrawAlpha8Color(paint->getColor(), alpha);
1640 }
Romain Guya168d732011-03-18 16:50:13 -07001641 setupDrawColorFilter();
1642 setupDrawShader();
1643 setupDrawBlending(true, mode);
1644 setupDrawProgram();
1645 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001646
Romain Guya168d732011-03-18 16:50:13 -07001647 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001648 texture->setWrap(GL_CLAMP_TO_EDGE);
1649 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001650
Romain Guya168d732011-03-18 16:50:13 -07001651 setupDrawPureColorUniforms();
1652 setupDrawColorFilterUniforms();
1653 setupDrawShaderUniforms();
1654 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1655
1656 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1657
1658 finishDrawTexture();
1659}
1660
Chet Haase48659092012-05-31 15:21:51 -07001661status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001662 const float right = left + bitmap->width();
1663 const float bottom = top + bitmap->height();
1664
1665 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001666 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001667 }
1668
Romain Guya1d3c912011-12-13 14:55:06 -08001669 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001670 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001671 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001672 const AutoTexture autoCleanup(texture);
1673
Romain Guy211370f2012-02-01 16:10:55 -08001674 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001675 drawAlphaBitmap(texture, left, top, paint);
1676 } else {
1677 drawTextureRect(left, top, right, bottom, texture, paint);
1678 }
Chet Haase48659092012-05-31 15:21:51 -07001679
1680 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001681}
1682
Chet Haase48659092012-05-31 15:21:51 -07001683status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001684 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1685 const mat4 transform(*matrix);
1686 transform.mapRect(r);
1687
Romain Guy6926c722010-07-12 20:20:03 -07001688 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001689 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001690 }
1691
Romain Guya1d3c912011-12-13 14:55:06 -08001692 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001693 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001694 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001695 const AutoTexture autoCleanup(texture);
1696
Romain Guy5b3b3522010-10-27 18:57:51 -07001697 // This could be done in a cheaper way, all we need is pass the matrix
1698 // to the vertex shader. The save/restore is a bit overkill.
1699 save(SkCanvas::kMatrix_SaveFlag);
1700 concatMatrix(matrix);
1701 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1702 restore();
Chet Haase48659092012-05-31 15:21:51 -07001703
1704 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001705}
1706
Chet Haase48659092012-05-31 15:21:51 -07001707status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001708 const float right = left + bitmap->width();
1709 const float bottom = top + bitmap->height();
1710
1711 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001712 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001713 }
1714
1715 mCaches.activeTexture(0);
1716 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1717 const AutoTexture autoCleanup(texture);
1718
1719 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001720
1721 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001722}
1723
Chet Haase48659092012-05-31 15:21:51 -07001724status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001725 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001726 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001727 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001728 }
1729
Romain Guya92bb4d2012-10-16 11:08:44 -07001730 // TODO: We should compute the bounding box when recording the display list
Romain Guyb18d2d02011-02-10 15:52:54 -08001731 float left = FLT_MAX;
1732 float top = FLT_MAX;
1733 float right = FLT_MIN;
1734 float bottom = FLT_MIN;
1735
Romain Guya92bb4d2012-10-16 11:08:44 -07001736 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001737
Romain Guya566b7c2011-01-23 16:36:11 -08001738 // TODO: Support the colors array
1739 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001740 TextureVertex* vertex = mesh;
Romain Guya92bb4d2012-10-16 11:08:44 -07001741
Romain Guy5a7b4662011-01-20 19:09:30 -08001742 for (int32_t y = 0; y < meshHeight; y++) {
1743 for (int32_t x = 0; x < meshWidth; x++) {
1744 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1745
1746 float u1 = float(x) / meshWidth;
1747 float u2 = float(x + 1) / meshWidth;
1748 float v1 = float(y) / meshHeight;
1749 float v2 = float(y + 1) / meshHeight;
1750
1751 int ax = i + (meshWidth + 1) * 2;
1752 int ay = ax + 1;
1753 int bx = i;
1754 int by = bx + 1;
1755 int cx = i + 2;
1756 int cy = cx + 1;
1757 int dx = i + (meshWidth + 1) * 2 + 2;
1758 int dy = dx + 1;
1759
1760 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1761 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1762 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1763
1764 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1765 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1766 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001767
Romain Guya92bb4d2012-10-16 11:08:44 -07001768 // TODO: This could be optimized to avoid unnecessary ops
1769 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1770 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1771 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1772 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001773 }
1774 }
1775
Romain Guya92bb4d2012-10-16 11:08:44 -07001776 if (quickReject(left, top, right, bottom)) {
1777 return DrawGlInfo::kStatusDone;
1778 }
1779
1780 mCaches.activeTexture(0);
1781 Texture* texture = mCaches.textureCache.get(bitmap);
1782 if (!texture) return DrawGlInfo::kStatusDone;
1783 const AutoTexture autoCleanup(texture);
1784
1785 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1786 texture->setFilter(FILTER(paint), true);
1787
1788 int alpha;
1789 SkXfermode::Mode mode;
1790 getAlphaAndMode(paint, &alpha, &mode);
1791
1792 if (hasLayer()) {
Romain Guyb18d2d02011-02-10 15:52:54 -08001793 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1794 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001795
Romain Guy5a7b4662011-01-20 19:09:30 -08001796 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1797 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001798 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001799
1800 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001801}
1802
Chet Haase48659092012-05-31 15:21:51 -07001803status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001804 float srcLeft, float srcTop, float srcRight, float srcBottom,
1805 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001806 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001807 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001808 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001809 }
1810
Romain Guya1d3c912011-12-13 14:55:06 -08001811 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001812 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001813 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001814 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001815
Romain Guy8ba548f2010-06-30 19:21:21 -07001816 const float width = texture->width;
1817 const float height = texture->height;
1818
Romain Guy68169722011-08-22 17:33:33 -07001819 const float u1 = fmax(0.0f, srcLeft / width);
1820 const float v1 = fmax(0.0f, srcTop / height);
1821 const float u2 = fmin(1.0f, srcRight / width);
1822 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001823
Romain Guy03750a02010-10-18 14:06:08 -07001824 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001825 resetDrawTextureTexCoords(u1, v1, u2, v2);
1826
Romain Guy03750a02010-10-18 14:06:08 -07001827 int alpha;
1828 SkXfermode::Mode mode;
1829 getAlphaAndMode(paint, &alpha, &mode);
1830
Romain Guyd21b6e12011-11-30 20:21:23 -08001831 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1832
Romain Guy211370f2012-02-01 16:10:55 -08001833 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001834 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1835 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1836
Romain Guyb5014982011-07-28 15:39:12 -07001837 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001838 // Enable linear filtering if the source rectangle is scaled
1839 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001840 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001841 }
Romain Guyb5014982011-07-28 15:39:12 -07001842
Romain Guyd21b6e12011-11-30 20:21:23 -08001843 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001844 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1845 texture->id, alpha / 255.0f, mode, texture->blend,
1846 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1847 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1848 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001849 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001850 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1851 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1852 GL_TRIANGLE_STRIP, gMeshCount);
1853 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001854
1855 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001856
1857 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001858}
1859
Chet Haase48659092012-05-31 15:21:51 -07001860status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001861 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001862 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001863 int alpha;
1864 SkXfermode::Mode mode;
1865 getAlphaAndModeDirect(paint, &alpha, &mode);
1866
1867 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1868 left, top, right, bottom, alpha, mode);
1869}
1870
1871status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1872 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1873 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001874 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001875 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001876 }
1877
Romain Guybe6f9dc2012-07-16 12:41:17 -07001878 alpha *= mSnapshot->alpha;
1879
Romain Guya1d3c912011-12-13 14:55:06 -08001880 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001881 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001882 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001883 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001884 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1885 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001886
Romain Guy2728f962010-10-08 18:36:15 -07001887 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001888 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001889
Romain Guy211370f2012-02-01 16:10:55 -08001890 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001891 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001892 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001893 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001894 const float offsetX = left + mSnapshot->transform->getTranslateX();
1895 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001896 const size_t count = mesh->quads.size();
1897 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001898 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001899 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001900 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1901 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1902 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001903 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001904 dirtyLayer(left + bounds.left, top + bounds.top,
1905 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001906 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001907 }
1908 }
1909
Romain Guy211370f2012-02-01 16:10:55 -08001910 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001911 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1912 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1913
1914 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1915 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1916 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1917 true, !mesh->hasEmptyQuads);
1918 } else {
1919 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1920 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1921 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1922 true, !mesh->hasEmptyQuads);
1923 }
Romain Guy054dc182010-10-15 17:55:25 -07001924 }
Chet Haase48659092012-05-31 15:21:51 -07001925
1926 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001927}
1928
Chet Haase99ecdc42011-05-06 12:06:34 -07001929/**
Chris Craikcb4d6002012-09-25 12:00:29 -07001930 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
1931 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
1932 * screen space in all directions. However, instead of using a fragment shader to compute the
1933 * translucency of the color from its position, we simply use a varying parameter to define how far
1934 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
1935 *
1936 * Doesn't yet support joins, caps, or path effects.
Chet Haase858aa932011-05-12 09:06:00 -07001937 */
Chris Craikcb4d6002012-09-25 12:00:29 -07001938void OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
1939 int color = paint->getColor();
1940 SkPaint::Style style = paint->getStyle();
1941 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
1942 bool isAA = paint->isAntiAlias();
1943
Chris Craik710f46d2012-09-17 17:25:49 -07001944 VertexBuffer vertexBuffer;
1945 // TODO: try clipping large paths to viewport
Chris Craikcb4d6002012-09-25 12:00:29 -07001946 PathRenderer::convexPathVertices(path, paint, mSnapshot->transform, vertexBuffer);
Romain Guy04299382012-07-18 17:15:41 -07001947
Chris Craikbf09ffb2012-10-01 13:50:37 -07001948 if (!vertexBuffer.getSize()) {
1949 // no vertices to draw
1950 return;
1951 }
1952
Chris Craik710f46d2012-09-17 17:25:49 -07001953 setupDraw();
1954 setupDrawNoTexture();
1955 if (isAA) setupDrawAA();
1956 setupDrawVertexShape();
1957 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1958 setupDrawColorFilter();
1959 setupDrawShader();
1960 setupDrawBlending(isAA, mode);
1961 setupDrawProgram();
Chris Craikcb4d6002012-09-25 12:00:29 -07001962 setupDrawModelViewIdentity();
Chris Craik710f46d2012-09-17 17:25:49 -07001963 setupDrawColorUniforms();
1964 setupDrawColorFilterUniforms();
1965 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07001966
Chris Craik710f46d2012-09-17 17:25:49 -07001967 void* vertices = vertexBuffer.getBuffer();
1968 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001969 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07001970 mCaches.resetTexCoordsVertexPointer();
1971 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07001972
Chris Craik710f46d2012-09-17 17:25:49 -07001973 int alphaSlot = -1;
1974 if (isAA) {
1975 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
1976 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07001977
Chris Craik710f46d2012-09-17 17:25:49 -07001978 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07001979 glEnableVertexAttribArray(alphaSlot);
1980 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07001981 }
Romain Guy04299382012-07-18 17:15:41 -07001982
Chris Craikcb4d6002012-09-25 12:00:29 -07001983 SkRect bounds = PathRenderer::computePathBounds(path, paint);
Chris Craik710f46d2012-09-17 17:25:49 -07001984 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy04299382012-07-18 17:15:41 -07001985
Chris Craik710f46d2012-09-17 17:25:49 -07001986 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07001987
Chris Craik710f46d2012-09-17 17:25:49 -07001988 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07001989 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001990 }
Chet Haase858aa932011-05-12 09:06:00 -07001991}
1992
1993/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001994 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1995 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1996 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1997 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1998 * of the line. Hairlines are more involved because we need to account for transform scaling
1999 * to end up with a one-pixel-wide line in screen space..
2000 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
2001 * in combination with values that we calculate and pass down in this method. The basic approach
2002 * is that the quad we create contains both the core line area plus a bounding area in which
2003 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
2004 * proportion of the width and the length of a given segment is represented by the boundary
2005 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
2006 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
2007 * on the inside). This ends up giving the result we want, with pixels that are completely
2008 * 'inside' the line area being filled opaquely and the other pixels being filled according to
2009 * how far into the boundary region they are, which is determined by shader interpolation.
2010 */
Chet Haase48659092012-05-31 15:21:51 -07002011status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
2012 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002013
2014 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07002015 // We use half the stroke width here because we're going to position the quad
2016 // corner vertices half of the width away from the line endpoints
2017 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002018 // A stroke width of 0 has a special meaning in Skia:
2019 // it draws a line 1 px wide regardless of current transform
2020 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07002021
Chet Haase99ecdc42011-05-06 12:06:34 -07002022 float inverseScaleX = 1.0f;
2023 float inverseScaleY = 1.0f;
2024 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07002025
Chet Haase8a5cc922011-04-26 07:28:09 -07002026 int alpha;
2027 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07002028
Chet Haase8a5cc922011-04-26 07:28:09 -07002029 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002030 int verticesCount = count;
2031 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07002032 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07002033 verticesCount += (count - 4);
2034 }
2035
2036 if (isHairLine || isAA) {
2037 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
2038 // the line on the screen should always be one pixel wide regardless of scale. For
2039 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08002040 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07002041 Matrix4 *mat = mSnapshot->transform;
2042 float m00 = mat->data[Matrix4::kScaleX];
2043 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07002044 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07002045 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07002046
Romain Guy211370f2012-02-01 16:10:55 -08002047 float scaleX = sqrtf(m00 * m00 + m01 * m01);
2048 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07002049
Chet Haase99ecdc42011-05-06 12:06:34 -07002050 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
2051 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07002052
Chet Haase99ecdc42011-05-06 12:06:34 -07002053 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
2054 scaled = true;
2055 }
2056 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002057 }
Chet Haase8a5cc922011-04-26 07:28:09 -07002058
2059 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07002060
2061 mCaches.enableScissor();
2062
Chet Haase8a5cc922011-04-26 07:28:09 -07002063 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002064 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002065 if (isAA) {
Chris Craik710f46d2012-09-17 17:25:49 -07002066 setupDrawAA();
Chet Haase8a5cc922011-04-26 07:28:09 -07002067 }
2068 setupDrawColor(paint->getColor(), alpha);
2069 setupDrawColorFilter();
2070 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08002071 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07002072 setupDrawProgram();
Chris Craikb30cb102012-10-05 19:11:37 -07002073 setupDrawModelViewIdentity(true);
Chet Haase8a5cc922011-04-26 07:28:09 -07002074 setupDrawColorUniforms();
2075 setupDrawColorFilterUniforms();
2076 setupDrawShaderIdentityUniforms();
2077
2078 if (isHairLine) {
2079 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07002080 halfStrokeWidth = isAA? 1 : .5;
2081 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002082 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07002083 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07002084 }
Romain Guy7b631422012-04-04 11:38:54 -07002085
2086 int widthSlot;
2087 int lengthSlot;
2088
Chet Haase5b0200b2011-04-13 17:58:08 -07002089 Vertex lines[verticesCount];
2090 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002091
Chet Haase99585ad2011-05-02 15:00:16 -07002092 AAVertex wLines[verticesCount];
2093 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002094
Romain Guy211370f2012-02-01 16:10:55 -08002095 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002096 setupDrawVertices(vertices);
2097 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002098 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2099 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002100 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002101 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2102 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002103 // We will need to calculate the actual width proportion on each segment for
2104 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002105 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002106 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2107 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002108 }
2109
Chet Haase99ecdc42011-05-06 12:06:34 -07002110 AAVertex* prevAAVertex = NULL;
2111 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002112
Chet Haase99585ad2011-05-02 15:00:16 -07002113 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002114 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002115
Chet Haase5b0200b2011-04-13 17:58:08 -07002116 for (int i = 0; i < count; i += 4) {
2117 // a = start point, b = end point
2118 vec2 a(points[i], points[i + 1]);
2119 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002120
Chet Haase99585ad2011-05-02 15:00:16 -07002121 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002122 float boundaryLengthProportion = 0;
2123 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002124
Chet Haase5b0200b2011-04-13 17:58:08 -07002125 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002126 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002127 float x = n.x;
2128 n.x = -n.y;
2129 n.y = x;
2130
Chet Haase8a5cc922011-04-26 07:28:09 -07002131 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002132 if (isAA) {
2133 float wideningFactor;
2134 if (fabs(n.x) >= fabs(n.y)) {
2135 wideningFactor = fabs(1.0f / n.x);
2136 } else {
2137 wideningFactor = fabs(1.0f / n.y);
2138 }
2139 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002140 }
Romain Guy7b631422012-04-04 11:38:54 -07002141
Chet Haase99ecdc42011-05-06 12:06:34 -07002142 if (scaled) {
2143 n.x *= inverseScaleX;
2144 n.y *= inverseScaleY;
2145 }
2146 } else if (scaled) {
2147 // Extend n by .5 pixel on each side, post-transform
2148 vec2 extendedN = n.copyNormalized();
2149 extendedN /= 2;
2150 extendedN.x *= inverseScaleX;
2151 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002152
Chet Haase99ecdc42011-05-06 12:06:34 -07002153 float extendedNLength = extendedN.length();
2154 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002155 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002156 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002157 }
Romain Guy7b631422012-04-04 11:38:54 -07002158
Chet Haase99585ad2011-05-02 15:00:16 -07002159 // aa lines expand the endpoint vertices to encompass the AA boundary
2160 if (isAA) {
2161 vec2 abVector = (b - a);
2162 length = abVector.length();
2163 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002164
Chet Haase99ecdc42011-05-06 12:06:34 -07002165 if (scaled) {
2166 abVector.x *= inverseScaleX;
2167 abVector.y *= inverseScaleY;
2168 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002169 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002170 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002171 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002172 }
Romain Guy7b631422012-04-04 11:38:54 -07002173
Chet Haase99ecdc42011-05-06 12:06:34 -07002174 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002175 a -= abVector;
2176 b += abVector;
2177 }
2178
Chet Haase5b0200b2011-04-13 17:58:08 -07002179 // Four corners of the rectangle defining a thick line
2180 vec2 p1 = a - n;
2181 vec2 p2 = a + n;
2182 vec2 p3 = b + n;
2183 vec2 p4 = b - n;
2184
Chet Haase99585ad2011-05-02 15:00:16 -07002185
Chet Haase5b0200b2011-04-13 17:58:08 -07002186 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2187 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2188 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2189 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2190
Romain Guy04299382012-07-18 17:15:41 -07002191 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002192 if (!isAA) {
2193 if (prevVertex != NULL) {
2194 // Issue two repeat vertices to create degenerate triangles to bridge
2195 // between the previous line and the new one. This is necessary because
2196 // we are creating a single triangle_strip which will contain
2197 // potentially discontinuous line segments.
2198 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2199 Vertex::set(vertices++, p1.x, p1.y);
2200 generatedVerticesCount += 2;
2201 }
Romain Guy7b631422012-04-04 11:38:54 -07002202
Chet Haase5b0200b2011-04-13 17:58:08 -07002203 Vertex::set(vertices++, p1.x, p1.y);
2204 Vertex::set(vertices++, p2.x, p2.y);
2205 Vertex::set(vertices++, p4.x, p4.y);
2206 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002207
Chet Haase5b0200b2011-04-13 17:58:08 -07002208 prevVertex = vertices - 1;
2209 generatedVerticesCount += 4;
2210 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002211 if (!isHairLine && scaled) {
2212 // Must set width proportions per-segment for scaled non-hairlines to use the
2213 // correct AA boundary dimensions
2214 if (boundaryWidthSlot < 0) {
2215 boundaryWidthSlot =
2216 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002217 }
Romain Guy7b631422012-04-04 11:38:54 -07002218
Chet Haase99ecdc42011-05-06 12:06:34 -07002219 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002220 }
Romain Guy7b631422012-04-04 11:38:54 -07002221
Chet Haase99585ad2011-05-02 15:00:16 -07002222 if (boundaryLengthSlot < 0) {
2223 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002224 }
Romain Guy7b631422012-04-04 11:38:54 -07002225
Chet Haase99ecdc42011-05-06 12:06:34 -07002226 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002227
Chet Haase5b0200b2011-04-13 17:58:08 -07002228 if (prevAAVertex != NULL) {
2229 // Issue two repeat vertices to create degenerate triangles to bridge
2230 // between the previous line and the new one. This is necessary because
2231 // we are creating a single triangle_strip which will contain
2232 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002233 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2234 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2235 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002236 generatedVerticesCount += 2;
2237 }
Romain Guy7b631422012-04-04 11:38:54 -07002238
Chet Haase99585ad2011-05-02 15:00:16 -07002239 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2240 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2241 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2242 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002243
Chet Haase5b0200b2011-04-13 17:58:08 -07002244 prevAAVertex = aaVertices - 1;
2245 generatedVerticesCount += 4;
2246 }
Romain Guy7b631422012-04-04 11:38:54 -07002247
Chet Haase99585ad2011-05-02 15:00:16 -07002248 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2249 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2250 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002251 }
2252 }
Romain Guy7b631422012-04-04 11:38:54 -07002253
Chet Haase5b0200b2011-04-13 17:58:08 -07002254 if (generatedVerticesCount > 0) {
2255 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2256 }
Romain Guy7b631422012-04-04 11:38:54 -07002257
2258 if (isAA) {
2259 finishDrawAALine(widthSlot, lengthSlot);
2260 }
Chet Haase48659092012-05-31 15:21:51 -07002261
2262 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002263}
2264
Chet Haase48659092012-05-31 15:21:51 -07002265status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2266 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002267
2268 // TODO: The paint's cap style defines whether the points are square or circular
2269 // TODO: Handle AA for round points
2270
Chet Haase5b0200b2011-04-13 17:58:08 -07002271 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002272 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002273 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002274 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002275 if (isHairLine) {
2276 // Now that we know it's hairline, we can set the effective width, to be used later
2277 strokeWidth = 1.0f;
2278 }
2279 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002280 int alpha;
2281 SkXfermode::Mode mode;
2282 getAlphaAndMode(paint, &alpha, &mode);
2283
2284 int verticesCount = count >> 1;
2285 int generatedVerticesCount = 0;
2286
2287 TextureVertex pointsData[verticesCount];
2288 TextureVertex* vertex = &pointsData[0];
2289
Romain Guy04299382012-07-18 17:15:41 -07002290 // TODO: We should optimize this method to not generate vertices for points
2291 // that lie outside of the clip.
2292 mCaches.enableScissor();
2293
Romain Guyed6fcb02011-03-21 13:11:28 -07002294 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002295 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002296 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002297 setupDrawColor(paint->getColor(), alpha);
2298 setupDrawColorFilter();
2299 setupDrawShader();
2300 setupDrawBlending(mode);
2301 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002302 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002303 setupDrawColorUniforms();
2304 setupDrawColorFilterUniforms();
2305 setupDrawPointUniforms();
2306 setupDrawShaderIdentityUniforms();
2307 setupDrawMesh(vertex);
2308
2309 for (int i = 0; i < count; i += 2) {
2310 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2311 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002312
Chet Haase8a5cc922011-04-26 07:28:09 -07002313 float left = points[i] - halfWidth;
2314 float right = points[i] + halfWidth;
2315 float top = points[i + 1] - halfWidth;
2316 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002317
Chet Haase8a5cc922011-04-26 07:28:09 -07002318 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002319 }
2320
2321 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002322
2323 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002324}
2325
Chet Haase48659092012-05-31 15:21:51 -07002326status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002327 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002328 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002329
Romain Guyae88e5e2010-10-22 17:49:18 -07002330 Rect& clip(*mSnapshot->clipRect);
2331 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002332
Romain Guy3d58c032010-07-14 16:34:53 -07002333 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002334
2335 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002336}
Romain Guy9d5316e2010-06-24 19:30:36 -07002337
Chet Haase48659092012-05-31 15:21:51 -07002338status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2339 SkPaint* paint) {
2340 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002341 const AutoTexture autoCleanup(texture);
2342
2343 const float x = left + texture->left - texture->offset;
2344 const float y = top + texture->top - texture->offset;
2345
2346 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002347
2348 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002349}
2350
Chet Haase48659092012-05-31 15:21:51 -07002351status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002352 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002353 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002354 return DrawGlInfo::kStatusDone;
2355 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002356
Chris Craikcb4d6002012-09-25 12:00:29 -07002357 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002358 mCaches.activeTexture(0);
2359 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2360 right - left, bottom - top, rx, ry, p);
2361 return drawShape(left, top, texture, p);
2362 }
2363
2364 SkPath path;
2365 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002366 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2367 float outset = p->getStrokeWidth() / 2;
2368 rect.outset(outset, outset);
2369 rx += outset;
2370 ry += outset;
2371 }
Chris Craik710f46d2012-09-17 17:25:49 -07002372 path.addRoundRect(rect, rx, ry);
Chris Craikcb4d6002012-09-25 12:00:29 -07002373 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002374
2375 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002376}
2377
Chris Craik710f46d2012-09-17 17:25:49 -07002378status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002379 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2380 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002381 return DrawGlInfo::kStatusDone;
2382 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002383 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002384 mCaches.activeTexture(0);
2385 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2386 return drawShape(x - radius, y - radius, texture, p);
2387 }
2388
2389 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002390 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2391 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2392 } else {
2393 path.addCircle(x, y, radius);
2394 }
2395 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002396
2397 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002398}
Romain Guy01d58e42011-01-19 21:54:02 -08002399
Chet Haase48659092012-05-31 15:21:51 -07002400status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002401 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002402 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002403 return DrawGlInfo::kStatusDone;
2404 }
Romain Guy01d58e42011-01-19 21:54:02 -08002405
Chris Craikcb4d6002012-09-25 12:00:29 -07002406 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002407 mCaches.activeTexture(0);
2408 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2409 return drawShape(left, top, texture, p);
2410 }
2411
2412 SkPath path;
2413 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002414 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2415 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2416 }
Chris Craik710f46d2012-09-17 17:25:49 -07002417 path.addOval(rect);
Chris Craikcb4d6002012-09-25 12:00:29 -07002418 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002419
2420 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002421}
2422
Chet Haase48659092012-05-31 15:21:51 -07002423status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002424 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
2425 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
2426 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002427 }
2428
Chris Craik780c1282012-10-04 14:10:49 -07002429 if (fabs(sweepAngle) >= 360.0f) {
2430 return drawOval(left, top, right, bottom, p);
2431 }
2432
2433 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik7fae5212012-11-01 11:27:03 -07002434 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || p->getStrokeCap() != SkPaint::kButt_Cap || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002435 mCaches.activeTexture(0);
2436 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2437 startAngle, sweepAngle, useCenter, p);
2438 return drawShape(left, top, texture, p);
2439 }
2440
2441 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2442 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2443 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2444 }
2445
2446 SkPath path;
2447 if (useCenter) {
2448 path.moveTo(rect.centerX(), rect.centerY());
2449 }
2450 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2451 if (useCenter) {
2452 path.close();
2453 }
2454 drawConvexPath(path, p);
2455
2456 return DrawGlInfo::kStatusDrew;
Romain Guy8b2f5262011-01-23 16:15:02 -08002457}
2458
Romain Guycf8675e2012-10-02 12:32:25 -07002459// See SkPaintDefaults.h
2460#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2461
Chet Haase48659092012-05-31 15:21:51 -07002462status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002463 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002464 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002465 }
2466
Chris Craik710f46d2012-09-17 17:25:49 -07002467 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002468 // only fill style is supported by drawConvexPath, since others have to handle joins
2469 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2470 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2471 mCaches.activeTexture(0);
2472 const PathTexture* texture =
2473 mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2474 return drawShape(left, top, texture, p);
2475 }
2476
2477 SkPath path;
2478 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2479 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2480 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2481 }
2482 path.addRect(rect);
2483 drawConvexPath(path, p);
2484
2485 return DrawGlInfo::kStatusDrew;
Romain Guy026c5e162010-06-28 17:12:22 -07002486 }
2487
Romain Guy181d0a62011-06-09 18:52:38 -07002488 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002489 SkPath path;
2490 path.addRect(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002491 drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002492 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002493 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chet Haase858aa932011-05-12 09:06:00 -07002494 }
Chet Haase48659092012-05-31 15:21:51 -07002495
2496 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002497}
Romain Guy9d5316e2010-06-24 19:30:36 -07002498
Raph Levien416a8472012-07-19 22:48:17 -07002499void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2500 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2501 float x, float y) {
2502 mCaches.activeTexture(0);
2503
2504 // NOTE: The drop shadow will not perform gamma correction
2505 // if shader-based correction is enabled
2506 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2507 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2508 paint, text, bytesCount, count, mShadowRadius, positions);
2509 const AutoTexture autoCleanup(shadow);
2510
2511 const float sx = x - shadow->left + mShadowDx;
2512 const float sy = y - shadow->top + mShadowDy;
2513
2514 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2515 int shadowColor = mShadowColor;
2516 if (mShader) {
2517 shadowColor = 0xffffffff;
2518 }
2519
2520 setupDraw();
2521 setupDrawWithTexture(true);
2522 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2523 setupDrawColorFilter();
2524 setupDrawShader();
2525 setupDrawBlending(true, mode);
2526 setupDrawProgram();
2527 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2528 setupDrawTexture(shadow->id);
2529 setupDrawPureColorUniforms();
2530 setupDrawColorFilterUniforms();
2531 setupDrawShaderUniforms();
2532 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2533
2534 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2535}
2536
Chet Haase48659092012-05-31 15:21:51 -07002537status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002538 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002539 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002540 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002541 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002542 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002543
Romain Guy671d6cf2012-01-18 12:39:17 -08002544 // NOTE: Skia does not support perspective transform on drawPosText yet
2545 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002546 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002547 }
2548
2549 float x = 0.0f;
2550 float y = 0.0f;
2551 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2552 if (pureTranslate) {
2553 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2554 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2555 }
2556
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002557 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002558 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2559 paint->getTextSize());
2560
2561 int alpha;
2562 SkXfermode::Mode mode;
2563 getAlphaAndMode(paint, &alpha, &mode);
2564
Raph Levien416a8472012-07-19 22:48:17 -07002565 if (CC_UNLIKELY(mHasShadow)) {
2566 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2567 0.0f, 0.0f);
2568 }
2569
Romain Guy671d6cf2012-01-18 12:39:17 -08002570 // Pick the appropriate texture filtering
2571 bool linearFilter = mSnapshot->transform->changesBounds();
2572 if (pureTranslate && !linearFilter) {
2573 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2574 }
2575
2576 mCaches.activeTexture(0);
2577 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002578 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002579 setupDrawDirtyRegionsDisabled();
2580 setupDrawWithTexture(true);
2581 setupDrawAlpha8Color(paint->getColor(), alpha);
2582 setupDrawColorFilter();
2583 setupDrawShader();
2584 setupDrawBlending(true, mode);
2585 setupDrawProgram();
2586 setupDrawModelView(x, y, x, y, pureTranslate, true);
2587 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2588 setupDrawPureColorUniforms();
2589 setupDrawColorFilterUniforms();
2590 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002591 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002592
2593 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2594 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2595
Romain Guy211370f2012-02-01 16:10:55 -08002596 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002597
2598 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2599 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002600 if (hasActiveLayer) {
2601 if (!pureTranslate) {
2602 mSnapshot->transform->mapRect(bounds);
2603 }
2604 dirtyLayerUnchecked(bounds, getRegion());
2605 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002606 }
Chet Haase48659092012-05-31 15:21:51 -07002607
2608 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002609}
2610
Romain Guyc2525952012-07-27 16:41:22 -07002611status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002612 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002613 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002614 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002615 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002616 }
2617
Chet Haasea1cff502012-02-21 13:43:44 -08002618 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002619 switch (paint->getTextAlign()) {
2620 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002621 x -= length / 2.0f;
2622 break;
2623 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002624 x -= length;
2625 break;
2626 default:
2627 break;
2628 }
2629
Romain Guycac5fd32011-12-01 20:08:50 -08002630 SkPaint::FontMetrics metrics;
2631 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002632 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002633 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002634 }
2635
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002636 const float oldX = x;
2637 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002638 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002639 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002640 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2641 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2642 }
2643
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002644#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002645 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002646 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002647#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002648
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002649 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002650 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002651 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002652
Romain Guy86568192010-12-14 15:55:39 -08002653 int alpha;
2654 SkXfermode::Mode mode;
2655 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002656
Romain Guy211370f2012-02-01 16:10:55 -08002657 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002658 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2659 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002660 }
2661
Romain Guy6620c6d2010-12-06 18:07:02 -08002662 // Pick the appropriate texture filtering
2663 bool linearFilter = mSnapshot->transform->changesBounds();
2664 if (pureTranslate && !linearFilter) {
2665 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2666 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002667
Romain Guy16c88082012-06-11 16:03:47 -07002668 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002669 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002670 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002671 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002672 setupDrawDirtyRegionsDisabled();
2673 setupDrawWithTexture(true);
2674 setupDrawAlpha8Color(paint->getColor(), alpha);
2675 setupDrawColorFilter();
2676 setupDrawShader();
2677 setupDrawBlending(true, mode);
2678 setupDrawProgram();
2679 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002680 // See comment above; the font renderer must use texture unit 0
2681 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002682 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2683 setupDrawPureColorUniforms();
2684 setupDrawColorFilterUniforms();
2685 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002686 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002687
Romain Guya3dc55f2012-09-28 13:55:44 -07002688 const Rect* clip = pureTranslate ? mSnapshot->clipRect :
2689 (mSnapshot->hasPerspectiveTransform() ? NULL : &mSnapshot->getLocalClip());
Romain Guy5b3b3522010-10-27 18:57:51 -07002690 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2691
Romain Guy211370f2012-02-01 16:10:55 -08002692 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002693
Raph Levien996e57c2012-07-23 15:22:52 -07002694 bool status;
Romain Guya3dc55f2012-09-28 13:55:44 -07002695 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002696 SkPaint paintCopy(*paint);
2697 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2698 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002699 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002700 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002701 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002702 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002703 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002704
2705 if (status && hasActiveLayer) {
2706 if (!pureTranslate) {
2707 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002708 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002709 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002710 }
Romain Guy694b5192010-07-21 21:33:20 -07002711
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002712 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002713
2714 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002715}
2716
Chet Haase48659092012-05-31 15:21:51 -07002717status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002718 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002719 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2720 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002721 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002722 }
2723
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002724 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002725 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2726 paint->getTextSize());
2727
2728 int alpha;
2729 SkXfermode::Mode mode;
2730 getAlphaAndMode(paint, &alpha, &mode);
2731
2732 mCaches.activeTexture(0);
2733 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002734 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002735 setupDrawDirtyRegionsDisabled();
2736 setupDrawWithTexture(true);
2737 setupDrawAlpha8Color(paint->getColor(), alpha);
2738 setupDrawColorFilter();
2739 setupDrawShader();
2740 setupDrawBlending(true, mode);
2741 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002742 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002743 setupDrawTexture(fontRenderer.getTexture(true));
2744 setupDrawPureColorUniforms();
2745 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002746 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002747 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002748
Romain Guy97771732012-02-28 18:17:02 -08002749 const Rect* clip = &mSnapshot->getLocalClip();
2750 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 -08002751
Romain Guy97771732012-02-28 18:17:02 -08002752 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002753
2754 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2755 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002756 if (hasActiveLayer) {
2757 mSnapshot->transform->mapRect(bounds);
2758 dirtyLayerUnchecked(bounds, getRegion());
2759 }
Romain Guy97771732012-02-28 18:17:02 -08002760 }
Chet Haase48659092012-05-31 15:21:51 -07002761
2762 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002763}
2764
Chet Haase48659092012-05-31 15:21:51 -07002765status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2766 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002767
Romain Guya1d3c912011-12-13 14:55:06 -08002768 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002769
Romain Guy33f6beb2012-02-16 19:24:51 -08002770 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002771 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002772 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002773 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002774
Romain Guy8b55f372010-08-18 17:10:07 -07002775 const float x = texture->left - texture->offset;
2776 const float y = texture->top - texture->offset;
2777
Romain Guy01d58e42011-01-19 21:54:02 -08002778 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002779
2780 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002781}
2782
Chet Haase48659092012-05-31 15:21:51 -07002783status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002784 if (!layer) {
2785 return DrawGlInfo::kStatusDone;
2786 }
2787
Romain Guyb2e2f242012-10-17 18:18:35 -07002788 mat4* transform = NULL;
2789 if (layer->isTextureLayer()) {
2790 transform = &layer->getTransform();
2791 if (!transform->isIdentity()) {
2792 save(0);
2793 mSnapshot->transform->multiply(*transform);
2794 }
2795 }
2796
Romain Guy35643dd2012-09-18 15:40:58 -07002797 Rect transformed;
2798 Rect clip;
2799 const bool rejected = quickRejectNoScissor(x, y,
2800 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2801
2802 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002803 if (transform && !transform->isIdentity()) {
2804 restore();
2805 }
Chet Haase48659092012-05-31 15:21:51 -07002806 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002807 }
2808
Romain Guy4ff0cf42012-08-06 14:51:10 -07002809 bool debugLayerUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -07002810 if (updateLayer(layer, true)) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002811 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002812 }
2813
Romain Guy87e2f7572012-09-24 11:37:12 -07002814 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002815 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002816
Romain Guy211370f2012-02-01 16:10:55 -08002817 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guye529ece2012-09-26 11:23:17 -07002818 SkiaColorFilter* oldFilter = mColorFilter;
2819 mColorFilter = layer->getColorFilter();
2820
Romain Guyc88e3572011-01-22 00:32:12 -08002821 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002822 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002823 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002824 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002825 setupDraw();
2826 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002827 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002828 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002829 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002830 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002831 setupDrawPureColorUniforms();
2832 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002833 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002834 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002835 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2836 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002837
Romain Guyd21b6e12011-11-30 20:21:23 -08002838 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002839 setupDrawModelViewTranslate(tx, ty,
2840 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002841 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002842 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002843 setupDrawModelViewTranslate(x, y,
2844 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2845 }
Romain Guyc88e3572011-01-22 00:32:12 -08002846 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002847
Romain Guyc88e3572011-01-22 00:32:12 -08002848 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2849 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002850
Romain Guyc88e3572011-01-22 00:32:12 -08002851 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002852
2853#if DEBUG_LAYERS_AS_REGIONS
2854 drawRegionRects(layer->region);
2855#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002856 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002857
Romain Guye529ece2012-09-26 11:23:17 -07002858 mColorFilter = oldFilter;
2859
Romain Guy4ff0cf42012-08-06 14:51:10 -07002860 if (debugLayerUpdate) {
2861 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2862 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2863 }
Romain Guyf219da52011-01-16 12:54:25 -08002864 }
Chet Haase48659092012-05-31 15:21:51 -07002865
Romain Guyb2e2f242012-10-17 18:18:35 -07002866 if (transform && !transform->isIdentity()) {
2867 restore();
2868 }
2869
Chet Haase48659092012-05-31 15:21:51 -07002870 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002871}
2872
Romain Guy6926c722010-07-12 20:20:03 -07002873///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002874// Shaders
2875///////////////////////////////////////////////////////////////////////////////
2876
2877void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002878 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002879}
2880
Romain Guy06f96e22010-07-30 19:18:16 -07002881void OpenGLRenderer::setupShader(SkiaShader* shader) {
2882 mShader = shader;
2883 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002884 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002885 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002886}
2887
Romain Guyd27977d2010-07-14 19:18:51 -07002888///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002889// Color filters
2890///////////////////////////////////////////////////////////////////////////////
2891
2892void OpenGLRenderer::resetColorFilter() {
2893 mColorFilter = NULL;
2894}
2895
2896void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2897 mColorFilter = filter;
2898}
2899
2900///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002901// Drop shadow
2902///////////////////////////////////////////////////////////////////////////////
2903
2904void OpenGLRenderer::resetShadow() {
2905 mHasShadow = false;
2906}
2907
2908void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2909 mHasShadow = true;
2910 mShadowRadius = radius;
2911 mShadowDx = dx;
2912 mShadowDy = dy;
2913 mShadowColor = color;
2914}
2915
2916///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002917// Draw filters
2918///////////////////////////////////////////////////////////////////////////////
2919
2920void OpenGLRenderer::resetPaintFilter() {
2921 mHasDrawFilter = false;
2922}
2923
2924void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2925 mHasDrawFilter = true;
2926 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2927 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2928}
2929
2930SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002931 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002932
2933 uint32_t flags = paint->getFlags();
2934
2935 mFilteredPaint = *paint;
2936 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2937
2938 return &mFilteredPaint;
2939}
2940
2941///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002942// Drawing implementation
2943///////////////////////////////////////////////////////////////////////////////
2944
Romain Guy01d58e42011-01-19 21:54:02 -08002945void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2946 float x, float y, SkPaint* paint) {
2947 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2948 return;
2949 }
2950
2951 int alpha;
2952 SkXfermode::Mode mode;
2953 getAlphaAndMode(paint, &alpha, &mode);
2954
2955 setupDraw();
2956 setupDrawWithTexture(true);
2957 setupDrawAlpha8Color(paint->getColor(), alpha);
2958 setupDrawColorFilter();
2959 setupDrawShader();
2960 setupDrawBlending(true, mode);
2961 setupDrawProgram();
2962 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2963 setupDrawTexture(texture->id);
2964 setupDrawPureColorUniforms();
2965 setupDrawColorFilterUniforms();
2966 setupDrawShaderUniforms();
2967 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2968
2969 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2970
2971 finishDrawTexture();
2972}
2973
Romain Guyf607bdc2010-09-10 19:20:06 -07002974// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002975#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2976#define kStdUnderline_Offset (1.0f / 9.0f)
2977#define kStdUnderline_Thickness (1.0f / 18.0f)
2978
2979void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2980 float x, float y, SkPaint* paint) {
2981 // Handle underline and strike-through
2982 uint32_t flags = paint->getFlags();
2983 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002984 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002985 float underlineWidth = length;
2986 // If length is > 0.0f, we already measured the text for the text alignment
2987 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002988 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002989 }
2990
Romain Guy211370f2012-02-01 16:10:55 -08002991 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002992 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002993 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002994
Raph Levien8b4072d2012-07-30 15:50:00 -07002995 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002996 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002997
Romain Guyf6834472011-01-23 13:32:12 -08002998 int linesCount = 0;
2999 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3000 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3001
3002 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003003 float points[pointsCount];
3004 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003005
3006 if (flags & SkPaint::kUnderlineText_Flag) {
3007 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003008 points[currentPoint++] = left;
3009 points[currentPoint++] = top;
3010 points[currentPoint++] = left + underlineWidth;
3011 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003012 }
3013
3014 if (flags & SkPaint::kStrikeThruText_Flag) {
3015 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003016 points[currentPoint++] = left;
3017 points[currentPoint++] = top;
3018 points[currentPoint++] = left + underlineWidth;
3019 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003020 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003021
Romain Guy726aeba2011-06-01 14:52:00 -07003022 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003023
Romain Guy726aeba2011-06-01 14:52:00 -07003024 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003025 }
3026 }
3027}
3028
Romain Guy026c5e162010-06-28 17:12:22 -07003029void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003030 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003031 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07003032 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003033 color |= 0x00ffffff;
3034 }
3035
Romain Guy70ca14e2010-12-13 18:24:33 -08003036 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003037 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003038 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003039 setupDrawShader();
3040 setupDrawColorFilter();
3041 setupDrawBlending(mode);
3042 setupDrawProgram();
3043 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3044 setupDrawColorUniforms();
3045 setupDrawShaderUniforms(ignoreTransform);
3046 setupDrawColorFilterUniforms();
3047 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003048
Romain Guyc95c8d62010-09-17 15:31:32 -07003049 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3050}
3051
Romain Guy82ba8142010-07-09 13:25:56 -07003052void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003053 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003054 int alpha;
3055 SkXfermode::Mode mode;
3056 getAlphaAndMode(paint, &alpha, &mode);
3057
Romain Guyd21b6e12011-11-30 20:21:23 -08003058 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003059
Romain Guy211370f2012-02-01 16:10:55 -08003060 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08003061 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
3062 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
3063
Romain Guyd21b6e12011-11-30 20:21:23 -08003064 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003065 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3066 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3067 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3068 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003069 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003070 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3071 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3072 GL_TRIANGLE_STRIP, gMeshCount);
3073 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003074}
3075
Romain Guybd6b79b2010-06-26 00:13:53 -07003076void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003077 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3078 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003079 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003080}
3081
3082void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003083 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003084 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003085 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003086
Romain Guy746b7402010-10-26 16:27:31 -07003087 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003088 setupDrawWithTexture();
3089 setupDrawColor(alpha, alpha, alpha, alpha);
3090 setupDrawColorFilter();
3091 setupDrawBlending(blend, mode, swapSrcDst);
3092 setupDrawProgram();
3093 if (!dirty) {
3094 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07003095 }
Romain Guy5b3b3522010-10-27 18:57:51 -07003096 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003097 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003098 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003099 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003100 }
Romain Guy86568192010-12-14 15:55:39 -08003101 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003102 setupDrawColorFilterUniforms();
3103 setupDrawTexture(texture);
3104 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003105
Romain Guy6820ac82010-09-15 18:11:50 -07003106 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003107
3108 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003109}
3110
Romain Guya5aed0d2010-09-09 14:42:43 -07003111void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003112 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003113 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003114
Romain Guy82ba8142010-07-09 13:25:56 -07003115 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003116 // These blend modes are not supported by OpenGL directly and have
3117 // to be implemented using shaders. Since the shader will perform
3118 // the blending, turn blending off here
3119 // If the blend mode cannot be implemented using shaders, fall
3120 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003121 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08003122 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003123 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003124 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003125
Romain Guy82bc7a72012-01-03 14:13:39 -08003126 if (mCaches.blend) {
3127 glDisable(GL_BLEND);
3128 mCaches.blend = false;
3129 }
3130
3131 return;
3132 } else {
3133 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003134 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003135 }
3136
3137 if (!mCaches.blend) {
3138 glEnable(GL_BLEND);
3139 }
3140
3141 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3142 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3143
3144 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3145 glBlendFunc(sourceMode, destMode);
3146 mCaches.lastSrcMode = sourceMode;
3147 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003148 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003149 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003150 glDisable(GL_BLEND);
3151 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003152 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003153}
3154
Romain Guy889f8d12010-07-29 14:37:42 -07003155bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003156 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003157 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003158 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003159 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003160 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003161 }
Romain Guy6926c722010-07-12 20:20:03 -07003162 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003163}
3164
Romain Guy026c5e162010-06-28 17:12:22 -07003165void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003166 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003167 TextureVertex::setUV(v++, u1, v1);
3168 TextureVertex::setUV(v++, u2, v1);
3169 TextureVertex::setUV(v++, u1, v2);
3170 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003171}
3172
Chet Haase5c13d892010-10-08 08:37:55 -07003173void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003174 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003175 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003176}
3177
Romain Guy9d5316e2010-06-24 19:30:36 -07003178}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003179}; // namespace android