blob: 014d2e6a1f84adbb3bf748999f89c1f3219b46f1 [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"
Romain Guya957eea2010-12-08 18:34:42 -080036#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070037
38namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070039namespace uirenderer {
40
41///////////////////////////////////////////////////////////////////////////////
42// Defines
43///////////////////////////////////////////////////////////////////////////////
44
Romain Guy759ea802010-09-16 20:49:46 -070045#define RAD_TO_DEG (180.0f / 3.14159265f)
46#define MIN_ANGLE 0.001f
47
Romain Guyf8773082012-07-12 18:01:00 -070048#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070049
Romain Guyd21b6e12011-11-30 20:21:23 -080050#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
51
Romain Guy9d5316e2010-06-24 19:30:36 -070052///////////////////////////////////////////////////////////////////////////////
53// Globals
54///////////////////////////////////////////////////////////////////////////////
55
Romain Guy889f8d12010-07-29 14:37:42 -070056/**
57 * Structure mapping Skia xfermodes to OpenGL blending factors.
58 */
59struct Blender {
60 SkXfermode::Mode mode;
61 GLenum src;
62 GLenum dst;
63}; // struct Blender
64
Romain Guy026c5e162010-06-28 17:12:22 -070065// In this array, the index of each Blender equals the value of the first
66// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
67static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070068 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
69 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
70 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
71 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
73 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
74 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
75 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
77 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
79 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
81 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
82 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070083};
Romain Guye4d01122010-06-16 18:44:05 -070084
Romain Guy87a76572010-09-13 18:11:21 -070085// This array contains the swapped version of each SkXfermode. For instance
86// this array's SrcOver blending mode is actually DstOver. You can refer to
87// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070088static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070089 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
90 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
91 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
92 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
93 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
94 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
95 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
96 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
99 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
101 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
102 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
103 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700104};
105
Romain Guyf6a11b82010-06-23 17:47:49 -0700106///////////////////////////////////////////////////////////////////////////////
107// Constructors/destructor
108///////////////////////////////////////////////////////////////////////////////
109
Romain Guyfb8b7632010-08-23 21:05:08 -0700110OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700111 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700112 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700113 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800114 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700115
Romain Guyac670c02010-07-27 17:39:27 -0700116 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
117
Romain Guyae5575b2010-07-29 18:48:04 -0700118 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700119}
120
Romain Guy85bf02f2010-06-22 13:11:24 -0700121OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700122 // The context has already been destroyed at this point, do not call
123 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700124}
125
Romain Guyf6a11b82010-06-23 17:47:49 -0700126///////////////////////////////////////////////////////////////////////////////
Romain Guy13631f32012-01-30 17:41:55 -0800127// Debug
128///////////////////////////////////////////////////////////////////////////////
129
130void OpenGLRenderer::startMark(const char* name) const {
131 mCaches.startMark(0, name);
132}
133
134void OpenGLRenderer::endMark() const {
135 mCaches.endMark();
136}
137
138///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700139// Setup
140///////////////////////////////////////////////////////////////////////////////
141
Romain Guy49c5fc02012-05-15 11:10:01 -0700142bool OpenGLRenderer::isDeferred() {
143 return false;
144}
145
Romain Guy85bf02f2010-06-22 13:11:24 -0700146void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700147 initViewport(width, height);
148
149 glDisable(GL_DITHER);
150 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
151
152 glEnableVertexAttribArray(Program::kBindingPosition);
153}
154
155void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700156 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700157
158 mWidth = width;
159 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700160
161 mFirstSnapshot->height = height;
162 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700163}
164
Chet Haase44b2fe32012-06-06 19:03:58 -0700165int OpenGLRenderer::prepare(bool opaque) {
166 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800167}
168
Chet Haase44b2fe32012-06-06 19:03:58 -0700169int OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800170 mCaches.clearGarbage();
171
Romain Guy8aef54f2010-09-01 15:13:49 -0700172 mSnapshot = new Snapshot(mFirstSnapshot,
173 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800174 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700175 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700176
Romain Guy7d7b5492011-01-24 16:33:45 -0800177 mSnapshot->setClip(left, top, right, bottom);
Romain Guy57b52682012-09-20 17:38:46 -0700178 mDirtyClip = opaque;
Romain Guyddf74372012-05-22 14:07:07 -0700179
Romain Guy11cb6422012-09-21 00:39:43 -0700180 updateLayers();
181
Romain Guy45e4c3d2012-09-11 17:17:07 -0700182 // If we know that we are going to redraw the entire framebuffer,
183 // perform a discard to let the driver know we don't need to preserve
184 // the back buffer for this frame.
185 if (mCaches.extensions.hasDiscardFramebuffer() &&
186 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
187 const GLenum attachments[] = { getTargetFbo() == 0 ? GL_COLOR_EXT : GL_COLOR_ATTACHMENT0 };
188 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
189 }
190
Romain Guyddf74372012-05-22 14:07:07 -0700191 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800192
Romain Guy2b7028e2012-09-19 17:25:38 -0700193 mTilingSnapshot = mSnapshot;
Romain Guy57b52682012-09-20 17:38:46 -0700194 startTiling(mTilingSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700195
Romain Guy7c450aa2012-09-21 19:15:00 -0700196 debugOverdraw(true, true);
197
Romain Guy6b7bd242010-10-06 19:49:23 -0700198 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700199 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700200 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700201 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700202 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700203 } else {
204 mCaches.resetScissor();
205 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700206
207 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700208}
209
210void OpenGLRenderer::syncState() {
211 glViewport(0, 0, mWidth, mHeight);
212
213 if (mCaches.blend) {
214 glEnable(GL_BLEND);
215 } else {
216 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700217 }
Romain Guybb9524b2010-06-22 18:56:38 -0700218}
219
Romain Guy57b52682012-09-20 17:38:46 -0700220void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700221 Rect* clip = mTilingSnapshot->clipRect;
Romain Guy2b7028e2012-09-19 17:25:38 -0700222 if (s->flags & Snapshot::kFlagIsFboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700223 clip = s->clipRect;
224 }
225
226 mCaches.startTiling(clip->left, s->height - clip->bottom,
227 clip->right - clip->left, clip->bottom - clip->top, opaque);
228}
229
230void OpenGLRenderer::endTiling() {
231 mCaches.endTiling();
232}
233
Romain Guyb025b9c2010-09-16 14:16:48 -0700234void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700235 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700236 endTiling();
237
Romain Guy11cb6422012-09-21 00:39:43 -0700238 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700239#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700240 GLenum status = GL_NO_ERROR;
241 while ((status = glGetError()) != GL_NO_ERROR) {
242 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
243 switch (status) {
244 case GL_INVALID_ENUM:
245 ALOGE(" GL_INVALID_ENUM");
246 break;
247 case GL_INVALID_VALUE:
248 ALOGE(" GL_INVALID_VALUE");
249 break;
250 case GL_INVALID_OPERATION:
251 ALOGE(" GL_INVALID_OPERATION");
252 break;
253 case GL_OUT_OF_MEMORY:
254 ALOGE(" Out of memory!");
255 break;
256 }
Romain Guya07105b2011-01-10 21:14:18 -0800257 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700258#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700259
Romain Guyc15008e2010-11-10 11:59:15 -0800260#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800261 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700262#else
263 if (mCaches.getDebugLevel() & kDebugMemory) {
264 mCaches.dumpMemoryUsage();
265 }
Romain Guyc15008e2010-11-10 11:59:15 -0800266#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700267 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700268}
269
Romain Guy7c450aa2012-09-21 19:15:00 -0700270void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
271 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
272 if (clear) {
273 mCaches.disableScissor();
274 mCaches.stencil.clear();
275 }
276 if (enable) {
277 mCaches.stencil.enableDebugWrite();
278 } else {
279 mCaches.stencil.disable();
280 }
281 }
282}
283
284void OpenGLRenderer::renderOverdraw() {
285 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
286 const Rect* clip = mTilingSnapshot->clipRect;
287
288 mCaches.enableScissor();
289 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
290 clip->right - clip->left, clip->bottom - clip->top);
291
292 mCaches.stencil.enableDebugTest(2);
293 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
294 mCaches.stencil.enableDebugTest(3);
295 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
296 mCaches.stencil.enableDebugTest(4);
297 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
298 mCaches.stencil.enableDebugTest(4, true);
299 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
300 mCaches.stencil.disable();
301 }
302}
303
Romain Guy6c319ca2011-01-11 14:29:25 -0800304void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700305 if (mCaches.currentProgram) {
306 if (mCaches.currentProgram->isInUse()) {
307 mCaches.currentProgram->remove();
308 mCaches.currentProgram = NULL;
309 }
310 }
Romain Guy50c0f092010-10-19 11:42:22 -0700311 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800312 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800313 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800314 mCaches.disbaleTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700315 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700316}
317
Romain Guy6c319ca2011-01-11 14:29:25 -0800318void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800319 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800320 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700321 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700322 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700323
Romain Guy3e263fa2011-12-12 16:47:48 -0800324 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
325
Chet Haase80250612012-08-15 13:46:54 -0700326 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700327 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800328 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700329 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700330
Romain Guya1d3c912011-12-13 14:55:06 -0800331 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700332
Romain Guy50c0f092010-10-19 11:42:22 -0700333 mCaches.blend = true;
334 glEnable(GL_BLEND);
335 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
336 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700337}
338
Romain Guy35643dd2012-09-18 15:40:58 -0700339void OpenGLRenderer::resumeAfterLayer() {
340 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
341 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
342 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700343 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700344
345 mCaches.resetScissor();
346 dirtyClip();
347}
348
Romain Guyba6be8a2012-04-23 18:22:09 -0700349void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700350 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700351}
352
353void OpenGLRenderer::attachFunctor(Functor* functor) {
354 mFunctors.add(functor);
355}
356
Romain Guy8f3b8e32012-03-27 16:33:45 -0700357status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
358 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700359 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700360
Romain Guyba6be8a2012-04-23 18:22:09 -0700361 if (count > 0) {
362 SortedVector<Functor*> functors(mFunctors);
363 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700364
Romain Guyba6be8a2012-04-23 18:22:09 -0700365 DrawGlInfo info;
366 info.clipLeft = 0;
367 info.clipTop = 0;
368 info.clipRight = 0;
369 info.clipBottom = 0;
370 info.isLayer = false;
371 info.width = 0;
372 info.height = 0;
373 memset(info.transform, 0, sizeof(float) * 16);
374
375 for (size_t i = 0; i < count; i++) {
376 Functor* f = functors.itemAt(i);
377 result |= (*f)(DrawGlInfo::kModeProcess, &info);
378
Chris Craikc2c95432012-04-25 15:13:52 -0700379 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700380 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
381 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700382 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700383
Chris Craikc2c95432012-04-25 15:13:52 -0700384 if (result & DrawGlInfo::kStatusInvoke) {
385 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700386 }
387 }
Chet Haasebeb8bd02012-09-09 16:13:26 -0700388 // protect against functors binding to other buffers
389 mCaches.unbindMeshBuffer();
390 mCaches.unbindIndicesBuffer();
391 mCaches.activeTexture(0);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700392 }
393
394 return result;
395}
396
397status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800398 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700399 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700400
Romain Guy8a4ac612012-07-17 17:32:48 -0700401 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800402 if (mDirtyClip) {
403 setScissorFromClip();
404 }
Romain Guyd643bb52011-03-01 14:55:21 -0800405
Romain Guy80911b82011-03-16 15:30:12 -0700406 Rect clip(*mSnapshot->clipRect);
407 clip.snapToPixelBoundaries();
408
Romain Guyd643bb52011-03-01 14:55:21 -0800409 // Since we don't know what the functor will draw, let's dirty
410 // tne entire clip region
411 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800412 dirtyLayerUnchecked(clip, getRegion());
413 }
Romain Guyd643bb52011-03-01 14:55:21 -0800414
Romain Guy08aa2cb2011-03-17 11:06:57 -0700415 DrawGlInfo info;
416 info.clipLeft = clip.left;
417 info.clipTop = clip.top;
418 info.clipRight = clip.right;
419 info.clipBottom = clip.bottom;
420 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700421 info.width = getSnapshot()->viewport.getWidth();
422 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700423 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700424
Chet Haase48659092012-05-31 15:21:51 -0700425 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800426
Romain Guy8f3b8e32012-03-27 16:33:45 -0700427 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700428 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800429 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700430
Chris Craik65924a32012-04-05 17:52:11 -0700431 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700432 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700433 }
Romain Guycabfcc12011-03-07 18:06:46 -0800434 }
435
Chet Haasedaf98e92011-01-10 14:10:36 -0800436 resume();
Romain Guy65549432012-03-26 16:45:05 -0700437 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800438}
439
Romain Guyf6a11b82010-06-23 17:47:49 -0700440///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700441// Layers
442///////////////////////////////////////////////////////////////////////////////
443
444bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
445 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
446 OpenGLRenderer* renderer = layer->renderer;
447 Rect& dirty = layer->dirtyRect;
448
Romain Guy7c450aa2012-09-21 19:15:00 -0700449 if (inFrame) {
450 endTiling();
451 debugOverdraw(false, false);
452 }
Romain Guy11cb6422012-09-21 00:39:43 -0700453
454 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
455 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
456 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
457 renderer->finish();
458
459 if (inFrame) {
460 resumeAfterLayer();
461 startTiling(mSnapshot);
462 }
463
464 dirty.setEmpty();
465 layer->deferredUpdateScheduled = false;
466 layer->renderer = NULL;
467 layer->displayList = NULL;
468
469 return true;
470 }
471
472 return false;
473}
474
475void OpenGLRenderer::updateLayers() {
476 int count = mLayerUpdates.size();
477 if (count > 0) {
478 startMark("Layer Updates");
479
480 // Note: it is very important to update the layers in reverse order
481 for (int i = count - 1; i >= 0; i--) {
482 Layer* layer = mLayerUpdates.itemAt(i);
483 updateLayer(layer, false);
484 mCaches.resourceCache.decrementRefcount(layer);
485 }
486 mLayerUpdates.clear();
487
488 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
489 endMark();
490 }
491}
492
493void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
494 if (layer) {
495 mLayerUpdates.push_back(layer);
496 mCaches.resourceCache.incrementRefcount(layer);
497 }
498}
499
500void OpenGLRenderer::clearLayerUpdates() {
501 size_t count = mLayerUpdates.size();
502 if (count > 0) {
503 mCaches.resourceCache.lock();
504 for (size_t i = 0; i < count; i++) {
505 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
506 }
507 mCaches.resourceCache.unlock();
508 mLayerUpdates.clear();
509 }
510}
511
512///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700513// State management
514///////////////////////////////////////////////////////////////////////////////
515
Romain Guybb9524b2010-06-22 18:56:38 -0700516int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700517 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700518}
519
520int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700521 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700522}
523
524void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700525 if (mSaveCount > 1) {
526 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700527 }
Romain Guybb9524b2010-06-22 18:56:38 -0700528}
529
530void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700531 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700532
Romain Guy8fb95422010-08-17 18:38:51 -0700533 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700534 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700535 }
Romain Guybb9524b2010-06-22 18:56:38 -0700536}
537
Romain Guy8aef54f2010-09-01 15:13:49 -0700538int OpenGLRenderer::saveSnapshot(int flags) {
539 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700540 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700541}
542
543bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700544 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700545 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700546 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700547
Romain Guybd6b79b2010-06-26 00:13:53 -0700548 sp<Snapshot> current = mSnapshot;
549 sp<Snapshot> previous = mSnapshot->previous;
550
Romain Guyeb993562010-10-05 18:14:38 -0700551 if (restoreOrtho) {
552 Rect& r = previous->viewport;
553 glViewport(r.left, r.top, r.right, r.bottom);
554 mOrthoMatrix.load(current->orthoMatrix);
555 }
556
Romain Guy8b55f372010-08-18 17:10:07 -0700557 mSaveCount--;
558 mSnapshot = previous;
559
Romain Guy2542d192010-08-18 11:47:12 -0700560 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700561 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700562 }
Romain Guy2542d192010-08-18 11:47:12 -0700563
Romain Guy5ec99242010-11-03 16:19:08 -0700564 if (restoreLayer) {
565 composeLayer(current, previous);
566 }
567
Romain Guy2542d192010-08-18 11:47:12 -0700568 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700569}
570
Romain Guyf6a11b82010-06-23 17:47:49 -0700571///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700572// Layers
573///////////////////////////////////////////////////////////////////////////////
574
575int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700576 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700577 const GLuint previousFbo = mSnapshot->fbo;
578 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700579
Romain Guyaf636eb2010-12-09 17:47:21 -0800580 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700581 int alpha = 255;
582 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700583
Romain Guye45362c2010-11-03 19:58:32 -0700584 if (p) {
585 alpha = p->getAlpha();
586 if (!mCaches.extensions.hasFramebufferFetch()) {
587 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
588 if (!isMode) {
589 // Assume SRC_OVER
590 mode = SkXfermode::kSrcOver_Mode;
591 }
592 } else {
593 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700594 }
595 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700596 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700597 }
Romain Guyd55a8612010-06-28 17:42:46 -0700598
Chet Haased48885a2012-08-28 17:43:28 -0700599 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700600 }
Romain Guyd55a8612010-06-28 17:42:46 -0700601
602 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700603}
604
605int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
606 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700607 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700608 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700609 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700610 SkPaint paint;
611 paint.setAlpha(alpha);
612 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700613 }
Romain Guyd55a8612010-06-28 17:42:46 -0700614}
Romain Guybd6b79b2010-06-26 00:13:53 -0700615
Romain Guy1c740bc2010-09-13 18:00:09 -0700616/**
617 * Layers are viewed by Skia are slightly different than layers in image editing
618 * programs (for instance.) When a layer is created, previously created layers
619 * and the frame buffer still receive every drawing command. For instance, if a
620 * layer is created and a shape intersecting the bounds of the layers and the
621 * framebuffer is draw, the shape will be drawn on both (unless the layer was
622 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
623 *
624 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
625 * texture. Unfortunately, this is inefficient as it requires every primitive to
626 * be drawn n + 1 times, where n is the number of active layers. In practice this
627 * means, for every primitive:
628 * - Switch active frame buffer
629 * - Change viewport, clip and projection matrix
630 * - Issue the drawing
631 *
632 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700633 * To avoid this, layers are implemented in a different way here, at least in the
634 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
635 * is set. When this flag is set we can redirect all drawing operations into a
636 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700637 *
638 * This implementation relies on the frame buffer being at least RGBA 8888. When
639 * a layer is created, only a texture is created, not an FBO. The content of the
640 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700641 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700642 * buffer and drawing continues as normal. This technique therefore treats the
643 * frame buffer as a scratch buffer for the layers.
644 *
645 * To compose the layers back onto the frame buffer, each layer texture
646 * (containing the original frame buffer data) is drawn as a simple quad over
647 * the frame buffer. The trick is that the quad is set as the composition
648 * destination in the blending equation, and the frame buffer becomes the source
649 * of the composition.
650 *
651 * Drawing layers with an alpha value requires an extra step before composition.
652 * An empty quad is drawn over the layer's region in the frame buffer. This quad
653 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
654 * quad is used to multiply the colors in the frame buffer. This is achieved by
655 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
656 * GL_ZERO, GL_SRC_ALPHA.
657 *
658 * Because glCopyTexImage2D() can be slow, an alternative implementation might
659 * be use to draw a single clipped layer. The implementation described above
660 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700661 *
662 * (1) The frame buffer is actually not cleared right away. To allow the GPU
663 * to potentially optimize series of calls to glCopyTexImage2D, the frame
664 * buffer is left untouched until the first drawing operation. Only when
665 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700666 */
Chet Haased48885a2012-08-28 17:43:28 -0700667bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
668 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700669 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700670 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700671
Romain Guyeb993562010-10-05 18:14:38 -0700672 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
673
Romain Guyf607bdc2010-09-10 19:20:06 -0700674 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700675 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700676 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700677 Rect untransformedBounds(bounds);
678 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700679
Chet Haased48885a2012-08-28 17:43:28 -0700680 // Layers only make sense if they are in the framebuffer's bounds
681 if (bounds.intersect(*mSnapshot->clipRect)) {
682 // We cannot work with sub-pixels in this case
683 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700684
Chet Haased48885a2012-08-28 17:43:28 -0700685 // When the layer is not an FBO, we may use glCopyTexImage so we
686 // need to make sure the layer does not extend outside the bounds
687 // of the framebuffer
688 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700689 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700690 } else if (fboLayer) {
691 clip.set(bounds);
692 mat4 inverse;
693 inverse.loadInverse(*mSnapshot->transform);
694 inverse.mapRect(clip);
695 clip.snapToPixelBoundaries();
696 if (clip.intersect(untransformedBounds)) {
697 clip.translate(-left, -top);
698 bounds.set(untransformedBounds);
699 } else {
700 clip.setEmpty();
701 }
Romain Guyad37cd32011-03-15 11:12:25 -0700702 }
Chet Haased48885a2012-08-28 17:43:28 -0700703 } else {
704 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700705 }
Romain Guybf434112010-09-16 14:40:17 -0700706
Romain Guy746b7402010-10-26 16:27:31 -0700707 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700708 bounds.getHeight() > mCaches.maxTextureSize ||
709 (fboLayer && clip.isEmpty())) {
710 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700711 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700712 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700713 }
714
715 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700716 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700717 return false;
718 }
Romain Guyf18fd992010-07-08 11:45:51 -0700719
Romain Guya1d3c912011-12-13 14:55:06 -0800720 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700721 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700722 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700723 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700724 }
725
Romain Guy9ace8f52011-07-07 20:50:11 -0700726 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700727 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700728 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
729 bounds.getWidth() / float(layer->getWidth()), 0.0f);
730 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700731 layer->setBlend(true);
Romain Guydda57022010-07-06 11:39:32 -0700732
Romain Guy8fb95422010-08-17 18:38:51 -0700733 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700734 mSnapshot->flags |= Snapshot::kFlagIsLayer;
735 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700736
Romain Guyeb993562010-10-05 18:14:38 -0700737 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700738 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700739 } else {
740 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700741 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800742 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700743 if (layer->isEmpty()) {
744 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700745 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700746 layer->getWidth(), layer->getHeight(), 0);
747 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800748 } else {
749 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700750 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800751 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700752
Romain Guy54be1cd2011-06-13 19:04:27 -0700753 // Enqueue the buffer coordinates to clear the corresponding region later
754 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700755 }
Romain Guyeb993562010-10-05 18:14:38 -0700756 }
Romain Guyf86ef572010-07-01 11:05:42 -0700757
Romain Guyd55a8612010-06-28 17:42:46 -0700758 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700759}
760
Chet Haased48885a2012-08-28 17:43:28 -0700761bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700762 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700763
Chet Haased48885a2012-08-28 17:43:28 -0700764 mSnapshot->region = &mSnapshot->layer->region;
765 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700766
Chet Haased48885a2012-08-28 17:43:28 -0700767 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
768 mSnapshot->fbo = layer->getFbo();
769 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
770 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
771 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
772 mSnapshot->height = bounds.getHeight();
773 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
774 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700775
Romain Guy2b7028e2012-09-19 17:25:38 -0700776 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700777 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700778 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700779 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
780 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700781
782 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700783 if (layer->isEmpty()) {
784 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
785 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700786 }
787
788 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700789 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700790
Romain Guy2b7028e2012-09-19 17:25:38 -0700791 startTiling(mSnapshot);
Romain Guy5b3b3522010-10-27 18:57:51 -0700792
793 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700794 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800795 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700796 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700797 glClear(GL_COLOR_BUFFER_BIT);
798
799 dirtyClip();
800
801 // Change the ortho projection
802 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
803 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
804
805 return true;
806}
807
Romain Guy1c740bc2010-09-13 18:00:09 -0700808/**
809 * Read the documentation of createLayer() before doing anything in this method.
810 */
Romain Guy1d83e192010-08-17 11:37:00 -0700811void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
812 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000813 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700814 return;
815 }
816
Romain Guy5b3b3522010-10-27 18:57:51 -0700817 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700818
819 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700820 endTiling();
821
Romain Guye0aa84b2012-04-03 19:30:26 -0700822 // Detach the texture from the FBO
823 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guyeb993562010-10-05 18:14:38 -0700824 // Unbind current FBO and restore previous one
825 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700826 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700827
828 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700829 }
830
Romain Guy1d83e192010-08-17 11:37:00 -0700831 Layer* layer = current->layer;
832 const Rect& rect = layer->layer;
833
Romain Guy9ace8f52011-07-07 20:50:11 -0700834 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700835 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700836 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700837 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700838 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700839 }
840
Romain Guy03750a02010-10-18 14:06:08 -0700841 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700842
Romain Guya1d3c912011-12-13 14:55:06 -0800843 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700844
Romain Guy5b3b3522010-10-27 18:57:51 -0700845 // When the layer is stored in an FBO, we can save a bit of fillrate by
846 // drawing only the dirty region
847 if (fboLayer) {
848 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700849 if (layer->getColorFilter()) {
850 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800851 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700852 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700853 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800854 resetColorFilter();
855 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700856 } else if (!rect.isEmpty()) {
857 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
858 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700859 }
Romain Guy8b55f372010-08-18 17:10:07 -0700860
Romain Guyeb993562010-10-05 18:14:38 -0700861 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800862 // Note: No need to use glDiscardFramebufferEXT() since we never
863 // create/compose layers that are not on screen with this
864 // code path
865 // See LayerRenderer::destroyLayer(Layer*)
866
Romain Guyeb993562010-10-05 18:14:38 -0700867 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
868 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700869 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700870 }
871
Romain Guy746b7402010-10-26 16:27:31 -0700872 dirtyClip();
873
Romain Guyeb993562010-10-05 18:14:38 -0700874 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700875 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700876 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700877 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700878 }
879}
880
Romain Guyaa6c24c2011-04-28 18:40:04 -0700881void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700882 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700883
Romain Guy302a9df2011-08-16 13:55:02 -0700884 mat4& transform = layer->getTransform();
885 if (!transform.isIdentity()) {
886 save(0);
887 mSnapshot->transform->multiply(transform);
888 }
889
Romain Guyaa6c24c2011-04-28 18:40:04 -0700890 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700891 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700892 setupDrawWithTexture();
893 } else {
894 setupDrawWithExternalTexture();
895 }
896 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700897 setupDrawColor(alpha, alpha, alpha, alpha);
898 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700899 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700900 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700901 setupDrawPureColorUniforms();
902 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700903 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
904 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700905 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700906 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700907 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700908 if (mSnapshot->transform->isPureTranslate() &&
909 layer->getWidth() == (uint32_t) rect.getWidth() &&
910 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700911 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
912 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
913
Romain Guyd21b6e12011-11-30 20:21:23 -0800914 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700915 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
916 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800917 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700918 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
919 }
920 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700921 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
922
923 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
924
925 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700926
927 if (!transform.isIdentity()) {
928 restore();
929 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700930}
931
Romain Guy5b3b3522010-10-27 18:57:51 -0700932void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700933 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700934 const Rect& texCoords = layer->texCoords;
935 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
936 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700937
Romain Guy9ace8f52011-07-07 20:50:11 -0700938 float x = rect.left;
939 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700940 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700941 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700942 layer->getHeight() == (uint32_t) rect.getHeight();
943
944 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700945 // When we're swapping, the layer is already in screen coordinates
946 if (!swap) {
947 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
948 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
949 }
950
Romain Guyd21b6e12011-11-30 20:21:23 -0800951 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700952 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800953 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700954 }
955
956 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
957 layer->getTexture(), layer->getAlpha() / 255.0f,
958 layer->getMode(), layer->isBlend(),
959 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
960 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700961
Romain Guyaa6c24c2011-04-28 18:40:04 -0700962 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
963 } else {
964 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
965 drawTextureLayer(layer, rect);
966 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
967 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700968}
969
970void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700971 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700972 layer->setRegionAsRect();
973
Romain Guy40667672011-03-18 14:34:03 -0700974 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700975
Romain Guy5b3b3522010-10-27 18:57:51 -0700976 layer->region.clear();
977 return;
978 }
979
Romain Guy8a3957d2011-09-07 17:55:15 -0700980 // TODO: See LayerRenderer.cpp::generateMesh() for important
981 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800982 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700983 size_t count;
984 const android::Rect* rects = layer->region.getArray(&count);
985
Romain Guy9ace8f52011-07-07 20:50:11 -0700986 const float alpha = layer->getAlpha() / 255.0f;
987 const float texX = 1.0f / float(layer->getWidth());
988 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800989 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700990
991 TextureVertex* mesh = mCaches.getRegionMesh();
992 GLsizei numQuads = 0;
993
Romain Guy7230a742011-01-10 22:26:16 -0800994 setupDraw();
995 setupDrawWithTexture();
996 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800997 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700998 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800999 setupDrawProgram();
1000 setupDrawDirtyRegionsDisabled();
1001 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001002 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001003 setupDrawTexture(layer->getTexture());
1004 if (mSnapshot->transform->isPureTranslate()) {
1005 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
1006 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
1007
Romain Guyd21b6e12011-11-30 20:21:23 -08001008 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001009 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1010 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001011 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001012 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1013 }
Romain Guy15bc6432011-12-13 13:11:32 -08001014 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001015
1016 for (size_t i = 0; i < count; i++) {
1017 const android::Rect* r = &rects[i];
1018
1019 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001020 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001021 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001022 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001023
1024 // TODO: Reject quads outside of the clip
1025 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1026 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1027 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1028 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1029
1030 numQuads++;
1031
1032 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1033 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1034 numQuads = 0;
1035 mesh = mCaches.getRegionMesh();
1036 }
1037 }
1038
1039 if (numQuads > 0) {
1040 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1041 }
1042
Romain Guy7230a742011-01-10 22:26:16 -08001043 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001044
1045#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001046 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001047#endif
1048
1049 layer->region.clear();
1050 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001051}
1052
Romain Guy3a3133d2011-02-01 22:59:58 -08001053void OpenGLRenderer::drawRegionRects(const Region& region) {
1054#if DEBUG_LAYERS_AS_REGIONS
1055 size_t count;
1056 const android::Rect* rects = region.getArray(&count);
1057
1058 uint32_t colors[] = {
1059 0x7fff0000, 0x7f00ff00,
1060 0x7f0000ff, 0x7fff00ff,
1061 };
1062
1063 int offset = 0;
1064 int32_t top = rects[0].top;
1065
1066 for (size_t i = 0; i < count; i++) {
1067 if (top != rects[i].top) {
1068 offset ^= 0x2;
1069 top = rects[i].top;
1070 }
1071
1072 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1073 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1074 SkXfermode::kSrcOver_Mode);
1075 }
1076#endif
1077}
1078
Romain Guy5b3b3522010-10-27 18:57:51 -07001079void OpenGLRenderer::dirtyLayer(const float left, const float top,
1080 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001081 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001082 Rect bounds(left, top, right, bottom);
1083 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001084 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001085 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001086}
1087
1088void OpenGLRenderer::dirtyLayer(const float left, const float top,
1089 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001090 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001091 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001092 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001093 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001094}
1095
1096void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001097 if (bounds.intersect(*mSnapshot->clipRect)) {
1098 bounds.snapToPixelBoundaries();
1099 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1100 if (!dirty.isEmpty()) {
1101 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001102 }
1103 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001104}
1105
Romain Guy54be1cd2011-06-13 19:04:27 -07001106void OpenGLRenderer::clearLayerRegions() {
1107 const size_t count = mLayers.size();
1108 if (count == 0) return;
1109
1110 if (!mSnapshot->isIgnored()) {
1111 // Doing several glScissor/glClear here can negatively impact
1112 // GPUs with a tiler architecture, instead we draw quads with
1113 // the Clear blending mode
1114
1115 // The list contains bounds that have already been clipped
1116 // against their initial clip rect, and the current clip
1117 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001118 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001119
1120 Vertex mesh[count * 6];
1121 Vertex* vertex = mesh;
1122
1123 for (uint32_t i = 0; i < count; i++) {
1124 Rect* bounds = mLayers.itemAt(i);
1125
1126 Vertex::set(vertex++, bounds->left, bounds->bottom);
1127 Vertex::set(vertex++, bounds->left, bounds->top);
1128 Vertex::set(vertex++, bounds->right, bounds->top);
1129 Vertex::set(vertex++, bounds->left, bounds->bottom);
1130 Vertex::set(vertex++, bounds->right, bounds->top);
1131 Vertex::set(vertex++, bounds->right, bounds->bottom);
1132
1133 delete bounds;
1134 }
1135
1136 setupDraw(false);
1137 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1138 setupDrawBlending(true, SkXfermode::kClear_Mode);
1139 setupDrawProgram();
1140 setupDrawPureColorUniforms();
1141 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001142 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001143
Romain Guy54be1cd2011-06-13 19:04:27 -07001144 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001145
1146 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001147 } else {
1148 for (uint32_t i = 0; i < count; i++) {
1149 delete mLayers.itemAt(i);
1150 }
1151 }
1152
1153 mLayers.clear();
1154}
1155
Romain Guybd6b79b2010-06-26 00:13:53 -07001156///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001157// Transforms
1158///////////////////////////////////////////////////////////////////////////////
1159
1160void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001161 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001162}
1163
1164void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001165 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001166}
1167
1168void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001169 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001170}
1171
Romain Guy807daf72011-01-18 11:19:19 -08001172void OpenGLRenderer::skew(float sx, float sy) {
1173 mSnapshot->transform->skew(sx, sy);
1174}
1175
Romain Guyf6a11b82010-06-23 17:47:49 -07001176void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001177 if (matrix) {
1178 mSnapshot->transform->load(*matrix);
1179 } else {
1180 mSnapshot->transform->loadIdentity();
1181 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001182}
1183
1184void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001185 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001186}
1187
1188void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001189 SkMatrix transform;
1190 mSnapshot->transform->copyTo(transform);
1191 transform.preConcat(*matrix);
1192 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001193}
1194
1195///////////////////////////////////////////////////////////////////////////////
1196// Clipping
1197///////////////////////////////////////////////////////////////////////////////
1198
Romain Guybb9524b2010-06-22 18:56:38 -07001199void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001200 Rect clip(*mSnapshot->clipRect);
1201 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001202
Romain Guy8a4ac612012-07-17 17:32:48 -07001203 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1204 clip.getWidth(), clip.getHeight())) {
1205 mDirtyClip = false;
1206 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001207}
1208
1209const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001210 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001211}
1212
Romain Guy8a4ac612012-07-17 17:32:48 -07001213bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1214 if (mSnapshot->isIgnored()) {
1215 return true;
1216 }
1217
1218 Rect r(left, top, right, bottom);
1219 mSnapshot->transform->mapRect(r);
1220 r.snapToPixelBoundaries();
1221
1222 Rect clipRect(*mSnapshot->clipRect);
1223 clipRect.snapToPixelBoundaries();
1224
1225 return !clipRect.intersects(r);
1226}
1227
Romain Guy35643dd2012-09-18 15:40:58 -07001228bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1229 Rect& transformed, Rect& clip) {
1230 if (mSnapshot->isIgnored()) {
1231 return true;
1232 }
1233
1234 transformed.set(left, top, right, bottom);
1235 mSnapshot->transform->mapRect(transformed);
1236 transformed.snapToPixelBoundaries();
1237
1238 clip.set(*mSnapshot->clipRect);
1239 clip.snapToPixelBoundaries();
1240
1241 return !clip.intersects(transformed);
1242}
1243
Romain Guyc7d53492010-06-25 13:41:57 -07001244bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001245 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001246 return true;
1247 }
1248
Romain Guy1d83e192010-08-17 11:37:00 -07001249 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001250 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001251 r.snapToPixelBoundaries();
1252
1253 Rect clipRect(*mSnapshot->clipRect);
1254 clipRect.snapToPixelBoundaries();
1255
Romain Guy586cae32012-07-13 15:28:31 -07001256 bool rejected = !clipRect.intersects(r);
1257 if (!isDeferred() && !rejected) {
1258 mCaches.setScissorEnabled(!clipRect.contains(r));
1259 }
1260
1261 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001262}
1263
Romain Guy079ba2c2010-07-16 14:12:24 -07001264bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1265 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001266 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001267 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001268 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001269 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001270}
1271
Chet Haasea23eed82012-04-12 15:19:04 -07001272Rect* OpenGLRenderer::getClipRect() {
1273 return mSnapshot->clipRect;
1274}
1275
Romain Guyf6a11b82010-06-23 17:47:49 -07001276///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001277// Drawing commands
1278///////////////////////////////////////////////////////////////////////////////
1279
Romain Guy54be1cd2011-06-13 19:04:27 -07001280void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001281 // TODO: It would be best if we could do this before quickReject()
1282 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001283 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001284 if (mDirtyClip) {
1285 setScissorFromClip();
1286 }
1287 mDescription.reset();
1288 mSetShaderColor = false;
1289 mColorSet = false;
1290 mColorA = mColorR = mColorG = mColorB = 0.0f;
1291 mTextureUnit = 0;
1292 mTrackDirtyRegions = true;
1293}
1294
1295void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1296 mDescription.hasTexture = true;
1297 mDescription.hasAlpha8Texture = isAlpha8;
1298}
1299
Romain Guyaa6c24c2011-04-28 18:40:04 -07001300void OpenGLRenderer::setupDrawWithExternalTexture() {
1301 mDescription.hasExternalTexture = true;
1302}
1303
Romain Guy15bc6432011-12-13 13:11:32 -08001304void OpenGLRenderer::setupDrawNoTexture() {
1305 mCaches.disbaleTexCoordsVertexArray();
1306}
1307
Chet Haase5b0200b2011-04-13 17:58:08 -07001308void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001309 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001310}
1311
Chris Craik6ebdc112012-08-31 18:24:33 -07001312void OpenGLRenderer::setupDrawAARect() {
1313 mDescription.isAARect = true;
1314}
1315
Romain Guyed6fcb02011-03-21 13:11:28 -07001316void OpenGLRenderer::setupDrawPoint(float pointSize) {
1317 mDescription.isPoint = true;
1318 mDescription.pointSize = pointSize;
1319}
1320
Romain Guy70ca14e2010-12-13 18:24:33 -08001321void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001322 setupDrawColor(color, (color >> 24) & 0xFF);
1323}
1324
1325void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1326 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001327 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1328 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001329 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001330 mColorR = a * ((color >> 16) & 0xFF);
1331 mColorG = a * ((color >> 8) & 0xFF);
1332 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001333 mColorSet = true;
1334 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1335}
1336
Romain Guy86568192010-12-14 15:55:39 -08001337void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1338 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001339 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1340 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001341 const float a = mColorA / 255.0f;
1342 mColorR = a * ((color >> 16) & 0xFF);
1343 mColorG = a * ((color >> 8) & 0xFF);
1344 mColorB = a * ((color ) & 0xFF);
1345 mColorSet = true;
1346 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1347}
1348
Romain Guy41210632012-07-16 17:04:24 -07001349void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1350 mCaches.fontRenderer->describe(mDescription, paint);
1351}
1352
Romain Guy70ca14e2010-12-13 18:24:33 -08001353void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1354 mColorA = a;
1355 mColorR = r;
1356 mColorG = g;
1357 mColorB = b;
1358 mColorSet = true;
1359 mSetShaderColor = mDescription.setColor(r, g, b, a);
1360}
1361
1362void OpenGLRenderer::setupDrawShader() {
1363 if (mShader) {
1364 mShader->describe(mDescription, mCaches.extensions);
1365 }
1366}
1367
1368void OpenGLRenderer::setupDrawColorFilter() {
1369 if (mColorFilter) {
1370 mColorFilter->describe(mDescription, mCaches.extensions);
1371 }
1372}
1373
Romain Guyf09ef512011-05-27 11:43:46 -07001374void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1375 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1376 mColorA = 1.0f;
1377 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001378 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001379 }
1380}
1381
Romain Guy70ca14e2010-12-13 18:24:33 -08001382void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001383 // When the blending mode is kClear_Mode, we need to use a modulate color
1384 // argb=1,0,0,0
1385 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001386 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1387 mDescription, swapSrcDst);
1388}
1389
1390void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001391 // When the blending mode is kClear_Mode, we need to use a modulate color
1392 // argb=1,0,0,0
1393 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001394 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1395 mDescription, swapSrcDst);
1396}
1397
1398void OpenGLRenderer::setupDrawProgram() {
1399 useProgram(mCaches.programCache.get(mDescription));
1400}
1401
1402void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1403 mTrackDirtyRegions = false;
1404}
1405
1406void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1407 bool ignoreTransform) {
1408 mModelView.loadTranslate(left, top, 0.0f);
1409 if (!ignoreTransform) {
1410 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1411 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1412 } else {
1413 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1414 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1415 }
1416}
1417
Chet Haase8a5cc922011-04-26 07:28:09 -07001418void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1419 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001420}
1421
Romain Guy70ca14e2010-12-13 18:24:33 -08001422void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1423 bool ignoreTransform, bool ignoreModelView) {
1424 if (!ignoreModelView) {
1425 mModelView.loadTranslate(left, top, 0.0f);
1426 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001427 } else {
1428 mModelView.loadIdentity();
1429 }
Romain Guy86568192010-12-14 15:55:39 -08001430 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1431 if (!ignoreTransform) {
1432 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1433 if (mTrackDirtyRegions && dirty) {
1434 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1435 }
1436 } else {
1437 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1438 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1439 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001440}
1441
Romain Guyed6fcb02011-03-21 13:11:28 -07001442void OpenGLRenderer::setupDrawPointUniforms() {
1443 int slot = mCaches.currentProgram->getUniform("pointSize");
1444 glUniform1f(slot, mDescription.pointSize);
1445}
1446
Romain Guy70ca14e2010-12-13 18:24:33 -08001447void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001448 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001449 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1450 }
1451}
1452
Romain Guy86568192010-12-14 15:55:39 -08001453void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001454 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001455 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001456 }
1457}
1458
Romain Guy70ca14e2010-12-13 18:24:33 -08001459void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1460 if (mShader) {
1461 if (ignoreTransform) {
1462 mModelView.loadInverse(*mSnapshot->transform);
1463 }
1464 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1465 }
1466}
1467
Romain Guy8d0d4782010-12-14 20:13:35 -08001468void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1469 if (mShader) {
1470 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1471 }
1472}
1473
Romain Guy70ca14e2010-12-13 18:24:33 -08001474void OpenGLRenderer::setupDrawColorFilterUniforms() {
1475 if (mColorFilter) {
1476 mColorFilter->setupProgram(mCaches.currentProgram);
1477 }
1478}
1479
Romain Guy41210632012-07-16 17:04:24 -07001480void OpenGLRenderer::setupDrawTextGammaUniforms() {
1481 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1482}
1483
Romain Guy70ca14e2010-12-13 18:24:33 -08001484void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001485 bool force = mCaches.bindMeshBuffer();
1486 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001487 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001488}
1489
1490void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1491 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001492 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001493 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001494}
1495
Romain Guyaa6c24c2011-04-28 18:40:04 -07001496void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1497 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001498 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001499 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001500}
1501
Romain Guy8f0095c2011-05-02 17:24:22 -07001502void OpenGLRenderer::setupDrawTextureTransform() {
1503 mDescription.hasTextureTransform = true;
1504}
1505
1506void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001507 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1508 GL_FALSE, &transform.data[0]);
1509}
1510
Romain Guy70ca14e2010-12-13 18:24:33 -08001511void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001512 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001513 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001514 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001515 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001516 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001517 }
Romain Guyd71dd362011-12-12 19:03:35 -08001518
Romain Guyf3a910b42011-12-12 20:35:21 -08001519 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001520 if (mCaches.currentProgram->texCoords >= 0) {
1521 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1522 }
1523
1524 mCaches.unbindIndicesBuffer();
1525}
1526
1527void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1528 bool force = mCaches.unbindMeshBuffer();
1529 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1530 if (mCaches.currentProgram->texCoords >= 0) {
1531 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001532 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001533}
1534
Chet Haase5b0200b2011-04-13 17:58:08 -07001535void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001536 bool force = mCaches.unbindMeshBuffer();
1537 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1538 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001539 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001540}
1541
1542/**
1543 * 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 -07001544 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1545 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1546 * attributes (one per vertex) are values from zero to one that tells the fragment
1547 * shader where the fragment is in relation to the line width/length overall; these values are
1548 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1549 * region of the line.
1550 * Note that we only pass down the width values in this setup function. The length coordinates
1551 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001552 */
Chet Haase99585ad2011-05-02 15:00:16 -07001553void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001554 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001555 bool force = mCaches.unbindMeshBuffer();
1556 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1557 vertices, gAAVertexStride);
1558 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001559 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001560
Romain Guy7b631422012-04-04 11:38:54 -07001561 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001562 glEnableVertexAttribArray(widthSlot);
1563 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001564
Romain Guy7b631422012-04-04 11:38:54 -07001565 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001566 glEnableVertexAttribArray(lengthSlot);
1567 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001568
Chet Haase5b0200b2011-04-13 17:58:08 -07001569 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001570 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001571}
1572
1573void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1574 glDisableVertexAttribArray(widthSlot);
1575 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001576}
1577
Romain Guy70ca14e2010-12-13 18:24:33 -08001578void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001579}
1580
1581///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001582// Drawing
1583///////////////////////////////////////////////////////////////////////////////
1584
Chet Haase1271e2c2012-04-20 09:54:27 -07001585status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001586 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001587
Romain Guy0fe478e2010-11-08 12:08:41 -08001588 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1589 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001590 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001591 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001592 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001593
Romain Guy65549432012-03-26 16:45:05 -07001594 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001595}
1596
Chet Haaseed30fd82011-04-22 16:18:45 -07001597void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1598 if (displayList) {
1599 displayList->output(*this, level);
1600 }
1601}
1602
Romain Guya168d732011-03-18 16:50:13 -07001603void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1604 int alpha;
1605 SkXfermode::Mode mode;
1606 getAlphaAndMode(paint, &alpha, &mode);
1607
Romain Guya168d732011-03-18 16:50:13 -07001608 float x = left;
1609 float y = top;
1610
Romain Guye3c26852011-07-25 16:36:01 -07001611 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001612 bool ignoreTransform = false;
1613 if (mSnapshot->transform->isPureTranslate()) {
1614 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1615 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1616 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001617 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001618 } else {
1619 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001620 }
1621
1622 setupDraw();
1623 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001624 if (paint) {
1625 setupDrawAlpha8Color(paint->getColor(), alpha);
1626 }
Romain Guya168d732011-03-18 16:50:13 -07001627 setupDrawColorFilter();
1628 setupDrawShader();
1629 setupDrawBlending(true, mode);
1630 setupDrawProgram();
1631 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001632
Romain Guya168d732011-03-18 16:50:13 -07001633 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001634 texture->setWrap(GL_CLAMP_TO_EDGE);
1635 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001636
Romain Guya168d732011-03-18 16:50:13 -07001637 setupDrawPureColorUniforms();
1638 setupDrawColorFilterUniforms();
1639 setupDrawShaderUniforms();
1640 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1641
1642 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1643
1644 finishDrawTexture();
1645}
1646
Chet Haase48659092012-05-31 15:21:51 -07001647status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001648 const float right = left + bitmap->width();
1649 const float bottom = top + bitmap->height();
1650
1651 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001652 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001653 }
1654
Romain Guya1d3c912011-12-13 14:55:06 -08001655 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001656 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001657 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001658 const AutoTexture autoCleanup(texture);
1659
Romain Guy211370f2012-02-01 16:10:55 -08001660 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001661 drawAlphaBitmap(texture, left, top, paint);
1662 } else {
1663 drawTextureRect(left, top, right, bottom, texture, paint);
1664 }
Chet Haase48659092012-05-31 15:21:51 -07001665
1666 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001667}
1668
Chet Haase48659092012-05-31 15:21:51 -07001669status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001670 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1671 const mat4 transform(*matrix);
1672 transform.mapRect(r);
1673
Romain Guy6926c722010-07-12 20:20:03 -07001674 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001675 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001676 }
1677
Romain Guya1d3c912011-12-13 14:55:06 -08001678 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001679 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001680 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001681 const AutoTexture autoCleanup(texture);
1682
Romain Guy5b3b3522010-10-27 18:57:51 -07001683 // This could be done in a cheaper way, all we need is pass the matrix
1684 // to the vertex shader. The save/restore is a bit overkill.
1685 save(SkCanvas::kMatrix_SaveFlag);
1686 concatMatrix(matrix);
1687 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1688 restore();
Chet Haase48659092012-05-31 15:21:51 -07001689
1690 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001691}
1692
Chet Haase48659092012-05-31 15:21:51 -07001693status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001694 const float right = left + bitmap->width();
1695 const float bottom = top + bitmap->height();
1696
1697 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001698 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001699 }
1700
1701 mCaches.activeTexture(0);
1702 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1703 const AutoTexture autoCleanup(texture);
1704
1705 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001706
1707 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001708}
1709
Chet Haase48659092012-05-31 15:21:51 -07001710status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001711 float* vertices, int* colors, SkPaint* paint) {
1712 // TODO: Do a quickReject
1713 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001714 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001715 }
1716
Romain Guya1d3c912011-12-13 14:55:06 -08001717 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001718 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001719 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001720 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001721
Romain Guyd21b6e12011-11-30 20:21:23 -08001722 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1723 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001724
1725 int alpha;
1726 SkXfermode::Mode mode;
1727 getAlphaAndMode(paint, &alpha, &mode);
1728
Romain Guy5a7b4662011-01-20 19:09:30 -08001729 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001730
Romain Guyb18d2d02011-02-10 15:52:54 -08001731 float left = FLT_MAX;
1732 float top = FLT_MAX;
1733 float right = FLT_MIN;
1734 float bottom = FLT_MIN;
1735
Romain Guy211370f2012-02-01 16:10:55 -08001736 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001737
Romain Guya566b7c2011-01-23 16:36:11 -08001738 // TODO: Support the colors array
1739 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001740 TextureVertex* vertex = mesh;
1741 for (int32_t y = 0; y < meshHeight; y++) {
1742 for (int32_t x = 0; x < meshWidth; x++) {
1743 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1744
1745 float u1 = float(x) / meshWidth;
1746 float u2 = float(x + 1) / meshWidth;
1747 float v1 = float(y) / meshHeight;
1748 float v2 = float(y + 1) / meshHeight;
1749
1750 int ax = i + (meshWidth + 1) * 2;
1751 int ay = ax + 1;
1752 int bx = i;
1753 int by = bx + 1;
1754 int cx = i + 2;
1755 int cy = cx + 1;
1756 int dx = i + (meshWidth + 1) * 2 + 2;
1757 int dy = dx + 1;
1758
1759 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1760 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1761 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1762
1763 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1764 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1765 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001766
Romain Guyb18d2d02011-02-10 15:52:54 -08001767 if (hasActiveLayer) {
1768 // TODO: This could be optimized to avoid unnecessary ops
1769 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1770 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1771 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1772 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1773 }
Romain Guy5a7b4662011-01-20 19:09:30 -08001774 }
1775 }
1776
Romain Guyb18d2d02011-02-10 15:52:54 -08001777 if (hasActiveLayer) {
1778 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1779 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001780
Romain Guy5a7b4662011-01-20 19:09:30 -08001781 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1782 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001783 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001784
1785 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001786}
1787
Chet Haase48659092012-05-31 15:21:51 -07001788status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001789 float srcLeft, float srcTop, float srcRight, float srcBottom,
1790 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001791 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001792 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001793 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001794 }
1795
Romain Guya1d3c912011-12-13 14:55:06 -08001796 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001797 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001798 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001799 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001800
Romain Guy8ba548f2010-06-30 19:21:21 -07001801 const float width = texture->width;
1802 const float height = texture->height;
1803
Romain Guy68169722011-08-22 17:33:33 -07001804 const float u1 = fmax(0.0f, srcLeft / width);
1805 const float v1 = fmax(0.0f, srcTop / height);
1806 const float u2 = fmin(1.0f, srcRight / width);
1807 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001808
Romain Guy03750a02010-10-18 14:06:08 -07001809 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001810 resetDrawTextureTexCoords(u1, v1, u2, v2);
1811
Romain Guy03750a02010-10-18 14:06:08 -07001812 int alpha;
1813 SkXfermode::Mode mode;
1814 getAlphaAndMode(paint, &alpha, &mode);
1815
Romain Guyd21b6e12011-11-30 20:21:23 -08001816 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1817
Romain Guy211370f2012-02-01 16:10:55 -08001818 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001819 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1820 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1821
Romain Guyb5014982011-07-28 15:39:12 -07001822 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001823 // Enable linear filtering if the source rectangle is scaled
1824 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001825 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001826 }
Romain Guyb5014982011-07-28 15:39:12 -07001827
Romain Guyd21b6e12011-11-30 20:21:23 -08001828 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001829 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1830 texture->id, alpha / 255.0f, mode, texture->blend,
1831 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1832 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1833 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001834 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001835 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1836 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1837 GL_TRIANGLE_STRIP, gMeshCount);
1838 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001839
1840 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001841
1842 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001843}
1844
Chet Haase48659092012-05-31 15:21:51 -07001845status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001846 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001847 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001848 int alpha;
1849 SkXfermode::Mode mode;
1850 getAlphaAndModeDirect(paint, &alpha, &mode);
1851
1852 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1853 left, top, right, bottom, alpha, mode);
1854}
1855
1856status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1857 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1858 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001859 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001860 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001861 }
1862
Romain Guybe6f9dc2012-07-16 12:41:17 -07001863 alpha *= mSnapshot->alpha;
1864
Romain Guya1d3c912011-12-13 14:55:06 -08001865 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001866 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001867 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001868 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001869 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1870 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001871
Romain Guy2728f962010-10-08 18:36:15 -07001872 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001873 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001874
Romain Guy211370f2012-02-01 16:10:55 -08001875 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001876 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001877 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001878 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001879 const float offsetX = left + mSnapshot->transform->getTranslateX();
1880 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001881 const size_t count = mesh->quads.size();
1882 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001883 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001884 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001885 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1886 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1887 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001888 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001889 dirtyLayer(left + bounds.left, top + bounds.top,
1890 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001891 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001892 }
1893 }
1894
Romain Guy211370f2012-02-01 16:10:55 -08001895 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001896 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1897 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1898
1899 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1900 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1901 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1902 true, !mesh->hasEmptyQuads);
1903 } else {
1904 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1905 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1906 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1907 true, !mesh->hasEmptyQuads);
1908 }
Romain Guy054dc182010-10-15 17:55:25 -07001909 }
Chet Haase48659092012-05-31 15:21:51 -07001910
1911 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001912}
1913
Chet Haase99ecdc42011-05-06 12:06:34 -07001914/**
Chet Haase858aa932011-05-12 09:06:00 -07001915 * This function uses a similar approach to that of AA lines in the drawLines() function.
Chris Craik6ebdc112012-08-31 18:24:33 -07001916 * We expand the rectangle by a half pixel in screen space on all sides. However, instead of using
1917 * a fragment shader to compute the translucency of the color from its position, we simply use a
1918 * varying parameter to define how far a given pixel is into the region.
Chet Haase858aa932011-05-12 09:06:00 -07001919 */
1920void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001921 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001922 float inverseScaleX = 1.0f;
1923 float inverseScaleY = 1.0f;
Romain Guy04299382012-07-18 17:15:41 -07001924
Chet Haase858aa932011-05-12 09:06:00 -07001925 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001926 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001927 Matrix4 *mat = mSnapshot->transform;
1928 float m00 = mat->data[Matrix4::kScaleX];
1929 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase858aa932011-05-12 09:06:00 -07001930 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001931 float m11 = mat->data[Matrix4::kScaleY];
Chet Haase858aa932011-05-12 09:06:00 -07001932 float scaleX = sqrt(m00 * m00 + m01 * m01);
1933 float scaleY = sqrt(m10 * m10 + m11 * m11);
1934 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1935 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1936 }
1937
Chet Haase858aa932011-05-12 09:06:00 -07001938 float boundarySizeX = .5 * inverseScaleX;
1939 float boundarySizeY = .5 * inverseScaleY;
1940
Chris Craik6ebdc112012-08-31 18:24:33 -07001941 float innerLeft = left + boundarySizeX;
1942 float innerRight = right - boundarySizeX;
1943 float innerTop = top + boundarySizeY;
1944 float innerBottom = bottom - boundarySizeY;
1945
Chet Haase858aa932011-05-12 09:06:00 -07001946 // Adjust the rect by the AA boundary padding
1947 left -= boundarySizeX;
1948 right += boundarySizeX;
1949 top -= boundarySizeY;
1950 bottom += boundarySizeY;
1951
Chet Haase858aa932011-05-12 09:06:00 -07001952 if (!quickReject(left, top, right, bottom)) {
Romain Guy04299382012-07-18 17:15:41 -07001953 setupDraw();
1954 setupDrawNoTexture();
Chris Craik6ebdc112012-08-31 18:24:33 -07001955 setupDrawAARect();
Romain Guy04299382012-07-18 17:15:41 -07001956 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1957 setupDrawColorFilter();
1958 setupDrawShader();
1959 setupDrawBlending(true, mode);
1960 setupDrawProgram();
1961 setupDrawModelViewIdentity(true);
1962 setupDrawColorUniforms();
1963 setupDrawColorFilterUniforms();
1964 setupDrawShaderIdentityUniforms();
1965
Chris Craik6ebdc112012-08-31 18:24:33 -07001966 AlphaVertex rects[14];
1967 AlphaVertex* aVertices = &rects[0];
1968 void* alphaCoords = ((GLbyte*) aVertices) + gVertexAlphaOffset;
Romain Guy04299382012-07-18 17:15:41 -07001969
Chris Craik6ebdc112012-08-31 18:24:33 -07001970 bool force = mCaches.unbindMeshBuffer();
1971 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1972 aVertices, gAlphaVertexStride);
1973 mCaches.resetTexCoordsVertexPointer();
1974 mCaches.unbindIndicesBuffer();
Romain Guy04299382012-07-18 17:15:41 -07001975
Chris Craik6ebdc112012-08-31 18:24:33 -07001976 int alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
1977 glEnableVertexAttribArray(alphaSlot);
1978 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Romain Guy04299382012-07-18 17:15:41 -07001979
Chris Craik6ebdc112012-08-31 18:24:33 -07001980 // draw left
1981 AlphaVertex::set(aVertices++, left, bottom, 0);
1982 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1983 AlphaVertex::set(aVertices++, left, top, 0);
1984 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001985
Chris Craik6ebdc112012-08-31 18:24:33 -07001986 // draw top
1987 AlphaVertex::set(aVertices++, right, top, 0);
1988 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001989
Chris Craik6ebdc112012-08-31 18:24:33 -07001990 // draw right
1991 AlphaVertex::set(aVertices++, right, bottom, 0);
1992 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1993
1994 // draw bottom
1995 AlphaVertex::set(aVertices++, left, bottom, 0);
1996 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1997
1998 // draw inner rect (repeating last vertex to create degenerate bridge triangles)
1999 // TODO: also consider drawing the inner rect without the blending-forced shader, if
2000 // blending is expensive. Note: can't use drawColorRect() since it doesn't use vertex
2001 // buffers like below, resulting in slightly different transformed coordinates.
2002 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
2003 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
2004 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
2005 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
2006
Chet Haase858aa932011-05-12 09:06:00 -07002007 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guy7b631422012-04-04 11:38:54 -07002008
Chris Craik6ebdc112012-08-31 18:24:33 -07002009 glDrawArrays(GL_TRIANGLE_STRIP, 0, 14);
Romain Guy04299382012-07-18 17:15:41 -07002010
Chris Craik6ebdc112012-08-31 18:24:33 -07002011 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002012 }
Chet Haase858aa932011-05-12 09:06:00 -07002013}
2014
2015/**
Chet Haase99ecdc42011-05-06 12:06:34 -07002016 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
2017 * rules for those lines produces some unexpected results, and may vary between hardware devices.
2018 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
2019 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
2020 * of the line. Hairlines are more involved because we need to account for transform scaling
2021 * to end up with a one-pixel-wide line in screen space..
2022 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
2023 * in combination with values that we calculate and pass down in this method. The basic approach
2024 * is that the quad we create contains both the core line area plus a bounding area in which
2025 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
2026 * proportion of the width and the length of a given segment is represented by the boundary
2027 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
2028 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
2029 * on the inside). This ends up giving the result we want, with pixels that are completely
2030 * 'inside' the line area being filled opaquely and the other pixels being filled according to
2031 * how far into the boundary region they are, which is determined by shader interpolation.
2032 */
Chet Haase48659092012-05-31 15:21:51 -07002033status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
2034 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002035
2036 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07002037 // We use half the stroke width here because we're going to position the quad
2038 // corner vertices half of the width away from the line endpoints
2039 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002040 // A stroke width of 0 has a special meaning in Skia:
2041 // it draws a line 1 px wide regardless of current transform
2042 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07002043
Chet Haase99ecdc42011-05-06 12:06:34 -07002044 float inverseScaleX = 1.0f;
2045 float inverseScaleY = 1.0f;
2046 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07002047
Chet Haase8a5cc922011-04-26 07:28:09 -07002048 int alpha;
2049 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07002050
Chet Haase8a5cc922011-04-26 07:28:09 -07002051 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002052 int verticesCount = count;
2053 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07002054 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07002055 verticesCount += (count - 4);
2056 }
2057
2058 if (isHairLine || isAA) {
2059 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
2060 // the line on the screen should always be one pixel wide regardless of scale. For
2061 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08002062 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07002063 Matrix4 *mat = mSnapshot->transform;
2064 float m00 = mat->data[Matrix4::kScaleX];
2065 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07002066 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07002067 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07002068
Romain Guy211370f2012-02-01 16:10:55 -08002069 float scaleX = sqrtf(m00 * m00 + m01 * m01);
2070 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07002071
Chet Haase99ecdc42011-05-06 12:06:34 -07002072 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
2073 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07002074
Chet Haase99ecdc42011-05-06 12:06:34 -07002075 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
2076 scaled = true;
2077 }
2078 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002079 }
Chet Haase8a5cc922011-04-26 07:28:09 -07002080
2081 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07002082
2083 mCaches.enableScissor();
2084
Chet Haase8a5cc922011-04-26 07:28:09 -07002085 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002086 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002087 if (isAA) {
2088 setupDrawAALine();
2089 }
2090 setupDrawColor(paint->getColor(), alpha);
2091 setupDrawColorFilter();
2092 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08002093 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07002094 setupDrawProgram();
2095 setupDrawModelViewIdentity(true);
2096 setupDrawColorUniforms();
2097 setupDrawColorFilterUniforms();
2098 setupDrawShaderIdentityUniforms();
2099
2100 if (isHairLine) {
2101 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07002102 halfStrokeWidth = isAA? 1 : .5;
2103 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002104 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07002105 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07002106 }
Romain Guy7b631422012-04-04 11:38:54 -07002107
2108 int widthSlot;
2109 int lengthSlot;
2110
Chet Haase5b0200b2011-04-13 17:58:08 -07002111 Vertex lines[verticesCount];
2112 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002113
Chet Haase99585ad2011-05-02 15:00:16 -07002114 AAVertex wLines[verticesCount];
2115 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002116
Romain Guy211370f2012-02-01 16:10:55 -08002117 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002118 setupDrawVertices(vertices);
2119 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002120 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2121 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002122 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002123 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2124 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002125 // We will need to calculate the actual width proportion on each segment for
2126 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002127 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002128 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2129 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002130 }
2131
Chet Haase99ecdc42011-05-06 12:06:34 -07002132 AAVertex* prevAAVertex = NULL;
2133 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002134
Chet Haase99585ad2011-05-02 15:00:16 -07002135 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002136 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002137
Chet Haase5b0200b2011-04-13 17:58:08 -07002138 for (int i = 0; i < count; i += 4) {
2139 // a = start point, b = end point
2140 vec2 a(points[i], points[i + 1]);
2141 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002142
Chet Haase99585ad2011-05-02 15:00:16 -07002143 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002144 float boundaryLengthProportion = 0;
2145 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002146
Chet Haase5b0200b2011-04-13 17:58:08 -07002147 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002148 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002149 float x = n.x;
2150 n.x = -n.y;
2151 n.y = x;
2152
Chet Haase8a5cc922011-04-26 07:28:09 -07002153 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002154 if (isAA) {
2155 float wideningFactor;
2156 if (fabs(n.x) >= fabs(n.y)) {
2157 wideningFactor = fabs(1.0f / n.x);
2158 } else {
2159 wideningFactor = fabs(1.0f / n.y);
2160 }
2161 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002162 }
Romain Guy7b631422012-04-04 11:38:54 -07002163
Chet Haase99ecdc42011-05-06 12:06:34 -07002164 if (scaled) {
2165 n.x *= inverseScaleX;
2166 n.y *= inverseScaleY;
2167 }
2168 } else if (scaled) {
2169 // Extend n by .5 pixel on each side, post-transform
2170 vec2 extendedN = n.copyNormalized();
2171 extendedN /= 2;
2172 extendedN.x *= inverseScaleX;
2173 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002174
Chet Haase99ecdc42011-05-06 12:06:34 -07002175 float extendedNLength = extendedN.length();
2176 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002177 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002178 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002179 }
Romain Guy7b631422012-04-04 11:38:54 -07002180
Chet Haase99585ad2011-05-02 15:00:16 -07002181 // aa lines expand the endpoint vertices to encompass the AA boundary
2182 if (isAA) {
2183 vec2 abVector = (b - a);
2184 length = abVector.length();
2185 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002186
Chet Haase99ecdc42011-05-06 12:06:34 -07002187 if (scaled) {
2188 abVector.x *= inverseScaleX;
2189 abVector.y *= inverseScaleY;
2190 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002191 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002192 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002193 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002194 }
Romain Guy7b631422012-04-04 11:38:54 -07002195
Chet Haase99ecdc42011-05-06 12:06:34 -07002196 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002197 a -= abVector;
2198 b += abVector;
2199 }
2200
Chet Haase5b0200b2011-04-13 17:58:08 -07002201 // Four corners of the rectangle defining a thick line
2202 vec2 p1 = a - n;
2203 vec2 p2 = a + n;
2204 vec2 p3 = b + n;
2205 vec2 p4 = b - n;
2206
Chet Haase99585ad2011-05-02 15:00:16 -07002207
Chet Haase5b0200b2011-04-13 17:58:08 -07002208 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2209 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2210 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2211 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2212
Romain Guy04299382012-07-18 17:15:41 -07002213 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002214 if (!isAA) {
2215 if (prevVertex != NULL) {
2216 // Issue two repeat vertices to create degenerate triangles to bridge
2217 // between the previous line and the new one. This is necessary because
2218 // we are creating a single triangle_strip which will contain
2219 // potentially discontinuous line segments.
2220 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2221 Vertex::set(vertices++, p1.x, p1.y);
2222 generatedVerticesCount += 2;
2223 }
Romain Guy7b631422012-04-04 11:38:54 -07002224
Chet Haase5b0200b2011-04-13 17:58:08 -07002225 Vertex::set(vertices++, p1.x, p1.y);
2226 Vertex::set(vertices++, p2.x, p2.y);
2227 Vertex::set(vertices++, p4.x, p4.y);
2228 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002229
Chet Haase5b0200b2011-04-13 17:58:08 -07002230 prevVertex = vertices - 1;
2231 generatedVerticesCount += 4;
2232 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002233 if (!isHairLine && scaled) {
2234 // Must set width proportions per-segment for scaled non-hairlines to use the
2235 // correct AA boundary dimensions
2236 if (boundaryWidthSlot < 0) {
2237 boundaryWidthSlot =
2238 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002239 }
Romain Guy7b631422012-04-04 11:38:54 -07002240
Chet Haase99ecdc42011-05-06 12:06:34 -07002241 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002242 }
Romain Guy7b631422012-04-04 11:38:54 -07002243
Chet Haase99585ad2011-05-02 15:00:16 -07002244 if (boundaryLengthSlot < 0) {
2245 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002246 }
Romain Guy7b631422012-04-04 11:38:54 -07002247
Chet Haase99ecdc42011-05-06 12:06:34 -07002248 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002249
Chet Haase5b0200b2011-04-13 17:58:08 -07002250 if (prevAAVertex != NULL) {
2251 // Issue two repeat vertices to create degenerate triangles to bridge
2252 // between the previous line and the new one. This is necessary because
2253 // we are creating a single triangle_strip which will contain
2254 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002255 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2256 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2257 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002258 generatedVerticesCount += 2;
2259 }
Romain Guy7b631422012-04-04 11:38:54 -07002260
Chet Haase99585ad2011-05-02 15:00:16 -07002261 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2262 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2263 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2264 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002265
Chet Haase5b0200b2011-04-13 17:58:08 -07002266 prevAAVertex = aaVertices - 1;
2267 generatedVerticesCount += 4;
2268 }
Romain Guy7b631422012-04-04 11:38:54 -07002269
Chet Haase99585ad2011-05-02 15:00:16 -07002270 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2271 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2272 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002273 }
2274 }
Romain Guy7b631422012-04-04 11:38:54 -07002275
Chet Haase5b0200b2011-04-13 17:58:08 -07002276 if (generatedVerticesCount > 0) {
2277 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2278 }
Romain Guy7b631422012-04-04 11:38:54 -07002279
2280 if (isAA) {
2281 finishDrawAALine(widthSlot, lengthSlot);
2282 }
Chet Haase48659092012-05-31 15:21:51 -07002283
2284 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002285}
2286
Chet Haase48659092012-05-31 15:21:51 -07002287status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2288 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002289
2290 // TODO: The paint's cap style defines whether the points are square or circular
2291 // TODO: Handle AA for round points
2292
Chet Haase5b0200b2011-04-13 17:58:08 -07002293 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002294 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002295 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002296 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002297 if (isHairLine) {
2298 // Now that we know it's hairline, we can set the effective width, to be used later
2299 strokeWidth = 1.0f;
2300 }
2301 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002302 int alpha;
2303 SkXfermode::Mode mode;
2304 getAlphaAndMode(paint, &alpha, &mode);
2305
2306 int verticesCount = count >> 1;
2307 int generatedVerticesCount = 0;
2308
2309 TextureVertex pointsData[verticesCount];
2310 TextureVertex* vertex = &pointsData[0];
2311
Romain Guy04299382012-07-18 17:15:41 -07002312 // TODO: We should optimize this method to not generate vertices for points
2313 // that lie outside of the clip.
2314 mCaches.enableScissor();
2315
Romain Guyed6fcb02011-03-21 13:11:28 -07002316 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002317 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002318 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002319 setupDrawColor(paint->getColor(), alpha);
2320 setupDrawColorFilter();
2321 setupDrawShader();
2322 setupDrawBlending(mode);
2323 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002324 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002325 setupDrawColorUniforms();
2326 setupDrawColorFilterUniforms();
2327 setupDrawPointUniforms();
2328 setupDrawShaderIdentityUniforms();
2329 setupDrawMesh(vertex);
2330
2331 for (int i = 0; i < count; i += 2) {
2332 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2333 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002334
Chet Haase8a5cc922011-04-26 07:28:09 -07002335 float left = points[i] - halfWidth;
2336 float right = points[i] + halfWidth;
2337 float top = points[i + 1] - halfWidth;
2338 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002339
Chet Haase8a5cc922011-04-26 07:28:09 -07002340 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002341 }
2342
2343 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002344
2345 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002346}
2347
Chet Haase48659092012-05-31 15:21:51 -07002348status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002349 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002350 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002351
Romain Guyae88e5e2010-10-22 17:49:18 -07002352 Rect& clip(*mSnapshot->clipRect);
2353 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002354
Romain Guy3d58c032010-07-14 16:34:53 -07002355 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002356
2357 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002358}
Romain Guy9d5316e2010-06-24 19:30:36 -07002359
Chet Haase48659092012-05-31 15:21:51 -07002360status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2361 SkPaint* paint) {
2362 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002363 const AutoTexture autoCleanup(texture);
2364
2365 const float x = left + texture->left - texture->offset;
2366 const float y = top + texture->top - texture->offset;
2367
2368 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002369
2370 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002371}
2372
Chet Haase48659092012-05-31 15:21:51 -07002373status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002374 float rx, float ry, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002375 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002376
Romain Guya1d3c912011-12-13 14:55:06 -08002377 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002378 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2379 right - left, bottom - top, rx, ry, paint);
Chet Haase48659092012-05-31 15:21:51 -07002380 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002381}
2382
Chet Haase48659092012-05-31 15:21:51 -07002383status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2384 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002385
Romain Guya1d3c912011-12-13 14:55:06 -08002386 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002387 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Chet Haase48659092012-05-31 15:21:51 -07002388 return drawShape(x - radius, y - radius, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002389}
Romain Guy01d58e42011-01-19 21:54:02 -08002390
Chet Haase48659092012-05-31 15:21:51 -07002391status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
2392 SkPaint* paint) {
2393 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002394
Romain Guya1d3c912011-12-13 14:55:06 -08002395 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002396 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002397 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002398}
2399
Chet Haase48659092012-05-31 15:21:51 -07002400status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002401 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002402 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002403
2404 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002405 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002406 }
2407
Romain Guya1d3c912011-12-13 14:55:06 -08002408 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002409 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2410 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002411 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002412}
2413
Chet Haase48659092012-05-31 15:21:51 -07002414status_t OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002415 SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002416 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002417
Romain Guya1d3c912011-12-13 14:55:06 -08002418 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002419 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002420 return drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002421}
2422
Chet Haase48659092012-05-31 15:21:51 -07002423status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002424 if (p->getStyle() != SkPaint::kFill_Style) {
Chet Haase48659092012-05-31 15:21:51 -07002425 return drawRectAsShape(left, top, right, bottom, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002426 }
2427
Romain Guy6926c722010-07-12 20:20:03 -07002428 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002429 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002430 }
2431
Romain Guy026c5e162010-06-28 17:12:22 -07002432 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002433 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002434 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2435 if (!isMode) {
2436 // Assume SRC_OVER
2437 mode = SkXfermode::kSrcOver_Mode;
2438 }
2439 } else {
2440 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002441 }
2442
Romain Guy026c5e162010-06-28 17:12:22 -07002443 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002444 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002445 drawAARect(left, top, right, bottom, color, mode);
2446 } else {
2447 drawColorRect(left, top, right, bottom, color, mode);
2448 }
Chet Haase48659092012-05-31 15:21:51 -07002449
2450 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002451}
Romain Guy9d5316e2010-06-24 19:30:36 -07002452
Raph Levien416a8472012-07-19 22:48:17 -07002453void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2454 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2455 float x, float y) {
2456 mCaches.activeTexture(0);
2457
2458 // NOTE: The drop shadow will not perform gamma correction
2459 // if shader-based correction is enabled
2460 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2461 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2462 paint, text, bytesCount, count, mShadowRadius, positions);
2463 const AutoTexture autoCleanup(shadow);
2464
2465 const float sx = x - shadow->left + mShadowDx;
2466 const float sy = y - shadow->top + mShadowDy;
2467
2468 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2469 int shadowColor = mShadowColor;
2470 if (mShader) {
2471 shadowColor = 0xffffffff;
2472 }
2473
2474 setupDraw();
2475 setupDrawWithTexture(true);
2476 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2477 setupDrawColorFilter();
2478 setupDrawShader();
2479 setupDrawBlending(true, mode);
2480 setupDrawProgram();
2481 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2482 setupDrawTexture(shadow->id);
2483 setupDrawPureColorUniforms();
2484 setupDrawColorFilterUniforms();
2485 setupDrawShaderUniforms();
2486 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2487
2488 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2489}
2490
Chet Haase48659092012-05-31 15:21:51 -07002491status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002492 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002493 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002494 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002495 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002496 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002497
Romain Guy671d6cf2012-01-18 12:39:17 -08002498 // NOTE: Skia does not support perspective transform on drawPosText yet
2499 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002500 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002501 }
2502
2503 float x = 0.0f;
2504 float y = 0.0f;
2505 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2506 if (pureTranslate) {
2507 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2508 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2509 }
2510
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002511 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002512 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2513 paint->getTextSize());
2514
2515 int alpha;
2516 SkXfermode::Mode mode;
2517 getAlphaAndMode(paint, &alpha, &mode);
2518
Raph Levien416a8472012-07-19 22:48:17 -07002519 if (CC_UNLIKELY(mHasShadow)) {
2520 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2521 0.0f, 0.0f);
2522 }
2523
Romain Guy671d6cf2012-01-18 12:39:17 -08002524 // Pick the appropriate texture filtering
2525 bool linearFilter = mSnapshot->transform->changesBounds();
2526 if (pureTranslate && !linearFilter) {
2527 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2528 }
2529
2530 mCaches.activeTexture(0);
2531 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002532 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002533 setupDrawDirtyRegionsDisabled();
2534 setupDrawWithTexture(true);
2535 setupDrawAlpha8Color(paint->getColor(), alpha);
2536 setupDrawColorFilter();
2537 setupDrawShader();
2538 setupDrawBlending(true, mode);
2539 setupDrawProgram();
2540 setupDrawModelView(x, y, x, y, pureTranslate, true);
2541 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2542 setupDrawPureColorUniforms();
2543 setupDrawColorFilterUniforms();
2544 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002545 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002546
2547 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2548 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2549
Romain Guy211370f2012-02-01 16:10:55 -08002550 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002551
2552 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2553 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002554 if (hasActiveLayer) {
2555 if (!pureTranslate) {
2556 mSnapshot->transform->mapRect(bounds);
2557 }
2558 dirtyLayerUnchecked(bounds, getRegion());
2559 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002560 }
Chet Haase48659092012-05-31 15:21:51 -07002561
2562 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002563}
2564
Romain Guyc2525952012-07-27 16:41:22 -07002565status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002566 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002567 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002568 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002569 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002570 }
2571
Chet Haasea1cff502012-02-21 13:43:44 -08002572 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002573 switch (paint->getTextAlign()) {
2574 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002575 x -= length / 2.0f;
2576 break;
2577 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002578 x -= length;
2579 break;
2580 default:
2581 break;
2582 }
2583
Romain Guycac5fd32011-12-01 20:08:50 -08002584 SkPaint::FontMetrics metrics;
2585 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002586 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002587 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002588 }
2589
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002590 const float oldX = x;
2591 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002592 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002593 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002594 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2595 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2596 }
2597
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002598#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002599 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002600 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002601#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002602
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002603 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002604 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002605 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002606
Romain Guy86568192010-12-14 15:55:39 -08002607 int alpha;
2608 SkXfermode::Mode mode;
2609 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002610
Romain Guy211370f2012-02-01 16:10:55 -08002611 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002612 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2613 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002614 }
2615
Romain Guy6620c6d2010-12-06 18:07:02 -08002616 // Pick the appropriate texture filtering
2617 bool linearFilter = mSnapshot->transform->changesBounds();
2618 if (pureTranslate && !linearFilter) {
2619 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2620 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002621
Romain Guy16c88082012-06-11 16:03:47 -07002622 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002623 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002624 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002625 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002626 setupDrawDirtyRegionsDisabled();
2627 setupDrawWithTexture(true);
2628 setupDrawAlpha8Color(paint->getColor(), alpha);
2629 setupDrawColorFilter();
2630 setupDrawShader();
2631 setupDrawBlending(true, mode);
2632 setupDrawProgram();
2633 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002634 // See comment above; the font renderer must use texture unit 0
2635 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002636 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2637 setupDrawPureColorUniforms();
2638 setupDrawColorFilterUniforms();
2639 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002640 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002641
Romain Guy6620c6d2010-12-06 18:07:02 -08002642 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002643 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2644
Romain Guy211370f2012-02-01 16:10:55 -08002645 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002646
Raph Levien996e57c2012-07-23 15:22:52 -07002647 bool status;
Raph Levien8b4072d2012-07-30 15:50:00 -07002648 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
2649 SkPaint paintCopy(*paint);
2650 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2651 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Raph Levien996e57c2012-07-23 15:22:52 -07002652 positions, hasActiveLayer ? &bounds : NULL);
2653 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002654 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2655 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002656 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002657
2658 if (status && hasActiveLayer) {
2659 if (!pureTranslate) {
2660 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002661 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002662 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002663 }
Romain Guy694b5192010-07-21 21:33:20 -07002664
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002665 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002666
2667 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002668}
2669
Chet Haase48659092012-05-31 15:21:51 -07002670status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002671 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002672 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2673 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002674 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002675 }
2676
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002677 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002678 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2679 paint->getTextSize());
2680
2681 int alpha;
2682 SkXfermode::Mode mode;
2683 getAlphaAndMode(paint, &alpha, &mode);
2684
2685 mCaches.activeTexture(0);
2686 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002687 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002688 setupDrawDirtyRegionsDisabled();
2689 setupDrawWithTexture(true);
2690 setupDrawAlpha8Color(paint->getColor(), alpha);
2691 setupDrawColorFilter();
2692 setupDrawShader();
2693 setupDrawBlending(true, mode);
2694 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002695 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002696 setupDrawTexture(fontRenderer.getTexture(true));
2697 setupDrawPureColorUniforms();
2698 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002699 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002700 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002701
Romain Guy97771732012-02-28 18:17:02 -08002702 const Rect* clip = &mSnapshot->getLocalClip();
2703 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 -08002704
Romain Guy97771732012-02-28 18:17:02 -08002705 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002706
2707 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2708 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002709 if (hasActiveLayer) {
2710 mSnapshot->transform->mapRect(bounds);
2711 dirtyLayerUnchecked(bounds, getRegion());
2712 }
Romain Guy97771732012-02-28 18:17:02 -08002713 }
Chet Haase48659092012-05-31 15:21:51 -07002714
2715 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002716}
2717
Chet Haase48659092012-05-31 15:21:51 -07002718status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2719 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002720
Romain Guya1d3c912011-12-13 14:55:06 -08002721 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002722
Romain Guy33f6beb2012-02-16 19:24:51 -08002723 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002724 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002725 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002726 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002727
Romain Guy8b55f372010-08-18 17:10:07 -07002728 const float x = texture->left - texture->offset;
2729 const float y = texture->top - texture->offset;
2730
Romain Guy01d58e42011-01-19 21:54:02 -08002731 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002732
2733 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002734}
2735
Chet Haase48659092012-05-31 15:21:51 -07002736status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002737 if (!layer) {
2738 return DrawGlInfo::kStatusDone;
2739 }
2740
2741 Rect transformed;
2742 Rect clip;
2743 const bool rejected = quickRejectNoScissor(x, y,
2744 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2745
2746 if (rejected) {
Chet Haase48659092012-05-31 15:21:51 -07002747 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002748 }
2749
Romain Guy4ff0cf42012-08-06 14:51:10 -07002750 bool debugLayerUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -07002751 if (updateLayer(layer, true)) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002752 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002753 }
2754
Romain Guy35643dd2012-09-18 15:40:58 -07002755 mCaches.setScissorEnabled(!clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002756 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002757
Romain Guy211370f2012-02-01 16:10:55 -08002758 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002759 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002760 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002761 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002762 const float a = layer->getAlpha() / 255.0f;
2763 SkiaColorFilter *oldFilter = mColorFilter;
2764 mColorFilter = layer->getColorFilter();
Romain Guyf219da52011-01-16 12:54:25 -08002765
Romain Guyc88e3572011-01-22 00:32:12 -08002766 setupDraw();
2767 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002768 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002769 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002770 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002771 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002772 setupDrawPureColorUniforms();
2773 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002774 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002775 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002776 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2777 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002778
Romain Guyd21b6e12011-11-30 20:21:23 -08002779 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002780 setupDrawModelViewTranslate(tx, ty,
2781 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002782 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002783 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002784 setupDrawModelViewTranslate(x, y,
2785 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2786 }
Romain Guyc88e3572011-01-22 00:32:12 -08002787 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002788
Romain Guyc88e3572011-01-22 00:32:12 -08002789 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2790 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002791
Romain Guyc88e3572011-01-22 00:32:12 -08002792 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002793
Chet Haased15ebf22012-09-05 11:40:29 -07002794 mColorFilter = oldFilter;
2795
Romain Guy3a3133d2011-02-01 22:59:58 -08002796#if DEBUG_LAYERS_AS_REGIONS
2797 drawRegionRects(layer->region);
2798#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002799 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002800
2801 if (debugLayerUpdate) {
2802 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2803 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2804 }
Romain Guyf219da52011-01-16 12:54:25 -08002805 }
Chet Haase48659092012-05-31 15:21:51 -07002806
2807 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002808}
2809
Romain Guy6926c722010-07-12 20:20:03 -07002810///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002811// Shaders
2812///////////////////////////////////////////////////////////////////////////////
2813
2814void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002815 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002816}
2817
Romain Guy06f96e22010-07-30 19:18:16 -07002818void OpenGLRenderer::setupShader(SkiaShader* shader) {
2819 mShader = shader;
2820 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002821 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002822 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002823}
2824
Romain Guyd27977d2010-07-14 19:18:51 -07002825///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002826// Color filters
2827///////////////////////////////////////////////////////////////////////////////
2828
2829void OpenGLRenderer::resetColorFilter() {
2830 mColorFilter = NULL;
2831}
2832
2833void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2834 mColorFilter = filter;
2835}
2836
2837///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002838// Drop shadow
2839///////////////////////////////////////////////////////////////////////////////
2840
2841void OpenGLRenderer::resetShadow() {
2842 mHasShadow = false;
2843}
2844
2845void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2846 mHasShadow = true;
2847 mShadowRadius = radius;
2848 mShadowDx = dx;
2849 mShadowDy = dy;
2850 mShadowColor = color;
2851}
2852
2853///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002854// Draw filters
2855///////////////////////////////////////////////////////////////////////////////
2856
2857void OpenGLRenderer::resetPaintFilter() {
2858 mHasDrawFilter = false;
2859}
2860
2861void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2862 mHasDrawFilter = true;
2863 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2864 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2865}
2866
2867SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002868 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002869
2870 uint32_t flags = paint->getFlags();
2871
2872 mFilteredPaint = *paint;
2873 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2874
2875 return &mFilteredPaint;
2876}
2877
2878///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002879// Drawing implementation
2880///////////////////////////////////////////////////////////////////////////////
2881
Romain Guy01d58e42011-01-19 21:54:02 -08002882void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2883 float x, float y, SkPaint* paint) {
2884 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2885 return;
2886 }
2887
2888 int alpha;
2889 SkXfermode::Mode mode;
2890 getAlphaAndMode(paint, &alpha, &mode);
2891
2892 setupDraw();
2893 setupDrawWithTexture(true);
2894 setupDrawAlpha8Color(paint->getColor(), alpha);
2895 setupDrawColorFilter();
2896 setupDrawShader();
2897 setupDrawBlending(true, mode);
2898 setupDrawProgram();
2899 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2900 setupDrawTexture(texture->id);
2901 setupDrawPureColorUniforms();
2902 setupDrawColorFilterUniforms();
2903 setupDrawShaderUniforms();
2904 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2905
2906 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2907
2908 finishDrawTexture();
2909}
2910
Romain Guyf607bdc2010-09-10 19:20:06 -07002911// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002912#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2913#define kStdUnderline_Offset (1.0f / 9.0f)
2914#define kStdUnderline_Thickness (1.0f / 18.0f)
2915
2916void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2917 float x, float y, SkPaint* paint) {
2918 // Handle underline and strike-through
2919 uint32_t flags = paint->getFlags();
2920 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002921 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002922 float underlineWidth = length;
2923 // If length is > 0.0f, we already measured the text for the text alignment
2924 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002925 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002926 }
2927
Romain Guy211370f2012-02-01 16:10:55 -08002928 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002929 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002930 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002931
Raph Levien8b4072d2012-07-30 15:50:00 -07002932 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002933 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002934
Romain Guyf6834472011-01-23 13:32:12 -08002935 int linesCount = 0;
2936 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2937 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2938
2939 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002940 float points[pointsCount];
2941 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002942
2943 if (flags & SkPaint::kUnderlineText_Flag) {
2944 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002945 points[currentPoint++] = left;
2946 points[currentPoint++] = top;
2947 points[currentPoint++] = left + underlineWidth;
2948 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002949 }
2950
2951 if (flags & SkPaint::kStrikeThruText_Flag) {
2952 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002953 points[currentPoint++] = left;
2954 points[currentPoint++] = top;
2955 points[currentPoint++] = left + underlineWidth;
2956 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002957 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002958
Romain Guy726aeba2011-06-01 14:52:00 -07002959 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002960
Romain Guy726aeba2011-06-01 14:52:00 -07002961 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002962 }
2963 }
2964}
2965
Romain Guy026c5e162010-06-28 17:12:22 -07002966void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002967 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002968 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002969 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002970 color |= 0x00ffffff;
2971 }
2972
Romain Guy70ca14e2010-12-13 18:24:33 -08002973 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002974 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002975 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002976 setupDrawShader();
2977 setupDrawColorFilter();
2978 setupDrawBlending(mode);
2979 setupDrawProgram();
2980 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2981 setupDrawColorUniforms();
2982 setupDrawShaderUniforms(ignoreTransform);
2983 setupDrawColorFilterUniforms();
2984 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002985
Romain Guyc95c8d62010-09-17 15:31:32 -07002986 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2987}
2988
Romain Guy82ba8142010-07-09 13:25:56 -07002989void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002990 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002991 int alpha;
2992 SkXfermode::Mode mode;
2993 getAlphaAndMode(paint, &alpha, &mode);
2994
Romain Guyd21b6e12011-11-30 20:21:23 -08002995 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002996
Romain Guy211370f2012-02-01 16:10:55 -08002997 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002998 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2999 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
3000
Romain Guyd21b6e12011-11-30 20:21:23 -08003001 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003002 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3003 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3004 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3005 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003006 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003007 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3008 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3009 GL_TRIANGLE_STRIP, gMeshCount);
3010 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003011}
3012
Romain Guybd6b79b2010-06-26 00:13:53 -07003013void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003014 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3015 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003016 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003017}
3018
3019void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003020 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003021 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003022 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003023
Romain Guy746b7402010-10-26 16:27:31 -07003024 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003025 setupDrawWithTexture();
3026 setupDrawColor(alpha, alpha, alpha, alpha);
3027 setupDrawColorFilter();
3028 setupDrawBlending(blend, mode, swapSrcDst);
3029 setupDrawProgram();
3030 if (!dirty) {
3031 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07003032 }
Romain Guy5b3b3522010-10-27 18:57:51 -07003033 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003034 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003035 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003036 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003037 }
Romain Guy86568192010-12-14 15:55:39 -08003038 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003039 setupDrawColorFilterUniforms();
3040 setupDrawTexture(texture);
3041 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003042
Romain Guy6820ac82010-09-15 18:11:50 -07003043 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003044
3045 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003046}
3047
Romain Guya5aed0d2010-09-09 14:42:43 -07003048void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003049 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003050 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003051
Romain Guy82ba8142010-07-09 13:25:56 -07003052 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003053 // These blend modes are not supported by OpenGL directly and have
3054 // to be implemented using shaders. Since the shader will perform
3055 // the blending, turn blending off here
3056 // If the blend mode cannot be implemented using shaders, fall
3057 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003058 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08003059 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003060 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003061 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003062
Romain Guy82bc7a72012-01-03 14:13:39 -08003063 if (mCaches.blend) {
3064 glDisable(GL_BLEND);
3065 mCaches.blend = false;
3066 }
3067
3068 return;
3069 } else {
3070 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003071 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003072 }
3073
3074 if (!mCaches.blend) {
3075 glEnable(GL_BLEND);
3076 }
3077
3078 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3079 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3080
3081 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3082 glBlendFunc(sourceMode, destMode);
3083 mCaches.lastSrcMode = sourceMode;
3084 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003085 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003086 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003087 glDisable(GL_BLEND);
3088 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003089 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003090}
3091
Romain Guy889f8d12010-07-29 14:37:42 -07003092bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003093 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003094 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003095 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003096 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003097 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003098 }
Romain Guy6926c722010-07-12 20:20:03 -07003099 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003100}
3101
Romain Guy026c5e162010-06-28 17:12:22 -07003102void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003103 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003104 TextureVertex::setUV(v++, u1, v1);
3105 TextureVertex::setUV(v++, u2, v1);
3106 TextureVertex::setUV(v++, u1, v2);
3107 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003108}
3109
Chet Haase5c13d892010-10-08 08:37:55 -07003110void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003111 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003112 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003113}
3114
Romain Guy9d5316e2010-06-24 19:30:36 -07003115}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003116}; // namespace android