blob: 9b9ca12e954a6e54b3876f337779e8f3ecd6afdd [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 Guy2b7028e2012-09-19 17:25:38 -0700178 mDirtyClip = mOpaqueFrame = opaque;
Romain Guyddf74372012-05-22 14:07:07 -0700179
Romain Guy45e4c3d2012-09-11 17:17:07 -0700180 // If we know that we are going to redraw the entire framebuffer,
181 // perform a discard to let the driver know we don't need to preserve
182 // the back buffer for this frame.
183 if (mCaches.extensions.hasDiscardFramebuffer() &&
184 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
185 const GLenum attachments[] = { getTargetFbo() == 0 ? GL_COLOR_EXT : GL_COLOR_ATTACHMENT0 };
186 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
187 }
188
Romain Guyddf74372012-05-22 14:07:07 -0700189 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800190
Romain Guy2b7028e2012-09-19 17:25:38 -0700191 mTilingSnapshot = mSnapshot;
192 startTiling();
193
Romain Guy6b7bd242010-10-06 19:49:23 -0700194 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700195 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700196 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700197 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700198 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700199 } else {
200 mCaches.resetScissor();
201 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700202
203 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700204}
205
206void OpenGLRenderer::syncState() {
207 glViewport(0, 0, mWidth, mHeight);
208
209 if (mCaches.blend) {
210 glEnable(GL_BLEND);
211 } else {
212 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700213 }
Romain Guybb9524b2010-06-22 18:56:38 -0700214}
215
Romain Guy2b7028e2012-09-19 17:25:38 -0700216void OpenGLRenderer::startTiling() {
217 startTiling(mTilingSnapshot);
218}
219
220void OpenGLRenderer::startTiling(const sp<Snapshot>& s) {
221 bool opaque = mOpaqueFrame;
222 Rect* clip = mTilingSnapshot->clipRect;
223
224 if (s->flags & Snapshot::kFlagIsFboLayer) {
225 opaque = !s->layer->isBlend();
226 clip = s->clipRect;
227 }
228
229 mCaches.startTiling(clip->left, s->height - clip->bottom,
230 clip->right - clip->left, clip->bottom - clip->top, opaque);
231}
232
233void OpenGLRenderer::endTiling() {
234 mCaches.endTiling();
235}
236
Romain Guyb025b9c2010-09-16 14:16:48 -0700237void OpenGLRenderer::finish() {
Romain Guy2b7028e2012-09-19 17:25:38 -0700238 endTiling();
239
Romain Guyb025b9c2010-09-16 14:16:48 -0700240#if DEBUG_OPENGL
241 GLenum status = GL_NO_ERROR;
242 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000243 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800244 switch (status) {
Romain Guya44a63a2012-04-26 14:05:02 -0700245 case GL_INVALID_ENUM:
246 ALOGE(" GL_INVALID_ENUM");
247 break;
248 case GL_INVALID_VALUE:
249 ALOGE(" GL_INVALID_VALUE");
250 break;
251 case GL_INVALID_OPERATION:
252 ALOGE(" GL_INVALID_OPERATION");
253 break;
Romain Guya07105b2011-01-10 21:14:18 -0800254 case GL_OUT_OF_MEMORY:
Romain Guya44a63a2012-04-26 14:05:02 -0700255 ALOGE(" Out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800256 break;
257 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700258 }
259#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800260#if DEBUG_MEMORY_USAGE
261 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800262#else
263 if (mCaches.getDebugLevel() & kDebugMemory) {
264 mCaches.dumpMemoryUsage();
265 }
Romain Guyc15008e2010-11-10 11:59:15 -0800266#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700267}
268
Romain Guy6c319ca2011-01-11 14:29:25 -0800269void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700270 if (mCaches.currentProgram) {
271 if (mCaches.currentProgram->isInUse()) {
272 mCaches.currentProgram->remove();
273 mCaches.currentProgram = NULL;
274 }
275 }
Romain Guy50c0f092010-10-19 11:42:22 -0700276 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800277 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800278 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800279 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700280}
281
Romain Guy6c319ca2011-01-11 14:29:25 -0800282void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800283 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800284 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700285 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
286
Romain Guy3e263fa2011-12-12 16:47:48 -0800287 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
288
Chet Haase80250612012-08-15 13:46:54 -0700289 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700290 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800291 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700292 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700293
Romain Guya1d3c912011-12-13 14:55:06 -0800294 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700295
Romain Guy50c0f092010-10-19 11:42:22 -0700296 mCaches.blend = true;
297 glEnable(GL_BLEND);
298 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
299 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700300}
301
Romain Guy35643dd2012-09-18 15:40:58 -0700302void OpenGLRenderer::resumeAfterLayer() {
303 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
304 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
305 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
306
307 mCaches.resetScissor();
308 dirtyClip();
309}
310
Romain Guyba6be8a2012-04-23 18:22:09 -0700311void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700312 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700313}
314
315void OpenGLRenderer::attachFunctor(Functor* functor) {
316 mFunctors.add(functor);
317}
318
Romain Guy8f3b8e32012-03-27 16:33:45 -0700319status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
320 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700321 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700322
Romain Guyba6be8a2012-04-23 18:22:09 -0700323 if (count > 0) {
324 SortedVector<Functor*> functors(mFunctors);
325 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700326
Romain Guyba6be8a2012-04-23 18:22:09 -0700327 DrawGlInfo info;
328 info.clipLeft = 0;
329 info.clipTop = 0;
330 info.clipRight = 0;
331 info.clipBottom = 0;
332 info.isLayer = false;
333 info.width = 0;
334 info.height = 0;
335 memset(info.transform, 0, sizeof(float) * 16);
336
337 for (size_t i = 0; i < count; i++) {
338 Functor* f = functors.itemAt(i);
339 result |= (*f)(DrawGlInfo::kModeProcess, &info);
340
Chris Craikc2c95432012-04-25 15:13:52 -0700341 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700342 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
343 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700344 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700345
Chris Craikc2c95432012-04-25 15:13:52 -0700346 if (result & DrawGlInfo::kStatusInvoke) {
347 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700348 }
349 }
Chet Haasebeb8bd02012-09-09 16:13:26 -0700350 // protect against functors binding to other buffers
351 mCaches.unbindMeshBuffer();
352 mCaches.unbindIndicesBuffer();
353 mCaches.activeTexture(0);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700354 }
355
356 return result;
357}
358
359status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800360 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700361 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700362
Romain Guy8a4ac612012-07-17 17:32:48 -0700363 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800364 if (mDirtyClip) {
365 setScissorFromClip();
366 }
Romain Guyd643bb52011-03-01 14:55:21 -0800367
Romain Guy80911b82011-03-16 15:30:12 -0700368 Rect clip(*mSnapshot->clipRect);
369 clip.snapToPixelBoundaries();
370
Romain Guyd643bb52011-03-01 14:55:21 -0800371 // Since we don't know what the functor will draw, let's dirty
372 // tne entire clip region
373 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800374 dirtyLayerUnchecked(clip, getRegion());
375 }
Romain Guyd643bb52011-03-01 14:55:21 -0800376
Romain Guy08aa2cb2011-03-17 11:06:57 -0700377 DrawGlInfo info;
378 info.clipLeft = clip.left;
379 info.clipTop = clip.top;
380 info.clipRight = clip.right;
381 info.clipBottom = clip.bottom;
382 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700383 info.width = getSnapshot()->viewport.getWidth();
384 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700385 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700386
Chet Haase48659092012-05-31 15:21:51 -0700387 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800388
Romain Guy8f3b8e32012-03-27 16:33:45 -0700389 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700390 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800391 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700392
Chris Craik65924a32012-04-05 17:52:11 -0700393 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700394 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700395 }
Romain Guycabfcc12011-03-07 18:06:46 -0800396 }
397
Chet Haasedaf98e92011-01-10 14:10:36 -0800398 resume();
Romain Guy65549432012-03-26 16:45:05 -0700399 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800400}
401
Romain Guyf6a11b82010-06-23 17:47:49 -0700402///////////////////////////////////////////////////////////////////////////////
403// State management
404///////////////////////////////////////////////////////////////////////////////
405
Romain Guybb9524b2010-06-22 18:56:38 -0700406int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700407 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700408}
409
410int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700411 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700412}
413
414void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700415 if (mSaveCount > 1) {
416 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700417 }
Romain Guybb9524b2010-06-22 18:56:38 -0700418}
419
420void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700421 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700422
Romain Guy8fb95422010-08-17 18:38:51 -0700423 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700424 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700425 }
Romain Guybb9524b2010-06-22 18:56:38 -0700426}
427
Romain Guy8aef54f2010-09-01 15:13:49 -0700428int OpenGLRenderer::saveSnapshot(int flags) {
429 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700430 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700431}
432
433bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700434 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700435 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700436 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700437
Romain Guybd6b79b2010-06-26 00:13:53 -0700438 sp<Snapshot> current = mSnapshot;
439 sp<Snapshot> previous = mSnapshot->previous;
440
Romain Guyeb993562010-10-05 18:14:38 -0700441 if (restoreOrtho) {
442 Rect& r = previous->viewport;
443 glViewport(r.left, r.top, r.right, r.bottom);
444 mOrthoMatrix.load(current->orthoMatrix);
445 }
446
Romain Guy8b55f372010-08-18 17:10:07 -0700447 mSaveCount--;
448 mSnapshot = previous;
449
Romain Guy2542d192010-08-18 11:47:12 -0700450 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700451 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700452 }
Romain Guy2542d192010-08-18 11:47:12 -0700453
Romain Guy5ec99242010-11-03 16:19:08 -0700454 if (restoreLayer) {
455 composeLayer(current, previous);
456 }
457
Romain Guy2542d192010-08-18 11:47:12 -0700458 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700459}
460
Romain Guyf6a11b82010-06-23 17:47:49 -0700461///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700462// Layers
463///////////////////////////////////////////////////////////////////////////////
464
465int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700466 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700467 const GLuint previousFbo = mSnapshot->fbo;
468 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700469
Romain Guyaf636eb2010-12-09 17:47:21 -0800470 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700471 int alpha = 255;
472 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700473
Romain Guye45362c2010-11-03 19:58:32 -0700474 if (p) {
475 alpha = p->getAlpha();
476 if (!mCaches.extensions.hasFramebufferFetch()) {
477 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
478 if (!isMode) {
479 // Assume SRC_OVER
480 mode = SkXfermode::kSrcOver_Mode;
481 }
482 } else {
483 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700484 }
485 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700486 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700487 }
Romain Guyd55a8612010-06-28 17:42:46 -0700488
Chet Haased48885a2012-08-28 17:43:28 -0700489 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700490 }
Romain Guyd55a8612010-06-28 17:42:46 -0700491
492 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700493}
494
495int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
496 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700497 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700498 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700499 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700500 SkPaint paint;
501 paint.setAlpha(alpha);
502 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700503 }
Romain Guyd55a8612010-06-28 17:42:46 -0700504}
Romain Guybd6b79b2010-06-26 00:13:53 -0700505
Romain Guy1c740bc2010-09-13 18:00:09 -0700506/**
507 * Layers are viewed by Skia are slightly different than layers in image editing
508 * programs (for instance.) When a layer is created, previously created layers
509 * and the frame buffer still receive every drawing command. For instance, if a
510 * layer is created and a shape intersecting the bounds of the layers and the
511 * framebuffer is draw, the shape will be drawn on both (unless the layer was
512 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
513 *
514 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
515 * texture. Unfortunately, this is inefficient as it requires every primitive to
516 * be drawn n + 1 times, where n is the number of active layers. In practice this
517 * means, for every primitive:
518 * - Switch active frame buffer
519 * - Change viewport, clip and projection matrix
520 * - Issue the drawing
521 *
522 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700523 * To avoid this, layers are implemented in a different way here, at least in the
524 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
525 * is set. When this flag is set we can redirect all drawing operations into a
526 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700527 *
528 * This implementation relies on the frame buffer being at least RGBA 8888. When
529 * a layer is created, only a texture is created, not an FBO. The content of the
530 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700531 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700532 * buffer and drawing continues as normal. This technique therefore treats the
533 * frame buffer as a scratch buffer for the layers.
534 *
535 * To compose the layers back onto the frame buffer, each layer texture
536 * (containing the original frame buffer data) is drawn as a simple quad over
537 * the frame buffer. The trick is that the quad is set as the composition
538 * destination in the blending equation, and the frame buffer becomes the source
539 * of the composition.
540 *
541 * Drawing layers with an alpha value requires an extra step before composition.
542 * An empty quad is drawn over the layer's region in the frame buffer. This quad
543 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
544 * quad is used to multiply the colors in the frame buffer. This is achieved by
545 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
546 * GL_ZERO, GL_SRC_ALPHA.
547 *
548 * Because glCopyTexImage2D() can be slow, an alternative implementation might
549 * be use to draw a single clipped layer. The implementation described above
550 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700551 *
552 * (1) The frame buffer is actually not cleared right away. To allow the GPU
553 * to potentially optimize series of calls to glCopyTexImage2D, the frame
554 * buffer is left untouched until the first drawing operation. Only when
555 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700556 */
Chet Haased48885a2012-08-28 17:43:28 -0700557bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
558 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700559 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700560 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700561
Romain Guyeb993562010-10-05 18:14:38 -0700562 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
563
Romain Guyf607bdc2010-09-10 19:20:06 -0700564 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700565 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700566 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700567 Rect untransformedBounds(bounds);
568 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700569
Chet Haased48885a2012-08-28 17:43:28 -0700570 // Layers only make sense if they are in the framebuffer's bounds
571 if (bounds.intersect(*mSnapshot->clipRect)) {
572 // We cannot work with sub-pixels in this case
573 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700574
Chet Haased48885a2012-08-28 17:43:28 -0700575 // When the layer is not an FBO, we may use glCopyTexImage so we
576 // need to make sure the layer does not extend outside the bounds
577 // of the framebuffer
578 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700579 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700580 } else if (fboLayer) {
581 clip.set(bounds);
582 mat4 inverse;
583 inverse.loadInverse(*mSnapshot->transform);
584 inverse.mapRect(clip);
585 clip.snapToPixelBoundaries();
586 if (clip.intersect(untransformedBounds)) {
587 clip.translate(-left, -top);
588 bounds.set(untransformedBounds);
589 } else {
590 clip.setEmpty();
591 }
Romain Guyad37cd32011-03-15 11:12:25 -0700592 }
Chet Haased48885a2012-08-28 17:43:28 -0700593 } else {
594 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700595 }
Romain Guybf434112010-09-16 14:40:17 -0700596
Romain Guy746b7402010-10-26 16:27:31 -0700597 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700598 bounds.getHeight() > mCaches.maxTextureSize ||
599 (fboLayer && clip.isEmpty())) {
600 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700601 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700602 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700603 }
604
605 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700606 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700607 return false;
608 }
Romain Guyf18fd992010-07-08 11:45:51 -0700609
Romain Guya1d3c912011-12-13 14:55:06 -0800610 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700611 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700612 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700613 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700614 }
615
Romain Guy9ace8f52011-07-07 20:50:11 -0700616 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700617 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700618 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
619 bounds.getWidth() / float(layer->getWidth()), 0.0f);
620 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700621 layer->setBlend(true);
Romain Guydda57022010-07-06 11:39:32 -0700622
Romain Guy8fb95422010-08-17 18:38:51 -0700623 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700624 mSnapshot->flags |= Snapshot::kFlagIsLayer;
625 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700626
Romain Guyeb993562010-10-05 18:14:38 -0700627 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700628 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700629 } else {
630 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700631 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800632 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700633 if (layer->isEmpty()) {
634 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700635 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700636 layer->getWidth(), layer->getHeight(), 0);
637 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800638 } else {
639 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700640 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800641 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700642
Romain Guy54be1cd2011-06-13 19:04:27 -0700643 // Enqueue the buffer coordinates to clear the corresponding region later
644 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700645 }
Romain Guyeb993562010-10-05 18:14:38 -0700646 }
Romain Guyf86ef572010-07-01 11:05:42 -0700647
Romain Guyd55a8612010-06-28 17:42:46 -0700648 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700649}
650
Chet Haased48885a2012-08-28 17:43:28 -0700651bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700652 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700653
Chet Haased48885a2012-08-28 17:43:28 -0700654 mSnapshot->region = &mSnapshot->layer->region;
655 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700656
Chet Haased48885a2012-08-28 17:43:28 -0700657 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
658 mSnapshot->fbo = layer->getFbo();
659 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
660 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
661 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
662 mSnapshot->height = bounds.getHeight();
663 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
664 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700665
Romain Guy2b7028e2012-09-19 17:25:38 -0700666 endTiling();
Romain Guy5b3b3522010-10-27 18:57:51 -0700667 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700668 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
669 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700670
671 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700672 if (layer->isEmpty()) {
673 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
674 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700675 }
676
677 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700678 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700679
Romain Guy2b7028e2012-09-19 17:25:38 -0700680 startTiling(mSnapshot);
Romain Guy5b3b3522010-10-27 18:57:51 -0700681
682 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700683 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800684 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700685 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700686 glClear(GL_COLOR_BUFFER_BIT);
687
688 dirtyClip();
689
690 // Change the ortho projection
691 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
692 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
693
694 return true;
695}
696
Romain Guy1c740bc2010-09-13 18:00:09 -0700697/**
698 * Read the documentation of createLayer() before doing anything in this method.
699 */
Romain Guy1d83e192010-08-17 11:37:00 -0700700void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
701 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000702 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700703 return;
704 }
705
Romain Guy5b3b3522010-10-27 18:57:51 -0700706 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700707
708 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700709 endTiling();
710
Romain Guye0aa84b2012-04-03 19:30:26 -0700711 // Detach the texture from the FBO
712 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guyeb993562010-10-05 18:14:38 -0700713 // Unbind current FBO and restore previous one
714 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy2b7028e2012-09-19 17:25:38 -0700715
716 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700717 }
718
Romain Guy1d83e192010-08-17 11:37:00 -0700719 Layer* layer = current->layer;
720 const Rect& rect = layer->layer;
721
Romain Guy9ace8f52011-07-07 20:50:11 -0700722 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700723 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700724 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700725 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700726 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700727 }
728
Romain Guy03750a02010-10-18 14:06:08 -0700729 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700730
Romain Guya1d3c912011-12-13 14:55:06 -0800731 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700732
Romain Guy5b3b3522010-10-27 18:57:51 -0700733 // When the layer is stored in an FBO, we can save a bit of fillrate by
734 // drawing only the dirty region
735 if (fboLayer) {
736 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700737 if (layer->getColorFilter()) {
738 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800739 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700740 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700741 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800742 resetColorFilter();
743 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700744 } else if (!rect.isEmpty()) {
745 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
746 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700747 }
Romain Guy8b55f372010-08-18 17:10:07 -0700748
Romain Guyeb993562010-10-05 18:14:38 -0700749 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800750 // Note: No need to use glDiscardFramebufferEXT() since we never
751 // create/compose layers that are not on screen with this
752 // code path
753 // See LayerRenderer::destroyLayer(Layer*)
754
Romain Guyeb993562010-10-05 18:14:38 -0700755 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
756 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700757 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700758 }
759
Romain Guy746b7402010-10-26 16:27:31 -0700760 dirtyClip();
761
Romain Guyeb993562010-10-05 18:14:38 -0700762 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700763 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700764 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700765 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700766 }
767}
768
Romain Guyaa6c24c2011-04-28 18:40:04 -0700769void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700770 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700771
Romain Guy302a9df2011-08-16 13:55:02 -0700772 mat4& transform = layer->getTransform();
773 if (!transform.isIdentity()) {
774 save(0);
775 mSnapshot->transform->multiply(transform);
776 }
777
Romain Guyaa6c24c2011-04-28 18:40:04 -0700778 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700779 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700780 setupDrawWithTexture();
781 } else {
782 setupDrawWithExternalTexture();
783 }
784 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700785 setupDrawColor(alpha, alpha, alpha, alpha);
786 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700787 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700788 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700789 setupDrawPureColorUniforms();
790 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700791 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
792 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700793 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700794 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700795 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700796 if (mSnapshot->transform->isPureTranslate() &&
797 layer->getWidth() == (uint32_t) rect.getWidth() &&
798 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700799 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
800 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
801
Romain Guyd21b6e12011-11-30 20:21:23 -0800802 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700803 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
804 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800805 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700806 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
807 }
808 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700809 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
810
811 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
812
813 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700814
815 if (!transform.isIdentity()) {
816 restore();
817 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700818}
819
Romain Guy5b3b3522010-10-27 18:57:51 -0700820void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700821 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700822 const Rect& texCoords = layer->texCoords;
823 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
824 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700825
Romain Guy9ace8f52011-07-07 20:50:11 -0700826 float x = rect.left;
827 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700828 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700829 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700830 layer->getHeight() == (uint32_t) rect.getHeight();
831
832 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700833 // When we're swapping, the layer is already in screen coordinates
834 if (!swap) {
835 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
836 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
837 }
838
Romain Guyd21b6e12011-11-30 20:21:23 -0800839 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700840 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800841 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700842 }
843
844 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
845 layer->getTexture(), layer->getAlpha() / 255.0f,
846 layer->getMode(), layer->isBlend(),
847 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
848 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700849
Romain Guyaa6c24c2011-04-28 18:40:04 -0700850 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
851 } else {
852 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
853 drawTextureLayer(layer, rect);
854 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
855 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700856}
857
858void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700859 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700860 layer->setRegionAsRect();
861
Romain Guy40667672011-03-18 14:34:03 -0700862 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700863
Romain Guy5b3b3522010-10-27 18:57:51 -0700864 layer->region.clear();
865 return;
866 }
867
Romain Guy8a3957d2011-09-07 17:55:15 -0700868 // TODO: See LayerRenderer.cpp::generateMesh() for important
869 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800870 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700871 size_t count;
872 const android::Rect* rects = layer->region.getArray(&count);
873
Romain Guy9ace8f52011-07-07 20:50:11 -0700874 const float alpha = layer->getAlpha() / 255.0f;
875 const float texX = 1.0f / float(layer->getWidth());
876 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800877 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700878
879 TextureVertex* mesh = mCaches.getRegionMesh();
880 GLsizei numQuads = 0;
881
Romain Guy7230a742011-01-10 22:26:16 -0800882 setupDraw();
883 setupDrawWithTexture();
884 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800885 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700886 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800887 setupDrawProgram();
888 setupDrawDirtyRegionsDisabled();
889 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800890 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700891 setupDrawTexture(layer->getTexture());
892 if (mSnapshot->transform->isPureTranslate()) {
893 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
894 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
895
Romain Guyd21b6e12011-11-30 20:21:23 -0800896 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700897 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
898 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800899 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700900 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
901 }
Romain Guy15bc6432011-12-13 13:11:32 -0800902 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700903
904 for (size_t i = 0; i < count; i++) {
905 const android::Rect* r = &rects[i];
906
907 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800908 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700909 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800910 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700911
912 // TODO: Reject quads outside of the clip
913 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
914 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
915 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
916 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
917
918 numQuads++;
919
920 if (numQuads >= REGION_MESH_QUAD_COUNT) {
921 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
922 numQuads = 0;
923 mesh = mCaches.getRegionMesh();
924 }
925 }
926
927 if (numQuads > 0) {
928 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
929 }
930
Romain Guy7230a742011-01-10 22:26:16 -0800931 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700932
933#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800934 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700935#endif
936
937 layer->region.clear();
938 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700939}
940
Romain Guy3a3133d2011-02-01 22:59:58 -0800941void OpenGLRenderer::drawRegionRects(const Region& region) {
942#if DEBUG_LAYERS_AS_REGIONS
943 size_t count;
944 const android::Rect* rects = region.getArray(&count);
945
946 uint32_t colors[] = {
947 0x7fff0000, 0x7f00ff00,
948 0x7f0000ff, 0x7fff00ff,
949 };
950
951 int offset = 0;
952 int32_t top = rects[0].top;
953
954 for (size_t i = 0; i < count; i++) {
955 if (top != rects[i].top) {
956 offset ^= 0x2;
957 top = rects[i].top;
958 }
959
960 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
961 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
962 SkXfermode::kSrcOver_Mode);
963 }
964#endif
965}
966
Romain Guy5b3b3522010-10-27 18:57:51 -0700967void OpenGLRenderer::dirtyLayer(const float left, const float top,
968 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -0800969 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700970 Rect bounds(left, top, right, bottom);
971 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800972 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700973 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700974}
975
976void OpenGLRenderer::dirtyLayer(const float left, const float top,
977 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -0800978 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700979 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800980 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800981 }
Romain Guy1bd1bad2011-01-14 20:07:20 -0800982}
983
984void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -0800985 if (bounds.intersect(*mSnapshot->clipRect)) {
986 bounds.snapToPixelBoundaries();
987 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
988 if (!dirty.isEmpty()) {
989 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700990 }
991 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700992}
993
Romain Guy54be1cd2011-06-13 19:04:27 -0700994void OpenGLRenderer::clearLayerRegions() {
995 const size_t count = mLayers.size();
996 if (count == 0) return;
997
998 if (!mSnapshot->isIgnored()) {
999 // Doing several glScissor/glClear here can negatively impact
1000 // GPUs with a tiler architecture, instead we draw quads with
1001 // the Clear blending mode
1002
1003 // The list contains bounds that have already been clipped
1004 // against their initial clip rect, and the current clip
1005 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001006 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001007
1008 Vertex mesh[count * 6];
1009 Vertex* vertex = mesh;
1010
1011 for (uint32_t i = 0; i < count; i++) {
1012 Rect* bounds = mLayers.itemAt(i);
1013
1014 Vertex::set(vertex++, bounds->left, bounds->bottom);
1015 Vertex::set(vertex++, bounds->left, bounds->top);
1016 Vertex::set(vertex++, bounds->right, bounds->top);
1017 Vertex::set(vertex++, bounds->left, bounds->bottom);
1018 Vertex::set(vertex++, bounds->right, bounds->top);
1019 Vertex::set(vertex++, bounds->right, bounds->bottom);
1020
1021 delete bounds;
1022 }
1023
1024 setupDraw(false);
1025 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1026 setupDrawBlending(true, SkXfermode::kClear_Mode);
1027 setupDrawProgram();
1028 setupDrawPureColorUniforms();
1029 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001030 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001031
Romain Guy54be1cd2011-06-13 19:04:27 -07001032 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001033
1034 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001035 } else {
1036 for (uint32_t i = 0; i < count; i++) {
1037 delete mLayers.itemAt(i);
1038 }
1039 }
1040
1041 mLayers.clear();
1042}
1043
Romain Guybd6b79b2010-06-26 00:13:53 -07001044///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001045// Transforms
1046///////////////////////////////////////////////////////////////////////////////
1047
1048void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001049 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001050}
1051
1052void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001053 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001054}
1055
1056void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001057 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001058}
1059
Romain Guy807daf72011-01-18 11:19:19 -08001060void OpenGLRenderer::skew(float sx, float sy) {
1061 mSnapshot->transform->skew(sx, sy);
1062}
1063
Romain Guyf6a11b82010-06-23 17:47:49 -07001064void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001065 if (matrix) {
1066 mSnapshot->transform->load(*matrix);
1067 } else {
1068 mSnapshot->transform->loadIdentity();
1069 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001070}
1071
1072void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001073 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001074}
1075
1076void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001077 SkMatrix transform;
1078 mSnapshot->transform->copyTo(transform);
1079 transform.preConcat(*matrix);
1080 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001081}
1082
1083///////////////////////////////////////////////////////////////////////////////
1084// Clipping
1085///////////////////////////////////////////////////////////////////////////////
1086
Romain Guybb9524b2010-06-22 18:56:38 -07001087void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001088 Rect clip(*mSnapshot->clipRect);
1089 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001090
Romain Guy8a4ac612012-07-17 17:32:48 -07001091 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1092 clip.getWidth(), clip.getHeight())) {
1093 mDirtyClip = false;
1094 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001095}
1096
1097const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001098 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001099}
1100
Romain Guy8a4ac612012-07-17 17:32:48 -07001101bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1102 if (mSnapshot->isIgnored()) {
1103 return true;
1104 }
1105
1106 Rect r(left, top, right, bottom);
1107 mSnapshot->transform->mapRect(r);
1108 r.snapToPixelBoundaries();
1109
1110 Rect clipRect(*mSnapshot->clipRect);
1111 clipRect.snapToPixelBoundaries();
1112
1113 return !clipRect.intersects(r);
1114}
1115
Romain Guy35643dd2012-09-18 15:40:58 -07001116bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1117 Rect& transformed, Rect& clip) {
1118 if (mSnapshot->isIgnored()) {
1119 return true;
1120 }
1121
1122 transformed.set(left, top, right, bottom);
1123 mSnapshot->transform->mapRect(transformed);
1124 transformed.snapToPixelBoundaries();
1125
1126 clip.set(*mSnapshot->clipRect);
1127 clip.snapToPixelBoundaries();
1128
1129 return !clip.intersects(transformed);
1130}
1131
Romain Guyc7d53492010-06-25 13:41:57 -07001132bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001133 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001134 return true;
1135 }
1136
Romain Guy1d83e192010-08-17 11:37:00 -07001137 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001138 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001139 r.snapToPixelBoundaries();
1140
1141 Rect clipRect(*mSnapshot->clipRect);
1142 clipRect.snapToPixelBoundaries();
1143
Romain Guy586cae32012-07-13 15:28:31 -07001144 bool rejected = !clipRect.intersects(r);
1145 if (!isDeferred() && !rejected) {
1146 mCaches.setScissorEnabled(!clipRect.contains(r));
1147 }
1148
1149 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001150}
1151
Romain Guy079ba2c2010-07-16 14:12:24 -07001152bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1153 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001154 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001155 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001156 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001157 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001158}
1159
Chet Haasea23eed82012-04-12 15:19:04 -07001160Rect* OpenGLRenderer::getClipRect() {
1161 return mSnapshot->clipRect;
1162}
1163
Romain Guyf6a11b82010-06-23 17:47:49 -07001164///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001165// Drawing commands
1166///////////////////////////////////////////////////////////////////////////////
1167
Romain Guy54be1cd2011-06-13 19:04:27 -07001168void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001169 // TODO: It would be best if we could do this before quickReject()
1170 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001171 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001172 if (mDirtyClip) {
1173 setScissorFromClip();
1174 }
1175 mDescription.reset();
1176 mSetShaderColor = false;
1177 mColorSet = false;
1178 mColorA = mColorR = mColorG = mColorB = 0.0f;
1179 mTextureUnit = 0;
1180 mTrackDirtyRegions = true;
1181}
1182
1183void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1184 mDescription.hasTexture = true;
1185 mDescription.hasAlpha8Texture = isAlpha8;
1186}
1187
Romain Guyaa6c24c2011-04-28 18:40:04 -07001188void OpenGLRenderer::setupDrawWithExternalTexture() {
1189 mDescription.hasExternalTexture = true;
1190}
1191
Romain Guy15bc6432011-12-13 13:11:32 -08001192void OpenGLRenderer::setupDrawNoTexture() {
1193 mCaches.disbaleTexCoordsVertexArray();
1194}
1195
Chet Haase5b0200b2011-04-13 17:58:08 -07001196void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001197 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001198}
1199
Chris Craik6ebdc112012-08-31 18:24:33 -07001200void OpenGLRenderer::setupDrawAARect() {
1201 mDescription.isAARect = true;
1202}
1203
Romain Guyed6fcb02011-03-21 13:11:28 -07001204void OpenGLRenderer::setupDrawPoint(float pointSize) {
1205 mDescription.isPoint = true;
1206 mDescription.pointSize = pointSize;
1207}
1208
Romain Guy70ca14e2010-12-13 18:24:33 -08001209void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001210 setupDrawColor(color, (color >> 24) & 0xFF);
1211}
1212
1213void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1214 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001215 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1216 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001217 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001218 mColorR = a * ((color >> 16) & 0xFF);
1219 mColorG = a * ((color >> 8) & 0xFF);
1220 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001221 mColorSet = true;
1222 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1223}
1224
Romain Guy86568192010-12-14 15:55:39 -08001225void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1226 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001227 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1228 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001229 const float a = mColorA / 255.0f;
1230 mColorR = a * ((color >> 16) & 0xFF);
1231 mColorG = a * ((color >> 8) & 0xFF);
1232 mColorB = a * ((color ) & 0xFF);
1233 mColorSet = true;
1234 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1235}
1236
Romain Guy41210632012-07-16 17:04:24 -07001237void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1238 mCaches.fontRenderer->describe(mDescription, paint);
1239}
1240
Romain Guy70ca14e2010-12-13 18:24:33 -08001241void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1242 mColorA = a;
1243 mColorR = r;
1244 mColorG = g;
1245 mColorB = b;
1246 mColorSet = true;
1247 mSetShaderColor = mDescription.setColor(r, g, b, a);
1248}
1249
1250void OpenGLRenderer::setupDrawShader() {
1251 if (mShader) {
1252 mShader->describe(mDescription, mCaches.extensions);
1253 }
1254}
1255
1256void OpenGLRenderer::setupDrawColorFilter() {
1257 if (mColorFilter) {
1258 mColorFilter->describe(mDescription, mCaches.extensions);
1259 }
1260}
1261
Romain Guyf09ef512011-05-27 11:43:46 -07001262void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1263 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1264 mColorA = 1.0f;
1265 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001266 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001267 }
1268}
1269
Romain Guy70ca14e2010-12-13 18:24:33 -08001270void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001271 // When the blending mode is kClear_Mode, we need to use a modulate color
1272 // argb=1,0,0,0
1273 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001274 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1275 mDescription, swapSrcDst);
1276}
1277
1278void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001279 // When the blending mode is kClear_Mode, we need to use a modulate color
1280 // argb=1,0,0,0
1281 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001282 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1283 mDescription, swapSrcDst);
1284}
1285
1286void OpenGLRenderer::setupDrawProgram() {
1287 useProgram(mCaches.programCache.get(mDescription));
1288}
1289
1290void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1291 mTrackDirtyRegions = false;
1292}
1293
1294void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1295 bool ignoreTransform) {
1296 mModelView.loadTranslate(left, top, 0.0f);
1297 if (!ignoreTransform) {
1298 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1299 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1300 } else {
1301 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1302 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1303 }
1304}
1305
Chet Haase8a5cc922011-04-26 07:28:09 -07001306void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1307 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001308}
1309
Romain Guy70ca14e2010-12-13 18:24:33 -08001310void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1311 bool ignoreTransform, bool ignoreModelView) {
1312 if (!ignoreModelView) {
1313 mModelView.loadTranslate(left, top, 0.0f);
1314 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001315 } else {
1316 mModelView.loadIdentity();
1317 }
Romain Guy86568192010-12-14 15:55:39 -08001318 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1319 if (!ignoreTransform) {
1320 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1321 if (mTrackDirtyRegions && dirty) {
1322 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1323 }
1324 } else {
1325 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1326 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1327 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001328}
1329
Romain Guyed6fcb02011-03-21 13:11:28 -07001330void OpenGLRenderer::setupDrawPointUniforms() {
1331 int slot = mCaches.currentProgram->getUniform("pointSize");
1332 glUniform1f(slot, mDescription.pointSize);
1333}
1334
Romain Guy70ca14e2010-12-13 18:24:33 -08001335void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001336 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001337 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1338 }
1339}
1340
Romain Guy86568192010-12-14 15:55:39 -08001341void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001342 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001343 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001344 }
1345}
1346
Romain Guy70ca14e2010-12-13 18:24:33 -08001347void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1348 if (mShader) {
1349 if (ignoreTransform) {
1350 mModelView.loadInverse(*mSnapshot->transform);
1351 }
1352 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1353 }
1354}
1355
Romain Guy8d0d4782010-12-14 20:13:35 -08001356void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1357 if (mShader) {
1358 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1359 }
1360}
1361
Romain Guy70ca14e2010-12-13 18:24:33 -08001362void OpenGLRenderer::setupDrawColorFilterUniforms() {
1363 if (mColorFilter) {
1364 mColorFilter->setupProgram(mCaches.currentProgram);
1365 }
1366}
1367
Romain Guy41210632012-07-16 17:04:24 -07001368void OpenGLRenderer::setupDrawTextGammaUniforms() {
1369 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1370}
1371
Romain Guy70ca14e2010-12-13 18:24:33 -08001372void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001373 bool force = mCaches.bindMeshBuffer();
1374 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001375 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001376}
1377
1378void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1379 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001380 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001381 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001382}
1383
Romain Guyaa6c24c2011-04-28 18:40:04 -07001384void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1385 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001386 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001387 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001388}
1389
Romain Guy8f0095c2011-05-02 17:24:22 -07001390void OpenGLRenderer::setupDrawTextureTransform() {
1391 mDescription.hasTextureTransform = true;
1392}
1393
1394void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001395 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1396 GL_FALSE, &transform.data[0]);
1397}
1398
Romain Guy70ca14e2010-12-13 18:24:33 -08001399void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001400 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001401 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001402 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001403 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001404 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001405 }
Romain Guyd71dd362011-12-12 19:03:35 -08001406
Romain Guyf3a910b42011-12-12 20:35:21 -08001407 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001408 if (mCaches.currentProgram->texCoords >= 0) {
1409 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1410 }
1411
1412 mCaches.unbindIndicesBuffer();
1413}
1414
1415void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1416 bool force = mCaches.unbindMeshBuffer();
1417 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1418 if (mCaches.currentProgram->texCoords >= 0) {
1419 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001420 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001421}
1422
Chet Haase5b0200b2011-04-13 17:58:08 -07001423void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001424 bool force = mCaches.unbindMeshBuffer();
1425 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1426 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001427 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001428}
1429
1430/**
1431 * 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 -07001432 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1433 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1434 * attributes (one per vertex) are values from zero to one that tells the fragment
1435 * shader where the fragment is in relation to the line width/length overall; these values are
1436 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1437 * region of the line.
1438 * Note that we only pass down the width values in this setup function. The length coordinates
1439 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001440 */
Chet Haase99585ad2011-05-02 15:00:16 -07001441void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001442 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001443 bool force = mCaches.unbindMeshBuffer();
1444 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1445 vertices, gAAVertexStride);
1446 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001447 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001448
Romain Guy7b631422012-04-04 11:38:54 -07001449 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001450 glEnableVertexAttribArray(widthSlot);
1451 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001452
Romain Guy7b631422012-04-04 11:38:54 -07001453 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001454 glEnableVertexAttribArray(lengthSlot);
1455 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001456
Chet Haase5b0200b2011-04-13 17:58:08 -07001457 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001458 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001459}
1460
1461void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1462 glDisableVertexAttribArray(widthSlot);
1463 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001464}
1465
Romain Guy70ca14e2010-12-13 18:24:33 -08001466void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001467}
1468
1469///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001470// Drawing
1471///////////////////////////////////////////////////////////////////////////////
1472
Chet Haase1271e2c2012-04-20 09:54:27 -07001473status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001474 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001475
Romain Guy0fe478e2010-11-08 12:08:41 -08001476 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1477 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001478 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001479 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001480 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001481
Romain Guy65549432012-03-26 16:45:05 -07001482 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001483}
1484
Chet Haaseed30fd82011-04-22 16:18:45 -07001485void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1486 if (displayList) {
1487 displayList->output(*this, level);
1488 }
1489}
1490
Romain Guya168d732011-03-18 16:50:13 -07001491void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1492 int alpha;
1493 SkXfermode::Mode mode;
1494 getAlphaAndMode(paint, &alpha, &mode);
1495
Romain Guya168d732011-03-18 16:50:13 -07001496 float x = left;
1497 float y = top;
1498
Romain Guye3c26852011-07-25 16:36:01 -07001499 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001500 bool ignoreTransform = false;
1501 if (mSnapshot->transform->isPureTranslate()) {
1502 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1503 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1504 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001505 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001506 } else {
1507 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001508 }
1509
1510 setupDraw();
1511 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001512 if (paint) {
1513 setupDrawAlpha8Color(paint->getColor(), alpha);
1514 }
Romain Guya168d732011-03-18 16:50:13 -07001515 setupDrawColorFilter();
1516 setupDrawShader();
1517 setupDrawBlending(true, mode);
1518 setupDrawProgram();
1519 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001520
Romain Guya168d732011-03-18 16:50:13 -07001521 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001522 texture->setWrap(GL_CLAMP_TO_EDGE);
1523 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001524
Romain Guya168d732011-03-18 16:50:13 -07001525 setupDrawPureColorUniforms();
1526 setupDrawColorFilterUniforms();
1527 setupDrawShaderUniforms();
1528 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1529
1530 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1531
1532 finishDrawTexture();
1533}
1534
Chet Haase48659092012-05-31 15:21:51 -07001535status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001536 const float right = left + bitmap->width();
1537 const float bottom = top + bitmap->height();
1538
1539 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001540 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001541 }
1542
Romain Guya1d3c912011-12-13 14:55:06 -08001543 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001544 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001545 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001546 const AutoTexture autoCleanup(texture);
1547
Romain Guy211370f2012-02-01 16:10:55 -08001548 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001549 drawAlphaBitmap(texture, left, top, paint);
1550 } else {
1551 drawTextureRect(left, top, right, bottom, texture, paint);
1552 }
Chet Haase48659092012-05-31 15:21:51 -07001553
1554 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001555}
1556
Chet Haase48659092012-05-31 15:21:51 -07001557status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001558 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1559 const mat4 transform(*matrix);
1560 transform.mapRect(r);
1561
Romain Guy6926c722010-07-12 20:20:03 -07001562 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001563 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001564 }
1565
Romain Guya1d3c912011-12-13 14:55:06 -08001566 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001567 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001568 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001569 const AutoTexture autoCleanup(texture);
1570
Romain Guy5b3b3522010-10-27 18:57:51 -07001571 // This could be done in a cheaper way, all we need is pass the matrix
1572 // to the vertex shader. The save/restore is a bit overkill.
1573 save(SkCanvas::kMatrix_SaveFlag);
1574 concatMatrix(matrix);
1575 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1576 restore();
Chet Haase48659092012-05-31 15:21:51 -07001577
1578 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001579}
1580
Chet Haase48659092012-05-31 15:21:51 -07001581status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001582 const float right = left + bitmap->width();
1583 const float bottom = top + bitmap->height();
1584
1585 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001586 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001587 }
1588
1589 mCaches.activeTexture(0);
1590 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1591 const AutoTexture autoCleanup(texture);
1592
1593 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001594
1595 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001596}
1597
Chet Haase48659092012-05-31 15:21:51 -07001598status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001599 float* vertices, int* colors, SkPaint* paint) {
1600 // TODO: Do a quickReject
1601 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001602 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001603 }
1604
Romain Guya1d3c912011-12-13 14:55:06 -08001605 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001606 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001607 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001608 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001609
Romain Guyd21b6e12011-11-30 20:21:23 -08001610 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1611 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001612
1613 int alpha;
1614 SkXfermode::Mode mode;
1615 getAlphaAndMode(paint, &alpha, &mode);
1616
Romain Guy5a7b4662011-01-20 19:09:30 -08001617 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001618
Romain Guyb18d2d02011-02-10 15:52:54 -08001619 float left = FLT_MAX;
1620 float top = FLT_MAX;
1621 float right = FLT_MIN;
1622 float bottom = FLT_MIN;
1623
Romain Guy211370f2012-02-01 16:10:55 -08001624 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001625
Romain Guya566b7c2011-01-23 16:36:11 -08001626 // TODO: Support the colors array
1627 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001628 TextureVertex* vertex = mesh;
1629 for (int32_t y = 0; y < meshHeight; y++) {
1630 for (int32_t x = 0; x < meshWidth; x++) {
1631 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1632
1633 float u1 = float(x) / meshWidth;
1634 float u2 = float(x + 1) / meshWidth;
1635 float v1 = float(y) / meshHeight;
1636 float v2 = float(y + 1) / meshHeight;
1637
1638 int ax = i + (meshWidth + 1) * 2;
1639 int ay = ax + 1;
1640 int bx = i;
1641 int by = bx + 1;
1642 int cx = i + 2;
1643 int cy = cx + 1;
1644 int dx = i + (meshWidth + 1) * 2 + 2;
1645 int dy = dx + 1;
1646
1647 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1648 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1649 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1650
1651 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1652 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1653 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001654
Romain Guyb18d2d02011-02-10 15:52:54 -08001655 if (hasActiveLayer) {
1656 // TODO: This could be optimized to avoid unnecessary ops
1657 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1658 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1659 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1660 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1661 }
Romain Guy5a7b4662011-01-20 19:09:30 -08001662 }
1663 }
1664
Romain Guyb18d2d02011-02-10 15:52:54 -08001665 if (hasActiveLayer) {
1666 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1667 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001668
Romain Guy5a7b4662011-01-20 19:09:30 -08001669 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1670 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001671 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001672
1673 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001674}
1675
Chet Haase48659092012-05-31 15:21:51 -07001676status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001677 float srcLeft, float srcTop, float srcRight, float srcBottom,
1678 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001679 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001680 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001681 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001682 }
1683
Romain Guya1d3c912011-12-13 14:55:06 -08001684 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001685 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001686 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001687 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001688
Romain Guy8ba548f2010-06-30 19:21:21 -07001689 const float width = texture->width;
1690 const float height = texture->height;
1691
Romain Guy68169722011-08-22 17:33:33 -07001692 const float u1 = fmax(0.0f, srcLeft / width);
1693 const float v1 = fmax(0.0f, srcTop / height);
1694 const float u2 = fmin(1.0f, srcRight / width);
1695 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001696
Romain Guy03750a02010-10-18 14:06:08 -07001697 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001698 resetDrawTextureTexCoords(u1, v1, u2, v2);
1699
Romain Guy03750a02010-10-18 14:06:08 -07001700 int alpha;
1701 SkXfermode::Mode mode;
1702 getAlphaAndMode(paint, &alpha, &mode);
1703
Romain Guyd21b6e12011-11-30 20:21:23 -08001704 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1705
Romain Guy211370f2012-02-01 16:10:55 -08001706 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001707 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1708 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1709
Romain Guyb5014982011-07-28 15:39:12 -07001710 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001711 // Enable linear filtering if the source rectangle is scaled
1712 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001713 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001714 }
Romain Guyb5014982011-07-28 15:39:12 -07001715
Romain Guyd21b6e12011-11-30 20:21:23 -08001716 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001717 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1718 texture->id, alpha / 255.0f, mode, texture->blend,
1719 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1720 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1721 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001722 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001723 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1724 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1725 GL_TRIANGLE_STRIP, gMeshCount);
1726 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001727
1728 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001729
1730 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001731}
1732
Chet Haase48659092012-05-31 15:21:51 -07001733status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001734 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001735 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001736 int alpha;
1737 SkXfermode::Mode mode;
1738 getAlphaAndModeDirect(paint, &alpha, &mode);
1739
1740 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1741 left, top, right, bottom, alpha, mode);
1742}
1743
1744status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1745 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1746 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001747 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001748 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001749 }
1750
Romain Guybe6f9dc2012-07-16 12:41:17 -07001751 alpha *= mSnapshot->alpha;
1752
Romain Guya1d3c912011-12-13 14:55:06 -08001753 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001754 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001755 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001756 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001757 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1758 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001759
Romain Guy2728f962010-10-08 18:36:15 -07001760 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001761 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001762
Romain Guy211370f2012-02-01 16:10:55 -08001763 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001764 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001765 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001766 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001767 const float offsetX = left + mSnapshot->transform->getTranslateX();
1768 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001769 const size_t count = mesh->quads.size();
1770 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001771 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001772 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001773 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1774 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1775 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001776 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001777 dirtyLayer(left + bounds.left, top + bounds.top,
1778 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001779 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001780 }
1781 }
1782
Romain Guy211370f2012-02-01 16:10:55 -08001783 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001784 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1785 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1786
1787 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1788 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1789 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1790 true, !mesh->hasEmptyQuads);
1791 } else {
1792 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1793 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1794 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1795 true, !mesh->hasEmptyQuads);
1796 }
Romain Guy054dc182010-10-15 17:55:25 -07001797 }
Chet Haase48659092012-05-31 15:21:51 -07001798
1799 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001800}
1801
Chet Haase99ecdc42011-05-06 12:06:34 -07001802/**
Chet Haase858aa932011-05-12 09:06:00 -07001803 * This function uses a similar approach to that of AA lines in the drawLines() function.
Chris Craik6ebdc112012-08-31 18:24:33 -07001804 * We expand the rectangle by a half pixel in screen space on all sides. However, instead of using
1805 * a fragment shader to compute the translucency of the color from its position, we simply use a
1806 * varying parameter to define how far a given pixel is into the region.
Chet Haase858aa932011-05-12 09:06:00 -07001807 */
1808void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001809 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001810 float inverseScaleX = 1.0f;
1811 float inverseScaleY = 1.0f;
Romain Guy04299382012-07-18 17:15:41 -07001812
Chet Haase858aa932011-05-12 09:06:00 -07001813 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001814 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001815 Matrix4 *mat = mSnapshot->transform;
1816 float m00 = mat->data[Matrix4::kScaleX];
1817 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase858aa932011-05-12 09:06:00 -07001818 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001819 float m11 = mat->data[Matrix4::kScaleY];
Chet Haase858aa932011-05-12 09:06:00 -07001820 float scaleX = sqrt(m00 * m00 + m01 * m01);
1821 float scaleY = sqrt(m10 * m10 + m11 * m11);
1822 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1823 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1824 }
1825
Chet Haase858aa932011-05-12 09:06:00 -07001826 float boundarySizeX = .5 * inverseScaleX;
1827 float boundarySizeY = .5 * inverseScaleY;
1828
Chris Craik6ebdc112012-08-31 18:24:33 -07001829 float innerLeft = left + boundarySizeX;
1830 float innerRight = right - boundarySizeX;
1831 float innerTop = top + boundarySizeY;
1832 float innerBottom = bottom - boundarySizeY;
1833
Chet Haase858aa932011-05-12 09:06:00 -07001834 // Adjust the rect by the AA boundary padding
1835 left -= boundarySizeX;
1836 right += boundarySizeX;
1837 top -= boundarySizeY;
1838 bottom += boundarySizeY;
1839
Chet Haase858aa932011-05-12 09:06:00 -07001840 if (!quickReject(left, top, right, bottom)) {
Romain Guy04299382012-07-18 17:15:41 -07001841 setupDraw();
1842 setupDrawNoTexture();
Chris Craik6ebdc112012-08-31 18:24:33 -07001843 setupDrawAARect();
Romain Guy04299382012-07-18 17:15:41 -07001844 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1845 setupDrawColorFilter();
1846 setupDrawShader();
1847 setupDrawBlending(true, mode);
1848 setupDrawProgram();
1849 setupDrawModelViewIdentity(true);
1850 setupDrawColorUniforms();
1851 setupDrawColorFilterUniforms();
1852 setupDrawShaderIdentityUniforms();
1853
Chris Craik6ebdc112012-08-31 18:24:33 -07001854 AlphaVertex rects[14];
1855 AlphaVertex* aVertices = &rects[0];
1856 void* alphaCoords = ((GLbyte*) aVertices) + gVertexAlphaOffset;
Romain Guy04299382012-07-18 17:15:41 -07001857
Chris Craik6ebdc112012-08-31 18:24:33 -07001858 bool force = mCaches.unbindMeshBuffer();
1859 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1860 aVertices, gAlphaVertexStride);
1861 mCaches.resetTexCoordsVertexPointer();
1862 mCaches.unbindIndicesBuffer();
Romain Guy04299382012-07-18 17:15:41 -07001863
Chris Craik6ebdc112012-08-31 18:24:33 -07001864 int alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
1865 glEnableVertexAttribArray(alphaSlot);
1866 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Romain Guy04299382012-07-18 17:15:41 -07001867
Chris Craik6ebdc112012-08-31 18:24:33 -07001868 // draw left
1869 AlphaVertex::set(aVertices++, left, bottom, 0);
1870 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1871 AlphaVertex::set(aVertices++, left, top, 0);
1872 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001873
Chris Craik6ebdc112012-08-31 18:24:33 -07001874 // draw top
1875 AlphaVertex::set(aVertices++, right, top, 0);
1876 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001877
Chris Craik6ebdc112012-08-31 18:24:33 -07001878 // draw right
1879 AlphaVertex::set(aVertices++, right, bottom, 0);
1880 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1881
1882 // draw bottom
1883 AlphaVertex::set(aVertices++, left, bottom, 0);
1884 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1885
1886 // draw inner rect (repeating last vertex to create degenerate bridge triangles)
1887 // TODO: also consider drawing the inner rect without the blending-forced shader, if
1888 // blending is expensive. Note: can't use drawColorRect() since it doesn't use vertex
1889 // buffers like below, resulting in slightly different transformed coordinates.
1890 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1891 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
1892 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1893 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
1894
Chet Haase858aa932011-05-12 09:06:00 -07001895 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guy7b631422012-04-04 11:38:54 -07001896
Chris Craik6ebdc112012-08-31 18:24:33 -07001897 glDrawArrays(GL_TRIANGLE_STRIP, 0, 14);
Romain Guy04299382012-07-18 17:15:41 -07001898
Chris Craik6ebdc112012-08-31 18:24:33 -07001899 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001900 }
Chet Haase858aa932011-05-12 09:06:00 -07001901}
1902
1903/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001904 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1905 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1906 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1907 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1908 * of the line. Hairlines are more involved because we need to account for transform scaling
1909 * to end up with a one-pixel-wide line in screen space..
1910 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1911 * in combination with values that we calculate and pass down in this method. The basic approach
1912 * is that the quad we create contains both the core line area plus a bounding area in which
1913 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1914 * proportion of the width and the length of a given segment is represented by the boundary
1915 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1916 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1917 * on the inside). This ends up giving the result we want, with pixels that are completely
1918 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1919 * how far into the boundary region they are, which is determined by shader interpolation.
1920 */
Chet Haase48659092012-05-31 15:21:51 -07001921status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1922 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001923
1924 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001925 // We use half the stroke width here because we're going to position the quad
1926 // corner vertices half of the width away from the line endpoints
1927 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001928 // A stroke width of 0 has a special meaning in Skia:
1929 // it draws a line 1 px wide regardless of current transform
1930 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001931
Chet Haase99ecdc42011-05-06 12:06:34 -07001932 float inverseScaleX = 1.0f;
1933 float inverseScaleY = 1.0f;
1934 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001935
Chet Haase8a5cc922011-04-26 07:28:09 -07001936 int alpha;
1937 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001938
Chet Haase8a5cc922011-04-26 07:28:09 -07001939 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001940 int verticesCount = count;
1941 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001942 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001943 verticesCount += (count - 4);
1944 }
1945
1946 if (isHairLine || isAA) {
1947 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1948 // the line on the screen should always be one pixel wide regardless of scale. For
1949 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001950 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001951 Matrix4 *mat = mSnapshot->transform;
1952 float m00 = mat->data[Matrix4::kScaleX];
1953 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07001954 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001955 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07001956
Romain Guy211370f2012-02-01 16:10:55 -08001957 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1958 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001959
Chet Haase99ecdc42011-05-06 12:06:34 -07001960 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1961 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001962
Chet Haase99ecdc42011-05-06 12:06:34 -07001963 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1964 scaled = true;
1965 }
1966 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001967 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001968
1969 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07001970
1971 mCaches.enableScissor();
1972
Chet Haase8a5cc922011-04-26 07:28:09 -07001973 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001974 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001975 if (isAA) {
1976 setupDrawAALine();
1977 }
1978 setupDrawColor(paint->getColor(), alpha);
1979 setupDrawColorFilter();
1980 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001981 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001982 setupDrawProgram();
1983 setupDrawModelViewIdentity(true);
1984 setupDrawColorUniforms();
1985 setupDrawColorFilterUniforms();
1986 setupDrawShaderIdentityUniforms();
1987
1988 if (isHairLine) {
1989 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001990 halfStrokeWidth = isAA? 1 : .5;
1991 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001992 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001993 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001994 }
Romain Guy7b631422012-04-04 11:38:54 -07001995
1996 int widthSlot;
1997 int lengthSlot;
1998
Chet Haase5b0200b2011-04-13 17:58:08 -07001999 Vertex lines[verticesCount];
2000 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002001
Chet Haase99585ad2011-05-02 15:00:16 -07002002 AAVertex wLines[verticesCount];
2003 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002004
Romain Guy211370f2012-02-01 16:10:55 -08002005 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002006 setupDrawVertices(vertices);
2007 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002008 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2009 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002010 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002011 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2012 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002013 // We will need to calculate the actual width proportion on each segment for
2014 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002015 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002016 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2017 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002018 }
2019
Chet Haase99ecdc42011-05-06 12:06:34 -07002020 AAVertex* prevAAVertex = NULL;
2021 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002022
Chet Haase99585ad2011-05-02 15:00:16 -07002023 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002024 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002025
Chet Haase5b0200b2011-04-13 17:58:08 -07002026 for (int i = 0; i < count; i += 4) {
2027 // a = start point, b = end point
2028 vec2 a(points[i], points[i + 1]);
2029 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002030
Chet Haase99585ad2011-05-02 15:00:16 -07002031 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002032 float boundaryLengthProportion = 0;
2033 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002034
Chet Haase5b0200b2011-04-13 17:58:08 -07002035 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002036 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002037 float x = n.x;
2038 n.x = -n.y;
2039 n.y = x;
2040
Chet Haase8a5cc922011-04-26 07:28:09 -07002041 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002042 if (isAA) {
2043 float wideningFactor;
2044 if (fabs(n.x) >= fabs(n.y)) {
2045 wideningFactor = fabs(1.0f / n.x);
2046 } else {
2047 wideningFactor = fabs(1.0f / n.y);
2048 }
2049 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002050 }
Romain Guy7b631422012-04-04 11:38:54 -07002051
Chet Haase99ecdc42011-05-06 12:06:34 -07002052 if (scaled) {
2053 n.x *= inverseScaleX;
2054 n.y *= inverseScaleY;
2055 }
2056 } else if (scaled) {
2057 // Extend n by .5 pixel on each side, post-transform
2058 vec2 extendedN = n.copyNormalized();
2059 extendedN /= 2;
2060 extendedN.x *= inverseScaleX;
2061 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002062
Chet Haase99ecdc42011-05-06 12:06:34 -07002063 float extendedNLength = extendedN.length();
2064 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002065 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002066 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002067 }
Romain Guy7b631422012-04-04 11:38:54 -07002068
Chet Haase99585ad2011-05-02 15:00:16 -07002069 // aa lines expand the endpoint vertices to encompass the AA boundary
2070 if (isAA) {
2071 vec2 abVector = (b - a);
2072 length = abVector.length();
2073 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002074
Chet Haase99ecdc42011-05-06 12:06:34 -07002075 if (scaled) {
2076 abVector.x *= inverseScaleX;
2077 abVector.y *= inverseScaleY;
2078 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002079 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002080 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002081 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002082 }
Romain Guy7b631422012-04-04 11:38:54 -07002083
Chet Haase99ecdc42011-05-06 12:06:34 -07002084 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002085 a -= abVector;
2086 b += abVector;
2087 }
2088
Chet Haase5b0200b2011-04-13 17:58:08 -07002089 // Four corners of the rectangle defining a thick line
2090 vec2 p1 = a - n;
2091 vec2 p2 = a + n;
2092 vec2 p3 = b + n;
2093 vec2 p4 = b - n;
2094
Chet Haase99585ad2011-05-02 15:00:16 -07002095
Chet Haase5b0200b2011-04-13 17:58:08 -07002096 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2097 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2098 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2099 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2100
Romain Guy04299382012-07-18 17:15:41 -07002101 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002102 if (!isAA) {
2103 if (prevVertex != NULL) {
2104 // Issue two repeat vertices to create degenerate triangles to bridge
2105 // between the previous line and the new one. This is necessary because
2106 // we are creating a single triangle_strip which will contain
2107 // potentially discontinuous line segments.
2108 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2109 Vertex::set(vertices++, p1.x, p1.y);
2110 generatedVerticesCount += 2;
2111 }
Romain Guy7b631422012-04-04 11:38:54 -07002112
Chet Haase5b0200b2011-04-13 17:58:08 -07002113 Vertex::set(vertices++, p1.x, p1.y);
2114 Vertex::set(vertices++, p2.x, p2.y);
2115 Vertex::set(vertices++, p4.x, p4.y);
2116 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002117
Chet Haase5b0200b2011-04-13 17:58:08 -07002118 prevVertex = vertices - 1;
2119 generatedVerticesCount += 4;
2120 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002121 if (!isHairLine && scaled) {
2122 // Must set width proportions per-segment for scaled non-hairlines to use the
2123 // correct AA boundary dimensions
2124 if (boundaryWidthSlot < 0) {
2125 boundaryWidthSlot =
2126 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002127 }
Romain Guy7b631422012-04-04 11:38:54 -07002128
Chet Haase99ecdc42011-05-06 12:06:34 -07002129 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002130 }
Romain Guy7b631422012-04-04 11:38:54 -07002131
Chet Haase99585ad2011-05-02 15:00:16 -07002132 if (boundaryLengthSlot < 0) {
2133 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002134 }
Romain Guy7b631422012-04-04 11:38:54 -07002135
Chet Haase99ecdc42011-05-06 12:06:34 -07002136 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002137
Chet Haase5b0200b2011-04-13 17:58:08 -07002138 if (prevAAVertex != NULL) {
2139 // Issue two repeat vertices to create degenerate triangles to bridge
2140 // between the previous line and the new one. This is necessary because
2141 // we are creating a single triangle_strip which will contain
2142 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002143 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2144 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2145 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002146 generatedVerticesCount += 2;
2147 }
Romain Guy7b631422012-04-04 11:38:54 -07002148
Chet Haase99585ad2011-05-02 15:00:16 -07002149 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2150 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2151 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2152 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002153
Chet Haase5b0200b2011-04-13 17:58:08 -07002154 prevAAVertex = aaVertices - 1;
2155 generatedVerticesCount += 4;
2156 }
Romain Guy7b631422012-04-04 11:38:54 -07002157
Chet Haase99585ad2011-05-02 15:00:16 -07002158 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2159 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2160 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002161 }
2162 }
Romain Guy7b631422012-04-04 11:38:54 -07002163
Chet Haase5b0200b2011-04-13 17:58:08 -07002164 if (generatedVerticesCount > 0) {
2165 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2166 }
Romain Guy7b631422012-04-04 11:38:54 -07002167
2168 if (isAA) {
2169 finishDrawAALine(widthSlot, lengthSlot);
2170 }
Chet Haase48659092012-05-31 15:21:51 -07002171
2172 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002173}
2174
Chet Haase48659092012-05-31 15:21:51 -07002175status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2176 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002177
2178 // TODO: The paint's cap style defines whether the points are square or circular
2179 // TODO: Handle AA for round points
2180
Chet Haase5b0200b2011-04-13 17:58:08 -07002181 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002182 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002183 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002184 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002185 if (isHairLine) {
2186 // Now that we know it's hairline, we can set the effective width, to be used later
2187 strokeWidth = 1.0f;
2188 }
2189 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002190 int alpha;
2191 SkXfermode::Mode mode;
2192 getAlphaAndMode(paint, &alpha, &mode);
2193
2194 int verticesCount = count >> 1;
2195 int generatedVerticesCount = 0;
2196
2197 TextureVertex pointsData[verticesCount];
2198 TextureVertex* vertex = &pointsData[0];
2199
Romain Guy04299382012-07-18 17:15:41 -07002200 // TODO: We should optimize this method to not generate vertices for points
2201 // that lie outside of the clip.
2202 mCaches.enableScissor();
2203
Romain Guyed6fcb02011-03-21 13:11:28 -07002204 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002205 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002206 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002207 setupDrawColor(paint->getColor(), alpha);
2208 setupDrawColorFilter();
2209 setupDrawShader();
2210 setupDrawBlending(mode);
2211 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002212 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002213 setupDrawColorUniforms();
2214 setupDrawColorFilterUniforms();
2215 setupDrawPointUniforms();
2216 setupDrawShaderIdentityUniforms();
2217 setupDrawMesh(vertex);
2218
2219 for (int i = 0; i < count; i += 2) {
2220 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2221 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002222
Chet Haase8a5cc922011-04-26 07:28:09 -07002223 float left = points[i] - halfWidth;
2224 float right = points[i] + halfWidth;
2225 float top = points[i + 1] - halfWidth;
2226 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002227
Chet Haase8a5cc922011-04-26 07:28:09 -07002228 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002229 }
2230
2231 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002232
2233 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002234}
2235
Chet Haase48659092012-05-31 15:21:51 -07002236status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002237 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002238 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002239
Romain Guyae88e5e2010-10-22 17:49:18 -07002240 Rect& clip(*mSnapshot->clipRect);
2241 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002242
Romain Guy3d58c032010-07-14 16:34:53 -07002243 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002244
2245 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002246}
Romain Guy9d5316e2010-06-24 19:30:36 -07002247
Chet Haase48659092012-05-31 15:21:51 -07002248status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2249 SkPaint* paint) {
2250 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002251 const AutoTexture autoCleanup(texture);
2252
2253 const float x = left + texture->left - texture->offset;
2254 const float y = top + texture->top - texture->offset;
2255
2256 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002257
2258 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002259}
2260
Chet Haase48659092012-05-31 15:21:51 -07002261status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002262 float rx, float ry, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002263 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002264
Romain Guya1d3c912011-12-13 14:55:06 -08002265 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002266 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2267 right - left, bottom - top, rx, ry, paint);
Chet Haase48659092012-05-31 15:21:51 -07002268 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002269}
2270
Chet Haase48659092012-05-31 15:21:51 -07002271status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2272 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002273
Romain Guya1d3c912011-12-13 14:55:06 -08002274 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002275 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Chet Haase48659092012-05-31 15:21:51 -07002276 return drawShape(x - radius, y - radius, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002277}
Romain Guy01d58e42011-01-19 21:54:02 -08002278
Chet Haase48659092012-05-31 15:21:51 -07002279status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
2280 SkPaint* paint) {
2281 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002282
Romain Guya1d3c912011-12-13 14:55:06 -08002283 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002284 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002285 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002286}
2287
Chet Haase48659092012-05-31 15:21:51 -07002288status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002289 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002290 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002291
2292 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002293 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002294 }
2295
Romain Guya1d3c912011-12-13 14:55:06 -08002296 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002297 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2298 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002299 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002300}
2301
Chet Haase48659092012-05-31 15:21:51 -07002302status_t OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002303 SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002304 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002305
Romain Guya1d3c912011-12-13 14:55:06 -08002306 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002307 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002308 return drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002309}
2310
Chet Haase48659092012-05-31 15:21:51 -07002311status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002312 if (p->getStyle() != SkPaint::kFill_Style) {
Chet Haase48659092012-05-31 15:21:51 -07002313 return drawRectAsShape(left, top, right, bottom, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002314 }
2315
Romain Guy6926c722010-07-12 20:20:03 -07002316 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002317 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002318 }
2319
Romain Guy026c5e162010-06-28 17:12:22 -07002320 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002321 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002322 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2323 if (!isMode) {
2324 // Assume SRC_OVER
2325 mode = SkXfermode::kSrcOver_Mode;
2326 }
2327 } else {
2328 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002329 }
2330
Romain Guy026c5e162010-06-28 17:12:22 -07002331 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002332 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002333 drawAARect(left, top, right, bottom, color, mode);
2334 } else {
2335 drawColorRect(left, top, right, bottom, color, mode);
2336 }
Chet Haase48659092012-05-31 15:21:51 -07002337
2338 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002339}
Romain Guy9d5316e2010-06-24 19:30:36 -07002340
Raph Levien416a8472012-07-19 22:48:17 -07002341void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2342 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2343 float x, float y) {
2344 mCaches.activeTexture(0);
2345
2346 // NOTE: The drop shadow will not perform gamma correction
2347 // if shader-based correction is enabled
2348 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2349 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2350 paint, text, bytesCount, count, mShadowRadius, positions);
2351 const AutoTexture autoCleanup(shadow);
2352
2353 const float sx = x - shadow->left + mShadowDx;
2354 const float sy = y - shadow->top + mShadowDy;
2355
2356 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2357 int shadowColor = mShadowColor;
2358 if (mShader) {
2359 shadowColor = 0xffffffff;
2360 }
2361
2362 setupDraw();
2363 setupDrawWithTexture(true);
2364 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2365 setupDrawColorFilter();
2366 setupDrawShader();
2367 setupDrawBlending(true, mode);
2368 setupDrawProgram();
2369 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2370 setupDrawTexture(shadow->id);
2371 setupDrawPureColorUniforms();
2372 setupDrawColorFilterUniforms();
2373 setupDrawShaderUniforms();
2374 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2375
2376 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2377}
2378
Chet Haase48659092012-05-31 15:21:51 -07002379status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002380 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002381 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002382 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002383 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002384 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002385
Romain Guy671d6cf2012-01-18 12:39:17 -08002386 // NOTE: Skia does not support perspective transform on drawPosText yet
2387 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002388 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002389 }
2390
2391 float x = 0.0f;
2392 float y = 0.0f;
2393 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2394 if (pureTranslate) {
2395 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2396 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2397 }
2398
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002399 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002400 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2401 paint->getTextSize());
2402
2403 int alpha;
2404 SkXfermode::Mode mode;
2405 getAlphaAndMode(paint, &alpha, &mode);
2406
Raph Levien416a8472012-07-19 22:48:17 -07002407 if (CC_UNLIKELY(mHasShadow)) {
2408 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2409 0.0f, 0.0f);
2410 }
2411
Romain Guy671d6cf2012-01-18 12:39:17 -08002412 // Pick the appropriate texture filtering
2413 bool linearFilter = mSnapshot->transform->changesBounds();
2414 if (pureTranslate && !linearFilter) {
2415 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2416 }
2417
2418 mCaches.activeTexture(0);
2419 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002420 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002421 setupDrawDirtyRegionsDisabled();
2422 setupDrawWithTexture(true);
2423 setupDrawAlpha8Color(paint->getColor(), alpha);
2424 setupDrawColorFilter();
2425 setupDrawShader();
2426 setupDrawBlending(true, mode);
2427 setupDrawProgram();
2428 setupDrawModelView(x, y, x, y, pureTranslate, true);
2429 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2430 setupDrawPureColorUniforms();
2431 setupDrawColorFilterUniforms();
2432 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002433 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002434
2435 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2436 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2437
Romain Guy211370f2012-02-01 16:10:55 -08002438 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002439
2440 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2441 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002442 if (hasActiveLayer) {
2443 if (!pureTranslate) {
2444 mSnapshot->transform->mapRect(bounds);
2445 }
2446 dirtyLayerUnchecked(bounds, getRegion());
2447 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002448 }
Chet Haase48659092012-05-31 15:21:51 -07002449
2450 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002451}
2452
Romain Guyc2525952012-07-27 16:41:22 -07002453status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002454 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002455 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002456 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002457 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002458 }
2459
Chet Haasea1cff502012-02-21 13:43:44 -08002460 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002461 switch (paint->getTextAlign()) {
2462 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002463 x -= length / 2.0f;
2464 break;
2465 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002466 x -= length;
2467 break;
2468 default:
2469 break;
2470 }
2471
Romain Guycac5fd32011-12-01 20:08:50 -08002472 SkPaint::FontMetrics metrics;
2473 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002474 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002475 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002476 }
2477
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002478 const float oldX = x;
2479 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002480 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002481 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002482 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2483 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2484 }
2485
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002486#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002487 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002488 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002489#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002490
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002491 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002492 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002493 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002494
Romain Guy86568192010-12-14 15:55:39 -08002495 int alpha;
2496 SkXfermode::Mode mode;
2497 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002498
Romain Guy211370f2012-02-01 16:10:55 -08002499 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002500 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2501 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002502 }
2503
Romain Guy6620c6d2010-12-06 18:07:02 -08002504 // Pick the appropriate texture filtering
2505 bool linearFilter = mSnapshot->transform->changesBounds();
2506 if (pureTranslate && !linearFilter) {
2507 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2508 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002509
Romain Guy16c88082012-06-11 16:03:47 -07002510 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002511 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002512 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002513 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002514 setupDrawDirtyRegionsDisabled();
2515 setupDrawWithTexture(true);
2516 setupDrawAlpha8Color(paint->getColor(), alpha);
2517 setupDrawColorFilter();
2518 setupDrawShader();
2519 setupDrawBlending(true, mode);
2520 setupDrawProgram();
2521 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002522 // See comment above; the font renderer must use texture unit 0
2523 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002524 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2525 setupDrawPureColorUniforms();
2526 setupDrawColorFilterUniforms();
2527 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002528 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002529
Romain Guy6620c6d2010-12-06 18:07:02 -08002530 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002531 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2532
Romain Guy211370f2012-02-01 16:10:55 -08002533 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002534
Raph Levien996e57c2012-07-23 15:22:52 -07002535 bool status;
Raph Levien8b4072d2012-07-30 15:50:00 -07002536 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
2537 SkPaint paintCopy(*paint);
2538 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2539 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Raph Levien996e57c2012-07-23 15:22:52 -07002540 positions, hasActiveLayer ? &bounds : NULL);
2541 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002542 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2543 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002544 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002545
2546 if (status && hasActiveLayer) {
2547 if (!pureTranslate) {
2548 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002549 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002550 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002551 }
Romain Guy694b5192010-07-21 21:33:20 -07002552
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002553 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002554
2555 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002556}
2557
Chet Haase48659092012-05-31 15:21:51 -07002558status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002559 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002560 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2561 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002562 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002563 }
2564
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002565 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002566 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2567 paint->getTextSize());
2568
2569 int alpha;
2570 SkXfermode::Mode mode;
2571 getAlphaAndMode(paint, &alpha, &mode);
2572
2573 mCaches.activeTexture(0);
2574 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002575 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002576 setupDrawDirtyRegionsDisabled();
2577 setupDrawWithTexture(true);
2578 setupDrawAlpha8Color(paint->getColor(), alpha);
2579 setupDrawColorFilter();
2580 setupDrawShader();
2581 setupDrawBlending(true, mode);
2582 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002583 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002584 setupDrawTexture(fontRenderer.getTexture(true));
2585 setupDrawPureColorUniforms();
2586 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002587 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002588 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002589
Romain Guy97771732012-02-28 18:17:02 -08002590 const Rect* clip = &mSnapshot->getLocalClip();
2591 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 -08002592
Romain Guy97771732012-02-28 18:17:02 -08002593 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002594
2595 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2596 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002597 if (hasActiveLayer) {
2598 mSnapshot->transform->mapRect(bounds);
2599 dirtyLayerUnchecked(bounds, getRegion());
2600 }
Romain Guy97771732012-02-28 18:17:02 -08002601 }
Chet Haase48659092012-05-31 15:21:51 -07002602
2603 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002604}
2605
Chet Haase48659092012-05-31 15:21:51 -07002606status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2607 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002608
Romain Guya1d3c912011-12-13 14:55:06 -08002609 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002610
Romain Guy33f6beb2012-02-16 19:24:51 -08002611 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002612 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002613 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002614 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002615
Romain Guy8b55f372010-08-18 17:10:07 -07002616 const float x = texture->left - texture->offset;
2617 const float y = texture->top - texture->offset;
2618
Romain Guy01d58e42011-01-19 21:54:02 -08002619 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002620
2621 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002622}
2623
Chet Haase48659092012-05-31 15:21:51 -07002624status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002625 if (!layer) {
2626 return DrawGlInfo::kStatusDone;
2627 }
2628
2629 Rect transformed;
2630 Rect clip;
2631 const bool rejected = quickRejectNoScissor(x, y,
2632 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2633
2634 if (rejected) {
Chet Haase48659092012-05-31 15:21:51 -07002635 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002636 }
2637
Romain Guy4ff0cf42012-08-06 14:51:10 -07002638 bool debugLayerUpdate = false;
Romain Guy2bf68f02012-03-02 13:37:47 -08002639 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2640 OpenGLRenderer* renderer = layer->renderer;
2641 Rect& dirty = layer->dirtyRect;
2642
Romain Guy2b7028e2012-09-19 17:25:38 -07002643 endTiling();
2644
Romain Guy2bf68f02012-03-02 13:37:47 -08002645 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2646 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002647 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002648 renderer->finish();
Romain Guy35643dd2012-09-18 15:40:58 -07002649
2650 resumeAfterLayer();
Romain Guy2b7028e2012-09-19 17:25:38 -07002651 startTiling(mSnapshot);
Romain Guy2bf68f02012-03-02 13:37:47 -08002652
2653 dirty.setEmpty();
2654 layer->deferredUpdateScheduled = false;
2655 layer->renderer = NULL;
2656 layer->displayList = NULL;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002657
2658 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002659 }
2660
Romain Guy35643dd2012-09-18 15:40:58 -07002661 mCaches.setScissorEnabled(!clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002662 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002663
Romain Guy211370f2012-02-01 16:10:55 -08002664 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002665 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002666 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002667 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002668 const float a = layer->getAlpha() / 255.0f;
2669 SkiaColorFilter *oldFilter = mColorFilter;
2670 mColorFilter = layer->getColorFilter();
Romain Guyf219da52011-01-16 12:54:25 -08002671
Romain Guyc88e3572011-01-22 00:32:12 -08002672 setupDraw();
2673 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002674 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002675 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002676 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002677 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002678 setupDrawPureColorUniforms();
2679 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002680 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002681 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002682 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2683 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002684
Romain Guyd21b6e12011-11-30 20:21:23 -08002685 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002686 setupDrawModelViewTranslate(tx, ty,
2687 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002688 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002689 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002690 setupDrawModelViewTranslate(x, y,
2691 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2692 }
Romain Guyc88e3572011-01-22 00:32:12 -08002693 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002694
Romain Guyc88e3572011-01-22 00:32:12 -08002695 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2696 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002697
Romain Guyc88e3572011-01-22 00:32:12 -08002698 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002699
Chet Haased15ebf22012-09-05 11:40:29 -07002700 mColorFilter = oldFilter;
2701
Romain Guy3a3133d2011-02-01 22:59:58 -08002702#if DEBUG_LAYERS_AS_REGIONS
2703 drawRegionRects(layer->region);
2704#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002705 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002706
2707 if (debugLayerUpdate) {
2708 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2709 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2710 }
Romain Guyf219da52011-01-16 12:54:25 -08002711 }
Chet Haase48659092012-05-31 15:21:51 -07002712
2713 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002714}
2715
Romain Guy6926c722010-07-12 20:20:03 -07002716///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002717// Shaders
2718///////////////////////////////////////////////////////////////////////////////
2719
2720void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002721 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002722}
2723
Romain Guy06f96e22010-07-30 19:18:16 -07002724void OpenGLRenderer::setupShader(SkiaShader* shader) {
2725 mShader = shader;
2726 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002727 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002728 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002729}
2730
Romain Guyd27977d2010-07-14 19:18:51 -07002731///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002732// Color filters
2733///////////////////////////////////////////////////////////////////////////////
2734
2735void OpenGLRenderer::resetColorFilter() {
2736 mColorFilter = NULL;
2737}
2738
2739void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2740 mColorFilter = filter;
2741}
2742
2743///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002744// Drop shadow
2745///////////////////////////////////////////////////////////////////////////////
2746
2747void OpenGLRenderer::resetShadow() {
2748 mHasShadow = false;
2749}
2750
2751void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2752 mHasShadow = true;
2753 mShadowRadius = radius;
2754 mShadowDx = dx;
2755 mShadowDy = dy;
2756 mShadowColor = color;
2757}
2758
2759///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002760// Draw filters
2761///////////////////////////////////////////////////////////////////////////////
2762
2763void OpenGLRenderer::resetPaintFilter() {
2764 mHasDrawFilter = false;
2765}
2766
2767void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2768 mHasDrawFilter = true;
2769 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2770 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2771}
2772
2773SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002774 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002775
2776 uint32_t flags = paint->getFlags();
2777
2778 mFilteredPaint = *paint;
2779 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2780
2781 return &mFilteredPaint;
2782}
2783
2784///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002785// Drawing implementation
2786///////////////////////////////////////////////////////////////////////////////
2787
Romain Guy01d58e42011-01-19 21:54:02 -08002788void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2789 float x, float y, SkPaint* paint) {
2790 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2791 return;
2792 }
2793
2794 int alpha;
2795 SkXfermode::Mode mode;
2796 getAlphaAndMode(paint, &alpha, &mode);
2797
2798 setupDraw();
2799 setupDrawWithTexture(true);
2800 setupDrawAlpha8Color(paint->getColor(), alpha);
2801 setupDrawColorFilter();
2802 setupDrawShader();
2803 setupDrawBlending(true, mode);
2804 setupDrawProgram();
2805 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2806 setupDrawTexture(texture->id);
2807 setupDrawPureColorUniforms();
2808 setupDrawColorFilterUniforms();
2809 setupDrawShaderUniforms();
2810 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2811
2812 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2813
2814 finishDrawTexture();
2815}
2816
Romain Guyf607bdc2010-09-10 19:20:06 -07002817// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002818#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2819#define kStdUnderline_Offset (1.0f / 9.0f)
2820#define kStdUnderline_Thickness (1.0f / 18.0f)
2821
2822void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2823 float x, float y, SkPaint* paint) {
2824 // Handle underline and strike-through
2825 uint32_t flags = paint->getFlags();
2826 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002827 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002828 float underlineWidth = length;
2829 // If length is > 0.0f, we already measured the text for the text alignment
2830 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002831 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002832 }
2833
Romain Guy211370f2012-02-01 16:10:55 -08002834 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002835 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002836 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002837
Raph Levien8b4072d2012-07-30 15:50:00 -07002838 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002839 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002840
Romain Guyf6834472011-01-23 13:32:12 -08002841 int linesCount = 0;
2842 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2843 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2844
2845 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002846 float points[pointsCount];
2847 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002848
2849 if (flags & SkPaint::kUnderlineText_Flag) {
2850 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002851 points[currentPoint++] = left;
2852 points[currentPoint++] = top;
2853 points[currentPoint++] = left + underlineWidth;
2854 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002855 }
2856
2857 if (flags & SkPaint::kStrikeThruText_Flag) {
2858 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002859 points[currentPoint++] = left;
2860 points[currentPoint++] = top;
2861 points[currentPoint++] = left + underlineWidth;
2862 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002863 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002864
Romain Guy726aeba2011-06-01 14:52:00 -07002865 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002866
Romain Guy726aeba2011-06-01 14:52:00 -07002867 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002868 }
2869 }
2870}
2871
Romain Guy026c5e162010-06-28 17:12:22 -07002872void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002873 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002874 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002875 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002876 color |= 0x00ffffff;
2877 }
2878
Romain Guy70ca14e2010-12-13 18:24:33 -08002879 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002880 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002881 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002882 setupDrawShader();
2883 setupDrawColorFilter();
2884 setupDrawBlending(mode);
2885 setupDrawProgram();
2886 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2887 setupDrawColorUniforms();
2888 setupDrawShaderUniforms(ignoreTransform);
2889 setupDrawColorFilterUniforms();
2890 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002891
Romain Guyc95c8d62010-09-17 15:31:32 -07002892 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2893}
2894
Romain Guy82ba8142010-07-09 13:25:56 -07002895void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002896 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002897 int alpha;
2898 SkXfermode::Mode mode;
2899 getAlphaAndMode(paint, &alpha, &mode);
2900
Romain Guyd21b6e12011-11-30 20:21:23 -08002901 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002902
Romain Guy211370f2012-02-01 16:10:55 -08002903 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002904 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2905 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2906
Romain Guyd21b6e12011-11-30 20:21:23 -08002907 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002908 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2909 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2910 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2911 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002912 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002913 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2914 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2915 GL_TRIANGLE_STRIP, gMeshCount);
2916 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002917}
2918
Romain Guybd6b79b2010-06-26 00:13:53 -07002919void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002920 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2921 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002922 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002923}
2924
2925void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002926 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002927 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002928 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002929
Romain Guy746b7402010-10-26 16:27:31 -07002930 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002931 setupDrawWithTexture();
2932 setupDrawColor(alpha, alpha, alpha, alpha);
2933 setupDrawColorFilter();
2934 setupDrawBlending(blend, mode, swapSrcDst);
2935 setupDrawProgram();
2936 if (!dirty) {
2937 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002938 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002939 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002940 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002941 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002942 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002943 }
Romain Guy86568192010-12-14 15:55:39 -08002944 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002945 setupDrawColorFilterUniforms();
2946 setupDrawTexture(texture);
2947 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002948
Romain Guy6820ac82010-09-15 18:11:50 -07002949 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002950
2951 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002952}
2953
Romain Guya5aed0d2010-09-09 14:42:43 -07002954void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002955 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002956 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002957
Romain Guy82ba8142010-07-09 13:25:56 -07002958 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002959 // These blend modes are not supported by OpenGL directly and have
2960 // to be implemented using shaders. Since the shader will perform
2961 // the blending, turn blending off here
2962 // If the blend mode cannot be implemented using shaders, fall
2963 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07002964 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08002965 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002966 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002967 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002968
Romain Guy82bc7a72012-01-03 14:13:39 -08002969 if (mCaches.blend) {
2970 glDisable(GL_BLEND);
2971 mCaches.blend = false;
2972 }
2973
2974 return;
2975 } else {
2976 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002977 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002978 }
2979
2980 if (!mCaches.blend) {
2981 glEnable(GL_BLEND);
2982 }
2983
2984 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2985 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2986
2987 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2988 glBlendFunc(sourceMode, destMode);
2989 mCaches.lastSrcMode = sourceMode;
2990 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002991 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002992 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002993 glDisable(GL_BLEND);
2994 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002995 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002996}
2997
Romain Guy889f8d12010-07-29 14:37:42 -07002998bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002999 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003000 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003001 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003002 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003003 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003004 }
Romain Guy6926c722010-07-12 20:20:03 -07003005 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003006}
3007
Romain Guy026c5e162010-06-28 17:12:22 -07003008void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003009 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003010 TextureVertex::setUV(v++, u1, v1);
3011 TextureVertex::setUV(v++, u2, v1);
3012 TextureVertex::setUV(v++, u1, v2);
3013 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003014}
3015
Chet Haase5c13d892010-10-08 08:37:55 -07003016void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003017 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003018 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003019}
3020
Romain Guy9d5316e2010-06-24 19:30:36 -07003021}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003022}; // namespace android