blob: 2dc9726ab9cadbba95cb94104693d37079a21beb [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 Guydbc26d22010-10-11 17:58:29 -070048// TODO: This should be set in properties
49#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
50
Romain Guyd21b6e12011-11-30 20:21:23 -080051#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
52
Romain Guy9d5316e2010-06-24 19:30:36 -070053///////////////////////////////////////////////////////////////////////////////
54// Globals
55///////////////////////////////////////////////////////////////////////////////
56
Romain Guy889f8d12010-07-29 14:37:42 -070057/**
58 * Structure mapping Skia xfermodes to OpenGL blending factors.
59 */
60struct Blender {
61 SkXfermode::Mode mode;
62 GLenum src;
63 GLenum dst;
64}; // struct Blender
65
Romain Guy026c5e162010-06-28 17:12:22 -070066// In this array, the index of each Blender equals the value of the first
67// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
68static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070069 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
70 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
71 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
72 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
73 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
74 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
75 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
76 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
80 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
82 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
83 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070084};
Romain Guye4d01122010-06-16 18:44:05 -070085
Romain Guy87a76572010-09-13 18:11:21 -070086// This array contains the swapped version of each SkXfermode. For instance
87// this array's SrcOver blending mode is actually DstOver. You can refer to
88// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070089static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070090 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
92 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
93 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
94 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
96 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
97 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
98 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
100 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
101 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
103 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
104 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700105};
106
Romain Guyf6a11b82010-06-23 17:47:49 -0700107///////////////////////////////////////////////////////////////////////////////
108// Constructors/destructor
109///////////////////////////////////////////////////////////////////////////////
110
Romain Guyfb8b7632010-08-23 21:05:08 -0700111OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700112 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700113 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700114 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800115 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700116
Romain Guyac670c02010-07-27 17:39:27 -0700117 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
118
Romain Guyae5575b2010-07-29 18:48:04 -0700119 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700120}
121
Romain Guy85bf02f2010-06-22 13:11:24 -0700122OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700123 // The context has already been destroyed at this point, do not call
124 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700125}
126
Romain Guyf6a11b82010-06-23 17:47:49 -0700127///////////////////////////////////////////////////////////////////////////////
Romain Guy13631f32012-01-30 17:41:55 -0800128// Debug
129///////////////////////////////////////////////////////////////////////////////
130
131void OpenGLRenderer::startMark(const char* name) const {
132 mCaches.startMark(0, name);
133}
134
135void OpenGLRenderer::endMark() const {
136 mCaches.endMark();
137}
138
139///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700140// Setup
141///////////////////////////////////////////////////////////////////////////////
142
Romain Guy530041d2012-01-25 18:56:29 -0800143uint32_t OpenGLRenderer::getStencilSize() {
144 return STENCIL_BUFFER_SIZE;
145}
146
Romain Guy49c5fc02012-05-15 11:10:01 -0700147bool OpenGLRenderer::isDeferred() {
148 return false;
149}
150
Romain Guy85bf02f2010-06-22 13:11:24 -0700151void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700152 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700153
154 mWidth = width;
155 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700156
157 mFirstSnapshot->height = height;
158 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700159
Romain Guy3e263fa2011-12-12 16:47:48 -0800160 glDisable(GL_DITHER);
Romain Guy39d252a2011-12-12 18:14:06 -0800161 glEnable(GL_SCISSOR_TEST);
Romain Guy3e263fa2011-12-12 16:47:48 -0800162 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800163
Romain Guy3e263fa2011-12-12 16:47:48 -0800164 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700165}
166
Romain Guy6b7bd242010-10-06 19:49:23 -0700167void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800168 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
169}
170
171void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800172 mCaches.clearGarbage();
173
Romain Guy8aef54f2010-09-01 15:13:49 -0700174 mSnapshot = new Snapshot(mFirstSnapshot,
175 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800176 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700177 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700178
Romain Guy7d7b5492011-01-24 16:33:45 -0800179 mSnapshot->setClip(left, top, right, bottom);
Romain Guyddf74372012-05-22 14:07:07 -0700180 mDirtyClip = opaque;
181
182 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800183
Romain Guy6b7bd242010-10-06 19:49:23 -0700184 if (!opaque) {
Romain Guyddf74372012-05-22 14:07:07 -0700185 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700186 glClear(GL_COLOR_BUFFER_BIT);
Romain Guyddf74372012-05-22 14:07:07 -0700187 } else {
188 mCaches.resetScissor();
189 }
190}
191
192void OpenGLRenderer::syncState() {
193 glViewport(0, 0, mWidth, mHeight);
194
195 if (mCaches.blend) {
196 glEnable(GL_BLEND);
197 } else {
198 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700199 }
Romain Guybb9524b2010-06-22 18:56:38 -0700200}
201
Romain Guyb025b9c2010-09-16 14:16:48 -0700202void OpenGLRenderer::finish() {
203#if DEBUG_OPENGL
204 GLenum status = GL_NO_ERROR;
205 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000206 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800207 switch (status) {
Romain Guya44a63a2012-04-26 14:05:02 -0700208 case GL_INVALID_ENUM:
209 ALOGE(" GL_INVALID_ENUM");
210 break;
211 case GL_INVALID_VALUE:
212 ALOGE(" GL_INVALID_VALUE");
213 break;
214 case GL_INVALID_OPERATION:
215 ALOGE(" GL_INVALID_OPERATION");
216 break;
Romain Guya07105b2011-01-10 21:14:18 -0800217 case GL_OUT_OF_MEMORY:
Romain Guya44a63a2012-04-26 14:05:02 -0700218 ALOGE(" Out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800219 break;
220 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700221 }
222#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800223#if DEBUG_MEMORY_USAGE
224 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800225#else
226 if (mCaches.getDebugLevel() & kDebugMemory) {
227 mCaches.dumpMemoryUsage();
228 }
Romain Guyc15008e2010-11-10 11:59:15 -0800229#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700230}
231
Romain Guy6c319ca2011-01-11 14:29:25 -0800232void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700233 if (mCaches.currentProgram) {
234 if (mCaches.currentProgram->isInUse()) {
235 mCaches.currentProgram->remove();
236 mCaches.currentProgram = NULL;
237 }
238 }
Romain Guy50c0f092010-10-19 11:42:22 -0700239 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800240 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800241 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800242 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700243}
244
Romain Guy6c319ca2011-01-11 14:29:25 -0800245void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800246 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
247
248 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800249 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
250
Romain Guyda8532c2010-08-31 11:50:35 -0700251 glEnable(GL_SCISSOR_TEST);
Romain Guy82bc7a72012-01-03 14:13:39 -0800252 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700253 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700254
Romain Guya1d3c912011-12-13 14:55:06 -0800255 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800256 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700257
Romain Guy50c0f092010-10-19 11:42:22 -0700258 mCaches.blend = true;
259 glEnable(GL_BLEND);
260 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
261 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700262}
263
Romain Guyba6be8a2012-04-23 18:22:09 -0700264void OpenGLRenderer::detachFunctor(Functor* functor) {
265 mFunctors.remove(functor);
266}
267
268void OpenGLRenderer::attachFunctor(Functor* functor) {
269 mFunctors.add(functor);
270}
271
Romain Guy8f3b8e32012-03-27 16:33:45 -0700272status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
273 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700274 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700275
Romain Guyba6be8a2012-04-23 18:22:09 -0700276 if (count > 0) {
277 SortedVector<Functor*> functors(mFunctors);
278 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700279
Romain Guyba6be8a2012-04-23 18:22:09 -0700280 DrawGlInfo info;
281 info.clipLeft = 0;
282 info.clipTop = 0;
283 info.clipRight = 0;
284 info.clipBottom = 0;
285 info.isLayer = false;
286 info.width = 0;
287 info.height = 0;
288 memset(info.transform, 0, sizeof(float) * 16);
289
290 for (size_t i = 0; i < count; i++) {
291 Functor* f = functors.itemAt(i);
292 result |= (*f)(DrawGlInfo::kModeProcess, &info);
293
Chris Craikc2c95432012-04-25 15:13:52 -0700294 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700295 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
296 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700297 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700298
Chris Craikc2c95432012-04-25 15:13:52 -0700299 if (result & DrawGlInfo::kStatusInvoke) {
300 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700301 }
302 }
303 }
304
Romain Guyc189ef52012-04-25 20:02:53 -0700305 mCaches.activeTexture(0);
306
Romain Guy8f3b8e32012-03-27 16:33:45 -0700307 return result;
308}
309
310status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800311 interrupt();
Chris Craik9e080122012-05-22 16:00:19 -0700312 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700313
Romain Guyf90f8172011-01-25 22:53:24 -0800314 if (mDirtyClip) {
315 setScissorFromClip();
316 }
Romain Guyd643bb52011-03-01 14:55:21 -0800317
Romain Guy80911b82011-03-16 15:30:12 -0700318 Rect clip(*mSnapshot->clipRect);
319 clip.snapToPixelBoundaries();
320
Romain Guyd643bb52011-03-01 14:55:21 -0800321#if RENDER_LAYERS_AS_REGIONS
322 // Since we don't know what the functor will draw, let's dirty
323 // tne entire clip region
324 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800325 dirtyLayerUnchecked(clip, getRegion());
326 }
327#endif
328
Romain Guy08aa2cb2011-03-17 11:06:57 -0700329 DrawGlInfo info;
330 info.clipLeft = clip.left;
331 info.clipTop = clip.top;
332 info.clipRight = clip.right;
333 info.clipBottom = clip.bottom;
334 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700335 info.width = getSnapshot()->viewport.getWidth();
336 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700337 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700338
Chet Haase48659092012-05-31 15:21:51 -0700339 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800340
Romain Guy8f3b8e32012-03-27 16:33:45 -0700341 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700342 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800343 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700344
Chris Craik65924a32012-04-05 17:52:11 -0700345 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700346 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700347 }
Romain Guycabfcc12011-03-07 18:06:46 -0800348 }
349
Chet Haasedaf98e92011-01-10 14:10:36 -0800350 resume();
Romain Guy65549432012-03-26 16:45:05 -0700351 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800352}
353
Romain Guyf6a11b82010-06-23 17:47:49 -0700354///////////////////////////////////////////////////////////////////////////////
355// State management
356///////////////////////////////////////////////////////////////////////////////
357
Romain Guybb9524b2010-06-22 18:56:38 -0700358int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700359 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700360}
361
362int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700363 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700364}
365
366void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700367 if (mSaveCount > 1) {
368 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700369 }
Romain Guybb9524b2010-06-22 18:56:38 -0700370}
371
372void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700373 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700374
Romain Guy8fb95422010-08-17 18:38:51 -0700375 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700376 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700377 }
Romain Guybb9524b2010-06-22 18:56:38 -0700378}
379
Romain Guy8aef54f2010-09-01 15:13:49 -0700380int OpenGLRenderer::saveSnapshot(int flags) {
381 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700382 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700383}
384
385bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700386 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700387 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700388 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700389
Romain Guybd6b79b2010-06-26 00:13:53 -0700390 sp<Snapshot> current = mSnapshot;
391 sp<Snapshot> previous = mSnapshot->previous;
392
Romain Guyeb993562010-10-05 18:14:38 -0700393 if (restoreOrtho) {
394 Rect& r = previous->viewport;
395 glViewport(r.left, r.top, r.right, r.bottom);
396 mOrthoMatrix.load(current->orthoMatrix);
397 }
398
Romain Guy8b55f372010-08-18 17:10:07 -0700399 mSaveCount--;
400 mSnapshot = previous;
401
Romain Guy2542d192010-08-18 11:47:12 -0700402 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700403 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700404 }
Romain Guy2542d192010-08-18 11:47:12 -0700405
Romain Guy5ec99242010-11-03 16:19:08 -0700406 if (restoreLayer) {
407 composeLayer(current, previous);
408 }
409
Romain Guy2542d192010-08-18 11:47:12 -0700410 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700411}
412
Romain Guyf6a11b82010-06-23 17:47:49 -0700413///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700414// Layers
415///////////////////////////////////////////////////////////////////////////////
416
417int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700418 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700419 const GLuint previousFbo = mSnapshot->fbo;
420 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700421
Romain Guyaf636eb2010-12-09 17:47:21 -0800422 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700423 int alpha = 255;
424 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700425
Romain Guye45362c2010-11-03 19:58:32 -0700426 if (p) {
427 alpha = p->getAlpha();
428 if (!mCaches.extensions.hasFramebufferFetch()) {
429 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
430 if (!isMode) {
431 // Assume SRC_OVER
432 mode = SkXfermode::kSrcOver_Mode;
433 }
434 } else {
435 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700436 }
437 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700438 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700439 }
Romain Guyd55a8612010-06-28 17:42:46 -0700440
Romain Guydbc26d22010-10-11 17:58:29 -0700441 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
442 }
Romain Guyd55a8612010-06-28 17:42:46 -0700443
444 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700445}
446
447int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
448 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700449 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700450 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700451 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700452 SkPaint paint;
453 paint.setAlpha(alpha);
454 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700455 }
Romain Guyd55a8612010-06-28 17:42:46 -0700456}
Romain Guybd6b79b2010-06-26 00:13:53 -0700457
Romain Guy1c740bc2010-09-13 18:00:09 -0700458/**
459 * Layers are viewed by Skia are slightly different than layers in image editing
460 * programs (for instance.) When a layer is created, previously created layers
461 * and the frame buffer still receive every drawing command. For instance, if a
462 * layer is created and a shape intersecting the bounds of the layers and the
463 * framebuffer is draw, the shape will be drawn on both (unless the layer was
464 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
465 *
466 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
467 * texture. Unfortunately, this is inefficient as it requires every primitive to
468 * be drawn n + 1 times, where n is the number of active layers. In practice this
469 * means, for every primitive:
470 * - Switch active frame buffer
471 * - Change viewport, clip and projection matrix
472 * - Issue the drawing
473 *
474 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700475 * To avoid this, layers are implemented in a different way here, at least in the
476 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
477 * is set. When this flag is set we can redirect all drawing operations into a
478 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700479 *
480 * This implementation relies on the frame buffer being at least RGBA 8888. When
481 * a layer is created, only a texture is created, not an FBO. The content of the
482 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700483 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700484 * buffer and drawing continues as normal. This technique therefore treats the
485 * frame buffer as a scratch buffer for the layers.
486 *
487 * To compose the layers back onto the frame buffer, each layer texture
488 * (containing the original frame buffer data) is drawn as a simple quad over
489 * the frame buffer. The trick is that the quad is set as the composition
490 * destination in the blending equation, and the frame buffer becomes the source
491 * of the composition.
492 *
493 * Drawing layers with an alpha value requires an extra step before composition.
494 * An empty quad is drawn over the layer's region in the frame buffer. This quad
495 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
496 * quad is used to multiply the colors in the frame buffer. This is achieved by
497 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
498 * GL_ZERO, GL_SRC_ALPHA.
499 *
500 * Because glCopyTexImage2D() can be slow, an alternative implementation might
501 * be use to draw a single clipped layer. The implementation described above
502 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700503 *
504 * (1) The frame buffer is actually not cleared right away. To allow the GPU
505 * to potentially optimize series of calls to glCopyTexImage2D, the frame
506 * buffer is left untouched until the first drawing operation. Only when
507 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700508 */
Romain Guyd55a8612010-06-28 17:42:46 -0700509bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700510 float right, float bottom, int alpha, SkXfermode::Mode mode,
511 int flags, GLuint previousFbo) {
512 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700513 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700514
Romain Guyeb993562010-10-05 18:14:38 -0700515 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
516
Romain Guyf607bdc2010-09-10 19:20:06 -0700517 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700518 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700519 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700520 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700521
Romain Guyeb993562010-10-05 18:14:38 -0700522 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700523 if (bounds.intersect(*snapshot->clipRect)) {
524 // We cannot work with sub-pixels in this case
525 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700526
Romain Guyad37cd32011-03-15 11:12:25 -0700527 // When the layer is not an FBO, we may use glCopyTexImage so we
528 // need to make sure the layer does not extend outside the bounds
529 // of the framebuffer
530 if (!bounds.intersect(snapshot->previous->viewport)) {
531 bounds.setEmpty();
532 }
533 } else {
534 bounds.setEmpty();
535 }
Romain Guyeb993562010-10-05 18:14:38 -0700536 }
Romain Guybf434112010-09-16 14:40:17 -0700537
Romain Guy746b7402010-10-26 16:27:31 -0700538 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
539 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800540 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700541 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700542 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700543 }
544
545 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800546 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700547 return false;
548 }
Romain Guyf18fd992010-07-08 11:45:51 -0700549
Romain Guya1d3c912011-12-13 14:55:06 -0800550 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700551 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700552 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700553 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700554 }
555
Romain Guy9ace8f52011-07-07 20:50:11 -0700556 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700557 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700558 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
559 bounds.getWidth() / float(layer->getWidth()), 0.0f);
560 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700561 layer->setBlend(true);
Romain Guydda57022010-07-06 11:39:32 -0700562
Romain Guy8fb95422010-08-17 18:38:51 -0700563 // Save the layer in the snapshot
564 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700565 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700566
Romain Guyeb993562010-10-05 18:14:38 -0700567 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700568 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700569 } else {
570 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700571 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800572 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700573 if (layer->isEmpty()) {
574 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
575 bounds.left, snapshot->height - bounds.bottom,
576 layer->getWidth(), layer->getHeight(), 0);
577 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800578 } else {
579 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
580 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
581 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700582
Romain Guy54be1cd2011-06-13 19:04:27 -0700583 // Enqueue the buffer coordinates to clear the corresponding region later
584 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700585 }
Romain Guyeb993562010-10-05 18:14:38 -0700586 }
Romain Guyf86ef572010-07-01 11:05:42 -0700587
Romain Guyd55a8612010-06-28 17:42:46 -0700588 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700589}
590
Romain Guy5b3b3522010-10-27 18:57:51 -0700591bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
592 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700593 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700594
595#if RENDER_LAYERS_AS_REGIONS
596 snapshot->region = &snapshot->layer->region;
597 snapshot->flags |= Snapshot::kFlagFboTarget;
598#endif
599
600 Rect clip(bounds);
601 snapshot->transform->mapRect(clip);
602 clip.intersect(*snapshot->clipRect);
603 clip.snapToPixelBoundaries();
604 clip.intersect(snapshot->previous->viewport);
605
606 mat4 inverse;
607 inverse.loadInverse(*mSnapshot->transform);
608
609 inverse.mapRect(clip);
610 clip.snapToPixelBoundaries();
611 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700612 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700613
614 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700615 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700616 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700617 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
618 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
619 snapshot->height = bounds.getHeight();
620 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
621 snapshot->orthoMatrix.load(mOrthoMatrix);
622
623 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700624 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
625 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700626
627 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700628 if (layer->isEmpty()) {
629 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
630 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700631 }
632
633 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700634 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700635
636#if DEBUG_LAYERS_AS_REGIONS
637 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
638 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000639 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700640
641 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700642 layer->deleteTexture();
643 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700644
645 delete layer;
646
647 return false;
648 }
649#endif
650
651 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800652 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700653 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700654 glClear(GL_COLOR_BUFFER_BIT);
655
656 dirtyClip();
657
658 // Change the ortho projection
659 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
660 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
661
662 return true;
663}
664
Romain Guy1c740bc2010-09-13 18:00:09 -0700665/**
666 * Read the documentation of createLayer() before doing anything in this method.
667 */
Romain Guy1d83e192010-08-17 11:37:00 -0700668void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
669 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000670 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700671 return;
672 }
673
Romain Guy5b3b3522010-10-27 18:57:51 -0700674 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700675
676 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700677 // Detach the texture from the FBO
678 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
679
Romain Guyeb993562010-10-05 18:14:38 -0700680 // Unbind current FBO and restore previous one
681 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
682 }
683
Romain Guy1d83e192010-08-17 11:37:00 -0700684 Layer* layer = current->layer;
685 const Rect& rect = layer->layer;
686
Romain Guy9ace8f52011-07-07 20:50:11 -0700687 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700688 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700689 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700690 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700691 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700692 }
693
Romain Guy03750a02010-10-18 14:06:08 -0700694 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700695
Romain Guya1d3c912011-12-13 14:55:06 -0800696 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700697
Romain Guy5b3b3522010-10-27 18:57:51 -0700698 // When the layer is stored in an FBO, we can save a bit of fillrate by
699 // drawing only the dirty region
700 if (fboLayer) {
701 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700702 if (layer->getColorFilter()) {
703 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800704 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700705 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700706 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800707 resetColorFilter();
708 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700709 } else if (!rect.isEmpty()) {
710 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
711 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700712 }
Romain Guy8b55f372010-08-18 17:10:07 -0700713
Romain Guyeb993562010-10-05 18:14:38 -0700714 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800715 // Note: No need to use glDiscardFramebufferEXT() since we never
716 // create/compose layers that are not on screen with this
717 // code path
718 // See LayerRenderer::destroyLayer(Layer*)
719
Romain Guyeb993562010-10-05 18:14:38 -0700720 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
721 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700722 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700723 }
724
Romain Guy746b7402010-10-26 16:27:31 -0700725 dirtyClip();
726
Romain Guyeb993562010-10-05 18:14:38 -0700727 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700728 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700729 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700730 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700731 delete layer;
732 }
733}
734
Romain Guyaa6c24c2011-04-28 18:40:04 -0700735void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700736 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700737
Romain Guy302a9df2011-08-16 13:55:02 -0700738 mat4& transform = layer->getTransform();
739 if (!transform.isIdentity()) {
740 save(0);
741 mSnapshot->transform->multiply(transform);
742 }
743
Romain Guyaa6c24c2011-04-28 18:40:04 -0700744 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700745 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700746 setupDrawWithTexture();
747 } else {
748 setupDrawWithExternalTexture();
749 }
750 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700751 setupDrawColor(alpha, alpha, alpha, alpha);
752 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700753 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700754 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700755 setupDrawPureColorUniforms();
756 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700757 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
758 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700759 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700760 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700761 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700762 if (mSnapshot->transform->isPureTranslate() &&
763 layer->getWidth() == (uint32_t) rect.getWidth() &&
764 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700765 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
766 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
767
Romain Guyd21b6e12011-11-30 20:21:23 -0800768 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700769 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
770 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800771 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700772 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
773 }
774 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700775 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
776
777 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
778
779 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700780
781 if (!transform.isIdentity()) {
782 restore();
783 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700784}
785
Romain Guy5b3b3522010-10-27 18:57:51 -0700786void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700787 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700788 const Rect& texCoords = layer->texCoords;
789 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
790 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700791
Romain Guy9ace8f52011-07-07 20:50:11 -0700792 float x = rect.left;
793 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700794 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700795 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700796 layer->getHeight() == (uint32_t) rect.getHeight();
797
798 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700799 // When we're swapping, the layer is already in screen coordinates
800 if (!swap) {
801 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
802 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
803 }
804
Romain Guyd21b6e12011-11-30 20:21:23 -0800805 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700806 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800807 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700808 }
809
810 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
811 layer->getTexture(), layer->getAlpha() / 255.0f,
812 layer->getMode(), layer->isBlend(),
813 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
814 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700815
Romain Guyaa6c24c2011-04-28 18:40:04 -0700816 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
817 } else {
818 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
819 drawTextureLayer(layer, rect);
820 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
821 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700822}
823
824void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
825#if RENDER_LAYERS_AS_REGIONS
826 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700827 layer->setRegionAsRect();
828
Romain Guy40667672011-03-18 14:34:03 -0700829 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700830
Romain Guy5b3b3522010-10-27 18:57:51 -0700831 layer->region.clear();
832 return;
833 }
834
Romain Guy8a3957d2011-09-07 17:55:15 -0700835 // TODO: See LayerRenderer.cpp::generateMesh() for important
836 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800837 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700838 size_t count;
839 const android::Rect* rects = layer->region.getArray(&count);
840
Romain Guy9ace8f52011-07-07 20:50:11 -0700841 const float alpha = layer->getAlpha() / 255.0f;
842 const float texX = 1.0f / float(layer->getWidth());
843 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800844 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700845
846 TextureVertex* mesh = mCaches.getRegionMesh();
847 GLsizei numQuads = 0;
848
Romain Guy7230a742011-01-10 22:26:16 -0800849 setupDraw();
850 setupDrawWithTexture();
851 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800852 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700853 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800854 setupDrawProgram();
855 setupDrawDirtyRegionsDisabled();
856 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800857 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700858 setupDrawTexture(layer->getTexture());
859 if (mSnapshot->transform->isPureTranslate()) {
860 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
861 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
862
Romain Guyd21b6e12011-11-30 20:21:23 -0800863 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700864 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
865 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800866 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700867 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
868 }
Romain Guy15bc6432011-12-13 13:11:32 -0800869 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700870
871 for (size_t i = 0; i < count; i++) {
872 const android::Rect* r = &rects[i];
873
874 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800875 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700876 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800877 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700878
879 // TODO: Reject quads outside of the clip
880 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
881 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
882 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
883 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
884
885 numQuads++;
886
887 if (numQuads >= REGION_MESH_QUAD_COUNT) {
888 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
889 numQuads = 0;
890 mesh = mCaches.getRegionMesh();
891 }
892 }
893
894 if (numQuads > 0) {
895 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
896 }
897
Romain Guy7230a742011-01-10 22:26:16 -0800898 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700899
900#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800901 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700902#endif
903
904 layer->region.clear();
905 }
906#else
907 composeLayerRect(layer, rect);
908#endif
909}
910
Romain Guy3a3133d2011-02-01 22:59:58 -0800911void OpenGLRenderer::drawRegionRects(const Region& region) {
912#if DEBUG_LAYERS_AS_REGIONS
913 size_t count;
914 const android::Rect* rects = region.getArray(&count);
915
916 uint32_t colors[] = {
917 0x7fff0000, 0x7f00ff00,
918 0x7f0000ff, 0x7fff00ff,
919 };
920
921 int offset = 0;
922 int32_t top = rects[0].top;
923
924 for (size_t i = 0; i < count; i++) {
925 if (top != rects[i].top) {
926 offset ^= 0x2;
927 top = rects[i].top;
928 }
929
930 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
931 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
932 SkXfermode::kSrcOver_Mode);
933 }
934#endif
935}
936
Romain Guy5b3b3522010-10-27 18:57:51 -0700937void OpenGLRenderer::dirtyLayer(const float left, const float top,
938 const float right, const float bottom, const mat4 transform) {
939#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800940 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700941 Rect bounds(left, top, right, bottom);
942 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800943 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700944 }
945#endif
946}
947
948void OpenGLRenderer::dirtyLayer(const float left, const float top,
949 const float right, const float bottom) {
950#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800951 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700952 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800953 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800954 }
955#endif
956}
957
958void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
959#if RENDER_LAYERS_AS_REGIONS
960 if (bounds.intersect(*mSnapshot->clipRect)) {
961 bounds.snapToPixelBoundaries();
962 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
963 if (!dirty.isEmpty()) {
964 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700965 }
966 }
967#endif
968}
969
Romain Guy54be1cd2011-06-13 19:04:27 -0700970void OpenGLRenderer::clearLayerRegions() {
971 const size_t count = mLayers.size();
972 if (count == 0) return;
973
974 if (!mSnapshot->isIgnored()) {
975 // Doing several glScissor/glClear here can negatively impact
976 // GPUs with a tiler architecture, instead we draw quads with
977 // the Clear blending mode
978
979 // The list contains bounds that have already been clipped
980 // against their initial clip rect, and the current clip
981 // is likely different so we need to disable clipping here
982 glDisable(GL_SCISSOR_TEST);
983
984 Vertex mesh[count * 6];
985 Vertex* vertex = mesh;
986
987 for (uint32_t i = 0; i < count; i++) {
988 Rect* bounds = mLayers.itemAt(i);
989
990 Vertex::set(vertex++, bounds->left, bounds->bottom);
991 Vertex::set(vertex++, bounds->left, bounds->top);
992 Vertex::set(vertex++, bounds->right, bounds->top);
993 Vertex::set(vertex++, bounds->left, bounds->bottom);
994 Vertex::set(vertex++, bounds->right, bounds->top);
995 Vertex::set(vertex++, bounds->right, bounds->bottom);
996
997 delete bounds;
998 }
999
1000 setupDraw(false);
1001 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1002 setupDrawBlending(true, SkXfermode::kClear_Mode);
1003 setupDrawProgram();
1004 setupDrawPureColorUniforms();
1005 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001006 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001007
Romain Guy54be1cd2011-06-13 19:04:27 -07001008 glDrawArrays(GL_TRIANGLES, 0, count * 6);
1009
1010 glEnable(GL_SCISSOR_TEST);
1011 } else {
1012 for (uint32_t i = 0; i < count; i++) {
1013 delete mLayers.itemAt(i);
1014 }
1015 }
1016
1017 mLayers.clear();
1018}
1019
Romain Guybd6b79b2010-06-26 00:13:53 -07001020///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001021// Transforms
1022///////////////////////////////////////////////////////////////////////////////
1023
1024void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001025 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001026}
1027
1028void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001029 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001030}
1031
1032void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001033 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001034}
1035
Romain Guy807daf72011-01-18 11:19:19 -08001036void OpenGLRenderer::skew(float sx, float sy) {
1037 mSnapshot->transform->skew(sx, sy);
1038}
1039
Romain Guyf6a11b82010-06-23 17:47:49 -07001040void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001041 if (matrix) {
1042 mSnapshot->transform->load(*matrix);
1043 } else {
1044 mSnapshot->transform->loadIdentity();
1045 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001046}
1047
1048void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001049 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001050}
1051
1052void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001053 SkMatrix transform;
1054 mSnapshot->transform->copyTo(transform);
1055 transform.preConcat(*matrix);
1056 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001057}
1058
1059///////////////////////////////////////////////////////////////////////////////
1060// Clipping
1061///////////////////////////////////////////////////////////////////////////////
1062
Romain Guybb9524b2010-06-22 18:56:38 -07001063void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001064 Rect clip(*mSnapshot->clipRect);
1065 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001066
1067 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1068 clip.getWidth(), clip.getHeight());
1069
Romain Guy746b7402010-10-26 16:27:31 -07001070 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -07001071}
1072
1073const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001074 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001075}
1076
Romain Guyc7d53492010-06-25 13:41:57 -07001077bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001078 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001079 return true;
1080 }
1081
Romain Guy1d83e192010-08-17 11:37:00 -07001082 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001083 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001084 r.snapToPixelBoundaries();
1085
1086 Rect clipRect(*mSnapshot->clipRect);
1087 clipRect.snapToPixelBoundaries();
1088
1089 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001090}
1091
Romain Guy079ba2c2010-07-16 14:12:24 -07001092bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1093 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001094 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001095 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001096 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001097 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001098}
1099
Chet Haasea23eed82012-04-12 15:19:04 -07001100Rect* OpenGLRenderer::getClipRect() {
1101 return mSnapshot->clipRect;
1102}
1103
Romain Guyf6a11b82010-06-23 17:47:49 -07001104///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001105// Drawing commands
1106///////////////////////////////////////////////////////////////////////////////
1107
Romain Guy54be1cd2011-06-13 19:04:27 -07001108void OpenGLRenderer::setupDraw(bool clear) {
1109 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001110 if (mDirtyClip) {
1111 setScissorFromClip();
1112 }
1113 mDescription.reset();
1114 mSetShaderColor = false;
1115 mColorSet = false;
1116 mColorA = mColorR = mColorG = mColorB = 0.0f;
1117 mTextureUnit = 0;
1118 mTrackDirtyRegions = true;
1119}
1120
1121void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1122 mDescription.hasTexture = true;
1123 mDescription.hasAlpha8Texture = isAlpha8;
1124}
1125
Romain Guyaa6c24c2011-04-28 18:40:04 -07001126void OpenGLRenderer::setupDrawWithExternalTexture() {
1127 mDescription.hasExternalTexture = true;
1128}
1129
Romain Guy15bc6432011-12-13 13:11:32 -08001130void OpenGLRenderer::setupDrawNoTexture() {
1131 mCaches.disbaleTexCoordsVertexArray();
1132}
1133
Chet Haase5b0200b2011-04-13 17:58:08 -07001134void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001135 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001136}
1137
Romain Guyed6fcb02011-03-21 13:11:28 -07001138void OpenGLRenderer::setupDrawPoint(float pointSize) {
1139 mDescription.isPoint = true;
1140 mDescription.pointSize = pointSize;
1141}
1142
Romain Guy70ca14e2010-12-13 18:24:33 -08001143void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001144 setupDrawColor(color, (color >> 24) & 0xFF);
1145}
1146
1147void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1148 mColorA = alpha / 255.0f;
Chet Haasedb8c9a62012-03-21 18:54:18 -07001149 mColorA *= mSnapshot->alpha;
Chet Haase6cfdf452011-04-22 16:42:10 -07001150 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1151 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001152 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001153 mColorR = a * ((color >> 16) & 0xFF);
1154 mColorG = a * ((color >> 8) & 0xFF);
1155 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001156 mColorSet = true;
1157 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1158}
1159
Romain Guy86568192010-12-14 15:55:39 -08001160void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1161 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001162 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1163 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001164 const float a = mColorA / 255.0f;
1165 mColorR = a * ((color >> 16) & 0xFF);
1166 mColorG = a * ((color >> 8) & 0xFF);
1167 mColorB = a * ((color ) & 0xFF);
1168 mColorSet = true;
1169 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1170}
1171
Romain Guy70ca14e2010-12-13 18:24:33 -08001172void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1173 mColorA = a;
1174 mColorR = r;
1175 mColorG = g;
1176 mColorB = b;
1177 mColorSet = true;
1178 mSetShaderColor = mDescription.setColor(r, g, b, a);
1179}
1180
Romain Guy86568192010-12-14 15:55:39 -08001181void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1182 mColorA = a;
1183 mColorR = r;
1184 mColorG = g;
1185 mColorB = b;
1186 mColorSet = true;
1187 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1188}
1189
Romain Guy70ca14e2010-12-13 18:24:33 -08001190void OpenGLRenderer::setupDrawShader() {
1191 if (mShader) {
1192 mShader->describe(mDescription, mCaches.extensions);
1193 }
1194}
1195
1196void OpenGLRenderer::setupDrawColorFilter() {
1197 if (mColorFilter) {
1198 mColorFilter->describe(mDescription, mCaches.extensions);
1199 }
1200}
1201
Romain Guyf09ef512011-05-27 11:43:46 -07001202void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1203 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1204 mColorA = 1.0f;
1205 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001206 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001207 }
1208}
1209
Romain Guy70ca14e2010-12-13 18:24:33 -08001210void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001211 // When the blending mode is kClear_Mode, we need to use a modulate color
1212 // argb=1,0,0,0
1213 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001214 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1215 mDescription, swapSrcDst);
1216}
1217
1218void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001219 // When the blending mode is kClear_Mode, we need to use a modulate color
1220 // argb=1,0,0,0
1221 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001222 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1223 mDescription, swapSrcDst);
1224}
1225
1226void OpenGLRenderer::setupDrawProgram() {
1227 useProgram(mCaches.programCache.get(mDescription));
1228}
1229
1230void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1231 mTrackDirtyRegions = false;
1232}
1233
1234void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1235 bool ignoreTransform) {
1236 mModelView.loadTranslate(left, top, 0.0f);
1237 if (!ignoreTransform) {
1238 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1239 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1240 } else {
1241 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1242 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1243 }
1244}
1245
Chet Haase8a5cc922011-04-26 07:28:09 -07001246void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1247 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001248}
1249
Romain Guy70ca14e2010-12-13 18:24:33 -08001250void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1251 bool ignoreTransform, bool ignoreModelView) {
1252 if (!ignoreModelView) {
1253 mModelView.loadTranslate(left, top, 0.0f);
1254 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001255 } else {
1256 mModelView.loadIdentity();
1257 }
Romain Guy86568192010-12-14 15:55:39 -08001258 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1259 if (!ignoreTransform) {
1260 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1261 if (mTrackDirtyRegions && dirty) {
1262 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1263 }
1264 } else {
1265 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1266 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1267 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001268}
1269
Romain Guyed6fcb02011-03-21 13:11:28 -07001270void OpenGLRenderer::setupDrawPointUniforms() {
1271 int slot = mCaches.currentProgram->getUniform("pointSize");
1272 glUniform1f(slot, mDescription.pointSize);
1273}
1274
Romain Guy70ca14e2010-12-13 18:24:33 -08001275void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001276 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001277 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1278 }
1279}
1280
Romain Guy86568192010-12-14 15:55:39 -08001281void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001282 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001283 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001284 }
1285}
1286
Romain Guy70ca14e2010-12-13 18:24:33 -08001287void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1288 if (mShader) {
1289 if (ignoreTransform) {
1290 mModelView.loadInverse(*mSnapshot->transform);
1291 }
1292 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1293 }
1294}
1295
Romain Guy8d0d4782010-12-14 20:13:35 -08001296void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1297 if (mShader) {
1298 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1299 }
1300}
1301
Romain Guy70ca14e2010-12-13 18:24:33 -08001302void OpenGLRenderer::setupDrawColorFilterUniforms() {
1303 if (mColorFilter) {
1304 mColorFilter->setupProgram(mCaches.currentProgram);
1305 }
1306}
1307
1308void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001309 bool force = mCaches.bindMeshBuffer();
1310 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001311 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001312}
1313
1314void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1315 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001316 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001317 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001318}
1319
Romain Guyaa6c24c2011-04-28 18:40:04 -07001320void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1321 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001322 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001323 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001324}
1325
Romain Guy8f0095c2011-05-02 17:24:22 -07001326void OpenGLRenderer::setupDrawTextureTransform() {
1327 mDescription.hasTextureTransform = true;
1328}
1329
1330void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001331 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1332 GL_FALSE, &transform.data[0]);
1333}
1334
Romain Guy70ca14e2010-12-13 18:24:33 -08001335void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001336 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001337 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001338 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001339 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001340 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001341 }
Romain Guyd71dd362011-12-12 19:03:35 -08001342
Romain Guyf3a910b42011-12-12 20:35:21 -08001343 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001344 if (mCaches.currentProgram->texCoords >= 0) {
1345 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1346 }
1347
1348 mCaches.unbindIndicesBuffer();
1349}
1350
1351void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1352 bool force = mCaches.unbindMeshBuffer();
1353 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1354 if (mCaches.currentProgram->texCoords >= 0) {
1355 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001356 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001357}
1358
Chet Haase5b0200b2011-04-13 17:58:08 -07001359void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001360 bool force = mCaches.unbindMeshBuffer();
1361 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1362 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001363 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001364}
1365
1366/**
1367 * 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 -07001368 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1369 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1370 * attributes (one per vertex) are values from zero to one that tells the fragment
1371 * shader where the fragment is in relation to the line width/length overall; these values are
1372 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1373 * region of the line.
1374 * Note that we only pass down the width values in this setup function. The length coordinates
1375 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001376 */
Chet Haase99585ad2011-05-02 15:00:16 -07001377void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001378 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001379 bool force = mCaches.unbindMeshBuffer();
1380 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1381 vertices, gAAVertexStride);
1382 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001383 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001384
Romain Guy7b631422012-04-04 11:38:54 -07001385 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001386 glEnableVertexAttribArray(widthSlot);
1387 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001388
Romain Guy7b631422012-04-04 11:38:54 -07001389 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001390 glEnableVertexAttribArray(lengthSlot);
1391 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001392
Chet Haase5b0200b2011-04-13 17:58:08 -07001393 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001394 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001395
Chet Haase99585ad2011-05-02 15:00:16 -07001396 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001397 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Romain Guy7b631422012-04-04 11:38:54 -07001398 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion);
1399}
1400
1401void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1402 glDisableVertexAttribArray(widthSlot);
1403 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001404}
1405
Romain Guy70ca14e2010-12-13 18:24:33 -08001406void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001407}
1408
1409///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001410// Drawing
1411///////////////////////////////////////////////////////////////////////////////
1412
Chet Haase1271e2c2012-04-20 09:54:27 -07001413status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001414 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001415
Romain Guy0fe478e2010-11-08 12:08:41 -08001416 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1417 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001418 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001419 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001420 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001421
Romain Guy65549432012-03-26 16:45:05 -07001422 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001423}
1424
Chet Haaseed30fd82011-04-22 16:18:45 -07001425void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1426 if (displayList) {
1427 displayList->output(*this, level);
1428 }
1429}
1430
Romain Guya168d732011-03-18 16:50:13 -07001431void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1432 int alpha;
1433 SkXfermode::Mode mode;
1434 getAlphaAndMode(paint, &alpha, &mode);
1435
Romain Guya168d732011-03-18 16:50:13 -07001436 float x = left;
1437 float y = top;
1438
Romain Guye3c26852011-07-25 16:36:01 -07001439 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001440 bool ignoreTransform = false;
1441 if (mSnapshot->transform->isPureTranslate()) {
1442 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1443 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1444 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001445 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001446 } else {
1447 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001448 }
1449
1450 setupDraw();
1451 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001452 if (paint) {
1453 setupDrawAlpha8Color(paint->getColor(), alpha);
1454 }
Romain Guya168d732011-03-18 16:50:13 -07001455 setupDrawColorFilter();
1456 setupDrawShader();
1457 setupDrawBlending(true, mode);
1458 setupDrawProgram();
1459 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001460
Romain Guya168d732011-03-18 16:50:13 -07001461 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001462 texture->setWrap(GL_CLAMP_TO_EDGE);
1463 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001464
Romain Guya168d732011-03-18 16:50:13 -07001465 setupDrawPureColorUniforms();
1466 setupDrawColorFilterUniforms();
1467 setupDrawShaderUniforms();
1468 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1469
1470 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1471
1472 finishDrawTexture();
1473}
1474
Chet Haase48659092012-05-31 15:21:51 -07001475status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001476 const float right = left + bitmap->width();
1477 const float bottom = top + bitmap->height();
1478
1479 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001480 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001481 }
1482
Romain Guya1d3c912011-12-13 14:55:06 -08001483 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001484 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001485 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001486 const AutoTexture autoCleanup(texture);
1487
Romain Guy211370f2012-02-01 16:10:55 -08001488 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001489 drawAlphaBitmap(texture, left, top, paint);
1490 } else {
1491 drawTextureRect(left, top, right, bottom, texture, paint);
1492 }
Chet Haase48659092012-05-31 15:21:51 -07001493
1494 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001495}
1496
Chet Haase48659092012-05-31 15:21:51 -07001497status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001498 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1499 const mat4 transform(*matrix);
1500 transform.mapRect(r);
1501
Romain Guy6926c722010-07-12 20:20:03 -07001502 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001503 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001504 }
1505
Romain Guya1d3c912011-12-13 14:55:06 -08001506 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001507 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001508 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001509 const AutoTexture autoCleanup(texture);
1510
Romain Guy5b3b3522010-10-27 18:57:51 -07001511 // This could be done in a cheaper way, all we need is pass the matrix
1512 // to the vertex shader. The save/restore is a bit overkill.
1513 save(SkCanvas::kMatrix_SaveFlag);
1514 concatMatrix(matrix);
1515 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1516 restore();
Chet Haase48659092012-05-31 15:21:51 -07001517
1518 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001519}
1520
Chet Haase48659092012-05-31 15:21:51 -07001521status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001522 const float right = left + bitmap->width();
1523 const float bottom = top + bitmap->height();
1524
1525 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001526 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001527 }
1528
1529 mCaches.activeTexture(0);
1530 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1531 const AutoTexture autoCleanup(texture);
1532
1533 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001534
1535 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001536}
1537
Chet Haase48659092012-05-31 15:21:51 -07001538status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001539 float* vertices, int* colors, SkPaint* paint) {
1540 // TODO: Do a quickReject
1541 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001542 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001543 }
1544
Romain Guya1d3c912011-12-13 14:55:06 -08001545 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001546 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001547 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001548 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001549
Romain Guyd21b6e12011-11-30 20:21:23 -08001550 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1551 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001552
1553 int alpha;
1554 SkXfermode::Mode mode;
1555 getAlphaAndMode(paint, &alpha, &mode);
1556
Romain Guy5a7b4662011-01-20 19:09:30 -08001557 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001558
Romain Guyb18d2d02011-02-10 15:52:54 -08001559 float left = FLT_MAX;
1560 float top = FLT_MAX;
1561 float right = FLT_MIN;
1562 float bottom = FLT_MIN;
1563
1564#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001565 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001566#else
Romain Guy211370f2012-02-01 16:10:55 -08001567 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001568#endif
1569
Romain Guya566b7c2011-01-23 16:36:11 -08001570 // TODO: Support the colors array
1571 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001572 TextureVertex* vertex = mesh;
1573 for (int32_t y = 0; y < meshHeight; y++) {
1574 for (int32_t x = 0; x < meshWidth; x++) {
1575 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1576
1577 float u1 = float(x) / meshWidth;
1578 float u2 = float(x + 1) / meshWidth;
1579 float v1 = float(y) / meshHeight;
1580 float v2 = float(y + 1) / meshHeight;
1581
1582 int ax = i + (meshWidth + 1) * 2;
1583 int ay = ax + 1;
1584 int bx = i;
1585 int by = bx + 1;
1586 int cx = i + 2;
1587 int cy = cx + 1;
1588 int dx = i + (meshWidth + 1) * 2 + 2;
1589 int dy = dx + 1;
1590
1591 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1592 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1593 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1594
1595 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1596 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1597 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001598
1599#if RENDER_LAYERS_AS_REGIONS
1600 if (hasActiveLayer) {
1601 // TODO: This could be optimized to avoid unnecessary ops
1602 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1603 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1604 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1605 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1606 }
1607#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001608 }
1609 }
1610
Romain Guyb18d2d02011-02-10 15:52:54 -08001611#if RENDER_LAYERS_AS_REGIONS
1612 if (hasActiveLayer) {
1613 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1614 }
1615#endif
1616
Romain Guy5a7b4662011-01-20 19:09:30 -08001617 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1618 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001619 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001620
1621 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001622}
1623
Chet Haase48659092012-05-31 15:21:51 -07001624status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001625 float srcLeft, float srcTop, float srcRight, float srcBottom,
1626 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001627 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001628 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001629 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001630 }
1631
Romain Guya1d3c912011-12-13 14:55:06 -08001632 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001633 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001634 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001635 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001636
Romain Guy8ba548f2010-06-30 19:21:21 -07001637 const float width = texture->width;
1638 const float height = texture->height;
1639
Romain Guy68169722011-08-22 17:33:33 -07001640 const float u1 = fmax(0.0f, srcLeft / width);
1641 const float v1 = fmax(0.0f, srcTop / height);
1642 const float u2 = fmin(1.0f, srcRight / width);
1643 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001644
Romain Guy03750a02010-10-18 14:06:08 -07001645 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001646 resetDrawTextureTexCoords(u1, v1, u2, v2);
1647
Romain Guy03750a02010-10-18 14:06:08 -07001648 int alpha;
1649 SkXfermode::Mode mode;
1650 getAlphaAndMode(paint, &alpha, &mode);
1651
Romain Guyd21b6e12011-11-30 20:21:23 -08001652 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1653
Romain Guy211370f2012-02-01 16:10:55 -08001654 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001655 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1656 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1657
Romain Guyb5014982011-07-28 15:39:12 -07001658 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001659 // Enable linear filtering if the source rectangle is scaled
1660 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001661 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001662 }
Romain Guyb5014982011-07-28 15:39:12 -07001663
Romain Guyd21b6e12011-11-30 20:21:23 -08001664 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001665 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1666 texture->id, alpha / 255.0f, mode, texture->blend,
1667 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1668 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1669 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001670 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001671 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1672 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1673 GL_TRIANGLE_STRIP, gMeshCount);
1674 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001675
1676 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001677
1678 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001679}
1680
Chet Haase48659092012-05-31 15:21:51 -07001681status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001682 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001683 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001684 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001685 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001686 }
1687
Romain Guya1d3c912011-12-13 14:55:06 -08001688 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001689 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001690 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001691 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001692 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1693 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001694
1695 int alpha;
1696 SkXfermode::Mode mode;
1697 getAlphaAndMode(paint, &alpha, &mode);
1698
Romain Guy2728f962010-10-08 18:36:15 -07001699 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001700 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001701
Romain Guy211370f2012-02-01 16:10:55 -08001702 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001703 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001704#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001705 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001706 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001707 const float offsetX = left + mSnapshot->transform->getTranslateX();
1708 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001709 const size_t count = mesh->quads.size();
1710 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001711 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001712 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001713 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1714 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1715 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001716 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001717 dirtyLayer(left + bounds.left, top + bounds.top,
1718 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001719 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001720 }
1721 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001722#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001723
Romain Guy211370f2012-02-01 16:10:55 -08001724 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001725 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1726 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1727
1728 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1729 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1730 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1731 true, !mesh->hasEmptyQuads);
1732 } else {
1733 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1734 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1735 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1736 true, !mesh->hasEmptyQuads);
1737 }
Romain Guy054dc182010-10-15 17:55:25 -07001738 }
Chet Haase48659092012-05-31 15:21:51 -07001739
1740 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001741}
1742
Chet Haase99ecdc42011-05-06 12:06:34 -07001743/**
Chet Haase858aa932011-05-12 09:06:00 -07001744 * This function uses a similar approach to that of AA lines in the drawLines() function.
1745 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1746 * shader to compute the translucency of the color, determined by whether a given pixel is
1747 * within that boundary region and how far into the region it is.
1748 */
1749void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001750 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001751 float inverseScaleX = 1.0f;
1752 float inverseScaleY = 1.0f;
1753 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001754 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001755 Matrix4 *mat = mSnapshot->transform;
1756 float m00 = mat->data[Matrix4::kScaleX];
1757 float m01 = mat->data[Matrix4::kSkewY];
1758 float m02 = mat->data[2];
1759 float m10 = mat->data[Matrix4::kSkewX];
1760 float m11 = mat->data[Matrix4::kScaleX];
1761 float m12 = mat->data[6];
1762 float scaleX = sqrt(m00 * m00 + m01 * m01);
1763 float scaleY = sqrt(m10 * m10 + m11 * m11);
1764 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1765 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1766 }
1767
1768 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001769 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001770 setupDrawAALine();
1771 setupDrawColor(color);
1772 setupDrawColorFilter();
1773 setupDrawShader();
1774 setupDrawBlending(true, mode);
1775 setupDrawProgram();
1776 setupDrawModelViewIdentity(true);
1777 setupDrawColorUniforms();
1778 setupDrawColorFilterUniforms();
1779 setupDrawShaderIdentityUniforms();
1780
1781 AAVertex rects[4];
1782 AAVertex* aaVertices = &rects[0];
1783 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1784 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1785
1786 float boundarySizeX = .5 * inverseScaleX;
1787 float boundarySizeY = .5 * inverseScaleY;
1788
1789 // Adjust the rect by the AA boundary padding
1790 left -= boundarySizeX;
1791 right += boundarySizeX;
1792 top -= boundarySizeY;
1793 bottom += boundarySizeY;
1794
1795 float width = right - left;
1796 float height = bottom - top;
1797
Romain Guy7b631422012-04-04 11:38:54 -07001798 int widthSlot;
1799 int lengthSlot;
1800
Chet Haase858aa932011-05-12 09:06:00 -07001801 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1802 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001803 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1804 boundaryWidthProportion, widthSlot, lengthSlot);
1805
Chet Haase858aa932011-05-12 09:06:00 -07001806 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1807 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1808 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001809 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion));
Chet Haase858aa932011-05-12 09:06:00 -07001810
1811 if (!quickReject(left, top, right, bottom)) {
1812 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1813 AAVertex::set(aaVertices++, left, top, 1, 0);
1814 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1815 AAVertex::set(aaVertices++, right, top, 0, 0);
1816 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1817 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1818 }
Romain Guy7b631422012-04-04 11:38:54 -07001819
1820 finishDrawAALine(widthSlot, lengthSlot);
Chet Haase858aa932011-05-12 09:06:00 -07001821}
1822
1823/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001824 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1825 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1826 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1827 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1828 * of the line. Hairlines are more involved because we need to account for transform scaling
1829 * to end up with a one-pixel-wide line in screen space..
1830 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1831 * in combination with values that we calculate and pass down in this method. The basic approach
1832 * is that the quad we create contains both the core line area plus a bounding area in which
1833 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1834 * proportion of the width and the length of a given segment is represented by the boundary
1835 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1836 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1837 * on the inside). This ends up giving the result we want, with pixels that are completely
1838 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1839 * how far into the boundary region they are, which is determined by shader interpolation.
1840 */
Chet Haase48659092012-05-31 15:21:51 -07001841status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1842 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001843
1844 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001845 // We use half the stroke width here because we're going to position the quad
1846 // corner vertices half of the width away from the line endpoints
1847 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001848 // A stroke width of 0 has a special meaning in Skia:
1849 // it draws a line 1 px wide regardless of current transform
1850 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001851
Chet Haase99ecdc42011-05-06 12:06:34 -07001852 float inverseScaleX = 1.0f;
1853 float inverseScaleY = 1.0f;
1854 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001855
Chet Haase8a5cc922011-04-26 07:28:09 -07001856 int alpha;
1857 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001858
Chet Haase8a5cc922011-04-26 07:28:09 -07001859 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001860 int verticesCount = count;
1861 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001862 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001863 verticesCount += (count - 4);
1864 }
1865
1866 if (isHairLine || isAA) {
1867 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1868 // the line on the screen should always be one pixel wide regardless of scale. For
1869 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001870 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001871 Matrix4 *mat = mSnapshot->transform;
1872 float m00 = mat->data[Matrix4::kScaleX];
1873 float m01 = mat->data[Matrix4::kSkewY];
1874 float m02 = mat->data[2];
1875 float m10 = mat->data[Matrix4::kSkewX];
1876 float m11 = mat->data[Matrix4::kScaleX];
1877 float m12 = mat->data[6];
Romain Guy7b631422012-04-04 11:38:54 -07001878
Romain Guy211370f2012-02-01 16:10:55 -08001879 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1880 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001881
Chet Haase99ecdc42011-05-06 12:06:34 -07001882 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1883 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001884
Chet Haase99ecdc42011-05-06 12:06:34 -07001885 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1886 scaled = true;
1887 }
1888 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001889 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001890
1891 getAlphaAndMode(paint, &alpha, &mode);
1892 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001893 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001894 if (isAA) {
1895 setupDrawAALine();
1896 }
1897 setupDrawColor(paint->getColor(), alpha);
1898 setupDrawColorFilter();
1899 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001900 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001901 setupDrawProgram();
1902 setupDrawModelViewIdentity(true);
1903 setupDrawColorUniforms();
1904 setupDrawColorFilterUniforms();
1905 setupDrawShaderIdentityUniforms();
1906
1907 if (isHairLine) {
1908 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001909 halfStrokeWidth = isAA? 1 : .5;
1910 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001911 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001912 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001913 }
Romain Guy7b631422012-04-04 11:38:54 -07001914
1915 int widthSlot;
1916 int lengthSlot;
1917
Chet Haase5b0200b2011-04-13 17:58:08 -07001918 Vertex lines[verticesCount];
1919 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001920
Chet Haase99585ad2011-05-02 15:00:16 -07001921 AAVertex wLines[verticesCount];
1922 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001923
Romain Guy211370f2012-02-01 16:10:55 -08001924 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001925 setupDrawVertices(vertices);
1926 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001927 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1928 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001929 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001930 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1931 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001932 // We will need to calculate the actual width proportion on each segment for
1933 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1934 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001935 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1936 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001937 }
1938
Chet Haase99ecdc42011-05-06 12:06:34 -07001939 AAVertex* prevAAVertex = NULL;
1940 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001941
Chet Haase99585ad2011-05-02 15:00:16 -07001942 int boundaryLengthSlot = -1;
1943 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001944 int boundaryWidthSlot = -1;
1945 int inverseBoundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001946
Chet Haase5b0200b2011-04-13 17:58:08 -07001947 for (int i = 0; i < count; i += 4) {
1948 // a = start point, b = end point
1949 vec2 a(points[i], points[i + 1]);
1950 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001951
Chet Haase99585ad2011-05-02 15:00:16 -07001952 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001953 float boundaryLengthProportion = 0;
1954 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001955
Chet Haase5b0200b2011-04-13 17:58:08 -07001956 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001957 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001958 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001959 if (isAA) {
1960 float wideningFactor;
1961 if (fabs(n.x) >= fabs(n.y)) {
1962 wideningFactor = fabs(1.0f / n.x);
1963 } else {
1964 wideningFactor = fabs(1.0f / n.y);
1965 }
1966 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001967 }
Romain Guy7b631422012-04-04 11:38:54 -07001968
Chet Haase99ecdc42011-05-06 12:06:34 -07001969 if (scaled) {
1970 n.x *= inverseScaleX;
1971 n.y *= inverseScaleY;
1972 }
1973 } else if (scaled) {
1974 // Extend n by .5 pixel on each side, post-transform
1975 vec2 extendedN = n.copyNormalized();
1976 extendedN /= 2;
1977 extendedN.x *= inverseScaleX;
1978 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07001979
Chet Haase99ecdc42011-05-06 12:06:34 -07001980 float extendedNLength = extendedN.length();
1981 // We need to set this value on the shader prior to drawing
1982 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1983 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001984 }
Romain Guy7b631422012-04-04 11:38:54 -07001985
Chet Haase5b0200b2011-04-13 17:58:08 -07001986 float x = n.x;
1987 n.x = -n.y;
1988 n.y = x;
1989
Chet Haase99585ad2011-05-02 15:00:16 -07001990 // aa lines expand the endpoint vertices to encompass the AA boundary
1991 if (isAA) {
1992 vec2 abVector = (b - a);
1993 length = abVector.length();
1994 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07001995
Chet Haase99ecdc42011-05-06 12:06:34 -07001996 if (scaled) {
1997 abVector.x *= inverseScaleX;
1998 abVector.y *= inverseScaleY;
1999 float abLength = abVector.length();
2000 boundaryLengthProportion = abLength / (length + abLength);
2001 } else {
2002 boundaryLengthProportion = .5 / (length + 1);
2003 }
Romain Guy7b631422012-04-04 11:38:54 -07002004
Chet Haase99ecdc42011-05-06 12:06:34 -07002005 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002006 a -= abVector;
2007 b += abVector;
2008 }
2009
Chet Haase5b0200b2011-04-13 17:58:08 -07002010 // Four corners of the rectangle defining a thick line
2011 vec2 p1 = a - n;
2012 vec2 p2 = a + n;
2013 vec2 p3 = b + n;
2014 vec2 p4 = b - n;
2015
Chet Haase99585ad2011-05-02 15:00:16 -07002016
Chet Haase5b0200b2011-04-13 17:58:08 -07002017 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2018 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2019 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2020 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2021
2022 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002023 if (!isAA) {
2024 if (prevVertex != NULL) {
2025 // Issue two repeat vertices to create degenerate triangles to bridge
2026 // between the previous line and the new one. This is necessary because
2027 // we are creating a single triangle_strip which will contain
2028 // potentially discontinuous line segments.
2029 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2030 Vertex::set(vertices++, p1.x, p1.y);
2031 generatedVerticesCount += 2;
2032 }
Romain Guy7b631422012-04-04 11:38:54 -07002033
Chet Haase5b0200b2011-04-13 17:58:08 -07002034 Vertex::set(vertices++, p1.x, p1.y);
2035 Vertex::set(vertices++, p2.x, p2.y);
2036 Vertex::set(vertices++, p4.x, p4.y);
2037 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002038
Chet Haase5b0200b2011-04-13 17:58:08 -07002039 prevVertex = vertices - 1;
2040 generatedVerticesCount += 4;
2041 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002042 if (!isHairLine && scaled) {
2043 // Must set width proportions per-segment for scaled non-hairlines to use the
2044 // correct AA boundary dimensions
2045 if (boundaryWidthSlot < 0) {
2046 boundaryWidthSlot =
2047 mCaches.currentProgram->getUniform("boundaryWidth");
2048 inverseBoundaryWidthSlot =
2049 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
2050 }
Romain Guy7b631422012-04-04 11:38:54 -07002051
Chet Haase99ecdc42011-05-06 12:06:34 -07002052 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
2053 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
2054 }
Romain Guy7b631422012-04-04 11:38:54 -07002055
Chet Haase99585ad2011-05-02 15:00:16 -07002056 if (boundaryLengthSlot < 0) {
2057 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
2058 inverseBoundaryLengthSlot =
2059 mCaches.currentProgram->getUniform("inverseBoundaryLength");
2060 }
Romain Guy7b631422012-04-04 11:38:54 -07002061
Chet Haase99ecdc42011-05-06 12:06:34 -07002062 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
2063 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07002064
Chet Haase5b0200b2011-04-13 17:58:08 -07002065 if (prevAAVertex != NULL) {
2066 // Issue two repeat vertices to create degenerate triangles to bridge
2067 // between the previous line and the new one. This is necessary because
2068 // we are creating a single triangle_strip which will contain
2069 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002070 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2071 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2072 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002073 generatedVerticesCount += 2;
2074 }
Romain Guy7b631422012-04-04 11:38:54 -07002075
Chet Haase99585ad2011-05-02 15:00:16 -07002076 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2077 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2078 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2079 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002080
Chet Haase5b0200b2011-04-13 17:58:08 -07002081 prevAAVertex = aaVertices - 1;
2082 generatedVerticesCount += 4;
2083 }
Romain Guy7b631422012-04-04 11:38:54 -07002084
Chet Haase99585ad2011-05-02 15:00:16 -07002085 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2086 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2087 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002088 }
2089 }
Romain Guy7b631422012-04-04 11:38:54 -07002090
Chet Haase5b0200b2011-04-13 17:58:08 -07002091 if (generatedVerticesCount > 0) {
2092 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2093 }
Romain Guy7b631422012-04-04 11:38:54 -07002094
2095 if (isAA) {
2096 finishDrawAALine(widthSlot, lengthSlot);
2097 }
Chet Haase48659092012-05-31 15:21:51 -07002098
2099 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002100}
2101
Chet Haase48659092012-05-31 15:21:51 -07002102status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2103 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002104
2105 // TODO: The paint's cap style defines whether the points are square or circular
2106 // TODO: Handle AA for round points
2107
Chet Haase5b0200b2011-04-13 17:58:08 -07002108 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002109 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002110 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002111 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002112 if (isHairLine) {
2113 // Now that we know it's hairline, we can set the effective width, to be used later
2114 strokeWidth = 1.0f;
2115 }
2116 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002117 int alpha;
2118 SkXfermode::Mode mode;
2119 getAlphaAndMode(paint, &alpha, &mode);
2120
2121 int verticesCount = count >> 1;
2122 int generatedVerticesCount = 0;
2123
2124 TextureVertex pointsData[verticesCount];
2125 TextureVertex* vertex = &pointsData[0];
2126
2127 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002128 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002129 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002130 setupDrawColor(paint->getColor(), alpha);
2131 setupDrawColorFilter();
2132 setupDrawShader();
2133 setupDrawBlending(mode);
2134 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002135 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002136 setupDrawColorUniforms();
2137 setupDrawColorFilterUniforms();
2138 setupDrawPointUniforms();
2139 setupDrawShaderIdentityUniforms();
2140 setupDrawMesh(vertex);
2141
2142 for (int i = 0; i < count; i += 2) {
2143 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2144 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002145
Chet Haase8a5cc922011-04-26 07:28:09 -07002146 float left = points[i] - halfWidth;
2147 float right = points[i] + halfWidth;
2148 float top = points[i + 1] - halfWidth;
2149 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002150
Chet Haase8a5cc922011-04-26 07:28:09 -07002151 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002152 }
2153
2154 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002155
2156 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002157}
2158
Chet Haase48659092012-05-31 15:21:51 -07002159status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002160 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002161 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002162
Romain Guyae88e5e2010-10-22 17:49:18 -07002163 Rect& clip(*mSnapshot->clipRect);
2164 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002165
Romain Guy3d58c032010-07-14 16:34:53 -07002166 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002167
2168 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002169}
Romain Guy9d5316e2010-06-24 19:30:36 -07002170
Chet Haase48659092012-05-31 15:21:51 -07002171status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2172 SkPaint* paint) {
2173 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002174 const AutoTexture autoCleanup(texture);
2175
2176 const float x = left + texture->left - texture->offset;
2177 const float y = top + texture->top - texture->offset;
2178
2179 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002180
2181 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002182}
2183
Chet Haase48659092012-05-31 15:21:51 -07002184status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002185 float rx, float ry, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002186 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002187
Romain Guya1d3c912011-12-13 14:55:06 -08002188 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002189 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2190 right - left, bottom - top, rx, ry, paint);
Chet Haase48659092012-05-31 15:21:51 -07002191 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002192}
2193
Chet Haase48659092012-05-31 15:21:51 -07002194status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2195 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002196
Romain Guya1d3c912011-12-13 14:55:06 -08002197 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002198 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Chet Haase48659092012-05-31 15:21:51 -07002199 return drawShape(x - radius, y - radius, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002200}
Romain Guy01d58e42011-01-19 21:54:02 -08002201
Chet Haase48659092012-05-31 15:21:51 -07002202status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
2203 SkPaint* paint) {
2204 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002205
Romain Guya1d3c912011-12-13 14:55:06 -08002206 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002207 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002208 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002209}
2210
Chet Haase48659092012-05-31 15:21:51 -07002211status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002212 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002213 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002214
2215 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002216 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002217 }
2218
Romain Guya1d3c912011-12-13 14:55:06 -08002219 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002220 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2221 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002222 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002223}
2224
Chet Haase48659092012-05-31 15:21:51 -07002225status_t OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002226 SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002227 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002228
Romain Guya1d3c912011-12-13 14:55:06 -08002229 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002230 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002231 return drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002232}
2233
Chet Haase48659092012-05-31 15:21:51 -07002234status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002235 if (p->getStyle() != SkPaint::kFill_Style) {
Chet Haase48659092012-05-31 15:21:51 -07002236 return drawRectAsShape(left, top, right, bottom, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002237 }
2238
Romain Guy6926c722010-07-12 20:20:03 -07002239 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002240 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002241 }
2242
Romain Guy026c5e162010-06-28 17:12:22 -07002243 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002244 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002245 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2246 if (!isMode) {
2247 // Assume SRC_OVER
2248 mode = SkXfermode::kSrcOver_Mode;
2249 }
2250 } else {
2251 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002252 }
2253
Romain Guy026c5e162010-06-28 17:12:22 -07002254 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002255 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002256 drawAARect(left, top, right, bottom, color, mode);
2257 } else {
2258 drawColorRect(left, top, right, bottom, color, mode);
2259 }
Chet Haase48659092012-05-31 15:21:51 -07002260
2261 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002262}
Romain Guy9d5316e2010-06-24 19:30:36 -07002263
Chet Haase48659092012-05-31 15:21:51 -07002264status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002265 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002266 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2267 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002268 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002269 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002270
Romain Guy671d6cf2012-01-18 12:39:17 -08002271 // NOTE: Skia does not support perspective transform on drawPosText yet
2272 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002273 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002274 }
2275
2276 float x = 0.0f;
2277 float y = 0.0f;
2278 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2279 if (pureTranslate) {
2280 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2281 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2282 }
2283
2284 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2285 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2286 paint->getTextSize());
2287
2288 int alpha;
2289 SkXfermode::Mode mode;
2290 getAlphaAndMode(paint, &alpha, &mode);
2291
2292 // Pick the appropriate texture filtering
2293 bool linearFilter = mSnapshot->transform->changesBounds();
2294 if (pureTranslate && !linearFilter) {
2295 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2296 }
2297
2298 mCaches.activeTexture(0);
2299 setupDraw();
2300 setupDrawDirtyRegionsDisabled();
2301 setupDrawWithTexture(true);
2302 setupDrawAlpha8Color(paint->getColor(), alpha);
2303 setupDrawColorFilter();
2304 setupDrawShader();
2305 setupDrawBlending(true, mode);
2306 setupDrawProgram();
2307 setupDrawModelView(x, y, x, y, pureTranslate, true);
2308 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2309 setupDrawPureColorUniforms();
2310 setupDrawColorFilterUniforms();
2311 setupDrawShaderUniforms(pureTranslate);
2312
2313 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2314 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2315
2316#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002317 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002318#else
Romain Guy211370f2012-02-01 16:10:55 -08002319 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002320#endif
2321
2322 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2323 positions, hasActiveLayer ? &bounds : NULL)) {
2324#if RENDER_LAYERS_AS_REGIONS
2325 if (hasActiveLayer) {
2326 if (!pureTranslate) {
2327 mSnapshot->transform->mapRect(bounds);
2328 }
2329 dirtyLayerUnchecked(bounds, getRegion());
2330 }
2331#endif
2332 }
Chet Haase48659092012-05-31 15:21:51 -07002333
2334 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002335}
2336
Chet Haase48659092012-05-31 15:21:51 -07002337status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002338 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002339 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2340 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002341 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002342 }
2343
Chet Haasea1cff502012-02-21 13:43:44 -08002344 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002345 switch (paint->getTextAlign()) {
2346 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002347 x -= length / 2.0f;
2348 break;
2349 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002350 x -= length;
2351 break;
2352 default:
2353 break;
2354 }
2355
Romain Guycac5fd32011-12-01 20:08:50 -08002356 SkPaint::FontMetrics metrics;
2357 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002358 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002359 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002360 }
2361
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002362 const float oldX = x;
2363 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002364 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002365 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002366 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2367 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2368 }
2369
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002370#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002371 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002372#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002373
2374 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002375 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002376 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002377
Romain Guy86568192010-12-14 15:55:39 -08002378 int alpha;
2379 SkXfermode::Mode mode;
2380 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002381
Romain Guy211370f2012-02-01 16:10:55 -08002382 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002383 mCaches.activeTexture(0);
2384
Romain Guyb45c0c92010-08-26 20:35:23 -07002385 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002386 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2387 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002388 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002389
Romain Guy740bf2b2011-04-26 15:33:10 -07002390 const float sx = oldX - shadow->left + mShadowDx;
2391 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002392
Romain Guy86568192010-12-14 15:55:39 -08002393 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002394 int shadowColor = mShadowColor;
2395 if (mShader) {
2396 shadowColor = 0xffffffff;
2397 }
Romain Guy86568192010-12-14 15:55:39 -08002398
Romain Guy86568192010-12-14 15:55:39 -08002399 setupDraw();
2400 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002401 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2402 setupDrawColorFilter();
2403 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002404 setupDrawBlending(true, mode);
2405 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002406 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002407 setupDrawTexture(shadow->id);
2408 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002409 setupDrawColorFilterUniforms();
2410 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002411 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2412
Romain Guy0a417492010-08-16 20:26:20 -07002413 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002414 }
2415
Romain Guy6620c6d2010-12-06 18:07:02 -08002416 // Pick the appropriate texture filtering
2417 bool linearFilter = mSnapshot->transform->changesBounds();
2418 if (pureTranslate && !linearFilter) {
2419 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2420 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002421
Romain Guya1d3c912011-12-13 14:55:06 -08002422 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002423 setupDraw();
2424 setupDrawDirtyRegionsDisabled();
2425 setupDrawWithTexture(true);
2426 setupDrawAlpha8Color(paint->getColor(), alpha);
2427 setupDrawColorFilter();
2428 setupDrawShader();
2429 setupDrawBlending(true, mode);
2430 setupDrawProgram();
2431 setupDrawModelView(x, y, x, y, pureTranslate, true);
2432 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2433 setupDrawPureColorUniforms();
2434 setupDrawColorFilterUniforms();
2435 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002436
Romain Guy6620c6d2010-12-06 18:07:02 -08002437 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002438 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2439
2440#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002441 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002442#else
Romain Guy211370f2012-02-01 16:10:55 -08002443 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002444#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002445
Romain Guy6620c6d2010-12-06 18:07:02 -08002446 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002447 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002448#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002449 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002450 if (!pureTranslate) {
2451 mSnapshot->transform->mapRect(bounds);
2452 }
Romain Guyf219da52011-01-16 12:54:25 -08002453 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002454 }
2455#endif
2456 }
Romain Guy694b5192010-07-21 21:33:20 -07002457
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002458 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002459
2460 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002461}
2462
Chet Haase48659092012-05-31 15:21:51 -07002463status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002464 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002465 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2466 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002467 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002468 }
2469
Romain Guy03d58522012-02-24 17:54:07 -08002470 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2471 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2472 paint->getTextSize());
2473
2474 int alpha;
2475 SkXfermode::Mode mode;
2476 getAlphaAndMode(paint, &alpha, &mode);
2477
2478 mCaches.activeTexture(0);
2479 setupDraw();
2480 setupDrawDirtyRegionsDisabled();
2481 setupDrawWithTexture(true);
2482 setupDrawAlpha8Color(paint->getColor(), alpha);
2483 setupDrawColorFilter();
2484 setupDrawShader();
2485 setupDrawBlending(true, mode);
2486 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002487 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002488 setupDrawTexture(fontRenderer.getTexture(true));
2489 setupDrawPureColorUniforms();
2490 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002491 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002492
Romain Guy97771732012-02-28 18:17:02 -08002493 const Rect* clip = &mSnapshot->getLocalClip();
2494 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 -08002495
Romain Guy97771732012-02-28 18:17:02 -08002496#if RENDER_LAYERS_AS_REGIONS
2497 const bool hasActiveLayer = hasLayer();
2498#else
2499 const bool hasActiveLayer = false;
2500#endif
2501
2502 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2503 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2504#if RENDER_LAYERS_AS_REGIONS
2505 if (hasActiveLayer) {
2506 mSnapshot->transform->mapRect(bounds);
2507 dirtyLayerUnchecked(bounds, getRegion());
2508 }
2509#endif
2510 }
Chet Haase48659092012-05-31 15:21:51 -07002511
2512 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002513}
2514
Chet Haase48659092012-05-31 15:21:51 -07002515status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2516 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002517
Romain Guya1d3c912011-12-13 14:55:06 -08002518 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002519
Romain Guy33f6beb2012-02-16 19:24:51 -08002520 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002521 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002522 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002523 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002524
Romain Guy8b55f372010-08-18 17:10:07 -07002525 const float x = texture->left - texture->offset;
2526 const float y = texture->top - texture->offset;
2527
Romain Guy01d58e42011-01-19 21:54:02 -08002528 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002529
2530 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002531}
2532
Chet Haase48659092012-05-31 15:21:51 -07002533status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guyada830f2011-01-13 12:13:20 -08002534 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Chet Haase48659092012-05-31 15:21:51 -07002535 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002536 }
2537
Romain Guy2bf68f02012-03-02 13:37:47 -08002538 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2539 OpenGLRenderer* renderer = layer->renderer;
2540 Rect& dirty = layer->dirtyRect;
2541
2542 interrupt();
2543 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2544 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002545 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002546 renderer->finish();
2547 resume();
2548
2549 dirty.setEmpty();
2550 layer->deferredUpdateScheduled = false;
2551 layer->renderer = NULL;
2552 layer->displayList = NULL;
2553 }
2554
Romain Guya1d3c912011-12-13 14:55:06 -08002555 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002556
2557 int alpha;
2558 SkXfermode::Mode mode;
2559 getAlphaAndMode(paint, &alpha, &mode);
2560
Romain Guy9ace8f52011-07-07 20:50:11 -07002561 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002562
Romain Guyf219da52011-01-16 12:54:25 -08002563#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002564 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002565 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002566 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002567 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002568 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002569 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002570
Romain Guyc88e3572011-01-22 00:32:12 -08002571 setupDraw();
2572 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002573 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002574 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002575 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002576 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002577 setupDrawPureColorUniforms();
2578 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002579 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002580 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002581 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2582 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2583
Romain Guyd21b6e12011-11-30 20:21:23 -08002584 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002585 setupDrawModelViewTranslate(x, y,
2586 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2587 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002588 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002589 setupDrawModelViewTranslate(x, y,
2590 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2591 }
Romain Guyc88e3572011-01-22 00:32:12 -08002592 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002593
Romain Guyc88e3572011-01-22 00:32:12 -08002594 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2595 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002596
Romain Guyc88e3572011-01-22 00:32:12 -08002597 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002598
2599#if DEBUG_LAYERS_AS_REGIONS
2600 drawRegionRects(layer->region);
2601#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002602 }
Romain Guyf219da52011-01-16 12:54:25 -08002603 }
2604#else
Romain Guyada830f2011-01-13 12:13:20 -08002605 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2606 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002607#endif
Chet Haase48659092012-05-31 15:21:51 -07002608
2609 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002610}
2611
Romain Guy6926c722010-07-12 20:20:03 -07002612///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002613// Shaders
2614///////////////////////////////////////////////////////////////////////////////
2615
2616void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002617 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002618}
2619
Romain Guy06f96e22010-07-30 19:18:16 -07002620void OpenGLRenderer::setupShader(SkiaShader* shader) {
2621 mShader = shader;
2622 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002623 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002624 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002625}
2626
Romain Guyd27977d2010-07-14 19:18:51 -07002627///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002628// Color filters
2629///////////////////////////////////////////////////////////////////////////////
2630
2631void OpenGLRenderer::resetColorFilter() {
2632 mColorFilter = NULL;
2633}
2634
2635void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2636 mColorFilter = filter;
2637}
2638
2639///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002640// Drop shadow
2641///////////////////////////////////////////////////////////////////////////////
2642
2643void OpenGLRenderer::resetShadow() {
2644 mHasShadow = false;
2645}
2646
2647void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2648 mHasShadow = true;
2649 mShadowRadius = radius;
2650 mShadowDx = dx;
2651 mShadowDy = dy;
2652 mShadowColor = color;
2653}
2654
2655///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002656// Draw filters
2657///////////////////////////////////////////////////////////////////////////////
2658
2659void OpenGLRenderer::resetPaintFilter() {
2660 mHasDrawFilter = false;
2661}
2662
2663void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2664 mHasDrawFilter = true;
2665 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2666 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2667}
2668
2669SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002670 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002671
2672 uint32_t flags = paint->getFlags();
2673
2674 mFilteredPaint = *paint;
2675 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2676
2677 return &mFilteredPaint;
2678}
2679
2680///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002681// Drawing implementation
2682///////////////////////////////////////////////////////////////////////////////
2683
Romain Guy01d58e42011-01-19 21:54:02 -08002684void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2685 float x, float y, SkPaint* paint) {
2686 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2687 return;
2688 }
2689
2690 int alpha;
2691 SkXfermode::Mode mode;
2692 getAlphaAndMode(paint, &alpha, &mode);
2693
2694 setupDraw();
2695 setupDrawWithTexture(true);
2696 setupDrawAlpha8Color(paint->getColor(), alpha);
2697 setupDrawColorFilter();
2698 setupDrawShader();
2699 setupDrawBlending(true, mode);
2700 setupDrawProgram();
2701 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2702 setupDrawTexture(texture->id);
2703 setupDrawPureColorUniforms();
2704 setupDrawColorFilterUniforms();
2705 setupDrawShaderUniforms();
2706 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2707
2708 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2709
2710 finishDrawTexture();
2711}
2712
Romain Guyf607bdc2010-09-10 19:20:06 -07002713// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002714#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2715#define kStdUnderline_Offset (1.0f / 9.0f)
2716#define kStdUnderline_Thickness (1.0f / 18.0f)
2717
2718void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2719 float x, float y, SkPaint* paint) {
2720 // Handle underline and strike-through
2721 uint32_t flags = paint->getFlags();
2722 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002723 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002724 float underlineWidth = length;
2725 // If length is > 0.0f, we already measured the text for the text alignment
2726 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002727 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002728 }
2729
2730 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002731 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002732 case SkPaint::kCenter_Align:
2733 offsetX = underlineWidth * 0.5f;
2734 break;
2735 case SkPaint::kRight_Align:
2736 offsetX = underlineWidth;
2737 break;
2738 default:
2739 break;
2740 }
2741
Romain Guy211370f2012-02-01 16:10:55 -08002742 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002743 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002744 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002745
Romain Guye20ecbd2010-09-22 19:49:04 -07002746 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002747 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002748
Romain Guyf6834472011-01-23 13:32:12 -08002749 int linesCount = 0;
2750 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2751 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2752
2753 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002754 float points[pointsCount];
2755 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002756
2757 if (flags & SkPaint::kUnderlineText_Flag) {
2758 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002759 points[currentPoint++] = left;
2760 points[currentPoint++] = top;
2761 points[currentPoint++] = left + underlineWidth;
2762 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002763 }
2764
2765 if (flags & SkPaint::kStrikeThruText_Flag) {
2766 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002767 points[currentPoint++] = left;
2768 points[currentPoint++] = top;
2769 points[currentPoint++] = left + underlineWidth;
2770 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002771 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002772
Romain Guy726aeba2011-06-01 14:52:00 -07002773 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002774
Romain Guy726aeba2011-06-01 14:52:00 -07002775 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002776 }
2777 }
2778}
2779
Romain Guy026c5e162010-06-28 17:12:22 -07002780void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002781 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002782 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002783 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002784 color |= 0x00ffffff;
2785 }
2786
Romain Guy70ca14e2010-12-13 18:24:33 -08002787 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002788 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002789 setupDrawColor(color);
2790 setupDrawShader();
2791 setupDrawColorFilter();
2792 setupDrawBlending(mode);
2793 setupDrawProgram();
2794 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2795 setupDrawColorUniforms();
2796 setupDrawShaderUniforms(ignoreTransform);
2797 setupDrawColorFilterUniforms();
2798 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002799
Romain Guyc95c8d62010-09-17 15:31:32 -07002800 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2801}
2802
Romain Guy82ba8142010-07-09 13:25:56 -07002803void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002804 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002805 int alpha;
2806 SkXfermode::Mode mode;
2807 getAlphaAndMode(paint, &alpha, &mode);
2808
Romain Guyd21b6e12011-11-30 20:21:23 -08002809 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002810
Romain Guy211370f2012-02-01 16:10:55 -08002811 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002812 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2813 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2814
Romain Guyd21b6e12011-11-30 20:21:23 -08002815 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002816 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2817 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2818 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2819 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002820 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002821 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2822 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2823 GL_TRIANGLE_STRIP, gMeshCount);
2824 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002825}
2826
Romain Guybd6b79b2010-06-26 00:13:53 -07002827void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002828 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2829 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002830 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002831}
2832
2833void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002834 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002835 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002836 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002837
Romain Guy746b7402010-10-26 16:27:31 -07002838 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002839 setupDrawWithTexture();
2840 setupDrawColor(alpha, alpha, alpha, alpha);
2841 setupDrawColorFilter();
2842 setupDrawBlending(blend, mode, swapSrcDst);
2843 setupDrawProgram();
2844 if (!dirty) {
2845 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002846 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002847 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002848 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002849 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002850 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002851 }
Romain Guy86568192010-12-14 15:55:39 -08002852 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002853 setupDrawColorFilterUniforms();
2854 setupDrawTexture(texture);
2855 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002856
Romain Guy6820ac82010-09-15 18:11:50 -07002857 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002858
2859 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002860}
2861
Romain Guya5aed0d2010-09-09 14:42:43 -07002862void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002863 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002864 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002865
Romain Guy82ba8142010-07-09 13:25:56 -07002866 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002867 // These blend modes are not supported by OpenGL directly and have
2868 // to be implemented using shaders. Since the shader will perform
2869 // the blending, turn blending off here
2870 // If the blend mode cannot be implemented using shaders, fall
2871 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002872 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2873 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002874 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002875 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002876
Romain Guy82bc7a72012-01-03 14:13:39 -08002877 if (mCaches.blend) {
2878 glDisable(GL_BLEND);
2879 mCaches.blend = false;
2880 }
2881
2882 return;
2883 } else {
2884 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002885 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002886 }
2887
2888 if (!mCaches.blend) {
2889 glEnable(GL_BLEND);
2890 }
2891
2892 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2893 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2894
2895 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2896 glBlendFunc(sourceMode, destMode);
2897 mCaches.lastSrcMode = sourceMode;
2898 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002899 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002900 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002901 glDisable(GL_BLEND);
2902 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002903 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002904}
2905
Romain Guy889f8d12010-07-29 14:37:42 -07002906bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002907 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002908 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002909 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002910 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002911 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002912 }
Romain Guy6926c722010-07-12 20:20:03 -07002913 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002914}
2915
Romain Guy026c5e162010-06-28 17:12:22 -07002916void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002917 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002918 TextureVertex::setUV(v++, u1, v1);
2919 TextureVertex::setUV(v++, u2, v1);
2920 TextureVertex::setUV(v++, u1, v2);
2921 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002922}
2923
Chet Haase5c13d892010-10-08 08:37:55 -07002924void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002925 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002926 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002927
2928 // Skia draws using the color's alpha channel if < 255
2929 // Otherwise, it uses the paint's alpha
2930 int color = paint->getColor();
2931 *alpha = (color >> 24) & 0xFF;
2932 if (*alpha == 255) {
2933 *alpha = paint->getAlpha();
2934 }
2935 } else {
2936 *mode = SkXfermode::kSrcOver_Mode;
2937 *alpha = 255;
2938 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07002939 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002940}
2941
Romain Guya5aed0d2010-09-09 14:42:43 -07002942SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002943 SkXfermode::Mode resultMode;
2944 if (!SkXfermode::AsMode(mode, &resultMode)) {
2945 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002946 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002947 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002948}
2949
Romain Guy9d5316e2010-06-24 19:30:36 -07002950}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002951}; // namespace android