blob: b0328f5966f59c347838a03fc5c238b002c3905a [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
Chris Craikcb4d6002012-09-25 12:00:29 -07001251bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom, SkPaint* paint) {
1252 if (paint->getStyle() != SkPaint::kFill_Style) {
1253 float outset = paint->getStrokeWidth() * 0.5f;
1254 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1255 } else {
1256 return quickReject(left, top, right, bottom);
1257 }
1258}
1259
Romain Guyc7d53492010-06-25 13:41:57 -07001260bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001261 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001262 return true;
1263 }
1264
Romain Guy1d83e192010-08-17 11:37:00 -07001265 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001266 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001267 r.snapToPixelBoundaries();
1268
1269 Rect clipRect(*mSnapshot->clipRect);
1270 clipRect.snapToPixelBoundaries();
1271
Romain Guy586cae32012-07-13 15:28:31 -07001272 bool rejected = !clipRect.intersects(r);
1273 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001274 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001275 }
1276
1277 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001278}
1279
Romain Guy079ba2c2010-07-16 14:12:24 -07001280bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1281 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001282 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001283 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001284 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001285 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001286}
1287
Chet Haasea23eed82012-04-12 15:19:04 -07001288Rect* OpenGLRenderer::getClipRect() {
1289 return mSnapshot->clipRect;
1290}
1291
Romain Guyf6a11b82010-06-23 17:47:49 -07001292///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001293// Drawing commands
1294///////////////////////////////////////////////////////////////////////////////
1295
Romain Guy54be1cd2011-06-13 19:04:27 -07001296void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001297 // TODO: It would be best if we could do this before quickReject()
1298 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001299 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001300 if (mDirtyClip) {
1301 setScissorFromClip();
1302 }
1303 mDescription.reset();
1304 mSetShaderColor = false;
1305 mColorSet = false;
1306 mColorA = mColorR = mColorG = mColorB = 0.0f;
1307 mTextureUnit = 0;
1308 mTrackDirtyRegions = true;
1309}
1310
1311void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1312 mDescription.hasTexture = true;
1313 mDescription.hasAlpha8Texture = isAlpha8;
1314}
1315
Romain Guyaa6c24c2011-04-28 18:40:04 -07001316void OpenGLRenderer::setupDrawWithExternalTexture() {
1317 mDescription.hasExternalTexture = true;
1318}
1319
Romain Guy15bc6432011-12-13 13:11:32 -08001320void OpenGLRenderer::setupDrawNoTexture() {
1321 mCaches.disbaleTexCoordsVertexArray();
1322}
1323
Chris Craik710f46d2012-09-17 17:25:49 -07001324void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001325 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001326}
1327
Chris Craik710f46d2012-09-17 17:25:49 -07001328void OpenGLRenderer::setupDrawVertexShape() {
1329 mDescription.isVertexShape = true;
Chris Craik6ebdc112012-08-31 18:24:33 -07001330}
1331
Romain Guyed6fcb02011-03-21 13:11:28 -07001332void OpenGLRenderer::setupDrawPoint(float pointSize) {
1333 mDescription.isPoint = true;
1334 mDescription.pointSize = pointSize;
1335}
1336
Romain Guy70ca14e2010-12-13 18:24:33 -08001337void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001338 setupDrawColor(color, (color >> 24) & 0xFF);
1339}
1340
1341void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1342 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001343 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1344 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001345 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001346 mColorR = a * ((color >> 16) & 0xFF);
1347 mColorG = a * ((color >> 8) & 0xFF);
1348 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001349 mColorSet = true;
1350 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1351}
1352
Romain Guy86568192010-12-14 15:55:39 -08001353void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1354 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001355 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1356 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001357 const float a = mColorA / 255.0f;
1358 mColorR = a * ((color >> 16) & 0xFF);
1359 mColorG = a * ((color >> 8) & 0xFF);
1360 mColorB = a * ((color ) & 0xFF);
1361 mColorSet = true;
1362 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1363}
1364
Romain Guy41210632012-07-16 17:04:24 -07001365void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1366 mCaches.fontRenderer->describe(mDescription, paint);
1367}
1368
Romain Guy70ca14e2010-12-13 18:24:33 -08001369void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1370 mColorA = a;
1371 mColorR = r;
1372 mColorG = g;
1373 mColorB = b;
1374 mColorSet = true;
1375 mSetShaderColor = mDescription.setColor(r, g, b, a);
1376}
1377
1378void OpenGLRenderer::setupDrawShader() {
1379 if (mShader) {
1380 mShader->describe(mDescription, mCaches.extensions);
1381 }
1382}
1383
1384void OpenGLRenderer::setupDrawColorFilter() {
1385 if (mColorFilter) {
1386 mColorFilter->describe(mDescription, mCaches.extensions);
1387 }
1388}
1389
Romain Guyf09ef512011-05-27 11:43:46 -07001390void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1391 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1392 mColorA = 1.0f;
1393 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001394 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001395 }
1396}
1397
Romain Guy70ca14e2010-12-13 18:24:33 -08001398void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001399 // When the blending mode is kClear_Mode, we need to use a modulate color
1400 // argb=1,0,0,0
1401 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001402 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1403 mDescription, swapSrcDst);
1404}
1405
1406void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001407 // When the blending mode is kClear_Mode, we need to use a modulate color
1408 // argb=1,0,0,0
1409 accountForClear(mode);
Romain Guye83221c2012-09-24 16:01:35 -07001410 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()) ||
1411 (mColorFilter && mColorFilter->blend()), mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001412}
1413
1414void OpenGLRenderer::setupDrawProgram() {
1415 useProgram(mCaches.programCache.get(mDescription));
1416}
1417
1418void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1419 mTrackDirtyRegions = false;
1420}
1421
1422void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1423 bool ignoreTransform) {
1424 mModelView.loadTranslate(left, top, 0.0f);
1425 if (!ignoreTransform) {
1426 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1427 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1428 } else {
1429 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1430 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1431 }
1432}
1433
Chet Haase8a5cc922011-04-26 07:28:09 -07001434void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1435 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001436}
1437
Romain Guy70ca14e2010-12-13 18:24:33 -08001438void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1439 bool ignoreTransform, bool ignoreModelView) {
1440 if (!ignoreModelView) {
1441 mModelView.loadTranslate(left, top, 0.0f);
1442 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001443 } else {
1444 mModelView.loadIdentity();
1445 }
Romain Guy86568192010-12-14 15:55:39 -08001446 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1447 if (!ignoreTransform) {
1448 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1449 if (mTrackDirtyRegions && dirty) {
1450 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1451 }
1452 } else {
1453 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1454 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1455 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001456}
1457
Romain Guyed6fcb02011-03-21 13:11:28 -07001458void OpenGLRenderer::setupDrawPointUniforms() {
1459 int slot = mCaches.currentProgram->getUniform("pointSize");
1460 glUniform1f(slot, mDescription.pointSize);
1461}
1462
Romain Guy70ca14e2010-12-13 18:24:33 -08001463void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001464 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001465 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1466 }
1467}
1468
Romain Guy86568192010-12-14 15:55:39 -08001469void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001470 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001471 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001472 }
1473}
1474
Romain Guy70ca14e2010-12-13 18:24:33 -08001475void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1476 if (mShader) {
1477 if (ignoreTransform) {
1478 mModelView.loadInverse(*mSnapshot->transform);
1479 }
1480 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1481 }
1482}
1483
Romain Guy8d0d4782010-12-14 20:13:35 -08001484void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1485 if (mShader) {
1486 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1487 }
1488}
1489
Romain Guy70ca14e2010-12-13 18:24:33 -08001490void OpenGLRenderer::setupDrawColorFilterUniforms() {
1491 if (mColorFilter) {
1492 mColorFilter->setupProgram(mCaches.currentProgram);
1493 }
1494}
1495
Romain Guy41210632012-07-16 17:04:24 -07001496void OpenGLRenderer::setupDrawTextGammaUniforms() {
1497 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1498}
1499
Romain Guy70ca14e2010-12-13 18:24:33 -08001500void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001501 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001502 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001503 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001504}
1505
1506void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1507 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001508 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001509 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001510}
1511
Romain Guyaa6c24c2011-04-28 18:40:04 -07001512void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1513 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001514 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001515 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001516}
1517
Romain Guy8f0095c2011-05-02 17:24:22 -07001518void OpenGLRenderer::setupDrawTextureTransform() {
1519 mDescription.hasTextureTransform = true;
1520}
1521
1522void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001523 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1524 GL_FALSE, &transform.data[0]);
1525}
1526
Romain Guy70ca14e2010-12-13 18:24:33 -08001527void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001528 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001529 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001530 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001531 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001532 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001533 }
Romain Guyd71dd362011-12-12 19:03:35 -08001534
Chris Craikcb4d6002012-09-25 12:00:29 -07001535 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001536 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001537 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001538 }
1539
1540 mCaches.unbindIndicesBuffer();
1541}
1542
1543void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1544 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001545 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001546 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001547 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001548 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001549}
1550
Chet Haase5b0200b2011-04-13 17:58:08 -07001551void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001552 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001553 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001554 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001555}
1556
1557/**
1558 * Sets up the shader to draw an AA line. We draw AA lines with quads, where there is an
Chet Haase99585ad2011-05-02 15:00:16 -07001559 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1560 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1561 * attributes (one per vertex) are values from zero to one that tells the fragment
1562 * shader where the fragment is in relation to the line width/length overall; these values are
1563 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1564 * region of the line.
1565 * Note that we only pass down the width values in this setup function. The length coordinates
1566 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001567 */
Chet Haase99585ad2011-05-02 15:00:16 -07001568void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001569 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001570 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001571 mCaches.bindPositionVertexPointer(force, vertices, gAAVertexStride);
Romain Guyf3a910b42011-12-12 20:35:21 -08001572 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001573 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001574
Romain Guy7b631422012-04-04 11:38:54 -07001575 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001576 glEnableVertexAttribArray(widthSlot);
1577 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001578
Romain Guy7b631422012-04-04 11:38:54 -07001579 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001580 glEnableVertexAttribArray(lengthSlot);
1581 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001582
Chet Haase5b0200b2011-04-13 17:58:08 -07001583 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001584 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001585}
1586
1587void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1588 glDisableVertexAttribArray(widthSlot);
1589 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001590}
1591
Romain Guy70ca14e2010-12-13 18:24:33 -08001592void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001593}
1594
1595///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001596// Drawing
1597///////////////////////////////////////////////////////////////////////////////
1598
Chet Haase1271e2c2012-04-20 09:54:27 -07001599status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001600 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001601
Romain Guy0fe478e2010-11-08 12:08:41 -08001602 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1603 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001604 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001605 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001606 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001607
Romain Guy65549432012-03-26 16:45:05 -07001608 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001609}
1610
Chet Haaseed30fd82011-04-22 16:18:45 -07001611void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1612 if (displayList) {
1613 displayList->output(*this, level);
1614 }
1615}
1616
Romain Guya168d732011-03-18 16:50:13 -07001617void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1618 int alpha;
1619 SkXfermode::Mode mode;
1620 getAlphaAndMode(paint, &alpha, &mode);
1621
Romain Guya168d732011-03-18 16:50:13 -07001622 float x = left;
1623 float y = top;
1624
Romain Guye3c26852011-07-25 16:36:01 -07001625 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001626 bool ignoreTransform = false;
1627 if (mSnapshot->transform->isPureTranslate()) {
1628 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1629 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1630 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001631 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001632 } else {
1633 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001634 }
1635
1636 setupDraw();
1637 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001638 if (paint) {
1639 setupDrawAlpha8Color(paint->getColor(), alpha);
1640 }
Romain Guya168d732011-03-18 16:50:13 -07001641 setupDrawColorFilter();
1642 setupDrawShader();
1643 setupDrawBlending(true, mode);
1644 setupDrawProgram();
1645 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001646
Romain Guya168d732011-03-18 16:50:13 -07001647 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001648 texture->setWrap(GL_CLAMP_TO_EDGE);
1649 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001650
Romain Guya168d732011-03-18 16:50:13 -07001651 setupDrawPureColorUniforms();
1652 setupDrawColorFilterUniforms();
1653 setupDrawShaderUniforms();
1654 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1655
1656 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1657
1658 finishDrawTexture();
1659}
1660
Chet Haase48659092012-05-31 15:21:51 -07001661status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001662 const float right = left + bitmap->width();
1663 const float bottom = top + bitmap->height();
1664
1665 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001666 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001667 }
1668
Romain Guya1d3c912011-12-13 14:55:06 -08001669 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001670 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001671 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001672 const AutoTexture autoCleanup(texture);
1673
Romain Guy211370f2012-02-01 16:10:55 -08001674 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001675 drawAlphaBitmap(texture, left, top, paint);
1676 } else {
1677 drawTextureRect(left, top, right, bottom, texture, paint);
1678 }
Chet Haase48659092012-05-31 15:21:51 -07001679
1680 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001681}
1682
Chet Haase48659092012-05-31 15:21:51 -07001683status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001684 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1685 const mat4 transform(*matrix);
1686 transform.mapRect(r);
1687
Romain Guy6926c722010-07-12 20:20:03 -07001688 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001689 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001690 }
1691
Romain Guya1d3c912011-12-13 14:55:06 -08001692 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001693 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001694 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001695 const AutoTexture autoCleanup(texture);
1696
Romain Guy5b3b3522010-10-27 18:57:51 -07001697 // This could be done in a cheaper way, all we need is pass the matrix
1698 // to the vertex shader. The save/restore is a bit overkill.
1699 save(SkCanvas::kMatrix_SaveFlag);
1700 concatMatrix(matrix);
1701 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1702 restore();
Chet Haase48659092012-05-31 15:21:51 -07001703
1704 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001705}
1706
Chet Haase48659092012-05-31 15:21:51 -07001707status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001708 const float right = left + bitmap->width();
1709 const float bottom = top + bitmap->height();
1710
1711 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001712 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001713 }
1714
1715 mCaches.activeTexture(0);
1716 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1717 const AutoTexture autoCleanup(texture);
1718
1719 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001720
1721 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001722}
1723
Chet Haase48659092012-05-31 15:21:51 -07001724status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001725 float* vertices, int* colors, SkPaint* paint) {
1726 // TODO: Do a quickReject
1727 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001728 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001729 }
1730
Romain Guya1d3c912011-12-13 14:55:06 -08001731 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001732 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001733 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001734 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001735
Romain Guyd21b6e12011-11-30 20:21:23 -08001736 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1737 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001738
1739 int alpha;
1740 SkXfermode::Mode mode;
1741 getAlphaAndMode(paint, &alpha, &mode);
1742
Romain Guy5a7b4662011-01-20 19:09:30 -08001743 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001744
Romain Guyb18d2d02011-02-10 15:52:54 -08001745 float left = FLT_MAX;
1746 float top = FLT_MAX;
1747 float right = FLT_MIN;
1748 float bottom = FLT_MIN;
1749
Romain Guy211370f2012-02-01 16:10:55 -08001750 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001751
Romain Guya566b7c2011-01-23 16:36:11 -08001752 // TODO: Support the colors array
1753 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001754 TextureVertex* vertex = mesh;
1755 for (int32_t y = 0; y < meshHeight; y++) {
1756 for (int32_t x = 0; x < meshWidth; x++) {
1757 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1758
1759 float u1 = float(x) / meshWidth;
1760 float u2 = float(x + 1) / meshWidth;
1761 float v1 = float(y) / meshHeight;
1762 float v2 = float(y + 1) / meshHeight;
1763
1764 int ax = i + (meshWidth + 1) * 2;
1765 int ay = ax + 1;
1766 int bx = i;
1767 int by = bx + 1;
1768 int cx = i + 2;
1769 int cy = cx + 1;
1770 int dx = i + (meshWidth + 1) * 2 + 2;
1771 int dy = dx + 1;
1772
1773 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1774 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1775 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1776
1777 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1778 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1779 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001780
Romain Guyb18d2d02011-02-10 15:52:54 -08001781 if (hasActiveLayer) {
1782 // TODO: This could be optimized to avoid unnecessary ops
1783 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1784 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1785 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1786 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1787 }
Romain Guy5a7b4662011-01-20 19:09:30 -08001788 }
1789 }
1790
Romain Guyb18d2d02011-02-10 15:52:54 -08001791 if (hasActiveLayer) {
1792 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1793 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001794
Romain Guy5a7b4662011-01-20 19:09:30 -08001795 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1796 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001797 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001798
1799 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001800}
1801
Chet Haase48659092012-05-31 15:21:51 -07001802status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001803 float srcLeft, float srcTop, float srcRight, float srcBottom,
1804 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001805 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001806 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001807 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001808 }
1809
Romain Guya1d3c912011-12-13 14:55:06 -08001810 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001811 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001812 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001813 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001814
Romain Guy8ba548f2010-06-30 19:21:21 -07001815 const float width = texture->width;
1816 const float height = texture->height;
1817
Romain Guy68169722011-08-22 17:33:33 -07001818 const float u1 = fmax(0.0f, srcLeft / width);
1819 const float v1 = fmax(0.0f, srcTop / height);
1820 const float u2 = fmin(1.0f, srcRight / width);
1821 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001822
Romain Guy03750a02010-10-18 14:06:08 -07001823 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001824 resetDrawTextureTexCoords(u1, v1, u2, v2);
1825
Romain Guy03750a02010-10-18 14:06:08 -07001826 int alpha;
1827 SkXfermode::Mode mode;
1828 getAlphaAndMode(paint, &alpha, &mode);
1829
Romain Guyd21b6e12011-11-30 20:21:23 -08001830 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1831
Romain Guy211370f2012-02-01 16:10:55 -08001832 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001833 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1834 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1835
Romain Guyb5014982011-07-28 15:39:12 -07001836 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001837 // Enable linear filtering if the source rectangle is scaled
1838 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001839 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001840 }
Romain Guyb5014982011-07-28 15:39:12 -07001841
Romain Guyd21b6e12011-11-30 20:21:23 -08001842 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001843 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1844 texture->id, alpha / 255.0f, mode, texture->blend,
1845 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1846 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1847 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001848 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001849 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1850 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1851 GL_TRIANGLE_STRIP, gMeshCount);
1852 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001853
1854 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001855
1856 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001857}
1858
Chet Haase48659092012-05-31 15:21:51 -07001859status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001860 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001861 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001862 int alpha;
1863 SkXfermode::Mode mode;
1864 getAlphaAndModeDirect(paint, &alpha, &mode);
1865
1866 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1867 left, top, right, bottom, alpha, mode);
1868}
1869
1870status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1871 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1872 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001873 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001874 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001875 }
1876
Romain Guybe6f9dc2012-07-16 12:41:17 -07001877 alpha *= mSnapshot->alpha;
1878
Romain Guya1d3c912011-12-13 14:55:06 -08001879 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001880 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001881 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001882 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001883 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1884 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001885
Romain Guy2728f962010-10-08 18:36:15 -07001886 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001887 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001888
Romain Guy211370f2012-02-01 16:10:55 -08001889 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001890 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001891 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001892 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001893 const float offsetX = left + mSnapshot->transform->getTranslateX();
1894 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001895 const size_t count = mesh->quads.size();
1896 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001897 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001898 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001899 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1900 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1901 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001902 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001903 dirtyLayer(left + bounds.left, top + bounds.top,
1904 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001905 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001906 }
1907 }
1908
Romain Guy211370f2012-02-01 16:10:55 -08001909 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001910 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1911 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1912
1913 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1914 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1915 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1916 true, !mesh->hasEmptyQuads);
1917 } else {
1918 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1919 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1920 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1921 true, !mesh->hasEmptyQuads);
1922 }
Romain Guy054dc182010-10-15 17:55:25 -07001923 }
Chet Haase48659092012-05-31 15:21:51 -07001924
1925 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001926}
1927
Chet Haase99ecdc42011-05-06 12:06:34 -07001928/**
Chris Craikcb4d6002012-09-25 12:00:29 -07001929 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
1930 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
1931 * screen space in all directions. However, instead of using a fragment shader to compute the
1932 * translucency of the color from its position, we simply use a varying parameter to define how far
1933 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
1934 *
1935 * Doesn't yet support joins, caps, or path effects.
Chet Haase858aa932011-05-12 09:06:00 -07001936 */
Chris Craikcb4d6002012-09-25 12:00:29 -07001937void OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
1938 int color = paint->getColor();
1939 SkPaint::Style style = paint->getStyle();
1940 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
1941 bool isAA = paint->isAntiAlias();
1942
Chris Craik710f46d2012-09-17 17:25:49 -07001943 VertexBuffer vertexBuffer;
1944 // TODO: try clipping large paths to viewport
Chris Craikcb4d6002012-09-25 12:00:29 -07001945 PathRenderer::convexPathVertices(path, paint, mSnapshot->transform, vertexBuffer);
Romain Guy04299382012-07-18 17:15:41 -07001946
Chris Craik710f46d2012-09-17 17:25:49 -07001947 setupDraw();
1948 setupDrawNoTexture();
1949 if (isAA) setupDrawAA();
1950 setupDrawVertexShape();
1951 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1952 setupDrawColorFilter();
1953 setupDrawShader();
1954 setupDrawBlending(isAA, mode);
1955 setupDrawProgram();
Chris Craikcb4d6002012-09-25 12:00:29 -07001956 setupDrawModelViewIdentity();
Chris Craik710f46d2012-09-17 17:25:49 -07001957 setupDrawColorUniforms();
1958 setupDrawColorFilterUniforms();
1959 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07001960
Chris Craik710f46d2012-09-17 17:25:49 -07001961 void* vertices = vertexBuffer.getBuffer();
1962 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001963 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07001964 mCaches.resetTexCoordsVertexPointer();
1965 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07001966
Chris Craik710f46d2012-09-17 17:25:49 -07001967 int alphaSlot = -1;
1968 if (isAA) {
1969 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
1970 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07001971
Chris Craik710f46d2012-09-17 17:25:49 -07001972 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07001973 glEnableVertexAttribArray(alphaSlot);
1974 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07001975 }
Romain Guy04299382012-07-18 17:15:41 -07001976
Chris Craikcb4d6002012-09-25 12:00:29 -07001977 SkRect bounds = PathRenderer::computePathBounds(path, paint);
Chris Craik710f46d2012-09-17 17:25:49 -07001978 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy04299382012-07-18 17:15:41 -07001979
Chris Craik710f46d2012-09-17 17:25:49 -07001980 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07001981
Chris Craik710f46d2012-09-17 17:25:49 -07001982 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07001983 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001984 }
Chet Haase858aa932011-05-12 09:06:00 -07001985}
1986
1987/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001988 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1989 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1990 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1991 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1992 * of the line. Hairlines are more involved because we need to account for transform scaling
1993 * to end up with a one-pixel-wide line in screen space..
1994 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1995 * in combination with values that we calculate and pass down in this method. The basic approach
1996 * is that the quad we create contains both the core line area plus a bounding area in which
1997 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1998 * proportion of the width and the length of a given segment is represented by the boundary
1999 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
2000 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
2001 * on the inside). This ends up giving the result we want, with pixels that are completely
2002 * 'inside' the line area being filled opaquely and the other pixels being filled according to
2003 * how far into the boundary region they are, which is determined by shader interpolation.
2004 */
Chet Haase48659092012-05-31 15:21:51 -07002005status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
2006 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002007
2008 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07002009 // We use half the stroke width here because we're going to position the quad
2010 // corner vertices half of the width away from the line endpoints
2011 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002012 // A stroke width of 0 has a special meaning in Skia:
2013 // it draws a line 1 px wide regardless of current transform
2014 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07002015
Chet Haase99ecdc42011-05-06 12:06:34 -07002016 float inverseScaleX = 1.0f;
2017 float inverseScaleY = 1.0f;
2018 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07002019
Chet Haase8a5cc922011-04-26 07:28:09 -07002020 int alpha;
2021 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07002022
Chet Haase8a5cc922011-04-26 07:28:09 -07002023 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002024 int verticesCount = count;
2025 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07002026 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07002027 verticesCount += (count - 4);
2028 }
2029
2030 if (isHairLine || isAA) {
2031 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
2032 // the line on the screen should always be one pixel wide regardless of scale. For
2033 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08002034 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07002035 Matrix4 *mat = mSnapshot->transform;
2036 float m00 = mat->data[Matrix4::kScaleX];
2037 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07002038 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07002039 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07002040
Romain Guy211370f2012-02-01 16:10:55 -08002041 float scaleX = sqrtf(m00 * m00 + m01 * m01);
2042 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07002043
Chet Haase99ecdc42011-05-06 12:06:34 -07002044 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
2045 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07002046
Chet Haase99ecdc42011-05-06 12:06:34 -07002047 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
2048 scaled = true;
2049 }
2050 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002051 }
Chet Haase8a5cc922011-04-26 07:28:09 -07002052
2053 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07002054
2055 mCaches.enableScissor();
2056
Chet Haase8a5cc922011-04-26 07:28:09 -07002057 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002058 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002059 if (isAA) {
Chris Craik710f46d2012-09-17 17:25:49 -07002060 setupDrawAA();
Chet Haase8a5cc922011-04-26 07:28:09 -07002061 }
2062 setupDrawColor(paint->getColor(), alpha);
2063 setupDrawColorFilter();
2064 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08002065 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07002066 setupDrawProgram();
Chris Craikcb4d6002012-09-25 12:00:29 -07002067 setupDrawModelViewIdentity();
Chet Haase8a5cc922011-04-26 07:28:09 -07002068 setupDrawColorUniforms();
2069 setupDrawColorFilterUniforms();
2070 setupDrawShaderIdentityUniforms();
2071
2072 if (isHairLine) {
2073 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07002074 halfStrokeWidth = isAA? 1 : .5;
2075 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002076 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07002077 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07002078 }
Romain Guy7b631422012-04-04 11:38:54 -07002079
2080 int widthSlot;
2081 int lengthSlot;
2082
Chet Haase5b0200b2011-04-13 17:58:08 -07002083 Vertex lines[verticesCount];
2084 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002085
Chet Haase99585ad2011-05-02 15:00:16 -07002086 AAVertex wLines[verticesCount];
2087 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002088
Romain Guy211370f2012-02-01 16:10:55 -08002089 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002090 setupDrawVertices(vertices);
2091 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002092 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2093 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002094 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002095 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2096 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002097 // We will need to calculate the actual width proportion on each segment for
2098 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002099 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002100 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2101 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002102 }
2103
Chet Haase99ecdc42011-05-06 12:06:34 -07002104 AAVertex* prevAAVertex = NULL;
2105 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002106
Chet Haase99585ad2011-05-02 15:00:16 -07002107 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002108 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002109
Chet Haase5b0200b2011-04-13 17:58:08 -07002110 for (int i = 0; i < count; i += 4) {
2111 // a = start point, b = end point
2112 vec2 a(points[i], points[i + 1]);
2113 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002114
Chet Haase99585ad2011-05-02 15:00:16 -07002115 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002116 float boundaryLengthProportion = 0;
2117 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002118
Chet Haase5b0200b2011-04-13 17:58:08 -07002119 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002120 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002121 float x = n.x;
2122 n.x = -n.y;
2123 n.y = x;
2124
Chet Haase8a5cc922011-04-26 07:28:09 -07002125 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002126 if (isAA) {
2127 float wideningFactor;
2128 if (fabs(n.x) >= fabs(n.y)) {
2129 wideningFactor = fabs(1.0f / n.x);
2130 } else {
2131 wideningFactor = fabs(1.0f / n.y);
2132 }
2133 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002134 }
Romain Guy7b631422012-04-04 11:38:54 -07002135
Chet Haase99ecdc42011-05-06 12:06:34 -07002136 if (scaled) {
2137 n.x *= inverseScaleX;
2138 n.y *= inverseScaleY;
2139 }
2140 } else if (scaled) {
2141 // Extend n by .5 pixel on each side, post-transform
2142 vec2 extendedN = n.copyNormalized();
2143 extendedN /= 2;
2144 extendedN.x *= inverseScaleX;
2145 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002146
Chet Haase99ecdc42011-05-06 12:06:34 -07002147 float extendedNLength = extendedN.length();
2148 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002149 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002150 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002151 }
Romain Guy7b631422012-04-04 11:38:54 -07002152
Chet Haase99585ad2011-05-02 15:00:16 -07002153 // aa lines expand the endpoint vertices to encompass the AA boundary
2154 if (isAA) {
2155 vec2 abVector = (b - a);
2156 length = abVector.length();
2157 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002158
Chet Haase99ecdc42011-05-06 12:06:34 -07002159 if (scaled) {
2160 abVector.x *= inverseScaleX;
2161 abVector.y *= inverseScaleY;
2162 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002163 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002164 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002165 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002166 }
Romain Guy7b631422012-04-04 11:38:54 -07002167
Chet Haase99ecdc42011-05-06 12:06:34 -07002168 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002169 a -= abVector;
2170 b += abVector;
2171 }
2172
Chet Haase5b0200b2011-04-13 17:58:08 -07002173 // Four corners of the rectangle defining a thick line
2174 vec2 p1 = a - n;
2175 vec2 p2 = a + n;
2176 vec2 p3 = b + n;
2177 vec2 p4 = b - n;
2178
Chet Haase99585ad2011-05-02 15:00:16 -07002179
Chet Haase5b0200b2011-04-13 17:58:08 -07002180 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2181 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2182 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2183 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2184
Romain Guy04299382012-07-18 17:15:41 -07002185 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002186 if (!isAA) {
2187 if (prevVertex != NULL) {
2188 // Issue two repeat vertices to create degenerate triangles to bridge
2189 // between the previous line and the new one. This is necessary because
2190 // we are creating a single triangle_strip which will contain
2191 // potentially discontinuous line segments.
2192 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2193 Vertex::set(vertices++, p1.x, p1.y);
2194 generatedVerticesCount += 2;
2195 }
Romain Guy7b631422012-04-04 11:38:54 -07002196
Chet Haase5b0200b2011-04-13 17:58:08 -07002197 Vertex::set(vertices++, p1.x, p1.y);
2198 Vertex::set(vertices++, p2.x, p2.y);
2199 Vertex::set(vertices++, p4.x, p4.y);
2200 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002201
Chet Haase5b0200b2011-04-13 17:58:08 -07002202 prevVertex = vertices - 1;
2203 generatedVerticesCount += 4;
2204 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002205 if (!isHairLine && scaled) {
2206 // Must set width proportions per-segment for scaled non-hairlines to use the
2207 // correct AA boundary dimensions
2208 if (boundaryWidthSlot < 0) {
2209 boundaryWidthSlot =
2210 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002211 }
Romain Guy7b631422012-04-04 11:38:54 -07002212
Chet Haase99ecdc42011-05-06 12:06:34 -07002213 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002214 }
Romain Guy7b631422012-04-04 11:38:54 -07002215
Chet Haase99585ad2011-05-02 15:00:16 -07002216 if (boundaryLengthSlot < 0) {
2217 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002218 }
Romain Guy7b631422012-04-04 11:38:54 -07002219
Chet Haase99ecdc42011-05-06 12:06:34 -07002220 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002221
Chet Haase5b0200b2011-04-13 17:58:08 -07002222 if (prevAAVertex != NULL) {
2223 // Issue two repeat vertices to create degenerate triangles to bridge
2224 // between the previous line and the new one. This is necessary because
2225 // we are creating a single triangle_strip which will contain
2226 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002227 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2228 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2229 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002230 generatedVerticesCount += 2;
2231 }
Romain Guy7b631422012-04-04 11:38:54 -07002232
Chet Haase99585ad2011-05-02 15:00:16 -07002233 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2234 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2235 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2236 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002237
Chet Haase5b0200b2011-04-13 17:58:08 -07002238 prevAAVertex = aaVertices - 1;
2239 generatedVerticesCount += 4;
2240 }
Romain Guy7b631422012-04-04 11:38:54 -07002241
Chet Haase99585ad2011-05-02 15:00:16 -07002242 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2243 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2244 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002245 }
2246 }
Romain Guy7b631422012-04-04 11:38:54 -07002247
Chet Haase5b0200b2011-04-13 17:58:08 -07002248 if (generatedVerticesCount > 0) {
2249 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2250 }
Romain Guy7b631422012-04-04 11:38:54 -07002251
2252 if (isAA) {
2253 finishDrawAALine(widthSlot, lengthSlot);
2254 }
Chet Haase48659092012-05-31 15:21:51 -07002255
2256 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002257}
2258
Chet Haase48659092012-05-31 15:21:51 -07002259status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2260 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002261
2262 // TODO: The paint's cap style defines whether the points are square or circular
2263 // TODO: Handle AA for round points
2264
Chet Haase5b0200b2011-04-13 17:58:08 -07002265 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002266 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002267 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002268 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002269 if (isHairLine) {
2270 // Now that we know it's hairline, we can set the effective width, to be used later
2271 strokeWidth = 1.0f;
2272 }
2273 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002274 int alpha;
2275 SkXfermode::Mode mode;
2276 getAlphaAndMode(paint, &alpha, &mode);
2277
2278 int verticesCount = count >> 1;
2279 int generatedVerticesCount = 0;
2280
2281 TextureVertex pointsData[verticesCount];
2282 TextureVertex* vertex = &pointsData[0];
2283
Romain Guy04299382012-07-18 17:15:41 -07002284 // TODO: We should optimize this method to not generate vertices for points
2285 // that lie outside of the clip.
2286 mCaches.enableScissor();
2287
Romain Guyed6fcb02011-03-21 13:11:28 -07002288 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002289 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002290 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002291 setupDrawColor(paint->getColor(), alpha);
2292 setupDrawColorFilter();
2293 setupDrawShader();
2294 setupDrawBlending(mode);
2295 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002296 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002297 setupDrawColorUniforms();
2298 setupDrawColorFilterUniforms();
2299 setupDrawPointUniforms();
2300 setupDrawShaderIdentityUniforms();
2301 setupDrawMesh(vertex);
2302
2303 for (int i = 0; i < count; i += 2) {
2304 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2305 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002306
Chet Haase8a5cc922011-04-26 07:28:09 -07002307 float left = points[i] - halfWidth;
2308 float right = points[i] + halfWidth;
2309 float top = points[i + 1] - halfWidth;
2310 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002311
Chet Haase8a5cc922011-04-26 07:28:09 -07002312 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002313 }
2314
2315 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002316
2317 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002318}
2319
Chet Haase48659092012-05-31 15:21:51 -07002320status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002321 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002322 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002323
Romain Guyae88e5e2010-10-22 17:49:18 -07002324 Rect& clip(*mSnapshot->clipRect);
2325 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002326
Romain Guy3d58c032010-07-14 16:34:53 -07002327 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002328
2329 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002330}
Romain Guy9d5316e2010-06-24 19:30:36 -07002331
Chet Haase48659092012-05-31 15:21:51 -07002332status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2333 SkPaint* paint) {
2334 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002335 const AutoTexture autoCleanup(texture);
2336
2337 const float x = left + texture->left - texture->offset;
2338 const float y = top + texture->top - texture->offset;
2339
2340 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002341
2342 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002343}
2344
Chet Haase48659092012-05-31 15:21:51 -07002345status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002346 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002347 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002348 return DrawGlInfo::kStatusDone;
2349 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002350
Chris Craikcb4d6002012-09-25 12:00:29 -07002351 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002352 mCaches.activeTexture(0);
2353 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2354 right - left, bottom - top, rx, ry, p);
2355 return drawShape(left, top, texture, p);
2356 }
2357
2358 SkPath path;
2359 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002360 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2361 float outset = p->getStrokeWidth() / 2;
2362 rect.outset(outset, outset);
2363 rx += outset;
2364 ry += outset;
2365 }
Chris Craik710f46d2012-09-17 17:25:49 -07002366 path.addRoundRect(rect, rx, ry);
Chris Craikcb4d6002012-09-25 12:00:29 -07002367 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002368
2369 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002370}
2371
Chris Craik710f46d2012-09-17 17:25:49 -07002372status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002373 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2374 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002375 return DrawGlInfo::kStatusDone;
2376 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002377 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002378 mCaches.activeTexture(0);
2379 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2380 return drawShape(x - radius, y - radius, texture, p);
2381 }
2382
2383 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002384 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2385 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2386 } else {
2387 path.addCircle(x, y, radius);
2388 }
2389 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002390
2391 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002392}
Romain Guy01d58e42011-01-19 21:54:02 -08002393
Chet Haase48659092012-05-31 15:21:51 -07002394status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002395 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002396 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002397 return DrawGlInfo::kStatusDone;
2398 }
Romain Guy01d58e42011-01-19 21:54:02 -08002399
Chris Craikcb4d6002012-09-25 12:00:29 -07002400 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002401 mCaches.activeTexture(0);
2402 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2403 return drawShape(left, top, texture, p);
2404 }
2405
2406 SkPath path;
2407 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002408 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2409 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2410 }
Chris Craik710f46d2012-09-17 17:25:49 -07002411 path.addOval(rect);
Chris Craikcb4d6002012-09-25 12:00:29 -07002412 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002413
2414 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002415}
2416
Chet Haase48659092012-05-31 15:21:51 -07002417status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002418 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002419 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002420
2421 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002422 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002423 }
2424
Romain Guya1d3c912011-12-13 14:55:06 -08002425 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002426 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2427 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002428 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002429}
2430
Chet Haase48659092012-05-31 15:21:51 -07002431status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002432 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002433 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002434 }
2435
Chris Craikcb4d6002012-09-25 12:00:29 -07002436 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craik710f46d2012-09-17 17:25:49 -07002437 if (p->getStyle() != SkPaint::kFill_Style) {
2438 mCaches.activeTexture(0);
2439 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2440 return drawShape(left, top, texture, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002441 }
2442
Romain Guy181d0a62011-06-09 18:52:38 -07002443 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002444 SkPath path;
2445 path.addRect(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002446 drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002447 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002448 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chet Haase858aa932011-05-12 09:06:00 -07002449 }
Chet Haase48659092012-05-31 15:21:51 -07002450
2451 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002452}
Romain Guy9d5316e2010-06-24 19:30:36 -07002453
Raph Levien416a8472012-07-19 22:48:17 -07002454void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2455 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2456 float x, float y) {
2457 mCaches.activeTexture(0);
2458
2459 // NOTE: The drop shadow will not perform gamma correction
2460 // if shader-based correction is enabled
2461 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2462 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2463 paint, text, bytesCount, count, mShadowRadius, positions);
2464 const AutoTexture autoCleanup(shadow);
2465
2466 const float sx = x - shadow->left + mShadowDx;
2467 const float sy = y - shadow->top + mShadowDy;
2468
2469 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2470 int shadowColor = mShadowColor;
2471 if (mShader) {
2472 shadowColor = 0xffffffff;
2473 }
2474
2475 setupDraw();
2476 setupDrawWithTexture(true);
2477 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2478 setupDrawColorFilter();
2479 setupDrawShader();
2480 setupDrawBlending(true, mode);
2481 setupDrawProgram();
2482 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2483 setupDrawTexture(shadow->id);
2484 setupDrawPureColorUniforms();
2485 setupDrawColorFilterUniforms();
2486 setupDrawShaderUniforms();
2487 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2488
2489 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2490}
2491
Chet Haase48659092012-05-31 15:21:51 -07002492status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002493 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002494 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002495 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002496 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002497 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002498
Romain Guy671d6cf2012-01-18 12:39:17 -08002499 // NOTE: Skia does not support perspective transform on drawPosText yet
2500 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002501 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002502 }
2503
2504 float x = 0.0f;
2505 float y = 0.0f;
2506 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2507 if (pureTranslate) {
2508 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2509 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2510 }
2511
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002512 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002513 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2514 paint->getTextSize());
2515
2516 int alpha;
2517 SkXfermode::Mode mode;
2518 getAlphaAndMode(paint, &alpha, &mode);
2519
Raph Levien416a8472012-07-19 22:48:17 -07002520 if (CC_UNLIKELY(mHasShadow)) {
2521 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2522 0.0f, 0.0f);
2523 }
2524
Romain Guy671d6cf2012-01-18 12:39:17 -08002525 // Pick the appropriate texture filtering
2526 bool linearFilter = mSnapshot->transform->changesBounds();
2527 if (pureTranslate && !linearFilter) {
2528 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2529 }
2530
2531 mCaches.activeTexture(0);
2532 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002533 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002534 setupDrawDirtyRegionsDisabled();
2535 setupDrawWithTexture(true);
2536 setupDrawAlpha8Color(paint->getColor(), alpha);
2537 setupDrawColorFilter();
2538 setupDrawShader();
2539 setupDrawBlending(true, mode);
2540 setupDrawProgram();
2541 setupDrawModelView(x, y, x, y, pureTranslate, true);
2542 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2543 setupDrawPureColorUniforms();
2544 setupDrawColorFilterUniforms();
2545 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002546 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002547
2548 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2549 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2550
Romain Guy211370f2012-02-01 16:10:55 -08002551 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002552
2553 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2554 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002555 if (hasActiveLayer) {
2556 if (!pureTranslate) {
2557 mSnapshot->transform->mapRect(bounds);
2558 }
2559 dirtyLayerUnchecked(bounds, getRegion());
2560 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002561 }
Chet Haase48659092012-05-31 15:21:51 -07002562
2563 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002564}
2565
Romain Guyc2525952012-07-27 16:41:22 -07002566status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002567 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002568 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002569 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002570 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002571 }
2572
Chet Haasea1cff502012-02-21 13:43:44 -08002573 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002574 switch (paint->getTextAlign()) {
2575 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002576 x -= length / 2.0f;
2577 break;
2578 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002579 x -= length;
2580 break;
2581 default:
2582 break;
2583 }
2584
Romain Guycac5fd32011-12-01 20:08:50 -08002585 SkPaint::FontMetrics metrics;
2586 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002587 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002588 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002589 }
2590
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002591 const float oldX = x;
2592 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002593 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002594 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002595 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2596 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2597 }
2598
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002599#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002600 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002601 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002602#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002603
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002604 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002605 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002606 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002607
Romain Guy86568192010-12-14 15:55:39 -08002608 int alpha;
2609 SkXfermode::Mode mode;
2610 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002611
Romain Guy211370f2012-02-01 16:10:55 -08002612 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002613 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2614 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002615 }
2616
Romain Guy6620c6d2010-12-06 18:07:02 -08002617 // Pick the appropriate texture filtering
2618 bool linearFilter = mSnapshot->transform->changesBounds();
2619 if (pureTranslate && !linearFilter) {
2620 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2621 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002622
Romain Guy16c88082012-06-11 16:03:47 -07002623 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002624 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002625 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002626 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002627 setupDrawDirtyRegionsDisabled();
2628 setupDrawWithTexture(true);
2629 setupDrawAlpha8Color(paint->getColor(), alpha);
2630 setupDrawColorFilter();
2631 setupDrawShader();
2632 setupDrawBlending(true, mode);
2633 setupDrawProgram();
2634 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002635 // See comment above; the font renderer must use texture unit 0
2636 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002637 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2638 setupDrawPureColorUniforms();
2639 setupDrawColorFilterUniforms();
2640 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002641 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002642
Romain Guy6620c6d2010-12-06 18:07:02 -08002643 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002644 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2645
Romain Guy211370f2012-02-01 16:10:55 -08002646 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002647
Raph Levien996e57c2012-07-23 15:22:52 -07002648 bool status;
Raph Levien8b4072d2012-07-30 15:50:00 -07002649 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
2650 SkPaint paintCopy(*paint);
2651 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2652 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Raph Levien996e57c2012-07-23 15:22:52 -07002653 positions, hasActiveLayer ? &bounds : NULL);
2654 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002655 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2656 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002657 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002658
2659 if (status && hasActiveLayer) {
2660 if (!pureTranslate) {
2661 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002662 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002663 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002664 }
Romain Guy694b5192010-07-21 21:33:20 -07002665
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002666 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002667
2668 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002669}
2670
Chet Haase48659092012-05-31 15:21:51 -07002671status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002672 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002673 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2674 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002675 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002676 }
2677
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002678 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002679 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2680 paint->getTextSize());
2681
2682 int alpha;
2683 SkXfermode::Mode mode;
2684 getAlphaAndMode(paint, &alpha, &mode);
2685
2686 mCaches.activeTexture(0);
2687 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002688 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002689 setupDrawDirtyRegionsDisabled();
2690 setupDrawWithTexture(true);
2691 setupDrawAlpha8Color(paint->getColor(), alpha);
2692 setupDrawColorFilter();
2693 setupDrawShader();
2694 setupDrawBlending(true, mode);
2695 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002696 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002697 setupDrawTexture(fontRenderer.getTexture(true));
2698 setupDrawPureColorUniforms();
2699 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002700 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002701 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002702
Romain Guy97771732012-02-28 18:17:02 -08002703 const Rect* clip = &mSnapshot->getLocalClip();
2704 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 -08002705
Romain Guy97771732012-02-28 18:17:02 -08002706 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002707
2708 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2709 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002710 if (hasActiveLayer) {
2711 mSnapshot->transform->mapRect(bounds);
2712 dirtyLayerUnchecked(bounds, getRegion());
2713 }
Romain Guy97771732012-02-28 18:17:02 -08002714 }
Chet Haase48659092012-05-31 15:21:51 -07002715
2716 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002717}
2718
Chet Haase48659092012-05-31 15:21:51 -07002719status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2720 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002721
Romain Guya1d3c912011-12-13 14:55:06 -08002722 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002723
Romain Guy33f6beb2012-02-16 19:24:51 -08002724 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002725 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002726 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002727 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002728
Romain Guy8b55f372010-08-18 17:10:07 -07002729 const float x = texture->left - texture->offset;
2730 const float y = texture->top - texture->offset;
2731
Romain Guy01d58e42011-01-19 21:54:02 -08002732 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002733
2734 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002735}
2736
Chet Haase48659092012-05-31 15:21:51 -07002737status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002738 if (!layer) {
2739 return DrawGlInfo::kStatusDone;
2740 }
2741
2742 Rect transformed;
2743 Rect clip;
2744 const bool rejected = quickRejectNoScissor(x, y,
2745 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2746
2747 if (rejected) {
Chet Haase48659092012-05-31 15:21:51 -07002748 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002749 }
2750
Romain Guy4ff0cf42012-08-06 14:51:10 -07002751 bool debugLayerUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -07002752 if (updateLayer(layer, true)) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002753 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002754 }
2755
Romain Guy87e2f7572012-09-24 11:37:12 -07002756 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002757 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002758
Romain Guy211370f2012-02-01 16:10:55 -08002759 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guye529ece2012-09-26 11:23:17 -07002760 SkiaColorFilter* oldFilter = mColorFilter;
2761 mColorFilter = layer->getColorFilter();
2762
Romain Guyc88e3572011-01-22 00:32:12 -08002763 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002764 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002765 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002766 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002767 setupDraw();
2768 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002769 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002770 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002771 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002772 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002773 setupDrawPureColorUniforms();
2774 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002775 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002776 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002777 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2778 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002779
Romain Guyd21b6e12011-11-30 20:21:23 -08002780 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002781 setupDrawModelViewTranslate(tx, ty,
2782 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002783 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002784 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002785 setupDrawModelViewTranslate(x, y,
2786 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2787 }
Romain Guyc88e3572011-01-22 00:32:12 -08002788 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002789
Romain Guyc88e3572011-01-22 00:32:12 -08002790 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2791 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002792
Romain Guyc88e3572011-01-22 00:32:12 -08002793 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002794
2795#if DEBUG_LAYERS_AS_REGIONS
2796 drawRegionRects(layer->region);
2797#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002798 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002799
Romain Guye529ece2012-09-26 11:23:17 -07002800 mColorFilter = oldFilter;
2801
Romain Guy4ff0cf42012-08-06 14:51:10 -07002802 if (debugLayerUpdate) {
2803 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2804 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2805 }
Romain Guyf219da52011-01-16 12:54:25 -08002806 }
Chet Haase48659092012-05-31 15:21:51 -07002807
2808 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002809}
2810
Romain Guy6926c722010-07-12 20:20:03 -07002811///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002812// Shaders
2813///////////////////////////////////////////////////////////////////////////////
2814
2815void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002816 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002817}
2818
Romain Guy06f96e22010-07-30 19:18:16 -07002819void OpenGLRenderer::setupShader(SkiaShader* shader) {
2820 mShader = shader;
2821 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002822 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002823 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002824}
2825
Romain Guyd27977d2010-07-14 19:18:51 -07002826///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002827// Color filters
2828///////////////////////////////////////////////////////////////////////////////
2829
2830void OpenGLRenderer::resetColorFilter() {
2831 mColorFilter = NULL;
2832}
2833
2834void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2835 mColorFilter = filter;
2836}
2837
2838///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002839// Drop shadow
2840///////////////////////////////////////////////////////////////////////////////
2841
2842void OpenGLRenderer::resetShadow() {
2843 mHasShadow = false;
2844}
2845
2846void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2847 mHasShadow = true;
2848 mShadowRadius = radius;
2849 mShadowDx = dx;
2850 mShadowDy = dy;
2851 mShadowColor = color;
2852}
2853
2854///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002855// Draw filters
2856///////////////////////////////////////////////////////////////////////////////
2857
2858void OpenGLRenderer::resetPaintFilter() {
2859 mHasDrawFilter = false;
2860}
2861
2862void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2863 mHasDrawFilter = true;
2864 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2865 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2866}
2867
2868SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002869 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002870
2871 uint32_t flags = paint->getFlags();
2872
2873 mFilteredPaint = *paint;
2874 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2875
2876 return &mFilteredPaint;
2877}
2878
2879///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002880// Drawing implementation
2881///////////////////////////////////////////////////////////////////////////////
2882
Romain Guy01d58e42011-01-19 21:54:02 -08002883void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2884 float x, float y, SkPaint* paint) {
2885 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2886 return;
2887 }
2888
2889 int alpha;
2890 SkXfermode::Mode mode;
2891 getAlphaAndMode(paint, &alpha, &mode);
2892
2893 setupDraw();
2894 setupDrawWithTexture(true);
2895 setupDrawAlpha8Color(paint->getColor(), alpha);
2896 setupDrawColorFilter();
2897 setupDrawShader();
2898 setupDrawBlending(true, mode);
2899 setupDrawProgram();
2900 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2901 setupDrawTexture(texture->id);
2902 setupDrawPureColorUniforms();
2903 setupDrawColorFilterUniforms();
2904 setupDrawShaderUniforms();
2905 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2906
2907 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2908
2909 finishDrawTexture();
2910}
2911
Romain Guyf607bdc2010-09-10 19:20:06 -07002912// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002913#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2914#define kStdUnderline_Offset (1.0f / 9.0f)
2915#define kStdUnderline_Thickness (1.0f / 18.0f)
2916
2917void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2918 float x, float y, SkPaint* paint) {
2919 // Handle underline and strike-through
2920 uint32_t flags = paint->getFlags();
2921 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002922 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002923 float underlineWidth = length;
2924 // If length is > 0.0f, we already measured the text for the text alignment
2925 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002926 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002927 }
2928
Romain Guy211370f2012-02-01 16:10:55 -08002929 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002930 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002931 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002932
Raph Levien8b4072d2012-07-30 15:50:00 -07002933 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002934 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002935
Romain Guyf6834472011-01-23 13:32:12 -08002936 int linesCount = 0;
2937 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2938 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2939
2940 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002941 float points[pointsCount];
2942 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002943
2944 if (flags & SkPaint::kUnderlineText_Flag) {
2945 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002946 points[currentPoint++] = left;
2947 points[currentPoint++] = top;
2948 points[currentPoint++] = left + underlineWidth;
2949 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002950 }
2951
2952 if (flags & SkPaint::kStrikeThruText_Flag) {
2953 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002954 points[currentPoint++] = left;
2955 points[currentPoint++] = top;
2956 points[currentPoint++] = left + underlineWidth;
2957 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002958 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002959
Romain Guy726aeba2011-06-01 14:52:00 -07002960 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002961
Romain Guy726aeba2011-06-01 14:52:00 -07002962 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002963 }
2964 }
2965}
2966
Romain Guy026c5e162010-06-28 17:12:22 -07002967void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002968 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002969 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002970 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002971 color |= 0x00ffffff;
2972 }
2973
Romain Guy70ca14e2010-12-13 18:24:33 -08002974 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002975 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002976 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002977 setupDrawShader();
2978 setupDrawColorFilter();
2979 setupDrawBlending(mode);
2980 setupDrawProgram();
2981 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2982 setupDrawColorUniforms();
2983 setupDrawShaderUniforms(ignoreTransform);
2984 setupDrawColorFilterUniforms();
2985 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002986
Romain Guyc95c8d62010-09-17 15:31:32 -07002987 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2988}
2989
Romain Guy82ba8142010-07-09 13:25:56 -07002990void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002991 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002992 int alpha;
2993 SkXfermode::Mode mode;
2994 getAlphaAndMode(paint, &alpha, &mode);
2995
Romain Guyd21b6e12011-11-30 20:21:23 -08002996 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002997
Romain Guy211370f2012-02-01 16:10:55 -08002998 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002999 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
3000 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
3001
Romain Guyd21b6e12011-11-30 20:21:23 -08003002 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003003 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3004 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3005 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3006 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003007 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003008 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3009 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3010 GL_TRIANGLE_STRIP, gMeshCount);
3011 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003012}
3013
Romain Guybd6b79b2010-06-26 00:13:53 -07003014void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003015 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3016 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003017 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003018}
3019
3020void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003021 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003022 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003023 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003024
Romain Guy746b7402010-10-26 16:27:31 -07003025 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003026 setupDrawWithTexture();
3027 setupDrawColor(alpha, alpha, alpha, alpha);
3028 setupDrawColorFilter();
3029 setupDrawBlending(blend, mode, swapSrcDst);
3030 setupDrawProgram();
3031 if (!dirty) {
3032 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07003033 }
Romain Guy5b3b3522010-10-27 18:57:51 -07003034 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003035 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003036 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003037 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003038 }
Romain Guy86568192010-12-14 15:55:39 -08003039 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003040 setupDrawColorFilterUniforms();
3041 setupDrawTexture(texture);
3042 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003043
Romain Guy6820ac82010-09-15 18:11:50 -07003044 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003045
3046 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003047}
3048
Romain Guya5aed0d2010-09-09 14:42:43 -07003049void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003050 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003051 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003052
Romain Guy82ba8142010-07-09 13:25:56 -07003053 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003054 // These blend modes are not supported by OpenGL directly and have
3055 // to be implemented using shaders. Since the shader will perform
3056 // the blending, turn blending off here
3057 // If the blend mode cannot be implemented using shaders, fall
3058 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003059 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08003060 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003061 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003062 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003063
Romain Guy82bc7a72012-01-03 14:13:39 -08003064 if (mCaches.blend) {
3065 glDisable(GL_BLEND);
3066 mCaches.blend = false;
3067 }
3068
3069 return;
3070 } else {
3071 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003072 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003073 }
3074
3075 if (!mCaches.blend) {
3076 glEnable(GL_BLEND);
3077 }
3078
3079 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3080 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3081
3082 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3083 glBlendFunc(sourceMode, destMode);
3084 mCaches.lastSrcMode = sourceMode;
3085 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003086 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003087 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003088 glDisable(GL_BLEND);
3089 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003090 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003091}
3092
Romain Guy889f8d12010-07-29 14:37:42 -07003093bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003094 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003095 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003096 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003097 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003098 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003099 }
Romain Guy6926c722010-07-12 20:20:03 -07003100 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003101}
3102
Romain Guy026c5e162010-06-28 17:12:22 -07003103void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003104 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003105 TextureVertex::setUV(v++, u1, v1);
3106 TextureVertex::setUV(v++, u2, v1);
3107 TextureVertex::setUV(v++, u1, v2);
3108 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003109}
3110
Chet Haase5c13d892010-10-08 08:37:55 -07003111void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003112 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003113 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003114}
3115
Romain Guy9d5316e2010-06-24 19:30:36 -07003116}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003117}; // namespace android