blob: a7553209fb1b977a06b328ee8f3eeae3ad6ac16e [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 Guy530041d2012-01-25 18:56:29 -0800142uint32_t OpenGLRenderer::getStencilSize() {
143 return STENCIL_BUFFER_SIZE;
144}
145
Romain Guy49c5fc02012-05-15 11:10:01 -0700146bool OpenGLRenderer::isDeferred() {
147 return false;
148}
149
Romain Guy85bf02f2010-06-22 13:11:24 -0700150void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700151 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700152
153 mWidth = width;
154 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700155
156 mFirstSnapshot->height = height;
157 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700158
Romain Guy3e263fa2011-12-12 16:47:48 -0800159 glDisable(GL_DITHER);
Romain Guy39d252a2011-12-12 18:14:06 -0800160 glEnable(GL_SCISSOR_TEST);
Romain Guy3e263fa2011-12-12 16:47:48 -0800161 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800162
Romain Guy3e263fa2011-12-12 16:47:48 -0800163 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700164}
165
Chet Haase44b2fe32012-06-06 19:03:58 -0700166int OpenGLRenderer::prepare(bool opaque) {
167 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800168}
169
Chet Haase44b2fe32012-06-06 19:03:58 -0700170int OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800171 mCaches.clearGarbage();
172
Romain Guy8aef54f2010-09-01 15:13:49 -0700173 mSnapshot = new Snapshot(mFirstSnapshot,
174 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800175 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700176 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700177
Romain Guy7d7b5492011-01-24 16:33:45 -0800178 mSnapshot->setClip(left, top, right, bottom);
Romain Guyddf74372012-05-22 14:07:07 -0700179 mDirtyClip = opaque;
180
181 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800182
Romain Guy6b7bd242010-10-06 19:49:23 -0700183 if (!opaque) {
Romain Guyddf74372012-05-22 14:07:07 -0700184 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700185 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700186 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700187 } else {
188 mCaches.resetScissor();
189 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700190
191 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700192}
193
194void OpenGLRenderer::syncState() {
195 glViewport(0, 0, mWidth, mHeight);
196
197 if (mCaches.blend) {
198 glEnable(GL_BLEND);
199 } else {
200 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700201 }
Romain Guybb9524b2010-06-22 18:56:38 -0700202}
203
Romain Guyb025b9c2010-09-16 14:16:48 -0700204void OpenGLRenderer::finish() {
205#if DEBUG_OPENGL
206 GLenum status = GL_NO_ERROR;
207 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000208 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800209 switch (status) {
Romain Guya44a63a2012-04-26 14:05:02 -0700210 case GL_INVALID_ENUM:
211 ALOGE(" GL_INVALID_ENUM");
212 break;
213 case GL_INVALID_VALUE:
214 ALOGE(" GL_INVALID_VALUE");
215 break;
216 case GL_INVALID_OPERATION:
217 ALOGE(" GL_INVALID_OPERATION");
218 break;
Romain Guya07105b2011-01-10 21:14:18 -0800219 case GL_OUT_OF_MEMORY:
Romain Guya44a63a2012-04-26 14:05:02 -0700220 ALOGE(" Out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800221 break;
222 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700223 }
224#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800225#if DEBUG_MEMORY_USAGE
226 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800227#else
228 if (mCaches.getDebugLevel() & kDebugMemory) {
229 mCaches.dumpMemoryUsage();
230 }
Romain Guyc15008e2010-11-10 11:59:15 -0800231#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700232}
233
Romain Guy6c319ca2011-01-11 14:29:25 -0800234void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700235 if (mCaches.currentProgram) {
236 if (mCaches.currentProgram->isInUse()) {
237 mCaches.currentProgram->remove();
238 mCaches.currentProgram = NULL;
239 }
240 }
Romain Guy50c0f092010-10-19 11:42:22 -0700241 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800242 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800243 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800244 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700245}
246
Romain Guy6c319ca2011-01-11 14:29:25 -0800247void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800248 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
249
250 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800251 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
252
Romain Guyda8532c2010-08-31 11:50:35 -0700253 glEnable(GL_SCISSOR_TEST);
Romain Guy82bc7a72012-01-03 14:13:39 -0800254 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700255 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700256
Romain Guya1d3c912011-12-13 14:55:06 -0800257 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800258 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700259
Romain Guy50c0f092010-10-19 11:42:22 -0700260 mCaches.blend = true;
261 glEnable(GL_BLEND);
262 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
263 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700264}
265
Romain Guyba6be8a2012-04-23 18:22:09 -0700266void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700267 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700268}
269
270void OpenGLRenderer::attachFunctor(Functor* functor) {
271 mFunctors.add(functor);
272}
273
Romain Guy8f3b8e32012-03-27 16:33:45 -0700274status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
275 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700276 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700277
Romain Guyba6be8a2012-04-23 18:22:09 -0700278 if (count > 0) {
279 SortedVector<Functor*> functors(mFunctors);
280 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700281
Romain Guyba6be8a2012-04-23 18:22:09 -0700282 DrawGlInfo info;
283 info.clipLeft = 0;
284 info.clipTop = 0;
285 info.clipRight = 0;
286 info.clipBottom = 0;
287 info.isLayer = false;
288 info.width = 0;
289 info.height = 0;
290 memset(info.transform, 0, sizeof(float) * 16);
291
292 for (size_t i = 0; i < count; i++) {
293 Functor* f = functors.itemAt(i);
294 result |= (*f)(DrawGlInfo::kModeProcess, &info);
295
Chris Craikc2c95432012-04-25 15:13:52 -0700296 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700297 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
298 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700299 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700300
Chris Craikc2c95432012-04-25 15:13:52 -0700301 if (result & DrawGlInfo::kStatusInvoke) {
302 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700303 }
304 }
305 }
306
Romain Guyc189ef52012-04-25 20:02:53 -0700307 mCaches.activeTexture(0);
308
Romain Guy8f3b8e32012-03-27 16:33:45 -0700309 return result;
310}
311
312status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800313 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700314 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700315
Romain Guyf90f8172011-01-25 22:53:24 -0800316 if (mDirtyClip) {
317 setScissorFromClip();
318 }
Romain Guyd643bb52011-03-01 14:55:21 -0800319
Romain Guy80911b82011-03-16 15:30:12 -0700320 Rect clip(*mSnapshot->clipRect);
321 clip.snapToPixelBoundaries();
322
Romain Guyd643bb52011-03-01 14:55:21 -0800323#if RENDER_LAYERS_AS_REGIONS
324 // Since we don't know what the functor will draw, let's dirty
325 // tne entire clip region
326 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800327 dirtyLayerUnchecked(clip, getRegion());
328 }
329#endif
330
Romain Guy08aa2cb2011-03-17 11:06:57 -0700331 DrawGlInfo info;
332 info.clipLeft = clip.left;
333 info.clipTop = clip.top;
334 info.clipRight = clip.right;
335 info.clipBottom = clip.bottom;
336 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700337 info.width = getSnapshot()->viewport.getWidth();
338 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700339 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700340
Chet Haase48659092012-05-31 15:21:51 -0700341 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800342
Romain Guy8f3b8e32012-03-27 16:33:45 -0700343 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700344 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800345 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700346
Chris Craik65924a32012-04-05 17:52:11 -0700347 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700348 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700349 }
Romain Guycabfcc12011-03-07 18:06:46 -0800350 }
351
Chet Haasedaf98e92011-01-10 14:10:36 -0800352 resume();
Romain Guy65549432012-03-26 16:45:05 -0700353 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800354}
355
Romain Guyf6a11b82010-06-23 17:47:49 -0700356///////////////////////////////////////////////////////////////////////////////
357// State management
358///////////////////////////////////////////////////////////////////////////////
359
Romain Guybb9524b2010-06-22 18:56:38 -0700360int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700361 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700362}
363
364int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700365 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700366}
367
368void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700369 if (mSaveCount > 1) {
370 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700371 }
Romain Guybb9524b2010-06-22 18:56:38 -0700372}
373
374void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700375 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700376
Romain Guy8fb95422010-08-17 18:38:51 -0700377 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700378 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700379 }
Romain Guybb9524b2010-06-22 18:56:38 -0700380}
381
Romain Guy8aef54f2010-09-01 15:13:49 -0700382int OpenGLRenderer::saveSnapshot(int flags) {
383 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700384 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700385}
386
387bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700388 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700389 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700390 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700391
Romain Guybd6b79b2010-06-26 00:13:53 -0700392 sp<Snapshot> current = mSnapshot;
393 sp<Snapshot> previous = mSnapshot->previous;
394
Romain Guyeb993562010-10-05 18:14:38 -0700395 if (restoreOrtho) {
396 Rect& r = previous->viewport;
397 glViewport(r.left, r.top, r.right, r.bottom);
398 mOrthoMatrix.load(current->orthoMatrix);
399 }
400
Romain Guy8b55f372010-08-18 17:10:07 -0700401 mSaveCount--;
402 mSnapshot = previous;
403
Romain Guy2542d192010-08-18 11:47:12 -0700404 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700405 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700406 }
Romain Guy2542d192010-08-18 11:47:12 -0700407
Romain Guy5ec99242010-11-03 16:19:08 -0700408 if (restoreLayer) {
409 composeLayer(current, previous);
410 }
411
Romain Guy2542d192010-08-18 11:47:12 -0700412 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700413}
414
Romain Guyf6a11b82010-06-23 17:47:49 -0700415///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700416// Layers
417///////////////////////////////////////////////////////////////////////////////
418
419int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700420 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700421 const GLuint previousFbo = mSnapshot->fbo;
422 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700423
Romain Guyaf636eb2010-12-09 17:47:21 -0800424 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700425 int alpha = 255;
426 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700427
Romain Guye45362c2010-11-03 19:58:32 -0700428 if (p) {
429 alpha = p->getAlpha();
430 if (!mCaches.extensions.hasFramebufferFetch()) {
431 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
432 if (!isMode) {
433 // Assume SRC_OVER
434 mode = SkXfermode::kSrcOver_Mode;
435 }
436 } else {
437 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700438 }
439 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700440 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700441 }
Romain Guyd55a8612010-06-28 17:42:46 -0700442
Romain Guydbc26d22010-10-11 17:58:29 -0700443 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
444 }
Romain Guyd55a8612010-06-28 17:42:46 -0700445
446 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700447}
448
449int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
450 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700451 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700452 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700453 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700454 SkPaint paint;
455 paint.setAlpha(alpha);
456 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700457 }
Romain Guyd55a8612010-06-28 17:42:46 -0700458}
Romain Guybd6b79b2010-06-26 00:13:53 -0700459
Romain Guy1c740bc2010-09-13 18:00:09 -0700460/**
461 * Layers are viewed by Skia are slightly different than layers in image editing
462 * programs (for instance.) When a layer is created, previously created layers
463 * and the frame buffer still receive every drawing command. For instance, if a
464 * layer is created and a shape intersecting the bounds of the layers and the
465 * framebuffer is draw, the shape will be drawn on both (unless the layer was
466 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
467 *
468 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
469 * texture. Unfortunately, this is inefficient as it requires every primitive to
470 * be drawn n + 1 times, where n is the number of active layers. In practice this
471 * means, for every primitive:
472 * - Switch active frame buffer
473 * - Change viewport, clip and projection matrix
474 * - Issue the drawing
475 *
476 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700477 * To avoid this, layers are implemented in a different way here, at least in the
478 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
479 * is set. When this flag is set we can redirect all drawing operations into a
480 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700481 *
482 * This implementation relies on the frame buffer being at least RGBA 8888. When
483 * a layer is created, only a texture is created, not an FBO. The content of the
484 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700485 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700486 * buffer and drawing continues as normal. This technique therefore treats the
487 * frame buffer as a scratch buffer for the layers.
488 *
489 * To compose the layers back onto the frame buffer, each layer texture
490 * (containing the original frame buffer data) is drawn as a simple quad over
491 * the frame buffer. The trick is that the quad is set as the composition
492 * destination in the blending equation, and the frame buffer becomes the source
493 * of the composition.
494 *
495 * Drawing layers with an alpha value requires an extra step before composition.
496 * An empty quad is drawn over the layer's region in the frame buffer. This quad
497 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
498 * quad is used to multiply the colors in the frame buffer. This is achieved by
499 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
500 * GL_ZERO, GL_SRC_ALPHA.
501 *
502 * Because glCopyTexImage2D() can be slow, an alternative implementation might
503 * be use to draw a single clipped layer. The implementation described above
504 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700505 *
506 * (1) The frame buffer is actually not cleared right away. To allow the GPU
507 * to potentially optimize series of calls to glCopyTexImage2D, the frame
508 * buffer is left untouched until the first drawing operation. Only when
509 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700510 */
Romain Guyd55a8612010-06-28 17:42:46 -0700511bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700512 float right, float bottom, int alpha, SkXfermode::Mode mode,
513 int flags, GLuint previousFbo) {
514 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700515 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700516
Romain Guyeb993562010-10-05 18:14:38 -0700517 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
518
Romain Guyf607bdc2010-09-10 19:20:06 -0700519 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700520 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700521 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700522 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700523
Romain Guyeb993562010-10-05 18:14:38 -0700524 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700525 if (bounds.intersect(*snapshot->clipRect)) {
526 // We cannot work with sub-pixels in this case
527 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700528
Romain Guyad37cd32011-03-15 11:12:25 -0700529 // When the layer is not an FBO, we may use glCopyTexImage so we
530 // need to make sure the layer does not extend outside the bounds
531 // of the framebuffer
532 if (!bounds.intersect(snapshot->previous->viewport)) {
533 bounds.setEmpty();
534 }
535 } else {
536 bounds.setEmpty();
537 }
Romain Guyeb993562010-10-05 18:14:38 -0700538 }
Romain Guybf434112010-09-16 14:40:17 -0700539
Romain Guy746b7402010-10-26 16:27:31 -0700540 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
541 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800542 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700543 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700544 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700545 }
546
547 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800548 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700549 return false;
550 }
Romain Guyf18fd992010-07-08 11:45:51 -0700551
Romain Guya1d3c912011-12-13 14:55:06 -0800552 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700553 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700554 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700555 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700556 }
557
Romain Guy9ace8f52011-07-07 20:50:11 -0700558 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700559 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700560 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
561 bounds.getWidth() / float(layer->getWidth()), 0.0f);
562 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700563 layer->setBlend(true);
Romain Guydda570202010-07-06 11:39:32 -0700564
Romain Guy8fb95422010-08-17 18:38:51 -0700565 // Save the layer in the snapshot
566 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700567 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700568
Romain Guyeb993562010-10-05 18:14:38 -0700569 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700570 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700571 } else {
572 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700573 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800574 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700575 if (layer->isEmpty()) {
576 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
577 bounds.left, snapshot->height - bounds.bottom,
578 layer->getWidth(), layer->getHeight(), 0);
579 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800580 } else {
581 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
582 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
583 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700584
Romain Guy54be1cd2011-06-13 19:04:27 -0700585 // Enqueue the buffer coordinates to clear the corresponding region later
586 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700587 }
Romain Guyeb993562010-10-05 18:14:38 -0700588 }
Romain Guyf86ef572010-07-01 11:05:42 -0700589
Romain Guyd55a8612010-06-28 17:42:46 -0700590 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700591}
592
Romain Guy5b3b3522010-10-27 18:57:51 -0700593bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
594 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700595 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700596
597#if RENDER_LAYERS_AS_REGIONS
598 snapshot->region = &snapshot->layer->region;
599 snapshot->flags |= Snapshot::kFlagFboTarget;
600#endif
601
602 Rect clip(bounds);
603 snapshot->transform->mapRect(clip);
604 clip.intersect(*snapshot->clipRect);
605 clip.snapToPixelBoundaries();
606 clip.intersect(snapshot->previous->viewport);
607
608 mat4 inverse;
609 inverse.loadInverse(*mSnapshot->transform);
610
611 inverse.mapRect(clip);
612 clip.snapToPixelBoundaries();
613 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700614 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700615
616 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700617 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700618 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700619 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
620 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
621 snapshot->height = bounds.getHeight();
622 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
623 snapshot->orthoMatrix.load(mOrthoMatrix);
624
625 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700626 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
627 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700628
629 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700630 if (layer->isEmpty()) {
631 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
632 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700633 }
634
635 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700636 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700637
638#if DEBUG_LAYERS_AS_REGIONS
639 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
640 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000641 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700642
643 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700644 layer->deleteTexture();
645 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700646
647 delete layer;
648
649 return false;
650 }
651#endif
652
653 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800654 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700655 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700656 glClear(GL_COLOR_BUFFER_BIT);
657
658 dirtyClip();
659
660 // Change the ortho projection
661 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
662 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
663
664 return true;
665}
666
Romain Guy1c740bc2010-09-13 18:00:09 -0700667/**
668 * Read the documentation of createLayer() before doing anything in this method.
669 */
Romain Guy1d83e192010-08-17 11:37:00 -0700670void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
671 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000672 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700673 return;
674 }
675
Romain Guy5b3b3522010-10-27 18:57:51 -0700676 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700677
678 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700679 // Detach the texture from the FBO
680 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
681
Romain Guyeb993562010-10-05 18:14:38 -0700682 // Unbind current FBO and restore previous one
683 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
684 }
685
Romain Guy1d83e192010-08-17 11:37:00 -0700686 Layer* layer = current->layer;
687 const Rect& rect = layer->layer;
688
Romain Guy9ace8f52011-07-07 20:50:11 -0700689 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700690 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700691 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700692 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700693 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700694 }
695
Romain Guy03750a02010-10-18 14:06:08 -0700696 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700697
Romain Guya1d3c912011-12-13 14:55:06 -0800698 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700699
Romain Guy5b3b3522010-10-27 18:57:51 -0700700 // When the layer is stored in an FBO, we can save a bit of fillrate by
701 // drawing only the dirty region
702 if (fboLayer) {
703 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700704 if (layer->getColorFilter()) {
705 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800706 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700707 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700708 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800709 resetColorFilter();
710 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700711 } else if (!rect.isEmpty()) {
712 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
713 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700714 }
Romain Guy8b55f372010-08-18 17:10:07 -0700715
Romain Guyeb993562010-10-05 18:14:38 -0700716 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800717 // Note: No need to use glDiscardFramebufferEXT() since we never
718 // create/compose layers that are not on screen with this
719 // code path
720 // See LayerRenderer::destroyLayer(Layer*)
721
Romain Guyeb993562010-10-05 18:14:38 -0700722 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
723 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700724 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700725 }
726
Romain Guy746b7402010-10-26 16:27:31 -0700727 dirtyClip();
728
Romain Guyeb993562010-10-05 18:14:38 -0700729 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700730 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700731 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700732 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700733 delete layer;
734 }
735}
736
Romain Guyaa6c24c2011-04-28 18:40:04 -0700737void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700738 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700739
Romain Guy302a9df2011-08-16 13:55:02 -0700740 mat4& transform = layer->getTransform();
741 if (!transform.isIdentity()) {
742 save(0);
743 mSnapshot->transform->multiply(transform);
744 }
745
Romain Guyaa6c24c2011-04-28 18:40:04 -0700746 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700747 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700748 setupDrawWithTexture();
749 } else {
750 setupDrawWithExternalTexture();
751 }
752 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700753 setupDrawColor(alpha, alpha, alpha, alpha);
754 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700755 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700756 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700757 setupDrawPureColorUniforms();
758 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700759 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
760 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700761 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700762 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700763 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700764 if (mSnapshot->transform->isPureTranslate() &&
765 layer->getWidth() == (uint32_t) rect.getWidth() &&
766 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700767 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
768 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
769
Romain Guyd21b6e12011-11-30 20:21:23 -0800770 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700771 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
772 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800773 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700774 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
775 }
776 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700777 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
778
779 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
780
781 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700782
783 if (!transform.isIdentity()) {
784 restore();
785 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700786}
787
Romain Guy5b3b3522010-10-27 18:57:51 -0700788void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700789 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700790 const Rect& texCoords = layer->texCoords;
791 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
792 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700793
Romain Guy9ace8f52011-07-07 20:50:11 -0700794 float x = rect.left;
795 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700796 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700797 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700798 layer->getHeight() == (uint32_t) rect.getHeight();
799
800 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700801 // When we're swapping, the layer is already in screen coordinates
802 if (!swap) {
803 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
804 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
805 }
806
Romain Guyd21b6e12011-11-30 20:21:23 -0800807 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700808 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800809 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700810 }
811
812 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
813 layer->getTexture(), layer->getAlpha() / 255.0f,
814 layer->getMode(), layer->isBlend(),
815 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
816 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700817
Romain Guyaa6c24c2011-04-28 18:40:04 -0700818 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
819 } else {
820 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
821 drawTextureLayer(layer, rect);
822 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
823 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700824}
825
826void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
827#if RENDER_LAYERS_AS_REGIONS
828 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700829 layer->setRegionAsRect();
830
Romain Guy40667672011-03-18 14:34:03 -0700831 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700832
Romain Guy5b3b3522010-10-27 18:57:51 -0700833 layer->region.clear();
834 return;
835 }
836
Romain Guy8a3957d2011-09-07 17:55:15 -0700837 // TODO: See LayerRenderer.cpp::generateMesh() for important
838 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800839 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700840 size_t count;
841 const android::Rect* rects = layer->region.getArray(&count);
842
Romain Guy9ace8f52011-07-07 20:50:11 -0700843 const float alpha = layer->getAlpha() / 255.0f;
844 const float texX = 1.0f / float(layer->getWidth());
845 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800846 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700847
848 TextureVertex* mesh = mCaches.getRegionMesh();
849 GLsizei numQuads = 0;
850
Romain Guy7230a742011-01-10 22:26:16 -0800851 setupDraw();
852 setupDrawWithTexture();
853 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800854 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700855 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800856 setupDrawProgram();
857 setupDrawDirtyRegionsDisabled();
858 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800859 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700860 setupDrawTexture(layer->getTexture());
861 if (mSnapshot->transform->isPureTranslate()) {
862 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
863 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
864
Romain Guyd21b6e12011-11-30 20:21:23 -0800865 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700866 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
867 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800868 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700869 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
870 }
Romain Guy15bc6432011-12-13 13:11:32 -0800871 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700872
873 for (size_t i = 0; i < count; i++) {
874 const android::Rect* r = &rects[i];
875
876 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800877 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700878 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800879 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700880
881 // TODO: Reject quads outside of the clip
882 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
883 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
884 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
885 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
886
887 numQuads++;
888
889 if (numQuads >= REGION_MESH_QUAD_COUNT) {
890 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
891 numQuads = 0;
892 mesh = mCaches.getRegionMesh();
893 }
894 }
895
896 if (numQuads > 0) {
897 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
898 }
899
Romain Guy7230a742011-01-10 22:26:16 -0800900 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700901
902#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800903 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700904#endif
905
906 layer->region.clear();
907 }
908#else
909 composeLayerRect(layer, rect);
910#endif
911}
912
Romain Guy3a3133d2011-02-01 22:59:58 -0800913void OpenGLRenderer::drawRegionRects(const Region& region) {
914#if DEBUG_LAYERS_AS_REGIONS
915 size_t count;
916 const android::Rect* rects = region.getArray(&count);
917
918 uint32_t colors[] = {
919 0x7fff0000, 0x7f00ff00,
920 0x7f0000ff, 0x7fff00ff,
921 };
922
923 int offset = 0;
924 int32_t top = rects[0].top;
925
926 for (size_t i = 0; i < count; i++) {
927 if (top != rects[i].top) {
928 offset ^= 0x2;
929 top = rects[i].top;
930 }
931
932 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
933 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
934 SkXfermode::kSrcOver_Mode);
935 }
936#endif
937}
938
Romain Guy5b3b3522010-10-27 18:57:51 -0700939void OpenGLRenderer::dirtyLayer(const float left, const float top,
940 const float right, const float bottom, const mat4 transform) {
941#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800942 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700943 Rect bounds(left, top, right, bottom);
944 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800945 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700946 }
947#endif
948}
949
950void OpenGLRenderer::dirtyLayer(const float left, const float top,
951 const float right, const float bottom) {
952#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800953 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700954 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800955 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800956 }
957#endif
958}
959
960void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
961#if RENDER_LAYERS_AS_REGIONS
962 if (bounds.intersect(*mSnapshot->clipRect)) {
963 bounds.snapToPixelBoundaries();
964 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
965 if (!dirty.isEmpty()) {
966 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700967 }
968 }
969#endif
970}
971
Romain Guy54be1cd2011-06-13 19:04:27 -0700972void OpenGLRenderer::clearLayerRegions() {
973 const size_t count = mLayers.size();
974 if (count == 0) return;
975
976 if (!mSnapshot->isIgnored()) {
977 // Doing several glScissor/glClear here can negatively impact
978 // GPUs with a tiler architecture, instead we draw quads with
979 // the Clear blending mode
980
981 // The list contains bounds that have already been clipped
982 // against their initial clip rect, and the current clip
983 // is likely different so we need to disable clipping here
984 glDisable(GL_SCISSOR_TEST);
985
986 Vertex mesh[count * 6];
987 Vertex* vertex = mesh;
988
989 for (uint32_t i = 0; i < count; i++) {
990 Rect* bounds = mLayers.itemAt(i);
991
992 Vertex::set(vertex++, bounds->left, bounds->bottom);
993 Vertex::set(vertex++, bounds->left, bounds->top);
994 Vertex::set(vertex++, bounds->right, bounds->top);
995 Vertex::set(vertex++, bounds->left, bounds->bottom);
996 Vertex::set(vertex++, bounds->right, bounds->top);
997 Vertex::set(vertex++, bounds->right, bounds->bottom);
998
999 delete bounds;
1000 }
1001
1002 setupDraw(false);
1003 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1004 setupDrawBlending(true, SkXfermode::kClear_Mode);
1005 setupDrawProgram();
1006 setupDrawPureColorUniforms();
1007 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001008 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001009
Romain Guy54be1cd2011-06-13 19:04:27 -07001010 glDrawArrays(GL_TRIANGLES, 0, count * 6);
1011
1012 glEnable(GL_SCISSOR_TEST);
1013 } else {
1014 for (uint32_t i = 0; i < count; i++) {
1015 delete mLayers.itemAt(i);
1016 }
1017 }
1018
1019 mLayers.clear();
1020}
1021
Romain Guybd6b79b2010-06-26 00:13:53 -07001022///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001023// Transforms
1024///////////////////////////////////////////////////////////////////////////////
1025
1026void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001027 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001028}
1029
1030void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001031 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001032}
1033
1034void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001035 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001036}
1037
Romain Guy807daf72011-01-18 11:19:19 -08001038void OpenGLRenderer::skew(float sx, float sy) {
1039 mSnapshot->transform->skew(sx, sy);
1040}
1041
Romain Guyf6a11b82010-06-23 17:47:49 -07001042void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001043 if (matrix) {
1044 mSnapshot->transform->load(*matrix);
1045 } else {
1046 mSnapshot->transform->loadIdentity();
1047 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001048}
1049
1050void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001051 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001052}
1053
1054void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001055 SkMatrix transform;
1056 mSnapshot->transform->copyTo(transform);
1057 transform.preConcat(*matrix);
1058 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001059}
1060
1061///////////////////////////////////////////////////////////////////////////////
1062// Clipping
1063///////////////////////////////////////////////////////////////////////////////
1064
Romain Guybb9524b2010-06-22 18:56:38 -07001065void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001066 Rect clip(*mSnapshot->clipRect);
1067 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001068
1069 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1070 clip.getWidth(), clip.getHeight());
1071
Romain Guy746b7402010-10-26 16:27:31 -07001072 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -07001073}
1074
1075const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001076 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001077}
1078
Romain Guyc7d53492010-06-25 13:41:57 -07001079bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001080 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001081 return true;
1082 }
1083
Romain Guy1d83e192010-08-17 11:37:00 -07001084 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001085 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001086 r.snapToPixelBoundaries();
1087
1088 Rect clipRect(*mSnapshot->clipRect);
1089 clipRect.snapToPixelBoundaries();
1090
1091 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001092}
1093
Romain Guy079ba2c2010-07-16 14:12:24 -07001094bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1095 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001096 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001097 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001098 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001099 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001100}
1101
Chet Haasea23eed82012-04-12 15:19:04 -07001102Rect* OpenGLRenderer::getClipRect() {
1103 return mSnapshot->clipRect;
1104}
1105
Romain Guyf6a11b82010-06-23 17:47:49 -07001106///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001107// Drawing commands
1108///////////////////////////////////////////////////////////////////////////////
1109
Romain Guy54be1cd2011-06-13 19:04:27 -07001110void OpenGLRenderer::setupDraw(bool clear) {
1111 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001112 if (mDirtyClip) {
1113 setScissorFromClip();
1114 }
1115 mDescription.reset();
1116 mSetShaderColor = false;
1117 mColorSet = false;
1118 mColorA = mColorR = mColorG = mColorB = 0.0f;
1119 mTextureUnit = 0;
1120 mTrackDirtyRegions = true;
1121}
1122
1123void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1124 mDescription.hasTexture = true;
1125 mDescription.hasAlpha8Texture = isAlpha8;
1126}
1127
Romain Guyaa6c24c2011-04-28 18:40:04 -07001128void OpenGLRenderer::setupDrawWithExternalTexture() {
1129 mDescription.hasExternalTexture = true;
1130}
1131
Romain Guy15bc6432011-12-13 13:11:32 -08001132void OpenGLRenderer::setupDrawNoTexture() {
1133 mCaches.disbaleTexCoordsVertexArray();
1134}
1135
Chet Haase5b0200b2011-04-13 17:58:08 -07001136void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001137 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001138}
1139
Romain Guyed6fcb02011-03-21 13:11:28 -07001140void OpenGLRenderer::setupDrawPoint(float pointSize) {
1141 mDescription.isPoint = true;
1142 mDescription.pointSize = pointSize;
1143}
1144
Romain Guy70ca14e2010-12-13 18:24:33 -08001145void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001146 setupDrawColor(color, (color >> 24) & 0xFF);
1147}
1148
1149void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1150 mColorA = alpha / 255.0f;
Chet Haasedb8c9a62012-03-21 18:54:18 -07001151 mColorA *= mSnapshot->alpha;
Chet Haase6cfdf452011-04-22 16:42:10 -07001152 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1153 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001154 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001155 mColorR = a * ((color >> 16) & 0xFF);
1156 mColorG = a * ((color >> 8) & 0xFF);
1157 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001158 mColorSet = true;
1159 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1160}
1161
Romain Guy86568192010-12-14 15:55:39 -08001162void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1163 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001164 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1165 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001166 const float a = mColorA / 255.0f;
1167 mColorR = a * ((color >> 16) & 0xFF);
1168 mColorG = a * ((color >> 8) & 0xFF);
1169 mColorB = a * ((color ) & 0xFF);
1170 mColorSet = true;
1171 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1172}
1173
Romain Guy70ca14e2010-12-13 18:24:33 -08001174void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1175 mColorA = a;
1176 mColorR = r;
1177 mColorG = g;
1178 mColorB = b;
1179 mColorSet = true;
1180 mSetShaderColor = mDescription.setColor(r, g, b, a);
1181}
1182
Romain Guy86568192010-12-14 15:55:39 -08001183void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1184 mColorA = a;
1185 mColorR = r;
1186 mColorG = g;
1187 mColorB = b;
1188 mColorSet = true;
1189 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1190}
1191
Romain Guy70ca14e2010-12-13 18:24:33 -08001192void OpenGLRenderer::setupDrawShader() {
1193 if (mShader) {
1194 mShader->describe(mDescription, mCaches.extensions);
1195 }
1196}
1197
1198void OpenGLRenderer::setupDrawColorFilter() {
1199 if (mColorFilter) {
1200 mColorFilter->describe(mDescription, mCaches.extensions);
1201 }
1202}
1203
Romain Guyf09ef512011-05-27 11:43:46 -07001204void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1205 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1206 mColorA = 1.0f;
1207 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001208 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001209 }
1210}
1211
Romain Guy70ca14e2010-12-13 18:24:33 -08001212void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001213 // When the blending mode is kClear_Mode, we need to use a modulate color
1214 // argb=1,0,0,0
1215 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001216 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1217 mDescription, swapSrcDst);
1218}
1219
1220void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001221 // When the blending mode is kClear_Mode, we need to use a modulate color
1222 // argb=1,0,0,0
1223 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001224 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1225 mDescription, swapSrcDst);
1226}
1227
1228void OpenGLRenderer::setupDrawProgram() {
1229 useProgram(mCaches.programCache.get(mDescription));
1230}
1231
1232void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1233 mTrackDirtyRegions = false;
1234}
1235
1236void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1237 bool ignoreTransform) {
1238 mModelView.loadTranslate(left, top, 0.0f);
1239 if (!ignoreTransform) {
1240 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1241 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1242 } else {
1243 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1244 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1245 }
1246}
1247
Chet Haase8a5cc922011-04-26 07:28:09 -07001248void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1249 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001250}
1251
Romain Guy70ca14e2010-12-13 18:24:33 -08001252void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1253 bool ignoreTransform, bool ignoreModelView) {
1254 if (!ignoreModelView) {
1255 mModelView.loadTranslate(left, top, 0.0f);
1256 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001257 } else {
1258 mModelView.loadIdentity();
1259 }
Romain Guy86568192010-12-14 15:55:39 -08001260 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1261 if (!ignoreTransform) {
1262 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1263 if (mTrackDirtyRegions && dirty) {
1264 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1265 }
1266 } else {
1267 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1268 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1269 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001270}
1271
Romain Guyed6fcb02011-03-21 13:11:28 -07001272void OpenGLRenderer::setupDrawPointUniforms() {
1273 int slot = mCaches.currentProgram->getUniform("pointSize");
1274 glUniform1f(slot, mDescription.pointSize);
1275}
1276
Romain Guy70ca14e2010-12-13 18:24:33 -08001277void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001278 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001279 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1280 }
1281}
1282
Romain Guy86568192010-12-14 15:55:39 -08001283void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001284 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001285 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001286 }
1287}
1288
Romain Guy70ca14e2010-12-13 18:24:33 -08001289void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1290 if (mShader) {
1291 if (ignoreTransform) {
1292 mModelView.loadInverse(*mSnapshot->transform);
1293 }
1294 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1295 }
1296}
1297
Romain Guy8d0d4782010-12-14 20:13:35 -08001298void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1299 if (mShader) {
1300 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1301 }
1302}
1303
Romain Guy70ca14e2010-12-13 18:24:33 -08001304void OpenGLRenderer::setupDrawColorFilterUniforms() {
1305 if (mColorFilter) {
1306 mColorFilter->setupProgram(mCaches.currentProgram);
1307 }
1308}
1309
1310void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001311 bool force = mCaches.bindMeshBuffer();
1312 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001313 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001314}
1315
1316void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1317 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001318 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001319 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001320}
1321
Romain Guyaa6c24c2011-04-28 18:40:04 -07001322void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1323 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001324 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001325 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001326}
1327
Romain Guy8f0095c2011-05-02 17:24:22 -07001328void OpenGLRenderer::setupDrawTextureTransform() {
1329 mDescription.hasTextureTransform = true;
1330}
1331
1332void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001333 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1334 GL_FALSE, &transform.data[0]);
1335}
1336
Romain Guy70ca14e2010-12-13 18:24:33 -08001337void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001338 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001339 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001340 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001341 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001342 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001343 }
Romain Guyd71dd362011-12-12 19:03:35 -08001344
Romain Guyf3a910b42011-12-12 20:35:21 -08001345 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001346 if (mCaches.currentProgram->texCoords >= 0) {
1347 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1348 }
1349
1350 mCaches.unbindIndicesBuffer();
1351}
1352
1353void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1354 bool force = mCaches.unbindMeshBuffer();
1355 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1356 if (mCaches.currentProgram->texCoords >= 0) {
1357 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001358 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001359}
1360
Chet Haase5b0200b2011-04-13 17:58:08 -07001361void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001362 bool force = mCaches.unbindMeshBuffer();
1363 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1364 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001365 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001366}
1367
1368/**
1369 * 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 -07001370 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1371 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1372 * attributes (one per vertex) are values from zero to one that tells the fragment
1373 * shader where the fragment is in relation to the line width/length overall; these values are
1374 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1375 * region of the line.
1376 * Note that we only pass down the width values in this setup function. The length coordinates
1377 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001378 */
Chet Haase99585ad2011-05-02 15:00:16 -07001379void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001380 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001381 bool force = mCaches.unbindMeshBuffer();
1382 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1383 vertices, gAAVertexStride);
1384 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001385 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001386
Romain Guy7b631422012-04-04 11:38:54 -07001387 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001388 glEnableVertexAttribArray(widthSlot);
1389 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001390
Romain Guy7b631422012-04-04 11:38:54 -07001391 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001392 glEnableVertexAttribArray(lengthSlot);
1393 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001394
Chet Haase5b0200b2011-04-13 17:58:08 -07001395 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001396 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001397
Chet Haase99585ad2011-05-02 15:00:16 -07001398 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001399 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Romain Guy7b631422012-04-04 11:38:54 -07001400 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion);
1401}
1402
1403void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1404 glDisableVertexAttribArray(widthSlot);
1405 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001406}
1407
Romain Guy70ca14e2010-12-13 18:24:33 -08001408void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001409}
1410
1411///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001412// Drawing
1413///////////////////////////////////////////////////////////////////////////////
1414
Chet Haase1271e2c2012-04-20 09:54:27 -07001415status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001416 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001417
Romain Guy0fe478e2010-11-08 12:08:41 -08001418 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1419 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001420 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001421 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001422 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001423
Romain Guy65549432012-03-26 16:45:05 -07001424 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001425}
1426
Chet Haaseed30fd82011-04-22 16:18:45 -07001427void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1428 if (displayList) {
1429 displayList->output(*this, level);
1430 }
1431}
1432
Romain Guya168d732011-03-18 16:50:13 -07001433void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1434 int alpha;
1435 SkXfermode::Mode mode;
1436 getAlphaAndMode(paint, &alpha, &mode);
1437
Romain Guya168d732011-03-18 16:50:13 -07001438 float x = left;
1439 float y = top;
1440
Romain Guye3c26852011-07-25 16:36:01 -07001441 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001442 bool ignoreTransform = false;
1443 if (mSnapshot->transform->isPureTranslate()) {
1444 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1445 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1446 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001447 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001448 } else {
1449 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001450 }
1451
1452 setupDraw();
1453 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001454 if (paint) {
1455 setupDrawAlpha8Color(paint->getColor(), alpha);
1456 }
Romain Guya168d732011-03-18 16:50:13 -07001457 setupDrawColorFilter();
1458 setupDrawShader();
1459 setupDrawBlending(true, mode);
1460 setupDrawProgram();
1461 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001462
Romain Guya168d732011-03-18 16:50:13 -07001463 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001464 texture->setWrap(GL_CLAMP_TO_EDGE);
1465 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001466
Romain Guya168d732011-03-18 16:50:13 -07001467 setupDrawPureColorUniforms();
1468 setupDrawColorFilterUniforms();
1469 setupDrawShaderUniforms();
1470 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1471
1472 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1473
1474 finishDrawTexture();
1475}
1476
Chet Haase48659092012-05-31 15:21:51 -07001477status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001478 const float right = left + bitmap->width();
1479 const float bottom = top + bitmap->height();
1480
1481 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001482 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001483 }
1484
Romain Guya1d3c912011-12-13 14:55:06 -08001485 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001486 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001487 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001488 const AutoTexture autoCleanup(texture);
1489
Romain Guy211370f2012-02-01 16:10:55 -08001490 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001491 drawAlphaBitmap(texture, left, top, paint);
1492 } else {
1493 drawTextureRect(left, top, right, bottom, texture, paint);
1494 }
Chet Haase48659092012-05-31 15:21:51 -07001495
1496 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001497}
1498
Chet Haase48659092012-05-31 15:21:51 -07001499status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001500 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1501 const mat4 transform(*matrix);
1502 transform.mapRect(r);
1503
Romain Guy6926c722010-07-12 20:20:03 -07001504 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001505 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001506 }
1507
Romain Guya1d3c912011-12-13 14:55:06 -08001508 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001509 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001510 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001511 const AutoTexture autoCleanup(texture);
1512
Romain Guy5b3b3522010-10-27 18:57:51 -07001513 // This could be done in a cheaper way, all we need is pass the matrix
1514 // to the vertex shader. The save/restore is a bit overkill.
1515 save(SkCanvas::kMatrix_SaveFlag);
1516 concatMatrix(matrix);
1517 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1518 restore();
Chet Haase48659092012-05-31 15:21:51 -07001519
1520 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001521}
1522
Chet Haase48659092012-05-31 15:21:51 -07001523status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001524 const float right = left + bitmap->width();
1525 const float bottom = top + bitmap->height();
1526
1527 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001528 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001529 }
1530
1531 mCaches.activeTexture(0);
1532 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1533 const AutoTexture autoCleanup(texture);
1534
1535 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001536
1537 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001538}
1539
Chet Haase48659092012-05-31 15:21:51 -07001540status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001541 float* vertices, int* colors, SkPaint* paint) {
1542 // TODO: Do a quickReject
1543 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001544 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001545 }
1546
Romain Guya1d3c912011-12-13 14:55:06 -08001547 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001548 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001549 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001550 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001551
Romain Guyd21b6e12011-11-30 20:21:23 -08001552 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1553 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001554
1555 int alpha;
1556 SkXfermode::Mode mode;
1557 getAlphaAndMode(paint, &alpha, &mode);
1558
Romain Guy5a7b4662011-01-20 19:09:30 -08001559 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001560
Romain Guyb18d2d02011-02-10 15:52:54 -08001561 float left = FLT_MAX;
1562 float top = FLT_MAX;
1563 float right = FLT_MIN;
1564 float bottom = FLT_MIN;
1565
1566#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001567 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001568#else
Romain Guy211370f2012-02-01 16:10:55 -08001569 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001570#endif
1571
Romain Guya566b7c2011-01-23 16:36:11 -08001572 // TODO: Support the colors array
1573 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001574 TextureVertex* vertex = mesh;
1575 for (int32_t y = 0; y < meshHeight; y++) {
1576 for (int32_t x = 0; x < meshWidth; x++) {
1577 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1578
1579 float u1 = float(x) / meshWidth;
1580 float u2 = float(x + 1) / meshWidth;
1581 float v1 = float(y) / meshHeight;
1582 float v2 = float(y + 1) / meshHeight;
1583
1584 int ax = i + (meshWidth + 1) * 2;
1585 int ay = ax + 1;
1586 int bx = i;
1587 int by = bx + 1;
1588 int cx = i + 2;
1589 int cy = cx + 1;
1590 int dx = i + (meshWidth + 1) * 2 + 2;
1591 int dy = dx + 1;
1592
1593 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1594 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1595 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1596
1597 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1598 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1599 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001600
1601#if RENDER_LAYERS_AS_REGIONS
1602 if (hasActiveLayer) {
1603 // TODO: This could be optimized to avoid unnecessary ops
1604 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1605 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1606 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1607 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1608 }
1609#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001610 }
1611 }
1612
Romain Guyb18d2d02011-02-10 15:52:54 -08001613#if RENDER_LAYERS_AS_REGIONS
1614 if (hasActiveLayer) {
1615 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1616 }
1617#endif
1618
Romain Guy5a7b4662011-01-20 19:09:30 -08001619 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1620 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001621 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001622
1623 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001624}
1625
Chet Haase48659092012-05-31 15:21:51 -07001626status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001627 float srcLeft, float srcTop, float srcRight, float srcBottom,
1628 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001629 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001630 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001631 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001632 }
1633
Romain Guya1d3c912011-12-13 14:55:06 -08001634 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001635 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001636 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001637 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001638
Romain Guy8ba548f2010-06-30 19:21:21 -07001639 const float width = texture->width;
1640 const float height = texture->height;
1641
Romain Guy68169722011-08-22 17:33:33 -07001642 const float u1 = fmax(0.0f, srcLeft / width);
1643 const float v1 = fmax(0.0f, srcTop / height);
1644 const float u2 = fmin(1.0f, srcRight / width);
1645 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001646
Romain Guy03750a02010-10-18 14:06:08 -07001647 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001648 resetDrawTextureTexCoords(u1, v1, u2, v2);
1649
Romain Guy03750a02010-10-18 14:06:08 -07001650 int alpha;
1651 SkXfermode::Mode mode;
1652 getAlphaAndMode(paint, &alpha, &mode);
1653
Romain Guyd21b6e12011-11-30 20:21:23 -08001654 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1655
Romain Guy211370f2012-02-01 16:10:55 -08001656 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001657 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1658 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1659
Romain Guyb5014982011-07-28 15:39:12 -07001660 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001661 // Enable linear filtering if the source rectangle is scaled
1662 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001663 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001664 }
Romain Guyb5014982011-07-28 15:39:12 -07001665
Romain Guyd21b6e12011-11-30 20:21:23 -08001666 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001667 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1668 texture->id, alpha / 255.0f, mode, texture->blend,
1669 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1670 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1671 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001672 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001673 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1674 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1675 GL_TRIANGLE_STRIP, gMeshCount);
1676 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001677
1678 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001679
1680 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001681}
1682
Chet Haase48659092012-05-31 15:21:51 -07001683status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001684 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001685 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001686 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001687 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001688 }
1689
Romain Guya1d3c912011-12-13 14:55:06 -08001690 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001691 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001692 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001693 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001694 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1695 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001696
1697 int alpha;
1698 SkXfermode::Mode mode;
1699 getAlphaAndMode(paint, &alpha, &mode);
1700
Romain Guy2728f962010-10-08 18:36:15 -07001701 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001702 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001703
Romain Guy211370f2012-02-01 16:10:55 -08001704 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001705 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001706#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001707 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001708 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001709 const float offsetX = left + mSnapshot->transform->getTranslateX();
1710 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001711 const size_t count = mesh->quads.size();
1712 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001713 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001714 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001715 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1716 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1717 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001718 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001719 dirtyLayer(left + bounds.left, top + bounds.top,
1720 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001721 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001722 }
1723 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001724#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001725
Romain Guy211370f2012-02-01 16:10:55 -08001726 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001727 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1728 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1729
1730 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1731 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1732 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1733 true, !mesh->hasEmptyQuads);
1734 } else {
1735 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1736 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1737 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1738 true, !mesh->hasEmptyQuads);
1739 }
Romain Guy054dc182010-10-15 17:55:25 -07001740 }
Chet Haase48659092012-05-31 15:21:51 -07001741
1742 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001743}
1744
Chet Haase99ecdc42011-05-06 12:06:34 -07001745/**
Chet Haase858aa932011-05-12 09:06:00 -07001746 * This function uses a similar approach to that of AA lines in the drawLines() function.
1747 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1748 * shader to compute the translucency of the color, determined by whether a given pixel is
1749 * within that boundary region and how far into the region it is.
1750 */
1751void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001752 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001753 float inverseScaleX = 1.0f;
1754 float inverseScaleY = 1.0f;
1755 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001756 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001757 Matrix4 *mat = mSnapshot->transform;
1758 float m00 = mat->data[Matrix4::kScaleX];
1759 float m01 = mat->data[Matrix4::kSkewY];
1760 float m02 = mat->data[2];
1761 float m10 = mat->data[Matrix4::kSkewX];
1762 float m11 = mat->data[Matrix4::kScaleX];
1763 float m12 = mat->data[6];
1764 float scaleX = sqrt(m00 * m00 + m01 * m01);
1765 float scaleY = sqrt(m10 * m10 + m11 * m11);
1766 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1767 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1768 }
1769
1770 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001771 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001772 setupDrawAALine();
1773 setupDrawColor(color);
1774 setupDrawColorFilter();
1775 setupDrawShader();
1776 setupDrawBlending(true, mode);
1777 setupDrawProgram();
1778 setupDrawModelViewIdentity(true);
1779 setupDrawColorUniforms();
1780 setupDrawColorFilterUniforms();
1781 setupDrawShaderIdentityUniforms();
1782
1783 AAVertex rects[4];
1784 AAVertex* aaVertices = &rects[0];
1785 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1786 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1787
1788 float boundarySizeX = .5 * inverseScaleX;
1789 float boundarySizeY = .5 * inverseScaleY;
1790
1791 // Adjust the rect by the AA boundary padding
1792 left -= boundarySizeX;
1793 right += boundarySizeX;
1794 top -= boundarySizeY;
1795 bottom += boundarySizeY;
1796
1797 float width = right - left;
1798 float height = bottom - top;
1799
Romain Guy7b631422012-04-04 11:38:54 -07001800 int widthSlot;
1801 int lengthSlot;
1802
Chet Haase858aa932011-05-12 09:06:00 -07001803 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1804 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001805 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1806 boundaryWidthProportion, widthSlot, lengthSlot);
1807
Chet Haase858aa932011-05-12 09:06:00 -07001808 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1809 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1810 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001811 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion));
Chet Haase858aa932011-05-12 09:06:00 -07001812
1813 if (!quickReject(left, top, right, bottom)) {
1814 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1815 AAVertex::set(aaVertices++, left, top, 1, 0);
1816 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1817 AAVertex::set(aaVertices++, right, top, 0, 0);
1818 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1819 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1820 }
Romain Guy7b631422012-04-04 11:38:54 -07001821
1822 finishDrawAALine(widthSlot, lengthSlot);
Chet Haase858aa932011-05-12 09:06:00 -07001823}
1824
1825/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001826 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1827 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1828 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1829 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1830 * of the line. Hairlines are more involved because we need to account for transform scaling
1831 * to end up with a one-pixel-wide line in screen space..
1832 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1833 * in combination with values that we calculate and pass down in this method. The basic approach
1834 * is that the quad we create contains both the core line area plus a bounding area in which
1835 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1836 * proportion of the width and the length of a given segment is represented by the boundary
1837 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1838 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1839 * on the inside). This ends up giving the result we want, with pixels that are completely
1840 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1841 * how far into the boundary region they are, which is determined by shader interpolation.
1842 */
Chet Haase48659092012-05-31 15:21:51 -07001843status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1844 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001845
1846 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001847 // We use half the stroke width here because we're going to position the quad
1848 // corner vertices half of the width away from the line endpoints
1849 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001850 // A stroke width of 0 has a special meaning in Skia:
1851 // it draws a line 1 px wide regardless of current transform
1852 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001853
Chet Haase99ecdc42011-05-06 12:06:34 -07001854 float inverseScaleX = 1.0f;
1855 float inverseScaleY = 1.0f;
1856 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001857
Chet Haase8a5cc922011-04-26 07:28:09 -07001858 int alpha;
1859 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001860
Chet Haase8a5cc922011-04-26 07:28:09 -07001861 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001862 int verticesCount = count;
1863 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001864 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001865 verticesCount += (count - 4);
1866 }
1867
1868 if (isHairLine || isAA) {
1869 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1870 // the line on the screen should always be one pixel wide regardless of scale. For
1871 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001872 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001873 Matrix4 *mat = mSnapshot->transform;
1874 float m00 = mat->data[Matrix4::kScaleX];
1875 float m01 = mat->data[Matrix4::kSkewY];
1876 float m02 = mat->data[2];
1877 float m10 = mat->data[Matrix4::kSkewX];
1878 float m11 = mat->data[Matrix4::kScaleX];
1879 float m12 = mat->data[6];
Romain Guy7b631422012-04-04 11:38:54 -07001880
Romain Guy211370f2012-02-01 16:10:55 -08001881 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1882 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001883
Chet Haase99ecdc42011-05-06 12:06:34 -07001884 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1885 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001886
Chet Haase99ecdc42011-05-06 12:06:34 -07001887 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1888 scaled = true;
1889 }
1890 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001891 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001892
1893 getAlphaAndMode(paint, &alpha, &mode);
1894 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001895 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001896 if (isAA) {
1897 setupDrawAALine();
1898 }
1899 setupDrawColor(paint->getColor(), alpha);
1900 setupDrawColorFilter();
1901 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001902 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001903 setupDrawProgram();
1904 setupDrawModelViewIdentity(true);
1905 setupDrawColorUniforms();
1906 setupDrawColorFilterUniforms();
1907 setupDrawShaderIdentityUniforms();
1908
1909 if (isHairLine) {
1910 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001911 halfStrokeWidth = isAA? 1 : .5;
1912 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001913 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001914 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001915 }
Romain Guy7b631422012-04-04 11:38:54 -07001916
1917 int widthSlot;
1918 int lengthSlot;
1919
Chet Haase5b0200b2011-04-13 17:58:08 -07001920 Vertex lines[verticesCount];
1921 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001922
Chet Haase99585ad2011-05-02 15:00:16 -07001923 AAVertex wLines[verticesCount];
1924 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001925
Romain Guy211370f2012-02-01 16:10:55 -08001926 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001927 setupDrawVertices(vertices);
1928 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001929 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1930 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001931 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001932 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1933 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001934 // We will need to calculate the actual width proportion on each segment for
1935 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1936 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001937 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1938 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001939 }
1940
Chet Haase99ecdc42011-05-06 12:06:34 -07001941 AAVertex* prevAAVertex = NULL;
1942 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001943
Chet Haase99585ad2011-05-02 15:00:16 -07001944 int boundaryLengthSlot = -1;
1945 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001946 int boundaryWidthSlot = -1;
1947 int inverseBoundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001948
Chet Haase5b0200b2011-04-13 17:58:08 -07001949 for (int i = 0; i < count; i += 4) {
1950 // a = start point, b = end point
1951 vec2 a(points[i], points[i + 1]);
1952 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001953
Chet Haase99585ad2011-05-02 15:00:16 -07001954 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001955 float boundaryLengthProportion = 0;
1956 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001957
Chet Haase5b0200b2011-04-13 17:58:08 -07001958 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001959 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001960 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001961 if (isAA) {
1962 float wideningFactor;
1963 if (fabs(n.x) >= fabs(n.y)) {
1964 wideningFactor = fabs(1.0f / n.x);
1965 } else {
1966 wideningFactor = fabs(1.0f / n.y);
1967 }
1968 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001969 }
Romain Guy7b631422012-04-04 11:38:54 -07001970
Chet Haase99ecdc42011-05-06 12:06:34 -07001971 if (scaled) {
1972 n.x *= inverseScaleX;
1973 n.y *= inverseScaleY;
1974 }
1975 } else if (scaled) {
1976 // Extend n by .5 pixel on each side, post-transform
1977 vec2 extendedN = n.copyNormalized();
1978 extendedN /= 2;
1979 extendedN.x *= inverseScaleX;
1980 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07001981
Chet Haase99ecdc42011-05-06 12:06:34 -07001982 float extendedNLength = extendedN.length();
1983 // We need to set this value on the shader prior to drawing
1984 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1985 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001986 }
Romain Guy7b631422012-04-04 11:38:54 -07001987
Chet Haase5b0200b2011-04-13 17:58:08 -07001988 float x = n.x;
1989 n.x = -n.y;
1990 n.y = x;
1991
Chet Haase99585ad2011-05-02 15:00:16 -07001992 // aa lines expand the endpoint vertices to encompass the AA boundary
1993 if (isAA) {
1994 vec2 abVector = (b - a);
1995 length = abVector.length();
1996 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07001997
Chet Haase99ecdc42011-05-06 12:06:34 -07001998 if (scaled) {
1999 abVector.x *= inverseScaleX;
2000 abVector.y *= inverseScaleY;
2001 float abLength = abVector.length();
2002 boundaryLengthProportion = abLength / (length + abLength);
2003 } else {
2004 boundaryLengthProportion = .5 / (length + 1);
2005 }
Romain Guy7b631422012-04-04 11:38:54 -07002006
Chet Haase99ecdc42011-05-06 12:06:34 -07002007 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002008 a -= abVector;
2009 b += abVector;
2010 }
2011
Chet Haase5b0200b2011-04-13 17:58:08 -07002012 // Four corners of the rectangle defining a thick line
2013 vec2 p1 = a - n;
2014 vec2 p2 = a + n;
2015 vec2 p3 = b + n;
2016 vec2 p4 = b - n;
2017
Chet Haase99585ad2011-05-02 15:00:16 -07002018
Chet Haase5b0200b2011-04-13 17:58:08 -07002019 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2020 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2021 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2022 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2023
2024 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002025 if (!isAA) {
2026 if (prevVertex != NULL) {
2027 // Issue two repeat vertices to create degenerate triangles to bridge
2028 // between the previous line and the new one. This is necessary because
2029 // we are creating a single triangle_strip which will contain
2030 // potentially discontinuous line segments.
2031 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2032 Vertex::set(vertices++, p1.x, p1.y);
2033 generatedVerticesCount += 2;
2034 }
Romain Guy7b631422012-04-04 11:38:54 -07002035
Chet Haase5b0200b2011-04-13 17:58:08 -07002036 Vertex::set(vertices++, p1.x, p1.y);
2037 Vertex::set(vertices++, p2.x, p2.y);
2038 Vertex::set(vertices++, p4.x, p4.y);
2039 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002040
Chet Haase5b0200b2011-04-13 17:58:08 -07002041 prevVertex = vertices - 1;
2042 generatedVerticesCount += 4;
2043 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002044 if (!isHairLine && scaled) {
2045 // Must set width proportions per-segment for scaled non-hairlines to use the
2046 // correct AA boundary dimensions
2047 if (boundaryWidthSlot < 0) {
2048 boundaryWidthSlot =
2049 mCaches.currentProgram->getUniform("boundaryWidth");
2050 inverseBoundaryWidthSlot =
2051 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
2052 }
Romain Guy7b631422012-04-04 11:38:54 -07002053
Chet Haase99ecdc42011-05-06 12:06:34 -07002054 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
2055 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
2056 }
Romain Guy7b631422012-04-04 11:38:54 -07002057
Chet Haase99585ad2011-05-02 15:00:16 -07002058 if (boundaryLengthSlot < 0) {
2059 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
2060 inverseBoundaryLengthSlot =
2061 mCaches.currentProgram->getUniform("inverseBoundaryLength");
2062 }
Romain Guy7b631422012-04-04 11:38:54 -07002063
Chet Haase99ecdc42011-05-06 12:06:34 -07002064 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
2065 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07002066
Chet Haase5b0200b2011-04-13 17:58:08 -07002067 if (prevAAVertex != NULL) {
2068 // Issue two repeat vertices to create degenerate triangles to bridge
2069 // between the previous line and the new one. This is necessary because
2070 // we are creating a single triangle_strip which will contain
2071 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002072 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2073 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2074 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002075 generatedVerticesCount += 2;
2076 }
Romain Guy7b631422012-04-04 11:38:54 -07002077
Chet Haase99585ad2011-05-02 15:00:16 -07002078 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2079 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2080 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2081 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002082
Chet Haase5b0200b2011-04-13 17:58:08 -07002083 prevAAVertex = aaVertices - 1;
2084 generatedVerticesCount += 4;
2085 }
Romain Guy7b631422012-04-04 11:38:54 -07002086
Chet Haase99585ad2011-05-02 15:00:16 -07002087 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2088 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2089 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002090 }
2091 }
Romain Guy7b631422012-04-04 11:38:54 -07002092
Chet Haase5b0200b2011-04-13 17:58:08 -07002093 if (generatedVerticesCount > 0) {
2094 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2095 }
Romain Guy7b631422012-04-04 11:38:54 -07002096
2097 if (isAA) {
2098 finishDrawAALine(widthSlot, lengthSlot);
2099 }
Chet Haase48659092012-05-31 15:21:51 -07002100
2101 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002102}
2103
Chet Haase48659092012-05-31 15:21:51 -07002104status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2105 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002106
2107 // TODO: The paint's cap style defines whether the points are square or circular
2108 // TODO: Handle AA for round points
2109
Chet Haase5b0200b2011-04-13 17:58:08 -07002110 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002111 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002112 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002113 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002114 if (isHairLine) {
2115 // Now that we know it's hairline, we can set the effective width, to be used later
2116 strokeWidth = 1.0f;
2117 }
2118 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002119 int alpha;
2120 SkXfermode::Mode mode;
2121 getAlphaAndMode(paint, &alpha, &mode);
2122
2123 int verticesCount = count >> 1;
2124 int generatedVerticesCount = 0;
2125
2126 TextureVertex pointsData[verticesCount];
2127 TextureVertex* vertex = &pointsData[0];
2128
2129 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002130 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002131 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002132 setupDrawColor(paint->getColor(), alpha);
2133 setupDrawColorFilter();
2134 setupDrawShader();
2135 setupDrawBlending(mode);
2136 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002137 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002138 setupDrawColorUniforms();
2139 setupDrawColorFilterUniforms();
2140 setupDrawPointUniforms();
2141 setupDrawShaderIdentityUniforms();
2142 setupDrawMesh(vertex);
2143
2144 for (int i = 0; i < count; i += 2) {
2145 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2146 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002147
Chet Haase8a5cc922011-04-26 07:28:09 -07002148 float left = points[i] - halfWidth;
2149 float right = points[i] + halfWidth;
2150 float top = points[i + 1] - halfWidth;
2151 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002152
Chet Haase8a5cc922011-04-26 07:28:09 -07002153 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002154 }
2155
2156 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002157
2158 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002159}
2160
Chet Haase48659092012-05-31 15:21:51 -07002161status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002162 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002163 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002164
Romain Guyae88e5e2010-10-22 17:49:18 -07002165 Rect& clip(*mSnapshot->clipRect);
2166 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002167
Romain Guy3d58c032010-07-14 16:34:53 -07002168 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002169
2170 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002171}
Romain Guy9d5316e2010-06-24 19:30:36 -07002172
Chet Haase48659092012-05-31 15:21:51 -07002173status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2174 SkPaint* paint) {
2175 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002176 const AutoTexture autoCleanup(texture);
2177
2178 const float x = left + texture->left - texture->offset;
2179 const float y = top + texture->top - texture->offset;
2180
2181 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002182
2183 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002184}
2185
Chet Haase48659092012-05-31 15:21:51 -07002186status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002187 float rx, float ry, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002188 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002189
Romain Guya1d3c912011-12-13 14:55:06 -08002190 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002191 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2192 right - left, bottom - top, rx, ry, paint);
Chet Haase48659092012-05-31 15:21:51 -07002193 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002194}
2195
Chet Haase48659092012-05-31 15:21:51 -07002196status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2197 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002198
Romain Guya1d3c912011-12-13 14:55:06 -08002199 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002200 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Chet Haase48659092012-05-31 15:21:51 -07002201 return drawShape(x - radius, y - radius, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002202}
Romain Guy01d58e42011-01-19 21:54:02 -08002203
Chet Haase48659092012-05-31 15:21:51 -07002204status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
2205 SkPaint* paint) {
2206 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002207
Romain Guya1d3c912011-12-13 14:55:06 -08002208 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002209 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002210 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002211}
2212
Chet Haase48659092012-05-31 15:21:51 -07002213status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002214 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002215 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002216
2217 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002218 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002219 }
2220
Romain Guya1d3c912011-12-13 14:55:06 -08002221 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002222 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2223 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002224 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002225}
2226
Chet Haase48659092012-05-31 15:21:51 -07002227status_t OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002228 SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002229 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002230
Romain Guya1d3c912011-12-13 14:55:06 -08002231 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002232 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002233 return drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002234}
2235
Chet Haase48659092012-05-31 15:21:51 -07002236status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002237 if (p->getStyle() != SkPaint::kFill_Style) {
Chet Haase48659092012-05-31 15:21:51 -07002238 return drawRectAsShape(left, top, right, bottom, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002239 }
2240
Romain Guy6926c722010-07-12 20:20:03 -07002241 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002242 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002243 }
2244
Romain Guy026c5e162010-06-28 17:12:22 -07002245 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002246 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002247 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2248 if (!isMode) {
2249 // Assume SRC_OVER
2250 mode = SkXfermode::kSrcOver_Mode;
2251 }
2252 } else {
2253 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002254 }
2255
Romain Guy026c5e162010-06-28 17:12:22 -07002256 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002257 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002258 drawAARect(left, top, right, bottom, color, mode);
2259 } else {
2260 drawColorRect(left, top, right, bottom, color, mode);
2261 }
Chet Haase48659092012-05-31 15:21:51 -07002262
2263 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002264}
Romain Guy9d5316e2010-06-24 19:30:36 -07002265
Chet Haase48659092012-05-31 15:21:51 -07002266status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002267 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002268 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2269 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002270 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002271 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002272
Romain Guy671d6cf2012-01-18 12:39:17 -08002273 // NOTE: Skia does not support perspective transform on drawPosText yet
2274 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002275 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002276 }
2277
2278 float x = 0.0f;
2279 float y = 0.0f;
2280 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2281 if (pureTranslate) {
2282 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2283 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2284 }
2285
2286 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2287 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2288 paint->getTextSize());
2289
2290 int alpha;
2291 SkXfermode::Mode mode;
2292 getAlphaAndMode(paint, &alpha, &mode);
2293
2294 // Pick the appropriate texture filtering
2295 bool linearFilter = mSnapshot->transform->changesBounds();
2296 if (pureTranslate && !linearFilter) {
2297 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2298 }
2299
2300 mCaches.activeTexture(0);
2301 setupDraw();
2302 setupDrawDirtyRegionsDisabled();
2303 setupDrawWithTexture(true);
2304 setupDrawAlpha8Color(paint->getColor(), alpha);
2305 setupDrawColorFilter();
2306 setupDrawShader();
2307 setupDrawBlending(true, mode);
2308 setupDrawProgram();
2309 setupDrawModelView(x, y, x, y, pureTranslate, true);
2310 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2311 setupDrawPureColorUniforms();
2312 setupDrawColorFilterUniforms();
2313 setupDrawShaderUniforms(pureTranslate);
2314
2315 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2316 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2317
2318#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002319 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002320#else
Romain Guy211370f2012-02-01 16:10:55 -08002321 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002322#endif
2323
2324 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2325 positions, hasActiveLayer ? &bounds : NULL)) {
2326#if RENDER_LAYERS_AS_REGIONS
2327 if (hasActiveLayer) {
2328 if (!pureTranslate) {
2329 mSnapshot->transform->mapRect(bounds);
2330 }
2331 dirtyLayerUnchecked(bounds, getRegion());
2332 }
2333#endif
2334 }
Chet Haase48659092012-05-31 15:21:51 -07002335
2336 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002337}
2338
Chet Haase48659092012-05-31 15:21:51 -07002339status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002340 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002341 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2342 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002343 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002344 }
2345
Chet Haasea1cff502012-02-21 13:43:44 -08002346 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002347 switch (paint->getTextAlign()) {
2348 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002349 x -= length / 2.0f;
2350 break;
2351 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002352 x -= length;
2353 break;
2354 default:
2355 break;
2356 }
2357
Romain Guycac5fd32011-12-01 20:08:50 -08002358 SkPaint::FontMetrics metrics;
2359 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002360 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002361 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002362 }
2363
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002364 const float oldX = x;
2365 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002366 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002367 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002368 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2369 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2370 }
2371
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002372#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002373 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002374#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002375
2376 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002377 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002378 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002379
Romain Guy86568192010-12-14 15:55:39 -08002380 int alpha;
2381 SkXfermode::Mode mode;
2382 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002383
Romain Guy211370f2012-02-01 16:10:55 -08002384 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002385 mCaches.activeTexture(0);
2386
Romain Guyb45c0c92010-08-26 20:35:23 -07002387 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002388 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2389 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002390 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002391
Romain Guy740bf2b2011-04-26 15:33:10 -07002392 const float sx = oldX - shadow->left + mShadowDx;
2393 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002394
Romain Guy86568192010-12-14 15:55:39 -08002395 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002396 int shadowColor = mShadowColor;
2397 if (mShader) {
2398 shadowColor = 0xffffffff;
2399 }
Romain Guy86568192010-12-14 15:55:39 -08002400
Romain Guy86568192010-12-14 15:55:39 -08002401 setupDraw();
2402 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002403 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2404 setupDrawColorFilter();
2405 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002406 setupDrawBlending(true, mode);
2407 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002408 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002409 setupDrawTexture(shadow->id);
2410 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002411 setupDrawColorFilterUniforms();
2412 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002413 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2414
Romain Guy0a417492010-08-16 20:26:20 -07002415 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002416 }
2417
Romain Guy6620c6d2010-12-06 18:07:02 -08002418 // Pick the appropriate texture filtering
2419 bool linearFilter = mSnapshot->transform->changesBounds();
2420 if (pureTranslate && !linearFilter) {
2421 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2422 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002423
Romain Guy16c88082012-06-11 16:03:47 -07002424 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002425 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002426 setupDraw();
2427 setupDrawDirtyRegionsDisabled();
2428 setupDrawWithTexture(true);
2429 setupDrawAlpha8Color(paint->getColor(), alpha);
2430 setupDrawColorFilter();
2431 setupDrawShader();
2432 setupDrawBlending(true, mode);
2433 setupDrawProgram();
2434 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002435 // See comment above; the font renderer must use texture unit 0
2436 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002437 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2438 setupDrawPureColorUniforms();
2439 setupDrawColorFilterUniforms();
2440 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002441
Romain Guy6620c6d2010-12-06 18:07:02 -08002442 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002443 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2444
2445#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002446 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002447#else
Romain Guy211370f2012-02-01 16:10:55 -08002448 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002449#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002450
Romain Guy6620c6d2010-12-06 18:07:02 -08002451 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002452 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002453#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002454 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002455 if (!pureTranslate) {
2456 mSnapshot->transform->mapRect(bounds);
2457 }
Romain Guyf219da52011-01-16 12:54:25 -08002458 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002459 }
2460#endif
2461 }
Romain Guy694b5192010-07-21 21:33:20 -07002462
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002463 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002464
2465 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002466}
2467
Chet Haase48659092012-05-31 15:21:51 -07002468status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002469 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002470 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2471 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002472 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002473 }
2474
Romain Guy03d58522012-02-24 17:54:07 -08002475 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2476 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2477 paint->getTextSize());
2478
2479 int alpha;
2480 SkXfermode::Mode mode;
2481 getAlphaAndMode(paint, &alpha, &mode);
2482
2483 mCaches.activeTexture(0);
2484 setupDraw();
2485 setupDrawDirtyRegionsDisabled();
2486 setupDrawWithTexture(true);
2487 setupDrawAlpha8Color(paint->getColor(), alpha);
2488 setupDrawColorFilter();
2489 setupDrawShader();
2490 setupDrawBlending(true, mode);
2491 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002492 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002493 setupDrawTexture(fontRenderer.getTexture(true));
2494 setupDrawPureColorUniforms();
2495 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002496 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002497
Romain Guy97771732012-02-28 18:17:02 -08002498 const Rect* clip = &mSnapshot->getLocalClip();
2499 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 -08002500
Romain Guy97771732012-02-28 18:17:02 -08002501#if RENDER_LAYERS_AS_REGIONS
2502 const bool hasActiveLayer = hasLayer();
2503#else
2504 const bool hasActiveLayer = false;
2505#endif
2506
2507 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2508 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2509#if RENDER_LAYERS_AS_REGIONS
2510 if (hasActiveLayer) {
2511 mSnapshot->transform->mapRect(bounds);
2512 dirtyLayerUnchecked(bounds, getRegion());
2513 }
2514#endif
2515 }
Chet Haase48659092012-05-31 15:21:51 -07002516
2517 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002518}
2519
Chet Haase48659092012-05-31 15:21:51 -07002520status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2521 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002522
Romain Guya1d3c912011-12-13 14:55:06 -08002523 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002524
Romain Guy33f6beb2012-02-16 19:24:51 -08002525 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002526 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002527 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002528 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002529
Romain Guy8b55f372010-08-18 17:10:07 -07002530 const float x = texture->left - texture->offset;
2531 const float y = texture->top - texture->offset;
2532
Romain Guy01d58e42011-01-19 21:54:02 -08002533 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002534
2535 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002536}
2537
Chet Haase48659092012-05-31 15:21:51 -07002538status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guyada830f2011-01-13 12:13:20 -08002539 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Chet Haase48659092012-05-31 15:21:51 -07002540 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002541 }
2542
Romain Guy2bf68f02012-03-02 13:37:47 -08002543 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2544 OpenGLRenderer* renderer = layer->renderer;
2545 Rect& dirty = layer->dirtyRect;
2546
2547 interrupt();
2548 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2549 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002550 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002551 renderer->finish();
2552 resume();
2553
2554 dirty.setEmpty();
2555 layer->deferredUpdateScheduled = false;
2556 layer->renderer = NULL;
2557 layer->displayList = NULL;
2558 }
2559
Romain Guya1d3c912011-12-13 14:55:06 -08002560 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002561
2562 int alpha;
2563 SkXfermode::Mode mode;
2564 getAlphaAndMode(paint, &alpha, &mode);
2565
Romain Guy9ace8f52011-07-07 20:50:11 -07002566 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002567
Romain Guyf219da52011-01-16 12:54:25 -08002568#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002569 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002570 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002571 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002572 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002573 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002574 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002575
Romain Guyc88e3572011-01-22 00:32:12 -08002576 setupDraw();
2577 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002578 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002579 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002580 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002581 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002582 setupDrawPureColorUniforms();
2583 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002584 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002585 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002586 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2587 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2588
Romain Guyd21b6e12011-11-30 20:21:23 -08002589 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002590 setupDrawModelViewTranslate(x, y,
2591 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2592 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002593 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002594 setupDrawModelViewTranslate(x, y,
2595 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2596 }
Romain Guyc88e3572011-01-22 00:32:12 -08002597 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002598
Romain Guyc88e3572011-01-22 00:32:12 -08002599 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2600 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002601
Romain Guyc88e3572011-01-22 00:32:12 -08002602 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002603
2604#if DEBUG_LAYERS_AS_REGIONS
2605 drawRegionRects(layer->region);
2606#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002607 }
Romain Guyf219da52011-01-16 12:54:25 -08002608 }
2609#else
Romain Guyada830f2011-01-13 12:13:20 -08002610 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2611 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002612#endif
Chet Haase48659092012-05-31 15:21:51 -07002613
2614 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002615}
2616
Romain Guy6926c722010-07-12 20:20:03 -07002617///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002618// Shaders
2619///////////////////////////////////////////////////////////////////////////////
2620
2621void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002622 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002623}
2624
Romain Guy06f96e22010-07-30 19:18:16 -07002625void OpenGLRenderer::setupShader(SkiaShader* shader) {
2626 mShader = shader;
2627 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002628 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002629 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002630}
2631
Romain Guyd27977d2010-07-14 19:18:51 -07002632///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002633// Color filters
2634///////////////////////////////////////////////////////////////////////////////
2635
2636void OpenGLRenderer::resetColorFilter() {
2637 mColorFilter = NULL;
2638}
2639
2640void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2641 mColorFilter = filter;
2642}
2643
2644///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002645// Drop shadow
2646///////////////////////////////////////////////////////////////////////////////
2647
2648void OpenGLRenderer::resetShadow() {
2649 mHasShadow = false;
2650}
2651
2652void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2653 mHasShadow = true;
2654 mShadowRadius = radius;
2655 mShadowDx = dx;
2656 mShadowDy = dy;
2657 mShadowColor = color;
2658}
2659
2660///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002661// Draw filters
2662///////////////////////////////////////////////////////////////////////////////
2663
2664void OpenGLRenderer::resetPaintFilter() {
2665 mHasDrawFilter = false;
2666}
2667
2668void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2669 mHasDrawFilter = true;
2670 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2671 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2672}
2673
2674SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002675 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002676
2677 uint32_t flags = paint->getFlags();
2678
2679 mFilteredPaint = *paint;
2680 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2681
2682 return &mFilteredPaint;
2683}
2684
2685///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002686// Drawing implementation
2687///////////////////////////////////////////////////////////////////////////////
2688
Romain Guy01d58e42011-01-19 21:54:02 -08002689void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2690 float x, float y, SkPaint* paint) {
2691 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2692 return;
2693 }
2694
2695 int alpha;
2696 SkXfermode::Mode mode;
2697 getAlphaAndMode(paint, &alpha, &mode);
2698
2699 setupDraw();
2700 setupDrawWithTexture(true);
2701 setupDrawAlpha8Color(paint->getColor(), alpha);
2702 setupDrawColorFilter();
2703 setupDrawShader();
2704 setupDrawBlending(true, mode);
2705 setupDrawProgram();
2706 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2707 setupDrawTexture(texture->id);
2708 setupDrawPureColorUniforms();
2709 setupDrawColorFilterUniforms();
2710 setupDrawShaderUniforms();
2711 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2712
2713 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2714
2715 finishDrawTexture();
2716}
2717
Romain Guyf607bdc2010-09-10 19:20:06 -07002718// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002719#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2720#define kStdUnderline_Offset (1.0f / 9.0f)
2721#define kStdUnderline_Thickness (1.0f / 18.0f)
2722
2723void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2724 float x, float y, SkPaint* paint) {
2725 // Handle underline and strike-through
2726 uint32_t flags = paint->getFlags();
2727 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002728 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002729 float underlineWidth = length;
2730 // If length is > 0.0f, we already measured the text for the text alignment
2731 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002732 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002733 }
2734
2735 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002736 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002737 case SkPaint::kCenter_Align:
2738 offsetX = underlineWidth * 0.5f;
2739 break;
2740 case SkPaint::kRight_Align:
2741 offsetX = underlineWidth;
2742 break;
2743 default:
2744 break;
2745 }
2746
Romain Guy211370f2012-02-01 16:10:55 -08002747 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002748 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002749 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002750
Romain Guye20ecbd2010-09-22 19:49:04 -07002751 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002752 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002753
Romain Guyf6834472011-01-23 13:32:12 -08002754 int linesCount = 0;
2755 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2756 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2757
2758 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002759 float points[pointsCount];
2760 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002761
2762 if (flags & SkPaint::kUnderlineText_Flag) {
2763 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002764 points[currentPoint++] = left;
2765 points[currentPoint++] = top;
2766 points[currentPoint++] = left + underlineWidth;
2767 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002768 }
2769
2770 if (flags & SkPaint::kStrikeThruText_Flag) {
2771 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002772 points[currentPoint++] = left;
2773 points[currentPoint++] = top;
2774 points[currentPoint++] = left + underlineWidth;
2775 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002776 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002777
Romain Guy726aeba2011-06-01 14:52:00 -07002778 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002779
Romain Guy726aeba2011-06-01 14:52:00 -07002780 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002781 }
2782 }
2783}
2784
Romain Guy026c5e162010-06-28 17:12:22 -07002785void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002786 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002787 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002788 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002789 color |= 0x00ffffff;
2790 }
2791
Romain Guy70ca14e2010-12-13 18:24:33 -08002792 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002793 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002794 setupDrawColor(color);
2795 setupDrawShader();
2796 setupDrawColorFilter();
2797 setupDrawBlending(mode);
2798 setupDrawProgram();
2799 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2800 setupDrawColorUniforms();
2801 setupDrawShaderUniforms(ignoreTransform);
2802 setupDrawColorFilterUniforms();
2803 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002804
Romain Guyc95c8d62010-09-17 15:31:32 -07002805 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2806}
2807
Romain Guy82ba8142010-07-09 13:25:56 -07002808void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002809 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002810 int alpha;
2811 SkXfermode::Mode mode;
2812 getAlphaAndMode(paint, &alpha, &mode);
2813
Romain Guyd21b6e12011-11-30 20:21:23 -08002814 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002815
Romain Guy211370f2012-02-01 16:10:55 -08002816 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002817 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2818 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2819
Romain Guyd21b6e12011-11-30 20:21:23 -08002820 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002821 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2822 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2823 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2824 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002825 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002826 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2827 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2828 GL_TRIANGLE_STRIP, gMeshCount);
2829 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002830}
2831
Romain Guybd6b79b2010-06-26 00:13:53 -07002832void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002833 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2834 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002835 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002836}
2837
2838void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002839 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002840 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002841 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002842
Romain Guy746b7402010-10-26 16:27:31 -07002843 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002844 setupDrawWithTexture();
2845 setupDrawColor(alpha, alpha, alpha, alpha);
2846 setupDrawColorFilter();
2847 setupDrawBlending(blend, mode, swapSrcDst);
2848 setupDrawProgram();
2849 if (!dirty) {
2850 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002851 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002852 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002853 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002854 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002855 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002856 }
Romain Guy86568192010-12-14 15:55:39 -08002857 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002858 setupDrawColorFilterUniforms();
2859 setupDrawTexture(texture);
2860 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002861
Romain Guy6820ac82010-09-15 18:11:50 -07002862 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002863
2864 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002865}
2866
Romain Guya5aed0d2010-09-09 14:42:43 -07002867void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002868 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002869 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002870
Romain Guy82ba8142010-07-09 13:25:56 -07002871 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002872 // These blend modes are not supported by OpenGL directly and have
2873 // to be implemented using shaders. Since the shader will perform
2874 // the blending, turn blending off here
2875 // If the blend mode cannot be implemented using shaders, fall
2876 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002877 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2878 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002879 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002880 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002881
Romain Guy82bc7a72012-01-03 14:13:39 -08002882 if (mCaches.blend) {
2883 glDisable(GL_BLEND);
2884 mCaches.blend = false;
2885 }
2886
2887 return;
2888 } else {
2889 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002890 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002891 }
2892
2893 if (!mCaches.blend) {
2894 glEnable(GL_BLEND);
2895 }
2896
2897 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2898 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2899
2900 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2901 glBlendFunc(sourceMode, destMode);
2902 mCaches.lastSrcMode = sourceMode;
2903 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002904 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002905 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002906 glDisable(GL_BLEND);
2907 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002908 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002909}
2910
Romain Guy889f8d12010-07-29 14:37:42 -07002911bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002912 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002913 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002914 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002915 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002916 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002917 }
Romain Guy6926c722010-07-12 20:20:03 -07002918 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002919}
2920
Romain Guy026c5e162010-06-28 17:12:22 -07002921void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002922 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002923 TextureVertex::setUV(v++, u1, v1);
2924 TextureVertex::setUV(v++, u2, v1);
2925 TextureVertex::setUV(v++, u1, v2);
2926 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002927}
2928
Chet Haase5c13d892010-10-08 08:37:55 -07002929void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002930 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002931 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002932
2933 // Skia draws using the color's alpha channel if < 255
2934 // Otherwise, it uses the paint's alpha
2935 int color = paint->getColor();
2936 *alpha = (color >> 24) & 0xFF;
2937 if (*alpha == 255) {
2938 *alpha = paint->getAlpha();
2939 }
2940 } else {
2941 *mode = SkXfermode::kSrcOver_Mode;
2942 *alpha = 255;
2943 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07002944 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002945}
2946
Romain Guya5aed0d2010-09-09 14:42:43 -07002947SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002948 SkXfermode::Mode resultMode;
2949 if (!SkXfermode::AsMode(mode, &resultMode)) {
2950 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002951 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002952 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002953}
2954
Romain Guy9d5316e2010-06-24 19:30:36 -07002955}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002956}; // namespace android