blob: c7a2014d1cf901b5313a03130e01bf0c86a4c0f5 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy03d58522012-02-24 17:54:07 -080024#include <SkPathMeasure.h>
Romain Guy694b5192010-07-21 21:33:20 -070025#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070028#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Romain Guy08aa2cb2011-03-17 11:06:57 -070030#include <private/hwui/DrawGlInfo.h>
31
Romain Guy5b3b3522010-10-27 18:57:51 -070032#include <ui/Rect.h>
33
Romain Guy85bf02f2010-06-22 13:11:24 -070034#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Chris Craik710f46d2012-09-17 17:25:49 -070036#include "PathRenderer.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070037#include "Properties.h"
Romain Guya957eea2010-12-08 18:34:42 -080038#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070039
40namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070041namespace uirenderer {
42
43///////////////////////////////////////////////////////////////////////////////
44// Defines
45///////////////////////////////////////////////////////////////////////////////
46
Romain Guy759ea802010-09-16 20:49:46 -070047#define RAD_TO_DEG (180.0f / 3.14159265f)
48#define MIN_ANGLE 0.001f
49
Romain Guyf8773082012-07-12 18:01:00 -070050#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070051
Romain Guy713e1bb2012-10-16 18:44:09 -070052#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080053
Romain Guy9d5316e2010-06-24 19:30:36 -070054///////////////////////////////////////////////////////////////////////////////
55// Globals
56///////////////////////////////////////////////////////////////////////////////
57
Romain Guy889f8d12010-07-29 14:37:42 -070058/**
59 * Structure mapping Skia xfermodes to OpenGL blending factors.
60 */
61struct Blender {
62 SkXfermode::Mode mode;
63 GLenum src;
64 GLenum dst;
65}; // struct Blender
66
Romain Guy026c5e162010-06-28 17:12:22 -070067// In this array, the index of each Blender equals the value of the first
68// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
69static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070070 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
71 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
72 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
73 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
74 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
75 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
77 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
78 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
81 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
82 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
83 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
84 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070085};
Romain Guye4d01122010-06-16 18:44:05 -070086
Romain Guy87a76572010-09-13 18:11:21 -070087// This array contains the swapped version of each SkXfermode. For instance
88// this array's SrcOver blending mode is actually DstOver. You can refer to
89// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070090static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070091 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
92 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
93 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
94 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
95 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
96 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
97 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
100 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
101 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
104 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
105 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700106};
107
Romain Guyf6a11b82010-06-23 17:47:49 -0700108///////////////////////////////////////////////////////////////////////////////
109// Constructors/destructor
110///////////////////////////////////////////////////////////////////////////////
111
Romain Guyfb8b7632010-08-23 21:05:08 -0700112OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700113 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700114 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700115 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800116 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700117
Romain Guyac670c02010-07-27 17:39:27 -0700118 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
119
Romain Guyae5575b2010-07-29 18:48:04 -0700120 mFirstSnapshot = new Snapshot;
Romain Guy87e2f7572012-09-24 11:37:12 -0700121
122 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700123}
124
Romain Guy85bf02f2010-06-22 13:11:24 -0700125OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700126 // The context has already been destroyed at this point, do not call
127 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700128}
129
Romain Guy87e2f7572012-09-24 11:37:12 -0700130void OpenGLRenderer::initProperties() {
131 char property[PROPERTY_VALUE_MAX];
132 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
133 mScissorOptimizationDisabled = !strcasecmp(property, "true");
134 INIT_LOGD(" Scissor optimization %s",
135 mScissorOptimizationDisabled ? "disabled" : "enabled");
136 } else {
137 INIT_LOGD(" Scissor optimization enabled");
138 }
Romain Guy13631f32012-01-30 17:41:55 -0800139}
140
141///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700142// Setup
143///////////////////////////////////////////////////////////////////////////////
144
Romain Guy49c5fc02012-05-15 11:10:01 -0700145bool OpenGLRenderer::isDeferred() {
146 return false;
147}
148
Romain Guy85bf02f2010-06-22 13:11:24 -0700149void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700150 initViewport(width, height);
151
152 glDisable(GL_DITHER);
153 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
154
155 glEnableVertexAttribArray(Program::kBindingPosition);
156}
157
158void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700159 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700160
161 mWidth = width;
162 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700163
164 mFirstSnapshot->height = height;
165 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700166}
167
Romain Guy7c25aab2012-10-18 15:05:02 -0700168status_t OpenGLRenderer::prepare(bool opaque) {
Chet Haase44b2fe32012-06-06 19:03:58 -0700169 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800170}
171
Romain Guy7c25aab2012-10-18 15:05:02 -0700172status_t OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom,
173 bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800174 mCaches.clearGarbage();
175
Romain Guy8aef54f2010-09-01 15:13:49 -0700176 mSnapshot = new Snapshot(mFirstSnapshot,
177 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800178 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700179 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700180
Romain Guy7d7b5492011-01-24 16:33:45 -0800181 mSnapshot->setClip(left, top, right, bottom);
Romain Guy41308e22012-10-22 20:02:43 -0700182 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700183
Romain Guy11cb6422012-09-21 00:39:43 -0700184 updateLayers();
185
Romain Guy45e4c3d2012-09-11 17:17:07 -0700186 // If we know that we are going to redraw the entire framebuffer,
187 // perform a discard to let the driver know we don't need to preserve
188 // the back buffer for this frame.
189 if (mCaches.extensions.hasDiscardFramebuffer() &&
190 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
191 const GLenum attachments[] = { getTargetFbo() == 0 ? GL_COLOR_EXT : GL_COLOR_ATTACHMENT0 };
192 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
193 }
194
Romain Guyddf74372012-05-22 14:07:07 -0700195 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800196
Romain Guy54c1a642012-09-27 17:55:46 -0700197 // Functors break the tiling extension in pretty spectacular ways
198 // This ensures we don't use tiling when a functor is going to be
199 // invoked during the frame
200 mSuppressTiling = mCaches.hasRegisteredFunctors();
201
Romain Guy2b7028e2012-09-19 17:25:38 -0700202 mTilingSnapshot = mSnapshot;
Romain Guy57b52682012-09-20 17:38:46 -0700203 startTiling(mTilingSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700204
Romain Guy7c450aa2012-09-21 19:15:00 -0700205 debugOverdraw(true, true);
206
Romain Guy7c25aab2012-10-18 15:05:02 -0700207 return clear(left, top, right, bottom, opaque);
208}
209
210status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700211 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700212 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700213 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700214 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700215 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700216 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700217
Romain Guy7c25aab2012-10-18 15:05:02 -0700218 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700219 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700220}
221
222void OpenGLRenderer::syncState() {
223 glViewport(0, 0, mWidth, mHeight);
224
225 if (mCaches.blend) {
226 glEnable(GL_BLEND);
227 } else {
228 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700229 }
Romain Guybb9524b2010-06-22 18:56:38 -0700230}
231
Romain Guy57b52682012-09-20 17:38:46 -0700232void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700233 if (!mSuppressTiling) {
234 Rect* clip = mTilingSnapshot->clipRect;
235 if (s->flags & Snapshot::kFlagIsFboLayer) {
236 clip = s->clipRect;
237 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700238
Romain Guy54c1a642012-09-27 17:55:46 -0700239 mCaches.startTiling(clip->left, s->height - clip->bottom,
240 clip->right - clip->left, clip->bottom - clip->top, opaque);
241 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700242}
243
244void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700245 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700246}
247
Romain Guyb025b9c2010-09-16 14:16:48 -0700248void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700249 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700250 endTiling();
251
Romain Guy11cb6422012-09-21 00:39:43 -0700252 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700253#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700254 GLenum status = GL_NO_ERROR;
255 while ((status = glGetError()) != GL_NO_ERROR) {
256 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
257 switch (status) {
258 case GL_INVALID_ENUM:
259 ALOGE(" GL_INVALID_ENUM");
260 break;
261 case GL_INVALID_VALUE:
262 ALOGE(" GL_INVALID_VALUE");
263 break;
264 case GL_INVALID_OPERATION:
265 ALOGE(" GL_INVALID_OPERATION");
266 break;
267 case GL_OUT_OF_MEMORY:
268 ALOGE(" Out of memory!");
269 break;
270 }
Romain Guya07105b2011-01-10 21:14:18 -0800271 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700272#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700273
Romain Guyc15008e2010-11-10 11:59:15 -0800274#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800275 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700276#else
277 if (mCaches.getDebugLevel() & kDebugMemory) {
278 mCaches.dumpMemoryUsage();
279 }
Romain Guyc15008e2010-11-10 11:59:15 -0800280#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700281 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700282}
283
Romain Guy6c319ca2011-01-11 14:29:25 -0800284void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700285 if (mCaches.currentProgram) {
286 if (mCaches.currentProgram->isInUse()) {
287 mCaches.currentProgram->remove();
288 mCaches.currentProgram = NULL;
289 }
290 }
Romain Guy50c0f092010-10-19 11:42:22 -0700291 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800292 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800293 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800294 mCaches.disbaleTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700295 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700296}
297
Romain Guy6c319ca2011-01-11 14:29:25 -0800298void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800299 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800300 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700301 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700302 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700303
Romain Guy3e263fa2011-12-12 16:47:48 -0800304 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
305
Chet Haase80250612012-08-15 13:46:54 -0700306 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700307 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800308 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700309 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700310
Romain Guya1d3c912011-12-13 14:55:06 -0800311 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700312
Romain Guy50c0f092010-10-19 11:42:22 -0700313 mCaches.blend = true;
314 glEnable(GL_BLEND);
315 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
316 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700317}
318
Romain Guy35643dd2012-09-18 15:40:58 -0700319void OpenGLRenderer::resumeAfterLayer() {
320 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
321 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
322 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700323 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700324
325 mCaches.resetScissor();
326 dirtyClip();
327}
328
Romain Guyba6be8a2012-04-23 18:22:09 -0700329void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700330 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700331}
332
333void OpenGLRenderer::attachFunctor(Functor* functor) {
334 mFunctors.add(functor);
335}
336
Romain Guy8f3b8e32012-03-27 16:33:45 -0700337status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
338 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700339 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700340
Romain Guyba6be8a2012-04-23 18:22:09 -0700341 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800342 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700343 SortedVector<Functor*> functors(mFunctors);
344 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700345
Romain Guyba6be8a2012-04-23 18:22:09 -0700346 DrawGlInfo info;
347 info.clipLeft = 0;
348 info.clipTop = 0;
349 info.clipRight = 0;
350 info.clipBottom = 0;
351 info.isLayer = false;
352 info.width = 0;
353 info.height = 0;
354 memset(info.transform, 0, sizeof(float) * 16);
355
356 for (size_t i = 0; i < count; i++) {
357 Functor* f = functors.itemAt(i);
358 result |= (*f)(DrawGlInfo::kModeProcess, &info);
359
Chris Craikc2c95432012-04-25 15:13:52 -0700360 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700361 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
362 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700363 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700364
Chris Craikc2c95432012-04-25 15:13:52 -0700365 if (result & DrawGlInfo::kStatusInvoke) {
366 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700367 }
368 }
Chris Craikd15321b2012-11-28 14:45:04 -0800369 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700370 }
371
372 return result;
373}
374
375status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800376 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700377 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700378
Romain Guy8a4ac612012-07-17 17:32:48 -0700379 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800380 if (mDirtyClip) {
381 setScissorFromClip();
382 }
Romain Guyd643bb52011-03-01 14:55:21 -0800383
Romain Guy80911b82011-03-16 15:30:12 -0700384 Rect clip(*mSnapshot->clipRect);
385 clip.snapToPixelBoundaries();
386
Romain Guyd643bb52011-03-01 14:55:21 -0800387 // Since we don't know what the functor will draw, let's dirty
388 // tne entire clip region
389 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800390 dirtyLayerUnchecked(clip, getRegion());
391 }
Romain Guyd643bb52011-03-01 14:55:21 -0800392
Romain Guy08aa2cb2011-03-17 11:06:57 -0700393 DrawGlInfo info;
394 info.clipLeft = clip.left;
395 info.clipTop = clip.top;
396 info.clipRight = clip.right;
397 info.clipBottom = clip.bottom;
398 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700399 info.width = getSnapshot()->viewport.getWidth();
400 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700401 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700402
Chet Haase48659092012-05-31 15:21:51 -0700403 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800404
Romain Guy8f3b8e32012-03-27 16:33:45 -0700405 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700406 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800407 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700408
Chris Craik65924a32012-04-05 17:52:11 -0700409 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700410 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700411 }
Romain Guycabfcc12011-03-07 18:06:46 -0800412 }
413
Chet Haasedaf98e92011-01-10 14:10:36 -0800414 resume();
Romain Guy65549432012-03-26 16:45:05 -0700415 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800416}
417
Romain Guyf6a11b82010-06-23 17:47:49 -0700418///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700419// Debug
420///////////////////////////////////////////////////////////////////////////////
421
422void OpenGLRenderer::startMark(const char* name) const {
423 mCaches.startMark(0, name);
424}
425
426void OpenGLRenderer::endMark() const {
427 mCaches.endMark();
428}
429
430void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
431 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
432 if (clear) {
433 mCaches.disableScissor();
434 mCaches.stencil.clear();
435 }
436 if (enable) {
437 mCaches.stencil.enableDebugWrite();
438 } else {
439 mCaches.stencil.disable();
440 }
441 }
442}
443
444void OpenGLRenderer::renderOverdraw() {
445 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
446 const Rect* clip = mTilingSnapshot->clipRect;
447
448 mCaches.enableScissor();
449 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
450 clip->right - clip->left, clip->bottom - clip->top);
451
452 mCaches.stencil.enableDebugTest(2);
453 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
454 mCaches.stencil.enableDebugTest(3);
455 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
456 mCaches.stencil.enableDebugTest(4);
457 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
458 mCaches.stencil.enableDebugTest(4, true);
459 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
460 mCaches.stencil.disable();
461 }
462}
463
464///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700465// Layers
466///////////////////////////////////////////////////////////////////////////////
467
468bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
469 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
470 OpenGLRenderer* renderer = layer->renderer;
471 Rect& dirty = layer->dirtyRect;
472
Romain Guy7c450aa2012-09-21 19:15:00 -0700473 if (inFrame) {
474 endTiling();
475 debugOverdraw(false, false);
476 }
Romain Guy11cb6422012-09-21 00:39:43 -0700477
478 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
479 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
480 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
481 renderer->finish();
482
483 if (inFrame) {
484 resumeAfterLayer();
485 startTiling(mSnapshot);
486 }
487
488 dirty.setEmpty();
489 layer->deferredUpdateScheduled = false;
490 layer->renderer = NULL;
491 layer->displayList = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -0800492 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700493
494 return true;
495 }
496
497 return false;
498}
499
500void OpenGLRenderer::updateLayers() {
501 int count = mLayerUpdates.size();
502 if (count > 0) {
503 startMark("Layer Updates");
504
505 // Note: it is very important to update the layers in reverse order
506 for (int i = count - 1; i >= 0; i--) {
507 Layer* layer = mLayerUpdates.itemAt(i);
508 updateLayer(layer, false);
509 mCaches.resourceCache.decrementRefcount(layer);
510 }
511 mLayerUpdates.clear();
512
513 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
514 endMark();
515 }
516}
517
518void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
519 if (layer) {
520 mLayerUpdates.push_back(layer);
521 mCaches.resourceCache.incrementRefcount(layer);
522 }
523}
524
525void OpenGLRenderer::clearLayerUpdates() {
526 size_t count = mLayerUpdates.size();
527 if (count > 0) {
528 mCaches.resourceCache.lock();
529 for (size_t i = 0; i < count; i++) {
530 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
531 }
532 mCaches.resourceCache.unlock();
533 mLayerUpdates.clear();
534 }
535}
536
537///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700538// State management
539///////////////////////////////////////////////////////////////////////////////
540
Romain Guybb9524b2010-06-22 18:56:38 -0700541int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700542 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700543}
544
545int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700546 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700547}
548
549void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700550 if (mSaveCount > 1) {
551 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700552 }
Romain Guybb9524b2010-06-22 18:56:38 -0700553}
554
555void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700556 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700557
Romain Guy8fb95422010-08-17 18:38:51 -0700558 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700559 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700560 }
Romain Guybb9524b2010-06-22 18:56:38 -0700561}
562
Romain Guy8aef54f2010-09-01 15:13:49 -0700563int OpenGLRenderer::saveSnapshot(int flags) {
564 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700565 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700566}
567
568bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700569 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700570 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700571 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700572
Romain Guybd6b79b2010-06-26 00:13:53 -0700573 sp<Snapshot> current = mSnapshot;
574 sp<Snapshot> previous = mSnapshot->previous;
575
Romain Guyeb993562010-10-05 18:14:38 -0700576 if (restoreOrtho) {
577 Rect& r = previous->viewport;
578 glViewport(r.left, r.top, r.right, r.bottom);
579 mOrthoMatrix.load(current->orthoMatrix);
580 }
581
Romain Guy8b55f372010-08-18 17:10:07 -0700582 mSaveCount--;
583 mSnapshot = previous;
584
Romain Guy2542d192010-08-18 11:47:12 -0700585 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700586 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700587 }
Romain Guy2542d192010-08-18 11:47:12 -0700588
Romain Guy5ec99242010-11-03 16:19:08 -0700589 if (restoreLayer) {
590 composeLayer(current, previous);
591 }
592
Romain Guy2542d192010-08-18 11:47:12 -0700593 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700594}
595
Romain Guyf6a11b82010-06-23 17:47:49 -0700596///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700597// Layers
598///////////////////////////////////////////////////////////////////////////////
599
600int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700601 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700602 const GLuint previousFbo = mSnapshot->fbo;
603 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700604
Romain Guyaf636eb2010-12-09 17:47:21 -0800605 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700606 int alpha = 255;
607 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700608
Romain Guye45362c2010-11-03 19:58:32 -0700609 if (p) {
610 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700611 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700612 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700613 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700614 }
Romain Guyd55a8612010-06-28 17:42:46 -0700615
Chet Haased48885a2012-08-28 17:43:28 -0700616 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700617 }
Romain Guyd55a8612010-06-28 17:42:46 -0700618
619 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700620}
621
622int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
623 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700624 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700625 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700626 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700627 SkPaint paint;
628 paint.setAlpha(alpha);
629 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700630 }
Romain Guyd55a8612010-06-28 17:42:46 -0700631}
Romain Guybd6b79b2010-06-26 00:13:53 -0700632
Romain Guy1c740bc2010-09-13 18:00:09 -0700633/**
634 * Layers are viewed by Skia are slightly different than layers in image editing
635 * programs (for instance.) When a layer is created, previously created layers
636 * and the frame buffer still receive every drawing command. For instance, if a
637 * layer is created and a shape intersecting the bounds of the layers and the
638 * framebuffer is draw, the shape will be drawn on both (unless the layer was
639 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
640 *
641 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
642 * texture. Unfortunately, this is inefficient as it requires every primitive to
643 * be drawn n + 1 times, where n is the number of active layers. In practice this
644 * means, for every primitive:
645 * - Switch active frame buffer
646 * - Change viewport, clip and projection matrix
647 * - Issue the drawing
648 *
649 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700650 * To avoid this, layers are implemented in a different way here, at least in the
651 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
652 * is set. When this flag is set we can redirect all drawing operations into a
653 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700654 *
655 * This implementation relies on the frame buffer being at least RGBA 8888. When
656 * a layer is created, only a texture is created, not an FBO. The content of the
657 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700658 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700659 * buffer and drawing continues as normal. This technique therefore treats the
660 * frame buffer as a scratch buffer for the layers.
661 *
662 * To compose the layers back onto the frame buffer, each layer texture
663 * (containing the original frame buffer data) is drawn as a simple quad over
664 * the frame buffer. The trick is that the quad is set as the composition
665 * destination in the blending equation, and the frame buffer becomes the source
666 * of the composition.
667 *
668 * Drawing layers with an alpha value requires an extra step before composition.
669 * An empty quad is drawn over the layer's region in the frame buffer. This quad
670 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
671 * quad is used to multiply the colors in the frame buffer. This is achieved by
672 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
673 * GL_ZERO, GL_SRC_ALPHA.
674 *
675 * Because glCopyTexImage2D() can be slow, an alternative implementation might
676 * be use to draw a single clipped layer. The implementation described above
677 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700678 *
679 * (1) The frame buffer is actually not cleared right away. To allow the GPU
680 * to potentially optimize series of calls to glCopyTexImage2D, the frame
681 * buffer is left untouched until the first drawing operation. Only when
682 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700683 */
Chet Haased48885a2012-08-28 17:43:28 -0700684bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
685 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700686 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700687 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700688
Romain Guyeb993562010-10-05 18:14:38 -0700689 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
690
Romain Guyf607bdc2010-09-10 19:20:06 -0700691 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700692 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700693 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700694 Rect untransformedBounds(bounds);
695 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700696
Chet Haased48885a2012-08-28 17:43:28 -0700697 // Layers only make sense if they are in the framebuffer's bounds
698 if (bounds.intersect(*mSnapshot->clipRect)) {
699 // We cannot work with sub-pixels in this case
700 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700701
Chet Haased48885a2012-08-28 17:43:28 -0700702 // When the layer is not an FBO, we may use glCopyTexImage so we
703 // need to make sure the layer does not extend outside the bounds
704 // of the framebuffer
705 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700706 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700707 } else if (fboLayer) {
708 clip.set(bounds);
709 mat4 inverse;
710 inverse.loadInverse(*mSnapshot->transform);
711 inverse.mapRect(clip);
712 clip.snapToPixelBoundaries();
713 if (clip.intersect(untransformedBounds)) {
714 clip.translate(-left, -top);
715 bounds.set(untransformedBounds);
716 } else {
717 clip.setEmpty();
718 }
Romain Guyad37cd32011-03-15 11:12:25 -0700719 }
Chet Haased48885a2012-08-28 17:43:28 -0700720 } else {
721 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700722 }
Romain Guybf434112010-09-16 14:40:17 -0700723
Romain Guy746b7402010-10-26 16:27:31 -0700724 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700725 bounds.getHeight() > mCaches.maxTextureSize ||
726 (fboLayer && clip.isEmpty())) {
727 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700728 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700729 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700730 }
731
732 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700733 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700734 return false;
735 }
Romain Guyf18fd992010-07-08 11:45:51 -0700736
Romain Guya1d3c912011-12-13 14:55:06 -0800737 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700738 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700739 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700740 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700741 }
742
Romain Guy9ace8f52011-07-07 20:50:11 -0700743 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700744 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700745 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
746 bounds.getWidth() / float(layer->getWidth()), 0.0f);
747 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700748 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700749 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700750
Romain Guy8fb95422010-08-17 18:38:51 -0700751 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700752 mSnapshot->flags |= Snapshot::kFlagIsLayer;
753 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700754
Romain Guyeb993562010-10-05 18:14:38 -0700755 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700756 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700757 } else {
758 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700759 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800760 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700761 if (layer->isEmpty()) {
762 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700763 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700764 layer->getWidth(), layer->getHeight(), 0);
765 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800766 } else {
767 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700768 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800769 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700770
Romain Guy54be1cd2011-06-13 19:04:27 -0700771 // Enqueue the buffer coordinates to clear the corresponding region later
772 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700773 }
Romain Guyeb993562010-10-05 18:14:38 -0700774 }
Romain Guyf86ef572010-07-01 11:05:42 -0700775
Romain Guyd55a8612010-06-28 17:42:46 -0700776 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700777}
778
Chet Haased48885a2012-08-28 17:43:28 -0700779bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700780 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700781
Chet Haased48885a2012-08-28 17:43:28 -0700782 mSnapshot->region = &mSnapshot->layer->region;
783 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700784
Chet Haased48885a2012-08-28 17:43:28 -0700785 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
786 mSnapshot->fbo = layer->getFbo();
787 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
788 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
789 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
790 mSnapshot->height = bounds.getHeight();
791 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
792 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700793
Romain Guy2b7028e2012-09-19 17:25:38 -0700794 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700795 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700796 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700797 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
798 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700799
800 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700801 if (layer->isEmpty()) {
802 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
803 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700804 }
805
806 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700807 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700808
Romain Guy2b7028e2012-09-19 17:25:38 -0700809 startTiling(mSnapshot);
Romain Guy5b3b3522010-10-27 18:57:51 -0700810
811 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700812 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800813 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700814 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700815 glClear(GL_COLOR_BUFFER_BIT);
816
817 dirtyClip();
818
819 // Change the ortho projection
820 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
821 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
822
823 return true;
824}
825
Romain Guy1c740bc2010-09-13 18:00:09 -0700826/**
827 * Read the documentation of createLayer() before doing anything in this method.
828 */
Romain Guy1d83e192010-08-17 11:37:00 -0700829void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
830 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000831 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700832 return;
833 }
834
Romain Guy5b3b3522010-10-27 18:57:51 -0700835 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700836
837 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700838 endTiling();
839
Romain Guye0aa84b2012-04-03 19:30:26 -0700840 // Detach the texture from the FBO
841 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guyeb993562010-10-05 18:14:38 -0700842 // Unbind current FBO and restore previous one
843 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700844 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700845
846 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700847 }
848
Romain Guy1d83e192010-08-17 11:37:00 -0700849 Layer* layer = current->layer;
850 const Rect& rect = layer->layer;
851
Romain Guy9ace8f52011-07-07 20:50:11 -0700852 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700853 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700854 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700855 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700856 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700857 }
858
Romain Guy03750a02010-10-18 14:06:08 -0700859 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700860
Romain Guya1d3c912011-12-13 14:55:06 -0800861 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700862
Romain Guy5b3b3522010-10-27 18:57:51 -0700863 // When the layer is stored in an FBO, we can save a bit of fillrate by
864 // drawing only the dirty region
865 if (fboLayer) {
866 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700867 if (layer->getColorFilter()) {
868 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800869 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700870 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700871 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800872 resetColorFilter();
873 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700874 } else if (!rect.isEmpty()) {
875 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
876 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700877 }
Romain Guy8b55f372010-08-18 17:10:07 -0700878
Romain Guyeb993562010-10-05 18:14:38 -0700879 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800880 // Note: No need to use glDiscardFramebufferEXT() since we never
881 // create/compose layers that are not on screen with this
882 // code path
883 // See LayerRenderer::destroyLayer(Layer*)
884
Romain Guyeb993562010-10-05 18:14:38 -0700885 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
886 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700887 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700888 }
889
Romain Guy746b7402010-10-26 16:27:31 -0700890 dirtyClip();
891
Romain Guyeb993562010-10-05 18:14:38 -0700892 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700893 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700894 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700895 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700896 }
897}
898
Romain Guyaa6c24c2011-04-28 18:40:04 -0700899void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700900 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700901
902 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700903 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700904 setupDrawWithTexture();
905 } else {
906 setupDrawWithExternalTexture();
907 }
908 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700909 setupDrawColor(alpha, alpha, alpha, alpha);
910 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700911 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700912 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700913 setupDrawPureColorUniforms();
914 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700915 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
916 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700917 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700918 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700919 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700920 if (mSnapshot->transform->isPureTranslate() &&
921 layer->getWidth() == (uint32_t) rect.getWidth() &&
922 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700923 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
924 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
925
Romain Guyd21b6e12011-11-30 20:21:23 -0800926 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700927 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
928 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800929 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700930 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
931 }
932 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700933 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
934
935 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
936
937 finishDrawTexture();
938}
939
Romain Guy5b3b3522010-10-27 18:57:51 -0700940void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700941 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700942 const Rect& texCoords = layer->texCoords;
943 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
944 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700945
Romain Guy9ace8f52011-07-07 20:50:11 -0700946 float x = rect.left;
947 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700948 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700949 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700950 layer->getHeight() == (uint32_t) rect.getHeight();
951
952 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700953 // When we're swapping, the layer is already in screen coordinates
954 if (!swap) {
955 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
956 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
957 }
958
Romain Guyd21b6e12011-11-30 20:21:23 -0800959 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700960 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800961 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700962 }
963
964 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
965 layer->getTexture(), layer->getAlpha() / 255.0f,
966 layer->getMode(), layer->isBlend(),
967 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
968 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700969
Romain Guyaa6c24c2011-04-28 18:40:04 -0700970 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
971 } else {
972 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
973 drawTextureLayer(layer, rect);
974 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
975 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700976}
977
978void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700979 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700980 layer->setRegionAsRect();
981
Romain Guy40667672011-03-18 14:34:03 -0700982 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700983
Romain Guy5b3b3522010-10-27 18:57:51 -0700984 layer->region.clear();
985 return;
986 }
987
Romain Guy8a3957d2011-09-07 17:55:15 -0700988 // TODO: See LayerRenderer.cpp::generateMesh() for important
989 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800990 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700991 size_t count;
992 const android::Rect* rects = layer->region.getArray(&count);
993
Romain Guy9ace8f52011-07-07 20:50:11 -0700994 const float alpha = layer->getAlpha() / 255.0f;
995 const float texX = 1.0f / float(layer->getWidth());
996 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800997 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700998
999 TextureVertex* mesh = mCaches.getRegionMesh();
1000 GLsizei numQuads = 0;
1001
Romain Guy7230a742011-01-10 22:26:16 -08001002 setupDraw();
1003 setupDrawWithTexture();
1004 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001005 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001006 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001007 setupDrawProgram();
1008 setupDrawDirtyRegionsDisabled();
1009 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001010 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001011 setupDrawTexture(layer->getTexture());
1012 if (mSnapshot->transform->isPureTranslate()) {
1013 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
1014 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
1015
Romain Guyd21b6e12011-11-30 20:21:23 -08001016 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001017 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1018 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001019 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001020 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1021 }
Romain Guy15bc6432011-12-13 13:11:32 -08001022 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001023
1024 for (size_t i = 0; i < count; i++) {
1025 const android::Rect* r = &rects[i];
1026
1027 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001028 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001029 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001030 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001031
1032 // TODO: Reject quads outside of the clip
1033 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1034 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1035 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1036 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1037
1038 numQuads++;
1039
1040 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1041 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1042 numQuads = 0;
1043 mesh = mCaches.getRegionMesh();
1044 }
1045 }
1046
1047 if (numQuads > 0) {
1048 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1049 }
1050
Romain Guy7230a742011-01-10 22:26:16 -08001051 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001052
1053#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001054 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001055#endif
1056
1057 layer->region.clear();
1058 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001059}
1060
Romain Guy3a3133d2011-02-01 22:59:58 -08001061void OpenGLRenderer::drawRegionRects(const Region& region) {
1062#if DEBUG_LAYERS_AS_REGIONS
1063 size_t count;
1064 const android::Rect* rects = region.getArray(&count);
1065
1066 uint32_t colors[] = {
1067 0x7fff0000, 0x7f00ff00,
1068 0x7f0000ff, 0x7fff00ff,
1069 };
1070
1071 int offset = 0;
1072 int32_t top = rects[0].top;
1073
1074 for (size_t i = 0; i < count; i++) {
1075 if (top != rects[i].top) {
1076 offset ^= 0x2;
1077 top = rects[i].top;
1078 }
1079
1080 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1081 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1082 SkXfermode::kSrcOver_Mode);
1083 }
1084#endif
1085}
1086
Romain Guy5b3b3522010-10-27 18:57:51 -07001087void OpenGLRenderer::dirtyLayer(const float left, const float top,
1088 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001089 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001090 Rect bounds(left, top, right, bottom);
1091 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001092 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001093 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001094}
1095
1096void OpenGLRenderer::dirtyLayer(const float left, const float top,
1097 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001098 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001099 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001100 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001101 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001102}
1103
1104void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001105 if (bounds.intersect(*mSnapshot->clipRect)) {
1106 bounds.snapToPixelBoundaries();
1107 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1108 if (!dirty.isEmpty()) {
1109 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001110 }
1111 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001112}
1113
Romain Guy54be1cd2011-06-13 19:04:27 -07001114void OpenGLRenderer::clearLayerRegions() {
1115 const size_t count = mLayers.size();
1116 if (count == 0) return;
1117
1118 if (!mSnapshot->isIgnored()) {
1119 // Doing several glScissor/glClear here can negatively impact
1120 // GPUs with a tiler architecture, instead we draw quads with
1121 // the Clear blending mode
1122
1123 // The list contains bounds that have already been clipped
1124 // against their initial clip rect, and the current clip
1125 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001126 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001127
1128 Vertex mesh[count * 6];
1129 Vertex* vertex = mesh;
1130
1131 for (uint32_t i = 0; i < count; i++) {
1132 Rect* bounds = mLayers.itemAt(i);
1133
1134 Vertex::set(vertex++, bounds->left, bounds->bottom);
1135 Vertex::set(vertex++, bounds->left, bounds->top);
1136 Vertex::set(vertex++, bounds->right, bounds->top);
1137 Vertex::set(vertex++, bounds->left, bounds->bottom);
1138 Vertex::set(vertex++, bounds->right, bounds->top);
1139 Vertex::set(vertex++, bounds->right, bounds->bottom);
1140
1141 delete bounds;
1142 }
1143
1144 setupDraw(false);
1145 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1146 setupDrawBlending(true, SkXfermode::kClear_Mode);
1147 setupDrawProgram();
1148 setupDrawPureColorUniforms();
1149 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001150 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001151
Romain Guy54be1cd2011-06-13 19:04:27 -07001152 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001153
1154 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001155 } else {
1156 for (uint32_t i = 0; i < count; i++) {
1157 delete mLayers.itemAt(i);
1158 }
1159 }
1160
1161 mLayers.clear();
1162}
1163
Romain Guybd6b79b2010-06-26 00:13:53 -07001164///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001165// Transforms
1166///////////////////////////////////////////////////////////////////////////////
1167
1168void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001169 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001170}
1171
1172void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001173 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001174}
1175
1176void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001177 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001178}
1179
Romain Guy807daf72011-01-18 11:19:19 -08001180void OpenGLRenderer::skew(float sx, float sy) {
1181 mSnapshot->transform->skew(sx, sy);
1182}
1183
Romain Guyf6a11b82010-06-23 17:47:49 -07001184void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001185 if (matrix) {
1186 mSnapshot->transform->load(*matrix);
1187 } else {
1188 mSnapshot->transform->loadIdentity();
1189 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001190}
1191
1192void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001193 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001194}
1195
1196void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001197 SkMatrix transform;
1198 mSnapshot->transform->copyTo(transform);
1199 transform.preConcat(*matrix);
1200 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001201}
1202
1203///////////////////////////////////////////////////////////////////////////////
1204// Clipping
1205///////////////////////////////////////////////////////////////////////////////
1206
Romain Guybb9524b2010-06-22 18:56:38 -07001207void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001208 Rect clip(*mSnapshot->clipRect);
1209 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001210
Romain Guy8a4ac612012-07-17 17:32:48 -07001211 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1212 clip.getWidth(), clip.getHeight())) {
1213 mDirtyClip = false;
1214 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001215}
1216
1217const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001218 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001219}
1220
Romain Guy8a4ac612012-07-17 17:32:48 -07001221bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1222 if (mSnapshot->isIgnored()) {
1223 return true;
1224 }
1225
1226 Rect r(left, top, right, bottom);
1227 mSnapshot->transform->mapRect(r);
1228 r.snapToPixelBoundaries();
1229
1230 Rect clipRect(*mSnapshot->clipRect);
1231 clipRect.snapToPixelBoundaries();
1232
1233 return !clipRect.intersects(r);
1234}
1235
Romain Guy35643dd2012-09-18 15:40:58 -07001236bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1237 Rect& transformed, Rect& clip) {
1238 if (mSnapshot->isIgnored()) {
1239 return true;
1240 }
1241
1242 transformed.set(left, top, right, bottom);
1243 mSnapshot->transform->mapRect(transformed);
1244 transformed.snapToPixelBoundaries();
1245
1246 clip.set(*mSnapshot->clipRect);
1247 clip.snapToPixelBoundaries();
1248
1249 return !clip.intersects(transformed);
1250}
1251
Chris Craikcb4d6002012-09-25 12:00:29 -07001252bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom, SkPaint* paint) {
1253 if (paint->getStyle() != SkPaint::kFill_Style) {
1254 float outset = paint->getStrokeWidth() * 0.5f;
1255 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1256 } else {
1257 return quickReject(left, top, right, bottom);
1258 }
1259}
1260
Romain Guyc7d53492010-06-25 13:41:57 -07001261bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001262 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001263 return true;
1264 }
1265
Romain Guy1d83e192010-08-17 11:37:00 -07001266 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001267 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001268 r.snapToPixelBoundaries();
1269
1270 Rect clipRect(*mSnapshot->clipRect);
1271 clipRect.snapToPixelBoundaries();
1272
Romain Guy586cae32012-07-13 15:28:31 -07001273 bool rejected = !clipRect.intersects(r);
1274 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001275 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001276 }
1277
1278 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001279}
1280
Romain Guy079ba2c2010-07-16 14:12:24 -07001281bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1282 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001283 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001284 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001285 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001286 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001287}
1288
Chet Haasea23eed82012-04-12 15:19:04 -07001289Rect* OpenGLRenderer::getClipRect() {
1290 return mSnapshot->clipRect;
1291}
1292
Romain Guyf6a11b82010-06-23 17:47:49 -07001293///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001294// Drawing commands
1295///////////////////////////////////////////////////////////////////////////////
1296
Romain Guy54be1cd2011-06-13 19:04:27 -07001297void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001298 // TODO: It would be best if we could do this before quickReject()
1299 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001300 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001301 if (mDirtyClip) {
1302 setScissorFromClip();
1303 }
1304 mDescription.reset();
1305 mSetShaderColor = false;
1306 mColorSet = false;
1307 mColorA = mColorR = mColorG = mColorB = 0.0f;
1308 mTextureUnit = 0;
1309 mTrackDirtyRegions = true;
1310}
1311
1312void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1313 mDescription.hasTexture = true;
1314 mDescription.hasAlpha8Texture = isAlpha8;
1315}
1316
Romain Guyaa6c24c2011-04-28 18:40:04 -07001317void OpenGLRenderer::setupDrawWithExternalTexture() {
1318 mDescription.hasExternalTexture = true;
1319}
1320
Romain Guy15bc6432011-12-13 13:11:32 -08001321void OpenGLRenderer::setupDrawNoTexture() {
1322 mCaches.disbaleTexCoordsVertexArray();
1323}
1324
Chris Craik710f46d2012-09-17 17:25:49 -07001325void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001326 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001327}
1328
Chris Craik710f46d2012-09-17 17:25:49 -07001329void OpenGLRenderer::setupDrawVertexShape() {
1330 mDescription.isVertexShape = true;
Chris Craik6ebdc112012-08-31 18:24:33 -07001331}
1332
Romain Guyed6fcb02011-03-21 13:11:28 -07001333void OpenGLRenderer::setupDrawPoint(float pointSize) {
1334 mDescription.isPoint = true;
1335 mDescription.pointSize = pointSize;
1336}
1337
Romain Guy70ca14e2010-12-13 18:24:33 -08001338void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001339 setupDrawColor(color, (color >> 24) & 0xFF);
1340}
1341
1342void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1343 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001344 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1345 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001346 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001347 mColorR = a * ((color >> 16) & 0xFF);
1348 mColorG = a * ((color >> 8) & 0xFF);
1349 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001350 mColorSet = true;
1351 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1352}
1353
Romain Guy86568192010-12-14 15:55:39 -08001354void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1355 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001356 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1357 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001358 const float a = mColorA / 255.0f;
1359 mColorR = a * ((color >> 16) & 0xFF);
1360 mColorG = a * ((color >> 8) & 0xFF);
1361 mColorB = a * ((color ) & 0xFF);
1362 mColorSet = true;
1363 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1364}
1365
Romain Guy41210632012-07-16 17:04:24 -07001366void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1367 mCaches.fontRenderer->describe(mDescription, paint);
1368}
1369
Romain Guy70ca14e2010-12-13 18:24:33 -08001370void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1371 mColorA = a;
1372 mColorR = r;
1373 mColorG = g;
1374 mColorB = b;
1375 mColorSet = true;
1376 mSetShaderColor = mDescription.setColor(r, g, b, a);
1377}
1378
1379void OpenGLRenderer::setupDrawShader() {
1380 if (mShader) {
1381 mShader->describe(mDescription, mCaches.extensions);
1382 }
1383}
1384
1385void OpenGLRenderer::setupDrawColorFilter() {
1386 if (mColorFilter) {
1387 mColorFilter->describe(mDescription, mCaches.extensions);
1388 }
1389}
1390
Romain Guyf09ef512011-05-27 11:43:46 -07001391void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1392 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1393 mColorA = 1.0f;
1394 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001395 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001396 }
1397}
1398
Romain Guy70ca14e2010-12-13 18:24:33 -08001399void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001400 // When the blending mode is kClear_Mode, we need to use a modulate color
1401 // argb=1,0,0,0
1402 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001403 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1404 mDescription, swapSrcDst);
1405}
1406
1407void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001408 // When the blending mode is kClear_Mode, we need to use a modulate color
1409 // argb=1,0,0,0
1410 accountForClear(mode);
Romain Guye83221c2012-09-24 16:01:35 -07001411 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()) ||
1412 (mColorFilter && mColorFilter->blend()), mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001413}
1414
1415void OpenGLRenderer::setupDrawProgram() {
1416 useProgram(mCaches.programCache.get(mDescription));
1417}
1418
1419void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1420 mTrackDirtyRegions = false;
1421}
1422
1423void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1424 bool ignoreTransform) {
1425 mModelView.loadTranslate(left, top, 0.0f);
1426 if (!ignoreTransform) {
1427 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1428 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1429 } else {
1430 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1431 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1432 }
1433}
1434
Chet Haase8a5cc922011-04-26 07:28:09 -07001435void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1436 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001437}
1438
Romain Guy70ca14e2010-12-13 18:24:33 -08001439void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1440 bool ignoreTransform, bool ignoreModelView) {
1441 if (!ignoreModelView) {
1442 mModelView.loadTranslate(left, top, 0.0f);
1443 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001444 } else {
1445 mModelView.loadIdentity();
1446 }
Romain Guy86568192010-12-14 15:55:39 -08001447 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1448 if (!ignoreTransform) {
1449 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1450 if (mTrackDirtyRegions && dirty) {
1451 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1452 }
1453 } else {
1454 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1455 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1456 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001457}
1458
Romain Guyed6fcb02011-03-21 13:11:28 -07001459void OpenGLRenderer::setupDrawPointUniforms() {
1460 int slot = mCaches.currentProgram->getUniform("pointSize");
1461 glUniform1f(slot, mDescription.pointSize);
1462}
1463
Romain Guy70ca14e2010-12-13 18:24:33 -08001464void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001465 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001466 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1467 }
1468}
1469
Romain Guy86568192010-12-14 15:55:39 -08001470void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001471 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001472 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001473 }
1474}
1475
Romain Guy70ca14e2010-12-13 18:24:33 -08001476void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1477 if (mShader) {
1478 if (ignoreTransform) {
1479 mModelView.loadInverse(*mSnapshot->transform);
1480 }
1481 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1482 }
1483}
1484
Romain Guy8d0d4782010-12-14 20:13:35 -08001485void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1486 if (mShader) {
1487 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1488 }
1489}
1490
Romain Guy70ca14e2010-12-13 18:24:33 -08001491void OpenGLRenderer::setupDrawColorFilterUniforms() {
1492 if (mColorFilter) {
1493 mColorFilter->setupProgram(mCaches.currentProgram);
1494 }
1495}
1496
Romain Guy41210632012-07-16 17:04:24 -07001497void OpenGLRenderer::setupDrawTextGammaUniforms() {
1498 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1499}
1500
Romain Guy70ca14e2010-12-13 18:24:33 -08001501void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001502 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001503 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001504 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001505}
1506
1507void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1508 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001509 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001510 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001511}
1512
Romain Guyaa6c24c2011-04-28 18:40:04 -07001513void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1514 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001515 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001516 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001517}
1518
Romain Guy8f0095c2011-05-02 17:24:22 -07001519void OpenGLRenderer::setupDrawTextureTransform() {
1520 mDescription.hasTextureTransform = true;
1521}
1522
1523void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001524 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1525 GL_FALSE, &transform.data[0]);
1526}
1527
Romain Guy70ca14e2010-12-13 18:24:33 -08001528void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001529 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001530 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001531 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001532 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001533 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001534 }
Romain Guyd71dd362011-12-12 19:03:35 -08001535
Chris Craikcb4d6002012-09-25 12:00:29 -07001536 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001537 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001538 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001539 }
1540
1541 mCaches.unbindIndicesBuffer();
1542}
1543
1544void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1545 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001546 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001547 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001548 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001549 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001550}
1551
Chet Haase5b0200b2011-04-13 17:58:08 -07001552void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001553 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001554 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001555 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001556}
1557
1558/**
1559 * 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 -07001560 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1561 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1562 * attributes (one per vertex) are values from zero to one that tells the fragment
1563 * shader where the fragment is in relation to the line width/length overall; these values are
1564 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1565 * region of the line.
1566 * Note that we only pass down the width values in this setup function. The length coordinates
1567 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001568 */
Chet Haase99585ad2011-05-02 15:00:16 -07001569void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001570 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001571 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001572 mCaches.bindPositionVertexPointer(force, vertices, gAAVertexStride);
Romain Guyf3a910b42011-12-12 20:35:21 -08001573 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001574 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001575
Romain Guy7b631422012-04-04 11:38:54 -07001576 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001577 glEnableVertexAttribArray(widthSlot);
1578 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001579
Romain Guy7b631422012-04-04 11:38:54 -07001580 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001581 glEnableVertexAttribArray(lengthSlot);
1582 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001583
Chet Haase5b0200b2011-04-13 17:58:08 -07001584 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001585 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001586}
1587
1588void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1589 glDisableVertexAttribArray(widthSlot);
1590 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001591}
1592
Romain Guy70ca14e2010-12-13 18:24:33 -08001593void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001594}
1595
1596///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001597// Drawing
1598///////////////////////////////////////////////////////////////////////////////
1599
Chet Haase1271e2c2012-04-20 09:54:27 -07001600status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001601 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001602
Romain Guy0fe478e2010-11-08 12:08:41 -08001603 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1604 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001605 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001606 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001607 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001608
Romain Guy65549432012-03-26 16:45:05 -07001609 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001610}
1611
Chet Haaseed30fd82011-04-22 16:18:45 -07001612void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1613 if (displayList) {
1614 displayList->output(*this, level);
1615 }
1616}
1617
Romain Guya168d732011-03-18 16:50:13 -07001618void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1619 int alpha;
1620 SkXfermode::Mode mode;
1621 getAlphaAndMode(paint, &alpha, &mode);
1622
Romain Guya168d732011-03-18 16:50:13 -07001623 float x = left;
1624 float y = top;
1625
Romain Guye3c26852011-07-25 16:36:01 -07001626 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001627 bool ignoreTransform = false;
1628 if (mSnapshot->transform->isPureTranslate()) {
1629 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1630 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1631 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001632 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001633 } else {
1634 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001635 }
1636
1637 setupDraw();
1638 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001639 if (paint) {
1640 setupDrawAlpha8Color(paint->getColor(), alpha);
1641 }
Romain Guya168d732011-03-18 16:50:13 -07001642 setupDrawColorFilter();
1643 setupDrawShader();
1644 setupDrawBlending(true, mode);
1645 setupDrawProgram();
1646 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001647
Romain Guya168d732011-03-18 16:50:13 -07001648 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001649 texture->setWrap(GL_CLAMP_TO_EDGE);
1650 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001651
Romain Guya168d732011-03-18 16:50:13 -07001652 setupDrawPureColorUniforms();
1653 setupDrawColorFilterUniforms();
1654 setupDrawShaderUniforms();
1655 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1656
1657 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1658
1659 finishDrawTexture();
1660}
1661
Chet Haase48659092012-05-31 15:21:51 -07001662status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001663 const float right = left + bitmap->width();
1664 const float bottom = top + bitmap->height();
1665
1666 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001667 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001668 }
1669
Romain Guya1d3c912011-12-13 14:55:06 -08001670 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001671 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001672 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001673 const AutoTexture autoCleanup(texture);
1674
Romain Guy211370f2012-02-01 16:10:55 -08001675 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001676 drawAlphaBitmap(texture, left, top, paint);
1677 } else {
1678 drawTextureRect(left, top, right, bottom, texture, paint);
1679 }
Chet Haase48659092012-05-31 15:21:51 -07001680
1681 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001682}
1683
Chet Haase48659092012-05-31 15:21:51 -07001684status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001685 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1686 const mat4 transform(*matrix);
1687 transform.mapRect(r);
1688
Romain Guy6926c722010-07-12 20:20:03 -07001689 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001690 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001691 }
1692
Romain Guya1d3c912011-12-13 14:55:06 -08001693 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001694 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001695 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001696 const AutoTexture autoCleanup(texture);
1697
Romain Guy5b3b3522010-10-27 18:57:51 -07001698 // This could be done in a cheaper way, all we need is pass the matrix
1699 // to the vertex shader. The save/restore is a bit overkill.
1700 save(SkCanvas::kMatrix_SaveFlag);
1701 concatMatrix(matrix);
1702 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1703 restore();
Chet Haase48659092012-05-31 15:21:51 -07001704
1705 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001706}
1707
Chet Haase48659092012-05-31 15:21:51 -07001708status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001709 const float right = left + bitmap->width();
1710 const float bottom = top + bitmap->height();
1711
1712 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001713 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001714 }
1715
1716 mCaches.activeTexture(0);
1717 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1718 const AutoTexture autoCleanup(texture);
1719
1720 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001721
1722 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001723}
1724
Chet Haase48659092012-05-31 15:21:51 -07001725status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001726 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001727 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001728 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001729 }
1730
Romain Guya92bb4d2012-10-16 11:08:44 -07001731 // TODO: We should compute the bounding box when recording the display list
Romain Guyb18d2d02011-02-10 15:52:54 -08001732 float left = FLT_MAX;
1733 float top = FLT_MAX;
1734 float right = FLT_MIN;
1735 float bottom = FLT_MIN;
1736
Romain Guya92bb4d2012-10-16 11:08:44 -07001737 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001738
Romain Guya566b7c2011-01-23 16:36:11 -08001739 // TODO: Support the colors array
1740 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001741 TextureVertex* vertex = mesh;
Romain Guya92bb4d2012-10-16 11:08:44 -07001742
Romain Guy5a7b4662011-01-20 19:09:30 -08001743 for (int32_t y = 0; y < meshHeight; y++) {
1744 for (int32_t x = 0; x < meshWidth; x++) {
1745 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1746
1747 float u1 = float(x) / meshWidth;
1748 float u2 = float(x + 1) / meshWidth;
1749 float v1 = float(y) / meshHeight;
1750 float v2 = float(y + 1) / meshHeight;
1751
1752 int ax = i + (meshWidth + 1) * 2;
1753 int ay = ax + 1;
1754 int bx = i;
1755 int by = bx + 1;
1756 int cx = i + 2;
1757 int cy = cx + 1;
1758 int dx = i + (meshWidth + 1) * 2 + 2;
1759 int dy = dx + 1;
1760
1761 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1762 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1763 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1764
1765 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1766 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1767 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001768
Romain Guya92bb4d2012-10-16 11:08:44 -07001769 // TODO: This could be optimized to avoid unnecessary ops
1770 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1771 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1772 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1773 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001774 }
1775 }
1776
Romain Guya92bb4d2012-10-16 11:08:44 -07001777 if (quickReject(left, top, right, bottom)) {
1778 return DrawGlInfo::kStatusDone;
1779 }
1780
1781 mCaches.activeTexture(0);
1782 Texture* texture = mCaches.textureCache.get(bitmap);
1783 if (!texture) return DrawGlInfo::kStatusDone;
1784 const AutoTexture autoCleanup(texture);
1785
1786 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1787 texture->setFilter(FILTER(paint), true);
1788
1789 int alpha;
1790 SkXfermode::Mode mode;
1791 getAlphaAndMode(paint, &alpha, &mode);
1792
1793 if (hasLayer()) {
Romain Guyb18d2d02011-02-10 15:52:54 -08001794 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1795 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001796
Romain Guy5a7b4662011-01-20 19:09:30 -08001797 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1798 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001799 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001800
1801 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001802}
1803
Chet Haase48659092012-05-31 15:21:51 -07001804status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001805 float srcLeft, float srcTop, float srcRight, float srcBottom,
1806 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001807 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001808 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001809 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001810 }
1811
Romain Guya1d3c912011-12-13 14:55:06 -08001812 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001813 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001814 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001815 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001816
Romain Guy8ba548f2010-06-30 19:21:21 -07001817 const float width = texture->width;
1818 const float height = texture->height;
1819
Romain Guy68169722011-08-22 17:33:33 -07001820 const float u1 = fmax(0.0f, srcLeft / width);
1821 const float v1 = fmax(0.0f, srcTop / height);
1822 const float u2 = fmin(1.0f, srcRight / width);
1823 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001824
Romain Guy03750a02010-10-18 14:06:08 -07001825 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001826 resetDrawTextureTexCoords(u1, v1, u2, v2);
1827
Romain Guy03750a02010-10-18 14:06:08 -07001828 int alpha;
1829 SkXfermode::Mode mode;
1830 getAlphaAndMode(paint, &alpha, &mode);
1831
Romain Guyd21b6e12011-11-30 20:21:23 -08001832 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1833
Romain Guy211370f2012-02-01 16:10:55 -08001834 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001835 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1836 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1837
Romain Guyb5014982011-07-28 15:39:12 -07001838 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001839 // Enable linear filtering if the source rectangle is scaled
1840 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001841 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001842 }
Romain Guyb5014982011-07-28 15:39:12 -07001843
Romain Guyd21b6e12011-11-30 20:21:23 -08001844 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001845 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1846 texture->id, alpha / 255.0f, mode, texture->blend,
1847 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1848 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1849 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001850 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001851 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1852 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1853 GL_TRIANGLE_STRIP, gMeshCount);
1854 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001855
1856 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001857
1858 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001859}
1860
Chet Haase48659092012-05-31 15:21:51 -07001861status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001862 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001863 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001864 int alpha;
1865 SkXfermode::Mode mode;
1866 getAlphaAndModeDirect(paint, &alpha, &mode);
1867
1868 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1869 left, top, right, bottom, alpha, mode);
1870}
1871
1872status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1873 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1874 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001875 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001876 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001877 }
1878
Romain Guybe6f9dc2012-07-16 12:41:17 -07001879 alpha *= mSnapshot->alpha;
1880
Romain Guya1d3c912011-12-13 14:55:06 -08001881 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001882 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001883 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001884 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001885 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1886 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001887
Romain Guy2728f962010-10-08 18:36:15 -07001888 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001889 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001890
Romain Guy211370f2012-02-01 16:10:55 -08001891 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001892 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001893 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001894 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001895 const float offsetX = left + mSnapshot->transform->getTranslateX();
1896 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001897 const size_t count = mesh->quads.size();
1898 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001899 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001900 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001901 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1902 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1903 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001904 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001905 dirtyLayer(left + bounds.left, top + bounds.top,
1906 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001907 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001908 }
1909 }
1910
Romain Guy211370f2012-02-01 16:10:55 -08001911 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001912 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1913 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1914
1915 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1916 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1917 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1918 true, !mesh->hasEmptyQuads);
1919 } else {
1920 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1921 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1922 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1923 true, !mesh->hasEmptyQuads);
1924 }
Romain Guy054dc182010-10-15 17:55:25 -07001925 }
Chet Haase48659092012-05-31 15:21:51 -07001926
1927 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001928}
1929
Chet Haase99ecdc42011-05-06 12:06:34 -07001930/**
Chris Craikcb4d6002012-09-25 12:00:29 -07001931 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
1932 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
1933 * screen space in all directions. However, instead of using a fragment shader to compute the
1934 * translucency of the color from its position, we simply use a varying parameter to define how far
1935 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
1936 *
1937 * Doesn't yet support joins, caps, or path effects.
Chet Haase858aa932011-05-12 09:06:00 -07001938 */
Chris Craikcb4d6002012-09-25 12:00:29 -07001939void OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
1940 int color = paint->getColor();
1941 SkPaint::Style style = paint->getStyle();
1942 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
1943 bool isAA = paint->isAntiAlias();
1944
Chris Craik710f46d2012-09-17 17:25:49 -07001945 VertexBuffer vertexBuffer;
1946 // TODO: try clipping large paths to viewport
Chris Craikcb4d6002012-09-25 12:00:29 -07001947 PathRenderer::convexPathVertices(path, paint, mSnapshot->transform, vertexBuffer);
Romain Guy04299382012-07-18 17:15:41 -07001948
Chris Craikbf09ffb2012-10-01 13:50:37 -07001949 if (!vertexBuffer.getSize()) {
1950 // no vertices to draw
1951 return;
1952 }
1953
Chris Craik710f46d2012-09-17 17:25:49 -07001954 setupDraw();
1955 setupDrawNoTexture();
1956 if (isAA) setupDrawAA();
1957 setupDrawVertexShape();
1958 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1959 setupDrawColorFilter();
1960 setupDrawShader();
1961 setupDrawBlending(isAA, mode);
1962 setupDrawProgram();
Chris Craikcb4d6002012-09-25 12:00:29 -07001963 setupDrawModelViewIdentity();
Chris Craik710f46d2012-09-17 17:25:49 -07001964 setupDrawColorUniforms();
1965 setupDrawColorFilterUniforms();
1966 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07001967
Chris Craik710f46d2012-09-17 17:25:49 -07001968 void* vertices = vertexBuffer.getBuffer();
1969 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001970 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07001971 mCaches.resetTexCoordsVertexPointer();
1972 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07001973
Chris Craik710f46d2012-09-17 17:25:49 -07001974 int alphaSlot = -1;
1975 if (isAA) {
1976 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
1977 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07001978
Chris Craik710f46d2012-09-17 17:25:49 -07001979 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07001980 glEnableVertexAttribArray(alphaSlot);
1981 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07001982 }
Romain Guy04299382012-07-18 17:15:41 -07001983
Chris Craikcb4d6002012-09-25 12:00:29 -07001984 SkRect bounds = PathRenderer::computePathBounds(path, paint);
Chris Craik710f46d2012-09-17 17:25:49 -07001985 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy04299382012-07-18 17:15:41 -07001986
Chris Craik710f46d2012-09-17 17:25:49 -07001987 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07001988
Chris Craik710f46d2012-09-17 17:25:49 -07001989 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07001990 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001991 }
Chet Haase858aa932011-05-12 09:06:00 -07001992}
1993
1994/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001995 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1996 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1997 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1998 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1999 * of the line. Hairlines are more involved because we need to account for transform scaling
2000 * to end up with a one-pixel-wide line in screen space..
2001 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
2002 * in combination with values that we calculate and pass down in this method. The basic approach
2003 * is that the quad we create contains both the core line area plus a bounding area in which
2004 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
2005 * proportion of the width and the length of a given segment is represented by the boundary
2006 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
2007 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
2008 * on the inside). This ends up giving the result we want, with pixels that are completely
2009 * 'inside' the line area being filled opaquely and the other pixels being filled according to
2010 * how far into the boundary region they are, which is determined by shader interpolation.
2011 */
Chet Haase48659092012-05-31 15:21:51 -07002012status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
2013 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002014
2015 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07002016 // We use half the stroke width here because we're going to position the quad
2017 // corner vertices half of the width away from the line endpoints
2018 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002019 // A stroke width of 0 has a special meaning in Skia:
2020 // it draws a line 1 px wide regardless of current transform
2021 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07002022
Chet Haase99ecdc42011-05-06 12:06:34 -07002023 float inverseScaleX = 1.0f;
2024 float inverseScaleY = 1.0f;
2025 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07002026
Chet Haase8a5cc922011-04-26 07:28:09 -07002027 int alpha;
2028 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07002029
Chet Haase8a5cc922011-04-26 07:28:09 -07002030 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002031 int verticesCount = count;
2032 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07002033 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07002034 verticesCount += (count - 4);
2035 }
2036
2037 if (isHairLine || isAA) {
2038 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
2039 // the line on the screen should always be one pixel wide regardless of scale. For
2040 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08002041 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07002042 Matrix4 *mat = mSnapshot->transform;
2043 float m00 = mat->data[Matrix4::kScaleX];
2044 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07002045 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07002046 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07002047
Romain Guy211370f2012-02-01 16:10:55 -08002048 float scaleX = sqrtf(m00 * m00 + m01 * m01);
2049 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07002050
Chet Haase99ecdc42011-05-06 12:06:34 -07002051 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
2052 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07002053
Chet Haase99ecdc42011-05-06 12:06:34 -07002054 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
2055 scaled = true;
2056 }
2057 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002058 }
Chet Haase8a5cc922011-04-26 07:28:09 -07002059
2060 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07002061
2062 mCaches.enableScissor();
2063
Chet Haase8a5cc922011-04-26 07:28:09 -07002064 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002065 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002066 if (isAA) {
Chris Craik710f46d2012-09-17 17:25:49 -07002067 setupDrawAA();
Chet Haase8a5cc922011-04-26 07:28:09 -07002068 }
2069 setupDrawColor(paint->getColor(), alpha);
2070 setupDrawColorFilter();
2071 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08002072 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07002073 setupDrawProgram();
Chris Craikb30cb102012-10-05 19:11:37 -07002074 setupDrawModelViewIdentity(true);
Chet Haase8a5cc922011-04-26 07:28:09 -07002075 setupDrawColorUniforms();
2076 setupDrawColorFilterUniforms();
2077 setupDrawShaderIdentityUniforms();
2078
2079 if (isHairLine) {
2080 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07002081 halfStrokeWidth = isAA? 1 : .5;
2082 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002083 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07002084 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07002085 }
Romain Guy7b631422012-04-04 11:38:54 -07002086
2087 int widthSlot;
2088 int lengthSlot;
2089
Chet Haase5b0200b2011-04-13 17:58:08 -07002090 Vertex lines[verticesCount];
2091 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002092
Chet Haase99585ad2011-05-02 15:00:16 -07002093 AAVertex wLines[verticesCount];
2094 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002095
Romain Guy211370f2012-02-01 16:10:55 -08002096 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002097 setupDrawVertices(vertices);
2098 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002099 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2100 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002101 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002102 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2103 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002104 // We will need to calculate the actual width proportion on each segment for
2105 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002106 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002107 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2108 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002109 }
2110
Chet Haase99ecdc42011-05-06 12:06:34 -07002111 AAVertex* prevAAVertex = NULL;
2112 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002113
Chet Haase99585ad2011-05-02 15:00:16 -07002114 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002115 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002116
Chet Haase5b0200b2011-04-13 17:58:08 -07002117 for (int i = 0; i < count; i += 4) {
2118 // a = start point, b = end point
2119 vec2 a(points[i], points[i + 1]);
2120 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002121
Chet Haase99585ad2011-05-02 15:00:16 -07002122 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002123 float boundaryLengthProportion = 0;
2124 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002125
Chet Haase5b0200b2011-04-13 17:58:08 -07002126 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002127 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002128 float x = n.x;
2129 n.x = -n.y;
2130 n.y = x;
2131
Chet Haase8a5cc922011-04-26 07:28:09 -07002132 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002133 if (isAA) {
2134 float wideningFactor;
2135 if (fabs(n.x) >= fabs(n.y)) {
2136 wideningFactor = fabs(1.0f / n.x);
2137 } else {
2138 wideningFactor = fabs(1.0f / n.y);
2139 }
2140 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002141 }
Romain Guy7b631422012-04-04 11:38:54 -07002142
Chet Haase99ecdc42011-05-06 12:06:34 -07002143 if (scaled) {
2144 n.x *= inverseScaleX;
2145 n.y *= inverseScaleY;
2146 }
2147 } else if (scaled) {
2148 // Extend n by .5 pixel on each side, post-transform
2149 vec2 extendedN = n.copyNormalized();
2150 extendedN /= 2;
2151 extendedN.x *= inverseScaleX;
2152 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002153
Chet Haase99ecdc42011-05-06 12:06:34 -07002154 float extendedNLength = extendedN.length();
2155 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002156 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002157 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002158 }
Romain Guy7b631422012-04-04 11:38:54 -07002159
Chet Haase99585ad2011-05-02 15:00:16 -07002160 // aa lines expand the endpoint vertices to encompass the AA boundary
2161 if (isAA) {
2162 vec2 abVector = (b - a);
2163 length = abVector.length();
2164 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002165
Chet Haase99ecdc42011-05-06 12:06:34 -07002166 if (scaled) {
2167 abVector.x *= inverseScaleX;
2168 abVector.y *= inverseScaleY;
2169 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002170 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002171 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002172 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002173 }
Romain Guy7b631422012-04-04 11:38:54 -07002174
Chet Haase99ecdc42011-05-06 12:06:34 -07002175 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002176 a -= abVector;
2177 b += abVector;
2178 }
2179
Chet Haase5b0200b2011-04-13 17:58:08 -07002180 // Four corners of the rectangle defining a thick line
2181 vec2 p1 = a - n;
2182 vec2 p2 = a + n;
2183 vec2 p3 = b + n;
2184 vec2 p4 = b - n;
2185
Chet Haase99585ad2011-05-02 15:00:16 -07002186
Chet Haase5b0200b2011-04-13 17:58:08 -07002187 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2188 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2189 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2190 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2191
Romain Guy04299382012-07-18 17:15:41 -07002192 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002193 if (!isAA) {
2194 if (prevVertex != NULL) {
2195 // Issue two repeat vertices to create degenerate triangles to bridge
2196 // between the previous line and the new one. This is necessary because
2197 // we are creating a single triangle_strip which will contain
2198 // potentially discontinuous line segments.
2199 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2200 Vertex::set(vertices++, p1.x, p1.y);
2201 generatedVerticesCount += 2;
2202 }
Romain Guy7b631422012-04-04 11:38:54 -07002203
Chet Haase5b0200b2011-04-13 17:58:08 -07002204 Vertex::set(vertices++, p1.x, p1.y);
2205 Vertex::set(vertices++, p2.x, p2.y);
2206 Vertex::set(vertices++, p4.x, p4.y);
2207 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002208
Chet Haase5b0200b2011-04-13 17:58:08 -07002209 prevVertex = vertices - 1;
2210 generatedVerticesCount += 4;
2211 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002212 if (!isHairLine && scaled) {
2213 // Must set width proportions per-segment for scaled non-hairlines to use the
2214 // correct AA boundary dimensions
2215 if (boundaryWidthSlot < 0) {
2216 boundaryWidthSlot =
2217 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002218 }
Romain Guy7b631422012-04-04 11:38:54 -07002219
Chet Haase99ecdc42011-05-06 12:06:34 -07002220 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002221 }
Romain Guy7b631422012-04-04 11:38:54 -07002222
Chet Haase99585ad2011-05-02 15:00:16 -07002223 if (boundaryLengthSlot < 0) {
2224 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002225 }
Romain Guy7b631422012-04-04 11:38:54 -07002226
Chet Haase99ecdc42011-05-06 12:06:34 -07002227 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002228
Chet Haase5b0200b2011-04-13 17:58:08 -07002229 if (prevAAVertex != NULL) {
2230 // Issue two repeat vertices to create degenerate triangles to bridge
2231 // between the previous line and the new one. This is necessary because
2232 // we are creating a single triangle_strip which will contain
2233 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002234 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2235 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2236 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002237 generatedVerticesCount += 2;
2238 }
Romain Guy7b631422012-04-04 11:38:54 -07002239
Chet Haase99585ad2011-05-02 15:00:16 -07002240 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2241 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2242 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2243 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002244
Chet Haase5b0200b2011-04-13 17:58:08 -07002245 prevAAVertex = aaVertices - 1;
2246 generatedVerticesCount += 4;
2247 }
Romain Guy7b631422012-04-04 11:38:54 -07002248
Chet Haase99585ad2011-05-02 15:00:16 -07002249 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2250 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2251 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002252 }
2253 }
Romain Guy7b631422012-04-04 11:38:54 -07002254
Chet Haase5b0200b2011-04-13 17:58:08 -07002255 if (generatedVerticesCount > 0) {
2256 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2257 }
Romain Guy7b631422012-04-04 11:38:54 -07002258
2259 if (isAA) {
2260 finishDrawAALine(widthSlot, lengthSlot);
2261 }
Chet Haase48659092012-05-31 15:21:51 -07002262
2263 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002264}
2265
Chet Haase48659092012-05-31 15:21:51 -07002266status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2267 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002268
2269 // TODO: The paint's cap style defines whether the points are square or circular
2270 // TODO: Handle AA for round points
2271
Chet Haase5b0200b2011-04-13 17:58:08 -07002272 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002273 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002274 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002275 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002276 if (isHairLine) {
2277 // Now that we know it's hairline, we can set the effective width, to be used later
2278 strokeWidth = 1.0f;
2279 }
2280 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002281 int alpha;
2282 SkXfermode::Mode mode;
2283 getAlphaAndMode(paint, &alpha, &mode);
2284
2285 int verticesCount = count >> 1;
2286 int generatedVerticesCount = 0;
2287
2288 TextureVertex pointsData[verticesCount];
2289 TextureVertex* vertex = &pointsData[0];
2290
Romain Guy04299382012-07-18 17:15:41 -07002291 // TODO: We should optimize this method to not generate vertices for points
2292 // that lie outside of the clip.
2293 mCaches.enableScissor();
2294
Romain Guyed6fcb02011-03-21 13:11:28 -07002295 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002296 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002297 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002298 setupDrawColor(paint->getColor(), alpha);
2299 setupDrawColorFilter();
2300 setupDrawShader();
2301 setupDrawBlending(mode);
2302 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002303 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002304 setupDrawColorUniforms();
2305 setupDrawColorFilterUniforms();
2306 setupDrawPointUniforms();
2307 setupDrawShaderIdentityUniforms();
2308 setupDrawMesh(vertex);
2309
2310 for (int i = 0; i < count; i += 2) {
2311 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2312 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002313
Chet Haase8a5cc922011-04-26 07:28:09 -07002314 float left = points[i] - halfWidth;
2315 float right = points[i] + halfWidth;
2316 float top = points[i + 1] - halfWidth;
2317 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002318
Chet Haase8a5cc922011-04-26 07:28:09 -07002319 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002320 }
2321
2322 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002323
2324 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002325}
2326
Chet Haase48659092012-05-31 15:21:51 -07002327status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002328 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002329 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002330
Romain Guyae88e5e2010-10-22 17:49:18 -07002331 Rect& clip(*mSnapshot->clipRect);
2332 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002333
Romain Guy3d58c032010-07-14 16:34:53 -07002334 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002335
2336 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002337}
Romain Guy9d5316e2010-06-24 19:30:36 -07002338
Chet Haase48659092012-05-31 15:21:51 -07002339status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2340 SkPaint* paint) {
2341 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002342 const AutoTexture autoCleanup(texture);
2343
2344 const float x = left + texture->left - texture->offset;
2345 const float y = top + texture->top - texture->offset;
2346
2347 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002348
2349 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002350}
2351
Chet Haase48659092012-05-31 15:21:51 -07002352status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002353 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002354 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002355 return DrawGlInfo::kStatusDone;
2356 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002357
Chris Craikcb4d6002012-09-25 12:00:29 -07002358 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002359 mCaches.activeTexture(0);
2360 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2361 right - left, bottom - top, rx, ry, p);
2362 return drawShape(left, top, texture, p);
2363 }
2364
2365 SkPath path;
2366 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002367 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2368 float outset = p->getStrokeWidth() / 2;
2369 rect.outset(outset, outset);
2370 rx += outset;
2371 ry += outset;
2372 }
Chris Craik710f46d2012-09-17 17:25:49 -07002373 path.addRoundRect(rect, rx, ry);
Chris Craikcb4d6002012-09-25 12:00:29 -07002374 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002375
2376 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002377}
2378
Chris Craik710f46d2012-09-17 17:25:49 -07002379status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002380 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2381 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002382 return DrawGlInfo::kStatusDone;
2383 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002384 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002385 mCaches.activeTexture(0);
2386 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2387 return drawShape(x - radius, y - radius, texture, p);
2388 }
2389
2390 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002391 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2392 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2393 } else {
2394 path.addCircle(x, y, radius);
2395 }
2396 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002397
2398 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002399}
Romain Guy01d58e42011-01-19 21:54:02 -08002400
Chet Haase48659092012-05-31 15:21:51 -07002401status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002402 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002403 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002404 return DrawGlInfo::kStatusDone;
2405 }
Romain Guy01d58e42011-01-19 21:54:02 -08002406
Chris Craikcb4d6002012-09-25 12:00:29 -07002407 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002408 mCaches.activeTexture(0);
2409 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2410 return drawShape(left, top, texture, p);
2411 }
2412
2413 SkPath path;
2414 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002415 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2416 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2417 }
Chris Craik710f46d2012-09-17 17:25:49 -07002418 path.addOval(rect);
Chris Craikcb4d6002012-09-25 12:00:29 -07002419 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002420
2421 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002422}
2423
Chet Haase48659092012-05-31 15:21:51 -07002424status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002425 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
2426 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
2427 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002428 }
2429
Chris Craik780c1282012-10-04 14:10:49 -07002430 if (fabs(sweepAngle) >= 360.0f) {
2431 return drawOval(left, top, right, bottom, p);
2432 }
2433
2434 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik7fae5212012-11-01 11:27:03 -07002435 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || p->getStrokeCap() != SkPaint::kButt_Cap || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002436 mCaches.activeTexture(0);
2437 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2438 startAngle, sweepAngle, useCenter, p);
2439 return drawShape(left, top, texture, p);
2440 }
2441
2442 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2443 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2444 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2445 }
2446
2447 SkPath path;
2448 if (useCenter) {
2449 path.moveTo(rect.centerX(), rect.centerY());
2450 }
2451 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2452 if (useCenter) {
2453 path.close();
2454 }
2455 drawConvexPath(path, p);
2456
2457 return DrawGlInfo::kStatusDrew;
Romain Guy8b2f5262011-01-23 16:15:02 -08002458}
2459
Romain Guycf8675e2012-10-02 12:32:25 -07002460// See SkPaintDefaults.h
2461#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2462
Chet Haase48659092012-05-31 15:21:51 -07002463status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002464 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002465 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002466 }
2467
Chris Craik710f46d2012-09-17 17:25:49 -07002468 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002469 // only fill style is supported by drawConvexPath, since others have to handle joins
2470 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2471 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2472 mCaches.activeTexture(0);
2473 const PathTexture* texture =
2474 mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2475 return drawShape(left, top, texture, p);
2476 }
2477
2478 SkPath path;
2479 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2480 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2481 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2482 }
2483 path.addRect(rect);
2484 drawConvexPath(path, p);
2485
2486 return DrawGlInfo::kStatusDrew;
Romain Guy026c5e162010-06-28 17:12:22 -07002487 }
2488
Romain Guy181d0a62011-06-09 18:52:38 -07002489 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002490 SkPath path;
2491 path.addRect(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002492 drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002493 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002494 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chet Haase858aa932011-05-12 09:06:00 -07002495 }
Chet Haase48659092012-05-31 15:21:51 -07002496
2497 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002498}
Romain Guy9d5316e2010-06-24 19:30:36 -07002499
Raph Levien416a8472012-07-19 22:48:17 -07002500void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2501 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2502 float x, float y) {
2503 mCaches.activeTexture(0);
2504
2505 // NOTE: The drop shadow will not perform gamma correction
2506 // if shader-based correction is enabled
2507 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2508 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2509 paint, text, bytesCount, count, mShadowRadius, positions);
2510 const AutoTexture autoCleanup(shadow);
2511
2512 const float sx = x - shadow->left + mShadowDx;
2513 const float sy = y - shadow->top + mShadowDy;
2514
2515 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2516 int shadowColor = mShadowColor;
2517 if (mShader) {
2518 shadowColor = 0xffffffff;
2519 }
2520
2521 setupDraw();
2522 setupDrawWithTexture(true);
2523 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2524 setupDrawColorFilter();
2525 setupDrawShader();
2526 setupDrawBlending(true, mode);
2527 setupDrawProgram();
2528 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2529 setupDrawTexture(shadow->id);
2530 setupDrawPureColorUniforms();
2531 setupDrawColorFilterUniforms();
2532 setupDrawShaderUniforms();
2533 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2534
2535 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2536}
2537
Chet Haase48659092012-05-31 15:21:51 -07002538status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002539 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002540 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002541 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002542 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002543 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002544
Romain Guy671d6cf2012-01-18 12:39:17 -08002545 // NOTE: Skia does not support perspective transform on drawPosText yet
2546 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002547 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002548 }
2549
2550 float x = 0.0f;
2551 float y = 0.0f;
2552 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2553 if (pureTranslate) {
2554 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2555 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2556 }
2557
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002558 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002559 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2560 paint->getTextSize());
2561
2562 int alpha;
2563 SkXfermode::Mode mode;
2564 getAlphaAndMode(paint, &alpha, &mode);
2565
Raph Levien416a8472012-07-19 22:48:17 -07002566 if (CC_UNLIKELY(mHasShadow)) {
2567 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2568 0.0f, 0.0f);
2569 }
2570
Romain Guy671d6cf2012-01-18 12:39:17 -08002571 // Pick the appropriate texture filtering
2572 bool linearFilter = mSnapshot->transform->changesBounds();
2573 if (pureTranslate && !linearFilter) {
2574 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2575 }
2576
2577 mCaches.activeTexture(0);
2578 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002579 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002580 setupDrawDirtyRegionsDisabled();
2581 setupDrawWithTexture(true);
2582 setupDrawAlpha8Color(paint->getColor(), alpha);
2583 setupDrawColorFilter();
2584 setupDrawShader();
2585 setupDrawBlending(true, mode);
2586 setupDrawProgram();
2587 setupDrawModelView(x, y, x, y, pureTranslate, true);
2588 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2589 setupDrawPureColorUniforms();
2590 setupDrawColorFilterUniforms();
2591 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002592 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002593
2594 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2595 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2596
Romain Guy211370f2012-02-01 16:10:55 -08002597 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002598
2599 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2600 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002601 if (hasActiveLayer) {
2602 if (!pureTranslate) {
2603 mSnapshot->transform->mapRect(bounds);
2604 }
2605 dirtyLayerUnchecked(bounds, getRegion());
2606 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002607 }
Chet Haase48659092012-05-31 15:21:51 -07002608
2609 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002610}
2611
Romain Guyc2525952012-07-27 16:41:22 -07002612status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002613 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002614 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002615 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002616 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002617 }
2618
Chet Haasea1cff502012-02-21 13:43:44 -08002619 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002620 switch (paint->getTextAlign()) {
2621 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002622 x -= length / 2.0f;
2623 break;
2624 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002625 x -= length;
2626 break;
2627 default:
2628 break;
2629 }
2630
Romain Guycac5fd32011-12-01 20:08:50 -08002631 SkPaint::FontMetrics metrics;
2632 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002633 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002634 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002635 }
2636
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002637 const float oldX = x;
2638 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002639 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002640 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002641 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2642 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2643 }
2644
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002645#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002646 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002647 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002648#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002649
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002650 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002651 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002652 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002653
Romain Guy86568192010-12-14 15:55:39 -08002654 int alpha;
2655 SkXfermode::Mode mode;
2656 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002657
Romain Guy211370f2012-02-01 16:10:55 -08002658 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002659 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2660 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002661 }
2662
Romain Guy6620c6d2010-12-06 18:07:02 -08002663 // Pick the appropriate texture filtering
2664 bool linearFilter = mSnapshot->transform->changesBounds();
2665 if (pureTranslate && !linearFilter) {
2666 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2667 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002668
Romain Guy16c88082012-06-11 16:03:47 -07002669 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002670 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002671 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002672 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002673 setupDrawDirtyRegionsDisabled();
2674 setupDrawWithTexture(true);
2675 setupDrawAlpha8Color(paint->getColor(), alpha);
2676 setupDrawColorFilter();
2677 setupDrawShader();
2678 setupDrawBlending(true, mode);
2679 setupDrawProgram();
2680 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002681 // See comment above; the font renderer must use texture unit 0
2682 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002683 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2684 setupDrawPureColorUniforms();
2685 setupDrawColorFilterUniforms();
2686 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002687 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002688
Romain Guya3dc55f2012-09-28 13:55:44 -07002689 const Rect* clip = pureTranslate ? mSnapshot->clipRect :
2690 (mSnapshot->hasPerspectiveTransform() ? NULL : &mSnapshot->getLocalClip());
Romain Guy5b3b3522010-10-27 18:57:51 -07002691 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2692
Romain Guy211370f2012-02-01 16:10:55 -08002693 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002694
Raph Levien996e57c2012-07-23 15:22:52 -07002695 bool status;
Romain Guya3dc55f2012-09-28 13:55:44 -07002696 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002697 SkPaint paintCopy(*paint);
2698 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2699 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002700 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002701 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002702 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002703 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002704 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002705
2706 if (status && hasActiveLayer) {
2707 if (!pureTranslate) {
2708 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002709 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002710 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002711 }
Romain Guy694b5192010-07-21 21:33:20 -07002712
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002713 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002714
2715 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002716}
2717
Chet Haase48659092012-05-31 15:21:51 -07002718status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002719 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002720 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2721 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002722 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002723 }
2724
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002725 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002726 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2727 paint->getTextSize());
2728
2729 int alpha;
2730 SkXfermode::Mode mode;
2731 getAlphaAndMode(paint, &alpha, &mode);
2732
2733 mCaches.activeTexture(0);
2734 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002735 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002736 setupDrawDirtyRegionsDisabled();
2737 setupDrawWithTexture(true);
2738 setupDrawAlpha8Color(paint->getColor(), alpha);
2739 setupDrawColorFilter();
2740 setupDrawShader();
2741 setupDrawBlending(true, mode);
2742 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002743 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002744 setupDrawTexture(fontRenderer.getTexture(true));
2745 setupDrawPureColorUniforms();
2746 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002747 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002748 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002749
Romain Guy97771732012-02-28 18:17:02 -08002750 const Rect* clip = &mSnapshot->getLocalClip();
2751 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 -08002752
Romain Guy97771732012-02-28 18:17:02 -08002753 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002754
2755 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2756 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002757 if (hasActiveLayer) {
2758 mSnapshot->transform->mapRect(bounds);
2759 dirtyLayerUnchecked(bounds, getRegion());
2760 }
Romain Guy97771732012-02-28 18:17:02 -08002761 }
Chet Haase48659092012-05-31 15:21:51 -07002762
2763 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002764}
2765
Chet Haase48659092012-05-31 15:21:51 -07002766status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2767 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002768
Romain Guya1d3c912011-12-13 14:55:06 -08002769 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002770
Romain Guy33f6beb2012-02-16 19:24:51 -08002771 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002772 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002773 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002774 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002775
Romain Guy8b55f372010-08-18 17:10:07 -07002776 const float x = texture->left - texture->offset;
2777 const float y = texture->top - texture->offset;
2778
Romain Guy01d58e42011-01-19 21:54:02 -08002779 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002780
2781 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002782}
2783
Chet Haase48659092012-05-31 15:21:51 -07002784status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002785 if (!layer) {
2786 return DrawGlInfo::kStatusDone;
2787 }
2788
Romain Guyb2e2f242012-10-17 18:18:35 -07002789 mat4* transform = NULL;
2790 if (layer->isTextureLayer()) {
2791 transform = &layer->getTransform();
2792 if (!transform->isIdentity()) {
2793 save(0);
2794 mSnapshot->transform->multiply(*transform);
2795 }
2796 }
2797
Romain Guy35643dd2012-09-18 15:40:58 -07002798 Rect transformed;
2799 Rect clip;
2800 const bool rejected = quickRejectNoScissor(x, y,
2801 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2802
2803 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002804 if (transform && !transform->isIdentity()) {
2805 restore();
2806 }
Chet Haase48659092012-05-31 15:21:51 -07002807 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002808 }
2809
Romain Guy5bb3c732012-11-29 17:52:58 -08002810 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002811
Romain Guy87e2f7572012-09-24 11:37:12 -07002812 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002813 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002814
Romain Guy211370f2012-02-01 16:10:55 -08002815 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guye529ece2012-09-26 11:23:17 -07002816 SkiaColorFilter* oldFilter = mColorFilter;
2817 mColorFilter = layer->getColorFilter();
2818
Romain Guyc88e3572011-01-22 00:32:12 -08002819 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002820 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002821 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002822 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002823 setupDraw();
2824 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002825 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002826 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002827 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002828 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002829 setupDrawPureColorUniforms();
2830 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002831 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002832 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002833 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2834 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002835
Romain Guyd21b6e12011-11-30 20:21:23 -08002836 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002837 setupDrawModelViewTranslate(tx, ty,
2838 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002839 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002840 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002841 setupDrawModelViewTranslate(x, y,
2842 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2843 }
Romain Guyc88e3572011-01-22 00:32:12 -08002844 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002845
Romain Guyc88e3572011-01-22 00:32:12 -08002846 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2847 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002848
Romain Guyc88e3572011-01-22 00:32:12 -08002849 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002850
2851#if DEBUG_LAYERS_AS_REGIONS
2852 drawRegionRects(layer->region);
2853#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002854 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002855
Romain Guye529ece2012-09-26 11:23:17 -07002856 mColorFilter = oldFilter;
2857
Romain Guy5bb3c732012-11-29 17:52:58 -08002858 if (layer->debugDrawUpdate) {
2859 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002860 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2861 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2862 }
Romain Guyf219da52011-01-16 12:54:25 -08002863 }
Chet Haase48659092012-05-31 15:21:51 -07002864
Romain Guyb2e2f242012-10-17 18:18:35 -07002865 if (transform && !transform->isIdentity()) {
2866 restore();
2867 }
2868
Chet Haase48659092012-05-31 15:21:51 -07002869 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002870}
2871
Romain Guy6926c722010-07-12 20:20:03 -07002872///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002873// Shaders
2874///////////////////////////////////////////////////////////////////////////////
2875
2876void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002877 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002878}
2879
Romain Guy06f96e22010-07-30 19:18:16 -07002880void OpenGLRenderer::setupShader(SkiaShader* shader) {
2881 mShader = shader;
2882 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002883 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002884 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002885}
2886
Romain Guyd27977d2010-07-14 19:18:51 -07002887///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002888// Color filters
2889///////////////////////////////////////////////////////////////////////////////
2890
2891void OpenGLRenderer::resetColorFilter() {
2892 mColorFilter = NULL;
2893}
2894
2895void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2896 mColorFilter = filter;
2897}
2898
2899///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002900// Drop shadow
2901///////////////////////////////////////////////////////////////////////////////
2902
2903void OpenGLRenderer::resetShadow() {
2904 mHasShadow = false;
2905}
2906
2907void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2908 mHasShadow = true;
2909 mShadowRadius = radius;
2910 mShadowDx = dx;
2911 mShadowDy = dy;
2912 mShadowColor = color;
2913}
2914
2915///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002916// Draw filters
2917///////////////////////////////////////////////////////////////////////////////
2918
2919void OpenGLRenderer::resetPaintFilter() {
2920 mHasDrawFilter = false;
2921}
2922
2923void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2924 mHasDrawFilter = true;
2925 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2926 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2927}
2928
2929SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002930 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002931
2932 uint32_t flags = paint->getFlags();
2933
2934 mFilteredPaint = *paint;
2935 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2936
2937 return &mFilteredPaint;
2938}
2939
2940///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002941// Drawing implementation
2942///////////////////////////////////////////////////////////////////////////////
2943
Romain Guy01d58e42011-01-19 21:54:02 -08002944void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2945 float x, float y, SkPaint* paint) {
2946 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2947 return;
2948 }
2949
2950 int alpha;
2951 SkXfermode::Mode mode;
2952 getAlphaAndMode(paint, &alpha, &mode);
2953
2954 setupDraw();
2955 setupDrawWithTexture(true);
2956 setupDrawAlpha8Color(paint->getColor(), alpha);
2957 setupDrawColorFilter();
2958 setupDrawShader();
2959 setupDrawBlending(true, mode);
2960 setupDrawProgram();
2961 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2962 setupDrawTexture(texture->id);
2963 setupDrawPureColorUniforms();
2964 setupDrawColorFilterUniforms();
2965 setupDrawShaderUniforms();
2966 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2967
2968 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2969
2970 finishDrawTexture();
2971}
2972
Romain Guyf607bdc2010-09-10 19:20:06 -07002973// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002974#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2975#define kStdUnderline_Offset (1.0f / 9.0f)
2976#define kStdUnderline_Thickness (1.0f / 18.0f)
2977
2978void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2979 float x, float y, SkPaint* paint) {
2980 // Handle underline and strike-through
2981 uint32_t flags = paint->getFlags();
2982 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002983 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002984 float underlineWidth = length;
2985 // If length is > 0.0f, we already measured the text for the text alignment
2986 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002987 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002988 }
2989
Romain Guy211370f2012-02-01 16:10:55 -08002990 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002991 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002992 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002993
Raph Levien8b4072d2012-07-30 15:50:00 -07002994 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002995 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002996
Romain Guyf6834472011-01-23 13:32:12 -08002997 int linesCount = 0;
2998 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2999 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3000
3001 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003002 float points[pointsCount];
3003 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003004
3005 if (flags & SkPaint::kUnderlineText_Flag) {
3006 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003007 points[currentPoint++] = left;
3008 points[currentPoint++] = top;
3009 points[currentPoint++] = left + underlineWidth;
3010 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003011 }
3012
3013 if (flags & SkPaint::kStrikeThruText_Flag) {
3014 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003015 points[currentPoint++] = left;
3016 points[currentPoint++] = top;
3017 points[currentPoint++] = left + underlineWidth;
3018 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003019 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003020
Romain Guy726aeba2011-06-01 14:52:00 -07003021 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003022
Romain Guy726aeba2011-06-01 14:52:00 -07003023 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003024 }
3025 }
3026}
3027
Romain Guy026c5e162010-06-28 17:12:22 -07003028void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003029 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003030 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07003031 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003032 color |= 0x00ffffff;
3033 }
3034
Romain Guy70ca14e2010-12-13 18:24:33 -08003035 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003036 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003037 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003038 setupDrawShader();
3039 setupDrawColorFilter();
3040 setupDrawBlending(mode);
3041 setupDrawProgram();
3042 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3043 setupDrawColorUniforms();
3044 setupDrawShaderUniforms(ignoreTransform);
3045 setupDrawColorFilterUniforms();
3046 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003047
Romain Guyc95c8d62010-09-17 15:31:32 -07003048 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3049}
3050
Romain Guy82ba8142010-07-09 13:25:56 -07003051void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003052 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003053 int alpha;
3054 SkXfermode::Mode mode;
3055 getAlphaAndMode(paint, &alpha, &mode);
3056
Romain Guyd21b6e12011-11-30 20:21:23 -08003057 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003058
Romain Guy211370f2012-02-01 16:10:55 -08003059 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08003060 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
3061 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
3062
Romain Guyd21b6e12011-11-30 20:21:23 -08003063 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003064 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3065 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3066 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3067 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003068 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003069 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3070 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3071 GL_TRIANGLE_STRIP, gMeshCount);
3072 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003073}
3074
Romain Guybd6b79b2010-06-26 00:13:53 -07003075void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003076 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3077 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003078 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003079}
3080
3081void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003082 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003083 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003084 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003085
Romain Guy746b7402010-10-26 16:27:31 -07003086 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003087 setupDrawWithTexture();
3088 setupDrawColor(alpha, alpha, alpha, alpha);
3089 setupDrawColorFilter();
3090 setupDrawBlending(blend, mode, swapSrcDst);
3091 setupDrawProgram();
3092 if (!dirty) {
3093 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07003094 }
Romain Guy5b3b3522010-10-27 18:57:51 -07003095 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003096 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003097 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003098 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003099 }
Romain Guy86568192010-12-14 15:55:39 -08003100 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003101 setupDrawColorFilterUniforms();
3102 setupDrawTexture(texture);
3103 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003104
Romain Guy6820ac82010-09-15 18:11:50 -07003105 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003106
3107 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003108}
3109
Romain Guya5aed0d2010-09-09 14:42:43 -07003110void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003111 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003112 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003113
Romain Guy82ba8142010-07-09 13:25:56 -07003114 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003115 // These blend modes are not supported by OpenGL directly and have
3116 // to be implemented using shaders. Since the shader will perform
3117 // the blending, turn blending off here
3118 // If the blend mode cannot be implemented using shaders, fall
3119 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003120 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08003121 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003122 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003123 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003124
Romain Guy82bc7a72012-01-03 14:13:39 -08003125 if (mCaches.blend) {
3126 glDisable(GL_BLEND);
3127 mCaches.blend = false;
3128 }
3129
3130 return;
3131 } else {
3132 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003133 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003134 }
3135
3136 if (!mCaches.blend) {
3137 glEnable(GL_BLEND);
3138 }
3139
3140 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3141 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3142
3143 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3144 glBlendFunc(sourceMode, destMode);
3145 mCaches.lastSrcMode = sourceMode;
3146 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003147 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003148 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003149 glDisable(GL_BLEND);
3150 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003151 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003152}
3153
Romain Guy889f8d12010-07-29 14:37:42 -07003154bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003155 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003156 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003157 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003158 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003159 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003160 }
Romain Guy6926c722010-07-12 20:20:03 -07003161 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003162}
3163
Romain Guy026c5e162010-06-28 17:12:22 -07003164void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003165 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003166 TextureVertex::setUV(v++, u1, v1);
3167 TextureVertex::setUV(v++, u2, v1);
3168 TextureVertex::setUV(v++, u1, v2);
3169 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003170}
3171
Chet Haase5c13d892010-10-08 08:37:55 -07003172void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003173 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003174 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003175}
3176
Romain Guy9d5316e2010-06-24 19:30:36 -07003177}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003178}; // namespace android