blob: 678770523f85ac3a5efdd19a56f39412985dd131 [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) {
342 SortedVector<Functor*> functors(mFunctors);
343 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700344
Romain Guyba6be8a2012-04-23 18:22:09 -0700345 DrawGlInfo info;
346 info.clipLeft = 0;
347 info.clipTop = 0;
348 info.clipRight = 0;
349 info.clipBottom = 0;
350 info.isLayer = false;
351 info.width = 0;
352 info.height = 0;
353 memset(info.transform, 0, sizeof(float) * 16);
354
355 for (size_t i = 0; i < count; i++) {
356 Functor* f = functors.itemAt(i);
357 result |= (*f)(DrawGlInfo::kModeProcess, &info);
358
Chris Craikc2c95432012-04-25 15:13:52 -0700359 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700360 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
361 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700362 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700363
Chris Craikc2c95432012-04-25 15:13:52 -0700364 if (result & DrawGlInfo::kStatusInvoke) {
365 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700366 }
367 }
Chet Haasebeb8bd02012-09-09 16:13:26 -0700368 // protect against functors binding to other buffers
369 mCaches.unbindMeshBuffer();
370 mCaches.unbindIndicesBuffer();
371 mCaches.activeTexture(0);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700372 }
373
374 return result;
375}
376
377status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800378 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700379 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700380
Romain Guy8a4ac612012-07-17 17:32:48 -0700381 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800382 if (mDirtyClip) {
383 setScissorFromClip();
384 }
Romain Guyd643bb52011-03-01 14:55:21 -0800385
Romain Guy80911b82011-03-16 15:30:12 -0700386 Rect clip(*mSnapshot->clipRect);
387 clip.snapToPixelBoundaries();
388
Romain Guyd643bb52011-03-01 14:55:21 -0800389 // Since we don't know what the functor will draw, let's dirty
390 // tne entire clip region
391 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800392 dirtyLayerUnchecked(clip, getRegion());
393 }
Romain Guyd643bb52011-03-01 14:55:21 -0800394
Romain Guy08aa2cb2011-03-17 11:06:57 -0700395 DrawGlInfo info;
396 info.clipLeft = clip.left;
397 info.clipTop = clip.top;
398 info.clipRight = clip.right;
399 info.clipBottom = clip.bottom;
400 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700401 info.width = getSnapshot()->viewport.getWidth();
402 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700403 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700404
Chet Haase48659092012-05-31 15:21:51 -0700405 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800406
Romain Guy8f3b8e32012-03-27 16:33:45 -0700407 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700408 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800409 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700410
Chris Craik65924a32012-04-05 17:52:11 -0700411 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700412 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700413 }
Romain Guycabfcc12011-03-07 18:06:46 -0800414 }
415
Chet Haasedaf98e92011-01-10 14:10:36 -0800416 resume();
Romain Guy65549432012-03-26 16:45:05 -0700417 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800418}
419
Romain Guyf6a11b82010-06-23 17:47:49 -0700420///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700421// Debug
422///////////////////////////////////////////////////////////////////////////////
423
424void OpenGLRenderer::startMark(const char* name) const {
425 mCaches.startMark(0, name);
426}
427
428void OpenGLRenderer::endMark() const {
429 mCaches.endMark();
430}
431
432void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
433 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
434 if (clear) {
435 mCaches.disableScissor();
436 mCaches.stencil.clear();
437 }
438 if (enable) {
439 mCaches.stencil.enableDebugWrite();
440 } else {
441 mCaches.stencil.disable();
442 }
443 }
444}
445
446void OpenGLRenderer::renderOverdraw() {
447 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
448 const Rect* clip = mTilingSnapshot->clipRect;
449
450 mCaches.enableScissor();
451 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
452 clip->right - clip->left, clip->bottom - clip->top);
453
454 mCaches.stencil.enableDebugTest(2);
455 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
456 mCaches.stencil.enableDebugTest(3);
457 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
458 mCaches.stencil.enableDebugTest(4);
459 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
460 mCaches.stencil.enableDebugTest(4, true);
461 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
462 mCaches.stencil.disable();
463 }
464}
465
466///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700467// Layers
468///////////////////////////////////////////////////////////////////////////////
469
470bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
471 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
472 OpenGLRenderer* renderer = layer->renderer;
473 Rect& dirty = layer->dirtyRect;
474
Romain Guy7c450aa2012-09-21 19:15:00 -0700475 if (inFrame) {
476 endTiling();
477 debugOverdraw(false, false);
478 }
Romain Guy11cb6422012-09-21 00:39:43 -0700479
480 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
481 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
482 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
483 renderer->finish();
484
485 if (inFrame) {
486 resumeAfterLayer();
487 startTiling(mSnapshot);
488 }
489
490 dirty.setEmpty();
491 layer->deferredUpdateScheduled = false;
492 layer->renderer = NULL;
493 layer->displayList = NULL;
494
495 return true;
496 }
497
498 return false;
499}
500
501void OpenGLRenderer::updateLayers() {
502 int count = mLayerUpdates.size();
503 if (count > 0) {
504 startMark("Layer Updates");
505
506 // Note: it is very important to update the layers in reverse order
507 for (int i = count - 1; i >= 0; i--) {
508 Layer* layer = mLayerUpdates.itemAt(i);
509 updateLayer(layer, false);
510 mCaches.resourceCache.decrementRefcount(layer);
511 }
512 mLayerUpdates.clear();
513
514 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
515 endMark();
516 }
517}
518
519void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
520 if (layer) {
521 mLayerUpdates.push_back(layer);
522 mCaches.resourceCache.incrementRefcount(layer);
523 }
524}
525
526void OpenGLRenderer::clearLayerUpdates() {
527 size_t count = mLayerUpdates.size();
528 if (count > 0) {
529 mCaches.resourceCache.lock();
530 for (size_t i = 0; i < count; i++) {
531 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
532 }
533 mCaches.resourceCache.unlock();
534 mLayerUpdates.clear();
535 }
536}
537
538///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700539// State management
540///////////////////////////////////////////////////////////////////////////////
541
Romain Guybb9524b2010-06-22 18:56:38 -0700542int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700543 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700544}
545
546int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700547 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700548}
549
550void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700551 if (mSaveCount > 1) {
552 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700553 }
Romain Guybb9524b2010-06-22 18:56:38 -0700554}
555
556void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700557 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700558
Romain Guy8fb95422010-08-17 18:38:51 -0700559 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700560 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700561 }
Romain Guybb9524b2010-06-22 18:56:38 -0700562}
563
Romain Guy8aef54f2010-09-01 15:13:49 -0700564int OpenGLRenderer::saveSnapshot(int flags) {
565 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700566 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700567}
568
569bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700570 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700571 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700572 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700573
Romain Guybd6b79b2010-06-26 00:13:53 -0700574 sp<Snapshot> current = mSnapshot;
575 sp<Snapshot> previous = mSnapshot->previous;
576
Romain Guyeb993562010-10-05 18:14:38 -0700577 if (restoreOrtho) {
578 Rect& r = previous->viewport;
579 glViewport(r.left, r.top, r.right, r.bottom);
580 mOrthoMatrix.load(current->orthoMatrix);
581 }
582
Romain Guy8b55f372010-08-18 17:10:07 -0700583 mSaveCount--;
584 mSnapshot = previous;
585
Romain Guy2542d192010-08-18 11:47:12 -0700586 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700587 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700588 }
Romain Guy2542d192010-08-18 11:47:12 -0700589
Romain Guy5ec99242010-11-03 16:19:08 -0700590 if (restoreLayer) {
591 composeLayer(current, previous);
592 }
593
Romain Guy2542d192010-08-18 11:47:12 -0700594 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700595}
596
Romain Guyf6a11b82010-06-23 17:47:49 -0700597///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700598// Layers
599///////////////////////////////////////////////////////////////////////////////
600
601int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700602 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700603 const GLuint previousFbo = mSnapshot->fbo;
604 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700605
Romain Guyaf636eb2010-12-09 17:47:21 -0800606 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700607 int alpha = 255;
608 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700609
Romain Guye45362c2010-11-03 19:58:32 -0700610 if (p) {
611 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700612 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700613 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700614 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700615 }
Romain Guyd55a8612010-06-28 17:42:46 -0700616
Chet Haased48885a2012-08-28 17:43:28 -0700617 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700618 }
Romain Guyd55a8612010-06-28 17:42:46 -0700619
620 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700621}
622
623int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
624 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700625 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700626 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700627 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700628 SkPaint paint;
629 paint.setAlpha(alpha);
630 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700631 }
Romain Guyd55a8612010-06-28 17:42:46 -0700632}
Romain Guybd6b79b2010-06-26 00:13:53 -0700633
Romain Guy1c740bc2010-09-13 18:00:09 -0700634/**
635 * Layers are viewed by Skia are slightly different than layers in image editing
636 * programs (for instance.) When a layer is created, previously created layers
637 * and the frame buffer still receive every drawing command. For instance, if a
638 * layer is created and a shape intersecting the bounds of the layers and the
639 * framebuffer is draw, the shape will be drawn on both (unless the layer was
640 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
641 *
642 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
643 * texture. Unfortunately, this is inefficient as it requires every primitive to
644 * be drawn n + 1 times, where n is the number of active layers. In practice this
645 * means, for every primitive:
646 * - Switch active frame buffer
647 * - Change viewport, clip and projection matrix
648 * - Issue the drawing
649 *
650 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700651 * To avoid this, layers are implemented in a different way here, at least in the
652 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
653 * is set. When this flag is set we can redirect all drawing operations into a
654 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700655 *
656 * This implementation relies on the frame buffer being at least RGBA 8888. When
657 * a layer is created, only a texture is created, not an FBO. The content of the
658 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700659 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700660 * buffer and drawing continues as normal. This technique therefore treats the
661 * frame buffer as a scratch buffer for the layers.
662 *
663 * To compose the layers back onto the frame buffer, each layer texture
664 * (containing the original frame buffer data) is drawn as a simple quad over
665 * the frame buffer. The trick is that the quad is set as the composition
666 * destination in the blending equation, and the frame buffer becomes the source
667 * of the composition.
668 *
669 * Drawing layers with an alpha value requires an extra step before composition.
670 * An empty quad is drawn over the layer's region in the frame buffer. This quad
671 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
672 * quad is used to multiply the colors in the frame buffer. This is achieved by
673 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
674 * GL_ZERO, GL_SRC_ALPHA.
675 *
676 * Because glCopyTexImage2D() can be slow, an alternative implementation might
677 * be use to draw a single clipped layer. The implementation described above
678 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700679 *
680 * (1) The frame buffer is actually not cleared right away. To allow the GPU
681 * to potentially optimize series of calls to glCopyTexImage2D, the frame
682 * buffer is left untouched until the first drawing operation. Only when
683 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700684 */
Chet Haased48885a2012-08-28 17:43:28 -0700685bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
686 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700687 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700688 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700689
Romain Guyeb993562010-10-05 18:14:38 -0700690 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
691
Romain Guyf607bdc2010-09-10 19:20:06 -0700692 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700693 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700694 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700695 Rect untransformedBounds(bounds);
696 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700697
Chet Haased48885a2012-08-28 17:43:28 -0700698 // Layers only make sense if they are in the framebuffer's bounds
699 if (bounds.intersect(*mSnapshot->clipRect)) {
700 // We cannot work with sub-pixels in this case
701 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700702
Chet Haased48885a2012-08-28 17:43:28 -0700703 // When the layer is not an FBO, we may use glCopyTexImage so we
704 // need to make sure the layer does not extend outside the bounds
705 // of the framebuffer
706 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700707 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700708 } else if (fboLayer) {
709 clip.set(bounds);
710 mat4 inverse;
711 inverse.loadInverse(*mSnapshot->transform);
712 inverse.mapRect(clip);
713 clip.snapToPixelBoundaries();
714 if (clip.intersect(untransformedBounds)) {
715 clip.translate(-left, -top);
716 bounds.set(untransformedBounds);
717 } else {
718 clip.setEmpty();
719 }
Romain Guyad37cd32011-03-15 11:12:25 -0700720 }
Chet Haased48885a2012-08-28 17:43:28 -0700721 } else {
722 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700723 }
Romain Guybf434112010-09-16 14:40:17 -0700724
Romain Guy746b7402010-10-26 16:27:31 -0700725 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700726 bounds.getHeight() > mCaches.maxTextureSize ||
727 (fboLayer && clip.isEmpty())) {
728 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700729 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700730 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700731 }
732
733 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700734 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700735 return false;
736 }
Romain Guyf18fd992010-07-08 11:45:51 -0700737
Romain Guya1d3c912011-12-13 14:55:06 -0800738 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700739 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700740 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700741 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700742 }
743
Romain Guy9ace8f52011-07-07 20:50:11 -0700744 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700745 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700746 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
747 bounds.getWidth() / float(layer->getWidth()), 0.0f);
748 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700749 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700750 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700751
Romain Guy8fb95422010-08-17 18:38:51 -0700752 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700753 mSnapshot->flags |= Snapshot::kFlagIsLayer;
754 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700755
Romain Guyeb993562010-10-05 18:14:38 -0700756 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700757 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700758 } else {
759 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700760 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800761 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700762 if (layer->isEmpty()) {
763 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700764 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700765 layer->getWidth(), layer->getHeight(), 0);
766 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800767 } else {
768 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700769 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800770 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700771
Romain Guy54be1cd2011-06-13 19:04:27 -0700772 // Enqueue the buffer coordinates to clear the corresponding region later
773 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700774 }
Romain Guyeb993562010-10-05 18:14:38 -0700775 }
Romain Guyf86ef572010-07-01 11:05:42 -0700776
Romain Guyd55a8612010-06-28 17:42:46 -0700777 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700778}
779
Chet Haased48885a2012-08-28 17:43:28 -0700780bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700781 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700782
Chet Haased48885a2012-08-28 17:43:28 -0700783 mSnapshot->region = &mSnapshot->layer->region;
784 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700785
Chet Haased48885a2012-08-28 17:43:28 -0700786 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
787 mSnapshot->fbo = layer->getFbo();
788 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
789 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
790 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
791 mSnapshot->height = bounds.getHeight();
792 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
793 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700794
Romain Guy2b7028e2012-09-19 17:25:38 -0700795 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700796 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700797 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700798 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
799 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700800
801 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700802 if (layer->isEmpty()) {
803 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
804 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700805 }
806
807 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700808 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700809
Romain Guy2b7028e2012-09-19 17:25:38 -0700810 startTiling(mSnapshot);
Romain Guy5b3b3522010-10-27 18:57:51 -0700811
812 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700813 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800814 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700815 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700816 glClear(GL_COLOR_BUFFER_BIT);
817
818 dirtyClip();
819
820 // Change the ortho projection
821 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
822 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
823
824 return true;
825}
826
Romain Guy1c740bc2010-09-13 18:00:09 -0700827/**
828 * Read the documentation of createLayer() before doing anything in this method.
829 */
Romain Guy1d83e192010-08-17 11:37:00 -0700830void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
831 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000832 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700833 return;
834 }
835
Romain Guy5b3b3522010-10-27 18:57:51 -0700836 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700837
838 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700839 endTiling();
840
Romain Guye0aa84b2012-04-03 19:30:26 -0700841 // Detach the texture from the FBO
842 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guyeb993562010-10-05 18:14:38 -0700843 // Unbind current FBO and restore previous one
844 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700845 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700846
847 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700848 }
849
Romain Guy1d83e192010-08-17 11:37:00 -0700850 Layer* layer = current->layer;
851 const Rect& rect = layer->layer;
852
Romain Guy9ace8f52011-07-07 20:50:11 -0700853 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700854 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700855 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700856 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700857 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700858 }
859
Romain Guy03750a02010-10-18 14:06:08 -0700860 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700861
Romain Guya1d3c912011-12-13 14:55:06 -0800862 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700863
Romain Guy5b3b3522010-10-27 18:57:51 -0700864 // When the layer is stored in an FBO, we can save a bit of fillrate by
865 // drawing only the dirty region
866 if (fboLayer) {
867 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700868 if (layer->getColorFilter()) {
869 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800870 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700871 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700872 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800873 resetColorFilter();
874 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700875 } else if (!rect.isEmpty()) {
876 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
877 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700878 }
Romain Guy8b55f372010-08-18 17:10:07 -0700879
Romain Guyeb993562010-10-05 18:14:38 -0700880 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800881 // Note: No need to use glDiscardFramebufferEXT() since we never
882 // create/compose layers that are not on screen with this
883 // code path
884 // See LayerRenderer::destroyLayer(Layer*)
885
Romain Guyeb993562010-10-05 18:14:38 -0700886 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
887 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700888 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700889 }
890
Romain Guy746b7402010-10-26 16:27:31 -0700891 dirtyClip();
892
Romain Guyeb993562010-10-05 18:14:38 -0700893 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700894 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700895 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700896 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700897 }
898}
899
Romain Guyaa6c24c2011-04-28 18:40:04 -0700900void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700901 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700902
903 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700904 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700905 setupDrawWithTexture();
906 } else {
907 setupDrawWithExternalTexture();
908 }
909 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700910 setupDrawColor(alpha, alpha, alpha, alpha);
911 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700912 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700913 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700914 setupDrawPureColorUniforms();
915 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700916 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
917 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700918 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700919 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700920 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700921 if (mSnapshot->transform->isPureTranslate() &&
922 layer->getWidth() == (uint32_t) rect.getWidth() &&
923 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700924 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
925 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
926
Romain Guyd21b6e12011-11-30 20:21:23 -0800927 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700928 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
929 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800930 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700931 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
932 }
933 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700934 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
935
936 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
937
938 finishDrawTexture();
939}
940
Romain Guy5b3b3522010-10-27 18:57:51 -0700941void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700942 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700943 const Rect& texCoords = layer->texCoords;
944 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
945 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700946
Romain Guy9ace8f52011-07-07 20:50:11 -0700947 float x = rect.left;
948 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700949 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700950 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700951 layer->getHeight() == (uint32_t) rect.getHeight();
952
953 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700954 // When we're swapping, the layer is already in screen coordinates
955 if (!swap) {
956 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
957 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
958 }
959
Romain Guyd21b6e12011-11-30 20:21:23 -0800960 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700961 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800962 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700963 }
964
965 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
966 layer->getTexture(), layer->getAlpha() / 255.0f,
967 layer->getMode(), layer->isBlend(),
968 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
969 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700970
Romain Guyaa6c24c2011-04-28 18:40:04 -0700971 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
972 } else {
973 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
974 drawTextureLayer(layer, rect);
975 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
976 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700977}
978
979void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700980 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700981 layer->setRegionAsRect();
982
Romain Guy40667672011-03-18 14:34:03 -0700983 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700984
Romain Guy5b3b3522010-10-27 18:57:51 -0700985 layer->region.clear();
986 return;
987 }
988
Romain Guy8a3957d2011-09-07 17:55:15 -0700989 // TODO: See LayerRenderer.cpp::generateMesh() for important
990 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800991 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700992 size_t count;
993 const android::Rect* rects = layer->region.getArray(&count);
994
Romain Guy9ace8f52011-07-07 20:50:11 -0700995 const float alpha = layer->getAlpha() / 255.0f;
996 const float texX = 1.0f / float(layer->getWidth());
997 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800998 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700999
1000 TextureVertex* mesh = mCaches.getRegionMesh();
1001 GLsizei numQuads = 0;
1002
Romain Guy7230a742011-01-10 22:26:16 -08001003 setupDraw();
1004 setupDrawWithTexture();
1005 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001006 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001007 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001008 setupDrawProgram();
1009 setupDrawDirtyRegionsDisabled();
1010 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001011 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001012 setupDrawTexture(layer->getTexture());
1013 if (mSnapshot->transform->isPureTranslate()) {
1014 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
1015 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
1016
Romain Guyd21b6e12011-11-30 20:21:23 -08001017 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001018 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1019 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001020 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001021 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1022 }
Romain Guy15bc6432011-12-13 13:11:32 -08001023 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001024
1025 for (size_t i = 0; i < count; i++) {
1026 const android::Rect* r = &rects[i];
1027
1028 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001029 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001030 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001031 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001032
1033 // TODO: Reject quads outside of the clip
1034 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1035 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1036 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1037 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1038
1039 numQuads++;
1040
1041 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1042 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1043 numQuads = 0;
1044 mesh = mCaches.getRegionMesh();
1045 }
1046 }
1047
1048 if (numQuads > 0) {
1049 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1050 }
1051
Romain Guy7230a742011-01-10 22:26:16 -08001052 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001053
1054#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001055 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001056#endif
1057
1058 layer->region.clear();
1059 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001060}
1061
Romain Guy3a3133d2011-02-01 22:59:58 -08001062void OpenGLRenderer::drawRegionRects(const Region& region) {
1063#if DEBUG_LAYERS_AS_REGIONS
1064 size_t count;
1065 const android::Rect* rects = region.getArray(&count);
1066
1067 uint32_t colors[] = {
1068 0x7fff0000, 0x7f00ff00,
1069 0x7f0000ff, 0x7fff00ff,
1070 };
1071
1072 int offset = 0;
1073 int32_t top = rects[0].top;
1074
1075 for (size_t i = 0; i < count; i++) {
1076 if (top != rects[i].top) {
1077 offset ^= 0x2;
1078 top = rects[i].top;
1079 }
1080
1081 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1082 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1083 SkXfermode::kSrcOver_Mode);
1084 }
1085#endif
1086}
1087
Romain Guy5b3b3522010-10-27 18:57:51 -07001088void OpenGLRenderer::dirtyLayer(const float left, const float top,
1089 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001090 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001091 Rect bounds(left, top, right, bottom);
1092 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001093 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001094 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001095}
1096
1097void OpenGLRenderer::dirtyLayer(const float left, const float top,
1098 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001099 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001100 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001101 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001102 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001103}
1104
1105void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001106 if (bounds.intersect(*mSnapshot->clipRect)) {
1107 bounds.snapToPixelBoundaries();
1108 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1109 if (!dirty.isEmpty()) {
1110 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001111 }
1112 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001113}
1114
Romain Guy54be1cd2011-06-13 19:04:27 -07001115void OpenGLRenderer::clearLayerRegions() {
1116 const size_t count = mLayers.size();
1117 if (count == 0) return;
1118
1119 if (!mSnapshot->isIgnored()) {
1120 // Doing several glScissor/glClear here can negatively impact
1121 // GPUs with a tiler architecture, instead we draw quads with
1122 // the Clear blending mode
1123
1124 // The list contains bounds that have already been clipped
1125 // against their initial clip rect, and the current clip
1126 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001127 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001128
1129 Vertex mesh[count * 6];
1130 Vertex* vertex = mesh;
1131
1132 for (uint32_t i = 0; i < count; i++) {
1133 Rect* bounds = mLayers.itemAt(i);
1134
1135 Vertex::set(vertex++, bounds->left, bounds->bottom);
1136 Vertex::set(vertex++, bounds->left, bounds->top);
1137 Vertex::set(vertex++, bounds->right, bounds->top);
1138 Vertex::set(vertex++, bounds->left, bounds->bottom);
1139 Vertex::set(vertex++, bounds->right, bounds->top);
1140 Vertex::set(vertex++, bounds->right, bounds->bottom);
1141
1142 delete bounds;
1143 }
1144
1145 setupDraw(false);
1146 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1147 setupDrawBlending(true, SkXfermode::kClear_Mode);
1148 setupDrawProgram();
1149 setupDrawPureColorUniforms();
1150 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001151 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001152
Romain Guy54be1cd2011-06-13 19:04:27 -07001153 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001154
1155 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001156 } else {
1157 for (uint32_t i = 0; i < count; i++) {
1158 delete mLayers.itemAt(i);
1159 }
1160 }
1161
1162 mLayers.clear();
1163}
1164
Romain Guybd6b79b2010-06-26 00:13:53 -07001165///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001166// Transforms
1167///////////////////////////////////////////////////////////////////////////////
1168
1169void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001170 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001171}
1172
1173void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001174 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001175}
1176
1177void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001178 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001179}
1180
Romain Guy807daf72011-01-18 11:19:19 -08001181void OpenGLRenderer::skew(float sx, float sy) {
1182 mSnapshot->transform->skew(sx, sy);
1183}
1184
Romain Guyf6a11b82010-06-23 17:47:49 -07001185void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001186 if (matrix) {
1187 mSnapshot->transform->load(*matrix);
1188 } else {
1189 mSnapshot->transform->loadIdentity();
1190 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001191}
1192
1193void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001194 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001195}
1196
1197void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001198 SkMatrix transform;
1199 mSnapshot->transform->copyTo(transform);
1200 transform.preConcat(*matrix);
1201 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001202}
1203
1204///////////////////////////////////////////////////////////////////////////////
1205// Clipping
1206///////////////////////////////////////////////////////////////////////////////
1207
Romain Guybb9524b2010-06-22 18:56:38 -07001208void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001209 Rect clip(*mSnapshot->clipRect);
1210 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001211
Romain Guy8a4ac612012-07-17 17:32:48 -07001212 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1213 clip.getWidth(), clip.getHeight())) {
1214 mDirtyClip = false;
1215 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001216}
1217
1218const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001219 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001220}
1221
Romain Guy8a4ac612012-07-17 17:32:48 -07001222bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1223 if (mSnapshot->isIgnored()) {
1224 return true;
1225 }
1226
1227 Rect r(left, top, right, bottom);
1228 mSnapshot->transform->mapRect(r);
1229 r.snapToPixelBoundaries();
1230
1231 Rect clipRect(*mSnapshot->clipRect);
1232 clipRect.snapToPixelBoundaries();
1233
1234 return !clipRect.intersects(r);
1235}
1236
Romain Guy35643dd2012-09-18 15:40:58 -07001237bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1238 Rect& transformed, Rect& clip) {
1239 if (mSnapshot->isIgnored()) {
1240 return true;
1241 }
1242
1243 transformed.set(left, top, right, bottom);
1244 mSnapshot->transform->mapRect(transformed);
1245 transformed.snapToPixelBoundaries();
1246
1247 clip.set(*mSnapshot->clipRect);
1248 clip.snapToPixelBoundaries();
1249
1250 return !clip.intersects(transformed);
1251}
1252
Chris Craikcb4d6002012-09-25 12:00:29 -07001253bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom, SkPaint* paint) {
1254 if (paint->getStyle() != SkPaint::kFill_Style) {
1255 float outset = paint->getStrokeWidth() * 0.5f;
1256 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1257 } else {
1258 return quickReject(left, top, right, bottom);
1259 }
1260}
1261
Romain Guyc7d53492010-06-25 13:41:57 -07001262bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001263 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001264 return true;
1265 }
1266
Romain Guy1d83e192010-08-17 11:37:00 -07001267 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001268 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001269 r.snapToPixelBoundaries();
1270
1271 Rect clipRect(*mSnapshot->clipRect);
1272 clipRect.snapToPixelBoundaries();
1273
Romain Guy586cae32012-07-13 15:28:31 -07001274 bool rejected = !clipRect.intersects(r);
1275 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001276 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001277 }
1278
1279 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001280}
1281
Romain Guy079ba2c2010-07-16 14:12:24 -07001282bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1283 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001284 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001285 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001286 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001287 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001288}
1289
Chet Haasea23eed82012-04-12 15:19:04 -07001290Rect* OpenGLRenderer::getClipRect() {
1291 return mSnapshot->clipRect;
1292}
1293
Romain Guyf6a11b82010-06-23 17:47:49 -07001294///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001295// Drawing commands
1296///////////////////////////////////////////////////////////////////////////////
1297
Romain Guy54be1cd2011-06-13 19:04:27 -07001298void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001299 // TODO: It would be best if we could do this before quickReject()
1300 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001301 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001302 if (mDirtyClip) {
1303 setScissorFromClip();
1304 }
1305 mDescription.reset();
1306 mSetShaderColor = false;
1307 mColorSet = false;
1308 mColorA = mColorR = mColorG = mColorB = 0.0f;
1309 mTextureUnit = 0;
1310 mTrackDirtyRegions = true;
1311}
1312
1313void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1314 mDescription.hasTexture = true;
1315 mDescription.hasAlpha8Texture = isAlpha8;
1316}
1317
Romain Guyaa6c24c2011-04-28 18:40:04 -07001318void OpenGLRenderer::setupDrawWithExternalTexture() {
1319 mDescription.hasExternalTexture = true;
1320}
1321
Romain Guy15bc6432011-12-13 13:11:32 -08001322void OpenGLRenderer::setupDrawNoTexture() {
1323 mCaches.disbaleTexCoordsVertexArray();
1324}
1325
Chris Craik710f46d2012-09-17 17:25:49 -07001326void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001327 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001328}
1329
Chris Craik710f46d2012-09-17 17:25:49 -07001330void OpenGLRenderer::setupDrawVertexShape() {
1331 mDescription.isVertexShape = true;
Chris Craik6ebdc112012-08-31 18:24:33 -07001332}
1333
Romain Guyed6fcb02011-03-21 13:11:28 -07001334void OpenGLRenderer::setupDrawPoint(float pointSize) {
1335 mDescription.isPoint = true;
1336 mDescription.pointSize = pointSize;
1337}
1338
Romain Guy70ca14e2010-12-13 18:24:33 -08001339void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001340 setupDrawColor(color, (color >> 24) & 0xFF);
1341}
1342
1343void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1344 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001345 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1346 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001347 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001348 mColorR = a * ((color >> 16) & 0xFF);
1349 mColorG = a * ((color >> 8) & 0xFF);
1350 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001351 mColorSet = true;
1352 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1353}
1354
Romain Guy86568192010-12-14 15:55:39 -08001355void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1356 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001357 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1358 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001359 const float a = mColorA / 255.0f;
1360 mColorR = a * ((color >> 16) & 0xFF);
1361 mColorG = a * ((color >> 8) & 0xFF);
1362 mColorB = a * ((color ) & 0xFF);
1363 mColorSet = true;
1364 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1365}
1366
Romain Guy41210632012-07-16 17:04:24 -07001367void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1368 mCaches.fontRenderer->describe(mDescription, paint);
1369}
1370
Romain Guy70ca14e2010-12-13 18:24:33 -08001371void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1372 mColorA = a;
1373 mColorR = r;
1374 mColorG = g;
1375 mColorB = b;
1376 mColorSet = true;
1377 mSetShaderColor = mDescription.setColor(r, g, b, a);
1378}
1379
1380void OpenGLRenderer::setupDrawShader() {
1381 if (mShader) {
1382 mShader->describe(mDescription, mCaches.extensions);
1383 }
1384}
1385
1386void OpenGLRenderer::setupDrawColorFilter() {
1387 if (mColorFilter) {
1388 mColorFilter->describe(mDescription, mCaches.extensions);
1389 }
1390}
1391
Romain Guyf09ef512011-05-27 11:43:46 -07001392void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1393 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1394 mColorA = 1.0f;
1395 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001396 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001397 }
1398}
1399
Romain Guy70ca14e2010-12-13 18:24:33 -08001400void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001401 // When the blending mode is kClear_Mode, we need to use a modulate color
1402 // argb=1,0,0,0
1403 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001404 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1405 mDescription, swapSrcDst);
1406}
1407
1408void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001409 // When the blending mode is kClear_Mode, we need to use a modulate color
1410 // argb=1,0,0,0
1411 accountForClear(mode);
Romain Guye83221c2012-09-24 16:01:35 -07001412 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()) ||
1413 (mColorFilter && mColorFilter->blend()), mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001414}
1415
1416void OpenGLRenderer::setupDrawProgram() {
1417 useProgram(mCaches.programCache.get(mDescription));
1418}
1419
1420void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1421 mTrackDirtyRegions = false;
1422}
1423
1424void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1425 bool ignoreTransform) {
1426 mModelView.loadTranslate(left, top, 0.0f);
1427 if (!ignoreTransform) {
1428 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1429 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1430 } else {
1431 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1432 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1433 }
1434}
1435
Chet Haase8a5cc922011-04-26 07:28:09 -07001436void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1437 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001438}
1439
Romain Guy70ca14e2010-12-13 18:24:33 -08001440void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1441 bool ignoreTransform, bool ignoreModelView) {
1442 if (!ignoreModelView) {
1443 mModelView.loadTranslate(left, top, 0.0f);
1444 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001445 } else {
1446 mModelView.loadIdentity();
1447 }
Romain Guy86568192010-12-14 15:55:39 -08001448 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1449 if (!ignoreTransform) {
1450 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1451 if (mTrackDirtyRegions && dirty) {
1452 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1453 }
1454 } else {
1455 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1456 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1457 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001458}
1459
Romain Guyed6fcb02011-03-21 13:11:28 -07001460void OpenGLRenderer::setupDrawPointUniforms() {
1461 int slot = mCaches.currentProgram->getUniform("pointSize");
1462 glUniform1f(slot, mDescription.pointSize);
1463}
1464
Romain Guy70ca14e2010-12-13 18:24:33 -08001465void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001466 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001467 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1468 }
1469}
1470
Romain Guy86568192010-12-14 15:55:39 -08001471void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001472 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001473 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001474 }
1475}
1476
Romain Guy70ca14e2010-12-13 18:24:33 -08001477void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1478 if (mShader) {
1479 if (ignoreTransform) {
1480 mModelView.loadInverse(*mSnapshot->transform);
1481 }
1482 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1483 }
1484}
1485
Romain Guy8d0d4782010-12-14 20:13:35 -08001486void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1487 if (mShader) {
1488 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1489 }
1490}
1491
Romain Guy70ca14e2010-12-13 18:24:33 -08001492void OpenGLRenderer::setupDrawColorFilterUniforms() {
1493 if (mColorFilter) {
1494 mColorFilter->setupProgram(mCaches.currentProgram);
1495 }
1496}
1497
Romain Guy41210632012-07-16 17:04:24 -07001498void OpenGLRenderer::setupDrawTextGammaUniforms() {
1499 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1500}
1501
Romain Guy70ca14e2010-12-13 18:24:33 -08001502void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001503 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001504 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001505 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001506}
1507
1508void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1509 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001510 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001511 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001512}
1513
Romain Guyaa6c24c2011-04-28 18:40:04 -07001514void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1515 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001516 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001517 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001518}
1519
Romain Guy8f0095c2011-05-02 17:24:22 -07001520void OpenGLRenderer::setupDrawTextureTransform() {
1521 mDescription.hasTextureTransform = true;
1522}
1523
1524void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001525 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1526 GL_FALSE, &transform.data[0]);
1527}
1528
Romain Guy70ca14e2010-12-13 18:24:33 -08001529void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001530 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001531 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001532 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001533 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001534 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001535 }
Romain Guyd71dd362011-12-12 19:03:35 -08001536
Chris Craikcb4d6002012-09-25 12:00:29 -07001537 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001538 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001539 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001540 }
1541
1542 mCaches.unbindIndicesBuffer();
1543}
1544
1545void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1546 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001547 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001548 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001549 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001550 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001551}
1552
Chet Haase5b0200b2011-04-13 17:58:08 -07001553void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001554 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001555 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001556 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001557}
1558
1559/**
1560 * 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 -07001561 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1562 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1563 * attributes (one per vertex) are values from zero to one that tells the fragment
1564 * shader where the fragment is in relation to the line width/length overall; these values are
1565 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1566 * region of the line.
1567 * Note that we only pass down the width values in this setup function. The length coordinates
1568 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001569 */
Chet Haase99585ad2011-05-02 15:00:16 -07001570void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001571 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001572 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001573 mCaches.bindPositionVertexPointer(force, vertices, gAAVertexStride);
Romain Guyf3a910b42011-12-12 20:35:21 -08001574 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001575 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001576
Romain Guy7b631422012-04-04 11:38:54 -07001577 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001578 glEnableVertexAttribArray(widthSlot);
1579 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001580
Romain Guy7b631422012-04-04 11:38:54 -07001581 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001582 glEnableVertexAttribArray(lengthSlot);
1583 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001584
Chet Haase5b0200b2011-04-13 17:58:08 -07001585 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001586 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001587}
1588
1589void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1590 glDisableVertexAttribArray(widthSlot);
1591 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001592}
1593
Romain Guy70ca14e2010-12-13 18:24:33 -08001594void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001595}
1596
1597///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001598// Drawing
1599///////////////////////////////////////////////////////////////////////////////
1600
Chet Haase1271e2c2012-04-20 09:54:27 -07001601status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001602 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001603
Romain Guy0fe478e2010-11-08 12:08:41 -08001604 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1605 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001606 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001607 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001608 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001609
Romain Guy65549432012-03-26 16:45:05 -07001610 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001611}
1612
Chet Haaseed30fd82011-04-22 16:18:45 -07001613void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1614 if (displayList) {
1615 displayList->output(*this, level);
1616 }
1617}
1618
Romain Guya168d732011-03-18 16:50:13 -07001619void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1620 int alpha;
1621 SkXfermode::Mode mode;
1622 getAlphaAndMode(paint, &alpha, &mode);
1623
Romain Guya168d732011-03-18 16:50:13 -07001624 float x = left;
1625 float y = top;
1626
Romain Guye3c26852011-07-25 16:36:01 -07001627 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001628 bool ignoreTransform = false;
1629 if (mSnapshot->transform->isPureTranslate()) {
1630 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1631 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1632 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001633 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001634 } else {
1635 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001636 }
1637
1638 setupDraw();
1639 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001640 if (paint) {
1641 setupDrawAlpha8Color(paint->getColor(), alpha);
1642 }
Romain Guya168d732011-03-18 16:50:13 -07001643 setupDrawColorFilter();
1644 setupDrawShader();
1645 setupDrawBlending(true, mode);
1646 setupDrawProgram();
1647 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001648
Romain Guya168d732011-03-18 16:50:13 -07001649 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001650 texture->setWrap(GL_CLAMP_TO_EDGE);
1651 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001652
Romain Guya168d732011-03-18 16:50:13 -07001653 setupDrawPureColorUniforms();
1654 setupDrawColorFilterUniforms();
1655 setupDrawShaderUniforms();
1656 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1657
1658 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1659
1660 finishDrawTexture();
1661}
1662
Chet Haase48659092012-05-31 15:21:51 -07001663status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001664 const float right = left + bitmap->width();
1665 const float bottom = top + bitmap->height();
1666
1667 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001668 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001669 }
1670
Romain Guya1d3c912011-12-13 14:55:06 -08001671 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001672 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001673 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001674 const AutoTexture autoCleanup(texture);
1675
Romain Guy211370f2012-02-01 16:10:55 -08001676 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001677 drawAlphaBitmap(texture, left, top, paint);
1678 } else {
1679 drawTextureRect(left, top, right, bottom, texture, paint);
1680 }
Chet Haase48659092012-05-31 15:21:51 -07001681
1682 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001683}
1684
Chet Haase48659092012-05-31 15:21:51 -07001685status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001686 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1687 const mat4 transform(*matrix);
1688 transform.mapRect(r);
1689
Romain Guy6926c722010-07-12 20:20:03 -07001690 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001691 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001692 }
1693
Romain Guya1d3c912011-12-13 14:55:06 -08001694 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001695 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001696 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001697 const AutoTexture autoCleanup(texture);
1698
Romain Guy5b3b3522010-10-27 18:57:51 -07001699 // This could be done in a cheaper way, all we need is pass the matrix
1700 // to the vertex shader. The save/restore is a bit overkill.
1701 save(SkCanvas::kMatrix_SaveFlag);
1702 concatMatrix(matrix);
1703 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1704 restore();
Chet Haase48659092012-05-31 15:21:51 -07001705
1706 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001707}
1708
Chet Haase48659092012-05-31 15:21:51 -07001709status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001710 const float right = left + bitmap->width();
1711 const float bottom = top + bitmap->height();
1712
1713 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001714 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001715 }
1716
1717 mCaches.activeTexture(0);
1718 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1719 const AutoTexture autoCleanup(texture);
1720
1721 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001722
1723 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001724}
1725
Chet Haase48659092012-05-31 15:21:51 -07001726status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001727 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001728 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001729 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001730 }
1731
Romain Guya92bb4d2012-10-16 11:08:44 -07001732 // TODO: We should compute the bounding box when recording the display list
Romain Guyb18d2d02011-02-10 15:52:54 -08001733 float left = FLT_MAX;
1734 float top = FLT_MAX;
1735 float right = FLT_MIN;
1736 float bottom = FLT_MIN;
1737
Romain Guya92bb4d2012-10-16 11:08:44 -07001738 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001739
Romain Guya566b7c2011-01-23 16:36:11 -08001740 // TODO: Support the colors array
1741 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001742 TextureVertex* vertex = mesh;
Romain Guya92bb4d2012-10-16 11:08:44 -07001743
Romain Guy5a7b4662011-01-20 19:09:30 -08001744 for (int32_t y = 0; y < meshHeight; y++) {
1745 for (int32_t x = 0; x < meshWidth; x++) {
1746 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1747
1748 float u1 = float(x) / meshWidth;
1749 float u2 = float(x + 1) / meshWidth;
1750 float v1 = float(y) / meshHeight;
1751 float v2 = float(y + 1) / meshHeight;
1752
1753 int ax = i + (meshWidth + 1) * 2;
1754 int ay = ax + 1;
1755 int bx = i;
1756 int by = bx + 1;
1757 int cx = i + 2;
1758 int cy = cx + 1;
1759 int dx = i + (meshWidth + 1) * 2 + 2;
1760 int dy = dx + 1;
1761
1762 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1763 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1764 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1765
1766 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1767 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1768 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001769
Romain Guya92bb4d2012-10-16 11:08:44 -07001770 // TODO: This could be optimized to avoid unnecessary ops
1771 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1772 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1773 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1774 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001775 }
1776 }
1777
Romain Guya92bb4d2012-10-16 11:08:44 -07001778 if (quickReject(left, top, right, bottom)) {
1779 return DrawGlInfo::kStatusDone;
1780 }
1781
1782 mCaches.activeTexture(0);
1783 Texture* texture = mCaches.textureCache.get(bitmap);
1784 if (!texture) return DrawGlInfo::kStatusDone;
1785 const AutoTexture autoCleanup(texture);
1786
1787 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1788 texture->setFilter(FILTER(paint), true);
1789
1790 int alpha;
1791 SkXfermode::Mode mode;
1792 getAlphaAndMode(paint, &alpha, &mode);
1793
1794 if (hasLayer()) {
Romain Guyb18d2d02011-02-10 15:52:54 -08001795 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1796 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001797
Romain Guy5a7b4662011-01-20 19:09:30 -08001798 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1799 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001800 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001801
1802 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001803}
1804
Chet Haase48659092012-05-31 15:21:51 -07001805status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001806 float srcLeft, float srcTop, float srcRight, float srcBottom,
1807 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001808 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001809 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001810 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001811 }
1812
Romain Guya1d3c912011-12-13 14:55:06 -08001813 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001814 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001815 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001816 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001817
Romain Guy8ba548f2010-06-30 19:21:21 -07001818 const float width = texture->width;
1819 const float height = texture->height;
1820
Romain Guy68169722011-08-22 17:33:33 -07001821 const float u1 = fmax(0.0f, srcLeft / width);
1822 const float v1 = fmax(0.0f, srcTop / height);
1823 const float u2 = fmin(1.0f, srcRight / width);
1824 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001825
Romain Guy03750a02010-10-18 14:06:08 -07001826 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001827 resetDrawTextureTexCoords(u1, v1, u2, v2);
1828
Romain Guy03750a02010-10-18 14:06:08 -07001829 int alpha;
1830 SkXfermode::Mode mode;
1831 getAlphaAndMode(paint, &alpha, &mode);
1832
Romain Guyd21b6e12011-11-30 20:21:23 -08001833 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1834
Romain Guy211370f2012-02-01 16:10:55 -08001835 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001836 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1837 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1838
Romain Guyb5014982011-07-28 15:39:12 -07001839 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001840 // Enable linear filtering if the source rectangle is scaled
1841 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001842 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001843 }
Romain Guyb5014982011-07-28 15:39:12 -07001844
Romain Guyd21b6e12011-11-30 20:21:23 -08001845 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001846 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1847 texture->id, alpha / 255.0f, mode, texture->blend,
1848 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1849 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1850 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001851 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001852 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1853 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1854 GL_TRIANGLE_STRIP, gMeshCount);
1855 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001856
1857 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001858
1859 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001860}
1861
Chet Haase48659092012-05-31 15:21:51 -07001862status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001863 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001864 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001865 int alpha;
1866 SkXfermode::Mode mode;
1867 getAlphaAndModeDirect(paint, &alpha, &mode);
1868
1869 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1870 left, top, right, bottom, alpha, mode);
1871}
1872
1873status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1874 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1875 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001876 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001877 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001878 }
1879
Romain Guybe6f9dc2012-07-16 12:41:17 -07001880 alpha *= mSnapshot->alpha;
1881
Romain Guya1d3c912011-12-13 14:55:06 -08001882 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001883 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001884 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001885 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001886 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1887 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001888
Romain Guy2728f962010-10-08 18:36:15 -07001889 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001890 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001891
Romain Guy211370f2012-02-01 16:10:55 -08001892 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001893 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001894 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001895 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001896 const float offsetX = left + mSnapshot->transform->getTranslateX();
1897 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001898 const size_t count = mesh->quads.size();
1899 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001900 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001901 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001902 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1903 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1904 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001905 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001906 dirtyLayer(left + bounds.left, top + bounds.top,
1907 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001908 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001909 }
1910 }
1911
Romain Guy211370f2012-02-01 16:10:55 -08001912 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001913 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1914 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1915
1916 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1917 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1918 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1919 true, !mesh->hasEmptyQuads);
1920 } else {
1921 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1922 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1923 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1924 true, !mesh->hasEmptyQuads);
1925 }
Romain Guy054dc182010-10-15 17:55:25 -07001926 }
Chet Haase48659092012-05-31 15:21:51 -07001927
1928 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001929}
1930
Chet Haase99ecdc42011-05-06 12:06:34 -07001931/**
Chris Craikcb4d6002012-09-25 12:00:29 -07001932 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
1933 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
1934 * screen space in all directions. However, instead of using a fragment shader to compute the
1935 * translucency of the color from its position, we simply use a varying parameter to define how far
1936 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
1937 *
1938 * Doesn't yet support joins, caps, or path effects.
Chet Haase858aa932011-05-12 09:06:00 -07001939 */
Chris Craikcb4d6002012-09-25 12:00:29 -07001940void OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
1941 int color = paint->getColor();
1942 SkPaint::Style style = paint->getStyle();
1943 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
1944 bool isAA = paint->isAntiAlias();
1945
Chris Craik710f46d2012-09-17 17:25:49 -07001946 VertexBuffer vertexBuffer;
1947 // TODO: try clipping large paths to viewport
Chris Craikcb4d6002012-09-25 12:00:29 -07001948 PathRenderer::convexPathVertices(path, paint, mSnapshot->transform, vertexBuffer);
Romain Guy04299382012-07-18 17:15:41 -07001949
Chris Craikbf09ffb2012-10-01 13:50:37 -07001950 if (!vertexBuffer.getSize()) {
1951 // no vertices to draw
1952 return;
1953 }
1954
Chris Craik710f46d2012-09-17 17:25:49 -07001955 setupDraw();
1956 setupDrawNoTexture();
1957 if (isAA) setupDrawAA();
1958 setupDrawVertexShape();
1959 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1960 setupDrawColorFilter();
1961 setupDrawShader();
1962 setupDrawBlending(isAA, mode);
1963 setupDrawProgram();
Chris Craikcb4d6002012-09-25 12:00:29 -07001964 setupDrawModelViewIdentity();
Chris Craik710f46d2012-09-17 17:25:49 -07001965 setupDrawColorUniforms();
1966 setupDrawColorFilterUniforms();
1967 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07001968
Chris Craik710f46d2012-09-17 17:25:49 -07001969 void* vertices = vertexBuffer.getBuffer();
1970 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001971 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07001972 mCaches.resetTexCoordsVertexPointer();
1973 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07001974
Chris Craik710f46d2012-09-17 17:25:49 -07001975 int alphaSlot = -1;
1976 if (isAA) {
1977 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
1978 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07001979
Chris Craik710f46d2012-09-17 17:25:49 -07001980 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07001981 glEnableVertexAttribArray(alphaSlot);
1982 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07001983 }
Romain Guy04299382012-07-18 17:15:41 -07001984
Chris Craikcb4d6002012-09-25 12:00:29 -07001985 SkRect bounds = PathRenderer::computePathBounds(path, paint);
Chris Craik710f46d2012-09-17 17:25:49 -07001986 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy04299382012-07-18 17:15:41 -07001987
Chris Craik710f46d2012-09-17 17:25:49 -07001988 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07001989
Chris Craik710f46d2012-09-17 17:25:49 -07001990 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07001991 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001992 }
Chet Haase858aa932011-05-12 09:06:00 -07001993}
1994
1995/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001996 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1997 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1998 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1999 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
2000 * of the line. Hairlines are more involved because we need to account for transform scaling
2001 * to end up with a one-pixel-wide line in screen space..
2002 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
2003 * in combination with values that we calculate and pass down in this method. The basic approach
2004 * is that the quad we create contains both the core line area plus a bounding area in which
2005 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
2006 * proportion of the width and the length of a given segment is represented by the boundary
2007 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
2008 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
2009 * on the inside). This ends up giving the result we want, with pixels that are completely
2010 * 'inside' the line area being filled opaquely and the other pixels being filled according to
2011 * how far into the boundary region they are, which is determined by shader interpolation.
2012 */
Chet Haase48659092012-05-31 15:21:51 -07002013status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
2014 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002015
2016 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07002017 // We use half the stroke width here because we're going to position the quad
2018 // corner vertices half of the width away from the line endpoints
2019 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002020 // A stroke width of 0 has a special meaning in Skia:
2021 // it draws a line 1 px wide regardless of current transform
2022 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07002023
Chet Haase99ecdc42011-05-06 12:06:34 -07002024 float inverseScaleX = 1.0f;
2025 float inverseScaleY = 1.0f;
2026 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07002027
Chet Haase8a5cc922011-04-26 07:28:09 -07002028 int alpha;
2029 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07002030
Chet Haase8a5cc922011-04-26 07:28:09 -07002031 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002032 int verticesCount = count;
2033 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07002034 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07002035 verticesCount += (count - 4);
2036 }
2037
2038 if (isHairLine || isAA) {
2039 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
2040 // the line on the screen should always be one pixel wide regardless of scale. For
2041 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08002042 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07002043 Matrix4 *mat = mSnapshot->transform;
2044 float m00 = mat->data[Matrix4::kScaleX];
2045 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07002046 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07002047 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07002048
Romain Guy211370f2012-02-01 16:10:55 -08002049 float scaleX = sqrtf(m00 * m00 + m01 * m01);
2050 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07002051
Chet Haase99ecdc42011-05-06 12:06:34 -07002052 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
2053 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07002054
Chet Haase99ecdc42011-05-06 12:06:34 -07002055 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
2056 scaled = true;
2057 }
2058 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002059 }
Chet Haase8a5cc922011-04-26 07:28:09 -07002060
2061 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07002062
2063 mCaches.enableScissor();
2064
Chet Haase8a5cc922011-04-26 07:28:09 -07002065 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002066 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002067 if (isAA) {
Chris Craik710f46d2012-09-17 17:25:49 -07002068 setupDrawAA();
Chet Haase8a5cc922011-04-26 07:28:09 -07002069 }
2070 setupDrawColor(paint->getColor(), alpha);
2071 setupDrawColorFilter();
2072 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08002073 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07002074 setupDrawProgram();
Chris Craikb30cb102012-10-05 19:11:37 -07002075 setupDrawModelViewIdentity(true);
Chet Haase8a5cc922011-04-26 07:28:09 -07002076 setupDrawColorUniforms();
2077 setupDrawColorFilterUniforms();
2078 setupDrawShaderIdentityUniforms();
2079
2080 if (isHairLine) {
2081 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07002082 halfStrokeWidth = isAA? 1 : .5;
2083 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002084 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07002085 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07002086 }
Romain Guy7b631422012-04-04 11:38:54 -07002087
2088 int widthSlot;
2089 int lengthSlot;
2090
Chet Haase5b0200b2011-04-13 17:58:08 -07002091 Vertex lines[verticesCount];
2092 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002093
Chet Haase99585ad2011-05-02 15:00:16 -07002094 AAVertex wLines[verticesCount];
2095 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002096
Romain Guy211370f2012-02-01 16:10:55 -08002097 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002098 setupDrawVertices(vertices);
2099 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002100 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2101 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002102 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002103 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2104 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002105 // We will need to calculate the actual width proportion on each segment for
2106 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002107 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002108 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2109 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002110 }
2111
Chet Haase99ecdc42011-05-06 12:06:34 -07002112 AAVertex* prevAAVertex = NULL;
2113 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002114
Chet Haase99585ad2011-05-02 15:00:16 -07002115 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002116 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002117
Chet Haase5b0200b2011-04-13 17:58:08 -07002118 for (int i = 0; i < count; i += 4) {
2119 // a = start point, b = end point
2120 vec2 a(points[i], points[i + 1]);
2121 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002122
Chet Haase99585ad2011-05-02 15:00:16 -07002123 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002124 float boundaryLengthProportion = 0;
2125 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002126
Chet Haase5b0200b2011-04-13 17:58:08 -07002127 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002128 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002129 float x = n.x;
2130 n.x = -n.y;
2131 n.y = x;
2132
Chet Haase8a5cc922011-04-26 07:28:09 -07002133 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002134 if (isAA) {
2135 float wideningFactor;
2136 if (fabs(n.x) >= fabs(n.y)) {
2137 wideningFactor = fabs(1.0f / n.x);
2138 } else {
2139 wideningFactor = fabs(1.0f / n.y);
2140 }
2141 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002142 }
Romain Guy7b631422012-04-04 11:38:54 -07002143
Chet Haase99ecdc42011-05-06 12:06:34 -07002144 if (scaled) {
2145 n.x *= inverseScaleX;
2146 n.y *= inverseScaleY;
2147 }
2148 } else if (scaled) {
2149 // Extend n by .5 pixel on each side, post-transform
2150 vec2 extendedN = n.copyNormalized();
2151 extendedN /= 2;
2152 extendedN.x *= inverseScaleX;
2153 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002154
Chet Haase99ecdc42011-05-06 12:06:34 -07002155 float extendedNLength = extendedN.length();
2156 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002157 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002158 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002159 }
Romain Guy7b631422012-04-04 11:38:54 -07002160
Chet Haase99585ad2011-05-02 15:00:16 -07002161 // aa lines expand the endpoint vertices to encompass the AA boundary
2162 if (isAA) {
2163 vec2 abVector = (b - a);
2164 length = abVector.length();
2165 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002166
Chet Haase99ecdc42011-05-06 12:06:34 -07002167 if (scaled) {
2168 abVector.x *= inverseScaleX;
2169 abVector.y *= inverseScaleY;
2170 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002171 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002172 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002173 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002174 }
Romain Guy7b631422012-04-04 11:38:54 -07002175
Chet Haase99ecdc42011-05-06 12:06:34 -07002176 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002177 a -= abVector;
2178 b += abVector;
2179 }
2180
Chet Haase5b0200b2011-04-13 17:58:08 -07002181 // Four corners of the rectangle defining a thick line
2182 vec2 p1 = a - n;
2183 vec2 p2 = a + n;
2184 vec2 p3 = b + n;
2185 vec2 p4 = b - n;
2186
Chet Haase99585ad2011-05-02 15:00:16 -07002187
Chet Haase5b0200b2011-04-13 17:58:08 -07002188 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2189 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2190 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2191 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2192
Romain Guy04299382012-07-18 17:15:41 -07002193 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002194 if (!isAA) {
2195 if (prevVertex != NULL) {
2196 // Issue two repeat vertices to create degenerate triangles to bridge
2197 // between the previous line and the new one. This is necessary because
2198 // we are creating a single triangle_strip which will contain
2199 // potentially discontinuous line segments.
2200 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2201 Vertex::set(vertices++, p1.x, p1.y);
2202 generatedVerticesCount += 2;
2203 }
Romain Guy7b631422012-04-04 11:38:54 -07002204
Chet Haase5b0200b2011-04-13 17:58:08 -07002205 Vertex::set(vertices++, p1.x, p1.y);
2206 Vertex::set(vertices++, p2.x, p2.y);
2207 Vertex::set(vertices++, p4.x, p4.y);
2208 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002209
Chet Haase5b0200b2011-04-13 17:58:08 -07002210 prevVertex = vertices - 1;
2211 generatedVerticesCount += 4;
2212 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002213 if (!isHairLine && scaled) {
2214 // Must set width proportions per-segment for scaled non-hairlines to use the
2215 // correct AA boundary dimensions
2216 if (boundaryWidthSlot < 0) {
2217 boundaryWidthSlot =
2218 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002219 }
Romain Guy7b631422012-04-04 11:38:54 -07002220
Chet Haase99ecdc42011-05-06 12:06:34 -07002221 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002222 }
Romain Guy7b631422012-04-04 11:38:54 -07002223
Chet Haase99585ad2011-05-02 15:00:16 -07002224 if (boundaryLengthSlot < 0) {
2225 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002226 }
Romain Guy7b631422012-04-04 11:38:54 -07002227
Chet Haase99ecdc42011-05-06 12:06:34 -07002228 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002229
Chet Haase5b0200b2011-04-13 17:58:08 -07002230 if (prevAAVertex != NULL) {
2231 // Issue two repeat vertices to create degenerate triangles to bridge
2232 // between the previous line and the new one. This is necessary because
2233 // we are creating a single triangle_strip which will contain
2234 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002235 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2236 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2237 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002238 generatedVerticesCount += 2;
2239 }
Romain Guy7b631422012-04-04 11:38:54 -07002240
Chet Haase99585ad2011-05-02 15:00:16 -07002241 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2242 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2243 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2244 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002245
Chet Haase5b0200b2011-04-13 17:58:08 -07002246 prevAAVertex = aaVertices - 1;
2247 generatedVerticesCount += 4;
2248 }
Romain Guy7b631422012-04-04 11:38:54 -07002249
Chet Haase99585ad2011-05-02 15:00:16 -07002250 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2251 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2252 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002253 }
2254 }
Romain Guy7b631422012-04-04 11:38:54 -07002255
Chet Haase5b0200b2011-04-13 17:58:08 -07002256 if (generatedVerticesCount > 0) {
2257 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2258 }
Romain Guy7b631422012-04-04 11:38:54 -07002259
2260 if (isAA) {
2261 finishDrawAALine(widthSlot, lengthSlot);
2262 }
Chet Haase48659092012-05-31 15:21:51 -07002263
2264 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002265}
2266
Chet Haase48659092012-05-31 15:21:51 -07002267status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2268 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002269
2270 // TODO: The paint's cap style defines whether the points are square or circular
2271 // TODO: Handle AA for round points
2272
Chet Haase5b0200b2011-04-13 17:58:08 -07002273 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002274 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002275 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002276 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002277 if (isHairLine) {
2278 // Now that we know it's hairline, we can set the effective width, to be used later
2279 strokeWidth = 1.0f;
2280 }
2281 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002282 int alpha;
2283 SkXfermode::Mode mode;
2284 getAlphaAndMode(paint, &alpha, &mode);
2285
2286 int verticesCount = count >> 1;
2287 int generatedVerticesCount = 0;
2288
2289 TextureVertex pointsData[verticesCount];
2290 TextureVertex* vertex = &pointsData[0];
2291
Romain Guy04299382012-07-18 17:15:41 -07002292 // TODO: We should optimize this method to not generate vertices for points
2293 // that lie outside of the clip.
2294 mCaches.enableScissor();
2295
Romain Guyed6fcb02011-03-21 13:11:28 -07002296 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002297 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002298 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002299 setupDrawColor(paint->getColor(), alpha);
2300 setupDrawColorFilter();
2301 setupDrawShader();
2302 setupDrawBlending(mode);
2303 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002304 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002305 setupDrawColorUniforms();
2306 setupDrawColorFilterUniforms();
2307 setupDrawPointUniforms();
2308 setupDrawShaderIdentityUniforms();
2309 setupDrawMesh(vertex);
2310
2311 for (int i = 0; i < count; i += 2) {
2312 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2313 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002314
Chet Haase8a5cc922011-04-26 07:28:09 -07002315 float left = points[i] - halfWidth;
2316 float right = points[i] + halfWidth;
2317 float top = points[i + 1] - halfWidth;
2318 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002319
Chet Haase8a5cc922011-04-26 07:28:09 -07002320 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002321 }
2322
2323 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002324
2325 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002326}
2327
Chet Haase48659092012-05-31 15:21:51 -07002328status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002329 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002330 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002331
Romain Guyae88e5e2010-10-22 17:49:18 -07002332 Rect& clip(*mSnapshot->clipRect);
2333 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002334
Romain Guy3d58c032010-07-14 16:34:53 -07002335 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002336
2337 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002338}
Romain Guy9d5316e2010-06-24 19:30:36 -07002339
Chet Haase48659092012-05-31 15:21:51 -07002340status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2341 SkPaint* paint) {
2342 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002343 const AutoTexture autoCleanup(texture);
2344
2345 const float x = left + texture->left - texture->offset;
2346 const float y = top + texture->top - texture->offset;
2347
2348 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002349
2350 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002351}
2352
Chet Haase48659092012-05-31 15:21:51 -07002353status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002354 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002355 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002356 return DrawGlInfo::kStatusDone;
2357 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002358
Chris Craikcb4d6002012-09-25 12:00:29 -07002359 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002360 mCaches.activeTexture(0);
2361 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2362 right - left, bottom - top, rx, ry, p);
2363 return drawShape(left, top, texture, p);
2364 }
2365
2366 SkPath path;
2367 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002368 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2369 float outset = p->getStrokeWidth() / 2;
2370 rect.outset(outset, outset);
2371 rx += outset;
2372 ry += outset;
2373 }
Chris Craik710f46d2012-09-17 17:25:49 -07002374 path.addRoundRect(rect, rx, ry);
Chris Craikcb4d6002012-09-25 12:00:29 -07002375 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002376
2377 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002378}
2379
Chris Craik710f46d2012-09-17 17:25:49 -07002380status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002381 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2382 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002383 return DrawGlInfo::kStatusDone;
2384 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002385 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002386 mCaches.activeTexture(0);
2387 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2388 return drawShape(x - radius, y - radius, texture, p);
2389 }
2390
2391 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002392 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2393 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2394 } else {
2395 path.addCircle(x, y, radius);
2396 }
2397 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002398
2399 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002400}
Romain Guy01d58e42011-01-19 21:54:02 -08002401
Chet Haase48659092012-05-31 15:21:51 -07002402status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002403 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002404 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002405 return DrawGlInfo::kStatusDone;
2406 }
Romain Guy01d58e42011-01-19 21:54:02 -08002407
Chris Craikcb4d6002012-09-25 12:00:29 -07002408 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002409 mCaches.activeTexture(0);
2410 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2411 return drawShape(left, top, texture, p);
2412 }
2413
2414 SkPath path;
2415 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002416 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2417 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2418 }
Chris Craik710f46d2012-09-17 17:25:49 -07002419 path.addOval(rect);
Chris Craikcb4d6002012-09-25 12:00:29 -07002420 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002421
2422 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002423}
2424
Chet Haase48659092012-05-31 15:21:51 -07002425status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002426 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
2427 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
2428 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002429 }
2430
Chris Craik780c1282012-10-04 14:10:49 -07002431 if (fabs(sweepAngle) >= 360.0f) {
2432 return drawOval(left, top, right, bottom, p);
2433 }
2434
2435 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik7fae5212012-11-01 11:27:03 -07002436 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || p->getStrokeCap() != SkPaint::kButt_Cap || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002437 mCaches.activeTexture(0);
2438 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2439 startAngle, sweepAngle, useCenter, p);
2440 return drawShape(left, top, texture, p);
2441 }
2442
2443 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2444 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2445 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2446 }
2447
2448 SkPath path;
2449 if (useCenter) {
2450 path.moveTo(rect.centerX(), rect.centerY());
2451 }
2452 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2453 if (useCenter) {
2454 path.close();
2455 }
2456 drawConvexPath(path, p);
2457
2458 return DrawGlInfo::kStatusDrew;
Romain Guy8b2f5262011-01-23 16:15:02 -08002459}
2460
Romain Guycf8675e2012-10-02 12:32:25 -07002461// See SkPaintDefaults.h
2462#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2463
Chet Haase48659092012-05-31 15:21:51 -07002464status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002465 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002466 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002467 }
2468
Chris Craik710f46d2012-09-17 17:25:49 -07002469 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002470 // only fill style is supported by drawConvexPath, since others have to handle joins
2471 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2472 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2473 mCaches.activeTexture(0);
2474 const PathTexture* texture =
2475 mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2476 return drawShape(left, top, texture, p);
2477 }
2478
2479 SkPath path;
2480 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2481 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2482 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2483 }
2484 path.addRect(rect);
2485 drawConvexPath(path, p);
2486
2487 return DrawGlInfo::kStatusDrew;
Romain Guy026c5e162010-06-28 17:12:22 -07002488 }
2489
Romain Guy181d0a62011-06-09 18:52:38 -07002490 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002491 SkPath path;
2492 path.addRect(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002493 drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002494 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002495 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chet Haase858aa932011-05-12 09:06:00 -07002496 }
Chet Haase48659092012-05-31 15:21:51 -07002497
2498 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002499}
Romain Guy9d5316e2010-06-24 19:30:36 -07002500
Raph Levien416a8472012-07-19 22:48:17 -07002501void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2502 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2503 float x, float y) {
2504 mCaches.activeTexture(0);
2505
2506 // NOTE: The drop shadow will not perform gamma correction
2507 // if shader-based correction is enabled
2508 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2509 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2510 paint, text, bytesCount, count, mShadowRadius, positions);
2511 const AutoTexture autoCleanup(shadow);
2512
2513 const float sx = x - shadow->left + mShadowDx;
2514 const float sy = y - shadow->top + mShadowDy;
2515
2516 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2517 int shadowColor = mShadowColor;
2518 if (mShader) {
2519 shadowColor = 0xffffffff;
2520 }
2521
2522 setupDraw();
2523 setupDrawWithTexture(true);
2524 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2525 setupDrawColorFilter();
2526 setupDrawShader();
2527 setupDrawBlending(true, mode);
2528 setupDrawProgram();
2529 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2530 setupDrawTexture(shadow->id);
2531 setupDrawPureColorUniforms();
2532 setupDrawColorFilterUniforms();
2533 setupDrawShaderUniforms();
2534 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2535
2536 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2537}
2538
Chet Haase48659092012-05-31 15:21:51 -07002539status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002540 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002541 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002542 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002543 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002544 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002545
Romain Guy671d6cf2012-01-18 12:39:17 -08002546 // NOTE: Skia does not support perspective transform on drawPosText yet
2547 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002548 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002549 }
2550
2551 float x = 0.0f;
2552 float y = 0.0f;
2553 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2554 if (pureTranslate) {
2555 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2556 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2557 }
2558
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002559 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002560 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2561 paint->getTextSize());
2562
2563 int alpha;
2564 SkXfermode::Mode mode;
2565 getAlphaAndMode(paint, &alpha, &mode);
2566
Raph Levien416a8472012-07-19 22:48:17 -07002567 if (CC_UNLIKELY(mHasShadow)) {
2568 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2569 0.0f, 0.0f);
2570 }
2571
Romain Guy671d6cf2012-01-18 12:39:17 -08002572 // Pick the appropriate texture filtering
2573 bool linearFilter = mSnapshot->transform->changesBounds();
2574 if (pureTranslate && !linearFilter) {
2575 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2576 }
2577
2578 mCaches.activeTexture(0);
2579 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002580 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002581 setupDrawDirtyRegionsDisabled();
2582 setupDrawWithTexture(true);
2583 setupDrawAlpha8Color(paint->getColor(), alpha);
2584 setupDrawColorFilter();
2585 setupDrawShader();
2586 setupDrawBlending(true, mode);
2587 setupDrawProgram();
2588 setupDrawModelView(x, y, x, y, pureTranslate, true);
2589 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2590 setupDrawPureColorUniforms();
2591 setupDrawColorFilterUniforms();
2592 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002593 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002594
2595 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2596 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2597
Romain Guy211370f2012-02-01 16:10:55 -08002598 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002599
2600 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2601 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002602 if (hasActiveLayer) {
2603 if (!pureTranslate) {
2604 mSnapshot->transform->mapRect(bounds);
2605 }
2606 dirtyLayerUnchecked(bounds, getRegion());
2607 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002608 }
Chet Haase48659092012-05-31 15:21:51 -07002609
2610 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002611}
2612
Romain Guyc2525952012-07-27 16:41:22 -07002613status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002614 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002615 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002616 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002617 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002618 }
2619
Chet Haasea1cff502012-02-21 13:43:44 -08002620 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002621 switch (paint->getTextAlign()) {
2622 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002623 x -= length / 2.0f;
2624 break;
2625 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002626 x -= length;
2627 break;
2628 default:
2629 break;
2630 }
2631
Romain Guycac5fd32011-12-01 20:08:50 -08002632 SkPaint::FontMetrics metrics;
2633 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002634 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002635 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002636 }
2637
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002638 const float oldX = x;
2639 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002640 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002641 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002642 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2643 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2644 }
2645
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002646#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002647 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002648 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002649#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002650
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002651 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002652 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002653 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002654
Romain Guy86568192010-12-14 15:55:39 -08002655 int alpha;
2656 SkXfermode::Mode mode;
2657 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002658
Romain Guy211370f2012-02-01 16:10:55 -08002659 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002660 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2661 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002662 }
2663
Romain Guy6620c6d2010-12-06 18:07:02 -08002664 // Pick the appropriate texture filtering
2665 bool linearFilter = mSnapshot->transform->changesBounds();
2666 if (pureTranslate && !linearFilter) {
2667 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2668 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002669
Romain Guy16c88082012-06-11 16:03:47 -07002670 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002671 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002672 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002673 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002674 setupDrawDirtyRegionsDisabled();
2675 setupDrawWithTexture(true);
2676 setupDrawAlpha8Color(paint->getColor(), alpha);
2677 setupDrawColorFilter();
2678 setupDrawShader();
2679 setupDrawBlending(true, mode);
2680 setupDrawProgram();
2681 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002682 // See comment above; the font renderer must use texture unit 0
2683 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002684 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2685 setupDrawPureColorUniforms();
2686 setupDrawColorFilterUniforms();
2687 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002688 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002689
Romain Guya3dc55f2012-09-28 13:55:44 -07002690 const Rect* clip = pureTranslate ? mSnapshot->clipRect :
2691 (mSnapshot->hasPerspectiveTransform() ? NULL : &mSnapshot->getLocalClip());
Romain Guy5b3b3522010-10-27 18:57:51 -07002692 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2693
Romain Guy211370f2012-02-01 16:10:55 -08002694 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002695
Raph Levien996e57c2012-07-23 15:22:52 -07002696 bool status;
Romain Guya3dc55f2012-09-28 13:55:44 -07002697 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002698 SkPaint paintCopy(*paint);
2699 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2700 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002701 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002702 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002703 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002704 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002705 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002706
2707 if (status && hasActiveLayer) {
2708 if (!pureTranslate) {
2709 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002710 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002711 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002712 }
Romain Guy694b5192010-07-21 21:33:20 -07002713
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002714 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002715
2716 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002717}
2718
Chet Haase48659092012-05-31 15:21:51 -07002719status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002720 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002721 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2722 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002723 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002724 }
2725
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002726 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002727 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2728 paint->getTextSize());
2729
2730 int alpha;
2731 SkXfermode::Mode mode;
2732 getAlphaAndMode(paint, &alpha, &mode);
2733
2734 mCaches.activeTexture(0);
2735 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002736 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002737 setupDrawDirtyRegionsDisabled();
2738 setupDrawWithTexture(true);
2739 setupDrawAlpha8Color(paint->getColor(), alpha);
2740 setupDrawColorFilter();
2741 setupDrawShader();
2742 setupDrawBlending(true, mode);
2743 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002744 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002745 setupDrawTexture(fontRenderer.getTexture(true));
2746 setupDrawPureColorUniforms();
2747 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002748 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002749 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002750
Romain Guy97771732012-02-28 18:17:02 -08002751 const Rect* clip = &mSnapshot->getLocalClip();
2752 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 -08002753
Romain Guy97771732012-02-28 18:17:02 -08002754 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002755
2756 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2757 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002758 if (hasActiveLayer) {
2759 mSnapshot->transform->mapRect(bounds);
2760 dirtyLayerUnchecked(bounds, getRegion());
2761 }
Romain Guy97771732012-02-28 18:17:02 -08002762 }
Chet Haase48659092012-05-31 15:21:51 -07002763
2764 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002765}
2766
Chet Haase48659092012-05-31 15:21:51 -07002767status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2768 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002769
Romain Guya1d3c912011-12-13 14:55:06 -08002770 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002771
Romain Guy33f6beb2012-02-16 19:24:51 -08002772 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002773 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002774 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002775 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002776
Romain Guy8b55f372010-08-18 17:10:07 -07002777 const float x = texture->left - texture->offset;
2778 const float y = texture->top - texture->offset;
2779
Romain Guy01d58e42011-01-19 21:54:02 -08002780 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002781
2782 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002783}
2784
Chet Haase48659092012-05-31 15:21:51 -07002785status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002786 if (!layer) {
2787 return DrawGlInfo::kStatusDone;
2788 }
2789
Romain Guyb2e2f242012-10-17 18:18:35 -07002790 mat4* transform = NULL;
2791 if (layer->isTextureLayer()) {
2792 transform = &layer->getTransform();
2793 if (!transform->isIdentity()) {
2794 save(0);
2795 mSnapshot->transform->multiply(*transform);
2796 }
2797 }
2798
Romain Guy35643dd2012-09-18 15:40:58 -07002799 Rect transformed;
2800 Rect clip;
2801 const bool rejected = quickRejectNoScissor(x, y,
2802 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2803
2804 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002805 if (transform && !transform->isIdentity()) {
2806 restore();
2807 }
Chet Haase48659092012-05-31 15:21:51 -07002808 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002809 }
2810
Romain Guy4ff0cf42012-08-06 14:51:10 -07002811 bool debugLayerUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -07002812 if (updateLayer(layer, true)) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002813 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002814 }
2815
Romain Guy87e2f7572012-09-24 11:37:12 -07002816 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002817 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002818
Romain Guy211370f2012-02-01 16:10:55 -08002819 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guye529ece2012-09-26 11:23:17 -07002820 SkiaColorFilter* oldFilter = mColorFilter;
2821 mColorFilter = layer->getColorFilter();
2822
Romain Guyc88e3572011-01-22 00:32:12 -08002823 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002824 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002825 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002826 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002827 setupDraw();
2828 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002829 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002830 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002831 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002832 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002833 setupDrawPureColorUniforms();
2834 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002835 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002836 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002837 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2838 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002839
Romain Guyd21b6e12011-11-30 20:21:23 -08002840 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002841 setupDrawModelViewTranslate(tx, ty,
2842 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002843 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002844 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002845 setupDrawModelViewTranslate(x, y,
2846 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2847 }
Romain Guyc88e3572011-01-22 00:32:12 -08002848 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002849
Romain Guyc88e3572011-01-22 00:32:12 -08002850 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2851 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002852
Romain Guyc88e3572011-01-22 00:32:12 -08002853 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002854
2855#if DEBUG_LAYERS_AS_REGIONS
2856 drawRegionRects(layer->region);
2857#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002858 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002859
Romain Guye529ece2012-09-26 11:23:17 -07002860 mColorFilter = oldFilter;
2861
Romain Guy4ff0cf42012-08-06 14:51:10 -07002862 if (debugLayerUpdate) {
2863 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2864 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2865 }
Romain Guyf219da52011-01-16 12:54:25 -08002866 }
Chet Haase48659092012-05-31 15:21:51 -07002867
Romain Guyb2e2f242012-10-17 18:18:35 -07002868 if (transform && !transform->isIdentity()) {
2869 restore();
2870 }
2871
Chet Haase48659092012-05-31 15:21:51 -07002872 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002873}
2874
Romain Guy6926c722010-07-12 20:20:03 -07002875///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002876// Shaders
2877///////////////////////////////////////////////////////////////////////////////
2878
2879void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002880 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002881}
2882
Romain Guy06f96e22010-07-30 19:18:16 -07002883void OpenGLRenderer::setupShader(SkiaShader* shader) {
2884 mShader = shader;
2885 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002886 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002887 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002888}
2889
Romain Guyd27977d2010-07-14 19:18:51 -07002890///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002891// Color filters
2892///////////////////////////////////////////////////////////////////////////////
2893
2894void OpenGLRenderer::resetColorFilter() {
2895 mColorFilter = NULL;
2896}
2897
2898void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2899 mColorFilter = filter;
2900}
2901
2902///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002903// Drop shadow
2904///////////////////////////////////////////////////////////////////////////////
2905
2906void OpenGLRenderer::resetShadow() {
2907 mHasShadow = false;
2908}
2909
2910void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2911 mHasShadow = true;
2912 mShadowRadius = radius;
2913 mShadowDx = dx;
2914 mShadowDy = dy;
2915 mShadowColor = color;
2916}
2917
2918///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002919// Draw filters
2920///////////////////////////////////////////////////////////////////////////////
2921
2922void OpenGLRenderer::resetPaintFilter() {
2923 mHasDrawFilter = false;
2924}
2925
2926void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2927 mHasDrawFilter = true;
2928 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2929 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2930}
2931
2932SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002933 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002934
2935 uint32_t flags = paint->getFlags();
2936
2937 mFilteredPaint = *paint;
2938 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2939
2940 return &mFilteredPaint;
2941}
2942
2943///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002944// Drawing implementation
2945///////////////////////////////////////////////////////////////////////////////
2946
Romain Guy01d58e42011-01-19 21:54:02 -08002947void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2948 float x, float y, SkPaint* paint) {
2949 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2950 return;
2951 }
2952
2953 int alpha;
2954 SkXfermode::Mode mode;
2955 getAlphaAndMode(paint, &alpha, &mode);
2956
2957 setupDraw();
2958 setupDrawWithTexture(true);
2959 setupDrawAlpha8Color(paint->getColor(), alpha);
2960 setupDrawColorFilter();
2961 setupDrawShader();
2962 setupDrawBlending(true, mode);
2963 setupDrawProgram();
2964 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2965 setupDrawTexture(texture->id);
2966 setupDrawPureColorUniforms();
2967 setupDrawColorFilterUniforms();
2968 setupDrawShaderUniforms();
2969 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2970
2971 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2972
2973 finishDrawTexture();
2974}
2975
Romain Guyf607bdc2010-09-10 19:20:06 -07002976// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002977#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2978#define kStdUnderline_Offset (1.0f / 9.0f)
2979#define kStdUnderline_Thickness (1.0f / 18.0f)
2980
2981void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2982 float x, float y, SkPaint* paint) {
2983 // Handle underline and strike-through
2984 uint32_t flags = paint->getFlags();
2985 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002986 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002987 float underlineWidth = length;
2988 // If length is > 0.0f, we already measured the text for the text alignment
2989 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002990 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002991 }
2992
Romain Guy211370f2012-02-01 16:10:55 -08002993 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002994 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002995 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002996
Raph Levien8b4072d2012-07-30 15:50:00 -07002997 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002998 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002999
Romain Guyf6834472011-01-23 13:32:12 -08003000 int linesCount = 0;
3001 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3002 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3003
3004 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003005 float points[pointsCount];
3006 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003007
3008 if (flags & SkPaint::kUnderlineText_Flag) {
3009 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003010 points[currentPoint++] = left;
3011 points[currentPoint++] = top;
3012 points[currentPoint++] = left + underlineWidth;
3013 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003014 }
3015
3016 if (flags & SkPaint::kStrikeThruText_Flag) {
3017 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003018 points[currentPoint++] = left;
3019 points[currentPoint++] = top;
3020 points[currentPoint++] = left + underlineWidth;
3021 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003022 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003023
Romain Guy726aeba2011-06-01 14:52:00 -07003024 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003025
Romain Guy726aeba2011-06-01 14:52:00 -07003026 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003027 }
3028 }
3029}
3030
Romain Guy026c5e162010-06-28 17:12:22 -07003031void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003032 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003033 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07003034 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003035 color |= 0x00ffffff;
3036 }
3037
Romain Guy70ca14e2010-12-13 18:24:33 -08003038 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003039 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003040 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003041 setupDrawShader();
3042 setupDrawColorFilter();
3043 setupDrawBlending(mode);
3044 setupDrawProgram();
3045 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3046 setupDrawColorUniforms();
3047 setupDrawShaderUniforms(ignoreTransform);
3048 setupDrawColorFilterUniforms();
3049 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003050
Romain Guyc95c8d62010-09-17 15:31:32 -07003051 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3052}
3053
Romain Guy82ba8142010-07-09 13:25:56 -07003054void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003055 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003056 int alpha;
3057 SkXfermode::Mode mode;
3058 getAlphaAndMode(paint, &alpha, &mode);
3059
Romain Guyd21b6e12011-11-30 20:21:23 -08003060 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003061
Romain Guy211370f2012-02-01 16:10:55 -08003062 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08003063 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
3064 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
3065
Romain Guyd21b6e12011-11-30 20:21:23 -08003066 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003067 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3068 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3069 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3070 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003071 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003072 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3073 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3074 GL_TRIANGLE_STRIP, gMeshCount);
3075 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003076}
3077
Romain Guybd6b79b2010-06-26 00:13:53 -07003078void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003079 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3080 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003081 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003082}
3083
3084void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003085 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003086 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003087 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003088
Romain Guy746b7402010-10-26 16:27:31 -07003089 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003090 setupDrawWithTexture();
3091 setupDrawColor(alpha, alpha, alpha, alpha);
3092 setupDrawColorFilter();
3093 setupDrawBlending(blend, mode, swapSrcDst);
3094 setupDrawProgram();
3095 if (!dirty) {
3096 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07003097 }
Romain Guy5b3b3522010-10-27 18:57:51 -07003098 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003099 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003100 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003101 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003102 }
Romain Guy86568192010-12-14 15:55:39 -08003103 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003104 setupDrawColorFilterUniforms();
3105 setupDrawTexture(texture);
3106 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003107
Romain Guy6820ac82010-09-15 18:11:50 -07003108 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003109
3110 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003111}
3112
Romain Guya5aed0d2010-09-09 14:42:43 -07003113void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003114 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003115 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003116
Romain Guy82ba8142010-07-09 13:25:56 -07003117 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003118 // These blend modes are not supported by OpenGL directly and have
3119 // to be implemented using shaders. Since the shader will perform
3120 // the blending, turn blending off here
3121 // If the blend mode cannot be implemented using shaders, fall
3122 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003123 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08003124 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003125 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003126 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003127
Romain Guy82bc7a72012-01-03 14:13:39 -08003128 if (mCaches.blend) {
3129 glDisable(GL_BLEND);
3130 mCaches.blend = false;
3131 }
3132
3133 return;
3134 } else {
3135 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003136 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003137 }
3138
3139 if (!mCaches.blend) {
3140 glEnable(GL_BLEND);
3141 }
3142
3143 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3144 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3145
3146 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3147 glBlendFunc(sourceMode, destMode);
3148 mCaches.lastSrcMode = sourceMode;
3149 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003150 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003151 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003152 glDisable(GL_BLEND);
3153 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003154 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003155}
3156
Romain Guy889f8d12010-07-29 14:37:42 -07003157bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003158 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003159 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003160 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003161 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003162 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003163 }
Romain Guy6926c722010-07-12 20:20:03 -07003164 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003165}
3166
Romain Guy026c5e162010-06-28 17:12:22 -07003167void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003168 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003169 TextureVertex::setUV(v++, u1, v1);
3170 TextureVertex::setUV(v++, u2, v1);
3171 TextureVertex::setUV(v++, u1, v2);
3172 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003173}
3174
Chet Haase5c13d892010-10-08 08:37:55 -07003175void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003176 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003177 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003178}
3179
Romain Guy9d5316e2010-06-24 19:30:36 -07003180}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003181}; // namespace android