blob: da0900abb4e9e19b30e160118f5d52b2784eb1a5 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy03d58522012-02-24 17:54:07 -080024#include <SkPathMeasure.h>
Romain Guy694b5192010-07-21 21:33:20 -070025#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070028#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Romain Guy08aa2cb2011-03-17 11:06:57 -070030#include <private/hwui/DrawGlInfo.h>
31
Romain Guy5b3b3522010-10-27 18:57:51 -070032#include <ui/Rect.h>
33
Romain Guy85bf02f2010-06-22 13:11:24 -070034#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Romain Guya957eea2010-12-08 18:34:42 -080036#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070037
38namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070039namespace uirenderer {
40
41///////////////////////////////////////////////////////////////////////////////
42// Defines
43///////////////////////////////////////////////////////////////////////////////
44
Romain Guy759ea802010-09-16 20:49:46 -070045#define RAD_TO_DEG (180.0f / 3.14159265f)
46#define MIN_ANGLE 0.001f
47
Romain Guyf8773082012-07-12 18:01:00 -070048#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070049
Romain Guyd21b6e12011-11-30 20:21:23 -080050#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
51
Romain Guy9d5316e2010-06-24 19:30:36 -070052///////////////////////////////////////////////////////////////////////////////
53// Globals
54///////////////////////////////////////////////////////////////////////////////
55
Romain Guy889f8d12010-07-29 14:37:42 -070056/**
57 * Structure mapping Skia xfermodes to OpenGL blending factors.
58 */
59struct Blender {
60 SkXfermode::Mode mode;
61 GLenum src;
62 GLenum dst;
63}; // struct Blender
64
Romain Guy026c5e162010-06-28 17:12:22 -070065// In this array, the index of each Blender equals the value of the first
66// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
67static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070068 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
69 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
70 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
71 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
73 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
74 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
75 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
77 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
79 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
81 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
82 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070083};
Romain Guye4d01122010-06-16 18:44:05 -070084
Romain Guy87a76572010-09-13 18:11:21 -070085// This array contains the swapped version of each SkXfermode. For instance
86// this array's SrcOver blending mode is actually DstOver. You can refer to
87// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070088static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070089 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
90 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
91 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
92 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
93 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
94 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
95 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
96 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
99 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
101 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
102 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
103 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700104};
105
Romain Guyf6a11b82010-06-23 17:47:49 -0700106///////////////////////////////////////////////////////////////////////////////
107// Constructors/destructor
108///////////////////////////////////////////////////////////////////////////////
109
Romain Guyfb8b7632010-08-23 21:05:08 -0700110OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700111 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700112 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700113 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800114 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700115
Romain Guyac670c02010-07-27 17:39:27 -0700116 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
117
Romain Guyae5575b2010-07-29 18:48:04 -0700118 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700119}
120
Romain Guy85bf02f2010-06-22 13:11:24 -0700121OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700122 // The context has already been destroyed at this point, do not call
123 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700124}
125
Romain Guyf6a11b82010-06-23 17:47:49 -0700126///////////////////////////////////////////////////////////////////////////////
Romain Guy13631f32012-01-30 17:41:55 -0800127// Debug
128///////////////////////////////////////////////////////////////////////////////
129
130void OpenGLRenderer::startMark(const char* name) const {
131 mCaches.startMark(0, name);
132}
133
134void OpenGLRenderer::endMark() const {
135 mCaches.endMark();
136}
137
138///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700139// Setup
140///////////////////////////////////////////////////////////////////////////////
141
Romain Guy49c5fc02012-05-15 11:10:01 -0700142bool OpenGLRenderer::isDeferred() {
143 return false;
144}
145
Romain Guy85bf02f2010-06-22 13:11:24 -0700146void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700147 initViewport(width, height);
148
149 glDisable(GL_DITHER);
150 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
151
152 glEnableVertexAttribArray(Program::kBindingPosition);
153}
154
155void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700156 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700157
158 mWidth = width;
159 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700160
161 mFirstSnapshot->height = height;
162 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700163}
164
Chet Haase44b2fe32012-06-06 19:03:58 -0700165int OpenGLRenderer::prepare(bool opaque) {
166 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800167}
168
Chet Haase44b2fe32012-06-06 19:03:58 -0700169int OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800170 mCaches.clearGarbage();
171
Romain Guy8aef54f2010-09-01 15:13:49 -0700172 mSnapshot = new Snapshot(mFirstSnapshot,
173 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800174 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700175 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700176
Romain Guy7d7b5492011-01-24 16:33:45 -0800177 mSnapshot->setClip(left, top, right, bottom);
Romain Guy57b52682012-09-20 17:38:46 -0700178 mDirtyClip = opaque;
Romain Guyddf74372012-05-22 14:07:07 -0700179
Romain 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;
Romain Guy57b52682012-09-20 17:38:46 -0700192 startTiling(mTilingSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700193
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 Guy57b52682012-09-20 17:38:46 -0700216void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700217 Rect* clip = mTilingSnapshot->clipRect;
Romain Guy2b7028e2012-09-19 17:25:38 -0700218 if (s->flags & Snapshot::kFlagIsFboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700219 clip = s->clipRect;
220 }
221
222 mCaches.startTiling(clip->left, s->height - clip->bottom,
223 clip->right - clip->left, clip->bottom - clip->top, opaque);
224}
225
226void OpenGLRenderer::endTiling() {
227 mCaches.endTiling();
228}
229
Romain Guyb025b9c2010-09-16 14:16:48 -0700230void OpenGLRenderer::finish() {
Romain Guy2b7028e2012-09-19 17:25:38 -0700231 endTiling();
232
Romain Guyb025b9c2010-09-16 14:16:48 -0700233#if DEBUG_OPENGL
234 GLenum status = GL_NO_ERROR;
235 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000236 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800237 switch (status) {
Romain Guya44a63a2012-04-26 14:05:02 -0700238 case GL_INVALID_ENUM:
239 ALOGE(" GL_INVALID_ENUM");
240 break;
241 case GL_INVALID_VALUE:
242 ALOGE(" GL_INVALID_VALUE");
243 break;
244 case GL_INVALID_OPERATION:
245 ALOGE(" GL_INVALID_OPERATION");
246 break;
Romain Guya07105b2011-01-10 21:14:18 -0800247 case GL_OUT_OF_MEMORY:
Romain Guya44a63a2012-04-26 14:05:02 -0700248 ALOGE(" Out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800249 break;
250 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700251 }
252#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800253#if DEBUG_MEMORY_USAGE
254 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800255#else
256 if (mCaches.getDebugLevel() & kDebugMemory) {
257 mCaches.dumpMemoryUsage();
258 }
Romain Guyc15008e2010-11-10 11:59:15 -0800259#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700260}
261
Romain Guy6c319ca2011-01-11 14:29:25 -0800262void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700263 if (mCaches.currentProgram) {
264 if (mCaches.currentProgram->isInUse()) {
265 mCaches.currentProgram->remove();
266 mCaches.currentProgram = NULL;
267 }
268 }
Romain Guy50c0f092010-10-19 11:42:22 -0700269 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800270 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800271 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800272 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700273}
274
Romain Guy6c319ca2011-01-11 14:29:25 -0800275void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800276 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800277 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700278 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
279
Romain Guy3e263fa2011-12-12 16:47:48 -0800280 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
281
Chet Haase80250612012-08-15 13:46:54 -0700282 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700283 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800284 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700285 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700286
Romain Guya1d3c912011-12-13 14:55:06 -0800287 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700288
Romain Guy50c0f092010-10-19 11:42:22 -0700289 mCaches.blend = true;
290 glEnable(GL_BLEND);
291 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
292 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700293}
294
Romain Guy35643dd2012-09-18 15:40:58 -0700295void OpenGLRenderer::resumeAfterLayer() {
296 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
297 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
298 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
299
300 mCaches.resetScissor();
301 dirtyClip();
302}
303
Romain Guyba6be8a2012-04-23 18:22:09 -0700304void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700305 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700306}
307
308void OpenGLRenderer::attachFunctor(Functor* functor) {
309 mFunctors.add(functor);
310}
311
Romain Guy8f3b8e32012-03-27 16:33:45 -0700312status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
313 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700314 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700315
Romain Guyba6be8a2012-04-23 18:22:09 -0700316 if (count > 0) {
317 SortedVector<Functor*> functors(mFunctors);
318 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700319
Romain Guyba6be8a2012-04-23 18:22:09 -0700320 DrawGlInfo info;
321 info.clipLeft = 0;
322 info.clipTop = 0;
323 info.clipRight = 0;
324 info.clipBottom = 0;
325 info.isLayer = false;
326 info.width = 0;
327 info.height = 0;
328 memset(info.transform, 0, sizeof(float) * 16);
329
330 for (size_t i = 0; i < count; i++) {
331 Functor* f = functors.itemAt(i);
332 result |= (*f)(DrawGlInfo::kModeProcess, &info);
333
Chris Craikc2c95432012-04-25 15:13:52 -0700334 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700335 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
336 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700337 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700338
Chris Craikc2c95432012-04-25 15:13:52 -0700339 if (result & DrawGlInfo::kStatusInvoke) {
340 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700341 }
342 }
Chet Haasebeb8bd02012-09-09 16:13:26 -0700343 // protect against functors binding to other buffers
344 mCaches.unbindMeshBuffer();
345 mCaches.unbindIndicesBuffer();
346 mCaches.activeTexture(0);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700347 }
348
349 return result;
350}
351
352status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800353 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700354 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700355
Romain Guy8a4ac612012-07-17 17:32:48 -0700356 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800357 if (mDirtyClip) {
358 setScissorFromClip();
359 }
Romain Guyd643bb52011-03-01 14:55:21 -0800360
Romain Guy80911b82011-03-16 15:30:12 -0700361 Rect clip(*mSnapshot->clipRect);
362 clip.snapToPixelBoundaries();
363
Romain Guyd643bb52011-03-01 14:55:21 -0800364 // Since we don't know what the functor will draw, let's dirty
365 // tne entire clip region
366 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800367 dirtyLayerUnchecked(clip, getRegion());
368 }
Romain Guyd643bb52011-03-01 14:55:21 -0800369
Romain Guy08aa2cb2011-03-17 11:06:57 -0700370 DrawGlInfo info;
371 info.clipLeft = clip.left;
372 info.clipTop = clip.top;
373 info.clipRight = clip.right;
374 info.clipBottom = clip.bottom;
375 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700376 info.width = getSnapshot()->viewport.getWidth();
377 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700378 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700379
Chet Haase48659092012-05-31 15:21:51 -0700380 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800381
Romain Guy8f3b8e32012-03-27 16:33:45 -0700382 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700383 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800384 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700385
Chris Craik65924a32012-04-05 17:52:11 -0700386 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700387 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700388 }
Romain Guycabfcc12011-03-07 18:06:46 -0800389 }
390
Chet Haasedaf98e92011-01-10 14:10:36 -0800391 resume();
Romain Guy65549432012-03-26 16:45:05 -0700392 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800393}
394
Romain Guyf6a11b82010-06-23 17:47:49 -0700395///////////////////////////////////////////////////////////////////////////////
396// State management
397///////////////////////////////////////////////////////////////////////////////
398
Romain Guybb9524b2010-06-22 18:56:38 -0700399int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700400 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700401}
402
403int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700404 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700405}
406
407void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700408 if (mSaveCount > 1) {
409 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700410 }
Romain Guybb9524b2010-06-22 18:56:38 -0700411}
412
413void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700414 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700415
Romain Guy8fb95422010-08-17 18:38:51 -0700416 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700417 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700418 }
Romain Guybb9524b2010-06-22 18:56:38 -0700419}
420
Romain Guy8aef54f2010-09-01 15:13:49 -0700421int OpenGLRenderer::saveSnapshot(int flags) {
422 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700423 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700424}
425
426bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700427 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700428 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700429 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700430
Romain Guybd6b79b2010-06-26 00:13:53 -0700431 sp<Snapshot> current = mSnapshot;
432 sp<Snapshot> previous = mSnapshot->previous;
433
Romain Guyeb993562010-10-05 18:14:38 -0700434 if (restoreOrtho) {
435 Rect& r = previous->viewport;
436 glViewport(r.left, r.top, r.right, r.bottom);
437 mOrthoMatrix.load(current->orthoMatrix);
438 }
439
Romain Guy8b55f372010-08-18 17:10:07 -0700440 mSaveCount--;
441 mSnapshot = previous;
442
Romain Guy2542d192010-08-18 11:47:12 -0700443 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700444 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700445 }
Romain Guy2542d192010-08-18 11:47:12 -0700446
Romain Guy5ec99242010-11-03 16:19:08 -0700447 if (restoreLayer) {
448 composeLayer(current, previous);
449 }
450
Romain Guy2542d192010-08-18 11:47:12 -0700451 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700452}
453
Romain Guyf6a11b82010-06-23 17:47:49 -0700454///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700455// Layers
456///////////////////////////////////////////////////////////////////////////////
457
458int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700459 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700460 const GLuint previousFbo = mSnapshot->fbo;
461 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700462
Romain Guyaf636eb2010-12-09 17:47:21 -0800463 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700464 int alpha = 255;
465 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700466
Romain Guye45362c2010-11-03 19:58:32 -0700467 if (p) {
468 alpha = p->getAlpha();
469 if (!mCaches.extensions.hasFramebufferFetch()) {
470 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
471 if (!isMode) {
472 // Assume SRC_OVER
473 mode = SkXfermode::kSrcOver_Mode;
474 }
475 } else {
476 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700477 }
478 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700479 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700480 }
Romain Guyd55a8612010-06-28 17:42:46 -0700481
Chet Haased48885a2012-08-28 17:43:28 -0700482 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700483 }
Romain Guyd55a8612010-06-28 17:42:46 -0700484
485 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700486}
487
488int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
489 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700490 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700491 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700492 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700493 SkPaint paint;
494 paint.setAlpha(alpha);
495 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700496 }
Romain Guyd55a8612010-06-28 17:42:46 -0700497}
Romain Guybd6b79b2010-06-26 00:13:53 -0700498
Romain Guy1c740bc2010-09-13 18:00:09 -0700499/**
500 * Layers are viewed by Skia are slightly different than layers in image editing
501 * programs (for instance.) When a layer is created, previously created layers
502 * and the frame buffer still receive every drawing command. For instance, if a
503 * layer is created and a shape intersecting the bounds of the layers and the
504 * framebuffer is draw, the shape will be drawn on both (unless the layer was
505 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
506 *
507 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
508 * texture. Unfortunately, this is inefficient as it requires every primitive to
509 * be drawn n + 1 times, where n is the number of active layers. In practice this
510 * means, for every primitive:
511 * - Switch active frame buffer
512 * - Change viewport, clip and projection matrix
513 * - Issue the drawing
514 *
515 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700516 * To avoid this, layers are implemented in a different way here, at least in the
517 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
518 * is set. When this flag is set we can redirect all drawing operations into a
519 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700520 *
521 * This implementation relies on the frame buffer being at least RGBA 8888. When
522 * a layer is created, only a texture is created, not an FBO. The content of the
523 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700524 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700525 * buffer and drawing continues as normal. This technique therefore treats the
526 * frame buffer as a scratch buffer for the layers.
527 *
528 * To compose the layers back onto the frame buffer, each layer texture
529 * (containing the original frame buffer data) is drawn as a simple quad over
530 * the frame buffer. The trick is that the quad is set as the composition
531 * destination in the blending equation, and the frame buffer becomes the source
532 * of the composition.
533 *
534 * Drawing layers with an alpha value requires an extra step before composition.
535 * An empty quad is drawn over the layer's region in the frame buffer. This quad
536 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
537 * quad is used to multiply the colors in the frame buffer. This is achieved by
538 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
539 * GL_ZERO, GL_SRC_ALPHA.
540 *
541 * Because glCopyTexImage2D() can be slow, an alternative implementation might
542 * be use to draw a single clipped layer. The implementation described above
543 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700544 *
545 * (1) The frame buffer is actually not cleared right away. To allow the GPU
546 * to potentially optimize series of calls to glCopyTexImage2D, the frame
547 * buffer is left untouched until the first drawing operation. Only when
548 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700549 */
Chet Haased48885a2012-08-28 17:43:28 -0700550bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
551 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700552 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700553 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700554
Romain Guyeb993562010-10-05 18:14:38 -0700555 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
556
Romain Guyf607bdc2010-09-10 19:20:06 -0700557 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700558 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700559 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700560 Rect untransformedBounds(bounds);
561 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700562
Chet Haased48885a2012-08-28 17:43:28 -0700563 // Layers only make sense if they are in the framebuffer's bounds
564 if (bounds.intersect(*mSnapshot->clipRect)) {
565 // We cannot work with sub-pixels in this case
566 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700567
Chet Haased48885a2012-08-28 17:43:28 -0700568 // When the layer is not an FBO, we may use glCopyTexImage so we
569 // need to make sure the layer does not extend outside the bounds
570 // of the framebuffer
571 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700572 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700573 } else if (fboLayer) {
574 clip.set(bounds);
575 mat4 inverse;
576 inverse.loadInverse(*mSnapshot->transform);
577 inverse.mapRect(clip);
578 clip.snapToPixelBoundaries();
579 if (clip.intersect(untransformedBounds)) {
580 clip.translate(-left, -top);
581 bounds.set(untransformedBounds);
582 } else {
583 clip.setEmpty();
584 }
Romain Guyad37cd32011-03-15 11:12:25 -0700585 }
Chet Haased48885a2012-08-28 17:43:28 -0700586 } else {
587 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700588 }
Romain Guybf434112010-09-16 14:40:17 -0700589
Romain Guy746b7402010-10-26 16:27:31 -0700590 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700591 bounds.getHeight() > mCaches.maxTextureSize ||
592 (fboLayer && clip.isEmpty())) {
593 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700594 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700595 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700596 }
597
598 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700599 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700600 return false;
601 }
Romain Guyf18fd992010-07-08 11:45:51 -0700602
Romain Guya1d3c912011-12-13 14:55:06 -0800603 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700604 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700605 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700606 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700607 }
608
Romain Guy9ace8f52011-07-07 20:50:11 -0700609 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700610 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700611 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
612 bounds.getWidth() / float(layer->getWidth()), 0.0f);
613 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700614 layer->setBlend(true);
Romain Guydda570202010-07-06 11:39:32 -0700615
Romain Guy8fb95422010-08-17 18:38:51 -0700616 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700617 mSnapshot->flags |= Snapshot::kFlagIsLayer;
618 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700619
Romain Guyeb993562010-10-05 18:14:38 -0700620 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700621 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700622 } else {
623 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700624 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800625 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700626 if (layer->isEmpty()) {
627 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700628 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700629 layer->getWidth(), layer->getHeight(), 0);
630 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800631 } else {
632 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700633 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800634 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700635
Romain Guy54be1cd2011-06-13 19:04:27 -0700636 // Enqueue the buffer coordinates to clear the corresponding region later
637 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700638 }
Romain Guyeb993562010-10-05 18:14:38 -0700639 }
Romain Guyf86ef572010-07-01 11:05:42 -0700640
Romain Guyd55a8612010-06-28 17:42:46 -0700641 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700642}
643
Chet Haased48885a2012-08-28 17:43:28 -0700644bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700645 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700646
Chet Haased48885a2012-08-28 17:43:28 -0700647 mSnapshot->region = &mSnapshot->layer->region;
648 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700649
Chet Haased48885a2012-08-28 17:43:28 -0700650 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
651 mSnapshot->fbo = layer->getFbo();
652 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
653 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
654 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
655 mSnapshot->height = bounds.getHeight();
656 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
657 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700658
Romain Guy2b7028e2012-09-19 17:25:38 -0700659 endTiling();
Romain Guy5b3b3522010-10-27 18:57:51 -0700660 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700661 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
662 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700663
664 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700665 if (layer->isEmpty()) {
666 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
667 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700668 }
669
670 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700671 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700672
Romain Guy2b7028e2012-09-19 17:25:38 -0700673 startTiling(mSnapshot);
Romain Guy5b3b3522010-10-27 18:57:51 -0700674
675 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700676 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800677 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700678 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700679 glClear(GL_COLOR_BUFFER_BIT);
680
681 dirtyClip();
682
683 // Change the ortho projection
684 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
685 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
686
687 return true;
688}
689
Romain Guy1c740bc2010-09-13 18:00:09 -0700690/**
691 * Read the documentation of createLayer() before doing anything in this method.
692 */
Romain Guy1d83e192010-08-17 11:37:00 -0700693void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
694 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000695 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700696 return;
697 }
698
Romain Guy5b3b3522010-10-27 18:57:51 -0700699 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700700
701 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700702 endTiling();
703
Romain Guye0aa84b2012-04-03 19:30:26 -0700704 // Detach the texture from the FBO
705 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guyeb993562010-10-05 18:14:38 -0700706 // Unbind current FBO and restore previous one
707 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy2b7028e2012-09-19 17:25:38 -0700708
709 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700710 }
711
Romain Guy1d83e192010-08-17 11:37:00 -0700712 Layer* layer = current->layer;
713 const Rect& rect = layer->layer;
714
Romain Guy9ace8f52011-07-07 20:50:11 -0700715 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700716 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700717 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700718 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700719 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700720 }
721
Romain Guy03750a02010-10-18 14:06:08 -0700722 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700723
Romain Guya1d3c912011-12-13 14:55:06 -0800724 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700725
Romain Guy5b3b3522010-10-27 18:57:51 -0700726 // When the layer is stored in an FBO, we can save a bit of fillrate by
727 // drawing only the dirty region
728 if (fboLayer) {
729 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700730 if (layer->getColorFilter()) {
731 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800732 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700733 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700734 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800735 resetColorFilter();
736 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700737 } else if (!rect.isEmpty()) {
738 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
739 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700740 }
Romain Guy8b55f372010-08-18 17:10:07 -0700741
Romain Guyeb993562010-10-05 18:14:38 -0700742 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800743 // Note: No need to use glDiscardFramebufferEXT() since we never
744 // create/compose layers that are not on screen with this
745 // code path
746 // See LayerRenderer::destroyLayer(Layer*)
747
Romain Guyeb993562010-10-05 18:14:38 -0700748 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
749 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700750 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700751 }
752
Romain Guy746b7402010-10-26 16:27:31 -0700753 dirtyClip();
754
Romain Guyeb993562010-10-05 18:14:38 -0700755 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700756 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700757 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700758 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700759 }
760}
761
Romain Guyaa6c24c2011-04-28 18:40:04 -0700762void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700763 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700764
Romain Guy302a9df2011-08-16 13:55:02 -0700765 mat4& transform = layer->getTransform();
766 if (!transform.isIdentity()) {
767 save(0);
768 mSnapshot->transform->multiply(transform);
769 }
770
Romain Guyaa6c24c2011-04-28 18:40:04 -0700771 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700772 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700773 setupDrawWithTexture();
774 } else {
775 setupDrawWithExternalTexture();
776 }
777 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700778 setupDrawColor(alpha, alpha, alpha, alpha);
779 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700780 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700781 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700782 setupDrawPureColorUniforms();
783 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700784 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
785 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700786 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700787 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700788 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700789 if (mSnapshot->transform->isPureTranslate() &&
790 layer->getWidth() == (uint32_t) rect.getWidth() &&
791 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700792 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
793 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
794
Romain Guyd21b6e12011-11-30 20:21:23 -0800795 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700796 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
797 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800798 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700799 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
800 }
801 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700802 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
803
804 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
805
806 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700807
808 if (!transform.isIdentity()) {
809 restore();
810 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700811}
812
Romain Guy5b3b3522010-10-27 18:57:51 -0700813void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700814 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700815 const Rect& texCoords = layer->texCoords;
816 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
817 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700818
Romain Guy9ace8f52011-07-07 20:50:11 -0700819 float x = rect.left;
820 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700821 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700822 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700823 layer->getHeight() == (uint32_t) rect.getHeight();
824
825 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700826 // When we're swapping, the layer is already in screen coordinates
827 if (!swap) {
828 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
829 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
830 }
831
Romain Guyd21b6e12011-11-30 20:21:23 -0800832 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700833 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800834 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700835 }
836
837 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
838 layer->getTexture(), layer->getAlpha() / 255.0f,
839 layer->getMode(), layer->isBlend(),
840 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
841 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700842
Romain Guyaa6c24c2011-04-28 18:40:04 -0700843 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
844 } else {
845 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
846 drawTextureLayer(layer, rect);
847 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
848 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700849}
850
851void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700852 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700853 layer->setRegionAsRect();
854
Romain Guy40667672011-03-18 14:34:03 -0700855 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700856
Romain Guy5b3b3522010-10-27 18:57:51 -0700857 layer->region.clear();
858 return;
859 }
860
Romain Guy8a3957d2011-09-07 17:55:15 -0700861 // TODO: See LayerRenderer.cpp::generateMesh() for important
862 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800863 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700864 size_t count;
865 const android::Rect* rects = layer->region.getArray(&count);
866
Romain Guy9ace8f52011-07-07 20:50:11 -0700867 const float alpha = layer->getAlpha() / 255.0f;
868 const float texX = 1.0f / float(layer->getWidth());
869 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800870 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700871
872 TextureVertex* mesh = mCaches.getRegionMesh();
873 GLsizei numQuads = 0;
874
Romain Guy7230a742011-01-10 22:26:16 -0800875 setupDraw();
876 setupDrawWithTexture();
877 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800878 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700879 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800880 setupDrawProgram();
881 setupDrawDirtyRegionsDisabled();
882 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800883 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700884 setupDrawTexture(layer->getTexture());
885 if (mSnapshot->transform->isPureTranslate()) {
886 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
887 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
888
Romain Guyd21b6e12011-11-30 20:21:23 -0800889 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700890 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
891 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800892 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700893 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
894 }
Romain Guy15bc6432011-12-13 13:11:32 -0800895 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700896
897 for (size_t i = 0; i < count; i++) {
898 const android::Rect* r = &rects[i];
899
900 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800901 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700902 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800903 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700904
905 // TODO: Reject quads outside of the clip
906 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
907 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
908 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
909 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
910
911 numQuads++;
912
913 if (numQuads >= REGION_MESH_QUAD_COUNT) {
914 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
915 numQuads = 0;
916 mesh = mCaches.getRegionMesh();
917 }
918 }
919
920 if (numQuads > 0) {
921 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
922 }
923
Romain Guy7230a742011-01-10 22:26:16 -0800924 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700925
926#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800927 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700928#endif
929
930 layer->region.clear();
931 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700932}
933
Romain Guy3a3133d2011-02-01 22:59:58 -0800934void OpenGLRenderer::drawRegionRects(const Region& region) {
935#if DEBUG_LAYERS_AS_REGIONS
936 size_t count;
937 const android::Rect* rects = region.getArray(&count);
938
939 uint32_t colors[] = {
940 0x7fff0000, 0x7f00ff00,
941 0x7f0000ff, 0x7fff00ff,
942 };
943
944 int offset = 0;
945 int32_t top = rects[0].top;
946
947 for (size_t i = 0; i < count; i++) {
948 if (top != rects[i].top) {
949 offset ^= 0x2;
950 top = rects[i].top;
951 }
952
953 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
954 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
955 SkXfermode::kSrcOver_Mode);
956 }
957#endif
958}
959
Romain Guy5b3b3522010-10-27 18:57:51 -0700960void OpenGLRenderer::dirtyLayer(const float left, const float top,
961 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -0800962 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700963 Rect bounds(left, top, right, bottom);
964 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800965 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700966 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700967}
968
969void OpenGLRenderer::dirtyLayer(const float left, const float top,
970 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -0800971 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700972 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800973 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800974 }
Romain Guy1bd1bad2011-01-14 20:07:20 -0800975}
976
977void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -0800978 if (bounds.intersect(*mSnapshot->clipRect)) {
979 bounds.snapToPixelBoundaries();
980 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
981 if (!dirty.isEmpty()) {
982 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700983 }
984 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700985}
986
Romain Guy54be1cd2011-06-13 19:04:27 -0700987void OpenGLRenderer::clearLayerRegions() {
988 const size_t count = mLayers.size();
989 if (count == 0) return;
990
991 if (!mSnapshot->isIgnored()) {
992 // Doing several glScissor/glClear here can negatively impact
993 // GPUs with a tiler architecture, instead we draw quads with
994 // the Clear blending mode
995
996 // The list contains bounds that have already been clipped
997 // against their initial clip rect, and the current clip
998 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -0700999 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001000
1001 Vertex mesh[count * 6];
1002 Vertex* vertex = mesh;
1003
1004 for (uint32_t i = 0; i < count; i++) {
1005 Rect* bounds = mLayers.itemAt(i);
1006
1007 Vertex::set(vertex++, bounds->left, bounds->bottom);
1008 Vertex::set(vertex++, bounds->left, bounds->top);
1009 Vertex::set(vertex++, bounds->right, bounds->top);
1010 Vertex::set(vertex++, bounds->left, bounds->bottom);
1011 Vertex::set(vertex++, bounds->right, bounds->top);
1012 Vertex::set(vertex++, bounds->right, bounds->bottom);
1013
1014 delete bounds;
1015 }
1016
1017 setupDraw(false);
1018 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1019 setupDrawBlending(true, SkXfermode::kClear_Mode);
1020 setupDrawProgram();
1021 setupDrawPureColorUniforms();
1022 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001023 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001024
Romain Guy54be1cd2011-06-13 19:04:27 -07001025 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001026
1027 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001028 } else {
1029 for (uint32_t i = 0; i < count; i++) {
1030 delete mLayers.itemAt(i);
1031 }
1032 }
1033
1034 mLayers.clear();
1035}
1036
Romain Guybd6b79b2010-06-26 00:13:53 -07001037///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001038// Transforms
1039///////////////////////////////////////////////////////////////////////////////
1040
1041void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001042 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001043}
1044
1045void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001046 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001047}
1048
1049void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001050 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001051}
1052
Romain Guy807daf72011-01-18 11:19:19 -08001053void OpenGLRenderer::skew(float sx, float sy) {
1054 mSnapshot->transform->skew(sx, sy);
1055}
1056
Romain Guyf6a11b82010-06-23 17:47:49 -07001057void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001058 if (matrix) {
1059 mSnapshot->transform->load(*matrix);
1060 } else {
1061 mSnapshot->transform->loadIdentity();
1062 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001063}
1064
1065void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001066 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001067}
1068
1069void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001070 SkMatrix transform;
1071 mSnapshot->transform->copyTo(transform);
1072 transform.preConcat(*matrix);
1073 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001074}
1075
1076///////////////////////////////////////////////////////////////////////////////
1077// Clipping
1078///////////////////////////////////////////////////////////////////////////////
1079
Romain Guybb9524b2010-06-22 18:56:38 -07001080void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001081 Rect clip(*mSnapshot->clipRect);
1082 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001083
Romain Guy8a4ac612012-07-17 17:32:48 -07001084 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1085 clip.getWidth(), clip.getHeight())) {
1086 mDirtyClip = false;
1087 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001088}
1089
1090const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001091 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001092}
1093
Romain Guy8a4ac612012-07-17 17:32:48 -07001094bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1095 if (mSnapshot->isIgnored()) {
1096 return true;
1097 }
1098
1099 Rect r(left, top, right, bottom);
1100 mSnapshot->transform->mapRect(r);
1101 r.snapToPixelBoundaries();
1102
1103 Rect clipRect(*mSnapshot->clipRect);
1104 clipRect.snapToPixelBoundaries();
1105
1106 return !clipRect.intersects(r);
1107}
1108
Romain Guy35643dd2012-09-18 15:40:58 -07001109bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1110 Rect& transformed, Rect& clip) {
1111 if (mSnapshot->isIgnored()) {
1112 return true;
1113 }
1114
1115 transformed.set(left, top, right, bottom);
1116 mSnapshot->transform->mapRect(transformed);
1117 transformed.snapToPixelBoundaries();
1118
1119 clip.set(*mSnapshot->clipRect);
1120 clip.snapToPixelBoundaries();
1121
1122 return !clip.intersects(transformed);
1123}
1124
Romain Guyc7d53492010-06-25 13:41:57 -07001125bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001126 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001127 return true;
1128 }
1129
Romain Guy1d83e192010-08-17 11:37:00 -07001130 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001131 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001132 r.snapToPixelBoundaries();
1133
1134 Rect clipRect(*mSnapshot->clipRect);
1135 clipRect.snapToPixelBoundaries();
1136
Romain Guy586cae32012-07-13 15:28:31 -07001137 bool rejected = !clipRect.intersects(r);
1138 if (!isDeferred() && !rejected) {
1139 mCaches.setScissorEnabled(!clipRect.contains(r));
1140 }
1141
1142 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001143}
1144
Romain Guy079ba2c2010-07-16 14:12:24 -07001145bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1146 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001147 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001148 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001149 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001150 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001151}
1152
Chet Haasea23eed82012-04-12 15:19:04 -07001153Rect* OpenGLRenderer::getClipRect() {
1154 return mSnapshot->clipRect;
1155}
1156
Romain Guyf6a11b82010-06-23 17:47:49 -07001157///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001158// Drawing commands
1159///////////////////////////////////////////////////////////////////////////////
1160
Romain Guy54be1cd2011-06-13 19:04:27 -07001161void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001162 // TODO: It would be best if we could do this before quickReject()
1163 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001164 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001165 if (mDirtyClip) {
1166 setScissorFromClip();
1167 }
1168 mDescription.reset();
1169 mSetShaderColor = false;
1170 mColorSet = false;
1171 mColorA = mColorR = mColorG = mColorB = 0.0f;
1172 mTextureUnit = 0;
1173 mTrackDirtyRegions = true;
1174}
1175
1176void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1177 mDescription.hasTexture = true;
1178 mDescription.hasAlpha8Texture = isAlpha8;
1179}
1180
Romain Guyaa6c24c2011-04-28 18:40:04 -07001181void OpenGLRenderer::setupDrawWithExternalTexture() {
1182 mDescription.hasExternalTexture = true;
1183}
1184
Romain Guy15bc6432011-12-13 13:11:32 -08001185void OpenGLRenderer::setupDrawNoTexture() {
1186 mCaches.disbaleTexCoordsVertexArray();
1187}
1188
Chet Haase5b0200b2011-04-13 17:58:08 -07001189void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001190 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001191}
1192
Chris Craik6ebdc112012-08-31 18:24:33 -07001193void OpenGLRenderer::setupDrawAARect() {
1194 mDescription.isAARect = true;
1195}
1196
Romain Guyed6fcb02011-03-21 13:11:28 -07001197void OpenGLRenderer::setupDrawPoint(float pointSize) {
1198 mDescription.isPoint = true;
1199 mDescription.pointSize = pointSize;
1200}
1201
Romain Guy70ca14e2010-12-13 18:24:33 -08001202void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001203 setupDrawColor(color, (color >> 24) & 0xFF);
1204}
1205
1206void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1207 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001208 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1209 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001210 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001211 mColorR = a * ((color >> 16) & 0xFF);
1212 mColorG = a * ((color >> 8) & 0xFF);
1213 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001214 mColorSet = true;
1215 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1216}
1217
Romain Guy86568192010-12-14 15:55:39 -08001218void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1219 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001220 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1221 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001222 const float a = mColorA / 255.0f;
1223 mColorR = a * ((color >> 16) & 0xFF);
1224 mColorG = a * ((color >> 8) & 0xFF);
1225 mColorB = a * ((color ) & 0xFF);
1226 mColorSet = true;
1227 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1228}
1229
Romain Guy41210632012-07-16 17:04:24 -07001230void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1231 mCaches.fontRenderer->describe(mDescription, paint);
1232}
1233
Romain Guy70ca14e2010-12-13 18:24:33 -08001234void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1235 mColorA = a;
1236 mColorR = r;
1237 mColorG = g;
1238 mColorB = b;
1239 mColorSet = true;
1240 mSetShaderColor = mDescription.setColor(r, g, b, a);
1241}
1242
1243void OpenGLRenderer::setupDrawShader() {
1244 if (mShader) {
1245 mShader->describe(mDescription, mCaches.extensions);
1246 }
1247}
1248
1249void OpenGLRenderer::setupDrawColorFilter() {
1250 if (mColorFilter) {
1251 mColorFilter->describe(mDescription, mCaches.extensions);
1252 }
1253}
1254
Romain Guyf09ef512011-05-27 11:43:46 -07001255void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1256 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1257 mColorA = 1.0f;
1258 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001259 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001260 }
1261}
1262
Romain Guy70ca14e2010-12-13 18:24:33 -08001263void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001264 // When the blending mode is kClear_Mode, we need to use a modulate color
1265 // argb=1,0,0,0
1266 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001267 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1268 mDescription, swapSrcDst);
1269}
1270
1271void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001272 // When the blending mode is kClear_Mode, we need to use a modulate color
1273 // argb=1,0,0,0
1274 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001275 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1276 mDescription, swapSrcDst);
1277}
1278
1279void OpenGLRenderer::setupDrawProgram() {
1280 useProgram(mCaches.programCache.get(mDescription));
1281}
1282
1283void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1284 mTrackDirtyRegions = false;
1285}
1286
1287void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1288 bool ignoreTransform) {
1289 mModelView.loadTranslate(left, top, 0.0f);
1290 if (!ignoreTransform) {
1291 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1292 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1293 } else {
1294 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1295 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1296 }
1297}
1298
Chet Haase8a5cc922011-04-26 07:28:09 -07001299void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1300 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001301}
1302
Romain Guy70ca14e2010-12-13 18:24:33 -08001303void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1304 bool ignoreTransform, bool ignoreModelView) {
1305 if (!ignoreModelView) {
1306 mModelView.loadTranslate(left, top, 0.0f);
1307 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001308 } else {
1309 mModelView.loadIdentity();
1310 }
Romain Guy86568192010-12-14 15:55:39 -08001311 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1312 if (!ignoreTransform) {
1313 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1314 if (mTrackDirtyRegions && dirty) {
1315 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1316 }
1317 } else {
1318 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1319 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1320 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001321}
1322
Romain Guyed6fcb02011-03-21 13:11:28 -07001323void OpenGLRenderer::setupDrawPointUniforms() {
1324 int slot = mCaches.currentProgram->getUniform("pointSize");
1325 glUniform1f(slot, mDescription.pointSize);
1326}
1327
Romain Guy70ca14e2010-12-13 18:24:33 -08001328void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001329 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001330 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1331 }
1332}
1333
Romain Guy86568192010-12-14 15:55:39 -08001334void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001335 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001336 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001337 }
1338}
1339
Romain Guy70ca14e2010-12-13 18:24:33 -08001340void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1341 if (mShader) {
1342 if (ignoreTransform) {
1343 mModelView.loadInverse(*mSnapshot->transform);
1344 }
1345 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1346 }
1347}
1348
Romain Guy8d0d4782010-12-14 20:13:35 -08001349void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1350 if (mShader) {
1351 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1352 }
1353}
1354
Romain Guy70ca14e2010-12-13 18:24:33 -08001355void OpenGLRenderer::setupDrawColorFilterUniforms() {
1356 if (mColorFilter) {
1357 mColorFilter->setupProgram(mCaches.currentProgram);
1358 }
1359}
1360
Romain Guy41210632012-07-16 17:04:24 -07001361void OpenGLRenderer::setupDrawTextGammaUniforms() {
1362 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1363}
1364
Romain Guy70ca14e2010-12-13 18:24:33 -08001365void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001366 bool force = mCaches.bindMeshBuffer();
1367 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001368 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001369}
1370
1371void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1372 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001373 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001374 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001375}
1376
Romain Guyaa6c24c2011-04-28 18:40:04 -07001377void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1378 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001379 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001380 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001381}
1382
Romain Guy8f0095c2011-05-02 17:24:22 -07001383void OpenGLRenderer::setupDrawTextureTransform() {
1384 mDescription.hasTextureTransform = true;
1385}
1386
1387void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001388 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1389 GL_FALSE, &transform.data[0]);
1390}
1391
Romain Guy70ca14e2010-12-13 18:24:33 -08001392void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001393 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001394 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001395 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001396 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001397 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001398 }
Romain Guyd71dd362011-12-12 19:03:35 -08001399
Romain Guyf3a910b42011-12-12 20:35:21 -08001400 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001401 if (mCaches.currentProgram->texCoords >= 0) {
1402 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1403 }
1404
1405 mCaches.unbindIndicesBuffer();
1406}
1407
1408void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1409 bool force = mCaches.unbindMeshBuffer();
1410 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1411 if (mCaches.currentProgram->texCoords >= 0) {
1412 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001413 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001414}
1415
Chet Haase5b0200b2011-04-13 17:58:08 -07001416void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001417 bool force = mCaches.unbindMeshBuffer();
1418 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1419 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001420 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001421}
1422
1423/**
1424 * 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 -07001425 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1426 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1427 * attributes (one per vertex) are values from zero to one that tells the fragment
1428 * shader where the fragment is in relation to the line width/length overall; these values are
1429 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1430 * region of the line.
1431 * Note that we only pass down the width values in this setup function. The length coordinates
1432 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001433 */
Chet Haase99585ad2011-05-02 15:00:16 -07001434void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001435 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001436 bool force = mCaches.unbindMeshBuffer();
1437 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1438 vertices, gAAVertexStride);
1439 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001440 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001441
Romain Guy7b631422012-04-04 11:38:54 -07001442 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001443 glEnableVertexAttribArray(widthSlot);
1444 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001445
Romain Guy7b631422012-04-04 11:38:54 -07001446 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001447 glEnableVertexAttribArray(lengthSlot);
1448 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001449
Chet Haase5b0200b2011-04-13 17:58:08 -07001450 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001451 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001452}
1453
1454void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1455 glDisableVertexAttribArray(widthSlot);
1456 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001457}
1458
Romain Guy70ca14e2010-12-13 18:24:33 -08001459void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001460}
1461
1462///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001463// Drawing
1464///////////////////////////////////////////////////////////////////////////////
1465
Chet Haase1271e2c2012-04-20 09:54:27 -07001466status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001467 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001468
Romain Guy0fe478e2010-11-08 12:08:41 -08001469 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1470 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001471 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001472 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001473 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001474
Romain Guy65549432012-03-26 16:45:05 -07001475 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001476}
1477
Chet Haaseed30fd82011-04-22 16:18:45 -07001478void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1479 if (displayList) {
1480 displayList->output(*this, level);
1481 }
1482}
1483
Romain Guya168d732011-03-18 16:50:13 -07001484void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1485 int alpha;
1486 SkXfermode::Mode mode;
1487 getAlphaAndMode(paint, &alpha, &mode);
1488
Romain Guya168d732011-03-18 16:50:13 -07001489 float x = left;
1490 float y = top;
1491
Romain Guye3c26852011-07-25 16:36:01 -07001492 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001493 bool ignoreTransform = false;
1494 if (mSnapshot->transform->isPureTranslate()) {
1495 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1496 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1497 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001498 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001499 } else {
1500 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001501 }
1502
1503 setupDraw();
1504 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001505 if (paint) {
1506 setupDrawAlpha8Color(paint->getColor(), alpha);
1507 }
Romain Guya168d732011-03-18 16:50:13 -07001508 setupDrawColorFilter();
1509 setupDrawShader();
1510 setupDrawBlending(true, mode);
1511 setupDrawProgram();
1512 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001513
Romain Guya168d732011-03-18 16:50:13 -07001514 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001515 texture->setWrap(GL_CLAMP_TO_EDGE);
1516 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001517
Romain Guya168d732011-03-18 16:50:13 -07001518 setupDrawPureColorUniforms();
1519 setupDrawColorFilterUniforms();
1520 setupDrawShaderUniforms();
1521 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1522
1523 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1524
1525 finishDrawTexture();
1526}
1527
Chet Haase48659092012-05-31 15:21:51 -07001528status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001529 const float right = left + bitmap->width();
1530 const float bottom = top + bitmap->height();
1531
1532 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001533 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001534 }
1535
Romain Guya1d3c912011-12-13 14:55:06 -08001536 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001537 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001538 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001539 const AutoTexture autoCleanup(texture);
1540
Romain Guy211370f2012-02-01 16:10:55 -08001541 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001542 drawAlphaBitmap(texture, left, top, paint);
1543 } else {
1544 drawTextureRect(left, top, right, bottom, texture, paint);
1545 }
Chet Haase48659092012-05-31 15:21:51 -07001546
1547 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001548}
1549
Chet Haase48659092012-05-31 15:21:51 -07001550status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001551 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1552 const mat4 transform(*matrix);
1553 transform.mapRect(r);
1554
Romain Guy6926c722010-07-12 20:20:03 -07001555 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001556 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001557 }
1558
Romain Guya1d3c912011-12-13 14:55:06 -08001559 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001560 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001561 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001562 const AutoTexture autoCleanup(texture);
1563
Romain Guy5b3b3522010-10-27 18:57:51 -07001564 // This could be done in a cheaper way, all we need is pass the matrix
1565 // to the vertex shader. The save/restore is a bit overkill.
1566 save(SkCanvas::kMatrix_SaveFlag);
1567 concatMatrix(matrix);
1568 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1569 restore();
Chet Haase48659092012-05-31 15:21:51 -07001570
1571 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001572}
1573
Chet Haase48659092012-05-31 15:21:51 -07001574status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001575 const float right = left + bitmap->width();
1576 const float bottom = top + bitmap->height();
1577
1578 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001579 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001580 }
1581
1582 mCaches.activeTexture(0);
1583 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1584 const AutoTexture autoCleanup(texture);
1585
1586 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001587
1588 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001589}
1590
Chet Haase48659092012-05-31 15:21:51 -07001591status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001592 float* vertices, int* colors, SkPaint* paint) {
1593 // TODO: Do a quickReject
1594 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001595 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001596 }
1597
Romain Guya1d3c912011-12-13 14:55:06 -08001598 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001599 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001600 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001601 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001602
Romain Guyd21b6e12011-11-30 20:21:23 -08001603 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1604 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001605
1606 int alpha;
1607 SkXfermode::Mode mode;
1608 getAlphaAndMode(paint, &alpha, &mode);
1609
Romain Guy5a7b4662011-01-20 19:09:30 -08001610 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001611
Romain Guyb18d2d02011-02-10 15:52:54 -08001612 float left = FLT_MAX;
1613 float top = FLT_MAX;
1614 float right = FLT_MIN;
1615 float bottom = FLT_MIN;
1616
Romain Guy211370f2012-02-01 16:10:55 -08001617 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001618
Romain Guya566b7c2011-01-23 16:36:11 -08001619 // TODO: Support the colors array
1620 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001621 TextureVertex* vertex = mesh;
1622 for (int32_t y = 0; y < meshHeight; y++) {
1623 for (int32_t x = 0; x < meshWidth; x++) {
1624 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1625
1626 float u1 = float(x) / meshWidth;
1627 float u2 = float(x + 1) / meshWidth;
1628 float v1 = float(y) / meshHeight;
1629 float v2 = float(y + 1) / meshHeight;
1630
1631 int ax = i + (meshWidth + 1) * 2;
1632 int ay = ax + 1;
1633 int bx = i;
1634 int by = bx + 1;
1635 int cx = i + 2;
1636 int cy = cx + 1;
1637 int dx = i + (meshWidth + 1) * 2 + 2;
1638 int dy = dx + 1;
1639
1640 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1641 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1642 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1643
1644 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1645 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1646 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001647
Romain Guyb18d2d02011-02-10 15:52:54 -08001648 if (hasActiveLayer) {
1649 // TODO: This could be optimized to avoid unnecessary ops
1650 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1651 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1652 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1653 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1654 }
Romain Guy5a7b4662011-01-20 19:09:30 -08001655 }
1656 }
1657
Romain Guyb18d2d02011-02-10 15:52:54 -08001658 if (hasActiveLayer) {
1659 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1660 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001661
Romain Guy5a7b4662011-01-20 19:09:30 -08001662 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1663 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001664 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001665
1666 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001667}
1668
Chet Haase48659092012-05-31 15:21:51 -07001669status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001670 float srcLeft, float srcTop, float srcRight, float srcBottom,
1671 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001672 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001673 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001674 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001675 }
1676
Romain Guya1d3c912011-12-13 14:55:06 -08001677 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001678 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001679 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001680 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001681
Romain Guy8ba548f2010-06-30 19:21:21 -07001682 const float width = texture->width;
1683 const float height = texture->height;
1684
Romain Guy68169722011-08-22 17:33:33 -07001685 const float u1 = fmax(0.0f, srcLeft / width);
1686 const float v1 = fmax(0.0f, srcTop / height);
1687 const float u2 = fmin(1.0f, srcRight / width);
1688 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001689
Romain Guy03750a02010-10-18 14:06:08 -07001690 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001691 resetDrawTextureTexCoords(u1, v1, u2, v2);
1692
Romain Guy03750a02010-10-18 14:06:08 -07001693 int alpha;
1694 SkXfermode::Mode mode;
1695 getAlphaAndMode(paint, &alpha, &mode);
1696
Romain Guyd21b6e12011-11-30 20:21:23 -08001697 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1698
Romain Guy211370f2012-02-01 16:10:55 -08001699 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001700 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1701 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1702
Romain Guyb5014982011-07-28 15:39:12 -07001703 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001704 // Enable linear filtering if the source rectangle is scaled
1705 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001706 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001707 }
Romain Guyb5014982011-07-28 15:39:12 -07001708
Romain Guyd21b6e12011-11-30 20:21:23 -08001709 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001710 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1711 texture->id, alpha / 255.0f, mode, texture->blend,
1712 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1713 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1714 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001715 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001716 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1717 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1718 GL_TRIANGLE_STRIP, gMeshCount);
1719 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001720
1721 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001722
1723 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001724}
1725
Chet Haase48659092012-05-31 15:21:51 -07001726status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001727 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001728 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001729 int alpha;
1730 SkXfermode::Mode mode;
1731 getAlphaAndModeDirect(paint, &alpha, &mode);
1732
1733 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1734 left, top, right, bottom, alpha, mode);
1735}
1736
1737status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1738 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1739 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001740 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001741 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001742 }
1743
Romain Guybe6f9dc2012-07-16 12:41:17 -07001744 alpha *= mSnapshot->alpha;
1745
Romain Guya1d3c912011-12-13 14:55:06 -08001746 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001747 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001748 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001749 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001750 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1751 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001752
Romain Guy2728f962010-10-08 18:36:15 -07001753 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001754 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001755
Romain Guy211370f2012-02-01 16:10:55 -08001756 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001757 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001758 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001759 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001760 const float offsetX = left + mSnapshot->transform->getTranslateX();
1761 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001762 const size_t count = mesh->quads.size();
1763 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001764 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001765 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001766 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1767 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1768 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001769 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001770 dirtyLayer(left + bounds.left, top + bounds.top,
1771 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001772 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001773 }
1774 }
1775
Romain Guy211370f2012-02-01 16:10:55 -08001776 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001777 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1778 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1779
1780 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1781 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1782 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1783 true, !mesh->hasEmptyQuads);
1784 } else {
1785 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1786 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1787 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1788 true, !mesh->hasEmptyQuads);
1789 }
Romain Guy054dc182010-10-15 17:55:25 -07001790 }
Chet Haase48659092012-05-31 15:21:51 -07001791
1792 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001793}
1794
Chet Haase99ecdc42011-05-06 12:06:34 -07001795/**
Chet Haase858aa932011-05-12 09:06:00 -07001796 * This function uses a similar approach to that of AA lines in the drawLines() function.
Chris Craik6ebdc112012-08-31 18:24:33 -07001797 * We expand the rectangle by a half pixel in screen space on all sides. However, instead of using
1798 * a fragment shader to compute the translucency of the color from its position, we simply use a
1799 * varying parameter to define how far a given pixel is into the region.
Chet Haase858aa932011-05-12 09:06:00 -07001800 */
1801void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001802 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001803 float inverseScaleX = 1.0f;
1804 float inverseScaleY = 1.0f;
Romain Guy04299382012-07-18 17:15:41 -07001805
Chet Haase858aa932011-05-12 09:06:00 -07001806 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001807 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001808 Matrix4 *mat = mSnapshot->transform;
1809 float m00 = mat->data[Matrix4::kScaleX];
1810 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase858aa932011-05-12 09:06:00 -07001811 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001812 float m11 = mat->data[Matrix4::kScaleY];
Chet Haase858aa932011-05-12 09:06:00 -07001813 float scaleX = sqrt(m00 * m00 + m01 * m01);
1814 float scaleY = sqrt(m10 * m10 + m11 * m11);
1815 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1816 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1817 }
1818
Chet Haase858aa932011-05-12 09:06:00 -07001819 float boundarySizeX = .5 * inverseScaleX;
1820 float boundarySizeY = .5 * inverseScaleY;
1821
Chris Craik6ebdc112012-08-31 18:24:33 -07001822 float innerLeft = left + boundarySizeX;
1823 float innerRight = right - boundarySizeX;
1824 float innerTop = top + boundarySizeY;
1825 float innerBottom = bottom - boundarySizeY;
1826
Chet Haase858aa932011-05-12 09:06:00 -07001827 // Adjust the rect by the AA boundary padding
1828 left -= boundarySizeX;
1829 right += boundarySizeX;
1830 top -= boundarySizeY;
1831 bottom += boundarySizeY;
1832
Chet Haase858aa932011-05-12 09:06:00 -07001833 if (!quickReject(left, top, right, bottom)) {
Romain Guy04299382012-07-18 17:15:41 -07001834 setupDraw();
1835 setupDrawNoTexture();
Chris Craik6ebdc112012-08-31 18:24:33 -07001836 setupDrawAARect();
Romain Guy04299382012-07-18 17:15:41 -07001837 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1838 setupDrawColorFilter();
1839 setupDrawShader();
1840 setupDrawBlending(true, mode);
1841 setupDrawProgram();
1842 setupDrawModelViewIdentity(true);
1843 setupDrawColorUniforms();
1844 setupDrawColorFilterUniforms();
1845 setupDrawShaderIdentityUniforms();
1846
Chris Craik6ebdc112012-08-31 18:24:33 -07001847 AlphaVertex rects[14];
1848 AlphaVertex* aVertices = &rects[0];
1849 void* alphaCoords = ((GLbyte*) aVertices) + gVertexAlphaOffset;
Romain Guy04299382012-07-18 17:15:41 -07001850
Chris Craik6ebdc112012-08-31 18:24:33 -07001851 bool force = mCaches.unbindMeshBuffer();
1852 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1853 aVertices, gAlphaVertexStride);
1854 mCaches.resetTexCoordsVertexPointer();
1855 mCaches.unbindIndicesBuffer();
Romain Guy04299382012-07-18 17:15:41 -07001856
Chris Craik6ebdc112012-08-31 18:24:33 -07001857 int alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
1858 glEnableVertexAttribArray(alphaSlot);
1859 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Romain Guy04299382012-07-18 17:15:41 -07001860
Chris Craik6ebdc112012-08-31 18:24:33 -07001861 // draw left
1862 AlphaVertex::set(aVertices++, left, bottom, 0);
1863 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1864 AlphaVertex::set(aVertices++, left, top, 0);
1865 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001866
Chris Craik6ebdc112012-08-31 18:24:33 -07001867 // draw top
1868 AlphaVertex::set(aVertices++, right, top, 0);
1869 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001870
Chris Craik6ebdc112012-08-31 18:24:33 -07001871 // draw right
1872 AlphaVertex::set(aVertices++, right, bottom, 0);
1873 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1874
1875 // draw bottom
1876 AlphaVertex::set(aVertices++, left, bottom, 0);
1877 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1878
1879 // draw inner rect (repeating last vertex to create degenerate bridge triangles)
1880 // TODO: also consider drawing the inner rect without the blending-forced shader, if
1881 // blending is expensive. Note: can't use drawColorRect() since it doesn't use vertex
1882 // buffers like below, resulting in slightly different transformed coordinates.
1883 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1884 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
1885 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1886 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
1887
Chet Haase858aa932011-05-12 09:06:00 -07001888 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guy7b631422012-04-04 11:38:54 -07001889
Chris Craik6ebdc112012-08-31 18:24:33 -07001890 glDrawArrays(GL_TRIANGLE_STRIP, 0, 14);
Romain Guy04299382012-07-18 17:15:41 -07001891
Chris Craik6ebdc112012-08-31 18:24:33 -07001892 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001893 }
Chet Haase858aa932011-05-12 09:06:00 -07001894}
1895
1896/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001897 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1898 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1899 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1900 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1901 * of the line. Hairlines are more involved because we need to account for transform scaling
1902 * to end up with a one-pixel-wide line in screen space..
1903 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1904 * in combination with values that we calculate and pass down in this method. The basic approach
1905 * is that the quad we create contains both the core line area plus a bounding area in which
1906 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1907 * proportion of the width and the length of a given segment is represented by the boundary
1908 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1909 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1910 * on the inside). This ends up giving the result we want, with pixels that are completely
1911 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1912 * how far into the boundary region they are, which is determined by shader interpolation.
1913 */
Chet Haase48659092012-05-31 15:21:51 -07001914status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1915 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001916
1917 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001918 // We use half the stroke width here because we're going to position the quad
1919 // corner vertices half of the width away from the line endpoints
1920 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001921 // A stroke width of 0 has a special meaning in Skia:
1922 // it draws a line 1 px wide regardless of current transform
1923 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001924
Chet Haase99ecdc42011-05-06 12:06:34 -07001925 float inverseScaleX = 1.0f;
1926 float inverseScaleY = 1.0f;
1927 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001928
Chet Haase8a5cc922011-04-26 07:28:09 -07001929 int alpha;
1930 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001931
Chet Haase8a5cc922011-04-26 07:28:09 -07001932 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001933 int verticesCount = count;
1934 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001935 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001936 verticesCount += (count - 4);
1937 }
1938
1939 if (isHairLine || isAA) {
1940 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1941 // the line on the screen should always be one pixel wide regardless of scale. For
1942 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001943 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001944 Matrix4 *mat = mSnapshot->transform;
1945 float m00 = mat->data[Matrix4::kScaleX];
1946 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07001947 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001948 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07001949
Romain Guy211370f2012-02-01 16:10:55 -08001950 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1951 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001952
Chet Haase99ecdc42011-05-06 12:06:34 -07001953 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1954 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001955
Chet Haase99ecdc42011-05-06 12:06:34 -07001956 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1957 scaled = true;
1958 }
1959 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001960 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001961
1962 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07001963
1964 mCaches.enableScissor();
1965
Chet Haase8a5cc922011-04-26 07:28:09 -07001966 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001967 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001968 if (isAA) {
1969 setupDrawAALine();
1970 }
1971 setupDrawColor(paint->getColor(), alpha);
1972 setupDrawColorFilter();
1973 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001974 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001975 setupDrawProgram();
1976 setupDrawModelViewIdentity(true);
1977 setupDrawColorUniforms();
1978 setupDrawColorFilterUniforms();
1979 setupDrawShaderIdentityUniforms();
1980
1981 if (isHairLine) {
1982 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001983 halfStrokeWidth = isAA? 1 : .5;
1984 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001985 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001986 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001987 }
Romain Guy7b631422012-04-04 11:38:54 -07001988
1989 int widthSlot;
1990 int lengthSlot;
1991
Chet Haase5b0200b2011-04-13 17:58:08 -07001992 Vertex lines[verticesCount];
1993 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001994
Chet Haase99585ad2011-05-02 15:00:16 -07001995 AAVertex wLines[verticesCount];
1996 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001997
Romain Guy211370f2012-02-01 16:10:55 -08001998 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001999 setupDrawVertices(vertices);
2000 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002001 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2002 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002003 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002004 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2005 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002006 // We will need to calculate the actual width proportion on each segment for
2007 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002008 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002009 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2010 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002011 }
2012
Chet Haase99ecdc42011-05-06 12:06:34 -07002013 AAVertex* prevAAVertex = NULL;
2014 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002015
Chet Haase99585ad2011-05-02 15:00:16 -07002016 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002017 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002018
Chet Haase5b0200b2011-04-13 17:58:08 -07002019 for (int i = 0; i < count; i += 4) {
2020 // a = start point, b = end point
2021 vec2 a(points[i], points[i + 1]);
2022 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002023
Chet Haase99585ad2011-05-02 15:00:16 -07002024 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002025 float boundaryLengthProportion = 0;
2026 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002027
Chet Haase5b0200b2011-04-13 17:58:08 -07002028 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002029 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002030 float x = n.x;
2031 n.x = -n.y;
2032 n.y = x;
2033
Chet Haase8a5cc922011-04-26 07:28:09 -07002034 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002035 if (isAA) {
2036 float wideningFactor;
2037 if (fabs(n.x) >= fabs(n.y)) {
2038 wideningFactor = fabs(1.0f / n.x);
2039 } else {
2040 wideningFactor = fabs(1.0f / n.y);
2041 }
2042 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002043 }
Romain Guy7b631422012-04-04 11:38:54 -07002044
Chet Haase99ecdc42011-05-06 12:06:34 -07002045 if (scaled) {
2046 n.x *= inverseScaleX;
2047 n.y *= inverseScaleY;
2048 }
2049 } else if (scaled) {
2050 // Extend n by .5 pixel on each side, post-transform
2051 vec2 extendedN = n.copyNormalized();
2052 extendedN /= 2;
2053 extendedN.x *= inverseScaleX;
2054 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002055
Chet Haase99ecdc42011-05-06 12:06:34 -07002056 float extendedNLength = extendedN.length();
2057 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002058 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002059 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002060 }
Romain Guy7b631422012-04-04 11:38:54 -07002061
Chet Haase99585ad2011-05-02 15:00:16 -07002062 // aa lines expand the endpoint vertices to encompass the AA boundary
2063 if (isAA) {
2064 vec2 abVector = (b - a);
2065 length = abVector.length();
2066 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002067
Chet Haase99ecdc42011-05-06 12:06:34 -07002068 if (scaled) {
2069 abVector.x *= inverseScaleX;
2070 abVector.y *= inverseScaleY;
2071 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002072 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002073 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002074 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002075 }
Romain Guy7b631422012-04-04 11:38:54 -07002076
Chet Haase99ecdc42011-05-06 12:06:34 -07002077 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002078 a -= abVector;
2079 b += abVector;
2080 }
2081
Chet Haase5b0200b2011-04-13 17:58:08 -07002082 // Four corners of the rectangle defining a thick line
2083 vec2 p1 = a - n;
2084 vec2 p2 = a + n;
2085 vec2 p3 = b + n;
2086 vec2 p4 = b - n;
2087
Chet Haase99585ad2011-05-02 15:00:16 -07002088
Chet Haase5b0200b2011-04-13 17:58:08 -07002089 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2090 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2091 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2092 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2093
Romain Guy04299382012-07-18 17:15:41 -07002094 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002095 if (!isAA) {
2096 if (prevVertex != NULL) {
2097 // Issue two repeat vertices to create degenerate triangles to bridge
2098 // between the previous line and the new one. This is necessary because
2099 // we are creating a single triangle_strip which will contain
2100 // potentially discontinuous line segments.
2101 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2102 Vertex::set(vertices++, p1.x, p1.y);
2103 generatedVerticesCount += 2;
2104 }
Romain Guy7b631422012-04-04 11:38:54 -07002105
Chet Haase5b0200b2011-04-13 17:58:08 -07002106 Vertex::set(vertices++, p1.x, p1.y);
2107 Vertex::set(vertices++, p2.x, p2.y);
2108 Vertex::set(vertices++, p4.x, p4.y);
2109 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002110
Chet Haase5b0200b2011-04-13 17:58:08 -07002111 prevVertex = vertices - 1;
2112 generatedVerticesCount += 4;
2113 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002114 if (!isHairLine && scaled) {
2115 // Must set width proportions per-segment for scaled non-hairlines to use the
2116 // correct AA boundary dimensions
2117 if (boundaryWidthSlot < 0) {
2118 boundaryWidthSlot =
2119 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002120 }
Romain Guy7b631422012-04-04 11:38:54 -07002121
Chet Haase99ecdc42011-05-06 12:06:34 -07002122 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002123 }
Romain Guy7b631422012-04-04 11:38:54 -07002124
Chet Haase99585ad2011-05-02 15:00:16 -07002125 if (boundaryLengthSlot < 0) {
2126 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002127 }
Romain Guy7b631422012-04-04 11:38:54 -07002128
Chet Haase99ecdc42011-05-06 12:06:34 -07002129 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002130
Chet Haase5b0200b2011-04-13 17:58:08 -07002131 if (prevAAVertex != NULL) {
2132 // Issue two repeat vertices to create degenerate triangles to bridge
2133 // between the previous line and the new one. This is necessary because
2134 // we are creating a single triangle_strip which will contain
2135 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002136 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2137 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2138 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002139 generatedVerticesCount += 2;
2140 }
Romain Guy7b631422012-04-04 11:38:54 -07002141
Chet Haase99585ad2011-05-02 15:00:16 -07002142 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2143 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2144 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2145 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002146
Chet Haase5b0200b2011-04-13 17:58:08 -07002147 prevAAVertex = aaVertices - 1;
2148 generatedVerticesCount += 4;
2149 }
Romain Guy7b631422012-04-04 11:38:54 -07002150
Chet Haase99585ad2011-05-02 15:00:16 -07002151 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2152 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2153 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002154 }
2155 }
Romain Guy7b631422012-04-04 11:38:54 -07002156
Chet Haase5b0200b2011-04-13 17:58:08 -07002157 if (generatedVerticesCount > 0) {
2158 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2159 }
Romain Guy7b631422012-04-04 11:38:54 -07002160
2161 if (isAA) {
2162 finishDrawAALine(widthSlot, lengthSlot);
2163 }
Chet Haase48659092012-05-31 15:21:51 -07002164
2165 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002166}
2167
Chet Haase48659092012-05-31 15:21:51 -07002168status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2169 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002170
2171 // TODO: The paint's cap style defines whether the points are square or circular
2172 // TODO: Handle AA for round points
2173
Chet Haase5b0200b2011-04-13 17:58:08 -07002174 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002175 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002176 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002177 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002178 if (isHairLine) {
2179 // Now that we know it's hairline, we can set the effective width, to be used later
2180 strokeWidth = 1.0f;
2181 }
2182 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002183 int alpha;
2184 SkXfermode::Mode mode;
2185 getAlphaAndMode(paint, &alpha, &mode);
2186
2187 int verticesCount = count >> 1;
2188 int generatedVerticesCount = 0;
2189
2190 TextureVertex pointsData[verticesCount];
2191 TextureVertex* vertex = &pointsData[0];
2192
Romain Guy04299382012-07-18 17:15:41 -07002193 // TODO: We should optimize this method to not generate vertices for points
2194 // that lie outside of the clip.
2195 mCaches.enableScissor();
2196
Romain Guyed6fcb02011-03-21 13:11:28 -07002197 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002198 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002199 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002200 setupDrawColor(paint->getColor(), alpha);
2201 setupDrawColorFilter();
2202 setupDrawShader();
2203 setupDrawBlending(mode);
2204 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002205 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002206 setupDrawColorUniforms();
2207 setupDrawColorFilterUniforms();
2208 setupDrawPointUniforms();
2209 setupDrawShaderIdentityUniforms();
2210 setupDrawMesh(vertex);
2211
2212 for (int i = 0; i < count; i += 2) {
2213 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2214 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002215
Chet Haase8a5cc922011-04-26 07:28:09 -07002216 float left = points[i] - halfWidth;
2217 float right = points[i] + halfWidth;
2218 float top = points[i + 1] - halfWidth;
2219 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002220
Chet Haase8a5cc922011-04-26 07:28:09 -07002221 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002222 }
2223
2224 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002225
2226 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002227}
2228
Chet Haase48659092012-05-31 15:21:51 -07002229status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002230 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002231 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002232
Romain Guyae88e5e2010-10-22 17:49:18 -07002233 Rect& clip(*mSnapshot->clipRect);
2234 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002235
Romain Guy3d58c032010-07-14 16:34:53 -07002236 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002237
2238 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002239}
Romain Guy9d5316e2010-06-24 19:30:36 -07002240
Chet Haase48659092012-05-31 15:21:51 -07002241status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2242 SkPaint* paint) {
2243 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002244 const AutoTexture autoCleanup(texture);
2245
2246 const float x = left + texture->left - texture->offset;
2247 const float y = top + texture->top - texture->offset;
2248
2249 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002250
2251 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002252}
2253
Chet Haase48659092012-05-31 15:21:51 -07002254status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002255 float rx, float ry, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002256 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002257
Romain Guya1d3c912011-12-13 14:55:06 -08002258 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002259 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2260 right - left, bottom - top, rx, ry, paint);
Chet Haase48659092012-05-31 15:21:51 -07002261 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002262}
2263
Chet Haase48659092012-05-31 15:21:51 -07002264status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2265 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002266
Romain Guya1d3c912011-12-13 14:55:06 -08002267 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002268 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Chet Haase48659092012-05-31 15:21:51 -07002269 return drawShape(x - radius, y - radius, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002270}
Romain Guy01d58e42011-01-19 21:54:02 -08002271
Chet Haase48659092012-05-31 15:21:51 -07002272status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
2273 SkPaint* paint) {
2274 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002275
Romain Guya1d3c912011-12-13 14:55:06 -08002276 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002277 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002278 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002279}
2280
Chet Haase48659092012-05-31 15:21:51 -07002281status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002282 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002283 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002284
2285 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002286 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002287 }
2288
Romain Guya1d3c912011-12-13 14:55:06 -08002289 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002290 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2291 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002292 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002293}
2294
Chet Haase48659092012-05-31 15:21:51 -07002295status_t OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002296 SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002297 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002298
Romain Guya1d3c912011-12-13 14:55:06 -08002299 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002300 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002301 return drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002302}
2303
Chet Haase48659092012-05-31 15:21:51 -07002304status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002305 if (p->getStyle() != SkPaint::kFill_Style) {
Chet Haase48659092012-05-31 15:21:51 -07002306 return drawRectAsShape(left, top, right, bottom, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002307 }
2308
Romain Guy6926c722010-07-12 20:20:03 -07002309 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002310 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002311 }
2312
Romain Guy026c5e162010-06-28 17:12:22 -07002313 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002314 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002315 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2316 if (!isMode) {
2317 // Assume SRC_OVER
2318 mode = SkXfermode::kSrcOver_Mode;
2319 }
2320 } else {
2321 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002322 }
2323
Romain Guy026c5e162010-06-28 17:12:22 -07002324 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002325 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002326 drawAARect(left, top, right, bottom, color, mode);
2327 } else {
2328 drawColorRect(left, top, right, bottom, color, mode);
2329 }
Chet Haase48659092012-05-31 15:21:51 -07002330
2331 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002332}
Romain Guy9d5316e2010-06-24 19:30:36 -07002333
Raph Levien416a8472012-07-19 22:48:17 -07002334void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2335 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2336 float x, float y) {
2337 mCaches.activeTexture(0);
2338
2339 // NOTE: The drop shadow will not perform gamma correction
2340 // if shader-based correction is enabled
2341 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2342 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2343 paint, text, bytesCount, count, mShadowRadius, positions);
2344 const AutoTexture autoCleanup(shadow);
2345
2346 const float sx = x - shadow->left + mShadowDx;
2347 const float sy = y - shadow->top + mShadowDy;
2348
2349 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2350 int shadowColor = mShadowColor;
2351 if (mShader) {
2352 shadowColor = 0xffffffff;
2353 }
2354
2355 setupDraw();
2356 setupDrawWithTexture(true);
2357 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2358 setupDrawColorFilter();
2359 setupDrawShader();
2360 setupDrawBlending(true, mode);
2361 setupDrawProgram();
2362 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2363 setupDrawTexture(shadow->id);
2364 setupDrawPureColorUniforms();
2365 setupDrawColorFilterUniforms();
2366 setupDrawShaderUniforms();
2367 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2368
2369 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2370}
2371
Chet Haase48659092012-05-31 15:21:51 -07002372status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002373 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002374 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002375 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002376 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002377 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002378
Romain Guy671d6cf2012-01-18 12:39:17 -08002379 // NOTE: Skia does not support perspective transform on drawPosText yet
2380 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002381 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002382 }
2383
2384 float x = 0.0f;
2385 float y = 0.0f;
2386 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2387 if (pureTranslate) {
2388 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2389 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2390 }
2391
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002392 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002393 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2394 paint->getTextSize());
2395
2396 int alpha;
2397 SkXfermode::Mode mode;
2398 getAlphaAndMode(paint, &alpha, &mode);
2399
Raph Levien416a8472012-07-19 22:48:17 -07002400 if (CC_UNLIKELY(mHasShadow)) {
2401 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2402 0.0f, 0.0f);
2403 }
2404
Romain Guy671d6cf2012-01-18 12:39:17 -08002405 // Pick the appropriate texture filtering
2406 bool linearFilter = mSnapshot->transform->changesBounds();
2407 if (pureTranslate && !linearFilter) {
2408 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2409 }
2410
2411 mCaches.activeTexture(0);
2412 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002413 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002414 setupDrawDirtyRegionsDisabled();
2415 setupDrawWithTexture(true);
2416 setupDrawAlpha8Color(paint->getColor(), alpha);
2417 setupDrawColorFilter();
2418 setupDrawShader();
2419 setupDrawBlending(true, mode);
2420 setupDrawProgram();
2421 setupDrawModelView(x, y, x, y, pureTranslate, true);
2422 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2423 setupDrawPureColorUniforms();
2424 setupDrawColorFilterUniforms();
2425 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002426 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002427
2428 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2429 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2430
Romain Guy211370f2012-02-01 16:10:55 -08002431 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002432
2433 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2434 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002435 if (hasActiveLayer) {
2436 if (!pureTranslate) {
2437 mSnapshot->transform->mapRect(bounds);
2438 }
2439 dirtyLayerUnchecked(bounds, getRegion());
2440 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002441 }
Chet Haase48659092012-05-31 15:21:51 -07002442
2443 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002444}
2445
Romain Guyc2525952012-07-27 16:41:22 -07002446status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002447 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002448 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002449 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002450 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002451 }
2452
Chet Haasea1cff502012-02-21 13:43:44 -08002453 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002454 switch (paint->getTextAlign()) {
2455 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002456 x -= length / 2.0f;
2457 break;
2458 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002459 x -= length;
2460 break;
2461 default:
2462 break;
2463 }
2464
Romain Guycac5fd32011-12-01 20:08:50 -08002465 SkPaint::FontMetrics metrics;
2466 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002467 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002468 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002469 }
2470
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002471 const float oldX = x;
2472 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002473 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002474 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002475 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2476 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2477 }
2478
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002479#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002480 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002481 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002482#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002483
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002484 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002485 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002486 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002487
Romain Guy86568192010-12-14 15:55:39 -08002488 int alpha;
2489 SkXfermode::Mode mode;
2490 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002491
Romain Guy211370f2012-02-01 16:10:55 -08002492 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002493 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2494 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002495 }
2496
Romain Guy6620c6d2010-12-06 18:07:02 -08002497 // Pick the appropriate texture filtering
2498 bool linearFilter = mSnapshot->transform->changesBounds();
2499 if (pureTranslate && !linearFilter) {
2500 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2501 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002502
Romain Guy16c88082012-06-11 16:03:47 -07002503 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002504 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002505 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002506 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002507 setupDrawDirtyRegionsDisabled();
2508 setupDrawWithTexture(true);
2509 setupDrawAlpha8Color(paint->getColor(), alpha);
2510 setupDrawColorFilter();
2511 setupDrawShader();
2512 setupDrawBlending(true, mode);
2513 setupDrawProgram();
2514 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002515 // See comment above; the font renderer must use texture unit 0
2516 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002517 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2518 setupDrawPureColorUniforms();
2519 setupDrawColorFilterUniforms();
2520 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002521 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002522
Romain Guy6620c6d2010-12-06 18:07:02 -08002523 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002524 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2525
Romain Guy211370f2012-02-01 16:10:55 -08002526 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002527
Raph Levien996e57c2012-07-23 15:22:52 -07002528 bool status;
Raph Levien8b4072d2012-07-30 15:50:00 -07002529 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
2530 SkPaint paintCopy(*paint);
2531 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2532 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Raph Levien996e57c2012-07-23 15:22:52 -07002533 positions, hasActiveLayer ? &bounds : NULL);
2534 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002535 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2536 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002537 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002538
2539 if (status && hasActiveLayer) {
2540 if (!pureTranslate) {
2541 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002542 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002543 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002544 }
Romain Guy694b5192010-07-21 21:33:20 -07002545
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002546 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002547
2548 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002549}
2550
Chet Haase48659092012-05-31 15:21:51 -07002551status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002552 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002553 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2554 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002555 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002556 }
2557
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002558 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002559 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2560 paint->getTextSize());
2561
2562 int alpha;
2563 SkXfermode::Mode mode;
2564 getAlphaAndMode(paint, &alpha, &mode);
2565
2566 mCaches.activeTexture(0);
2567 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002568 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002569 setupDrawDirtyRegionsDisabled();
2570 setupDrawWithTexture(true);
2571 setupDrawAlpha8Color(paint->getColor(), alpha);
2572 setupDrawColorFilter();
2573 setupDrawShader();
2574 setupDrawBlending(true, mode);
2575 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002576 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002577 setupDrawTexture(fontRenderer.getTexture(true));
2578 setupDrawPureColorUniforms();
2579 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002580 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002581 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002582
Romain Guy97771732012-02-28 18:17:02 -08002583 const Rect* clip = &mSnapshot->getLocalClip();
2584 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 -08002585
Romain Guy97771732012-02-28 18:17:02 -08002586 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002587
2588 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2589 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002590 if (hasActiveLayer) {
2591 mSnapshot->transform->mapRect(bounds);
2592 dirtyLayerUnchecked(bounds, getRegion());
2593 }
Romain Guy97771732012-02-28 18:17:02 -08002594 }
Chet Haase48659092012-05-31 15:21:51 -07002595
2596 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002597}
2598
Chet Haase48659092012-05-31 15:21:51 -07002599status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2600 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002601
Romain Guya1d3c912011-12-13 14:55:06 -08002602 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002603
Romain Guy33f6beb2012-02-16 19:24:51 -08002604 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002605 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002606 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002607 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002608
Romain Guy8b55f372010-08-18 17:10:07 -07002609 const float x = texture->left - texture->offset;
2610 const float y = texture->top - texture->offset;
2611
Romain Guy01d58e42011-01-19 21:54:02 -08002612 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002613
2614 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002615}
2616
Chet Haase48659092012-05-31 15:21:51 -07002617status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002618 if (!layer) {
2619 return DrawGlInfo::kStatusDone;
2620 }
2621
2622 Rect transformed;
2623 Rect clip;
2624 const bool rejected = quickRejectNoScissor(x, y,
2625 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2626
2627 if (rejected) {
Chet Haase48659092012-05-31 15:21:51 -07002628 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002629 }
2630
Romain Guy4ff0cf42012-08-06 14:51:10 -07002631 bool debugLayerUpdate = false;
Romain Guy2bf68f02012-03-02 13:37:47 -08002632 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2633 OpenGLRenderer* renderer = layer->renderer;
2634 Rect& dirty = layer->dirtyRect;
2635
Romain Guy2b7028e2012-09-19 17:25:38 -07002636 endTiling();
2637
Romain Guy2bf68f02012-03-02 13:37:47 -08002638 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2639 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002640 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002641 renderer->finish();
Romain Guy35643dd2012-09-18 15:40:58 -07002642
2643 resumeAfterLayer();
Romain Guy2b7028e2012-09-19 17:25:38 -07002644 startTiling(mSnapshot);
Romain Guy2bf68f02012-03-02 13:37:47 -08002645
2646 dirty.setEmpty();
2647 layer->deferredUpdateScheduled = false;
2648 layer->renderer = NULL;
2649 layer->displayList = NULL;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002650
2651 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002652 }
2653
Romain Guy35643dd2012-09-18 15:40:58 -07002654 mCaches.setScissorEnabled(!clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002655 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002656
Romain Guy211370f2012-02-01 16:10:55 -08002657 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002658 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002659 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002660 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002661 const float a = layer->getAlpha() / 255.0f;
2662 SkiaColorFilter *oldFilter = mColorFilter;
2663 mColorFilter = layer->getColorFilter();
Romain Guyf219da52011-01-16 12:54:25 -08002664
Romain Guyc88e3572011-01-22 00:32:12 -08002665 setupDraw();
2666 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002667 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002668 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002669 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002670 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002671 setupDrawPureColorUniforms();
2672 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002673 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002674 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002675 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2676 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002677
Romain Guyd21b6e12011-11-30 20:21:23 -08002678 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002679 setupDrawModelViewTranslate(tx, ty,
2680 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002681 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002682 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002683 setupDrawModelViewTranslate(x, y,
2684 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2685 }
Romain Guyc88e3572011-01-22 00:32:12 -08002686 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002687
Romain Guyc88e3572011-01-22 00:32:12 -08002688 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2689 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002690
Romain Guyc88e3572011-01-22 00:32:12 -08002691 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002692
Chet Haased15ebf22012-09-05 11:40:29 -07002693 mColorFilter = oldFilter;
2694
Romain Guy3a3133d2011-02-01 22:59:58 -08002695#if DEBUG_LAYERS_AS_REGIONS
2696 drawRegionRects(layer->region);
2697#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002698 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002699
2700 if (debugLayerUpdate) {
2701 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2702 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2703 }
Romain Guyf219da52011-01-16 12:54:25 -08002704 }
Chet Haase48659092012-05-31 15:21:51 -07002705
2706 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002707}
2708
Romain Guy6926c722010-07-12 20:20:03 -07002709///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002710// Shaders
2711///////////////////////////////////////////////////////////////////////////////
2712
2713void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002714 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002715}
2716
Romain Guy06f96e22010-07-30 19:18:16 -07002717void OpenGLRenderer::setupShader(SkiaShader* shader) {
2718 mShader = shader;
2719 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002720 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002721 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002722}
2723
Romain Guyd27977d2010-07-14 19:18:51 -07002724///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002725// Color filters
2726///////////////////////////////////////////////////////////////////////////////
2727
2728void OpenGLRenderer::resetColorFilter() {
2729 mColorFilter = NULL;
2730}
2731
2732void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2733 mColorFilter = filter;
2734}
2735
2736///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002737// Drop shadow
2738///////////////////////////////////////////////////////////////////////////////
2739
2740void OpenGLRenderer::resetShadow() {
2741 mHasShadow = false;
2742}
2743
2744void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2745 mHasShadow = true;
2746 mShadowRadius = radius;
2747 mShadowDx = dx;
2748 mShadowDy = dy;
2749 mShadowColor = color;
2750}
2751
2752///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002753// Draw filters
2754///////////////////////////////////////////////////////////////////////////////
2755
2756void OpenGLRenderer::resetPaintFilter() {
2757 mHasDrawFilter = false;
2758}
2759
2760void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2761 mHasDrawFilter = true;
2762 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2763 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2764}
2765
2766SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002767 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002768
2769 uint32_t flags = paint->getFlags();
2770
2771 mFilteredPaint = *paint;
2772 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2773
2774 return &mFilteredPaint;
2775}
2776
2777///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002778// Drawing implementation
2779///////////////////////////////////////////////////////////////////////////////
2780
Romain Guy01d58e42011-01-19 21:54:02 -08002781void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2782 float x, float y, SkPaint* paint) {
2783 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2784 return;
2785 }
2786
2787 int alpha;
2788 SkXfermode::Mode mode;
2789 getAlphaAndMode(paint, &alpha, &mode);
2790
2791 setupDraw();
2792 setupDrawWithTexture(true);
2793 setupDrawAlpha8Color(paint->getColor(), alpha);
2794 setupDrawColorFilter();
2795 setupDrawShader();
2796 setupDrawBlending(true, mode);
2797 setupDrawProgram();
2798 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2799 setupDrawTexture(texture->id);
2800 setupDrawPureColorUniforms();
2801 setupDrawColorFilterUniforms();
2802 setupDrawShaderUniforms();
2803 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2804
2805 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2806
2807 finishDrawTexture();
2808}
2809
Romain Guyf607bdc2010-09-10 19:20:06 -07002810// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002811#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2812#define kStdUnderline_Offset (1.0f / 9.0f)
2813#define kStdUnderline_Thickness (1.0f / 18.0f)
2814
2815void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2816 float x, float y, SkPaint* paint) {
2817 // Handle underline and strike-through
2818 uint32_t flags = paint->getFlags();
2819 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002820 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002821 float underlineWidth = length;
2822 // If length is > 0.0f, we already measured the text for the text alignment
2823 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002824 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002825 }
2826
Romain Guy211370f2012-02-01 16:10:55 -08002827 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002828 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002829 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002830
Raph Levien8b4072d2012-07-30 15:50:00 -07002831 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002832 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002833
Romain Guyf6834472011-01-23 13:32:12 -08002834 int linesCount = 0;
2835 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2836 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2837
2838 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002839 float points[pointsCount];
2840 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002841
2842 if (flags & SkPaint::kUnderlineText_Flag) {
2843 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002844 points[currentPoint++] = left;
2845 points[currentPoint++] = top;
2846 points[currentPoint++] = left + underlineWidth;
2847 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002848 }
2849
2850 if (flags & SkPaint::kStrikeThruText_Flag) {
2851 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002852 points[currentPoint++] = left;
2853 points[currentPoint++] = top;
2854 points[currentPoint++] = left + underlineWidth;
2855 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002856 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002857
Romain Guy726aeba2011-06-01 14:52:00 -07002858 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002859
Romain Guy726aeba2011-06-01 14:52:00 -07002860 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002861 }
2862 }
2863}
2864
Romain Guy026c5e162010-06-28 17:12:22 -07002865void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002866 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002867 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002868 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002869 color |= 0x00ffffff;
2870 }
2871
Romain Guy70ca14e2010-12-13 18:24:33 -08002872 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002873 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002874 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002875 setupDrawShader();
2876 setupDrawColorFilter();
2877 setupDrawBlending(mode);
2878 setupDrawProgram();
2879 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2880 setupDrawColorUniforms();
2881 setupDrawShaderUniforms(ignoreTransform);
2882 setupDrawColorFilterUniforms();
2883 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002884
Romain Guyc95c8d62010-09-17 15:31:32 -07002885 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2886}
2887
Romain Guy82ba8142010-07-09 13:25:56 -07002888void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002889 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002890 int alpha;
2891 SkXfermode::Mode mode;
2892 getAlphaAndMode(paint, &alpha, &mode);
2893
Romain Guyd21b6e12011-11-30 20:21:23 -08002894 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002895
Romain Guy211370f2012-02-01 16:10:55 -08002896 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002897 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2898 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2899
Romain Guyd21b6e12011-11-30 20:21:23 -08002900 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002901 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2902 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2903 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2904 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002905 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002906 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2907 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2908 GL_TRIANGLE_STRIP, gMeshCount);
2909 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002910}
2911
Romain Guybd6b79b2010-06-26 00:13:53 -07002912void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002913 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2914 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002915 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002916}
2917
2918void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002919 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002920 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002921 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002922
Romain Guy746b7402010-10-26 16:27:31 -07002923 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002924 setupDrawWithTexture();
2925 setupDrawColor(alpha, alpha, alpha, alpha);
2926 setupDrawColorFilter();
2927 setupDrawBlending(blend, mode, swapSrcDst);
2928 setupDrawProgram();
2929 if (!dirty) {
2930 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002931 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002932 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002933 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002934 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002935 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002936 }
Romain Guy86568192010-12-14 15:55:39 -08002937 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002938 setupDrawColorFilterUniforms();
2939 setupDrawTexture(texture);
2940 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002941
Romain Guy6820ac82010-09-15 18:11:50 -07002942 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002943
2944 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002945}
2946
Romain Guya5aed0d2010-09-09 14:42:43 -07002947void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002948 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002949 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002950
Romain Guy82ba8142010-07-09 13:25:56 -07002951 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002952 // These blend modes are not supported by OpenGL directly and have
2953 // to be implemented using shaders. Since the shader will perform
2954 // the blending, turn blending off here
2955 // If the blend mode cannot be implemented using shaders, fall
2956 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07002957 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08002958 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002959 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002960 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002961
Romain Guy82bc7a72012-01-03 14:13:39 -08002962 if (mCaches.blend) {
2963 glDisable(GL_BLEND);
2964 mCaches.blend = false;
2965 }
2966
2967 return;
2968 } else {
2969 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002970 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002971 }
2972
2973 if (!mCaches.blend) {
2974 glEnable(GL_BLEND);
2975 }
2976
2977 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2978 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2979
2980 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2981 glBlendFunc(sourceMode, destMode);
2982 mCaches.lastSrcMode = sourceMode;
2983 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002984 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002985 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002986 glDisable(GL_BLEND);
2987 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002988 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002989}
2990
Romain Guy889f8d12010-07-29 14:37:42 -07002991bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002992 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002993 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002994 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002995 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002996 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002997 }
Romain Guy6926c722010-07-12 20:20:03 -07002998 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002999}
3000
Romain Guy026c5e162010-06-28 17:12:22 -07003001void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003002 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003003 TextureVertex::setUV(v++, u1, v1);
3004 TextureVertex::setUV(v++, u2, v1);
3005 TextureVertex::setUV(v++, u1, v2);
3006 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003007}
3008
Chet Haase5c13d892010-10-08 08:37:55 -07003009void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003010 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003011 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003012}
3013
Romain Guy9d5316e2010-06-24 19:30:36 -07003014}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003015}; // namespace android