blob: 11eb7a1a9c2a1020d30f4a754d0016440578699c [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 Guyddf74372012-05-22 14:07:07 -0700178 mDirtyClip = opaque;
179
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 Guy6b7bd242010-10-06 19:49:23 -0700191 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700192 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700193 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700194 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700195 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700196 } else {
197 mCaches.resetScissor();
198 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700199
200 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700201}
202
203void OpenGLRenderer::syncState() {
204 glViewport(0, 0, mWidth, mHeight);
205
206 if (mCaches.blend) {
207 glEnable(GL_BLEND);
208 } else {
209 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700210 }
Romain Guybb9524b2010-06-22 18:56:38 -0700211}
212
Romain Guyb025b9c2010-09-16 14:16:48 -0700213void OpenGLRenderer::finish() {
214#if DEBUG_OPENGL
215 GLenum status = GL_NO_ERROR;
216 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000217 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800218 switch (status) {
Romain Guya44a63a2012-04-26 14:05:02 -0700219 case GL_INVALID_ENUM:
220 ALOGE(" GL_INVALID_ENUM");
221 break;
222 case GL_INVALID_VALUE:
223 ALOGE(" GL_INVALID_VALUE");
224 break;
225 case GL_INVALID_OPERATION:
226 ALOGE(" GL_INVALID_OPERATION");
227 break;
Romain Guya07105b2011-01-10 21:14:18 -0800228 case GL_OUT_OF_MEMORY:
Romain Guya44a63a2012-04-26 14:05:02 -0700229 ALOGE(" Out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800230 break;
231 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700232 }
233#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800234#if DEBUG_MEMORY_USAGE
235 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800236#else
237 if (mCaches.getDebugLevel() & kDebugMemory) {
238 mCaches.dumpMemoryUsage();
239 }
Romain Guyc15008e2010-11-10 11:59:15 -0800240#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700241}
242
Romain Guy6c319ca2011-01-11 14:29:25 -0800243void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700244 if (mCaches.currentProgram) {
245 if (mCaches.currentProgram->isInUse()) {
246 mCaches.currentProgram->remove();
247 mCaches.currentProgram = NULL;
248 }
249 }
Romain Guy50c0f092010-10-19 11:42:22 -0700250 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800251 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800252 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800253 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700254}
255
Romain Guy6c319ca2011-01-11 14:29:25 -0800256void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800257 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800258 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700259 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
260
Romain Guy3e263fa2011-12-12 16:47:48 -0800261 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
262
Chet Haase80250612012-08-15 13:46:54 -0700263 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700264 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800265 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700266 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700267
Romain Guya1d3c912011-12-13 14:55:06 -0800268 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700269
Romain Guy50c0f092010-10-19 11:42:22 -0700270 mCaches.blend = true;
271 glEnable(GL_BLEND);
272 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
273 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700274}
275
Romain Guy35643dd2012-09-18 15:40:58 -0700276void OpenGLRenderer::resumeAfterLayer() {
277 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
278 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
279 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
280
281 mCaches.resetScissor();
282 dirtyClip();
283}
284
Romain Guyba6be8a2012-04-23 18:22:09 -0700285void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700286 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700287}
288
289void OpenGLRenderer::attachFunctor(Functor* functor) {
290 mFunctors.add(functor);
291}
292
Romain Guy8f3b8e32012-03-27 16:33:45 -0700293status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
294 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700295 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700296
Romain Guyba6be8a2012-04-23 18:22:09 -0700297 if (count > 0) {
298 SortedVector<Functor*> functors(mFunctors);
299 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700300
Romain Guyba6be8a2012-04-23 18:22:09 -0700301 DrawGlInfo info;
302 info.clipLeft = 0;
303 info.clipTop = 0;
304 info.clipRight = 0;
305 info.clipBottom = 0;
306 info.isLayer = false;
307 info.width = 0;
308 info.height = 0;
309 memset(info.transform, 0, sizeof(float) * 16);
310
311 for (size_t i = 0; i < count; i++) {
312 Functor* f = functors.itemAt(i);
313 result |= (*f)(DrawGlInfo::kModeProcess, &info);
314
Chris Craikc2c95432012-04-25 15:13:52 -0700315 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700316 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
317 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700318 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700319
Chris Craikc2c95432012-04-25 15:13:52 -0700320 if (result & DrawGlInfo::kStatusInvoke) {
321 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700322 }
323 }
Chet Haasebeb8bd02012-09-09 16:13:26 -0700324 // protect against functors binding to other buffers
325 mCaches.unbindMeshBuffer();
326 mCaches.unbindIndicesBuffer();
327 mCaches.activeTexture(0);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700328 }
329
330 return result;
331}
332
333status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800334 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700335 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700336
Romain Guy8a4ac612012-07-17 17:32:48 -0700337 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800338 if (mDirtyClip) {
339 setScissorFromClip();
340 }
Romain Guyd643bb52011-03-01 14:55:21 -0800341
Romain Guy80911b82011-03-16 15:30:12 -0700342 Rect clip(*mSnapshot->clipRect);
343 clip.snapToPixelBoundaries();
344
Romain Guyd643bb52011-03-01 14:55:21 -0800345 // Since we don't know what the functor will draw, let's dirty
346 // tne entire clip region
347 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800348 dirtyLayerUnchecked(clip, getRegion());
349 }
Romain Guyd643bb52011-03-01 14:55:21 -0800350
Romain Guy08aa2cb2011-03-17 11:06:57 -0700351 DrawGlInfo info;
352 info.clipLeft = clip.left;
353 info.clipTop = clip.top;
354 info.clipRight = clip.right;
355 info.clipBottom = clip.bottom;
356 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700357 info.width = getSnapshot()->viewport.getWidth();
358 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700359 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700360
Chet Haase48659092012-05-31 15:21:51 -0700361 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800362
Romain Guy8f3b8e32012-03-27 16:33:45 -0700363 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700364 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800365 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700366
Chris Craik65924a32012-04-05 17:52:11 -0700367 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700368 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700369 }
Romain Guycabfcc12011-03-07 18:06:46 -0800370 }
371
Chet Haasedaf98e92011-01-10 14:10:36 -0800372 resume();
Romain Guy65549432012-03-26 16:45:05 -0700373 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800374}
375
Romain Guyf6a11b82010-06-23 17:47:49 -0700376///////////////////////////////////////////////////////////////////////////////
377// State management
378///////////////////////////////////////////////////////////////////////////////
379
Romain Guybb9524b2010-06-22 18:56:38 -0700380int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700381 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700382}
383
384int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700385 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700386}
387
388void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700389 if (mSaveCount > 1) {
390 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700391 }
Romain Guybb9524b2010-06-22 18:56:38 -0700392}
393
394void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700395 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700396
Romain Guy8fb95422010-08-17 18:38:51 -0700397 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700398 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700399 }
Romain Guybb9524b2010-06-22 18:56:38 -0700400}
401
Romain Guy8aef54f2010-09-01 15:13:49 -0700402int OpenGLRenderer::saveSnapshot(int flags) {
403 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700404 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700405}
406
407bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700408 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700409 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700410 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700411
Romain Guybd6b79b2010-06-26 00:13:53 -0700412 sp<Snapshot> current = mSnapshot;
413 sp<Snapshot> previous = mSnapshot->previous;
414
Romain Guyeb993562010-10-05 18:14:38 -0700415 if (restoreOrtho) {
416 Rect& r = previous->viewport;
417 glViewport(r.left, r.top, r.right, r.bottom);
418 mOrthoMatrix.load(current->orthoMatrix);
419 }
420
Romain Guy8b55f372010-08-18 17:10:07 -0700421 mSaveCount--;
422 mSnapshot = previous;
423
Romain Guy2542d192010-08-18 11:47:12 -0700424 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700425 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700426 }
Romain Guy2542d192010-08-18 11:47:12 -0700427
Romain Guy5ec99242010-11-03 16:19:08 -0700428 if (restoreLayer) {
429 composeLayer(current, previous);
430 }
431
Romain Guy2542d192010-08-18 11:47:12 -0700432 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700433}
434
Romain Guyf6a11b82010-06-23 17:47:49 -0700435///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700436// Layers
437///////////////////////////////////////////////////////////////////////////////
438
439int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700440 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700441 const GLuint previousFbo = mSnapshot->fbo;
442 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700443
Romain Guyaf636eb2010-12-09 17:47:21 -0800444 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700445 int alpha = 255;
446 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700447
Romain Guye45362c2010-11-03 19:58:32 -0700448 if (p) {
449 alpha = p->getAlpha();
450 if (!mCaches.extensions.hasFramebufferFetch()) {
451 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
452 if (!isMode) {
453 // Assume SRC_OVER
454 mode = SkXfermode::kSrcOver_Mode;
455 }
456 } else {
457 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700458 }
459 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700460 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700461 }
Romain Guyd55a8612010-06-28 17:42:46 -0700462
Chet Haased48885a2012-08-28 17:43:28 -0700463 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700464 }
Romain Guyd55a8612010-06-28 17:42:46 -0700465
466 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700467}
468
469int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
470 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700471 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700472 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700473 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700474 SkPaint paint;
475 paint.setAlpha(alpha);
476 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700477 }
Romain Guyd55a8612010-06-28 17:42:46 -0700478}
Romain Guybd6b79b2010-06-26 00:13:53 -0700479
Romain Guy1c740bc2010-09-13 18:00:09 -0700480/**
481 * Layers are viewed by Skia are slightly different than layers in image editing
482 * programs (for instance.) When a layer is created, previously created layers
483 * and the frame buffer still receive every drawing command. For instance, if a
484 * layer is created and a shape intersecting the bounds of the layers and the
485 * framebuffer is draw, the shape will be drawn on both (unless the layer was
486 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
487 *
488 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
489 * texture. Unfortunately, this is inefficient as it requires every primitive to
490 * be drawn n + 1 times, where n is the number of active layers. In practice this
491 * means, for every primitive:
492 * - Switch active frame buffer
493 * - Change viewport, clip and projection matrix
494 * - Issue the drawing
495 *
496 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700497 * To avoid this, layers are implemented in a different way here, at least in the
498 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
499 * is set. When this flag is set we can redirect all drawing operations into a
500 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700501 *
502 * This implementation relies on the frame buffer being at least RGBA 8888. When
503 * a layer is created, only a texture is created, not an FBO. The content of the
504 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700505 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700506 * buffer and drawing continues as normal. This technique therefore treats the
507 * frame buffer as a scratch buffer for the layers.
508 *
509 * To compose the layers back onto the frame buffer, each layer texture
510 * (containing the original frame buffer data) is drawn as a simple quad over
511 * the frame buffer. The trick is that the quad is set as the composition
512 * destination in the blending equation, and the frame buffer becomes the source
513 * of the composition.
514 *
515 * Drawing layers with an alpha value requires an extra step before composition.
516 * An empty quad is drawn over the layer's region in the frame buffer. This quad
517 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
518 * quad is used to multiply the colors in the frame buffer. This is achieved by
519 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
520 * GL_ZERO, GL_SRC_ALPHA.
521 *
522 * Because glCopyTexImage2D() can be slow, an alternative implementation might
523 * be use to draw a single clipped layer. The implementation described above
524 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700525 *
526 * (1) The frame buffer is actually not cleared right away. To allow the GPU
527 * to potentially optimize series of calls to glCopyTexImage2D, the frame
528 * buffer is left untouched until the first drawing operation. Only when
529 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700530 */
Chet Haased48885a2012-08-28 17:43:28 -0700531bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
532 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700533 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700534 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700535
Romain Guyeb993562010-10-05 18:14:38 -0700536 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
537
Romain Guyf607bdc2010-09-10 19:20:06 -0700538 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700539 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700540 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700541 Rect untransformedBounds(bounds);
542 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700543
Chet Haased48885a2012-08-28 17:43:28 -0700544 // Layers only make sense if they are in the framebuffer's bounds
545 if (bounds.intersect(*mSnapshot->clipRect)) {
546 // We cannot work with sub-pixels in this case
547 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700548
Chet Haased48885a2012-08-28 17:43:28 -0700549 // When the layer is not an FBO, we may use glCopyTexImage so we
550 // need to make sure the layer does not extend outside the bounds
551 // of the framebuffer
552 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700553 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700554 } else if (fboLayer) {
555 clip.set(bounds);
556 mat4 inverse;
557 inverse.loadInverse(*mSnapshot->transform);
558 inverse.mapRect(clip);
559 clip.snapToPixelBoundaries();
560 if (clip.intersect(untransformedBounds)) {
561 clip.translate(-left, -top);
562 bounds.set(untransformedBounds);
563 } else {
564 clip.setEmpty();
565 }
Romain Guyad37cd32011-03-15 11:12:25 -0700566 }
Chet Haased48885a2012-08-28 17:43:28 -0700567 } else {
568 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700569 }
Romain Guybf434112010-09-16 14:40:17 -0700570
Romain Guy746b7402010-10-26 16:27:31 -0700571 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700572 bounds.getHeight() > mCaches.maxTextureSize ||
573 (fboLayer && clip.isEmpty())) {
574 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700575 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700576 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700577 }
578
579 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700580 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700581 return false;
582 }
Romain Guyf18fd992010-07-08 11:45:51 -0700583
Romain Guya1d3c912011-12-13 14:55:06 -0800584 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700585 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700586 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700587 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700588 }
589
Romain Guy9ace8f52011-07-07 20:50:11 -0700590 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700591 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700592 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
593 bounds.getWidth() / float(layer->getWidth()), 0.0f);
594 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700595 layer->setBlend(true);
Romain Guydda57022010-07-06 11:39:32 -0700596
Romain Guy8fb95422010-08-17 18:38:51 -0700597 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700598 mSnapshot->flags |= Snapshot::kFlagIsLayer;
599 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700600
Romain Guyeb993562010-10-05 18:14:38 -0700601 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700602 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700603 } else {
604 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700605 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800606 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700607 if (layer->isEmpty()) {
608 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700609 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700610 layer->getWidth(), layer->getHeight(), 0);
611 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800612 } else {
613 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700614 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800615 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700616
Romain Guy54be1cd2011-06-13 19:04:27 -0700617 // Enqueue the buffer coordinates to clear the corresponding region later
618 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700619 }
Romain Guyeb993562010-10-05 18:14:38 -0700620 }
Romain Guyf86ef572010-07-01 11:05:42 -0700621
Romain Guyd55a8612010-06-28 17:42:46 -0700622 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700623}
624
Chet Haased48885a2012-08-28 17:43:28 -0700625bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700626 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700627
Chet Haased48885a2012-08-28 17:43:28 -0700628 mSnapshot->region = &mSnapshot->layer->region;
629 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700630
Chet Haased48885a2012-08-28 17:43:28 -0700631 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
632 mSnapshot->fbo = layer->getFbo();
633 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
634 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
635 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
636 mSnapshot->height = bounds.getHeight();
637 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
638 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700639
640 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700641 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
642 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700643
644 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700645 if (layer->isEmpty()) {
646 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
647 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700648 }
649
650 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700651 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700652
653#if DEBUG_LAYERS_AS_REGIONS
654 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
655 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000656 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700657
658 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy5b3b3522010-10-27 18:57:51 -0700659
Chet Haase603f6de2012-09-14 15:31:25 -0700660 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy5b3b3522010-10-27 18:57:51 -0700661
662 return false;
663 }
664#endif
665
666 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700667 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800668 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700669 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700670 glClear(GL_COLOR_BUFFER_BIT);
671
672 dirtyClip();
673
674 // Change the ortho projection
675 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
676 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
677
678 return true;
679}
680
Romain Guy1c740bc2010-09-13 18:00:09 -0700681/**
682 * Read the documentation of createLayer() before doing anything in this method.
683 */
Romain Guy1d83e192010-08-17 11:37:00 -0700684void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
685 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000686 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700687 return;
688 }
689
Romain Guy5b3b3522010-10-27 18:57:51 -0700690 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700691
692 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700693 // Detach the texture from the FBO
694 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
695
Romain Guyeb993562010-10-05 18:14:38 -0700696 // Unbind current FBO and restore previous one
697 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
698 }
699
Romain Guy1d83e192010-08-17 11:37:00 -0700700 Layer* layer = current->layer;
701 const Rect& rect = layer->layer;
702
Romain Guy9ace8f52011-07-07 20:50:11 -0700703 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700704 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700705 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700706 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700707 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700708 }
709
Romain Guy03750a02010-10-18 14:06:08 -0700710 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700711
Romain Guya1d3c912011-12-13 14:55:06 -0800712 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700713
Romain Guy5b3b3522010-10-27 18:57:51 -0700714 // When the layer is stored in an FBO, we can save a bit of fillrate by
715 // drawing only the dirty region
716 if (fboLayer) {
717 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700718 if (layer->getColorFilter()) {
719 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800720 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700721 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700722 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800723 resetColorFilter();
724 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700725 } else if (!rect.isEmpty()) {
726 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
727 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700728 }
Romain Guy8b55f372010-08-18 17:10:07 -0700729
Romain Guyeb993562010-10-05 18:14:38 -0700730 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800731 // Note: No need to use glDiscardFramebufferEXT() since we never
732 // create/compose layers that are not on screen with this
733 // code path
734 // See LayerRenderer::destroyLayer(Layer*)
735
Romain Guyeb993562010-10-05 18:14:38 -0700736 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
737 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700738 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700739 }
740
Romain Guy746b7402010-10-26 16:27:31 -0700741 dirtyClip();
742
Romain Guyeb993562010-10-05 18:14:38 -0700743 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700744 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700745 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700746 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700747 }
748}
749
Romain Guyaa6c24c2011-04-28 18:40:04 -0700750void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700751 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700752
Romain Guy302a9df2011-08-16 13:55:02 -0700753 mat4& transform = layer->getTransform();
754 if (!transform.isIdentity()) {
755 save(0);
756 mSnapshot->transform->multiply(transform);
757 }
758
Romain Guyaa6c24c2011-04-28 18:40:04 -0700759 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700760 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700761 setupDrawWithTexture();
762 } else {
763 setupDrawWithExternalTexture();
764 }
765 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700766 setupDrawColor(alpha, alpha, alpha, alpha);
767 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700768 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700769 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700770 setupDrawPureColorUniforms();
771 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700772 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
773 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700774 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700775 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700776 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700777 if (mSnapshot->transform->isPureTranslate() &&
778 layer->getWidth() == (uint32_t) rect.getWidth() &&
779 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700780 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
781 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
782
Romain Guyd21b6e12011-11-30 20:21:23 -0800783 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700784 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
785 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800786 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700787 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
788 }
789 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700790 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
791
792 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
793
794 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700795
796 if (!transform.isIdentity()) {
797 restore();
798 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700799}
800
Romain Guy5b3b3522010-10-27 18:57:51 -0700801void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700802 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700803 const Rect& texCoords = layer->texCoords;
804 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
805 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700806
Romain Guy9ace8f52011-07-07 20:50:11 -0700807 float x = rect.left;
808 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700809 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700810 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700811 layer->getHeight() == (uint32_t) rect.getHeight();
812
813 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700814 // When we're swapping, the layer is already in screen coordinates
815 if (!swap) {
816 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
817 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
818 }
819
Romain Guyd21b6e12011-11-30 20:21:23 -0800820 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700821 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800822 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700823 }
824
825 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
826 layer->getTexture(), layer->getAlpha() / 255.0f,
827 layer->getMode(), layer->isBlend(),
828 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
829 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700830
Romain Guyaa6c24c2011-04-28 18:40:04 -0700831 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
832 } else {
833 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
834 drawTextureLayer(layer, rect);
835 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
836 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700837}
838
839void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700840 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700841 layer->setRegionAsRect();
842
Romain Guy40667672011-03-18 14:34:03 -0700843 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700844
Romain Guy5b3b3522010-10-27 18:57:51 -0700845 layer->region.clear();
846 return;
847 }
848
Romain Guy8a3957d2011-09-07 17:55:15 -0700849 // TODO: See LayerRenderer.cpp::generateMesh() for important
850 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800851 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700852 size_t count;
853 const android::Rect* rects = layer->region.getArray(&count);
854
Romain Guy9ace8f52011-07-07 20:50:11 -0700855 const float alpha = layer->getAlpha() / 255.0f;
856 const float texX = 1.0f / float(layer->getWidth());
857 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800858 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700859
860 TextureVertex* mesh = mCaches.getRegionMesh();
861 GLsizei numQuads = 0;
862
Romain Guy7230a742011-01-10 22:26:16 -0800863 setupDraw();
864 setupDrawWithTexture();
865 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800866 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700867 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800868 setupDrawProgram();
869 setupDrawDirtyRegionsDisabled();
870 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800871 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700872 setupDrawTexture(layer->getTexture());
873 if (mSnapshot->transform->isPureTranslate()) {
874 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
875 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
876
Romain Guyd21b6e12011-11-30 20:21:23 -0800877 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700878 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
879 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800880 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700881 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
882 }
Romain Guy15bc6432011-12-13 13:11:32 -0800883 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700884
885 for (size_t i = 0; i < count; i++) {
886 const android::Rect* r = &rects[i];
887
888 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800889 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700890 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800891 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700892
893 // TODO: Reject quads outside of the clip
894 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
895 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
896 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
897 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
898
899 numQuads++;
900
901 if (numQuads >= REGION_MESH_QUAD_COUNT) {
902 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
903 numQuads = 0;
904 mesh = mCaches.getRegionMesh();
905 }
906 }
907
908 if (numQuads > 0) {
909 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
910 }
911
Romain Guy7230a742011-01-10 22:26:16 -0800912 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700913
914#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800915 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700916#endif
917
918 layer->region.clear();
919 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700920}
921
Romain Guy3a3133d2011-02-01 22:59:58 -0800922void OpenGLRenderer::drawRegionRects(const Region& region) {
923#if DEBUG_LAYERS_AS_REGIONS
924 size_t count;
925 const android::Rect* rects = region.getArray(&count);
926
927 uint32_t colors[] = {
928 0x7fff0000, 0x7f00ff00,
929 0x7f0000ff, 0x7fff00ff,
930 };
931
932 int offset = 0;
933 int32_t top = rects[0].top;
934
935 for (size_t i = 0; i < count; i++) {
936 if (top != rects[i].top) {
937 offset ^= 0x2;
938 top = rects[i].top;
939 }
940
941 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
942 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
943 SkXfermode::kSrcOver_Mode);
944 }
945#endif
946}
947
Romain Guy5b3b3522010-10-27 18:57:51 -0700948void OpenGLRenderer::dirtyLayer(const float left, const float top,
949 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -0800950 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700951 Rect bounds(left, top, right, bottom);
952 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800953 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700954 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700955}
956
957void OpenGLRenderer::dirtyLayer(const float left, const float top,
958 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -0800959 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700960 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800961 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800962 }
Romain Guy1bd1bad2011-01-14 20:07:20 -0800963}
964
965void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -0800966 if (bounds.intersect(*mSnapshot->clipRect)) {
967 bounds.snapToPixelBoundaries();
968 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
969 if (!dirty.isEmpty()) {
970 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700971 }
972 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700973}
974
Romain Guy54be1cd2011-06-13 19:04:27 -0700975void OpenGLRenderer::clearLayerRegions() {
976 const size_t count = mLayers.size();
977 if (count == 0) return;
978
979 if (!mSnapshot->isIgnored()) {
980 // Doing several glScissor/glClear here can negatively impact
981 // GPUs with a tiler architecture, instead we draw quads with
982 // the Clear blending mode
983
984 // The list contains bounds that have already been clipped
985 // against their initial clip rect, and the current clip
986 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -0700987 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -0700988
989 Vertex mesh[count * 6];
990 Vertex* vertex = mesh;
991
992 for (uint32_t i = 0; i < count; i++) {
993 Rect* bounds = mLayers.itemAt(i);
994
995 Vertex::set(vertex++, bounds->left, bounds->bottom);
996 Vertex::set(vertex++, bounds->left, bounds->top);
997 Vertex::set(vertex++, bounds->right, bounds->top);
998 Vertex::set(vertex++, bounds->left, bounds->bottom);
999 Vertex::set(vertex++, bounds->right, bounds->top);
1000 Vertex::set(vertex++, bounds->right, bounds->bottom);
1001
1002 delete bounds;
1003 }
1004
1005 setupDraw(false);
1006 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1007 setupDrawBlending(true, SkXfermode::kClear_Mode);
1008 setupDrawProgram();
1009 setupDrawPureColorUniforms();
1010 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001011 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001012
Romain Guy54be1cd2011-06-13 19:04:27 -07001013 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001014
1015 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001016 } else {
1017 for (uint32_t i = 0; i < count; i++) {
1018 delete mLayers.itemAt(i);
1019 }
1020 }
1021
1022 mLayers.clear();
1023}
1024
Romain Guybd6b79b2010-06-26 00:13:53 -07001025///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001026// Transforms
1027///////////////////////////////////////////////////////////////////////////////
1028
1029void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001030 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001031}
1032
1033void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001034 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001035}
1036
1037void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001038 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001039}
1040
Romain Guy807daf72011-01-18 11:19:19 -08001041void OpenGLRenderer::skew(float sx, float sy) {
1042 mSnapshot->transform->skew(sx, sy);
1043}
1044
Romain Guyf6a11b82010-06-23 17:47:49 -07001045void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001046 if (matrix) {
1047 mSnapshot->transform->load(*matrix);
1048 } else {
1049 mSnapshot->transform->loadIdentity();
1050 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001051}
1052
1053void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001054 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001055}
1056
1057void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001058 SkMatrix transform;
1059 mSnapshot->transform->copyTo(transform);
1060 transform.preConcat(*matrix);
1061 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001062}
1063
1064///////////////////////////////////////////////////////////////////////////////
1065// Clipping
1066///////////////////////////////////////////////////////////////////////////////
1067
Romain Guybb9524b2010-06-22 18:56:38 -07001068void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001069 Rect clip(*mSnapshot->clipRect);
1070 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001071
Romain Guy8a4ac612012-07-17 17:32:48 -07001072 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1073 clip.getWidth(), clip.getHeight())) {
1074 mDirtyClip = false;
1075 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001076}
1077
1078const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001079 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001080}
1081
Romain Guy8a4ac612012-07-17 17:32:48 -07001082bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1083 if (mSnapshot->isIgnored()) {
1084 return true;
1085 }
1086
1087 Rect r(left, top, right, bottom);
1088 mSnapshot->transform->mapRect(r);
1089 r.snapToPixelBoundaries();
1090
1091 Rect clipRect(*mSnapshot->clipRect);
1092 clipRect.snapToPixelBoundaries();
1093
1094 return !clipRect.intersects(r);
1095}
1096
Romain Guy35643dd2012-09-18 15:40:58 -07001097bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1098 Rect& transformed, Rect& clip) {
1099 if (mSnapshot->isIgnored()) {
1100 return true;
1101 }
1102
1103 transformed.set(left, top, right, bottom);
1104 mSnapshot->transform->mapRect(transformed);
1105 transformed.snapToPixelBoundaries();
1106
1107 clip.set(*mSnapshot->clipRect);
1108 clip.snapToPixelBoundaries();
1109
1110 return !clip.intersects(transformed);
1111}
1112
Romain Guyc7d53492010-06-25 13:41:57 -07001113bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001114 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001115 return true;
1116 }
1117
Romain Guy1d83e192010-08-17 11:37:00 -07001118 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001119 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001120 r.snapToPixelBoundaries();
1121
1122 Rect clipRect(*mSnapshot->clipRect);
1123 clipRect.snapToPixelBoundaries();
1124
Romain Guy586cae32012-07-13 15:28:31 -07001125 bool rejected = !clipRect.intersects(r);
1126 if (!isDeferred() && !rejected) {
1127 mCaches.setScissorEnabled(!clipRect.contains(r));
1128 }
1129
1130 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001131}
1132
Romain Guy079ba2c2010-07-16 14:12:24 -07001133bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1134 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001135 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001136 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001137 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001138 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001139}
1140
Chet Haasea23eed82012-04-12 15:19:04 -07001141Rect* OpenGLRenderer::getClipRect() {
1142 return mSnapshot->clipRect;
1143}
1144
Romain Guyf6a11b82010-06-23 17:47:49 -07001145///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001146// Drawing commands
1147///////////////////////////////////////////////////////////////////////////////
1148
Romain Guy54be1cd2011-06-13 19:04:27 -07001149void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001150 // TODO: It would be best if we could do this before quickReject()
1151 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001152 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001153 if (mDirtyClip) {
1154 setScissorFromClip();
1155 }
1156 mDescription.reset();
1157 mSetShaderColor = false;
1158 mColorSet = false;
1159 mColorA = mColorR = mColorG = mColorB = 0.0f;
1160 mTextureUnit = 0;
1161 mTrackDirtyRegions = true;
1162}
1163
1164void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1165 mDescription.hasTexture = true;
1166 mDescription.hasAlpha8Texture = isAlpha8;
1167}
1168
Romain Guyaa6c24c2011-04-28 18:40:04 -07001169void OpenGLRenderer::setupDrawWithExternalTexture() {
1170 mDescription.hasExternalTexture = true;
1171}
1172
Romain Guy15bc6432011-12-13 13:11:32 -08001173void OpenGLRenderer::setupDrawNoTexture() {
1174 mCaches.disbaleTexCoordsVertexArray();
1175}
1176
Chet Haase5b0200b2011-04-13 17:58:08 -07001177void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001178 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001179}
1180
Chris Craik6ebdc112012-08-31 18:24:33 -07001181void OpenGLRenderer::setupDrawAARect() {
1182 mDescription.isAARect = true;
1183}
1184
Romain Guyed6fcb02011-03-21 13:11:28 -07001185void OpenGLRenderer::setupDrawPoint(float pointSize) {
1186 mDescription.isPoint = true;
1187 mDescription.pointSize = pointSize;
1188}
1189
Romain Guy70ca14e2010-12-13 18:24:33 -08001190void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001191 setupDrawColor(color, (color >> 24) & 0xFF);
1192}
1193
1194void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1195 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001196 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1197 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001198 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001199 mColorR = a * ((color >> 16) & 0xFF);
1200 mColorG = a * ((color >> 8) & 0xFF);
1201 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001202 mColorSet = true;
1203 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1204}
1205
Romain Guy86568192010-12-14 15:55:39 -08001206void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1207 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001208 // Double-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 Guy86568192010-12-14 15:55:39 -08001210 const float a = mColorA / 255.0f;
1211 mColorR = a * ((color >> 16) & 0xFF);
1212 mColorG = a * ((color >> 8) & 0xFF);
1213 mColorB = a * ((color ) & 0xFF);
1214 mColorSet = true;
1215 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1216}
1217
Romain Guy41210632012-07-16 17:04:24 -07001218void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1219 mCaches.fontRenderer->describe(mDescription, paint);
1220}
1221
Romain Guy70ca14e2010-12-13 18:24:33 -08001222void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1223 mColorA = a;
1224 mColorR = r;
1225 mColorG = g;
1226 mColorB = b;
1227 mColorSet = true;
1228 mSetShaderColor = mDescription.setColor(r, g, b, a);
1229}
1230
1231void OpenGLRenderer::setupDrawShader() {
1232 if (mShader) {
1233 mShader->describe(mDescription, mCaches.extensions);
1234 }
1235}
1236
1237void OpenGLRenderer::setupDrawColorFilter() {
1238 if (mColorFilter) {
1239 mColorFilter->describe(mDescription, mCaches.extensions);
1240 }
1241}
1242
Romain Guyf09ef512011-05-27 11:43:46 -07001243void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1244 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1245 mColorA = 1.0f;
1246 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001247 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001248 }
1249}
1250
Romain Guy70ca14e2010-12-13 18:24:33 -08001251void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001252 // When the blending mode is kClear_Mode, we need to use a modulate color
1253 // argb=1,0,0,0
1254 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001255 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1256 mDescription, swapSrcDst);
1257}
1258
1259void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001260 // When the blending mode is kClear_Mode, we need to use a modulate color
1261 // argb=1,0,0,0
1262 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001263 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1264 mDescription, swapSrcDst);
1265}
1266
1267void OpenGLRenderer::setupDrawProgram() {
1268 useProgram(mCaches.programCache.get(mDescription));
1269}
1270
1271void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1272 mTrackDirtyRegions = false;
1273}
1274
1275void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1276 bool ignoreTransform) {
1277 mModelView.loadTranslate(left, top, 0.0f);
1278 if (!ignoreTransform) {
1279 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1280 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1281 } else {
1282 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1283 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1284 }
1285}
1286
Chet Haase8a5cc922011-04-26 07:28:09 -07001287void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1288 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001289}
1290
Romain Guy70ca14e2010-12-13 18:24:33 -08001291void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1292 bool ignoreTransform, bool ignoreModelView) {
1293 if (!ignoreModelView) {
1294 mModelView.loadTranslate(left, top, 0.0f);
1295 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001296 } else {
1297 mModelView.loadIdentity();
1298 }
Romain Guy86568192010-12-14 15:55:39 -08001299 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1300 if (!ignoreTransform) {
1301 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1302 if (mTrackDirtyRegions && dirty) {
1303 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1304 }
1305 } else {
1306 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1307 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1308 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001309}
1310
Romain Guyed6fcb02011-03-21 13:11:28 -07001311void OpenGLRenderer::setupDrawPointUniforms() {
1312 int slot = mCaches.currentProgram->getUniform("pointSize");
1313 glUniform1f(slot, mDescription.pointSize);
1314}
1315
Romain Guy70ca14e2010-12-13 18:24:33 -08001316void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001317 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001318 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1319 }
1320}
1321
Romain Guy86568192010-12-14 15:55:39 -08001322void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001323 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001324 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001325 }
1326}
1327
Romain Guy70ca14e2010-12-13 18:24:33 -08001328void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1329 if (mShader) {
1330 if (ignoreTransform) {
1331 mModelView.loadInverse(*mSnapshot->transform);
1332 }
1333 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1334 }
1335}
1336
Romain Guy8d0d4782010-12-14 20:13:35 -08001337void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1338 if (mShader) {
1339 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1340 }
1341}
1342
Romain Guy70ca14e2010-12-13 18:24:33 -08001343void OpenGLRenderer::setupDrawColorFilterUniforms() {
1344 if (mColorFilter) {
1345 mColorFilter->setupProgram(mCaches.currentProgram);
1346 }
1347}
1348
Romain Guy41210632012-07-16 17:04:24 -07001349void OpenGLRenderer::setupDrawTextGammaUniforms() {
1350 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1351}
1352
Romain Guy70ca14e2010-12-13 18:24:33 -08001353void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001354 bool force = mCaches.bindMeshBuffer();
1355 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001356 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001357}
1358
1359void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1360 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001361 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001362 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001363}
1364
Romain Guyaa6c24c2011-04-28 18:40:04 -07001365void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1366 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001367 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001368 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001369}
1370
Romain Guy8f0095c2011-05-02 17:24:22 -07001371void OpenGLRenderer::setupDrawTextureTransform() {
1372 mDescription.hasTextureTransform = true;
1373}
1374
1375void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001376 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1377 GL_FALSE, &transform.data[0]);
1378}
1379
Romain Guy70ca14e2010-12-13 18:24:33 -08001380void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001381 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001382 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001383 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001384 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001385 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001386 }
Romain Guyd71dd362011-12-12 19:03:35 -08001387
Romain Guyf3a910b42011-12-12 20:35:21 -08001388 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001389 if (mCaches.currentProgram->texCoords >= 0) {
1390 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1391 }
1392
1393 mCaches.unbindIndicesBuffer();
1394}
1395
1396void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1397 bool force = mCaches.unbindMeshBuffer();
1398 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1399 if (mCaches.currentProgram->texCoords >= 0) {
1400 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001401 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001402}
1403
Chet Haase5b0200b2011-04-13 17:58:08 -07001404void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001405 bool force = mCaches.unbindMeshBuffer();
1406 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1407 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001408 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001409}
1410
1411/**
1412 * 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 -07001413 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1414 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1415 * attributes (one per vertex) are values from zero to one that tells the fragment
1416 * shader where the fragment is in relation to the line width/length overall; these values are
1417 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1418 * region of the line.
1419 * Note that we only pass down the width values in this setup function. The length coordinates
1420 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001421 */
Chet Haase99585ad2011-05-02 15:00:16 -07001422void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001423 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001424 bool force = mCaches.unbindMeshBuffer();
1425 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1426 vertices, gAAVertexStride);
1427 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001428 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001429
Romain Guy7b631422012-04-04 11:38:54 -07001430 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001431 glEnableVertexAttribArray(widthSlot);
1432 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001433
Romain Guy7b631422012-04-04 11:38:54 -07001434 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001435 glEnableVertexAttribArray(lengthSlot);
1436 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001437
Chet Haase5b0200b2011-04-13 17:58:08 -07001438 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001439 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001440}
1441
1442void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1443 glDisableVertexAttribArray(widthSlot);
1444 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001445}
1446
Romain Guy70ca14e2010-12-13 18:24:33 -08001447void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001448}
1449
1450///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001451// Drawing
1452///////////////////////////////////////////////////////////////////////////////
1453
Chet Haase1271e2c2012-04-20 09:54:27 -07001454status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001455 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001456
Romain Guy0fe478e2010-11-08 12:08:41 -08001457 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1458 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001459 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001460 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001461 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001462
Romain Guy65549432012-03-26 16:45:05 -07001463 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001464}
1465
Chet Haaseed30fd82011-04-22 16:18:45 -07001466void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1467 if (displayList) {
1468 displayList->output(*this, level);
1469 }
1470}
1471
Romain Guya168d732011-03-18 16:50:13 -07001472void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1473 int alpha;
1474 SkXfermode::Mode mode;
1475 getAlphaAndMode(paint, &alpha, &mode);
1476
Romain Guya168d732011-03-18 16:50:13 -07001477 float x = left;
1478 float y = top;
1479
Romain Guye3c26852011-07-25 16:36:01 -07001480 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001481 bool ignoreTransform = false;
1482 if (mSnapshot->transform->isPureTranslate()) {
1483 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1484 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1485 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001486 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001487 } else {
1488 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001489 }
1490
1491 setupDraw();
1492 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001493 if (paint) {
1494 setupDrawAlpha8Color(paint->getColor(), alpha);
1495 }
Romain Guya168d732011-03-18 16:50:13 -07001496 setupDrawColorFilter();
1497 setupDrawShader();
1498 setupDrawBlending(true, mode);
1499 setupDrawProgram();
1500 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001501
Romain Guya168d732011-03-18 16:50:13 -07001502 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001503 texture->setWrap(GL_CLAMP_TO_EDGE);
1504 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001505
Romain Guya168d732011-03-18 16:50:13 -07001506 setupDrawPureColorUniforms();
1507 setupDrawColorFilterUniforms();
1508 setupDrawShaderUniforms();
1509 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1510
1511 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1512
1513 finishDrawTexture();
1514}
1515
Chet Haase48659092012-05-31 15:21:51 -07001516status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001517 const float right = left + bitmap->width();
1518 const float bottom = top + bitmap->height();
1519
1520 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001521 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001522 }
1523
Romain Guya1d3c912011-12-13 14:55:06 -08001524 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001525 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001526 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001527 const AutoTexture autoCleanup(texture);
1528
Romain Guy211370f2012-02-01 16:10:55 -08001529 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001530 drawAlphaBitmap(texture, left, top, paint);
1531 } else {
1532 drawTextureRect(left, top, right, bottom, texture, paint);
1533 }
Chet Haase48659092012-05-31 15:21:51 -07001534
1535 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001536}
1537
Chet Haase48659092012-05-31 15:21:51 -07001538status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001539 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1540 const mat4 transform(*matrix);
1541 transform.mapRect(r);
1542
Romain Guy6926c722010-07-12 20:20:03 -07001543 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001544 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001545 }
1546
Romain Guya1d3c912011-12-13 14:55:06 -08001547 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001548 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001549 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001550 const AutoTexture autoCleanup(texture);
1551
Romain Guy5b3b3522010-10-27 18:57:51 -07001552 // This could be done in a cheaper way, all we need is pass the matrix
1553 // to the vertex shader. The save/restore is a bit overkill.
1554 save(SkCanvas::kMatrix_SaveFlag);
1555 concatMatrix(matrix);
1556 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1557 restore();
Chet Haase48659092012-05-31 15:21:51 -07001558
1559 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001560}
1561
Chet Haase48659092012-05-31 15:21:51 -07001562status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001563 const float right = left + bitmap->width();
1564 const float bottom = top + bitmap->height();
1565
1566 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001567 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001568 }
1569
1570 mCaches.activeTexture(0);
1571 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1572 const AutoTexture autoCleanup(texture);
1573
1574 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001575
1576 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001577}
1578
Chet Haase48659092012-05-31 15:21:51 -07001579status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001580 float* vertices, int* colors, SkPaint* paint) {
1581 // TODO: Do a quickReject
1582 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001583 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001584 }
1585
Romain Guya1d3c912011-12-13 14:55:06 -08001586 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001587 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001588 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001589 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001590
Romain Guyd21b6e12011-11-30 20:21:23 -08001591 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1592 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001593
1594 int alpha;
1595 SkXfermode::Mode mode;
1596 getAlphaAndMode(paint, &alpha, &mode);
1597
Romain Guy5a7b4662011-01-20 19:09:30 -08001598 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001599
Romain Guyb18d2d02011-02-10 15:52:54 -08001600 float left = FLT_MAX;
1601 float top = FLT_MAX;
1602 float right = FLT_MIN;
1603 float bottom = FLT_MIN;
1604
Romain Guy211370f2012-02-01 16:10:55 -08001605 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001606
Romain Guya566b7c2011-01-23 16:36:11 -08001607 // TODO: Support the colors array
1608 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001609 TextureVertex* vertex = mesh;
1610 for (int32_t y = 0; y < meshHeight; y++) {
1611 for (int32_t x = 0; x < meshWidth; x++) {
1612 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1613
1614 float u1 = float(x) / meshWidth;
1615 float u2 = float(x + 1) / meshWidth;
1616 float v1 = float(y) / meshHeight;
1617 float v2 = float(y + 1) / meshHeight;
1618
1619 int ax = i + (meshWidth + 1) * 2;
1620 int ay = ax + 1;
1621 int bx = i;
1622 int by = bx + 1;
1623 int cx = i + 2;
1624 int cy = cx + 1;
1625 int dx = i + (meshWidth + 1) * 2 + 2;
1626 int dy = dx + 1;
1627
1628 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1629 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1630 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1631
1632 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1633 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1634 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001635
Romain Guyb18d2d02011-02-10 15:52:54 -08001636 if (hasActiveLayer) {
1637 // TODO: This could be optimized to avoid unnecessary ops
1638 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1639 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1640 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1641 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1642 }
Romain Guy5a7b4662011-01-20 19:09:30 -08001643 }
1644 }
1645
Romain Guyb18d2d02011-02-10 15:52:54 -08001646 if (hasActiveLayer) {
1647 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1648 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001649
Romain Guy5a7b4662011-01-20 19:09:30 -08001650 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1651 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001652 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001653
1654 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001655}
1656
Chet Haase48659092012-05-31 15:21:51 -07001657status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001658 float srcLeft, float srcTop, float srcRight, float srcBottom,
1659 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001660 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001661 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001662 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001663 }
1664
Romain Guya1d3c912011-12-13 14:55:06 -08001665 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001666 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001667 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001668 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001669
Romain Guy8ba548f2010-06-30 19:21:21 -07001670 const float width = texture->width;
1671 const float height = texture->height;
1672
Romain Guy68169722011-08-22 17:33:33 -07001673 const float u1 = fmax(0.0f, srcLeft / width);
1674 const float v1 = fmax(0.0f, srcTop / height);
1675 const float u2 = fmin(1.0f, srcRight / width);
1676 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001677
Romain Guy03750a02010-10-18 14:06:08 -07001678 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001679 resetDrawTextureTexCoords(u1, v1, u2, v2);
1680
Romain Guy03750a02010-10-18 14:06:08 -07001681 int alpha;
1682 SkXfermode::Mode mode;
1683 getAlphaAndMode(paint, &alpha, &mode);
1684
Romain Guyd21b6e12011-11-30 20:21:23 -08001685 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1686
Romain Guy211370f2012-02-01 16:10:55 -08001687 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001688 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1689 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1690
Romain Guyb5014982011-07-28 15:39:12 -07001691 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001692 // Enable linear filtering if the source rectangle is scaled
1693 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001694 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001695 }
Romain Guyb5014982011-07-28 15:39:12 -07001696
Romain Guyd21b6e12011-11-30 20:21:23 -08001697 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001698 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1699 texture->id, alpha / 255.0f, mode, texture->blend,
1700 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1701 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1702 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001703 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001704 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1705 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1706 GL_TRIANGLE_STRIP, gMeshCount);
1707 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001708
1709 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001710
1711 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001712}
1713
Chet Haase48659092012-05-31 15:21:51 -07001714status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001715 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001716 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001717 int alpha;
1718 SkXfermode::Mode mode;
1719 getAlphaAndModeDirect(paint, &alpha, &mode);
1720
1721 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1722 left, top, right, bottom, alpha, mode);
1723}
1724
1725status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1726 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1727 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001728 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001729 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001730 }
1731
Romain Guybe6f9dc2012-07-16 12:41:17 -07001732 alpha *= mSnapshot->alpha;
1733
Romain Guya1d3c912011-12-13 14:55:06 -08001734 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001735 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001736 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001737 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001738 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1739 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001740
Romain Guy2728f962010-10-08 18:36:15 -07001741 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001742 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001743
Romain Guy211370f2012-02-01 16:10:55 -08001744 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001745 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001746 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001747 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001748 const float offsetX = left + mSnapshot->transform->getTranslateX();
1749 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001750 const size_t count = mesh->quads.size();
1751 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001752 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001753 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001754 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1755 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1756 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001757 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001758 dirtyLayer(left + bounds.left, top + bounds.top,
1759 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001760 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001761 }
1762 }
1763
Romain Guy211370f2012-02-01 16:10:55 -08001764 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001765 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1766 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1767
1768 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1769 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1770 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1771 true, !mesh->hasEmptyQuads);
1772 } else {
1773 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1774 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1775 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1776 true, !mesh->hasEmptyQuads);
1777 }
Romain Guy054dc182010-10-15 17:55:25 -07001778 }
Chet Haase48659092012-05-31 15:21:51 -07001779
1780 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001781}
1782
Chet Haase99ecdc42011-05-06 12:06:34 -07001783/**
Chet Haase858aa932011-05-12 09:06:00 -07001784 * This function uses a similar approach to that of AA lines in the drawLines() function.
Chris Craik6ebdc112012-08-31 18:24:33 -07001785 * We expand the rectangle by a half pixel in screen space on all sides. However, instead of using
1786 * a fragment shader to compute the translucency of the color from its position, we simply use a
1787 * varying parameter to define how far a given pixel is into the region.
Chet Haase858aa932011-05-12 09:06:00 -07001788 */
1789void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001790 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001791 float inverseScaleX = 1.0f;
1792 float inverseScaleY = 1.0f;
Romain Guy04299382012-07-18 17:15:41 -07001793
Chet Haase858aa932011-05-12 09:06:00 -07001794 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001795 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001796 Matrix4 *mat = mSnapshot->transform;
1797 float m00 = mat->data[Matrix4::kScaleX];
1798 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase858aa932011-05-12 09:06:00 -07001799 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001800 float m11 = mat->data[Matrix4::kScaleY];
Chet Haase858aa932011-05-12 09:06:00 -07001801 float scaleX = sqrt(m00 * m00 + m01 * m01);
1802 float scaleY = sqrt(m10 * m10 + m11 * m11);
1803 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1804 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1805 }
1806
Chet Haase858aa932011-05-12 09:06:00 -07001807 float boundarySizeX = .5 * inverseScaleX;
1808 float boundarySizeY = .5 * inverseScaleY;
1809
Chris Craik6ebdc112012-08-31 18:24:33 -07001810 float innerLeft = left + boundarySizeX;
1811 float innerRight = right - boundarySizeX;
1812 float innerTop = top + boundarySizeY;
1813 float innerBottom = bottom - boundarySizeY;
1814
Chet Haase858aa932011-05-12 09:06:00 -07001815 // Adjust the rect by the AA boundary padding
1816 left -= boundarySizeX;
1817 right += boundarySizeX;
1818 top -= boundarySizeY;
1819 bottom += boundarySizeY;
1820
Chet Haase858aa932011-05-12 09:06:00 -07001821 if (!quickReject(left, top, right, bottom)) {
Romain Guy04299382012-07-18 17:15:41 -07001822 setupDraw();
1823 setupDrawNoTexture();
Chris Craik6ebdc112012-08-31 18:24:33 -07001824 setupDrawAARect();
Romain Guy04299382012-07-18 17:15:41 -07001825 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1826 setupDrawColorFilter();
1827 setupDrawShader();
1828 setupDrawBlending(true, mode);
1829 setupDrawProgram();
1830 setupDrawModelViewIdentity(true);
1831 setupDrawColorUniforms();
1832 setupDrawColorFilterUniforms();
1833 setupDrawShaderIdentityUniforms();
1834
Chris Craik6ebdc112012-08-31 18:24:33 -07001835 AlphaVertex rects[14];
1836 AlphaVertex* aVertices = &rects[0];
1837 void* alphaCoords = ((GLbyte*) aVertices) + gVertexAlphaOffset;
Romain Guy04299382012-07-18 17:15:41 -07001838
Chris Craik6ebdc112012-08-31 18:24:33 -07001839 bool force = mCaches.unbindMeshBuffer();
1840 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1841 aVertices, gAlphaVertexStride);
1842 mCaches.resetTexCoordsVertexPointer();
1843 mCaches.unbindIndicesBuffer();
Romain Guy04299382012-07-18 17:15:41 -07001844
Chris Craik6ebdc112012-08-31 18:24:33 -07001845 int alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
1846 glEnableVertexAttribArray(alphaSlot);
1847 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Romain Guy04299382012-07-18 17:15:41 -07001848
Chris Craik6ebdc112012-08-31 18:24:33 -07001849 // draw left
1850 AlphaVertex::set(aVertices++, left, bottom, 0);
1851 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1852 AlphaVertex::set(aVertices++, left, top, 0);
1853 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001854
Chris Craik6ebdc112012-08-31 18:24:33 -07001855 // draw top
1856 AlphaVertex::set(aVertices++, right, top, 0);
1857 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001858
Chris Craik6ebdc112012-08-31 18:24:33 -07001859 // draw right
1860 AlphaVertex::set(aVertices++, right, bottom, 0);
1861 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1862
1863 // draw bottom
1864 AlphaVertex::set(aVertices++, left, bottom, 0);
1865 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1866
1867 // draw inner rect (repeating last vertex to create degenerate bridge triangles)
1868 // TODO: also consider drawing the inner rect without the blending-forced shader, if
1869 // blending is expensive. Note: can't use drawColorRect() since it doesn't use vertex
1870 // buffers like below, resulting in slightly different transformed coordinates.
1871 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1872 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
1873 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1874 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
1875
Chet Haase858aa932011-05-12 09:06:00 -07001876 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guy7b631422012-04-04 11:38:54 -07001877
Chris Craik6ebdc112012-08-31 18:24:33 -07001878 glDrawArrays(GL_TRIANGLE_STRIP, 0, 14);
Romain Guy04299382012-07-18 17:15:41 -07001879
Chris Craik6ebdc112012-08-31 18:24:33 -07001880 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001881 }
Chet Haase858aa932011-05-12 09:06:00 -07001882}
1883
1884/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001885 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1886 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1887 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1888 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1889 * of the line. Hairlines are more involved because we need to account for transform scaling
1890 * to end up with a one-pixel-wide line in screen space..
1891 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1892 * in combination with values that we calculate and pass down in this method. The basic approach
1893 * is that the quad we create contains both the core line area plus a bounding area in which
1894 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1895 * proportion of the width and the length of a given segment is represented by the boundary
1896 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1897 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1898 * on the inside). This ends up giving the result we want, with pixels that are completely
1899 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1900 * how far into the boundary region they are, which is determined by shader interpolation.
1901 */
Chet Haase48659092012-05-31 15:21:51 -07001902status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1903 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001904
1905 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001906 // We use half the stroke width here because we're going to position the quad
1907 // corner vertices half of the width away from the line endpoints
1908 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001909 // A stroke width of 0 has a special meaning in Skia:
1910 // it draws a line 1 px wide regardless of current transform
1911 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001912
Chet Haase99ecdc42011-05-06 12:06:34 -07001913 float inverseScaleX = 1.0f;
1914 float inverseScaleY = 1.0f;
1915 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001916
Chet Haase8a5cc922011-04-26 07:28:09 -07001917 int alpha;
1918 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001919
Chet Haase8a5cc922011-04-26 07:28:09 -07001920 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001921 int verticesCount = count;
1922 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001923 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001924 verticesCount += (count - 4);
1925 }
1926
1927 if (isHairLine || isAA) {
1928 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1929 // the line on the screen should always be one pixel wide regardless of scale. For
1930 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001931 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001932 Matrix4 *mat = mSnapshot->transform;
1933 float m00 = mat->data[Matrix4::kScaleX];
1934 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07001935 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001936 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07001937
Romain Guy211370f2012-02-01 16:10:55 -08001938 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1939 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001940
Chet Haase99ecdc42011-05-06 12:06:34 -07001941 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1942 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001943
Chet Haase99ecdc42011-05-06 12:06:34 -07001944 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1945 scaled = true;
1946 }
1947 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001948 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001949
1950 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07001951
1952 mCaches.enableScissor();
1953
Chet Haase8a5cc922011-04-26 07:28:09 -07001954 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001955 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001956 if (isAA) {
1957 setupDrawAALine();
1958 }
1959 setupDrawColor(paint->getColor(), alpha);
1960 setupDrawColorFilter();
1961 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001962 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001963 setupDrawProgram();
1964 setupDrawModelViewIdentity(true);
1965 setupDrawColorUniforms();
1966 setupDrawColorFilterUniforms();
1967 setupDrawShaderIdentityUniforms();
1968
1969 if (isHairLine) {
1970 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001971 halfStrokeWidth = isAA? 1 : .5;
1972 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001973 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001974 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001975 }
Romain Guy7b631422012-04-04 11:38:54 -07001976
1977 int widthSlot;
1978 int lengthSlot;
1979
Chet Haase5b0200b2011-04-13 17:58:08 -07001980 Vertex lines[verticesCount];
1981 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001982
Chet Haase99585ad2011-05-02 15:00:16 -07001983 AAVertex wLines[verticesCount];
1984 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001985
Romain Guy211370f2012-02-01 16:10:55 -08001986 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001987 setupDrawVertices(vertices);
1988 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001989 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1990 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001991 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001992 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1993 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001994 // We will need to calculate the actual width proportion on each segment for
1995 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07001996 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001997 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1998 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001999 }
2000
Chet Haase99ecdc42011-05-06 12:06:34 -07002001 AAVertex* prevAAVertex = NULL;
2002 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002003
Chet Haase99585ad2011-05-02 15:00:16 -07002004 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002005 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002006
Chet Haase5b0200b2011-04-13 17:58:08 -07002007 for (int i = 0; i < count; i += 4) {
2008 // a = start point, b = end point
2009 vec2 a(points[i], points[i + 1]);
2010 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002011
Chet Haase99585ad2011-05-02 15:00:16 -07002012 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002013 float boundaryLengthProportion = 0;
2014 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002015
Chet Haase5b0200b2011-04-13 17:58:08 -07002016 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002017 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002018 float x = n.x;
2019 n.x = -n.y;
2020 n.y = x;
2021
Chet Haase8a5cc922011-04-26 07:28:09 -07002022 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002023 if (isAA) {
2024 float wideningFactor;
2025 if (fabs(n.x) >= fabs(n.y)) {
2026 wideningFactor = fabs(1.0f / n.x);
2027 } else {
2028 wideningFactor = fabs(1.0f / n.y);
2029 }
2030 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002031 }
Romain Guy7b631422012-04-04 11:38:54 -07002032
Chet Haase99ecdc42011-05-06 12:06:34 -07002033 if (scaled) {
2034 n.x *= inverseScaleX;
2035 n.y *= inverseScaleY;
2036 }
2037 } else if (scaled) {
2038 // Extend n by .5 pixel on each side, post-transform
2039 vec2 extendedN = n.copyNormalized();
2040 extendedN /= 2;
2041 extendedN.x *= inverseScaleX;
2042 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002043
Chet Haase99ecdc42011-05-06 12:06:34 -07002044 float extendedNLength = extendedN.length();
2045 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002046 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002047 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002048 }
Romain Guy7b631422012-04-04 11:38:54 -07002049
Chet Haase99585ad2011-05-02 15:00:16 -07002050 // aa lines expand the endpoint vertices to encompass the AA boundary
2051 if (isAA) {
2052 vec2 abVector = (b - a);
2053 length = abVector.length();
2054 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002055
Chet Haase99ecdc42011-05-06 12:06:34 -07002056 if (scaled) {
2057 abVector.x *= inverseScaleX;
2058 abVector.y *= inverseScaleY;
2059 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002060 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002061 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002062 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002063 }
Romain Guy7b631422012-04-04 11:38:54 -07002064
Chet Haase99ecdc42011-05-06 12:06:34 -07002065 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002066 a -= abVector;
2067 b += abVector;
2068 }
2069
Chet Haase5b0200b2011-04-13 17:58:08 -07002070 // Four corners of the rectangle defining a thick line
2071 vec2 p1 = a - n;
2072 vec2 p2 = a + n;
2073 vec2 p3 = b + n;
2074 vec2 p4 = b - n;
2075
Chet Haase99585ad2011-05-02 15:00:16 -07002076
Chet Haase5b0200b2011-04-13 17:58:08 -07002077 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2078 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2079 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2080 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2081
Romain Guy04299382012-07-18 17:15:41 -07002082 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002083 if (!isAA) {
2084 if (prevVertex != NULL) {
2085 // Issue two repeat vertices to create degenerate triangles to bridge
2086 // between the previous line and the new one. This is necessary because
2087 // we are creating a single triangle_strip which will contain
2088 // potentially discontinuous line segments.
2089 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2090 Vertex::set(vertices++, p1.x, p1.y);
2091 generatedVerticesCount += 2;
2092 }
Romain Guy7b631422012-04-04 11:38:54 -07002093
Chet Haase5b0200b2011-04-13 17:58:08 -07002094 Vertex::set(vertices++, p1.x, p1.y);
2095 Vertex::set(vertices++, p2.x, p2.y);
2096 Vertex::set(vertices++, p4.x, p4.y);
2097 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002098
Chet Haase5b0200b2011-04-13 17:58:08 -07002099 prevVertex = vertices - 1;
2100 generatedVerticesCount += 4;
2101 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002102 if (!isHairLine && scaled) {
2103 // Must set width proportions per-segment for scaled non-hairlines to use the
2104 // correct AA boundary dimensions
2105 if (boundaryWidthSlot < 0) {
2106 boundaryWidthSlot =
2107 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002108 }
Romain Guy7b631422012-04-04 11:38:54 -07002109
Chet Haase99ecdc42011-05-06 12:06:34 -07002110 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002111 }
Romain Guy7b631422012-04-04 11:38:54 -07002112
Chet Haase99585ad2011-05-02 15:00:16 -07002113 if (boundaryLengthSlot < 0) {
2114 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002115 }
Romain Guy7b631422012-04-04 11:38:54 -07002116
Chet Haase99ecdc42011-05-06 12:06:34 -07002117 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002118
Chet Haase5b0200b2011-04-13 17:58:08 -07002119 if (prevAAVertex != NULL) {
2120 // Issue two repeat vertices to create degenerate triangles to bridge
2121 // between the previous line and the new one. This is necessary because
2122 // we are creating a single triangle_strip which will contain
2123 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002124 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2125 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2126 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002127 generatedVerticesCount += 2;
2128 }
Romain Guy7b631422012-04-04 11:38:54 -07002129
Chet Haase99585ad2011-05-02 15:00:16 -07002130 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2131 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2132 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2133 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002134
Chet Haase5b0200b2011-04-13 17:58:08 -07002135 prevAAVertex = aaVertices - 1;
2136 generatedVerticesCount += 4;
2137 }
Romain Guy7b631422012-04-04 11:38:54 -07002138
Chet Haase99585ad2011-05-02 15:00:16 -07002139 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2140 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2141 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002142 }
2143 }
Romain Guy7b631422012-04-04 11:38:54 -07002144
Chet Haase5b0200b2011-04-13 17:58:08 -07002145 if (generatedVerticesCount > 0) {
2146 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2147 }
Romain Guy7b631422012-04-04 11:38:54 -07002148
2149 if (isAA) {
2150 finishDrawAALine(widthSlot, lengthSlot);
2151 }
Chet Haase48659092012-05-31 15:21:51 -07002152
2153 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002154}
2155
Chet Haase48659092012-05-31 15:21:51 -07002156status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2157 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002158
2159 // TODO: The paint's cap style defines whether the points are square or circular
2160 // TODO: Handle AA for round points
2161
Chet Haase5b0200b2011-04-13 17:58:08 -07002162 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002163 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002164 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002165 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002166 if (isHairLine) {
2167 // Now that we know it's hairline, we can set the effective width, to be used later
2168 strokeWidth = 1.0f;
2169 }
2170 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002171 int alpha;
2172 SkXfermode::Mode mode;
2173 getAlphaAndMode(paint, &alpha, &mode);
2174
2175 int verticesCount = count >> 1;
2176 int generatedVerticesCount = 0;
2177
2178 TextureVertex pointsData[verticesCount];
2179 TextureVertex* vertex = &pointsData[0];
2180
Romain Guy04299382012-07-18 17:15:41 -07002181 // TODO: We should optimize this method to not generate vertices for points
2182 // that lie outside of the clip.
2183 mCaches.enableScissor();
2184
Romain Guyed6fcb02011-03-21 13:11:28 -07002185 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002186 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002187 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002188 setupDrawColor(paint->getColor(), alpha);
2189 setupDrawColorFilter();
2190 setupDrawShader();
2191 setupDrawBlending(mode);
2192 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002193 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002194 setupDrawColorUniforms();
2195 setupDrawColorFilterUniforms();
2196 setupDrawPointUniforms();
2197 setupDrawShaderIdentityUniforms();
2198 setupDrawMesh(vertex);
2199
2200 for (int i = 0; i < count; i += 2) {
2201 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2202 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002203
Chet Haase8a5cc922011-04-26 07:28:09 -07002204 float left = points[i] - halfWidth;
2205 float right = points[i] + halfWidth;
2206 float top = points[i + 1] - halfWidth;
2207 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002208
Chet Haase8a5cc922011-04-26 07:28:09 -07002209 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002210 }
2211
2212 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002213
2214 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002215}
2216
Chet Haase48659092012-05-31 15:21:51 -07002217status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002218 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002219 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002220
Romain Guyae88e5e2010-10-22 17:49:18 -07002221 Rect& clip(*mSnapshot->clipRect);
2222 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002223
Romain Guy3d58c032010-07-14 16:34:53 -07002224 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002225
2226 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002227}
Romain Guy9d5316e2010-06-24 19:30:36 -07002228
Chet Haase48659092012-05-31 15:21:51 -07002229status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2230 SkPaint* paint) {
2231 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002232 const AutoTexture autoCleanup(texture);
2233
2234 const float x = left + texture->left - texture->offset;
2235 const float y = top + texture->top - texture->offset;
2236
2237 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002238
2239 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002240}
2241
Chet Haase48659092012-05-31 15:21:51 -07002242status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002243 float rx, float ry, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002244 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002245
Romain Guya1d3c912011-12-13 14:55:06 -08002246 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002247 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2248 right - left, bottom - top, rx, ry, paint);
Chet Haase48659092012-05-31 15:21:51 -07002249 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002250}
2251
Chet Haase48659092012-05-31 15:21:51 -07002252status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2253 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002254
Romain Guya1d3c912011-12-13 14:55:06 -08002255 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002256 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Chet Haase48659092012-05-31 15:21:51 -07002257 return drawShape(x - radius, y - radius, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002258}
Romain Guy01d58e42011-01-19 21:54:02 -08002259
Chet Haase48659092012-05-31 15:21:51 -07002260status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
2261 SkPaint* paint) {
2262 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002263
Romain Guya1d3c912011-12-13 14:55:06 -08002264 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002265 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002266 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002267}
2268
Chet Haase48659092012-05-31 15:21:51 -07002269status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002270 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002271 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002272
2273 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002274 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002275 }
2276
Romain Guya1d3c912011-12-13 14:55:06 -08002277 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002278 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2279 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002280 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002281}
2282
Chet Haase48659092012-05-31 15:21:51 -07002283status_t OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002284 SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002285 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002286
Romain Guya1d3c912011-12-13 14:55:06 -08002287 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002288 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002289 return drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002290}
2291
Chet Haase48659092012-05-31 15:21:51 -07002292status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002293 if (p->getStyle() != SkPaint::kFill_Style) {
Chet Haase48659092012-05-31 15:21:51 -07002294 return drawRectAsShape(left, top, right, bottom, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002295 }
2296
Romain Guy6926c722010-07-12 20:20:03 -07002297 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002298 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002299 }
2300
Romain Guy026c5e162010-06-28 17:12:22 -07002301 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002302 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002303 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2304 if (!isMode) {
2305 // Assume SRC_OVER
2306 mode = SkXfermode::kSrcOver_Mode;
2307 }
2308 } else {
2309 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002310 }
2311
Romain Guy026c5e162010-06-28 17:12:22 -07002312 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002313 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002314 drawAARect(left, top, right, bottom, color, mode);
2315 } else {
2316 drawColorRect(left, top, right, bottom, color, mode);
2317 }
Chet Haase48659092012-05-31 15:21:51 -07002318
2319 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002320}
Romain Guy9d5316e2010-06-24 19:30:36 -07002321
Raph Levien416a8472012-07-19 22:48:17 -07002322void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2323 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2324 float x, float y) {
2325 mCaches.activeTexture(0);
2326
2327 // NOTE: The drop shadow will not perform gamma correction
2328 // if shader-based correction is enabled
2329 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2330 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2331 paint, text, bytesCount, count, mShadowRadius, positions);
2332 const AutoTexture autoCleanup(shadow);
2333
2334 const float sx = x - shadow->left + mShadowDx;
2335 const float sy = y - shadow->top + mShadowDy;
2336
2337 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2338 int shadowColor = mShadowColor;
2339 if (mShader) {
2340 shadowColor = 0xffffffff;
2341 }
2342
2343 setupDraw();
2344 setupDrawWithTexture(true);
2345 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2346 setupDrawColorFilter();
2347 setupDrawShader();
2348 setupDrawBlending(true, mode);
2349 setupDrawProgram();
2350 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2351 setupDrawTexture(shadow->id);
2352 setupDrawPureColorUniforms();
2353 setupDrawColorFilterUniforms();
2354 setupDrawShaderUniforms();
2355 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2356
2357 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2358}
2359
Chet Haase48659092012-05-31 15:21:51 -07002360status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002361 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002362 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002363 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002364 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002365 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002366
Romain Guy671d6cf2012-01-18 12:39:17 -08002367 // NOTE: Skia does not support perspective transform on drawPosText yet
2368 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002369 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002370 }
2371
2372 float x = 0.0f;
2373 float y = 0.0f;
2374 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2375 if (pureTranslate) {
2376 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2377 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2378 }
2379
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002380 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002381 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2382 paint->getTextSize());
2383
2384 int alpha;
2385 SkXfermode::Mode mode;
2386 getAlphaAndMode(paint, &alpha, &mode);
2387
Raph Levien416a8472012-07-19 22:48:17 -07002388 if (CC_UNLIKELY(mHasShadow)) {
2389 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2390 0.0f, 0.0f);
2391 }
2392
Romain Guy671d6cf2012-01-18 12:39:17 -08002393 // Pick the appropriate texture filtering
2394 bool linearFilter = mSnapshot->transform->changesBounds();
2395 if (pureTranslate && !linearFilter) {
2396 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2397 }
2398
2399 mCaches.activeTexture(0);
2400 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002401 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002402 setupDrawDirtyRegionsDisabled();
2403 setupDrawWithTexture(true);
2404 setupDrawAlpha8Color(paint->getColor(), alpha);
2405 setupDrawColorFilter();
2406 setupDrawShader();
2407 setupDrawBlending(true, mode);
2408 setupDrawProgram();
2409 setupDrawModelView(x, y, x, y, pureTranslate, true);
2410 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2411 setupDrawPureColorUniforms();
2412 setupDrawColorFilterUniforms();
2413 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002414 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002415
2416 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2417 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2418
Romain Guy211370f2012-02-01 16:10:55 -08002419 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002420
2421 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2422 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002423 if (hasActiveLayer) {
2424 if (!pureTranslate) {
2425 mSnapshot->transform->mapRect(bounds);
2426 }
2427 dirtyLayerUnchecked(bounds, getRegion());
2428 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002429 }
Chet Haase48659092012-05-31 15:21:51 -07002430
2431 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002432}
2433
Romain Guyc2525952012-07-27 16:41:22 -07002434status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002435 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002436 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002437 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002438 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002439 }
2440
Chet Haasea1cff502012-02-21 13:43:44 -08002441 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002442 switch (paint->getTextAlign()) {
2443 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002444 x -= length / 2.0f;
2445 break;
2446 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002447 x -= length;
2448 break;
2449 default:
2450 break;
2451 }
2452
Romain Guycac5fd32011-12-01 20:08:50 -08002453 SkPaint::FontMetrics metrics;
2454 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002455 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002456 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002457 }
2458
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002459 const float oldX = x;
2460 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002461 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002462 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002463 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2464 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2465 }
2466
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002467#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002468 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002469 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002470#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002471
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002472 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002473 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002474 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002475
Romain Guy86568192010-12-14 15:55:39 -08002476 int alpha;
2477 SkXfermode::Mode mode;
2478 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002479
Romain Guy211370f2012-02-01 16:10:55 -08002480 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002481 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2482 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002483 }
2484
Romain Guy6620c6d2010-12-06 18:07:02 -08002485 // Pick the appropriate texture filtering
2486 bool linearFilter = mSnapshot->transform->changesBounds();
2487 if (pureTranslate && !linearFilter) {
2488 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2489 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002490
Romain Guy16c88082012-06-11 16:03:47 -07002491 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002492 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002493 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002494 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002495 setupDrawDirtyRegionsDisabled();
2496 setupDrawWithTexture(true);
2497 setupDrawAlpha8Color(paint->getColor(), alpha);
2498 setupDrawColorFilter();
2499 setupDrawShader();
2500 setupDrawBlending(true, mode);
2501 setupDrawProgram();
2502 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002503 // See comment above; the font renderer must use texture unit 0
2504 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002505 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2506 setupDrawPureColorUniforms();
2507 setupDrawColorFilterUniforms();
2508 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002509 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002510
Romain Guy6620c6d2010-12-06 18:07:02 -08002511 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002512 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2513
Romain Guy211370f2012-02-01 16:10:55 -08002514 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002515
Raph Levien996e57c2012-07-23 15:22:52 -07002516 bool status;
Raph Levien8b4072d2012-07-30 15:50:00 -07002517 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
2518 SkPaint paintCopy(*paint);
2519 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2520 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Raph Levien996e57c2012-07-23 15:22:52 -07002521 positions, hasActiveLayer ? &bounds : NULL);
2522 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002523 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2524 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002525 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002526
2527 if (status && hasActiveLayer) {
2528 if (!pureTranslate) {
2529 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002530 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002531 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002532 }
Romain Guy694b5192010-07-21 21:33:20 -07002533
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002534 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002535
2536 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002537}
2538
Chet Haase48659092012-05-31 15:21:51 -07002539status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002540 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002541 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2542 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002543 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002544 }
2545
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002546 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002547 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2548 paint->getTextSize());
2549
2550 int alpha;
2551 SkXfermode::Mode mode;
2552 getAlphaAndMode(paint, &alpha, &mode);
2553
2554 mCaches.activeTexture(0);
2555 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002556 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002557 setupDrawDirtyRegionsDisabled();
2558 setupDrawWithTexture(true);
2559 setupDrawAlpha8Color(paint->getColor(), alpha);
2560 setupDrawColorFilter();
2561 setupDrawShader();
2562 setupDrawBlending(true, mode);
2563 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002564 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002565 setupDrawTexture(fontRenderer.getTexture(true));
2566 setupDrawPureColorUniforms();
2567 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002568 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002569 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002570
Romain Guy97771732012-02-28 18:17:02 -08002571 const Rect* clip = &mSnapshot->getLocalClip();
2572 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 -08002573
Romain Guy97771732012-02-28 18:17:02 -08002574 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002575
2576 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2577 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002578 if (hasActiveLayer) {
2579 mSnapshot->transform->mapRect(bounds);
2580 dirtyLayerUnchecked(bounds, getRegion());
2581 }
Romain Guy97771732012-02-28 18:17:02 -08002582 }
Chet Haase48659092012-05-31 15:21:51 -07002583
2584 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002585}
2586
Chet Haase48659092012-05-31 15:21:51 -07002587status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2588 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002589
Romain Guya1d3c912011-12-13 14:55:06 -08002590 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002591
Romain Guy33f6beb2012-02-16 19:24:51 -08002592 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002593 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002594 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002595 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002596
Romain Guy8b55f372010-08-18 17:10:07 -07002597 const float x = texture->left - texture->offset;
2598 const float y = texture->top - texture->offset;
2599
Romain Guy01d58e42011-01-19 21:54:02 -08002600 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002601
2602 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002603}
2604
Chet Haase48659092012-05-31 15:21:51 -07002605status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002606 if (!layer) {
2607 return DrawGlInfo::kStatusDone;
2608 }
2609
2610 Rect transformed;
2611 Rect clip;
2612 const bool rejected = quickRejectNoScissor(x, y,
2613 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2614
2615 if (rejected) {
Chet Haase48659092012-05-31 15:21:51 -07002616 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002617 }
2618
Romain Guy4ff0cf42012-08-06 14:51:10 -07002619 bool debugLayerUpdate = false;
Romain Guy2bf68f02012-03-02 13:37:47 -08002620 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2621 OpenGLRenderer* renderer = layer->renderer;
2622 Rect& dirty = layer->dirtyRect;
2623
Romain Guy2bf68f02012-03-02 13:37:47 -08002624 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2625 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002626 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002627 renderer->finish();
Romain Guy35643dd2012-09-18 15:40:58 -07002628
2629 resumeAfterLayer();
Romain Guy2bf68f02012-03-02 13:37:47 -08002630
2631 dirty.setEmpty();
2632 layer->deferredUpdateScheduled = false;
2633 layer->renderer = NULL;
2634 layer->displayList = NULL;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002635
2636 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002637 }
2638
Romain Guy35643dd2012-09-18 15:40:58 -07002639 mCaches.setScissorEnabled(!clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002640 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002641
Romain Guy211370f2012-02-01 16:10:55 -08002642 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002643 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002644 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002645 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002646 const float a = layer->getAlpha() / 255.0f;
2647 SkiaColorFilter *oldFilter = mColorFilter;
2648 mColorFilter = layer->getColorFilter();
Romain Guyf219da52011-01-16 12:54:25 -08002649
Romain Guyc88e3572011-01-22 00:32:12 -08002650 setupDraw();
2651 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002652 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002653 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002654 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002655 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002656 setupDrawPureColorUniforms();
2657 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002658 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002659 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002660 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2661 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002662
Romain Guyd21b6e12011-11-30 20:21:23 -08002663 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002664 setupDrawModelViewTranslate(tx, ty,
2665 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002666 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002667 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002668 setupDrawModelViewTranslate(x, y,
2669 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2670 }
Romain Guyc88e3572011-01-22 00:32:12 -08002671 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002672
Romain Guyc88e3572011-01-22 00:32:12 -08002673 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2674 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002675
Romain Guyc88e3572011-01-22 00:32:12 -08002676 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002677
Chet Haased15ebf22012-09-05 11:40:29 -07002678 mColorFilter = oldFilter;
2679
Romain Guy3a3133d2011-02-01 22:59:58 -08002680#if DEBUG_LAYERS_AS_REGIONS
2681 drawRegionRects(layer->region);
2682#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002683 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002684
2685 if (debugLayerUpdate) {
2686 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2687 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2688 }
Romain Guyf219da52011-01-16 12:54:25 -08002689 }
Chet Haase48659092012-05-31 15:21:51 -07002690
2691 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002692}
2693
Romain Guy6926c722010-07-12 20:20:03 -07002694///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002695// Shaders
2696///////////////////////////////////////////////////////////////////////////////
2697
2698void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002699 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002700}
2701
Romain Guy06f96e22010-07-30 19:18:16 -07002702void OpenGLRenderer::setupShader(SkiaShader* shader) {
2703 mShader = shader;
2704 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002705 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002706 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002707}
2708
Romain Guyd27977d2010-07-14 19:18:51 -07002709///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002710// Color filters
2711///////////////////////////////////////////////////////////////////////////////
2712
2713void OpenGLRenderer::resetColorFilter() {
2714 mColorFilter = NULL;
2715}
2716
2717void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2718 mColorFilter = filter;
2719}
2720
2721///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002722// Drop shadow
2723///////////////////////////////////////////////////////////////////////////////
2724
2725void OpenGLRenderer::resetShadow() {
2726 mHasShadow = false;
2727}
2728
2729void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2730 mHasShadow = true;
2731 mShadowRadius = radius;
2732 mShadowDx = dx;
2733 mShadowDy = dy;
2734 mShadowColor = color;
2735}
2736
2737///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002738// Draw filters
2739///////////////////////////////////////////////////////////////////////////////
2740
2741void OpenGLRenderer::resetPaintFilter() {
2742 mHasDrawFilter = false;
2743}
2744
2745void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2746 mHasDrawFilter = true;
2747 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2748 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2749}
2750
2751SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002752 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002753
2754 uint32_t flags = paint->getFlags();
2755
2756 mFilteredPaint = *paint;
2757 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2758
2759 return &mFilteredPaint;
2760}
2761
2762///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002763// Drawing implementation
2764///////////////////////////////////////////////////////////////////////////////
2765
Romain Guy01d58e42011-01-19 21:54:02 -08002766void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2767 float x, float y, SkPaint* paint) {
2768 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2769 return;
2770 }
2771
2772 int alpha;
2773 SkXfermode::Mode mode;
2774 getAlphaAndMode(paint, &alpha, &mode);
2775
2776 setupDraw();
2777 setupDrawWithTexture(true);
2778 setupDrawAlpha8Color(paint->getColor(), alpha);
2779 setupDrawColorFilter();
2780 setupDrawShader();
2781 setupDrawBlending(true, mode);
2782 setupDrawProgram();
2783 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2784 setupDrawTexture(texture->id);
2785 setupDrawPureColorUniforms();
2786 setupDrawColorFilterUniforms();
2787 setupDrawShaderUniforms();
2788 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2789
2790 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2791
2792 finishDrawTexture();
2793}
2794
Romain Guyf607bdc2010-09-10 19:20:06 -07002795// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002796#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2797#define kStdUnderline_Offset (1.0f / 9.0f)
2798#define kStdUnderline_Thickness (1.0f / 18.0f)
2799
2800void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2801 float x, float y, SkPaint* paint) {
2802 // Handle underline and strike-through
2803 uint32_t flags = paint->getFlags();
2804 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002805 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002806 float underlineWidth = length;
2807 // If length is > 0.0f, we already measured the text for the text alignment
2808 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002809 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002810 }
2811
Romain Guy211370f2012-02-01 16:10:55 -08002812 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002813 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002814 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002815
Raph Levien8b4072d2012-07-30 15:50:00 -07002816 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002817 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002818
Romain Guyf6834472011-01-23 13:32:12 -08002819 int linesCount = 0;
2820 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2821 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2822
2823 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002824 float points[pointsCount];
2825 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002826
2827 if (flags & SkPaint::kUnderlineText_Flag) {
2828 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002829 points[currentPoint++] = left;
2830 points[currentPoint++] = top;
2831 points[currentPoint++] = left + underlineWidth;
2832 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002833 }
2834
2835 if (flags & SkPaint::kStrikeThruText_Flag) {
2836 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002837 points[currentPoint++] = left;
2838 points[currentPoint++] = top;
2839 points[currentPoint++] = left + underlineWidth;
2840 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002841 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002842
Romain Guy726aeba2011-06-01 14:52:00 -07002843 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002844
Romain Guy726aeba2011-06-01 14:52:00 -07002845 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002846 }
2847 }
2848}
2849
Romain Guy026c5e162010-06-28 17:12:22 -07002850void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002851 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002852 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002853 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002854 color |= 0x00ffffff;
2855 }
2856
Romain Guy70ca14e2010-12-13 18:24:33 -08002857 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002858 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002859 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002860 setupDrawShader();
2861 setupDrawColorFilter();
2862 setupDrawBlending(mode);
2863 setupDrawProgram();
2864 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2865 setupDrawColorUniforms();
2866 setupDrawShaderUniforms(ignoreTransform);
2867 setupDrawColorFilterUniforms();
2868 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002869
Romain Guyc95c8d62010-09-17 15:31:32 -07002870 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2871}
2872
Romain Guy82ba8142010-07-09 13:25:56 -07002873void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002874 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002875 int alpha;
2876 SkXfermode::Mode mode;
2877 getAlphaAndMode(paint, &alpha, &mode);
2878
Romain Guyd21b6e12011-11-30 20:21:23 -08002879 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002880
Romain Guy211370f2012-02-01 16:10:55 -08002881 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002882 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2883 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2884
Romain Guyd21b6e12011-11-30 20:21:23 -08002885 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002886 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2887 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2888 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2889 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002890 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002891 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2892 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2893 GL_TRIANGLE_STRIP, gMeshCount);
2894 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002895}
2896
Romain Guybd6b79b2010-06-26 00:13:53 -07002897void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002898 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2899 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002900 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002901}
2902
2903void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002904 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002905 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002906 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002907
Romain Guy746b7402010-10-26 16:27:31 -07002908 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002909 setupDrawWithTexture();
2910 setupDrawColor(alpha, alpha, alpha, alpha);
2911 setupDrawColorFilter();
2912 setupDrawBlending(blend, mode, swapSrcDst);
2913 setupDrawProgram();
2914 if (!dirty) {
2915 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002916 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002917 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002918 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002919 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002920 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002921 }
Romain Guy86568192010-12-14 15:55:39 -08002922 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002923 setupDrawColorFilterUniforms();
2924 setupDrawTexture(texture);
2925 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002926
Romain Guy6820ac82010-09-15 18:11:50 -07002927 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002928
2929 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002930}
2931
Romain Guya5aed0d2010-09-09 14:42:43 -07002932void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002933 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002934 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002935
Romain Guy82ba8142010-07-09 13:25:56 -07002936 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002937 // These blend modes are not supported by OpenGL directly and have
2938 // to be implemented using shaders. Since the shader will perform
2939 // the blending, turn blending off here
2940 // If the blend mode cannot be implemented using shaders, fall
2941 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07002942 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08002943 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002944 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002945 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002946
Romain Guy82bc7a72012-01-03 14:13:39 -08002947 if (mCaches.blend) {
2948 glDisable(GL_BLEND);
2949 mCaches.blend = false;
2950 }
2951
2952 return;
2953 } else {
2954 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002955 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002956 }
2957
2958 if (!mCaches.blend) {
2959 glEnable(GL_BLEND);
2960 }
2961
2962 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2963 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2964
2965 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2966 glBlendFunc(sourceMode, destMode);
2967 mCaches.lastSrcMode = sourceMode;
2968 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002969 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002970 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002971 glDisable(GL_BLEND);
2972 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002973 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002974}
2975
Romain Guy889f8d12010-07-29 14:37:42 -07002976bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002977 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002978 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002979 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002980 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002981 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002982 }
Romain Guy6926c722010-07-12 20:20:03 -07002983 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002984}
2985
Romain Guy026c5e162010-06-28 17:12:22 -07002986void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002987 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002988 TextureVertex::setUV(v++, u1, v1);
2989 TextureVertex::setUV(v++, u2, v1);
2990 TextureVertex::setUV(v++, u1, v2);
2991 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002992}
2993
Chet Haase5c13d892010-10-08 08:37:55 -07002994void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002995 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07002996 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002997}
2998
Romain Guy9d5316e2010-06-24 19:30:36 -07002999}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003000}; // namespace android