blob: 35cc7160fc31cdd73b185c79d4dfeb0cbadd3ceb [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 Guy2b7028e2012-09-19 17:25:38 -0700196 mTilingSnapshot = mSnapshot;
Romain Guy57b52682012-09-20 17:38:46 -0700197 startTiling(mTilingSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700198
Romain Guy7c450aa2012-09-21 19:15:00 -0700199 debugOverdraw(true, true);
200
Romain Guy6b7bd242010-10-06 19:49:23 -0700201 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700202 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700203 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700204 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700205 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700206 } else {
207 mCaches.resetScissor();
208 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700209
210 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700211}
212
213void OpenGLRenderer::syncState() {
214 glViewport(0, 0, mWidth, mHeight);
215
216 if (mCaches.blend) {
217 glEnable(GL_BLEND);
218 } else {
219 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700220 }
Romain Guybb9524b2010-06-22 18:56:38 -0700221}
222
Romain Guy57b52682012-09-20 17:38:46 -0700223void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700224 Rect* clip = mTilingSnapshot->clipRect;
Romain Guy2b7028e2012-09-19 17:25:38 -0700225 if (s->flags & Snapshot::kFlagIsFboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700226 clip = s->clipRect;
227 }
228
229 mCaches.startTiling(clip->left, s->height - clip->bottom,
230 clip->right - clip->left, clip->bottom - clip->top, opaque);
231}
232
233void OpenGLRenderer::endTiling() {
234 mCaches.endTiling();
235}
236
Romain Guyb025b9c2010-09-16 14:16:48 -0700237void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700238 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700239 endTiling();
240
Romain Guy11cb6422012-09-21 00:39:43 -0700241 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700242#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700243 GLenum status = GL_NO_ERROR;
244 while ((status = glGetError()) != GL_NO_ERROR) {
245 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
246 switch (status) {
247 case GL_INVALID_ENUM:
248 ALOGE(" GL_INVALID_ENUM");
249 break;
250 case GL_INVALID_VALUE:
251 ALOGE(" GL_INVALID_VALUE");
252 break;
253 case GL_INVALID_OPERATION:
254 ALOGE(" GL_INVALID_OPERATION");
255 break;
256 case GL_OUT_OF_MEMORY:
257 ALOGE(" Out of memory!");
258 break;
259 }
Romain Guya07105b2011-01-10 21:14:18 -0800260 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700261#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700262
Romain Guyc15008e2010-11-10 11:59:15 -0800263#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800264 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700265#else
266 if (mCaches.getDebugLevel() & kDebugMemory) {
267 mCaches.dumpMemoryUsage();
268 }
Romain Guyc15008e2010-11-10 11:59:15 -0800269#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700270 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700271}
272
Romain Guy6c319ca2011-01-11 14:29:25 -0800273void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700274 if (mCaches.currentProgram) {
275 if (mCaches.currentProgram->isInUse()) {
276 mCaches.currentProgram->remove();
277 mCaches.currentProgram = NULL;
278 }
279 }
Romain Guy50c0f092010-10-19 11:42:22 -0700280 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800281 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800282 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800283 mCaches.disbaleTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700284 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700285}
286
Romain Guy6c319ca2011-01-11 14:29:25 -0800287void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800288 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800289 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700290 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700291 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700292
Romain Guy3e263fa2011-12-12 16:47:48 -0800293 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
294
Chet Haase80250612012-08-15 13:46:54 -0700295 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700296 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800297 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700298 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700299
Romain Guya1d3c912011-12-13 14:55:06 -0800300 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700301
Romain Guy50c0f092010-10-19 11:42:22 -0700302 mCaches.blend = true;
303 glEnable(GL_BLEND);
304 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
305 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700306}
307
Romain Guy35643dd2012-09-18 15:40:58 -0700308void OpenGLRenderer::resumeAfterLayer() {
309 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
310 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
311 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700312 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700313
314 mCaches.resetScissor();
315 dirtyClip();
316}
317
Romain Guyba6be8a2012-04-23 18:22:09 -0700318void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700319 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700320}
321
322void OpenGLRenderer::attachFunctor(Functor* functor) {
323 mFunctors.add(functor);
324}
325
Romain Guy8f3b8e32012-03-27 16:33:45 -0700326status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
327 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700328 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700329
Romain Guyba6be8a2012-04-23 18:22:09 -0700330 if (count > 0) {
331 SortedVector<Functor*> functors(mFunctors);
332 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700333
Romain Guyba6be8a2012-04-23 18:22:09 -0700334 DrawGlInfo info;
335 info.clipLeft = 0;
336 info.clipTop = 0;
337 info.clipRight = 0;
338 info.clipBottom = 0;
339 info.isLayer = false;
340 info.width = 0;
341 info.height = 0;
342 memset(info.transform, 0, sizeof(float) * 16);
343
344 for (size_t i = 0; i < count; i++) {
345 Functor* f = functors.itemAt(i);
346 result |= (*f)(DrawGlInfo::kModeProcess, &info);
347
Chris Craikc2c95432012-04-25 15:13:52 -0700348 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700349 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
350 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700351 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700352
Chris Craikc2c95432012-04-25 15:13:52 -0700353 if (result & DrawGlInfo::kStatusInvoke) {
354 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700355 }
356 }
Chet Haasebeb8bd02012-09-09 16:13:26 -0700357 // protect against functors binding to other buffers
358 mCaches.unbindMeshBuffer();
359 mCaches.unbindIndicesBuffer();
360 mCaches.activeTexture(0);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700361 }
362
363 return result;
364}
365
366status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800367 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700368 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700369
Romain Guy8a4ac612012-07-17 17:32:48 -0700370 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800371 if (mDirtyClip) {
372 setScissorFromClip();
373 }
Romain Guyd643bb52011-03-01 14:55:21 -0800374
Romain Guy80911b82011-03-16 15:30:12 -0700375 Rect clip(*mSnapshot->clipRect);
376 clip.snapToPixelBoundaries();
377
Romain Guyd643bb52011-03-01 14:55:21 -0800378 // Since we don't know what the functor will draw, let's dirty
379 // tne entire clip region
380 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800381 dirtyLayerUnchecked(clip, getRegion());
382 }
Romain Guyd643bb52011-03-01 14:55:21 -0800383
Romain Guy08aa2cb2011-03-17 11:06:57 -0700384 DrawGlInfo info;
385 info.clipLeft = clip.left;
386 info.clipTop = clip.top;
387 info.clipRight = clip.right;
388 info.clipBottom = clip.bottom;
389 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700390 info.width = getSnapshot()->viewport.getWidth();
391 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700392 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700393
Chet Haase48659092012-05-31 15:21:51 -0700394 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800395
Romain Guy8f3b8e32012-03-27 16:33:45 -0700396 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700397 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800398 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700399
Chris Craik65924a32012-04-05 17:52:11 -0700400 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700401 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700402 }
Romain Guycabfcc12011-03-07 18:06:46 -0800403 }
404
Chet Haasedaf98e92011-01-10 14:10:36 -0800405 resume();
Romain Guy65549432012-03-26 16:45:05 -0700406 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800407}
408
Romain Guyf6a11b82010-06-23 17:47:49 -0700409///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700410// Debug
411///////////////////////////////////////////////////////////////////////////////
412
413void OpenGLRenderer::startMark(const char* name) const {
414 mCaches.startMark(0, name);
415}
416
417void OpenGLRenderer::endMark() const {
418 mCaches.endMark();
419}
420
421void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
422 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
423 if (clear) {
424 mCaches.disableScissor();
425 mCaches.stencil.clear();
426 }
427 if (enable) {
428 mCaches.stencil.enableDebugWrite();
429 } else {
430 mCaches.stencil.disable();
431 }
432 }
433}
434
435void OpenGLRenderer::renderOverdraw() {
436 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
437 const Rect* clip = mTilingSnapshot->clipRect;
438
439 mCaches.enableScissor();
440 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
441 clip->right - clip->left, clip->bottom - clip->top);
442
443 mCaches.stencil.enableDebugTest(2);
444 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
445 mCaches.stencil.enableDebugTest(3);
446 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
447 mCaches.stencil.enableDebugTest(4);
448 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
449 mCaches.stencil.enableDebugTest(4, true);
450 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
451 mCaches.stencil.disable();
452 }
453}
454
455///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700456// Layers
457///////////////////////////////////////////////////////////////////////////////
458
459bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
460 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
461 OpenGLRenderer* renderer = layer->renderer;
462 Rect& dirty = layer->dirtyRect;
463
Romain Guy7c450aa2012-09-21 19:15:00 -0700464 if (inFrame) {
465 endTiling();
466 debugOverdraw(false, false);
467 }
Romain Guy11cb6422012-09-21 00:39:43 -0700468
469 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
470 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
471 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
472 renderer->finish();
473
474 if (inFrame) {
475 resumeAfterLayer();
476 startTiling(mSnapshot);
477 }
478
479 dirty.setEmpty();
480 layer->deferredUpdateScheduled = false;
481 layer->renderer = NULL;
482 layer->displayList = NULL;
483
484 return true;
485 }
486
487 return false;
488}
489
490void OpenGLRenderer::updateLayers() {
491 int count = mLayerUpdates.size();
492 if (count > 0) {
493 startMark("Layer Updates");
494
495 // Note: it is very important to update the layers in reverse order
496 for (int i = count - 1; i >= 0; i--) {
497 Layer* layer = mLayerUpdates.itemAt(i);
498 updateLayer(layer, false);
499 mCaches.resourceCache.decrementRefcount(layer);
500 }
501 mLayerUpdates.clear();
502
503 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
504 endMark();
505 }
506}
507
508void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
509 if (layer) {
510 mLayerUpdates.push_back(layer);
511 mCaches.resourceCache.incrementRefcount(layer);
512 }
513}
514
515void OpenGLRenderer::clearLayerUpdates() {
516 size_t count = mLayerUpdates.size();
517 if (count > 0) {
518 mCaches.resourceCache.lock();
519 for (size_t i = 0; i < count; i++) {
520 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
521 }
522 mCaches.resourceCache.unlock();
523 mLayerUpdates.clear();
524 }
525}
526
527///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700528// State management
529///////////////////////////////////////////////////////////////////////////////
530
Romain Guybb9524b2010-06-22 18:56:38 -0700531int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700532 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700533}
534
535int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700536 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700537}
538
539void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700540 if (mSaveCount > 1) {
541 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700542 }
Romain Guybb9524b2010-06-22 18:56:38 -0700543}
544
545void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700546 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700547
Romain Guy8fb95422010-08-17 18:38:51 -0700548 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700549 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700550 }
Romain Guybb9524b2010-06-22 18:56:38 -0700551}
552
Romain Guy8aef54f2010-09-01 15:13:49 -0700553int OpenGLRenderer::saveSnapshot(int flags) {
554 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700555 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700556}
557
558bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700559 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700560 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700561 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700562
Romain Guybd6b79b2010-06-26 00:13:53 -0700563 sp<Snapshot> current = mSnapshot;
564 sp<Snapshot> previous = mSnapshot->previous;
565
Romain Guyeb993562010-10-05 18:14:38 -0700566 if (restoreOrtho) {
567 Rect& r = previous->viewport;
568 glViewport(r.left, r.top, r.right, r.bottom);
569 mOrthoMatrix.load(current->orthoMatrix);
570 }
571
Romain Guy8b55f372010-08-18 17:10:07 -0700572 mSaveCount--;
573 mSnapshot = previous;
574
Romain Guy2542d192010-08-18 11:47:12 -0700575 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700576 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700577 }
Romain Guy2542d192010-08-18 11:47:12 -0700578
Romain Guy5ec99242010-11-03 16:19:08 -0700579 if (restoreLayer) {
580 composeLayer(current, previous);
581 }
582
Romain Guy2542d192010-08-18 11:47:12 -0700583 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700584}
585
Romain Guyf6a11b82010-06-23 17:47:49 -0700586///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700587// Layers
588///////////////////////////////////////////////////////////////////////////////
589
590int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700591 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700592 const GLuint previousFbo = mSnapshot->fbo;
593 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700594
Romain Guyaf636eb2010-12-09 17:47:21 -0800595 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700596 int alpha = 255;
597 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700598
Romain Guye45362c2010-11-03 19:58:32 -0700599 if (p) {
600 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700601 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700602 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700603 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700604 }
Romain Guyd55a8612010-06-28 17:42:46 -0700605
Chet Haased48885a2012-08-28 17:43:28 -0700606 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700607 }
Romain Guyd55a8612010-06-28 17:42:46 -0700608
609 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700610}
611
612int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
613 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700614 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700615 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700616 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700617 SkPaint paint;
618 paint.setAlpha(alpha);
619 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700620 }
Romain Guyd55a8612010-06-28 17:42:46 -0700621}
Romain Guybd6b79b2010-06-26 00:13:53 -0700622
Romain Guy1c740bc2010-09-13 18:00:09 -0700623/**
624 * Layers are viewed by Skia are slightly different than layers in image editing
625 * programs (for instance.) When a layer is created, previously created layers
626 * and the frame buffer still receive every drawing command. For instance, if a
627 * layer is created and a shape intersecting the bounds of the layers and the
628 * framebuffer is draw, the shape will be drawn on both (unless the layer was
629 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
630 *
631 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
632 * texture. Unfortunately, this is inefficient as it requires every primitive to
633 * be drawn n + 1 times, where n is the number of active layers. In practice this
634 * means, for every primitive:
635 * - Switch active frame buffer
636 * - Change viewport, clip and projection matrix
637 * - Issue the drawing
638 *
639 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700640 * To avoid this, layers are implemented in a different way here, at least in the
641 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
642 * is set. When this flag is set we can redirect all drawing operations into a
643 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700644 *
645 * This implementation relies on the frame buffer being at least RGBA 8888. When
646 * a layer is created, only a texture is created, not an FBO. The content of the
647 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700648 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700649 * buffer and drawing continues as normal. This technique therefore treats the
650 * frame buffer as a scratch buffer for the layers.
651 *
652 * To compose the layers back onto the frame buffer, each layer texture
653 * (containing the original frame buffer data) is drawn as a simple quad over
654 * the frame buffer. The trick is that the quad is set as the composition
655 * destination in the blending equation, and the frame buffer becomes the source
656 * of the composition.
657 *
658 * Drawing layers with an alpha value requires an extra step before composition.
659 * An empty quad is drawn over the layer's region in the frame buffer. This quad
660 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
661 * quad is used to multiply the colors in the frame buffer. This is achieved by
662 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
663 * GL_ZERO, GL_SRC_ALPHA.
664 *
665 * Because glCopyTexImage2D() can be slow, an alternative implementation might
666 * be use to draw a single clipped layer. The implementation described above
667 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700668 *
669 * (1) The frame buffer is actually not cleared right away. To allow the GPU
670 * to potentially optimize series of calls to glCopyTexImage2D, the frame
671 * buffer is left untouched until the first drawing operation. Only when
672 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700673 */
Chet Haased48885a2012-08-28 17:43:28 -0700674bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
675 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700676 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700677 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700678
Romain Guyeb993562010-10-05 18:14:38 -0700679 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
680
Romain Guyf607bdc2010-09-10 19:20:06 -0700681 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700682 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700683 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700684 Rect untransformedBounds(bounds);
685 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700686
Chet Haased48885a2012-08-28 17:43:28 -0700687 // Layers only make sense if they are in the framebuffer's bounds
688 if (bounds.intersect(*mSnapshot->clipRect)) {
689 // We cannot work with sub-pixels in this case
690 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700691
Chet Haased48885a2012-08-28 17:43:28 -0700692 // When the layer is not an FBO, we may use glCopyTexImage so we
693 // need to make sure the layer does not extend outside the bounds
694 // of the framebuffer
695 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700696 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700697 } else if (fboLayer) {
698 clip.set(bounds);
699 mat4 inverse;
700 inverse.loadInverse(*mSnapshot->transform);
701 inverse.mapRect(clip);
702 clip.snapToPixelBoundaries();
703 if (clip.intersect(untransformedBounds)) {
704 clip.translate(-left, -top);
705 bounds.set(untransformedBounds);
706 } else {
707 clip.setEmpty();
708 }
Romain Guyad37cd32011-03-15 11:12:25 -0700709 }
Chet Haased48885a2012-08-28 17:43:28 -0700710 } else {
711 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700712 }
Romain Guybf434112010-09-16 14:40:17 -0700713
Romain Guy746b7402010-10-26 16:27:31 -0700714 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700715 bounds.getHeight() > mCaches.maxTextureSize ||
716 (fboLayer && clip.isEmpty())) {
717 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700718 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700719 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700720 }
721
722 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700723 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700724 return false;
725 }
Romain Guyf18fd992010-07-08 11:45:51 -0700726
Romain Guya1d3c912011-12-13 14:55:06 -0800727 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700728 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700729 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700730 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700731 }
732
Romain Guy9ace8f52011-07-07 20:50:11 -0700733 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700734 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700735 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
736 bounds.getWidth() / float(layer->getWidth()), 0.0f);
737 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700738 layer->setBlend(true);
Romain Guydda57022010-07-06 11:39:32 -0700739
Romain Guy8fb95422010-08-17 18:38:51 -0700740 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700741 mSnapshot->flags |= Snapshot::kFlagIsLayer;
742 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700743
Romain Guyeb993562010-10-05 18:14:38 -0700744 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700745 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700746 } else {
747 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700748 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800749 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700750 if (layer->isEmpty()) {
751 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700752 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700753 layer->getWidth(), layer->getHeight(), 0);
754 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800755 } else {
756 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700757 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800758 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700759
Romain Guy54be1cd2011-06-13 19:04:27 -0700760 // Enqueue the buffer coordinates to clear the corresponding region later
761 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700762 }
Romain Guyeb993562010-10-05 18:14:38 -0700763 }
Romain Guyf86ef572010-07-01 11:05:42 -0700764
Romain Guyd55a8612010-06-28 17:42:46 -0700765 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700766}
767
Chet Haased48885a2012-08-28 17:43:28 -0700768bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700769 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700770
Chet Haased48885a2012-08-28 17:43:28 -0700771 mSnapshot->region = &mSnapshot->layer->region;
772 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700773
Chet Haased48885a2012-08-28 17:43:28 -0700774 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
775 mSnapshot->fbo = layer->getFbo();
776 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
777 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
778 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
779 mSnapshot->height = bounds.getHeight();
780 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
781 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700782
Romain Guy2b7028e2012-09-19 17:25:38 -0700783 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700784 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700785 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700786 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
787 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700788
789 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700790 if (layer->isEmpty()) {
791 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
792 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700793 }
794
795 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700796 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700797
Romain Guy2b7028e2012-09-19 17:25:38 -0700798 startTiling(mSnapshot);
Romain Guy5b3b3522010-10-27 18:57:51 -0700799
800 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700801 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800802 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700803 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700804 glClear(GL_COLOR_BUFFER_BIT);
805
806 dirtyClip();
807
808 // Change the ortho projection
809 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
810 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
811
812 return true;
813}
814
Romain Guy1c740bc2010-09-13 18:00:09 -0700815/**
816 * Read the documentation of createLayer() before doing anything in this method.
817 */
Romain Guy1d83e192010-08-17 11:37:00 -0700818void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
819 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000820 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700821 return;
822 }
823
Romain Guy5b3b3522010-10-27 18:57:51 -0700824 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700825
826 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700827 endTiling();
828
Romain Guye0aa84b2012-04-03 19:30:26 -0700829 // Detach the texture from the FBO
830 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guyeb993562010-10-05 18:14:38 -0700831 // Unbind current FBO and restore previous one
832 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700833 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700834
835 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700836 }
837
Romain Guy1d83e192010-08-17 11:37:00 -0700838 Layer* layer = current->layer;
839 const Rect& rect = layer->layer;
840
Romain Guy9ace8f52011-07-07 20:50:11 -0700841 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700842 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700843 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700844 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700845 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700846 }
847
Romain Guy03750a02010-10-18 14:06:08 -0700848 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700849
Romain Guya1d3c912011-12-13 14:55:06 -0800850 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700851
Romain Guy5b3b3522010-10-27 18:57:51 -0700852 // When the layer is stored in an FBO, we can save a bit of fillrate by
853 // drawing only the dirty region
854 if (fboLayer) {
855 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700856 if (layer->getColorFilter()) {
857 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800858 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700859 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700860 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800861 resetColorFilter();
862 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700863 } else if (!rect.isEmpty()) {
864 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
865 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700866 }
Romain Guy8b55f372010-08-18 17:10:07 -0700867
Romain Guyeb993562010-10-05 18:14:38 -0700868 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800869 // Note: No need to use glDiscardFramebufferEXT() since we never
870 // create/compose layers that are not on screen with this
871 // code path
872 // See LayerRenderer::destroyLayer(Layer*)
873
Romain Guyeb993562010-10-05 18:14:38 -0700874 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
875 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700876 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700877 }
878
Romain Guy746b7402010-10-26 16:27:31 -0700879 dirtyClip();
880
Romain Guyeb993562010-10-05 18:14:38 -0700881 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700882 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700883 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700884 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700885 }
886}
887
Romain Guyaa6c24c2011-04-28 18:40:04 -0700888void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700889 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700890
Romain Guy302a9df2011-08-16 13:55:02 -0700891 mat4& transform = layer->getTransform();
892 if (!transform.isIdentity()) {
893 save(0);
894 mSnapshot->transform->multiply(transform);
895 }
896
Romain Guyaa6c24c2011-04-28 18:40:04 -0700897 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700898 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700899 setupDrawWithTexture();
900 } else {
901 setupDrawWithExternalTexture();
902 }
903 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700904 setupDrawColor(alpha, alpha, alpha, alpha);
905 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700906 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700907 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700908 setupDrawPureColorUniforms();
909 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700910 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
911 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700912 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700913 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700914 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700915 if (mSnapshot->transform->isPureTranslate() &&
916 layer->getWidth() == (uint32_t) rect.getWidth() &&
917 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700918 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
919 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
920
Romain Guyd21b6e12011-11-30 20:21:23 -0800921 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700922 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
923 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800924 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700925 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
926 }
927 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700928 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
929
930 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
931
932 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700933
934 if (!transform.isIdentity()) {
935 restore();
936 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700937}
938
Romain Guy5b3b3522010-10-27 18:57:51 -0700939void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700940 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700941 const Rect& texCoords = layer->texCoords;
942 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
943 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700944
Romain Guy9ace8f52011-07-07 20:50:11 -0700945 float x = rect.left;
946 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700947 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700948 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700949 layer->getHeight() == (uint32_t) rect.getHeight();
950
951 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700952 // When we're swapping, the layer is already in screen coordinates
953 if (!swap) {
954 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
955 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
956 }
957
Romain Guyd21b6e12011-11-30 20:21:23 -0800958 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700959 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800960 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700961 }
962
963 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
964 layer->getTexture(), layer->getAlpha() / 255.0f,
965 layer->getMode(), layer->isBlend(),
966 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
967 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700968
Romain Guyaa6c24c2011-04-28 18:40:04 -0700969 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
970 } else {
971 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
972 drawTextureLayer(layer, rect);
973 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
974 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700975}
976
977void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700978 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700979 layer->setRegionAsRect();
980
Romain Guy40667672011-03-18 14:34:03 -0700981 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700982
Romain Guy5b3b3522010-10-27 18:57:51 -0700983 layer->region.clear();
984 return;
985 }
986
Romain Guy8a3957d2011-09-07 17:55:15 -0700987 // TODO: See LayerRenderer.cpp::generateMesh() for important
988 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800989 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700990 size_t count;
991 const android::Rect* rects = layer->region.getArray(&count);
992
Romain Guy9ace8f52011-07-07 20:50:11 -0700993 const float alpha = layer->getAlpha() / 255.0f;
994 const float texX = 1.0f / float(layer->getWidth());
995 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800996 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700997
998 TextureVertex* mesh = mCaches.getRegionMesh();
999 GLsizei numQuads = 0;
1000
Romain Guy7230a742011-01-10 22:26:16 -08001001 setupDraw();
1002 setupDrawWithTexture();
1003 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001004 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001005 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001006 setupDrawProgram();
1007 setupDrawDirtyRegionsDisabled();
1008 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001009 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001010 setupDrawTexture(layer->getTexture());
1011 if (mSnapshot->transform->isPureTranslate()) {
1012 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
1013 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
1014
Romain Guyd21b6e12011-11-30 20:21:23 -08001015 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001016 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1017 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001018 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001019 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1020 }
Romain Guy15bc6432011-12-13 13:11:32 -08001021 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001022
1023 for (size_t i = 0; i < count; i++) {
1024 const android::Rect* r = &rects[i];
1025
1026 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001027 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001028 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001029 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001030
1031 // TODO: Reject quads outside of the clip
1032 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1033 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1034 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1035 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1036
1037 numQuads++;
1038
1039 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1040 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1041 numQuads = 0;
1042 mesh = mCaches.getRegionMesh();
1043 }
1044 }
1045
1046 if (numQuads > 0) {
1047 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1048 }
1049
Romain Guy7230a742011-01-10 22:26:16 -08001050 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001051
1052#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001053 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001054#endif
1055
1056 layer->region.clear();
1057 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001058}
1059
Romain Guy3a3133d2011-02-01 22:59:58 -08001060void OpenGLRenderer::drawRegionRects(const Region& region) {
1061#if DEBUG_LAYERS_AS_REGIONS
1062 size_t count;
1063 const android::Rect* rects = region.getArray(&count);
1064
1065 uint32_t colors[] = {
1066 0x7fff0000, 0x7f00ff00,
1067 0x7f0000ff, 0x7fff00ff,
1068 };
1069
1070 int offset = 0;
1071 int32_t top = rects[0].top;
1072
1073 for (size_t i = 0; i < count; i++) {
1074 if (top != rects[i].top) {
1075 offset ^= 0x2;
1076 top = rects[i].top;
1077 }
1078
1079 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1080 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1081 SkXfermode::kSrcOver_Mode);
1082 }
1083#endif
1084}
1085
Romain Guy5b3b3522010-10-27 18:57:51 -07001086void OpenGLRenderer::dirtyLayer(const float left, const float top,
1087 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001088 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001089 Rect bounds(left, top, right, bottom);
1090 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001091 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001092 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001093}
1094
1095void OpenGLRenderer::dirtyLayer(const float left, const float top,
1096 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001097 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001098 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001099 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001100 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001101}
1102
1103void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001104 if (bounds.intersect(*mSnapshot->clipRect)) {
1105 bounds.snapToPixelBoundaries();
1106 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1107 if (!dirty.isEmpty()) {
1108 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001109 }
1110 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001111}
1112
Romain Guy54be1cd2011-06-13 19:04:27 -07001113void OpenGLRenderer::clearLayerRegions() {
1114 const size_t count = mLayers.size();
1115 if (count == 0) return;
1116
1117 if (!mSnapshot->isIgnored()) {
1118 // Doing several glScissor/glClear here can negatively impact
1119 // GPUs with a tiler architecture, instead we draw quads with
1120 // the Clear blending mode
1121
1122 // The list contains bounds that have already been clipped
1123 // against their initial clip rect, and the current clip
1124 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001125 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001126
1127 Vertex mesh[count * 6];
1128 Vertex* vertex = mesh;
1129
1130 for (uint32_t i = 0; i < count; i++) {
1131 Rect* bounds = mLayers.itemAt(i);
1132
1133 Vertex::set(vertex++, bounds->left, bounds->bottom);
1134 Vertex::set(vertex++, bounds->left, bounds->top);
1135 Vertex::set(vertex++, bounds->right, bounds->top);
1136 Vertex::set(vertex++, bounds->left, bounds->bottom);
1137 Vertex::set(vertex++, bounds->right, bounds->top);
1138 Vertex::set(vertex++, bounds->right, bounds->bottom);
1139
1140 delete bounds;
1141 }
1142
1143 setupDraw(false);
1144 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1145 setupDrawBlending(true, SkXfermode::kClear_Mode);
1146 setupDrawProgram();
1147 setupDrawPureColorUniforms();
1148 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001149 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001150
Romain Guy54be1cd2011-06-13 19:04:27 -07001151 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001152
1153 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001154 } else {
1155 for (uint32_t i = 0; i < count; i++) {
1156 delete mLayers.itemAt(i);
1157 }
1158 }
1159
1160 mLayers.clear();
1161}
1162
Romain Guybd6b79b2010-06-26 00:13:53 -07001163///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001164// Transforms
1165///////////////////////////////////////////////////////////////////////////////
1166
1167void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001168 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001169}
1170
1171void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001172 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001173}
1174
1175void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001176 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001177}
1178
Romain Guy807daf72011-01-18 11:19:19 -08001179void OpenGLRenderer::skew(float sx, float sy) {
1180 mSnapshot->transform->skew(sx, sy);
1181}
1182
Romain Guyf6a11b82010-06-23 17:47:49 -07001183void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001184 if (matrix) {
1185 mSnapshot->transform->load(*matrix);
1186 } else {
1187 mSnapshot->transform->loadIdentity();
1188 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001189}
1190
1191void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001192 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001193}
1194
1195void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001196 SkMatrix transform;
1197 mSnapshot->transform->copyTo(transform);
1198 transform.preConcat(*matrix);
1199 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001200}
1201
1202///////////////////////////////////////////////////////////////////////////////
1203// Clipping
1204///////////////////////////////////////////////////////////////////////////////
1205
Romain Guybb9524b2010-06-22 18:56:38 -07001206void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001207 Rect clip(*mSnapshot->clipRect);
1208 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001209
Romain Guy8a4ac612012-07-17 17:32:48 -07001210 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1211 clip.getWidth(), clip.getHeight())) {
1212 mDirtyClip = false;
1213 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001214}
1215
1216const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001217 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001218}
1219
Romain Guy8a4ac612012-07-17 17:32:48 -07001220bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1221 if (mSnapshot->isIgnored()) {
1222 return true;
1223 }
1224
1225 Rect r(left, top, right, bottom);
1226 mSnapshot->transform->mapRect(r);
1227 r.snapToPixelBoundaries();
1228
1229 Rect clipRect(*mSnapshot->clipRect);
1230 clipRect.snapToPixelBoundaries();
1231
1232 return !clipRect.intersects(r);
1233}
1234
Romain Guy35643dd2012-09-18 15:40:58 -07001235bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1236 Rect& transformed, Rect& clip) {
1237 if (mSnapshot->isIgnored()) {
1238 return true;
1239 }
1240
1241 transformed.set(left, top, right, bottom);
1242 mSnapshot->transform->mapRect(transformed);
1243 transformed.snapToPixelBoundaries();
1244
1245 clip.set(*mSnapshot->clipRect);
1246 clip.snapToPixelBoundaries();
1247
1248 return !clip.intersects(transformed);
1249}
1250
Romain Guyc7d53492010-06-25 13:41:57 -07001251bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001252 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001253 return true;
1254 }
1255
Romain Guy1d83e192010-08-17 11:37:00 -07001256 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001257 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001258 r.snapToPixelBoundaries();
1259
1260 Rect clipRect(*mSnapshot->clipRect);
1261 clipRect.snapToPixelBoundaries();
1262
Romain Guy586cae32012-07-13 15:28:31 -07001263 bool rejected = !clipRect.intersects(r);
1264 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001265 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001266 }
1267
1268 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001269}
1270
Romain Guy079ba2c2010-07-16 14:12:24 -07001271bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1272 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001273 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001274 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001275 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001276 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001277}
1278
Chet Haasea23eed82012-04-12 15:19:04 -07001279Rect* OpenGLRenderer::getClipRect() {
1280 return mSnapshot->clipRect;
1281}
1282
Romain Guyf6a11b82010-06-23 17:47:49 -07001283///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001284// Drawing commands
1285///////////////////////////////////////////////////////////////////////////////
1286
Romain Guy54be1cd2011-06-13 19:04:27 -07001287void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001288 // TODO: It would be best if we could do this before quickReject()
1289 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001290 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001291 if (mDirtyClip) {
1292 setScissorFromClip();
1293 }
1294 mDescription.reset();
1295 mSetShaderColor = false;
1296 mColorSet = false;
1297 mColorA = mColorR = mColorG = mColorB = 0.0f;
1298 mTextureUnit = 0;
1299 mTrackDirtyRegions = true;
1300}
1301
1302void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1303 mDescription.hasTexture = true;
1304 mDescription.hasAlpha8Texture = isAlpha8;
1305}
1306
Romain Guyaa6c24c2011-04-28 18:40:04 -07001307void OpenGLRenderer::setupDrawWithExternalTexture() {
1308 mDescription.hasExternalTexture = true;
1309}
1310
Romain Guy15bc6432011-12-13 13:11:32 -08001311void OpenGLRenderer::setupDrawNoTexture() {
1312 mCaches.disbaleTexCoordsVertexArray();
1313}
1314
Chris Craik710f46d2012-09-17 17:25:49 -07001315void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001316 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001317}
1318
Chris Craik710f46d2012-09-17 17:25:49 -07001319void OpenGLRenderer::setupDrawVertexShape() {
1320 mDescription.isVertexShape = true;
Chris Craik6ebdc112012-08-31 18:24:33 -07001321}
1322
Romain Guyed6fcb02011-03-21 13:11:28 -07001323void OpenGLRenderer::setupDrawPoint(float pointSize) {
1324 mDescription.isPoint = true;
1325 mDescription.pointSize = pointSize;
1326}
1327
Romain Guy70ca14e2010-12-13 18:24:33 -08001328void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001329 setupDrawColor(color, (color >> 24) & 0xFF);
1330}
1331
1332void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1333 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001334 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1335 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001336 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001337 mColorR = a * ((color >> 16) & 0xFF);
1338 mColorG = a * ((color >> 8) & 0xFF);
1339 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001340 mColorSet = true;
1341 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1342}
1343
Romain Guy86568192010-12-14 15:55:39 -08001344void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1345 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001346 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1347 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001348 const float a = mColorA / 255.0f;
1349 mColorR = a * ((color >> 16) & 0xFF);
1350 mColorG = a * ((color >> 8) & 0xFF);
1351 mColorB = a * ((color ) & 0xFF);
1352 mColorSet = true;
1353 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1354}
1355
Romain Guy41210632012-07-16 17:04:24 -07001356void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1357 mCaches.fontRenderer->describe(mDescription, paint);
1358}
1359
Romain Guy70ca14e2010-12-13 18:24:33 -08001360void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1361 mColorA = a;
1362 mColorR = r;
1363 mColorG = g;
1364 mColorB = b;
1365 mColorSet = true;
1366 mSetShaderColor = mDescription.setColor(r, g, b, a);
1367}
1368
1369void OpenGLRenderer::setupDrawShader() {
1370 if (mShader) {
1371 mShader->describe(mDescription, mCaches.extensions);
1372 }
1373}
1374
1375void OpenGLRenderer::setupDrawColorFilter() {
1376 if (mColorFilter) {
1377 mColorFilter->describe(mDescription, mCaches.extensions);
1378 }
1379}
1380
Romain Guyf09ef512011-05-27 11:43:46 -07001381void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1382 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1383 mColorA = 1.0f;
1384 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001385 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001386 }
1387}
1388
Romain Guy70ca14e2010-12-13 18:24:33 -08001389void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001390 // When the blending mode is kClear_Mode, we need to use a modulate color
1391 // argb=1,0,0,0
1392 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001393 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1394 mDescription, swapSrcDst);
1395}
1396
1397void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001398 // When the blending mode is kClear_Mode, we need to use a modulate color
1399 // argb=1,0,0,0
1400 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001401 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1402 mDescription, swapSrcDst);
1403}
1404
1405void OpenGLRenderer::setupDrawProgram() {
1406 useProgram(mCaches.programCache.get(mDescription));
1407}
1408
1409void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1410 mTrackDirtyRegions = false;
1411}
1412
1413void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1414 bool ignoreTransform) {
1415 mModelView.loadTranslate(left, top, 0.0f);
1416 if (!ignoreTransform) {
1417 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1418 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1419 } else {
1420 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1421 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1422 }
1423}
1424
Chet Haase8a5cc922011-04-26 07:28:09 -07001425void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1426 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001427}
1428
Romain Guy70ca14e2010-12-13 18:24:33 -08001429void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1430 bool ignoreTransform, bool ignoreModelView) {
1431 if (!ignoreModelView) {
1432 mModelView.loadTranslate(left, top, 0.0f);
1433 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001434 } else {
1435 mModelView.loadIdentity();
1436 }
Romain Guy86568192010-12-14 15:55:39 -08001437 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1438 if (!ignoreTransform) {
1439 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1440 if (mTrackDirtyRegions && dirty) {
1441 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1442 }
1443 } else {
1444 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1445 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1446 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001447}
1448
Romain Guyed6fcb02011-03-21 13:11:28 -07001449void OpenGLRenderer::setupDrawPointUniforms() {
1450 int slot = mCaches.currentProgram->getUniform("pointSize");
1451 glUniform1f(slot, mDescription.pointSize);
1452}
1453
Romain Guy70ca14e2010-12-13 18:24:33 -08001454void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001455 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001456 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1457 }
1458}
1459
Romain Guy86568192010-12-14 15:55:39 -08001460void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001461 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001462 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001463 }
1464}
1465
Romain Guy70ca14e2010-12-13 18:24:33 -08001466void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1467 if (mShader) {
1468 if (ignoreTransform) {
1469 mModelView.loadInverse(*mSnapshot->transform);
1470 }
1471 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1472 }
1473}
1474
Romain Guy8d0d4782010-12-14 20:13:35 -08001475void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1476 if (mShader) {
1477 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1478 }
1479}
1480
Romain Guy70ca14e2010-12-13 18:24:33 -08001481void OpenGLRenderer::setupDrawColorFilterUniforms() {
1482 if (mColorFilter) {
1483 mColorFilter->setupProgram(mCaches.currentProgram);
1484 }
1485}
1486
Romain Guy41210632012-07-16 17:04:24 -07001487void OpenGLRenderer::setupDrawTextGammaUniforms() {
1488 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1489}
1490
Romain Guy70ca14e2010-12-13 18:24:33 -08001491void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001492 bool force = mCaches.bindMeshBuffer();
1493 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001494 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001495}
1496
1497void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1498 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001499 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001500 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001501}
1502
Romain Guyaa6c24c2011-04-28 18:40:04 -07001503void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1504 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001505 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001506 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001507}
1508
Romain Guy8f0095c2011-05-02 17:24:22 -07001509void OpenGLRenderer::setupDrawTextureTransform() {
1510 mDescription.hasTextureTransform = true;
1511}
1512
1513void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001514 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1515 GL_FALSE, &transform.data[0]);
1516}
1517
Romain Guy70ca14e2010-12-13 18:24:33 -08001518void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001519 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001520 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001521 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001522 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001523 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001524 }
Romain Guyd71dd362011-12-12 19:03:35 -08001525
Romain Guyf3a910b42011-12-12 20:35:21 -08001526 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001527 if (mCaches.currentProgram->texCoords >= 0) {
1528 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1529 }
1530
1531 mCaches.unbindIndicesBuffer();
1532}
1533
1534void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1535 bool force = mCaches.unbindMeshBuffer();
1536 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1537 if (mCaches.currentProgram->texCoords >= 0) {
1538 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001539 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001540}
1541
Chet Haase5b0200b2011-04-13 17:58:08 -07001542void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001543 bool force = mCaches.unbindMeshBuffer();
1544 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1545 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001546 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001547}
1548
1549/**
1550 * 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 -07001551 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1552 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1553 * attributes (one per vertex) are values from zero to one that tells the fragment
1554 * shader where the fragment is in relation to the line width/length overall; these values are
1555 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1556 * region of the line.
1557 * Note that we only pass down the width values in this setup function. The length coordinates
1558 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001559 */
Chet Haase99585ad2011-05-02 15:00:16 -07001560void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001561 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001562 bool force = mCaches.unbindMeshBuffer();
1563 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1564 vertices, gAAVertexStride);
1565 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001566 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001567
Romain Guy7b631422012-04-04 11:38:54 -07001568 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001569 glEnableVertexAttribArray(widthSlot);
1570 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001571
Romain Guy7b631422012-04-04 11:38:54 -07001572 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001573 glEnableVertexAttribArray(lengthSlot);
1574 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001575
Chet Haase5b0200b2011-04-13 17:58:08 -07001576 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001577 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001578}
1579
1580void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1581 glDisableVertexAttribArray(widthSlot);
1582 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001583}
1584
Romain Guy70ca14e2010-12-13 18:24:33 -08001585void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001586}
1587
1588///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001589// Drawing
1590///////////////////////////////////////////////////////////////////////////////
1591
Chet Haase1271e2c2012-04-20 09:54:27 -07001592status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001593 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001594
Romain Guy0fe478e2010-11-08 12:08:41 -08001595 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1596 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001597 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001598 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001599 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001600
Romain Guy65549432012-03-26 16:45:05 -07001601 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001602}
1603
Chet Haaseed30fd82011-04-22 16:18:45 -07001604void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1605 if (displayList) {
1606 displayList->output(*this, level);
1607 }
1608}
1609
Romain Guya168d732011-03-18 16:50:13 -07001610void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1611 int alpha;
1612 SkXfermode::Mode mode;
1613 getAlphaAndMode(paint, &alpha, &mode);
1614
Romain Guya168d732011-03-18 16:50:13 -07001615 float x = left;
1616 float y = top;
1617
Romain Guye3c26852011-07-25 16:36:01 -07001618 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001619 bool ignoreTransform = false;
1620 if (mSnapshot->transform->isPureTranslate()) {
1621 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1622 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1623 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001624 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001625 } else {
1626 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001627 }
1628
1629 setupDraw();
1630 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001631 if (paint) {
1632 setupDrawAlpha8Color(paint->getColor(), alpha);
1633 }
Romain Guya168d732011-03-18 16:50:13 -07001634 setupDrawColorFilter();
1635 setupDrawShader();
1636 setupDrawBlending(true, mode);
1637 setupDrawProgram();
1638 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001639
Romain Guya168d732011-03-18 16:50:13 -07001640 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001641 texture->setWrap(GL_CLAMP_TO_EDGE);
1642 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001643
Romain Guya168d732011-03-18 16:50:13 -07001644 setupDrawPureColorUniforms();
1645 setupDrawColorFilterUniforms();
1646 setupDrawShaderUniforms();
1647 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1648
1649 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1650
1651 finishDrawTexture();
1652}
1653
Chet Haase48659092012-05-31 15:21:51 -07001654status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001655 const float right = left + bitmap->width();
1656 const float bottom = top + bitmap->height();
1657
1658 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001659 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001660 }
1661
Romain Guya1d3c912011-12-13 14:55:06 -08001662 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001663 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001664 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001665 const AutoTexture autoCleanup(texture);
1666
Romain Guy211370f2012-02-01 16:10:55 -08001667 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001668 drawAlphaBitmap(texture, left, top, paint);
1669 } else {
1670 drawTextureRect(left, top, right, bottom, texture, paint);
1671 }
Chet Haase48659092012-05-31 15:21:51 -07001672
1673 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001674}
1675
Chet Haase48659092012-05-31 15:21:51 -07001676status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001677 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1678 const mat4 transform(*matrix);
1679 transform.mapRect(r);
1680
Romain Guy6926c722010-07-12 20:20:03 -07001681 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001682 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001683 }
1684
Romain Guya1d3c912011-12-13 14:55:06 -08001685 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001686 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001687 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001688 const AutoTexture autoCleanup(texture);
1689
Romain Guy5b3b3522010-10-27 18:57:51 -07001690 // This could be done in a cheaper way, all we need is pass the matrix
1691 // to the vertex shader. The save/restore is a bit overkill.
1692 save(SkCanvas::kMatrix_SaveFlag);
1693 concatMatrix(matrix);
1694 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1695 restore();
Chet Haase48659092012-05-31 15:21:51 -07001696
1697 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001698}
1699
Chet Haase48659092012-05-31 15:21:51 -07001700status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001701 const float right = left + bitmap->width();
1702 const float bottom = top + bitmap->height();
1703
1704 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001705 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001706 }
1707
1708 mCaches.activeTexture(0);
1709 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1710 const AutoTexture autoCleanup(texture);
1711
1712 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001713
1714 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001715}
1716
Chet Haase48659092012-05-31 15:21:51 -07001717status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001718 float* vertices, int* colors, SkPaint* paint) {
1719 // TODO: Do a quickReject
1720 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001721 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001722 }
1723
Romain Guya1d3c912011-12-13 14:55:06 -08001724 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001725 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001726 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001727 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001728
Romain Guyd21b6e12011-11-30 20:21:23 -08001729 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1730 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001731
1732 int alpha;
1733 SkXfermode::Mode mode;
1734 getAlphaAndMode(paint, &alpha, &mode);
1735
Romain Guy5a7b4662011-01-20 19:09:30 -08001736 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001737
Romain Guyb18d2d02011-02-10 15:52:54 -08001738 float left = FLT_MAX;
1739 float top = FLT_MAX;
1740 float right = FLT_MIN;
1741 float bottom = FLT_MIN;
1742
Romain Guy211370f2012-02-01 16:10:55 -08001743 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001744
Romain Guya566b7c2011-01-23 16:36:11 -08001745 // TODO: Support the colors array
1746 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001747 TextureVertex* vertex = mesh;
1748 for (int32_t y = 0; y < meshHeight; y++) {
1749 for (int32_t x = 0; x < meshWidth; x++) {
1750 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1751
1752 float u1 = float(x) / meshWidth;
1753 float u2 = float(x + 1) / meshWidth;
1754 float v1 = float(y) / meshHeight;
1755 float v2 = float(y + 1) / meshHeight;
1756
1757 int ax = i + (meshWidth + 1) * 2;
1758 int ay = ax + 1;
1759 int bx = i;
1760 int by = bx + 1;
1761 int cx = i + 2;
1762 int cy = cx + 1;
1763 int dx = i + (meshWidth + 1) * 2 + 2;
1764 int dy = dx + 1;
1765
1766 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1767 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1768 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1769
1770 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1771 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1772 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001773
Romain Guyb18d2d02011-02-10 15:52:54 -08001774 if (hasActiveLayer) {
1775 // TODO: This could be optimized to avoid unnecessary ops
1776 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1777 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1778 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1779 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1780 }
Romain Guy5a7b4662011-01-20 19:09:30 -08001781 }
1782 }
1783
Romain Guyb18d2d02011-02-10 15:52:54 -08001784 if (hasActiveLayer) {
1785 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1786 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001787
Romain Guy5a7b4662011-01-20 19:09:30 -08001788 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1789 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001790 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001791
1792 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001793}
1794
Chet Haase48659092012-05-31 15:21:51 -07001795status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001796 float srcLeft, float srcTop, float srcRight, float srcBottom,
1797 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001798 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001799 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001800 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001801 }
1802
Romain Guya1d3c912011-12-13 14:55:06 -08001803 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001804 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001805 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001806 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001807
Romain Guy8ba548f2010-06-30 19:21:21 -07001808 const float width = texture->width;
1809 const float height = texture->height;
1810
Romain Guy68169722011-08-22 17:33:33 -07001811 const float u1 = fmax(0.0f, srcLeft / width);
1812 const float v1 = fmax(0.0f, srcTop / height);
1813 const float u2 = fmin(1.0f, srcRight / width);
1814 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001815
Romain Guy03750a02010-10-18 14:06:08 -07001816 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001817 resetDrawTextureTexCoords(u1, v1, u2, v2);
1818
Romain Guy03750a02010-10-18 14:06:08 -07001819 int alpha;
1820 SkXfermode::Mode mode;
1821 getAlphaAndMode(paint, &alpha, &mode);
1822
Romain Guyd21b6e12011-11-30 20:21:23 -08001823 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1824
Romain Guy211370f2012-02-01 16:10:55 -08001825 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001826 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1827 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1828
Romain Guyb5014982011-07-28 15:39:12 -07001829 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001830 // Enable linear filtering if the source rectangle is scaled
1831 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001832 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001833 }
Romain Guyb5014982011-07-28 15:39:12 -07001834
Romain Guyd21b6e12011-11-30 20:21:23 -08001835 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001836 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1837 texture->id, alpha / 255.0f, mode, texture->blend,
1838 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1839 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1840 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001841 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001842 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1843 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1844 GL_TRIANGLE_STRIP, gMeshCount);
1845 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001846
1847 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001848
1849 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001850}
1851
Chet Haase48659092012-05-31 15:21:51 -07001852status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001853 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001854 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001855 int alpha;
1856 SkXfermode::Mode mode;
1857 getAlphaAndModeDirect(paint, &alpha, &mode);
1858
1859 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1860 left, top, right, bottom, alpha, mode);
1861}
1862
1863status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1864 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1865 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001866 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001867 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001868 }
1869
Romain Guybe6f9dc2012-07-16 12:41:17 -07001870 alpha *= mSnapshot->alpha;
1871
Romain Guya1d3c912011-12-13 14:55:06 -08001872 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001873 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001874 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001875 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001876 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1877 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001878
Romain Guy2728f962010-10-08 18:36:15 -07001879 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001880 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001881
Romain Guy211370f2012-02-01 16:10:55 -08001882 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001883 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001884 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001885 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001886 const float offsetX = left + mSnapshot->transform->getTranslateX();
1887 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001888 const size_t count = mesh->quads.size();
1889 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001890 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001891 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001892 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1893 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1894 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001895 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001896 dirtyLayer(left + bounds.left, top + bounds.top,
1897 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001898 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001899 }
1900 }
1901
Romain Guy211370f2012-02-01 16:10:55 -08001902 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001903 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1904 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1905
1906 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1907 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1908 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1909 true, !mesh->hasEmptyQuads);
1910 } else {
1911 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1912 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1913 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1914 true, !mesh->hasEmptyQuads);
1915 }
Romain Guy054dc182010-10-15 17:55:25 -07001916 }
Chet Haase48659092012-05-31 15:21:51 -07001917
1918 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001919}
1920
Chet Haase99ecdc42011-05-06 12:06:34 -07001921/**
Chet Haase858aa932011-05-12 09:06:00 -07001922 * This function uses a similar approach to that of AA lines in the drawLines() function.
Chris Craik6ebdc112012-08-31 18:24:33 -07001923 * We expand the rectangle by a half pixel in screen space on all sides. However, instead of using
1924 * a fragment shader to compute the translucency of the color from its position, we simply use a
1925 * varying parameter to define how far a given pixel is into the region.
Chet Haase858aa932011-05-12 09:06:00 -07001926 */
Chris Craik710f46d2012-09-17 17:25:49 -07001927void OpenGLRenderer::drawConvexPath(const SkPath& path, int color, SkXfermode::Mode mode, bool isAA) {
1928 VertexBuffer vertexBuffer;
1929 // TODO: try clipping large paths to viewport
1930 PathRenderer::convexPathFillVertices(path, mSnapshot->transform, vertexBuffer, isAA);
Romain Guy04299382012-07-18 17:15:41 -07001931
Chris Craik710f46d2012-09-17 17:25:49 -07001932 setupDraw();
1933 setupDrawNoTexture();
1934 if (isAA) setupDrawAA();
1935 setupDrawVertexShape();
1936 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1937 setupDrawColorFilter();
1938 setupDrawShader();
1939 setupDrawBlending(isAA, mode);
1940 setupDrawProgram();
1941 setupDrawModelViewIdentity(true);
1942 setupDrawColorUniforms();
1943 setupDrawColorFilterUniforms();
1944 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07001945
Chris Craik710f46d2012-09-17 17:25:49 -07001946 void* vertices = vertexBuffer.getBuffer();
1947 bool force = mCaches.unbindMeshBuffer();
1948 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1949 vertices, isAA ? gAlphaVertexStride : gVertexStride);
1950 mCaches.resetTexCoordsVertexPointer();
1951 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07001952
Chris Craik710f46d2012-09-17 17:25:49 -07001953 int alphaSlot = -1;
1954 if (isAA) {
1955 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
1956 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07001957
Chris Craik710f46d2012-09-17 17:25:49 -07001958 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07001959 glEnableVertexAttribArray(alphaSlot);
1960 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07001961 }
Romain Guy04299382012-07-18 17:15:41 -07001962
Chris Craik710f46d2012-09-17 17:25:49 -07001963 SkRect bounds = path.getBounds();
1964 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy04299382012-07-18 17:15:41 -07001965
Chris Craik710f46d2012-09-17 17:25:49 -07001966 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07001967
Chris Craik710f46d2012-09-17 17:25:49 -07001968 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07001969 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001970 }
Chet Haase858aa932011-05-12 09:06:00 -07001971}
1972
1973/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001974 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1975 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1976 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1977 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1978 * of the line. Hairlines are more involved because we need to account for transform scaling
1979 * to end up with a one-pixel-wide line in screen space..
1980 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1981 * in combination with values that we calculate and pass down in this method. The basic approach
1982 * is that the quad we create contains both the core line area plus a bounding area in which
1983 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1984 * proportion of the width and the length of a given segment is represented by the boundary
1985 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1986 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1987 * on the inside). This ends up giving the result we want, with pixels that are completely
1988 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1989 * how far into the boundary region they are, which is determined by shader interpolation.
1990 */
Chet Haase48659092012-05-31 15:21:51 -07001991status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1992 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001993
1994 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001995 // We use half the stroke width here because we're going to position the quad
1996 // corner vertices half of the width away from the line endpoints
1997 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001998 // A stroke width of 0 has a special meaning in Skia:
1999 // it draws a line 1 px wide regardless of current transform
2000 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07002001
Chet Haase99ecdc42011-05-06 12:06:34 -07002002 float inverseScaleX = 1.0f;
2003 float inverseScaleY = 1.0f;
2004 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07002005
Chet Haase8a5cc922011-04-26 07:28:09 -07002006 int alpha;
2007 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07002008
Chet Haase8a5cc922011-04-26 07:28:09 -07002009 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002010 int verticesCount = count;
2011 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07002012 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07002013 verticesCount += (count - 4);
2014 }
2015
2016 if (isHairLine || isAA) {
2017 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
2018 // the line on the screen should always be one pixel wide regardless of scale. For
2019 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08002020 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07002021 Matrix4 *mat = mSnapshot->transform;
2022 float m00 = mat->data[Matrix4::kScaleX];
2023 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07002024 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07002025 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07002026
Romain Guy211370f2012-02-01 16:10:55 -08002027 float scaleX = sqrtf(m00 * m00 + m01 * m01);
2028 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07002029
Chet Haase99ecdc42011-05-06 12:06:34 -07002030 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
2031 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07002032
Chet Haase99ecdc42011-05-06 12:06:34 -07002033 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
2034 scaled = true;
2035 }
2036 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002037 }
Chet Haase8a5cc922011-04-26 07:28:09 -07002038
2039 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07002040
2041 mCaches.enableScissor();
2042
Chet Haase8a5cc922011-04-26 07:28:09 -07002043 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002044 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002045 if (isAA) {
Chris Craik710f46d2012-09-17 17:25:49 -07002046 setupDrawAA();
Chet Haase8a5cc922011-04-26 07:28:09 -07002047 }
2048 setupDrawColor(paint->getColor(), alpha);
2049 setupDrawColorFilter();
2050 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08002051 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07002052 setupDrawProgram();
2053 setupDrawModelViewIdentity(true);
2054 setupDrawColorUniforms();
2055 setupDrawColorFilterUniforms();
2056 setupDrawShaderIdentityUniforms();
2057
2058 if (isHairLine) {
2059 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07002060 halfStrokeWidth = isAA? 1 : .5;
2061 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002062 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07002063 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07002064 }
Romain Guy7b631422012-04-04 11:38:54 -07002065
2066 int widthSlot;
2067 int lengthSlot;
2068
Chet Haase5b0200b2011-04-13 17:58:08 -07002069 Vertex lines[verticesCount];
2070 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002071
Chet Haase99585ad2011-05-02 15:00:16 -07002072 AAVertex wLines[verticesCount];
2073 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002074
Romain Guy211370f2012-02-01 16:10:55 -08002075 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002076 setupDrawVertices(vertices);
2077 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002078 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2079 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002080 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002081 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2082 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002083 // We will need to calculate the actual width proportion on each segment for
2084 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002085 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002086 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2087 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002088 }
2089
Chet Haase99ecdc42011-05-06 12:06:34 -07002090 AAVertex* prevAAVertex = NULL;
2091 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002092
Chet Haase99585ad2011-05-02 15:00:16 -07002093 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002094 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002095
Chet Haase5b0200b2011-04-13 17:58:08 -07002096 for (int i = 0; i < count; i += 4) {
2097 // a = start point, b = end point
2098 vec2 a(points[i], points[i + 1]);
2099 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002100
Chet Haase99585ad2011-05-02 15:00:16 -07002101 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002102 float boundaryLengthProportion = 0;
2103 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002104
Chet Haase5b0200b2011-04-13 17:58:08 -07002105 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002106 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002107 float x = n.x;
2108 n.x = -n.y;
2109 n.y = x;
2110
Chet Haase8a5cc922011-04-26 07:28:09 -07002111 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002112 if (isAA) {
2113 float wideningFactor;
2114 if (fabs(n.x) >= fabs(n.y)) {
2115 wideningFactor = fabs(1.0f / n.x);
2116 } else {
2117 wideningFactor = fabs(1.0f / n.y);
2118 }
2119 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002120 }
Romain Guy7b631422012-04-04 11:38:54 -07002121
Chet Haase99ecdc42011-05-06 12:06:34 -07002122 if (scaled) {
2123 n.x *= inverseScaleX;
2124 n.y *= inverseScaleY;
2125 }
2126 } else if (scaled) {
2127 // Extend n by .5 pixel on each side, post-transform
2128 vec2 extendedN = n.copyNormalized();
2129 extendedN /= 2;
2130 extendedN.x *= inverseScaleX;
2131 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002132
Chet Haase99ecdc42011-05-06 12:06:34 -07002133 float extendedNLength = extendedN.length();
2134 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002135 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002136 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002137 }
Romain Guy7b631422012-04-04 11:38:54 -07002138
Chet Haase99585ad2011-05-02 15:00:16 -07002139 // aa lines expand the endpoint vertices to encompass the AA boundary
2140 if (isAA) {
2141 vec2 abVector = (b - a);
2142 length = abVector.length();
2143 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002144
Chet Haase99ecdc42011-05-06 12:06:34 -07002145 if (scaled) {
2146 abVector.x *= inverseScaleX;
2147 abVector.y *= inverseScaleY;
2148 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002149 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002150 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002151 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002152 }
Romain Guy7b631422012-04-04 11:38:54 -07002153
Chet Haase99ecdc42011-05-06 12:06:34 -07002154 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002155 a -= abVector;
2156 b += abVector;
2157 }
2158
Chet Haase5b0200b2011-04-13 17:58:08 -07002159 // Four corners of the rectangle defining a thick line
2160 vec2 p1 = a - n;
2161 vec2 p2 = a + n;
2162 vec2 p3 = b + n;
2163 vec2 p4 = b - n;
2164
Chet Haase99585ad2011-05-02 15:00:16 -07002165
Chet Haase5b0200b2011-04-13 17:58:08 -07002166 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2167 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2168 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2169 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2170
Romain Guy04299382012-07-18 17:15:41 -07002171 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002172 if (!isAA) {
2173 if (prevVertex != NULL) {
2174 // Issue two repeat vertices to create degenerate triangles to bridge
2175 // between the previous line and the new one. This is necessary because
2176 // we are creating a single triangle_strip which will contain
2177 // potentially discontinuous line segments.
2178 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2179 Vertex::set(vertices++, p1.x, p1.y);
2180 generatedVerticesCount += 2;
2181 }
Romain Guy7b631422012-04-04 11:38:54 -07002182
Chet Haase5b0200b2011-04-13 17:58:08 -07002183 Vertex::set(vertices++, p1.x, p1.y);
2184 Vertex::set(vertices++, p2.x, p2.y);
2185 Vertex::set(vertices++, p4.x, p4.y);
2186 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002187
Chet Haase5b0200b2011-04-13 17:58:08 -07002188 prevVertex = vertices - 1;
2189 generatedVerticesCount += 4;
2190 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002191 if (!isHairLine && scaled) {
2192 // Must set width proportions per-segment for scaled non-hairlines to use the
2193 // correct AA boundary dimensions
2194 if (boundaryWidthSlot < 0) {
2195 boundaryWidthSlot =
2196 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002197 }
Romain Guy7b631422012-04-04 11:38:54 -07002198
Chet Haase99ecdc42011-05-06 12:06:34 -07002199 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002200 }
Romain Guy7b631422012-04-04 11:38:54 -07002201
Chet Haase99585ad2011-05-02 15:00:16 -07002202 if (boundaryLengthSlot < 0) {
2203 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002204 }
Romain Guy7b631422012-04-04 11:38:54 -07002205
Chet Haase99ecdc42011-05-06 12:06:34 -07002206 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002207
Chet Haase5b0200b2011-04-13 17:58:08 -07002208 if (prevAAVertex != NULL) {
2209 // Issue two repeat vertices to create degenerate triangles to bridge
2210 // between the previous line and the new one. This is necessary because
2211 // we are creating a single triangle_strip which will contain
2212 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002213 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2214 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2215 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002216 generatedVerticesCount += 2;
2217 }
Romain Guy7b631422012-04-04 11:38:54 -07002218
Chet Haase99585ad2011-05-02 15:00:16 -07002219 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2220 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2221 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2222 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002223
Chet Haase5b0200b2011-04-13 17:58:08 -07002224 prevAAVertex = aaVertices - 1;
2225 generatedVerticesCount += 4;
2226 }
Romain Guy7b631422012-04-04 11:38:54 -07002227
Chet Haase99585ad2011-05-02 15:00:16 -07002228 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2229 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2230 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002231 }
2232 }
Romain Guy7b631422012-04-04 11:38:54 -07002233
Chet Haase5b0200b2011-04-13 17:58:08 -07002234 if (generatedVerticesCount > 0) {
2235 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2236 }
Romain Guy7b631422012-04-04 11:38:54 -07002237
2238 if (isAA) {
2239 finishDrawAALine(widthSlot, lengthSlot);
2240 }
Chet Haase48659092012-05-31 15:21:51 -07002241
2242 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002243}
2244
Chet Haase48659092012-05-31 15:21:51 -07002245status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2246 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002247
2248 // TODO: The paint's cap style defines whether the points are square or circular
2249 // TODO: Handle AA for round points
2250
Chet Haase5b0200b2011-04-13 17:58:08 -07002251 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002252 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002253 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002254 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002255 if (isHairLine) {
2256 // Now that we know it's hairline, we can set the effective width, to be used later
2257 strokeWidth = 1.0f;
2258 }
2259 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002260 int alpha;
2261 SkXfermode::Mode mode;
2262 getAlphaAndMode(paint, &alpha, &mode);
2263
2264 int verticesCount = count >> 1;
2265 int generatedVerticesCount = 0;
2266
2267 TextureVertex pointsData[verticesCount];
2268 TextureVertex* vertex = &pointsData[0];
2269
Romain Guy04299382012-07-18 17:15:41 -07002270 // TODO: We should optimize this method to not generate vertices for points
2271 // that lie outside of the clip.
2272 mCaches.enableScissor();
2273
Romain Guyed6fcb02011-03-21 13:11:28 -07002274 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002275 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002276 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002277 setupDrawColor(paint->getColor(), alpha);
2278 setupDrawColorFilter();
2279 setupDrawShader();
2280 setupDrawBlending(mode);
2281 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002282 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002283 setupDrawColorUniforms();
2284 setupDrawColorFilterUniforms();
2285 setupDrawPointUniforms();
2286 setupDrawShaderIdentityUniforms();
2287 setupDrawMesh(vertex);
2288
2289 for (int i = 0; i < count; i += 2) {
2290 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2291 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002292
Chet Haase8a5cc922011-04-26 07:28:09 -07002293 float left = points[i] - halfWidth;
2294 float right = points[i] + halfWidth;
2295 float top = points[i + 1] - halfWidth;
2296 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002297
Chet Haase8a5cc922011-04-26 07:28:09 -07002298 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002299 }
2300
2301 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002302
2303 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002304}
2305
Chet Haase48659092012-05-31 15:21:51 -07002306status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002307 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002308 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002309
Romain Guyae88e5e2010-10-22 17:49:18 -07002310 Rect& clip(*mSnapshot->clipRect);
2311 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002312
Romain Guy3d58c032010-07-14 16:34:53 -07002313 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002314
2315 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002316}
Romain Guy9d5316e2010-06-24 19:30:36 -07002317
Chet Haase48659092012-05-31 15:21:51 -07002318status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2319 SkPaint* paint) {
2320 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002321 const AutoTexture autoCleanup(texture);
2322
2323 const float x = left + texture->left - texture->offset;
2324 const float y = top + texture->top - texture->offset;
2325
2326 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002327
2328 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002329}
2330
Chet Haase48659092012-05-31 15:21:51 -07002331status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002332 float rx, float ry, SkPaint* p) {
2333 if (mSnapshot->isIgnored() || quickReject(left, top, right, bottom)) {
2334 return DrawGlInfo::kStatusDone;
2335 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002336
Chris Craik710f46d2012-09-17 17:25:49 -07002337 if (p->getStyle() != SkPaint::kFill_Style) {
2338 mCaches.activeTexture(0);
2339 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2340 right - left, bottom - top, rx, ry, p);
2341 return drawShape(left, top, texture, p);
2342 }
2343
2344 SkPath path;
2345 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2346 path.addRoundRect(rect, rx, ry);
2347 drawConvexPath(path, p->getColor(), getXfermode(p->getXfermode()), p->isAntiAlias());
2348
2349 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002350}
2351
Chris Craik710f46d2012-09-17 17:25:49 -07002352status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
2353 if (mSnapshot->isIgnored() || quickReject(x - radius, y - radius, x + radius, y + radius)) {
2354 return DrawGlInfo::kStatusDone;
2355 }
Romain Guy01d58e42011-01-19 21:54:02 -08002356
Chris Craik710f46d2012-09-17 17:25:49 -07002357 if (p->getStyle() != SkPaint::kFill_Style) {
2358 mCaches.activeTexture(0);
2359 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2360 return drawShape(x - radius, y - radius, texture, p);
2361 }
2362
2363 SkPath path;
2364 path.addCircle(x, y, radius);
2365 drawConvexPath(path, p->getColor(), getXfermode(p->getXfermode()), p->isAntiAlias());
2366
2367 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002368}
Romain Guy01d58e42011-01-19 21:54:02 -08002369
Chet Haase48659092012-05-31 15:21:51 -07002370status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002371 SkPaint* p) {
2372 if (mSnapshot->isIgnored() || quickReject(left, top, right, bottom)) {
2373 return DrawGlInfo::kStatusDone;
2374 }
Romain Guy01d58e42011-01-19 21:54:02 -08002375
Chris Craik710f46d2012-09-17 17:25:49 -07002376 if (p->getStyle() != SkPaint::kFill_Style) {
2377 mCaches.activeTexture(0);
2378 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2379 return drawShape(left, top, texture, p);
2380 }
2381
2382 SkPath path;
2383 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2384 path.addOval(rect);
2385 drawConvexPath(path, p->getColor(), getXfermode(p->getXfermode()), p->isAntiAlias());
2386
2387 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002388}
2389
Chet Haase48659092012-05-31 15:21:51 -07002390status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002391 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002392 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002393
2394 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002395 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002396 }
2397
Romain Guya1d3c912011-12-13 14:55:06 -08002398 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002399 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2400 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002401 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002402}
2403
Chet Haase48659092012-05-31 15:21:51 -07002404status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craik710f46d2012-09-17 17:25:49 -07002405 if (mSnapshot->isIgnored() || quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002406 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002407 }
2408
Chris Craik710f46d2012-09-17 17:25:49 -07002409 if (p->getStyle() != SkPaint::kFill_Style) {
2410 mCaches.activeTexture(0);
2411 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2412 return drawShape(left, top, texture, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002413 }
2414
Romain Guy181d0a62011-06-09 18:52:38 -07002415 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002416 SkPath path;
2417 path.addRect(left, top, right, bottom);
2418 drawConvexPath(path, p->getColor(), getXfermode(p->getXfermode()), true);
Chet Haase858aa932011-05-12 09:06:00 -07002419 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002420 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chet Haase858aa932011-05-12 09:06:00 -07002421 }
Chet Haase48659092012-05-31 15:21:51 -07002422
2423 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002424}
Romain Guy9d5316e2010-06-24 19:30:36 -07002425
Raph Levien416a8472012-07-19 22:48:17 -07002426void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2427 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2428 float x, float y) {
2429 mCaches.activeTexture(0);
2430
2431 // NOTE: The drop shadow will not perform gamma correction
2432 // if shader-based correction is enabled
2433 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2434 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2435 paint, text, bytesCount, count, mShadowRadius, positions);
2436 const AutoTexture autoCleanup(shadow);
2437
2438 const float sx = x - shadow->left + mShadowDx;
2439 const float sy = y - shadow->top + mShadowDy;
2440
2441 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2442 int shadowColor = mShadowColor;
2443 if (mShader) {
2444 shadowColor = 0xffffffff;
2445 }
2446
2447 setupDraw();
2448 setupDrawWithTexture(true);
2449 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2450 setupDrawColorFilter();
2451 setupDrawShader();
2452 setupDrawBlending(true, mode);
2453 setupDrawProgram();
2454 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2455 setupDrawTexture(shadow->id);
2456 setupDrawPureColorUniforms();
2457 setupDrawColorFilterUniforms();
2458 setupDrawShaderUniforms();
2459 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2460
2461 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2462}
2463
Chet Haase48659092012-05-31 15:21:51 -07002464status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002465 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002466 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002467 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002468 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002469 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002470
Romain Guy671d6cf2012-01-18 12:39:17 -08002471 // NOTE: Skia does not support perspective transform on drawPosText yet
2472 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002473 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002474 }
2475
2476 float x = 0.0f;
2477 float y = 0.0f;
2478 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2479 if (pureTranslate) {
2480 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2481 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2482 }
2483
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002484 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002485 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2486 paint->getTextSize());
2487
2488 int alpha;
2489 SkXfermode::Mode mode;
2490 getAlphaAndMode(paint, &alpha, &mode);
2491
Raph Levien416a8472012-07-19 22:48:17 -07002492 if (CC_UNLIKELY(mHasShadow)) {
2493 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2494 0.0f, 0.0f);
2495 }
2496
Romain Guy671d6cf2012-01-18 12:39:17 -08002497 // Pick the appropriate texture filtering
2498 bool linearFilter = mSnapshot->transform->changesBounds();
2499 if (pureTranslate && !linearFilter) {
2500 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2501 }
2502
2503 mCaches.activeTexture(0);
2504 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002505 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002506 setupDrawDirtyRegionsDisabled();
2507 setupDrawWithTexture(true);
2508 setupDrawAlpha8Color(paint->getColor(), alpha);
2509 setupDrawColorFilter();
2510 setupDrawShader();
2511 setupDrawBlending(true, mode);
2512 setupDrawProgram();
2513 setupDrawModelView(x, y, x, y, pureTranslate, true);
2514 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2515 setupDrawPureColorUniforms();
2516 setupDrawColorFilterUniforms();
2517 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002518 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002519
2520 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2521 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2522
Romain Guy211370f2012-02-01 16:10:55 -08002523 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002524
2525 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2526 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002527 if (hasActiveLayer) {
2528 if (!pureTranslate) {
2529 mSnapshot->transform->mapRect(bounds);
2530 }
2531 dirtyLayerUnchecked(bounds, getRegion());
2532 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002533 }
Chet Haase48659092012-05-31 15:21:51 -07002534
2535 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002536}
2537
Romain Guyc2525952012-07-27 16:41:22 -07002538status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002539 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002540 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002541 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002542 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002543 }
2544
Chet Haasea1cff502012-02-21 13:43:44 -08002545 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002546 switch (paint->getTextAlign()) {
2547 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002548 x -= length / 2.0f;
2549 break;
2550 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002551 x -= length;
2552 break;
2553 default:
2554 break;
2555 }
2556
Romain Guycac5fd32011-12-01 20:08:50 -08002557 SkPaint::FontMetrics metrics;
2558 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002559 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002560 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002561 }
2562
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002563 const float oldX = x;
2564 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002565 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002566 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002567 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2568 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2569 }
2570
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002571#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002572 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002573 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002574#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002575
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002576 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002577 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002578 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002579
Romain Guy86568192010-12-14 15:55:39 -08002580 int alpha;
2581 SkXfermode::Mode mode;
2582 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002583
Romain Guy211370f2012-02-01 16:10:55 -08002584 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002585 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2586 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002587 }
2588
Romain Guy6620c6d2010-12-06 18:07:02 -08002589 // Pick the appropriate texture filtering
2590 bool linearFilter = mSnapshot->transform->changesBounds();
2591 if (pureTranslate && !linearFilter) {
2592 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2593 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002594
Romain Guy16c88082012-06-11 16:03:47 -07002595 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002596 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002597 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002598 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002599 setupDrawDirtyRegionsDisabled();
2600 setupDrawWithTexture(true);
2601 setupDrawAlpha8Color(paint->getColor(), alpha);
2602 setupDrawColorFilter();
2603 setupDrawShader();
2604 setupDrawBlending(true, mode);
2605 setupDrawProgram();
2606 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002607 // See comment above; the font renderer must use texture unit 0
2608 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002609 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2610 setupDrawPureColorUniforms();
2611 setupDrawColorFilterUniforms();
2612 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002613 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002614
Romain Guy6620c6d2010-12-06 18:07:02 -08002615 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002616 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2617
Romain Guy211370f2012-02-01 16:10:55 -08002618 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002619
Raph Levien996e57c2012-07-23 15:22:52 -07002620 bool status;
Raph Levien8b4072d2012-07-30 15:50:00 -07002621 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
2622 SkPaint paintCopy(*paint);
2623 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2624 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Raph Levien996e57c2012-07-23 15:22:52 -07002625 positions, hasActiveLayer ? &bounds : NULL);
2626 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002627 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2628 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002629 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002630
2631 if (status && hasActiveLayer) {
2632 if (!pureTranslate) {
2633 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002634 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002635 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002636 }
Romain Guy694b5192010-07-21 21:33:20 -07002637
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002638 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002639
2640 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002641}
2642
Chet Haase48659092012-05-31 15:21:51 -07002643status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002644 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002645 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2646 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002647 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002648 }
2649
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002650 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002651 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2652 paint->getTextSize());
2653
2654 int alpha;
2655 SkXfermode::Mode mode;
2656 getAlphaAndMode(paint, &alpha, &mode);
2657
2658 mCaches.activeTexture(0);
2659 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002660 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002661 setupDrawDirtyRegionsDisabled();
2662 setupDrawWithTexture(true);
2663 setupDrawAlpha8Color(paint->getColor(), alpha);
2664 setupDrawColorFilter();
2665 setupDrawShader();
2666 setupDrawBlending(true, mode);
2667 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002668 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002669 setupDrawTexture(fontRenderer.getTexture(true));
2670 setupDrawPureColorUniforms();
2671 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002672 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002673 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002674
Romain Guy97771732012-02-28 18:17:02 -08002675 const Rect* clip = &mSnapshot->getLocalClip();
2676 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 -08002677
Romain Guy97771732012-02-28 18:17:02 -08002678 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002679
2680 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2681 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002682 if (hasActiveLayer) {
2683 mSnapshot->transform->mapRect(bounds);
2684 dirtyLayerUnchecked(bounds, getRegion());
2685 }
Romain Guy97771732012-02-28 18:17:02 -08002686 }
Chet Haase48659092012-05-31 15:21:51 -07002687
2688 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002689}
2690
Chet Haase48659092012-05-31 15:21:51 -07002691status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2692 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002693
Romain Guya1d3c912011-12-13 14:55:06 -08002694 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002695
Romain Guy33f6beb2012-02-16 19:24:51 -08002696 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002697 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002698 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002699 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002700
Romain Guy8b55f372010-08-18 17:10:07 -07002701 const float x = texture->left - texture->offset;
2702 const float y = texture->top - texture->offset;
2703
Romain Guy01d58e42011-01-19 21:54:02 -08002704 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002705
2706 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002707}
2708
Chet Haase48659092012-05-31 15:21:51 -07002709status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002710 if (!layer) {
2711 return DrawGlInfo::kStatusDone;
2712 }
2713
2714 Rect transformed;
2715 Rect clip;
2716 const bool rejected = quickRejectNoScissor(x, y,
2717 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2718
2719 if (rejected) {
Chet Haase48659092012-05-31 15:21:51 -07002720 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002721 }
2722
Romain Guy4ff0cf42012-08-06 14:51:10 -07002723 bool debugLayerUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -07002724 if (updateLayer(layer, true)) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002725 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002726 }
2727
Romain Guy87e2f7572012-09-24 11:37:12 -07002728 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002729 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002730
Romain Guy211370f2012-02-01 16:10:55 -08002731 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002732 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002733 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002734 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002735 const float a = layer->getAlpha() / 255.0f;
2736 SkiaColorFilter *oldFilter = mColorFilter;
2737 mColorFilter = layer->getColorFilter();
Romain Guyf219da52011-01-16 12:54:25 -08002738
Romain Guyc88e3572011-01-22 00:32:12 -08002739 setupDraw();
2740 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002741 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002742 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002743 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002744 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002745 setupDrawPureColorUniforms();
2746 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002747 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002748 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002749 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2750 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002751
Romain Guyd21b6e12011-11-30 20:21:23 -08002752 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002753 setupDrawModelViewTranslate(tx, ty,
2754 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002755 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002756 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002757 setupDrawModelViewTranslate(x, y,
2758 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2759 }
Romain Guyc88e3572011-01-22 00:32:12 -08002760 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002761
Romain Guyc88e3572011-01-22 00:32:12 -08002762 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2763 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002764
Romain Guyc88e3572011-01-22 00:32:12 -08002765 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002766
Chet Haased15ebf22012-09-05 11:40:29 -07002767 mColorFilter = oldFilter;
2768
Romain Guy3a3133d2011-02-01 22:59:58 -08002769#if DEBUG_LAYERS_AS_REGIONS
2770 drawRegionRects(layer->region);
2771#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002772 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002773
2774 if (debugLayerUpdate) {
2775 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2776 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2777 }
Romain Guyf219da52011-01-16 12:54:25 -08002778 }
Chet Haase48659092012-05-31 15:21:51 -07002779
2780 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002781}
2782
Romain Guy6926c722010-07-12 20:20:03 -07002783///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002784// Shaders
2785///////////////////////////////////////////////////////////////////////////////
2786
2787void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002788 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002789}
2790
Romain Guy06f96e22010-07-30 19:18:16 -07002791void OpenGLRenderer::setupShader(SkiaShader* shader) {
2792 mShader = shader;
2793 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002794 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002795 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002796}
2797
Romain Guyd27977d2010-07-14 19:18:51 -07002798///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002799// Color filters
2800///////////////////////////////////////////////////////////////////////////////
2801
2802void OpenGLRenderer::resetColorFilter() {
2803 mColorFilter = NULL;
2804}
2805
2806void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2807 mColorFilter = filter;
2808}
2809
2810///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002811// Drop shadow
2812///////////////////////////////////////////////////////////////////////////////
2813
2814void OpenGLRenderer::resetShadow() {
2815 mHasShadow = false;
2816}
2817
2818void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2819 mHasShadow = true;
2820 mShadowRadius = radius;
2821 mShadowDx = dx;
2822 mShadowDy = dy;
2823 mShadowColor = color;
2824}
2825
2826///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002827// Draw filters
2828///////////////////////////////////////////////////////////////////////////////
2829
2830void OpenGLRenderer::resetPaintFilter() {
2831 mHasDrawFilter = false;
2832}
2833
2834void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2835 mHasDrawFilter = true;
2836 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2837 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2838}
2839
2840SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002841 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002842
2843 uint32_t flags = paint->getFlags();
2844
2845 mFilteredPaint = *paint;
2846 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2847
2848 return &mFilteredPaint;
2849}
2850
2851///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002852// Drawing implementation
2853///////////////////////////////////////////////////////////////////////////////
2854
Romain Guy01d58e42011-01-19 21:54:02 -08002855void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2856 float x, float y, SkPaint* paint) {
2857 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2858 return;
2859 }
2860
2861 int alpha;
2862 SkXfermode::Mode mode;
2863 getAlphaAndMode(paint, &alpha, &mode);
2864
2865 setupDraw();
2866 setupDrawWithTexture(true);
2867 setupDrawAlpha8Color(paint->getColor(), alpha);
2868 setupDrawColorFilter();
2869 setupDrawShader();
2870 setupDrawBlending(true, mode);
2871 setupDrawProgram();
2872 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2873 setupDrawTexture(texture->id);
2874 setupDrawPureColorUniforms();
2875 setupDrawColorFilterUniforms();
2876 setupDrawShaderUniforms();
2877 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2878
2879 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2880
2881 finishDrawTexture();
2882}
2883
Romain Guyf607bdc2010-09-10 19:20:06 -07002884// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002885#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2886#define kStdUnderline_Offset (1.0f / 9.0f)
2887#define kStdUnderline_Thickness (1.0f / 18.0f)
2888
2889void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2890 float x, float y, SkPaint* paint) {
2891 // Handle underline and strike-through
2892 uint32_t flags = paint->getFlags();
2893 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002894 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002895 float underlineWidth = length;
2896 // If length is > 0.0f, we already measured the text for the text alignment
2897 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002898 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002899 }
2900
Romain Guy211370f2012-02-01 16:10:55 -08002901 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002902 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002903 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002904
Raph Levien8b4072d2012-07-30 15:50:00 -07002905 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002906 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002907
Romain Guyf6834472011-01-23 13:32:12 -08002908 int linesCount = 0;
2909 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2910 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2911
2912 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002913 float points[pointsCount];
2914 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002915
2916 if (flags & SkPaint::kUnderlineText_Flag) {
2917 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002918 points[currentPoint++] = left;
2919 points[currentPoint++] = top;
2920 points[currentPoint++] = left + underlineWidth;
2921 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002922 }
2923
2924 if (flags & SkPaint::kStrikeThruText_Flag) {
2925 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002926 points[currentPoint++] = left;
2927 points[currentPoint++] = top;
2928 points[currentPoint++] = left + underlineWidth;
2929 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002930 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002931
Romain Guy726aeba2011-06-01 14:52:00 -07002932 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002933
Romain Guy726aeba2011-06-01 14:52:00 -07002934 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002935 }
2936 }
2937}
2938
Romain Guy026c5e162010-06-28 17:12:22 -07002939void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002940 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002941 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002942 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002943 color |= 0x00ffffff;
2944 }
2945
Romain Guy70ca14e2010-12-13 18:24:33 -08002946 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002947 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002948 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002949 setupDrawShader();
2950 setupDrawColorFilter();
2951 setupDrawBlending(mode);
2952 setupDrawProgram();
2953 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2954 setupDrawColorUniforms();
2955 setupDrawShaderUniforms(ignoreTransform);
2956 setupDrawColorFilterUniforms();
2957 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002958
Romain Guyc95c8d62010-09-17 15:31:32 -07002959 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2960}
2961
Romain Guy82ba8142010-07-09 13:25:56 -07002962void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002963 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002964 int alpha;
2965 SkXfermode::Mode mode;
2966 getAlphaAndMode(paint, &alpha, &mode);
2967
Romain Guyd21b6e12011-11-30 20:21:23 -08002968 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002969
Romain Guy211370f2012-02-01 16:10:55 -08002970 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002971 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2972 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2973
Romain Guyd21b6e12011-11-30 20:21:23 -08002974 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002975 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2976 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2977 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2978 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002979 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002980 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2981 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2982 GL_TRIANGLE_STRIP, gMeshCount);
2983 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002984}
2985
Romain Guybd6b79b2010-06-26 00:13:53 -07002986void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002987 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2988 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002989 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002990}
2991
2992void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002993 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002994 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002995 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002996
Romain Guy746b7402010-10-26 16:27:31 -07002997 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002998 setupDrawWithTexture();
2999 setupDrawColor(alpha, alpha, alpha, alpha);
3000 setupDrawColorFilter();
3001 setupDrawBlending(blend, mode, swapSrcDst);
3002 setupDrawProgram();
3003 if (!dirty) {
3004 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07003005 }
Romain Guy5b3b3522010-10-27 18:57:51 -07003006 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003007 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003008 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003009 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003010 }
Romain Guy86568192010-12-14 15:55:39 -08003011 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003012 setupDrawColorFilterUniforms();
3013 setupDrawTexture(texture);
3014 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003015
Romain Guy6820ac82010-09-15 18:11:50 -07003016 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003017
3018 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003019}
3020
Romain Guya5aed0d2010-09-09 14:42:43 -07003021void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003022 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003023 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003024
Romain Guy82ba8142010-07-09 13:25:56 -07003025 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003026 // These blend modes are not supported by OpenGL directly and have
3027 // to be implemented using shaders. Since the shader will perform
3028 // the blending, turn blending off here
3029 // If the blend mode cannot be implemented using shaders, fall
3030 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003031 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08003032 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003033 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003034 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003035
Romain Guy82bc7a72012-01-03 14:13:39 -08003036 if (mCaches.blend) {
3037 glDisable(GL_BLEND);
3038 mCaches.blend = false;
3039 }
3040
3041 return;
3042 } else {
3043 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003044 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003045 }
3046
3047 if (!mCaches.blend) {
3048 glEnable(GL_BLEND);
3049 }
3050
3051 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3052 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3053
3054 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3055 glBlendFunc(sourceMode, destMode);
3056 mCaches.lastSrcMode = sourceMode;
3057 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003058 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003059 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003060 glDisable(GL_BLEND);
3061 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003062 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003063}
3064
Romain Guy889f8d12010-07-29 14:37:42 -07003065bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003066 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003067 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003068 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003069 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003070 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003071 }
Romain Guy6926c722010-07-12 20:20:03 -07003072 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003073}
3074
Romain Guy026c5e162010-06-28 17:12:22 -07003075void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003076 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003077 TextureVertex::setUV(v++, u1, v1);
3078 TextureVertex::setUV(v++, u2, v1);
3079 TextureVertex::setUV(v++, u1, v2);
3080 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003081}
3082
Chet Haase5c13d892010-10-08 08:37:55 -07003083void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003084 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003085 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003086}
3087
Romain Guy9d5316e2010-06-24 19:30:36 -07003088}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003089}; // namespace android