blob: e1a5132e202147fb19d7700fd9dc82ea274e605f [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 Guyd21b6e12011-11-30 20:21:23 -080052#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
53
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
Chet Haase44b2fe32012-06-06 19:03:58 -0700168int OpenGLRenderer::prepare(bool opaque) {
169 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800170}
171
Chet Haase44b2fe32012-06-06 19:03:58 -0700172int OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800173 mCaches.clearGarbage();
174
Romain Guy8aef54f2010-09-01 15:13:49 -0700175 mSnapshot = new Snapshot(mFirstSnapshot,
176 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800177 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700178 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700179
Romain Guy7d7b5492011-01-24 16:33:45 -0800180 mSnapshot->setClip(left, top, right, bottom);
Romain Guy57b52682012-09-20 17:38:46 -0700181 mDirtyClip = opaque;
Romain Guyddf74372012-05-22 14:07:07 -0700182
Romain Guy11cb6422012-09-21 00:39:43 -0700183 updateLayers();
184
Romain Guy45e4c3d2012-09-11 17:17:07 -0700185 // If we know that we are going to redraw the entire framebuffer,
186 // perform a discard to let the driver know we don't need to preserve
187 // the back buffer for this frame.
188 if (mCaches.extensions.hasDiscardFramebuffer() &&
189 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
190 const GLenum attachments[] = { getTargetFbo() == 0 ? GL_COLOR_EXT : GL_COLOR_ATTACHMENT0 };
191 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
192 }
193
Romain Guyddf74372012-05-22 14:07:07 -0700194 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800195
Romain Guy54c1a642012-09-27 17:55:46 -0700196 // Functors break the tiling extension in pretty spectacular ways
197 // This ensures we don't use tiling when a functor is going to be
198 // invoked during the frame
199 mSuppressTiling = mCaches.hasRegisteredFunctors();
200
Romain Guy2b7028e2012-09-19 17:25:38 -0700201 mTilingSnapshot = mSnapshot;
Romain Guy57b52682012-09-20 17:38:46 -0700202 startTiling(mTilingSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700203
Romain Guy7c450aa2012-09-21 19:15:00 -0700204 debugOverdraw(true, true);
205
Romain Guy6b7bd242010-10-06 19:49:23 -0700206 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700207 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700208 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700209 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700210 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700211 } else {
212 mCaches.resetScissor();
213 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700214
215 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700216}
217
218void OpenGLRenderer::syncState() {
219 glViewport(0, 0, mWidth, mHeight);
220
221 if (mCaches.blend) {
222 glEnable(GL_BLEND);
223 } else {
224 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700225 }
Romain Guybb9524b2010-06-22 18:56:38 -0700226}
227
Romain Guy57b52682012-09-20 17:38:46 -0700228void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700229 if (!mSuppressTiling) {
230 Rect* clip = mTilingSnapshot->clipRect;
231 if (s->flags & Snapshot::kFlagIsFboLayer) {
232 clip = s->clipRect;
233 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700234
Romain Guy54c1a642012-09-27 17:55:46 -0700235 mCaches.startTiling(clip->left, s->height - clip->bottom,
236 clip->right - clip->left, clip->bottom - clip->top, opaque);
237 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700238}
239
240void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700241 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700242}
243
Romain Guyb025b9c2010-09-16 14:16:48 -0700244void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700245 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700246 endTiling();
247
Romain Guy11cb6422012-09-21 00:39:43 -0700248 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700249#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700250 GLenum status = GL_NO_ERROR;
251 while ((status = glGetError()) != GL_NO_ERROR) {
252 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
253 switch (status) {
254 case GL_INVALID_ENUM:
255 ALOGE(" GL_INVALID_ENUM");
256 break;
257 case GL_INVALID_VALUE:
258 ALOGE(" GL_INVALID_VALUE");
259 break;
260 case GL_INVALID_OPERATION:
261 ALOGE(" GL_INVALID_OPERATION");
262 break;
263 case GL_OUT_OF_MEMORY:
264 ALOGE(" Out of memory!");
265 break;
266 }
Romain Guya07105b2011-01-10 21:14:18 -0800267 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700268#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700269
Romain Guyc15008e2010-11-10 11:59:15 -0800270#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800271 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700272#else
273 if (mCaches.getDebugLevel() & kDebugMemory) {
274 mCaches.dumpMemoryUsage();
275 }
Romain Guyc15008e2010-11-10 11:59:15 -0800276#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700277 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700278}
279
Romain Guy6c319ca2011-01-11 14:29:25 -0800280void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700281 if (mCaches.currentProgram) {
282 if (mCaches.currentProgram->isInUse()) {
283 mCaches.currentProgram->remove();
284 mCaches.currentProgram = NULL;
285 }
286 }
Romain Guy50c0f092010-10-19 11:42:22 -0700287 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800288 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800289 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800290 mCaches.disbaleTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700291 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700292}
293
Romain Guy6c319ca2011-01-11 14:29:25 -0800294void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800295 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800296 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700297 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700298 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700299
Romain Guy3e263fa2011-12-12 16:47:48 -0800300 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
301
Chet Haase80250612012-08-15 13:46:54 -0700302 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700303 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800304 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700305 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700306
Romain Guya1d3c912011-12-13 14:55:06 -0800307 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700308
Romain Guy50c0f092010-10-19 11:42:22 -0700309 mCaches.blend = true;
310 glEnable(GL_BLEND);
311 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
312 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700313}
314
Romain Guy35643dd2012-09-18 15:40:58 -0700315void OpenGLRenderer::resumeAfterLayer() {
316 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
317 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
318 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700319 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700320
321 mCaches.resetScissor();
322 dirtyClip();
323}
324
Romain Guyba6be8a2012-04-23 18:22:09 -0700325void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700326 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700327}
328
329void OpenGLRenderer::attachFunctor(Functor* functor) {
330 mFunctors.add(functor);
331}
332
Romain Guy8f3b8e32012-03-27 16:33:45 -0700333status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
334 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700335 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700336
Romain Guyba6be8a2012-04-23 18:22:09 -0700337 if (count > 0) {
338 SortedVector<Functor*> functors(mFunctors);
339 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700340
Romain Guyba6be8a2012-04-23 18:22:09 -0700341 DrawGlInfo info;
342 info.clipLeft = 0;
343 info.clipTop = 0;
344 info.clipRight = 0;
345 info.clipBottom = 0;
346 info.isLayer = false;
347 info.width = 0;
348 info.height = 0;
349 memset(info.transform, 0, sizeof(float) * 16);
350
351 for (size_t i = 0; i < count; i++) {
352 Functor* f = functors.itemAt(i);
353 result |= (*f)(DrawGlInfo::kModeProcess, &info);
354
Chris Craikc2c95432012-04-25 15:13:52 -0700355 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700356 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
357 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700358 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700359
Chris Craikc2c95432012-04-25 15:13:52 -0700360 if (result & DrawGlInfo::kStatusInvoke) {
361 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700362 }
363 }
Chet Haasebeb8bd02012-09-09 16:13:26 -0700364 // protect against functors binding to other buffers
365 mCaches.unbindMeshBuffer();
366 mCaches.unbindIndicesBuffer();
367 mCaches.activeTexture(0);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700368 }
369
370 return result;
371}
372
373status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800374 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700375 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700376
Romain Guy8a4ac612012-07-17 17:32:48 -0700377 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800378 if (mDirtyClip) {
379 setScissorFromClip();
380 }
Romain Guyd643bb52011-03-01 14:55:21 -0800381
Romain Guy80911b82011-03-16 15:30:12 -0700382 Rect clip(*mSnapshot->clipRect);
383 clip.snapToPixelBoundaries();
384
Romain Guyd643bb52011-03-01 14:55:21 -0800385 // Since we don't know what the functor will draw, let's dirty
386 // tne entire clip region
387 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800388 dirtyLayerUnchecked(clip, getRegion());
389 }
Romain Guyd643bb52011-03-01 14:55:21 -0800390
Romain Guy08aa2cb2011-03-17 11:06:57 -0700391 DrawGlInfo info;
392 info.clipLeft = clip.left;
393 info.clipTop = clip.top;
394 info.clipRight = clip.right;
395 info.clipBottom = clip.bottom;
396 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700397 info.width = getSnapshot()->viewport.getWidth();
398 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700399 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700400
Chet Haase48659092012-05-31 15:21:51 -0700401 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800402
Romain Guy8f3b8e32012-03-27 16:33:45 -0700403 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700404 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800405 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700406
Chris Craik65924a32012-04-05 17:52:11 -0700407 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700408 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700409 }
Romain Guycabfcc12011-03-07 18:06:46 -0800410 }
411
Chet Haasedaf98e92011-01-10 14:10:36 -0800412 resume();
Romain Guy65549432012-03-26 16:45:05 -0700413 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800414}
415
Romain Guyf6a11b82010-06-23 17:47:49 -0700416///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700417// Debug
418///////////////////////////////////////////////////////////////////////////////
419
420void OpenGLRenderer::startMark(const char* name) const {
421 mCaches.startMark(0, name);
422}
423
424void OpenGLRenderer::endMark() const {
425 mCaches.endMark();
426}
427
428void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
429 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
430 if (clear) {
431 mCaches.disableScissor();
432 mCaches.stencil.clear();
433 }
434 if (enable) {
435 mCaches.stencil.enableDebugWrite();
436 } else {
437 mCaches.stencil.disable();
438 }
439 }
440}
441
442void OpenGLRenderer::renderOverdraw() {
443 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
444 const Rect* clip = mTilingSnapshot->clipRect;
445
446 mCaches.enableScissor();
447 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
448 clip->right - clip->left, clip->bottom - clip->top);
449
450 mCaches.stencil.enableDebugTest(2);
451 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
452 mCaches.stencil.enableDebugTest(3);
453 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
454 mCaches.stencil.enableDebugTest(4);
455 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
456 mCaches.stencil.enableDebugTest(4, true);
457 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
458 mCaches.stencil.disable();
459 }
460}
461
462///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700463// Layers
464///////////////////////////////////////////////////////////////////////////////
465
466bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
467 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
468 OpenGLRenderer* renderer = layer->renderer;
469 Rect& dirty = layer->dirtyRect;
470
Romain Guy7c450aa2012-09-21 19:15:00 -0700471 if (inFrame) {
472 endTiling();
473 debugOverdraw(false, false);
474 }
Romain Guy11cb6422012-09-21 00:39:43 -0700475
476 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
477 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
478 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
479 renderer->finish();
480
481 if (inFrame) {
482 resumeAfterLayer();
483 startTiling(mSnapshot);
484 }
485
486 dirty.setEmpty();
487 layer->deferredUpdateScheduled = false;
488 layer->renderer = NULL;
489 layer->displayList = NULL;
490
491 return true;
492 }
493
494 return false;
495}
496
497void OpenGLRenderer::updateLayers() {
498 int count = mLayerUpdates.size();
499 if (count > 0) {
500 startMark("Layer Updates");
501
502 // Note: it is very important to update the layers in reverse order
503 for (int i = count - 1; i >= 0; i--) {
504 Layer* layer = mLayerUpdates.itemAt(i);
505 updateLayer(layer, false);
506 mCaches.resourceCache.decrementRefcount(layer);
507 }
508 mLayerUpdates.clear();
509
510 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
511 endMark();
512 }
513}
514
515void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
516 if (layer) {
517 mLayerUpdates.push_back(layer);
518 mCaches.resourceCache.incrementRefcount(layer);
519 }
520}
521
522void OpenGLRenderer::clearLayerUpdates() {
523 size_t count = mLayerUpdates.size();
524 if (count > 0) {
525 mCaches.resourceCache.lock();
526 for (size_t i = 0; i < count; i++) {
527 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
528 }
529 mCaches.resourceCache.unlock();
530 mLayerUpdates.clear();
531 }
532}
533
534///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700535// State management
536///////////////////////////////////////////////////////////////////////////////
537
Romain Guybb9524b2010-06-22 18:56:38 -0700538int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700539 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700540}
541
542int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700543 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700544}
545
546void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700547 if (mSaveCount > 1) {
548 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700549 }
Romain Guybb9524b2010-06-22 18:56:38 -0700550}
551
552void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700553 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700554
Romain Guy8fb95422010-08-17 18:38:51 -0700555 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700556 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700557 }
Romain Guybb9524b2010-06-22 18:56:38 -0700558}
559
Romain Guy8aef54f2010-09-01 15:13:49 -0700560int OpenGLRenderer::saveSnapshot(int flags) {
561 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700562 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700563}
564
565bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700566 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700567 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700568 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700569
Romain Guybd6b79b2010-06-26 00:13:53 -0700570 sp<Snapshot> current = mSnapshot;
571 sp<Snapshot> previous = mSnapshot->previous;
572
Romain Guyeb993562010-10-05 18:14:38 -0700573 if (restoreOrtho) {
574 Rect& r = previous->viewport;
575 glViewport(r.left, r.top, r.right, r.bottom);
576 mOrthoMatrix.load(current->orthoMatrix);
577 }
578
Romain Guy8b55f372010-08-18 17:10:07 -0700579 mSaveCount--;
580 mSnapshot = previous;
581
Romain Guy2542d192010-08-18 11:47:12 -0700582 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700583 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700584 }
Romain Guy2542d192010-08-18 11:47:12 -0700585
Romain Guy5ec99242010-11-03 16:19:08 -0700586 if (restoreLayer) {
587 composeLayer(current, previous);
588 }
589
Romain Guy2542d192010-08-18 11:47:12 -0700590 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700591}
592
Romain Guyf6a11b82010-06-23 17:47:49 -0700593///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700594// Layers
595///////////////////////////////////////////////////////////////////////////////
596
597int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700598 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700599 const GLuint previousFbo = mSnapshot->fbo;
600 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700601
Romain Guyaf636eb2010-12-09 17:47:21 -0800602 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700603 int alpha = 255;
604 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700605
Romain Guye45362c2010-11-03 19:58:32 -0700606 if (p) {
607 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700608 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700609 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700610 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700611 }
Romain Guyd55a8612010-06-28 17:42:46 -0700612
Chet Haased48885a2012-08-28 17:43:28 -0700613 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700614 }
Romain Guyd55a8612010-06-28 17:42:46 -0700615
616 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700617}
618
619int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
620 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700621 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700622 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700623 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700624 SkPaint paint;
625 paint.setAlpha(alpha);
626 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700627 }
Romain Guyd55a8612010-06-28 17:42:46 -0700628}
Romain Guybd6b79b2010-06-26 00:13:53 -0700629
Romain Guy1c740bc2010-09-13 18:00:09 -0700630/**
631 * Layers are viewed by Skia are slightly different than layers in image editing
632 * programs (for instance.) When a layer is created, previously created layers
633 * and the frame buffer still receive every drawing command. For instance, if a
634 * layer is created and a shape intersecting the bounds of the layers and the
635 * framebuffer is draw, the shape will be drawn on both (unless the layer was
636 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
637 *
638 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
639 * texture. Unfortunately, this is inefficient as it requires every primitive to
640 * be drawn n + 1 times, where n is the number of active layers. In practice this
641 * means, for every primitive:
642 * - Switch active frame buffer
643 * - Change viewport, clip and projection matrix
644 * - Issue the drawing
645 *
646 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700647 * To avoid this, layers are implemented in a different way here, at least in the
648 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
649 * is set. When this flag is set we can redirect all drawing operations into a
650 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700651 *
652 * This implementation relies on the frame buffer being at least RGBA 8888. When
653 * a layer is created, only a texture is created, not an FBO. The content of the
654 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700655 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700656 * buffer and drawing continues as normal. This technique therefore treats the
657 * frame buffer as a scratch buffer for the layers.
658 *
659 * To compose the layers back onto the frame buffer, each layer texture
660 * (containing the original frame buffer data) is drawn as a simple quad over
661 * the frame buffer. The trick is that the quad is set as the composition
662 * destination in the blending equation, and the frame buffer becomes the source
663 * of the composition.
664 *
665 * Drawing layers with an alpha value requires an extra step before composition.
666 * An empty quad is drawn over the layer's region in the frame buffer. This quad
667 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
668 * quad is used to multiply the colors in the frame buffer. This is achieved by
669 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
670 * GL_ZERO, GL_SRC_ALPHA.
671 *
672 * Because glCopyTexImage2D() can be slow, an alternative implementation might
673 * be use to draw a single clipped layer. The implementation described above
674 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700675 *
676 * (1) The frame buffer is actually not cleared right away. To allow the GPU
677 * to potentially optimize series of calls to glCopyTexImage2D, the frame
678 * buffer is left untouched until the first drawing operation. Only when
679 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700680 */
Chet Haased48885a2012-08-28 17:43:28 -0700681bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
682 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700683 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700684 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700685
Romain Guyeb993562010-10-05 18:14:38 -0700686 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
687
Romain Guyf607bdc2010-09-10 19:20:06 -0700688 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700689 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700690 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700691 Rect untransformedBounds(bounds);
692 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700693
Chet Haased48885a2012-08-28 17:43:28 -0700694 // Layers only make sense if they are in the framebuffer's bounds
695 if (bounds.intersect(*mSnapshot->clipRect)) {
696 // We cannot work with sub-pixels in this case
697 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700698
Chet Haased48885a2012-08-28 17:43:28 -0700699 // When the layer is not an FBO, we may use glCopyTexImage so we
700 // need to make sure the layer does not extend outside the bounds
701 // of the framebuffer
702 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700703 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700704 } else if (fboLayer) {
705 clip.set(bounds);
706 mat4 inverse;
707 inverse.loadInverse(*mSnapshot->transform);
708 inverse.mapRect(clip);
709 clip.snapToPixelBoundaries();
710 if (clip.intersect(untransformedBounds)) {
711 clip.translate(-left, -top);
712 bounds.set(untransformedBounds);
713 } else {
714 clip.setEmpty();
715 }
Romain Guyad37cd32011-03-15 11:12:25 -0700716 }
Chet Haased48885a2012-08-28 17:43:28 -0700717 } else {
718 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700719 }
Romain Guybf434112010-09-16 14:40:17 -0700720
Romain Guy746b7402010-10-26 16:27:31 -0700721 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700722 bounds.getHeight() > mCaches.maxTextureSize ||
723 (fboLayer && clip.isEmpty())) {
724 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700725 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700726 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700727 }
728
729 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700730 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700731 return false;
732 }
Romain Guyf18fd992010-07-08 11:45:51 -0700733
Romain Guya1d3c912011-12-13 14:55:06 -0800734 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700735 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700736 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700737 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700738 }
739
Romain Guy9ace8f52011-07-07 20:50:11 -0700740 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700741 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700742 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
743 bounds.getWidth() / float(layer->getWidth()), 0.0f);
744 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700745 layer->setBlend(true);
Romain Guydda57022010-07-06 11:39:32 -0700746
Romain Guy8fb95422010-08-17 18:38:51 -0700747 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700748 mSnapshot->flags |= Snapshot::kFlagIsLayer;
749 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700750
Romain Guyeb993562010-10-05 18:14:38 -0700751 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700752 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700753 } else {
754 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700755 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800756 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700757 if (layer->isEmpty()) {
758 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700759 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700760 layer->getWidth(), layer->getHeight(), 0);
761 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800762 } else {
763 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700764 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800765 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700766
Romain Guy54be1cd2011-06-13 19:04:27 -0700767 // Enqueue the buffer coordinates to clear the corresponding region later
768 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700769 }
Romain Guyeb993562010-10-05 18:14:38 -0700770 }
Romain Guyf86ef572010-07-01 11:05:42 -0700771
Romain Guyd55a8612010-06-28 17:42:46 -0700772 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700773}
774
Chet Haased48885a2012-08-28 17:43:28 -0700775bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700776 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700777
Chet Haased48885a2012-08-28 17:43:28 -0700778 mSnapshot->region = &mSnapshot->layer->region;
779 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700780
Chet Haased48885a2012-08-28 17:43:28 -0700781 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
782 mSnapshot->fbo = layer->getFbo();
783 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
784 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
785 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
786 mSnapshot->height = bounds.getHeight();
787 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
788 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700789
Romain Guy2b7028e2012-09-19 17:25:38 -0700790 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700791 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700792 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700793 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
794 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700795
796 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700797 if (layer->isEmpty()) {
798 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
799 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700800 }
801
802 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700803 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700804
Romain Guy2b7028e2012-09-19 17:25:38 -0700805 startTiling(mSnapshot);
Romain Guy5b3b3522010-10-27 18:57:51 -0700806
807 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700808 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800809 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700810 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700811 glClear(GL_COLOR_BUFFER_BIT);
812
813 dirtyClip();
814
815 // Change the ortho projection
816 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
817 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
818
819 return true;
820}
821
Romain Guy1c740bc2010-09-13 18:00:09 -0700822/**
823 * Read the documentation of createLayer() before doing anything in this method.
824 */
Romain Guy1d83e192010-08-17 11:37:00 -0700825void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
826 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000827 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700828 return;
829 }
830
Romain Guy5b3b3522010-10-27 18:57:51 -0700831 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700832
833 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700834 endTiling();
835
Romain Guye0aa84b2012-04-03 19:30:26 -0700836 // Detach the texture from the FBO
837 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guyeb993562010-10-05 18:14:38 -0700838 // Unbind current FBO and restore previous one
839 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700840 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700841
842 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700843 }
844
Romain Guy1d83e192010-08-17 11:37:00 -0700845 Layer* layer = current->layer;
846 const Rect& rect = layer->layer;
847
Romain Guy9ace8f52011-07-07 20:50:11 -0700848 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700849 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700850 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700851 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700852 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700853 }
854
Romain Guy03750a02010-10-18 14:06:08 -0700855 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700856
Romain Guya1d3c912011-12-13 14:55:06 -0800857 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700858
Romain Guy5b3b3522010-10-27 18:57:51 -0700859 // When the layer is stored in an FBO, we can save a bit of fillrate by
860 // drawing only the dirty region
861 if (fboLayer) {
862 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700863 if (layer->getColorFilter()) {
864 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800865 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700866 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700867 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800868 resetColorFilter();
869 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700870 } else if (!rect.isEmpty()) {
871 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
872 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700873 }
Romain Guy8b55f372010-08-18 17:10:07 -0700874
Romain Guyeb993562010-10-05 18:14:38 -0700875 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800876 // Note: No need to use glDiscardFramebufferEXT() since we never
877 // create/compose layers that are not on screen with this
878 // code path
879 // See LayerRenderer::destroyLayer(Layer*)
880
Romain Guyeb993562010-10-05 18:14:38 -0700881 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
882 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700883 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700884 }
885
Romain Guy746b7402010-10-26 16:27:31 -0700886 dirtyClip();
887
Romain Guyeb993562010-10-05 18:14:38 -0700888 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700889 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700890 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700891 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700892 }
893}
894
Romain Guyaa6c24c2011-04-28 18:40:04 -0700895void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700896 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700897
Romain Guy302a9df2011-08-16 13:55:02 -0700898 mat4& transform = layer->getTransform();
899 if (!transform.isIdentity()) {
900 save(0);
901 mSnapshot->transform->multiply(transform);
902 }
903
Romain Guyaa6c24c2011-04-28 18:40:04 -0700904 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700905 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700906 setupDrawWithTexture();
907 } else {
908 setupDrawWithExternalTexture();
909 }
910 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700911 setupDrawColor(alpha, alpha, alpha, alpha);
912 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700913 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700914 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700915 setupDrawPureColorUniforms();
916 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700917 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
918 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700919 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700920 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700921 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700922 if (mSnapshot->transform->isPureTranslate() &&
923 layer->getWidth() == (uint32_t) rect.getWidth() &&
924 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700925 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
926 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
927
Romain Guyd21b6e12011-11-30 20:21:23 -0800928 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700929 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
930 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800931 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700932 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
933 }
934 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700935 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
936
937 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
938
939 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700940
941 if (!transform.isIdentity()) {
942 restore();
943 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700944}
945
Romain Guy5b3b3522010-10-27 18:57:51 -0700946void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700947 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700948 const Rect& texCoords = layer->texCoords;
949 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
950 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700951
Romain Guy9ace8f52011-07-07 20:50:11 -0700952 float x = rect.left;
953 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700954 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700955 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700956 layer->getHeight() == (uint32_t) rect.getHeight();
957
958 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700959 // When we're swapping, the layer is already in screen coordinates
960 if (!swap) {
961 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
962 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
963 }
964
Romain Guyd21b6e12011-11-30 20:21:23 -0800965 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700966 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800967 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700968 }
969
970 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
971 layer->getTexture(), layer->getAlpha() / 255.0f,
972 layer->getMode(), layer->isBlend(),
973 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
974 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700975
Romain Guyaa6c24c2011-04-28 18:40:04 -0700976 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
977 } else {
978 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
979 drawTextureLayer(layer, rect);
980 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
981 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700982}
983
984void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700985 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700986 layer->setRegionAsRect();
987
Romain Guy40667672011-03-18 14:34:03 -0700988 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700989
Romain Guy5b3b3522010-10-27 18:57:51 -0700990 layer->region.clear();
991 return;
992 }
993
Romain Guy8a3957d2011-09-07 17:55:15 -0700994 // TODO: See LayerRenderer.cpp::generateMesh() for important
995 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800996 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700997 size_t count;
998 const android::Rect* rects = layer->region.getArray(&count);
999
Romain Guy9ace8f52011-07-07 20:50:11 -07001000 const float alpha = layer->getAlpha() / 255.0f;
1001 const float texX = 1.0f / float(layer->getWidth());
1002 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001003 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001004
1005 TextureVertex* mesh = mCaches.getRegionMesh();
1006 GLsizei numQuads = 0;
1007
Romain Guy7230a742011-01-10 22:26:16 -08001008 setupDraw();
1009 setupDrawWithTexture();
1010 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001011 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001012 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001013 setupDrawProgram();
1014 setupDrawDirtyRegionsDisabled();
1015 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001016 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001017 setupDrawTexture(layer->getTexture());
1018 if (mSnapshot->transform->isPureTranslate()) {
1019 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
1020 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
1021
Romain Guyd21b6e12011-11-30 20:21:23 -08001022 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001023 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1024 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001025 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001026 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1027 }
Romain Guy15bc6432011-12-13 13:11:32 -08001028 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001029
1030 for (size_t i = 0; i < count; i++) {
1031 const android::Rect* r = &rects[i];
1032
1033 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001034 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001035 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001036 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001037
1038 // TODO: Reject quads outside of the clip
1039 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1040 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1041 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1042 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1043
1044 numQuads++;
1045
1046 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1047 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1048 numQuads = 0;
1049 mesh = mCaches.getRegionMesh();
1050 }
1051 }
1052
1053 if (numQuads > 0) {
1054 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1055 }
1056
Romain Guy7230a742011-01-10 22:26:16 -08001057 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001058
1059#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001060 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001061#endif
1062
1063 layer->region.clear();
1064 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001065}
1066
Romain Guy3a3133d2011-02-01 22:59:58 -08001067void OpenGLRenderer::drawRegionRects(const Region& region) {
1068#if DEBUG_LAYERS_AS_REGIONS
1069 size_t count;
1070 const android::Rect* rects = region.getArray(&count);
1071
1072 uint32_t colors[] = {
1073 0x7fff0000, 0x7f00ff00,
1074 0x7f0000ff, 0x7fff00ff,
1075 };
1076
1077 int offset = 0;
1078 int32_t top = rects[0].top;
1079
1080 for (size_t i = 0; i < count; i++) {
1081 if (top != rects[i].top) {
1082 offset ^= 0x2;
1083 top = rects[i].top;
1084 }
1085
1086 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1087 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1088 SkXfermode::kSrcOver_Mode);
1089 }
1090#endif
1091}
1092
Romain Guy5b3b3522010-10-27 18:57:51 -07001093void OpenGLRenderer::dirtyLayer(const float left, const float top,
1094 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001095 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001096 Rect bounds(left, top, right, bottom);
1097 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001098 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001099 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001100}
1101
1102void OpenGLRenderer::dirtyLayer(const float left, const float top,
1103 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001104 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001105 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001106 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001107 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001108}
1109
1110void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001111 if (bounds.intersect(*mSnapshot->clipRect)) {
1112 bounds.snapToPixelBoundaries();
1113 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1114 if (!dirty.isEmpty()) {
1115 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001116 }
1117 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001118}
1119
Romain Guy54be1cd2011-06-13 19:04:27 -07001120void OpenGLRenderer::clearLayerRegions() {
1121 const size_t count = mLayers.size();
1122 if (count == 0) return;
1123
1124 if (!mSnapshot->isIgnored()) {
1125 // Doing several glScissor/glClear here can negatively impact
1126 // GPUs with a tiler architecture, instead we draw quads with
1127 // the Clear blending mode
1128
1129 // The list contains bounds that have already been clipped
1130 // against their initial clip rect, and the current clip
1131 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001132 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001133
1134 Vertex mesh[count * 6];
1135 Vertex* vertex = mesh;
1136
1137 for (uint32_t i = 0; i < count; i++) {
1138 Rect* bounds = mLayers.itemAt(i);
1139
1140 Vertex::set(vertex++, bounds->left, bounds->bottom);
1141 Vertex::set(vertex++, bounds->left, bounds->top);
1142 Vertex::set(vertex++, bounds->right, bounds->top);
1143 Vertex::set(vertex++, bounds->left, bounds->bottom);
1144 Vertex::set(vertex++, bounds->right, bounds->top);
1145 Vertex::set(vertex++, bounds->right, bounds->bottom);
1146
1147 delete bounds;
1148 }
1149
1150 setupDraw(false);
1151 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1152 setupDrawBlending(true, SkXfermode::kClear_Mode);
1153 setupDrawProgram();
1154 setupDrawPureColorUniforms();
1155 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001156 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001157
Romain Guy54be1cd2011-06-13 19:04:27 -07001158 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001159
1160 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001161 } else {
1162 for (uint32_t i = 0; i < count; i++) {
1163 delete mLayers.itemAt(i);
1164 }
1165 }
1166
1167 mLayers.clear();
1168}
1169
Romain Guybd6b79b2010-06-26 00:13:53 -07001170///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001171// Transforms
1172///////////////////////////////////////////////////////////////////////////////
1173
1174void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001175 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001176}
1177
1178void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001179 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001180}
1181
1182void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001183 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001184}
1185
Romain Guy807daf72011-01-18 11:19:19 -08001186void OpenGLRenderer::skew(float sx, float sy) {
1187 mSnapshot->transform->skew(sx, sy);
1188}
1189
Romain Guyf6a11b82010-06-23 17:47:49 -07001190void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001191 if (matrix) {
1192 mSnapshot->transform->load(*matrix);
1193 } else {
1194 mSnapshot->transform->loadIdentity();
1195 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001196}
1197
1198void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001199 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001200}
1201
1202void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001203 SkMatrix transform;
1204 mSnapshot->transform->copyTo(transform);
1205 transform.preConcat(*matrix);
1206 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001207}
1208
1209///////////////////////////////////////////////////////////////////////////////
1210// Clipping
1211///////////////////////////////////////////////////////////////////////////////
1212
Romain Guybb9524b2010-06-22 18:56:38 -07001213void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001214 Rect clip(*mSnapshot->clipRect);
1215 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001216
Romain Guy8a4ac612012-07-17 17:32:48 -07001217 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1218 clip.getWidth(), clip.getHeight())) {
1219 mDirtyClip = false;
1220 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001221}
1222
1223const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001224 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001225}
1226
Romain Guy8a4ac612012-07-17 17:32:48 -07001227bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1228 if (mSnapshot->isIgnored()) {
1229 return true;
1230 }
1231
1232 Rect r(left, top, right, bottom);
1233 mSnapshot->transform->mapRect(r);
1234 r.snapToPixelBoundaries();
1235
1236 Rect clipRect(*mSnapshot->clipRect);
1237 clipRect.snapToPixelBoundaries();
1238
1239 return !clipRect.intersects(r);
1240}
1241
Romain Guy35643dd2012-09-18 15:40:58 -07001242bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1243 Rect& transformed, Rect& clip) {
1244 if (mSnapshot->isIgnored()) {
1245 return true;
1246 }
1247
1248 transformed.set(left, top, right, bottom);
1249 mSnapshot->transform->mapRect(transformed);
1250 transformed.snapToPixelBoundaries();
1251
1252 clip.set(*mSnapshot->clipRect);
1253 clip.snapToPixelBoundaries();
1254
1255 return !clip.intersects(transformed);
1256}
1257
Romain Guyc7d53492010-06-25 13:41:57 -07001258bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001259 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001260 return true;
1261 }
1262
Romain Guy1d83e192010-08-17 11:37:00 -07001263 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001264 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001265 r.snapToPixelBoundaries();
1266
1267 Rect clipRect(*mSnapshot->clipRect);
1268 clipRect.snapToPixelBoundaries();
1269
Romain Guy586cae32012-07-13 15:28:31 -07001270 bool rejected = !clipRect.intersects(r);
1271 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001272 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001273 }
1274
1275 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001276}
1277
Romain Guy079ba2c2010-07-16 14:12:24 -07001278bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1279 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001280 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001281 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001282 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001283 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001284}
1285
Chet Haasea23eed82012-04-12 15:19:04 -07001286Rect* OpenGLRenderer::getClipRect() {
1287 return mSnapshot->clipRect;
1288}
1289
Romain Guyf6a11b82010-06-23 17:47:49 -07001290///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001291// Drawing commands
1292///////////////////////////////////////////////////////////////////////////////
1293
Romain Guy54be1cd2011-06-13 19:04:27 -07001294void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001295 // TODO: It would be best if we could do this before quickReject()
1296 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001297 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001298 if (mDirtyClip) {
1299 setScissorFromClip();
1300 }
1301 mDescription.reset();
1302 mSetShaderColor = false;
1303 mColorSet = false;
1304 mColorA = mColorR = mColorG = mColorB = 0.0f;
1305 mTextureUnit = 0;
1306 mTrackDirtyRegions = true;
1307}
1308
1309void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1310 mDescription.hasTexture = true;
1311 mDescription.hasAlpha8Texture = isAlpha8;
1312}
1313
Romain Guyaa6c24c2011-04-28 18:40:04 -07001314void OpenGLRenderer::setupDrawWithExternalTexture() {
1315 mDescription.hasExternalTexture = true;
1316}
1317
Romain Guy15bc6432011-12-13 13:11:32 -08001318void OpenGLRenderer::setupDrawNoTexture() {
1319 mCaches.disbaleTexCoordsVertexArray();
1320}
1321
Chris Craik710f46d2012-09-17 17:25:49 -07001322void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001323 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001324}
1325
Chris Craik710f46d2012-09-17 17:25:49 -07001326void OpenGLRenderer::setupDrawVertexShape() {
1327 mDescription.isVertexShape = true;
Chris Craik6ebdc112012-08-31 18:24:33 -07001328}
1329
Romain Guyed6fcb02011-03-21 13:11:28 -07001330void OpenGLRenderer::setupDrawPoint(float pointSize) {
1331 mDescription.isPoint = true;
1332 mDescription.pointSize = pointSize;
1333}
1334
Romain Guy70ca14e2010-12-13 18:24:33 -08001335void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001336 setupDrawColor(color, (color >> 24) & 0xFF);
1337}
1338
1339void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1340 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001341 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1342 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001343 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001344 mColorR = a * ((color >> 16) & 0xFF);
1345 mColorG = a * ((color >> 8) & 0xFF);
1346 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001347 mColorSet = true;
1348 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1349}
1350
Romain Guy86568192010-12-14 15:55:39 -08001351void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1352 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001353 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1354 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001355 const float a = mColorA / 255.0f;
1356 mColorR = a * ((color >> 16) & 0xFF);
1357 mColorG = a * ((color >> 8) & 0xFF);
1358 mColorB = a * ((color ) & 0xFF);
1359 mColorSet = true;
1360 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1361}
1362
Romain Guy41210632012-07-16 17:04:24 -07001363void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1364 mCaches.fontRenderer->describe(mDescription, paint);
1365}
1366
Romain Guy70ca14e2010-12-13 18:24:33 -08001367void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1368 mColorA = a;
1369 mColorR = r;
1370 mColorG = g;
1371 mColorB = b;
1372 mColorSet = true;
1373 mSetShaderColor = mDescription.setColor(r, g, b, a);
1374}
1375
1376void OpenGLRenderer::setupDrawShader() {
1377 if (mShader) {
1378 mShader->describe(mDescription, mCaches.extensions);
1379 }
1380}
1381
1382void OpenGLRenderer::setupDrawColorFilter() {
1383 if (mColorFilter) {
1384 mColorFilter->describe(mDescription, mCaches.extensions);
1385 }
1386}
1387
Romain Guyf09ef512011-05-27 11:43:46 -07001388void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1389 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1390 mColorA = 1.0f;
1391 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001392 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001393 }
1394}
1395
Romain Guy70ca14e2010-12-13 18:24:33 -08001396void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001397 // When the blending mode is kClear_Mode, we need to use a modulate color
1398 // argb=1,0,0,0
1399 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001400 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1401 mDescription, swapSrcDst);
1402}
1403
1404void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001405 // When the blending mode is kClear_Mode, we need to use a modulate color
1406 // argb=1,0,0,0
1407 accountForClear(mode);
Romain Guye83221c2012-09-24 16:01:35 -07001408 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()) ||
1409 (mColorFilter && mColorFilter->blend()), mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001410}
1411
1412void OpenGLRenderer::setupDrawProgram() {
1413 useProgram(mCaches.programCache.get(mDescription));
1414}
1415
1416void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1417 mTrackDirtyRegions = false;
1418}
1419
1420void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1421 bool ignoreTransform) {
1422 mModelView.loadTranslate(left, top, 0.0f);
1423 if (!ignoreTransform) {
1424 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1425 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1426 } else {
1427 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1428 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1429 }
1430}
1431
Chet Haase8a5cc922011-04-26 07:28:09 -07001432void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1433 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001434}
1435
Romain Guy70ca14e2010-12-13 18:24:33 -08001436void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1437 bool ignoreTransform, bool ignoreModelView) {
1438 if (!ignoreModelView) {
1439 mModelView.loadTranslate(left, top, 0.0f);
1440 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001441 } else {
1442 mModelView.loadIdentity();
1443 }
Romain Guy86568192010-12-14 15:55:39 -08001444 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1445 if (!ignoreTransform) {
1446 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1447 if (mTrackDirtyRegions && dirty) {
1448 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1449 }
1450 } else {
1451 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1452 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1453 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001454}
1455
Romain Guyed6fcb02011-03-21 13:11:28 -07001456void OpenGLRenderer::setupDrawPointUniforms() {
1457 int slot = mCaches.currentProgram->getUniform("pointSize");
1458 glUniform1f(slot, mDescription.pointSize);
1459}
1460
Romain Guy70ca14e2010-12-13 18:24:33 -08001461void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001462 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001463 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1464 }
1465}
1466
Romain Guy86568192010-12-14 15:55:39 -08001467void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001468 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001469 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001470 }
1471}
1472
Romain Guy70ca14e2010-12-13 18:24:33 -08001473void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1474 if (mShader) {
1475 if (ignoreTransform) {
1476 mModelView.loadInverse(*mSnapshot->transform);
1477 }
1478 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1479 }
1480}
1481
Romain Guy8d0d4782010-12-14 20:13:35 -08001482void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1483 if (mShader) {
1484 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1485 }
1486}
1487
Romain Guy70ca14e2010-12-13 18:24:33 -08001488void OpenGLRenderer::setupDrawColorFilterUniforms() {
1489 if (mColorFilter) {
1490 mColorFilter->setupProgram(mCaches.currentProgram);
1491 }
1492}
1493
Romain Guy41210632012-07-16 17:04:24 -07001494void OpenGLRenderer::setupDrawTextGammaUniforms() {
1495 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1496}
1497
Romain Guy70ca14e2010-12-13 18:24:33 -08001498void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001499 bool force = mCaches.bindMeshBuffer();
1500 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001501 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001502}
1503
1504void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1505 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001506 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001507 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001508}
1509
Romain Guyaa6c24c2011-04-28 18:40:04 -07001510void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1511 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001512 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001513 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001514}
1515
Romain Guy8f0095c2011-05-02 17:24:22 -07001516void OpenGLRenderer::setupDrawTextureTransform() {
1517 mDescription.hasTextureTransform = true;
1518}
1519
1520void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001521 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1522 GL_FALSE, &transform.data[0]);
1523}
1524
Romain Guy70ca14e2010-12-13 18:24:33 -08001525void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001526 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001527 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001528 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001529 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001530 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001531 }
Romain Guyd71dd362011-12-12 19:03:35 -08001532
Romain Guyf3a910b42011-12-12 20:35:21 -08001533 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001534 if (mCaches.currentProgram->texCoords >= 0) {
1535 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1536 }
1537
1538 mCaches.unbindIndicesBuffer();
1539}
1540
1541void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1542 bool force = mCaches.unbindMeshBuffer();
1543 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1544 if (mCaches.currentProgram->texCoords >= 0) {
1545 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001546 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001547}
1548
Chet Haase5b0200b2011-04-13 17:58:08 -07001549void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001550 bool force = mCaches.unbindMeshBuffer();
1551 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1552 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001553 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001554}
1555
1556/**
1557 * 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 -07001558 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1559 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1560 * attributes (one per vertex) are values from zero to one that tells the fragment
1561 * shader where the fragment is in relation to the line width/length overall; these values are
1562 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1563 * region of the line.
1564 * Note that we only pass down the width values in this setup function. The length coordinates
1565 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001566 */
Chet Haase99585ad2011-05-02 15:00:16 -07001567void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001568 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001569 bool force = mCaches.unbindMeshBuffer();
1570 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1571 vertices, gAAVertexStride);
1572 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001573 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001574
Romain Guy7b631422012-04-04 11:38:54 -07001575 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001576 glEnableVertexAttribArray(widthSlot);
1577 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001578
Romain Guy7b631422012-04-04 11:38:54 -07001579 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001580 glEnableVertexAttribArray(lengthSlot);
1581 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001582
Chet Haase5b0200b2011-04-13 17:58:08 -07001583 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001584 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001585}
1586
1587void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1588 glDisableVertexAttribArray(widthSlot);
1589 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001590}
1591
Romain Guy70ca14e2010-12-13 18:24:33 -08001592void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001593}
1594
1595///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001596// Drawing
1597///////////////////////////////////////////////////////////////////////////////
1598
Chet Haase1271e2c2012-04-20 09:54:27 -07001599status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001600 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001601
Romain Guy0fe478e2010-11-08 12:08:41 -08001602 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1603 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001604 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001605 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001606 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001607
Romain Guy65549432012-03-26 16:45:05 -07001608 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001609}
1610
Chet Haaseed30fd82011-04-22 16:18:45 -07001611void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1612 if (displayList) {
1613 displayList->output(*this, level);
1614 }
1615}
1616
Romain Guya168d732011-03-18 16:50:13 -07001617void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1618 int alpha;
1619 SkXfermode::Mode mode;
1620 getAlphaAndMode(paint, &alpha, &mode);
1621
Romain Guya168d732011-03-18 16:50:13 -07001622 float x = left;
1623 float y = top;
1624
Romain Guye3c26852011-07-25 16:36:01 -07001625 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001626 bool ignoreTransform = false;
1627 if (mSnapshot->transform->isPureTranslate()) {
1628 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1629 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1630 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001631 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001632 } else {
1633 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001634 }
1635
1636 setupDraw();
1637 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001638 if (paint) {
1639 setupDrawAlpha8Color(paint->getColor(), alpha);
1640 }
Romain Guya168d732011-03-18 16:50:13 -07001641 setupDrawColorFilter();
1642 setupDrawShader();
1643 setupDrawBlending(true, mode);
1644 setupDrawProgram();
1645 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001646
Romain Guya168d732011-03-18 16:50:13 -07001647 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001648 texture->setWrap(GL_CLAMP_TO_EDGE);
1649 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001650
Romain Guya168d732011-03-18 16:50:13 -07001651 setupDrawPureColorUniforms();
1652 setupDrawColorFilterUniforms();
1653 setupDrawShaderUniforms();
1654 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1655
1656 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1657
1658 finishDrawTexture();
1659}
1660
Chet Haase48659092012-05-31 15:21:51 -07001661status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001662 const float right = left + bitmap->width();
1663 const float bottom = top + bitmap->height();
1664
1665 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001666 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001667 }
1668
Romain Guya1d3c912011-12-13 14:55:06 -08001669 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001670 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001671 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001672 const AutoTexture autoCleanup(texture);
1673
Romain Guy211370f2012-02-01 16:10:55 -08001674 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001675 drawAlphaBitmap(texture, left, top, paint);
1676 } else {
1677 drawTextureRect(left, top, right, bottom, texture, paint);
1678 }
Chet Haase48659092012-05-31 15:21:51 -07001679
1680 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001681}
1682
Chet Haase48659092012-05-31 15:21:51 -07001683status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001684 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1685 const mat4 transform(*matrix);
1686 transform.mapRect(r);
1687
Romain Guy6926c722010-07-12 20:20:03 -07001688 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001689 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001690 }
1691
Romain Guya1d3c912011-12-13 14:55:06 -08001692 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001693 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001694 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001695 const AutoTexture autoCleanup(texture);
1696
Romain Guy5b3b3522010-10-27 18:57:51 -07001697 // This could be done in a cheaper way, all we need is pass the matrix
1698 // to the vertex shader. The save/restore is a bit overkill.
1699 save(SkCanvas::kMatrix_SaveFlag);
1700 concatMatrix(matrix);
1701 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1702 restore();
Chet Haase48659092012-05-31 15:21:51 -07001703
1704 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001705}
1706
Chet Haase48659092012-05-31 15:21:51 -07001707status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001708 const float right = left + bitmap->width();
1709 const float bottom = top + bitmap->height();
1710
1711 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001712 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001713 }
1714
1715 mCaches.activeTexture(0);
1716 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1717 const AutoTexture autoCleanup(texture);
1718
1719 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001720
1721 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001722}
1723
Chet Haase48659092012-05-31 15:21:51 -07001724status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001725 float* vertices, int* colors, SkPaint* paint) {
1726 // TODO: Do a quickReject
1727 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 Guya1d3c912011-12-13 14:55:06 -08001731 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001732 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001733 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001734 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001735
Romain Guyd21b6e12011-11-30 20:21:23 -08001736 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1737 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001738
1739 int alpha;
1740 SkXfermode::Mode mode;
1741 getAlphaAndMode(paint, &alpha, &mode);
1742
Romain Guy5a7b4662011-01-20 19:09:30 -08001743 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001744
Romain Guyb18d2d02011-02-10 15:52:54 -08001745 float left = FLT_MAX;
1746 float top = FLT_MAX;
1747 float right = FLT_MIN;
1748 float bottom = FLT_MIN;
1749
Romain Guy211370f2012-02-01 16:10:55 -08001750 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001751
Romain Guya566b7c2011-01-23 16:36:11 -08001752 // TODO: Support the colors array
1753 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001754 TextureVertex* vertex = mesh;
1755 for (int32_t y = 0; y < meshHeight; y++) {
1756 for (int32_t x = 0; x < meshWidth; x++) {
1757 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1758
1759 float u1 = float(x) / meshWidth;
1760 float u2 = float(x + 1) / meshWidth;
1761 float v1 = float(y) / meshHeight;
1762 float v2 = float(y + 1) / meshHeight;
1763
1764 int ax = i + (meshWidth + 1) * 2;
1765 int ay = ax + 1;
1766 int bx = i;
1767 int by = bx + 1;
1768 int cx = i + 2;
1769 int cy = cx + 1;
1770 int dx = i + (meshWidth + 1) * 2 + 2;
1771 int dy = dx + 1;
1772
1773 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1774 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1775 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1776
1777 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1778 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1779 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001780
Romain Guyb18d2d02011-02-10 15:52:54 -08001781 if (hasActiveLayer) {
1782 // TODO: This could be optimized to avoid unnecessary ops
1783 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1784 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1785 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1786 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1787 }
Romain Guy5a7b4662011-01-20 19:09:30 -08001788 }
1789 }
1790
Romain Guyb18d2d02011-02-10 15:52:54 -08001791 if (hasActiveLayer) {
1792 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1793 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001794
Romain Guy5a7b4662011-01-20 19:09:30 -08001795 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1796 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001797 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001798
1799 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001800}
1801
Chet Haase48659092012-05-31 15:21:51 -07001802status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001803 float srcLeft, float srcTop, float srcRight, float srcBottom,
1804 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001805 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001806 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001807 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001808 }
1809
Romain Guya1d3c912011-12-13 14:55:06 -08001810 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001811 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001812 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001813 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001814
Romain Guy8ba548f2010-06-30 19:21:21 -07001815 const float width = texture->width;
1816 const float height = texture->height;
1817
Romain Guy68169722011-08-22 17:33:33 -07001818 const float u1 = fmax(0.0f, srcLeft / width);
1819 const float v1 = fmax(0.0f, srcTop / height);
1820 const float u2 = fmin(1.0f, srcRight / width);
1821 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001822
Romain Guy03750a02010-10-18 14:06:08 -07001823 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001824 resetDrawTextureTexCoords(u1, v1, u2, v2);
1825
Romain Guy03750a02010-10-18 14:06:08 -07001826 int alpha;
1827 SkXfermode::Mode mode;
1828 getAlphaAndMode(paint, &alpha, &mode);
1829
Romain Guyd21b6e12011-11-30 20:21:23 -08001830 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1831
Romain Guy211370f2012-02-01 16:10:55 -08001832 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001833 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1834 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1835
Romain Guyb5014982011-07-28 15:39:12 -07001836 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001837 // Enable linear filtering if the source rectangle is scaled
1838 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001839 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001840 }
Romain Guyb5014982011-07-28 15:39:12 -07001841
Romain Guyd21b6e12011-11-30 20:21:23 -08001842 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001843 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1844 texture->id, alpha / 255.0f, mode, texture->blend,
1845 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1846 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1847 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001848 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001849 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1850 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1851 GL_TRIANGLE_STRIP, gMeshCount);
1852 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001853
1854 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001855
1856 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001857}
1858
Chet Haase48659092012-05-31 15:21:51 -07001859status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001860 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001861 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001862 int alpha;
1863 SkXfermode::Mode mode;
1864 getAlphaAndModeDirect(paint, &alpha, &mode);
1865
1866 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1867 left, top, right, bottom, alpha, mode);
1868}
1869
1870status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1871 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1872 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001873 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001874 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001875 }
1876
Romain Guybe6f9dc2012-07-16 12:41:17 -07001877 alpha *= mSnapshot->alpha;
1878
Romain Guya1d3c912011-12-13 14:55:06 -08001879 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001880 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001881 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001882 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001883 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1884 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001885
Romain Guy2728f962010-10-08 18:36:15 -07001886 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001887 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001888
Romain Guy211370f2012-02-01 16:10:55 -08001889 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001890 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001891 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001892 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001893 const float offsetX = left + mSnapshot->transform->getTranslateX();
1894 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001895 const size_t count = mesh->quads.size();
1896 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001897 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001898 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001899 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1900 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1901 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001902 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001903 dirtyLayer(left + bounds.left, top + bounds.top,
1904 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001905 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001906 }
1907 }
1908
Romain Guy211370f2012-02-01 16:10:55 -08001909 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001910 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1911 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1912
1913 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1914 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1915 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1916 true, !mesh->hasEmptyQuads);
1917 } else {
1918 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1919 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1920 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1921 true, !mesh->hasEmptyQuads);
1922 }
Romain Guy054dc182010-10-15 17:55:25 -07001923 }
Chet Haase48659092012-05-31 15:21:51 -07001924
1925 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001926}
1927
Chet Haase99ecdc42011-05-06 12:06:34 -07001928/**
Chet Haase858aa932011-05-12 09:06:00 -07001929 * This function uses a similar approach to that of AA lines in the drawLines() function.
Chris Craik6ebdc112012-08-31 18:24:33 -07001930 * We expand the rectangle by a half pixel in screen space on all sides. However, instead of using
1931 * a fragment shader to compute the translucency of the color from its position, we simply use a
1932 * varying parameter to define how far a given pixel is into the region.
Chet Haase858aa932011-05-12 09:06:00 -07001933 */
Chris Craik710f46d2012-09-17 17:25:49 -07001934void OpenGLRenderer::drawConvexPath(const SkPath& path, int color, SkXfermode::Mode mode, bool isAA) {
1935 VertexBuffer vertexBuffer;
1936 // TODO: try clipping large paths to viewport
1937 PathRenderer::convexPathFillVertices(path, mSnapshot->transform, vertexBuffer, isAA);
Romain Guy04299382012-07-18 17:15:41 -07001938
Chris Craik710f46d2012-09-17 17:25:49 -07001939 setupDraw();
1940 setupDrawNoTexture();
1941 if (isAA) setupDrawAA();
1942 setupDrawVertexShape();
1943 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1944 setupDrawColorFilter();
1945 setupDrawShader();
1946 setupDrawBlending(isAA, mode);
1947 setupDrawProgram();
1948 setupDrawModelViewIdentity(true);
1949 setupDrawColorUniforms();
1950 setupDrawColorFilterUniforms();
1951 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07001952
Chris Craik710f46d2012-09-17 17:25:49 -07001953 void* vertices = vertexBuffer.getBuffer();
1954 bool force = mCaches.unbindMeshBuffer();
1955 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1956 vertices, isAA ? gAlphaVertexStride : gVertexStride);
1957 mCaches.resetTexCoordsVertexPointer();
1958 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07001959
Chris Craik710f46d2012-09-17 17:25:49 -07001960 int alphaSlot = -1;
1961 if (isAA) {
1962 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
1963 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07001964
Chris Craik710f46d2012-09-17 17:25:49 -07001965 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07001966 glEnableVertexAttribArray(alphaSlot);
1967 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07001968 }
Romain Guy04299382012-07-18 17:15:41 -07001969
Chris Craik710f46d2012-09-17 17:25:49 -07001970 SkRect bounds = path.getBounds();
1971 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy04299382012-07-18 17:15:41 -07001972
Chris Craik710f46d2012-09-17 17:25:49 -07001973 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07001974
Chris Craik710f46d2012-09-17 17:25:49 -07001975 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07001976 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001977 }
Chet Haase858aa932011-05-12 09:06:00 -07001978}
1979
1980/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001981 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1982 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1983 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1984 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1985 * of the line. Hairlines are more involved because we need to account for transform scaling
1986 * to end up with a one-pixel-wide line in screen space..
1987 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1988 * in combination with values that we calculate and pass down in this method. The basic approach
1989 * is that the quad we create contains both the core line area plus a bounding area in which
1990 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1991 * proportion of the width and the length of a given segment is represented by the boundary
1992 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1993 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1994 * on the inside). This ends up giving the result we want, with pixels that are completely
1995 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1996 * how far into the boundary region they are, which is determined by shader interpolation.
1997 */
Chet Haase48659092012-05-31 15:21:51 -07001998status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1999 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002000
2001 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07002002 // We use half the stroke width here because we're going to position the quad
2003 // corner vertices half of the width away from the line endpoints
2004 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002005 // A stroke width of 0 has a special meaning in Skia:
2006 // it draws a line 1 px wide regardless of current transform
2007 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07002008
Chet Haase99ecdc42011-05-06 12:06:34 -07002009 float inverseScaleX = 1.0f;
2010 float inverseScaleY = 1.0f;
2011 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07002012
Chet Haase8a5cc922011-04-26 07:28:09 -07002013 int alpha;
2014 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07002015
Chet Haase8a5cc922011-04-26 07:28:09 -07002016 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002017 int verticesCount = count;
2018 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07002019 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07002020 verticesCount += (count - 4);
2021 }
2022
2023 if (isHairLine || isAA) {
2024 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
2025 // the line on the screen should always be one pixel wide regardless of scale. For
2026 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08002027 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07002028 Matrix4 *mat = mSnapshot->transform;
2029 float m00 = mat->data[Matrix4::kScaleX];
2030 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07002031 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07002032 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07002033
Romain Guy211370f2012-02-01 16:10:55 -08002034 float scaleX = sqrtf(m00 * m00 + m01 * m01);
2035 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07002036
Chet Haase99ecdc42011-05-06 12:06:34 -07002037 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
2038 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07002039
Chet Haase99ecdc42011-05-06 12:06:34 -07002040 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
2041 scaled = true;
2042 }
2043 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002044 }
Chet Haase8a5cc922011-04-26 07:28:09 -07002045
2046 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07002047
2048 mCaches.enableScissor();
2049
Chet Haase8a5cc922011-04-26 07:28:09 -07002050 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002051 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002052 if (isAA) {
Chris Craik710f46d2012-09-17 17:25:49 -07002053 setupDrawAA();
Chet Haase8a5cc922011-04-26 07:28:09 -07002054 }
2055 setupDrawColor(paint->getColor(), alpha);
2056 setupDrawColorFilter();
2057 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08002058 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07002059 setupDrawProgram();
2060 setupDrawModelViewIdentity(true);
2061 setupDrawColorUniforms();
2062 setupDrawColorFilterUniforms();
2063 setupDrawShaderIdentityUniforms();
2064
2065 if (isHairLine) {
2066 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07002067 halfStrokeWidth = isAA? 1 : .5;
2068 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002069 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07002070 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07002071 }
Romain Guy7b631422012-04-04 11:38:54 -07002072
2073 int widthSlot;
2074 int lengthSlot;
2075
Chet Haase5b0200b2011-04-13 17:58:08 -07002076 Vertex lines[verticesCount];
2077 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002078
Chet Haase99585ad2011-05-02 15:00:16 -07002079 AAVertex wLines[verticesCount];
2080 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002081
Romain Guy211370f2012-02-01 16:10:55 -08002082 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002083 setupDrawVertices(vertices);
2084 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002085 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2086 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002087 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002088 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2089 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002090 // We will need to calculate the actual width proportion on each segment for
2091 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002092 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002093 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2094 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002095 }
2096
Chet Haase99ecdc42011-05-06 12:06:34 -07002097 AAVertex* prevAAVertex = NULL;
2098 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002099
Chet Haase99585ad2011-05-02 15:00:16 -07002100 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002101 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002102
Chet Haase5b0200b2011-04-13 17:58:08 -07002103 for (int i = 0; i < count; i += 4) {
2104 // a = start point, b = end point
2105 vec2 a(points[i], points[i + 1]);
2106 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002107
Chet Haase99585ad2011-05-02 15:00:16 -07002108 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002109 float boundaryLengthProportion = 0;
2110 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002111
Chet Haase5b0200b2011-04-13 17:58:08 -07002112 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002113 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002114 float x = n.x;
2115 n.x = -n.y;
2116 n.y = x;
2117
Chet Haase8a5cc922011-04-26 07:28:09 -07002118 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002119 if (isAA) {
2120 float wideningFactor;
2121 if (fabs(n.x) >= fabs(n.y)) {
2122 wideningFactor = fabs(1.0f / n.x);
2123 } else {
2124 wideningFactor = fabs(1.0f / n.y);
2125 }
2126 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002127 }
Romain Guy7b631422012-04-04 11:38:54 -07002128
Chet Haase99ecdc42011-05-06 12:06:34 -07002129 if (scaled) {
2130 n.x *= inverseScaleX;
2131 n.y *= inverseScaleY;
2132 }
2133 } else if (scaled) {
2134 // Extend n by .5 pixel on each side, post-transform
2135 vec2 extendedN = n.copyNormalized();
2136 extendedN /= 2;
2137 extendedN.x *= inverseScaleX;
2138 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002139
Chet Haase99ecdc42011-05-06 12:06:34 -07002140 float extendedNLength = extendedN.length();
2141 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002142 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002143 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002144 }
Romain Guy7b631422012-04-04 11:38:54 -07002145
Chet Haase99585ad2011-05-02 15:00:16 -07002146 // aa lines expand the endpoint vertices to encompass the AA boundary
2147 if (isAA) {
2148 vec2 abVector = (b - a);
2149 length = abVector.length();
2150 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002151
Chet Haase99ecdc42011-05-06 12:06:34 -07002152 if (scaled) {
2153 abVector.x *= inverseScaleX;
2154 abVector.y *= inverseScaleY;
2155 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002156 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002157 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002158 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002159 }
Romain Guy7b631422012-04-04 11:38:54 -07002160
Chet Haase99ecdc42011-05-06 12:06:34 -07002161 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002162 a -= abVector;
2163 b += abVector;
2164 }
2165
Chet Haase5b0200b2011-04-13 17:58:08 -07002166 // Four corners of the rectangle defining a thick line
2167 vec2 p1 = a - n;
2168 vec2 p2 = a + n;
2169 vec2 p3 = b + n;
2170 vec2 p4 = b - n;
2171
Chet Haase99585ad2011-05-02 15:00:16 -07002172
Chet Haase5b0200b2011-04-13 17:58:08 -07002173 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2174 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2175 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2176 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2177
Romain Guy04299382012-07-18 17:15:41 -07002178 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002179 if (!isAA) {
2180 if (prevVertex != NULL) {
2181 // Issue two repeat vertices to create degenerate triangles to bridge
2182 // between the previous line and the new one. This is necessary because
2183 // we are creating a single triangle_strip which will contain
2184 // potentially discontinuous line segments.
2185 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2186 Vertex::set(vertices++, p1.x, p1.y);
2187 generatedVerticesCount += 2;
2188 }
Romain Guy7b631422012-04-04 11:38:54 -07002189
Chet Haase5b0200b2011-04-13 17:58:08 -07002190 Vertex::set(vertices++, p1.x, p1.y);
2191 Vertex::set(vertices++, p2.x, p2.y);
2192 Vertex::set(vertices++, p4.x, p4.y);
2193 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002194
Chet Haase5b0200b2011-04-13 17:58:08 -07002195 prevVertex = vertices - 1;
2196 generatedVerticesCount += 4;
2197 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002198 if (!isHairLine && scaled) {
2199 // Must set width proportions per-segment for scaled non-hairlines to use the
2200 // correct AA boundary dimensions
2201 if (boundaryWidthSlot < 0) {
2202 boundaryWidthSlot =
2203 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002204 }
Romain Guy7b631422012-04-04 11:38:54 -07002205
Chet Haase99ecdc42011-05-06 12:06:34 -07002206 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002207 }
Romain Guy7b631422012-04-04 11:38:54 -07002208
Chet Haase99585ad2011-05-02 15:00:16 -07002209 if (boundaryLengthSlot < 0) {
2210 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002211 }
Romain Guy7b631422012-04-04 11:38:54 -07002212
Chet Haase99ecdc42011-05-06 12:06:34 -07002213 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002214
Chet Haase5b0200b2011-04-13 17:58:08 -07002215 if (prevAAVertex != NULL) {
2216 // Issue two repeat vertices to create degenerate triangles to bridge
2217 // between the previous line and the new one. This is necessary because
2218 // we are creating a single triangle_strip which will contain
2219 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002220 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2221 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2222 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002223 generatedVerticesCount += 2;
2224 }
Romain Guy7b631422012-04-04 11:38:54 -07002225
Chet Haase99585ad2011-05-02 15:00:16 -07002226 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2227 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2228 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2229 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002230
Chet Haase5b0200b2011-04-13 17:58:08 -07002231 prevAAVertex = aaVertices - 1;
2232 generatedVerticesCount += 4;
2233 }
Romain Guy7b631422012-04-04 11:38:54 -07002234
Chet Haase99585ad2011-05-02 15:00:16 -07002235 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2236 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2237 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002238 }
2239 }
Romain Guy7b631422012-04-04 11:38:54 -07002240
Chet Haase5b0200b2011-04-13 17:58:08 -07002241 if (generatedVerticesCount > 0) {
2242 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2243 }
Romain Guy7b631422012-04-04 11:38:54 -07002244
2245 if (isAA) {
2246 finishDrawAALine(widthSlot, lengthSlot);
2247 }
Chet Haase48659092012-05-31 15:21:51 -07002248
2249 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002250}
2251
Chet Haase48659092012-05-31 15:21:51 -07002252status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2253 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002254
2255 // TODO: The paint's cap style defines whether the points are square or circular
2256 // TODO: Handle AA for round points
2257
Chet Haase5b0200b2011-04-13 17:58:08 -07002258 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002259 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002260 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002261 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002262 if (isHairLine) {
2263 // Now that we know it's hairline, we can set the effective width, to be used later
2264 strokeWidth = 1.0f;
2265 }
2266 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002267 int alpha;
2268 SkXfermode::Mode mode;
2269 getAlphaAndMode(paint, &alpha, &mode);
2270
2271 int verticesCount = count >> 1;
2272 int generatedVerticesCount = 0;
2273
2274 TextureVertex pointsData[verticesCount];
2275 TextureVertex* vertex = &pointsData[0];
2276
Romain Guy04299382012-07-18 17:15:41 -07002277 // TODO: We should optimize this method to not generate vertices for points
2278 // that lie outside of the clip.
2279 mCaches.enableScissor();
2280
Romain Guyed6fcb02011-03-21 13:11:28 -07002281 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002282 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002283 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002284 setupDrawColor(paint->getColor(), alpha);
2285 setupDrawColorFilter();
2286 setupDrawShader();
2287 setupDrawBlending(mode);
2288 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002289 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002290 setupDrawColorUniforms();
2291 setupDrawColorFilterUniforms();
2292 setupDrawPointUniforms();
2293 setupDrawShaderIdentityUniforms();
2294 setupDrawMesh(vertex);
2295
2296 for (int i = 0; i < count; i += 2) {
2297 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2298 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002299
Chet Haase8a5cc922011-04-26 07:28:09 -07002300 float left = points[i] - halfWidth;
2301 float right = points[i] + halfWidth;
2302 float top = points[i + 1] - halfWidth;
2303 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002304
Chet Haase8a5cc922011-04-26 07:28:09 -07002305 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002306 }
2307
2308 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002309
2310 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002311}
2312
Chet Haase48659092012-05-31 15:21:51 -07002313status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002314 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002315 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002316
Romain Guyae88e5e2010-10-22 17:49:18 -07002317 Rect& clip(*mSnapshot->clipRect);
2318 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002319
Romain Guy3d58c032010-07-14 16:34:53 -07002320 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002321
2322 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002323}
Romain Guy9d5316e2010-06-24 19:30:36 -07002324
Chet Haase48659092012-05-31 15:21:51 -07002325status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2326 SkPaint* paint) {
2327 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002328 const AutoTexture autoCleanup(texture);
2329
2330 const float x = left + texture->left - texture->offset;
2331 const float y = top + texture->top - texture->offset;
2332
2333 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002334
2335 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002336}
2337
Chet Haase48659092012-05-31 15:21:51 -07002338status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002339 float rx, float ry, SkPaint* p) {
2340 if (mSnapshot->isIgnored() || quickReject(left, top, right, bottom)) {
2341 return DrawGlInfo::kStatusDone;
2342 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002343
Chris Craik710f46d2012-09-17 17:25:49 -07002344 if (p->getStyle() != SkPaint::kFill_Style) {
2345 mCaches.activeTexture(0);
2346 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2347 right - left, bottom - top, rx, ry, p);
2348 return drawShape(left, top, texture, p);
2349 }
2350
2351 SkPath path;
2352 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2353 path.addRoundRect(rect, rx, ry);
2354 drawConvexPath(path, p->getColor(), getXfermode(p->getXfermode()), p->isAntiAlias());
2355
2356 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002357}
2358
Chris Craik710f46d2012-09-17 17:25:49 -07002359status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
2360 if (mSnapshot->isIgnored() || quickReject(x - radius, y - radius, x + radius, y + radius)) {
2361 return DrawGlInfo::kStatusDone;
2362 }
Romain Guy01d58e42011-01-19 21:54:02 -08002363
Chris Craik710f46d2012-09-17 17:25:49 -07002364 if (p->getStyle() != SkPaint::kFill_Style) {
2365 mCaches.activeTexture(0);
2366 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2367 return drawShape(x - radius, y - radius, texture, p);
2368 }
2369
2370 SkPath path;
2371 path.addCircle(x, y, radius);
2372 drawConvexPath(path, p->getColor(), getXfermode(p->getXfermode()), p->isAntiAlias());
2373
2374 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002375}
Romain Guy01d58e42011-01-19 21:54:02 -08002376
Chet Haase48659092012-05-31 15:21:51 -07002377status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002378 SkPaint* p) {
2379 if (mSnapshot->isIgnored() || quickReject(left, top, right, bottom)) {
2380 return DrawGlInfo::kStatusDone;
2381 }
Romain Guy01d58e42011-01-19 21:54:02 -08002382
Chris Craik710f46d2012-09-17 17:25:49 -07002383 if (p->getStyle() != SkPaint::kFill_Style) {
2384 mCaches.activeTexture(0);
2385 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2386 return drawShape(left, top, texture, p);
2387 }
2388
2389 SkPath path;
2390 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2391 path.addOval(rect);
2392 drawConvexPath(path, p->getColor(), getXfermode(p->getXfermode()), p->isAntiAlias());
2393
2394 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002395}
2396
Chet Haase48659092012-05-31 15:21:51 -07002397status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002398 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002399 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002400
2401 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002402 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002403 }
2404
Romain Guya1d3c912011-12-13 14:55:06 -08002405 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002406 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2407 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002408 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002409}
2410
Chet Haase48659092012-05-31 15:21:51 -07002411status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craik710f46d2012-09-17 17:25:49 -07002412 if (mSnapshot->isIgnored() || quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002413 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002414 }
2415
Chris Craik710f46d2012-09-17 17:25:49 -07002416 if (p->getStyle() != SkPaint::kFill_Style) {
2417 mCaches.activeTexture(0);
2418 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2419 return drawShape(left, top, texture, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002420 }
2421
Romain Guy181d0a62011-06-09 18:52:38 -07002422 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002423 SkPath path;
2424 path.addRect(left, top, right, bottom);
2425 drawConvexPath(path, p->getColor(), getXfermode(p->getXfermode()), true);
Chet Haase858aa932011-05-12 09:06:00 -07002426 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002427 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chet Haase858aa932011-05-12 09:06:00 -07002428 }
Chet Haase48659092012-05-31 15:21:51 -07002429
2430 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002431}
Romain Guy9d5316e2010-06-24 19:30:36 -07002432
Raph Levien416a8472012-07-19 22:48:17 -07002433void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2434 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2435 float x, float y) {
2436 mCaches.activeTexture(0);
2437
2438 // NOTE: The drop shadow will not perform gamma correction
2439 // if shader-based correction is enabled
2440 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2441 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2442 paint, text, bytesCount, count, mShadowRadius, positions);
2443 const AutoTexture autoCleanup(shadow);
2444
2445 const float sx = x - shadow->left + mShadowDx;
2446 const float sy = y - shadow->top + mShadowDy;
2447
2448 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2449 int shadowColor = mShadowColor;
2450 if (mShader) {
2451 shadowColor = 0xffffffff;
2452 }
2453
2454 setupDraw();
2455 setupDrawWithTexture(true);
2456 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2457 setupDrawColorFilter();
2458 setupDrawShader();
2459 setupDrawBlending(true, mode);
2460 setupDrawProgram();
2461 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2462 setupDrawTexture(shadow->id);
2463 setupDrawPureColorUniforms();
2464 setupDrawColorFilterUniforms();
2465 setupDrawShaderUniforms();
2466 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2467
2468 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2469}
2470
Chet Haase48659092012-05-31 15:21:51 -07002471status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002472 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002473 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002474 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002475 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002476 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002477
Romain Guy671d6cf2012-01-18 12:39:17 -08002478 // NOTE: Skia does not support perspective transform on drawPosText yet
2479 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002480 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002481 }
2482
2483 float x = 0.0f;
2484 float y = 0.0f;
2485 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2486 if (pureTranslate) {
2487 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2488 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2489 }
2490
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002491 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002492 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2493 paint->getTextSize());
2494
2495 int alpha;
2496 SkXfermode::Mode mode;
2497 getAlphaAndMode(paint, &alpha, &mode);
2498
Raph Levien416a8472012-07-19 22:48:17 -07002499 if (CC_UNLIKELY(mHasShadow)) {
2500 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2501 0.0f, 0.0f);
2502 }
2503
Romain Guy671d6cf2012-01-18 12:39:17 -08002504 // Pick the appropriate texture filtering
2505 bool linearFilter = mSnapshot->transform->changesBounds();
2506 if (pureTranslate && !linearFilter) {
2507 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2508 }
2509
2510 mCaches.activeTexture(0);
2511 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002512 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002513 setupDrawDirtyRegionsDisabled();
2514 setupDrawWithTexture(true);
2515 setupDrawAlpha8Color(paint->getColor(), alpha);
2516 setupDrawColorFilter();
2517 setupDrawShader();
2518 setupDrawBlending(true, mode);
2519 setupDrawProgram();
2520 setupDrawModelView(x, y, x, y, pureTranslate, true);
2521 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2522 setupDrawPureColorUniforms();
2523 setupDrawColorFilterUniforms();
2524 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002525 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002526
2527 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2528 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2529
Romain Guy211370f2012-02-01 16:10:55 -08002530 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002531
2532 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2533 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002534 if (hasActiveLayer) {
2535 if (!pureTranslate) {
2536 mSnapshot->transform->mapRect(bounds);
2537 }
2538 dirtyLayerUnchecked(bounds, getRegion());
2539 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002540 }
Chet Haase48659092012-05-31 15:21:51 -07002541
2542 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002543}
2544
Romain Guyc2525952012-07-27 16:41:22 -07002545status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002546 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002547 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002548 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002549 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002550 }
2551
Chet Haasea1cff502012-02-21 13:43:44 -08002552 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002553 switch (paint->getTextAlign()) {
2554 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002555 x -= length / 2.0f;
2556 break;
2557 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002558 x -= length;
2559 break;
2560 default:
2561 break;
2562 }
2563
Romain Guycac5fd32011-12-01 20:08:50 -08002564 SkPaint::FontMetrics metrics;
2565 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002566 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002567 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002568 }
2569
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002570 const float oldX = x;
2571 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002572 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002573 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002574 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2575 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2576 }
2577
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002578#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002579 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002580 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002581#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002582
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002583 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002584 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002585 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002586
Romain Guy86568192010-12-14 15:55:39 -08002587 int alpha;
2588 SkXfermode::Mode mode;
2589 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002590
Romain Guy211370f2012-02-01 16:10:55 -08002591 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002592 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2593 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002594 }
2595
Romain Guy6620c6d2010-12-06 18:07:02 -08002596 // Pick the appropriate texture filtering
2597 bool linearFilter = mSnapshot->transform->changesBounds();
2598 if (pureTranslate && !linearFilter) {
2599 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2600 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002601
Romain Guy16c88082012-06-11 16:03:47 -07002602 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002603 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002604 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002605 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002606 setupDrawDirtyRegionsDisabled();
2607 setupDrawWithTexture(true);
2608 setupDrawAlpha8Color(paint->getColor(), alpha);
2609 setupDrawColorFilter();
2610 setupDrawShader();
2611 setupDrawBlending(true, mode);
2612 setupDrawProgram();
2613 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002614 // See comment above; the font renderer must use texture unit 0
2615 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002616 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2617 setupDrawPureColorUniforms();
2618 setupDrawColorFilterUniforms();
2619 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002620 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002621
Romain Guy6620c6d2010-12-06 18:07:02 -08002622 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002623 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2624
Romain Guy211370f2012-02-01 16:10:55 -08002625 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002626
Raph Levien996e57c2012-07-23 15:22:52 -07002627 bool status;
Raph Levien8b4072d2012-07-30 15:50:00 -07002628 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
2629 SkPaint paintCopy(*paint);
2630 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2631 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Raph Levien996e57c2012-07-23 15:22:52 -07002632 positions, hasActiveLayer ? &bounds : NULL);
2633 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002634 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2635 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002636 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002637
2638 if (status && hasActiveLayer) {
2639 if (!pureTranslate) {
2640 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002641 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002642 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002643 }
Romain Guy694b5192010-07-21 21:33:20 -07002644
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002645 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002646
2647 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002648}
2649
Chet Haase48659092012-05-31 15:21:51 -07002650status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002651 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002652 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2653 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002654 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002655 }
2656
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002657 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002658 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2659 paint->getTextSize());
2660
2661 int alpha;
2662 SkXfermode::Mode mode;
2663 getAlphaAndMode(paint, &alpha, &mode);
2664
2665 mCaches.activeTexture(0);
2666 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002667 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002668 setupDrawDirtyRegionsDisabled();
2669 setupDrawWithTexture(true);
2670 setupDrawAlpha8Color(paint->getColor(), alpha);
2671 setupDrawColorFilter();
2672 setupDrawShader();
2673 setupDrawBlending(true, mode);
2674 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002675 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002676 setupDrawTexture(fontRenderer.getTexture(true));
2677 setupDrawPureColorUniforms();
2678 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002679 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002680 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002681
Romain Guy97771732012-02-28 18:17:02 -08002682 const Rect* clip = &mSnapshot->getLocalClip();
2683 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 -08002684
Romain Guy97771732012-02-28 18:17:02 -08002685 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002686
2687 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2688 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002689 if (hasActiveLayer) {
2690 mSnapshot->transform->mapRect(bounds);
2691 dirtyLayerUnchecked(bounds, getRegion());
2692 }
Romain Guy97771732012-02-28 18:17:02 -08002693 }
Chet Haase48659092012-05-31 15:21:51 -07002694
2695 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002696}
2697
Chet Haase48659092012-05-31 15:21:51 -07002698status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2699 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002700
Romain Guya1d3c912011-12-13 14:55:06 -08002701 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002702
Romain Guy33f6beb2012-02-16 19:24:51 -08002703 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002704 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002705 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002706 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002707
Romain Guy8b55f372010-08-18 17:10:07 -07002708 const float x = texture->left - texture->offset;
2709 const float y = texture->top - texture->offset;
2710
Romain Guy01d58e42011-01-19 21:54:02 -08002711 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002712
2713 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002714}
2715
Chet Haase48659092012-05-31 15:21:51 -07002716status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002717 if (!layer) {
2718 return DrawGlInfo::kStatusDone;
2719 }
2720
2721 Rect transformed;
2722 Rect clip;
2723 const bool rejected = quickRejectNoScissor(x, y,
2724 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2725
2726 if (rejected) {
Chet Haase48659092012-05-31 15:21:51 -07002727 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002728 }
2729
Romain Guy4ff0cf42012-08-06 14:51:10 -07002730 bool debugLayerUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -07002731 if (updateLayer(layer, true)) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002732 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002733 }
2734
Romain Guy87e2f7572012-09-24 11:37:12 -07002735 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002736 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002737
Romain Guy211370f2012-02-01 16:10:55 -08002738 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guye529ece2012-09-26 11:23:17 -07002739 SkiaColorFilter* oldFilter = mColorFilter;
2740 mColorFilter = layer->getColorFilter();
2741
Romain Guyc88e3572011-01-22 00:32:12 -08002742 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002743 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002744 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002745 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002746 setupDraw();
2747 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002748 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002749 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002750 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002751 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002752 setupDrawPureColorUniforms();
2753 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002754 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002755 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002756 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2757 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002758
Romain Guyd21b6e12011-11-30 20:21:23 -08002759 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002760 setupDrawModelViewTranslate(tx, ty,
2761 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002762 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002763 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002764 setupDrawModelViewTranslate(x, y,
2765 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2766 }
Romain Guyc88e3572011-01-22 00:32:12 -08002767 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002768
Romain Guyc88e3572011-01-22 00:32:12 -08002769 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2770 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002771
Romain Guyc88e3572011-01-22 00:32:12 -08002772 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002773
2774#if DEBUG_LAYERS_AS_REGIONS
2775 drawRegionRects(layer->region);
2776#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002777 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002778
Romain Guye529ece2012-09-26 11:23:17 -07002779 mColorFilter = oldFilter;
2780
Romain Guy4ff0cf42012-08-06 14:51:10 -07002781 if (debugLayerUpdate) {
2782 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2783 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2784 }
Romain Guyf219da52011-01-16 12:54:25 -08002785 }
Chet Haase48659092012-05-31 15:21:51 -07002786
2787 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002788}
2789
Romain Guy6926c722010-07-12 20:20:03 -07002790///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002791// Shaders
2792///////////////////////////////////////////////////////////////////////////////
2793
2794void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002795 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002796}
2797
Romain Guy06f96e22010-07-30 19:18:16 -07002798void OpenGLRenderer::setupShader(SkiaShader* shader) {
2799 mShader = shader;
2800 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002801 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002802 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002803}
2804
Romain Guyd27977d2010-07-14 19:18:51 -07002805///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002806// Color filters
2807///////////////////////////////////////////////////////////////////////////////
2808
2809void OpenGLRenderer::resetColorFilter() {
2810 mColorFilter = NULL;
2811}
2812
2813void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2814 mColorFilter = filter;
2815}
2816
2817///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002818// Drop shadow
2819///////////////////////////////////////////////////////////////////////////////
2820
2821void OpenGLRenderer::resetShadow() {
2822 mHasShadow = false;
2823}
2824
2825void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2826 mHasShadow = true;
2827 mShadowRadius = radius;
2828 mShadowDx = dx;
2829 mShadowDy = dy;
2830 mShadowColor = color;
2831}
2832
2833///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002834// Draw filters
2835///////////////////////////////////////////////////////////////////////////////
2836
2837void OpenGLRenderer::resetPaintFilter() {
2838 mHasDrawFilter = false;
2839}
2840
2841void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2842 mHasDrawFilter = true;
2843 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2844 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2845}
2846
2847SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002848 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002849
2850 uint32_t flags = paint->getFlags();
2851
2852 mFilteredPaint = *paint;
2853 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2854
2855 return &mFilteredPaint;
2856}
2857
2858///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002859// Drawing implementation
2860///////////////////////////////////////////////////////////////////////////////
2861
Romain Guy01d58e42011-01-19 21:54:02 -08002862void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2863 float x, float y, SkPaint* paint) {
2864 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2865 return;
2866 }
2867
2868 int alpha;
2869 SkXfermode::Mode mode;
2870 getAlphaAndMode(paint, &alpha, &mode);
2871
2872 setupDraw();
2873 setupDrawWithTexture(true);
2874 setupDrawAlpha8Color(paint->getColor(), alpha);
2875 setupDrawColorFilter();
2876 setupDrawShader();
2877 setupDrawBlending(true, mode);
2878 setupDrawProgram();
2879 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2880 setupDrawTexture(texture->id);
2881 setupDrawPureColorUniforms();
2882 setupDrawColorFilterUniforms();
2883 setupDrawShaderUniforms();
2884 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2885
2886 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2887
2888 finishDrawTexture();
2889}
2890
Romain Guyf607bdc2010-09-10 19:20:06 -07002891// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002892#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2893#define kStdUnderline_Offset (1.0f / 9.0f)
2894#define kStdUnderline_Thickness (1.0f / 18.0f)
2895
2896void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2897 float x, float y, SkPaint* paint) {
2898 // Handle underline and strike-through
2899 uint32_t flags = paint->getFlags();
2900 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002901 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002902 float underlineWidth = length;
2903 // If length is > 0.0f, we already measured the text for the text alignment
2904 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002905 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002906 }
2907
Romain Guy211370f2012-02-01 16:10:55 -08002908 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002909 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002910 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002911
Raph Levien8b4072d2012-07-30 15:50:00 -07002912 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002913 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002914
Romain Guyf6834472011-01-23 13:32:12 -08002915 int linesCount = 0;
2916 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2917 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2918
2919 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002920 float points[pointsCount];
2921 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002922
2923 if (flags & SkPaint::kUnderlineText_Flag) {
2924 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002925 points[currentPoint++] = left;
2926 points[currentPoint++] = top;
2927 points[currentPoint++] = left + underlineWidth;
2928 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002929 }
2930
2931 if (flags & SkPaint::kStrikeThruText_Flag) {
2932 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002933 points[currentPoint++] = left;
2934 points[currentPoint++] = top;
2935 points[currentPoint++] = left + underlineWidth;
2936 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002937 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002938
Romain Guy726aeba2011-06-01 14:52:00 -07002939 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002940
Romain Guy726aeba2011-06-01 14:52:00 -07002941 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002942 }
2943 }
2944}
2945
Romain Guy026c5e162010-06-28 17:12:22 -07002946void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002947 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002948 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002949 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002950 color |= 0x00ffffff;
2951 }
2952
Romain Guy70ca14e2010-12-13 18:24:33 -08002953 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002954 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002955 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002956 setupDrawShader();
2957 setupDrawColorFilter();
2958 setupDrawBlending(mode);
2959 setupDrawProgram();
2960 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2961 setupDrawColorUniforms();
2962 setupDrawShaderUniforms(ignoreTransform);
2963 setupDrawColorFilterUniforms();
2964 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002965
Romain Guyc95c8d62010-09-17 15:31:32 -07002966 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2967}
2968
Romain Guy82ba8142010-07-09 13:25:56 -07002969void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002970 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002971 int alpha;
2972 SkXfermode::Mode mode;
2973 getAlphaAndMode(paint, &alpha, &mode);
2974
Romain Guyd21b6e12011-11-30 20:21:23 -08002975 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002976
Romain Guy211370f2012-02-01 16:10:55 -08002977 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002978 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2979 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2980
Romain Guyd21b6e12011-11-30 20:21:23 -08002981 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002982 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2983 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2984 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2985 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002986 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002987 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2988 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2989 GL_TRIANGLE_STRIP, gMeshCount);
2990 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002991}
2992
Romain Guybd6b79b2010-06-26 00:13:53 -07002993void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002994 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2995 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002996 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002997}
2998
2999void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003000 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003001 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003002 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003003
Romain Guy746b7402010-10-26 16:27:31 -07003004 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003005 setupDrawWithTexture();
3006 setupDrawColor(alpha, alpha, alpha, alpha);
3007 setupDrawColorFilter();
3008 setupDrawBlending(blend, mode, swapSrcDst);
3009 setupDrawProgram();
3010 if (!dirty) {
3011 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07003012 }
Romain Guy5b3b3522010-10-27 18:57:51 -07003013 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003014 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003015 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003016 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003017 }
Romain Guy86568192010-12-14 15:55:39 -08003018 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003019 setupDrawColorFilterUniforms();
3020 setupDrawTexture(texture);
3021 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003022
Romain Guy6820ac82010-09-15 18:11:50 -07003023 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003024
3025 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003026}
3027
Romain Guya5aed0d2010-09-09 14:42:43 -07003028void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003029 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003030 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003031
Romain Guy82ba8142010-07-09 13:25:56 -07003032 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003033 // These blend modes are not supported by OpenGL directly and have
3034 // to be implemented using shaders. Since the shader will perform
3035 // the blending, turn blending off here
3036 // If the blend mode cannot be implemented using shaders, fall
3037 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003038 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08003039 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003040 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003041 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003042
Romain Guy82bc7a72012-01-03 14:13:39 -08003043 if (mCaches.blend) {
3044 glDisable(GL_BLEND);
3045 mCaches.blend = false;
3046 }
3047
3048 return;
3049 } else {
3050 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003051 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003052 }
3053
3054 if (!mCaches.blend) {
3055 glEnable(GL_BLEND);
3056 }
3057
3058 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3059 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3060
3061 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3062 glBlendFunc(sourceMode, destMode);
3063 mCaches.lastSrcMode = sourceMode;
3064 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003065 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003066 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003067 glDisable(GL_BLEND);
3068 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003069 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003070}
3071
Romain Guy889f8d12010-07-29 14:37:42 -07003072bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003073 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003074 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003075 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003076 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003077 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003078 }
Romain Guy6926c722010-07-12 20:20:03 -07003079 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003080}
3081
Romain Guy026c5e162010-06-28 17:12:22 -07003082void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003083 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003084 TextureVertex::setUV(v++, u1, v1);
3085 TextureVertex::setUV(v++, u2, v1);
3086 TextureVertex::setUV(v++, u1, v2);
3087 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003088}
3089
Chet Haase5c13d892010-10-08 08:37:55 -07003090void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003091 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003092 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003093}
3094
Romain Guy9d5316e2010-06-24 19:30:36 -07003095}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003096}; // namespace android