blob: 9a90bfd1e0ffe8e77e188dc40155c1513c845c44 [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
Chet Haase44b2fe32012-06-06 19:03:58 -0700167int OpenGLRenderer::prepare(bool opaque) {
168 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800169}
170
Chet Haase44b2fe32012-06-06 19:03:58 -0700171int 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);
Chet Haase44b2fe32012-06-06 19:03:58 -0700187 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700188 } else {
189 mCaches.resetScissor();
190 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700191
192 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700193}
194
195void OpenGLRenderer::syncState() {
196 glViewport(0, 0, mWidth, mHeight);
197
198 if (mCaches.blend) {
199 glEnable(GL_BLEND);
200 } else {
201 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700202 }
Romain Guybb9524b2010-06-22 18:56:38 -0700203}
204
Romain Guyb025b9c2010-09-16 14:16:48 -0700205void OpenGLRenderer::finish() {
206#if DEBUG_OPENGL
207 GLenum status = GL_NO_ERROR;
208 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000209 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800210 switch (status) {
Romain Guya44a63a2012-04-26 14:05:02 -0700211 case GL_INVALID_ENUM:
212 ALOGE(" GL_INVALID_ENUM");
213 break;
214 case GL_INVALID_VALUE:
215 ALOGE(" GL_INVALID_VALUE");
216 break;
217 case GL_INVALID_OPERATION:
218 ALOGE(" GL_INVALID_OPERATION");
219 break;
Romain Guya07105b2011-01-10 21:14:18 -0800220 case GL_OUT_OF_MEMORY:
Romain Guya44a63a2012-04-26 14:05:02 -0700221 ALOGE(" Out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800222 break;
223 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700224 }
225#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800226#if DEBUG_MEMORY_USAGE
227 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800228#else
229 if (mCaches.getDebugLevel() & kDebugMemory) {
230 mCaches.dumpMemoryUsage();
231 }
Romain Guyc15008e2010-11-10 11:59:15 -0800232#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700233}
234
Romain Guy6c319ca2011-01-11 14:29:25 -0800235void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700236 if (mCaches.currentProgram) {
237 if (mCaches.currentProgram->isInUse()) {
238 mCaches.currentProgram->remove();
239 mCaches.currentProgram = NULL;
240 }
241 }
Romain Guy50c0f092010-10-19 11:42:22 -0700242 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800243 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800244 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800245 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700246}
247
Romain Guy6c319ca2011-01-11 14:29:25 -0800248void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800249 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
250
251 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800252 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
253
Romain Guyda8532c2010-08-31 11:50:35 -0700254 glEnable(GL_SCISSOR_TEST);
Romain Guy82bc7a72012-01-03 14:13:39 -0800255 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700256 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700257
Romain Guya1d3c912011-12-13 14:55:06 -0800258 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800259 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700260
Romain Guy50c0f092010-10-19 11:42:22 -0700261 mCaches.blend = true;
262 glEnable(GL_BLEND);
263 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
264 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700265}
266
Romain Guyba6be8a2012-04-23 18:22:09 -0700267void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700268 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700269}
270
271void OpenGLRenderer::attachFunctor(Functor* functor) {
272 mFunctors.add(functor);
273}
274
Romain Guy8f3b8e32012-03-27 16:33:45 -0700275status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
276 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700277 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700278
Romain Guyba6be8a2012-04-23 18:22:09 -0700279 if (count > 0) {
280 SortedVector<Functor*> functors(mFunctors);
281 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700282
Romain Guyba6be8a2012-04-23 18:22:09 -0700283 DrawGlInfo info;
284 info.clipLeft = 0;
285 info.clipTop = 0;
286 info.clipRight = 0;
287 info.clipBottom = 0;
288 info.isLayer = false;
289 info.width = 0;
290 info.height = 0;
291 memset(info.transform, 0, sizeof(float) * 16);
292
293 for (size_t i = 0; i < count; i++) {
294 Functor* f = functors.itemAt(i);
295 result |= (*f)(DrawGlInfo::kModeProcess, &info);
296
Chris Craikc2c95432012-04-25 15:13:52 -0700297 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700298 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
299 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700300 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700301
Chris Craikc2c95432012-04-25 15:13:52 -0700302 if (result & DrawGlInfo::kStatusInvoke) {
303 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700304 }
305 }
306 }
307
Romain Guyc189ef52012-04-25 20:02:53 -0700308 mCaches.activeTexture(0);
309
Romain Guy8f3b8e32012-03-27 16:33:45 -0700310 return result;
311}
312
313status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800314 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700315 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700316
Romain Guyf90f8172011-01-25 22:53:24 -0800317 if (mDirtyClip) {
318 setScissorFromClip();
319 }
Romain Guyd643bb52011-03-01 14:55:21 -0800320
Romain Guy80911b82011-03-16 15:30:12 -0700321 Rect clip(*mSnapshot->clipRect);
322 clip.snapToPixelBoundaries();
323
Romain Guyd643bb52011-03-01 14:55:21 -0800324#if RENDER_LAYERS_AS_REGIONS
325 // Since we don't know what the functor will draw, let's dirty
326 // tne entire clip region
327 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800328 dirtyLayerUnchecked(clip, getRegion());
329 }
330#endif
331
Romain Guy08aa2cb2011-03-17 11:06:57 -0700332 DrawGlInfo info;
333 info.clipLeft = clip.left;
334 info.clipTop = clip.top;
335 info.clipRight = clip.right;
336 info.clipBottom = clip.bottom;
337 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700338 info.width = getSnapshot()->viewport.getWidth();
339 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700340 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700341
Chet Haase48659092012-05-31 15:21:51 -0700342 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800343
Romain Guy8f3b8e32012-03-27 16:33:45 -0700344 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700345 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800346 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700347
Chris Craik65924a32012-04-05 17:52:11 -0700348 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700349 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700350 }
Romain Guycabfcc12011-03-07 18:06:46 -0800351 }
352
Chet Haasedaf98e92011-01-10 14:10:36 -0800353 resume();
Romain Guy65549432012-03-26 16:45:05 -0700354 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800355}
356
Romain Guyf6a11b82010-06-23 17:47:49 -0700357///////////////////////////////////////////////////////////////////////////////
358// State management
359///////////////////////////////////////////////////////////////////////////////
360
Romain Guybb9524b2010-06-22 18:56:38 -0700361int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700362 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700363}
364
365int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700366 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700367}
368
369void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700370 if (mSaveCount > 1) {
371 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700372 }
Romain Guybb9524b2010-06-22 18:56:38 -0700373}
374
375void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700376 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700377
Romain Guy8fb95422010-08-17 18:38:51 -0700378 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700379 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700380 }
Romain Guybb9524b2010-06-22 18:56:38 -0700381}
382
Romain Guy8aef54f2010-09-01 15:13:49 -0700383int OpenGLRenderer::saveSnapshot(int flags) {
384 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700385 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700386}
387
388bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700389 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700390 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700391 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700392
Romain Guybd6b79b2010-06-26 00:13:53 -0700393 sp<Snapshot> current = mSnapshot;
394 sp<Snapshot> previous = mSnapshot->previous;
395
Romain Guyeb993562010-10-05 18:14:38 -0700396 if (restoreOrtho) {
397 Rect& r = previous->viewport;
398 glViewport(r.left, r.top, r.right, r.bottom);
399 mOrthoMatrix.load(current->orthoMatrix);
400 }
401
Romain Guy8b55f372010-08-18 17:10:07 -0700402 mSaveCount--;
403 mSnapshot = previous;
404
Romain Guy2542d192010-08-18 11:47:12 -0700405 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700406 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700407 }
Romain Guy2542d192010-08-18 11:47:12 -0700408
Romain Guy5ec99242010-11-03 16:19:08 -0700409 if (restoreLayer) {
410 composeLayer(current, previous);
411 }
412
Romain Guy2542d192010-08-18 11:47:12 -0700413 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700414}
415
Romain Guyf6a11b82010-06-23 17:47:49 -0700416///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700417// Layers
418///////////////////////////////////////////////////////////////////////////////
419
420int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700421 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700422 const GLuint previousFbo = mSnapshot->fbo;
423 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700424
Romain Guyaf636eb2010-12-09 17:47:21 -0800425 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700426 int alpha = 255;
427 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700428
Romain Guye45362c2010-11-03 19:58:32 -0700429 if (p) {
430 alpha = p->getAlpha();
431 if (!mCaches.extensions.hasFramebufferFetch()) {
432 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
433 if (!isMode) {
434 // Assume SRC_OVER
435 mode = SkXfermode::kSrcOver_Mode;
436 }
437 } else {
438 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700439 }
440 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700441 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700442 }
Romain Guyd55a8612010-06-28 17:42:46 -0700443
Romain Guydbc26d22010-10-11 17:58:29 -0700444 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
445 }
Romain Guyd55a8612010-06-28 17:42:46 -0700446
447 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700448}
449
450int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
451 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700452 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700453 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700454 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700455 SkPaint paint;
456 paint.setAlpha(alpha);
457 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700458 }
Romain Guyd55a8612010-06-28 17:42:46 -0700459}
Romain Guybd6b79b2010-06-26 00:13:53 -0700460
Romain Guy1c740bc2010-09-13 18:00:09 -0700461/**
462 * Layers are viewed by Skia are slightly different than layers in image editing
463 * programs (for instance.) When a layer is created, previously created layers
464 * and the frame buffer still receive every drawing command. For instance, if a
465 * layer is created and a shape intersecting the bounds of the layers and the
466 * framebuffer is draw, the shape will be drawn on both (unless the layer was
467 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
468 *
469 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
470 * texture. Unfortunately, this is inefficient as it requires every primitive to
471 * be drawn n + 1 times, where n is the number of active layers. In practice this
472 * means, for every primitive:
473 * - Switch active frame buffer
474 * - Change viewport, clip and projection matrix
475 * - Issue the drawing
476 *
477 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700478 * To avoid this, layers are implemented in a different way here, at least in the
479 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
480 * is set. When this flag is set we can redirect all drawing operations into a
481 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700482 *
483 * This implementation relies on the frame buffer being at least RGBA 8888. When
484 * a layer is created, only a texture is created, not an FBO. The content of the
485 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700486 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700487 * buffer and drawing continues as normal. This technique therefore treats the
488 * frame buffer as a scratch buffer for the layers.
489 *
490 * To compose the layers back onto the frame buffer, each layer texture
491 * (containing the original frame buffer data) is drawn as a simple quad over
492 * the frame buffer. The trick is that the quad is set as the composition
493 * destination in the blending equation, and the frame buffer becomes the source
494 * of the composition.
495 *
496 * Drawing layers with an alpha value requires an extra step before composition.
497 * An empty quad is drawn over the layer's region in the frame buffer. This quad
498 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
499 * quad is used to multiply the colors in the frame buffer. This is achieved by
500 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
501 * GL_ZERO, GL_SRC_ALPHA.
502 *
503 * Because glCopyTexImage2D() can be slow, an alternative implementation might
504 * be use to draw a single clipped layer. The implementation described above
505 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700506 *
507 * (1) The frame buffer is actually not cleared right away. To allow the GPU
508 * to potentially optimize series of calls to glCopyTexImage2D, the frame
509 * buffer is left untouched until the first drawing operation. Only when
510 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700511 */
Romain Guyd55a8612010-06-28 17:42:46 -0700512bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700513 float right, float bottom, int alpha, SkXfermode::Mode mode,
514 int flags, GLuint previousFbo) {
515 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700516 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700517
Romain Guyeb993562010-10-05 18:14:38 -0700518 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
519
Romain Guyf607bdc2010-09-10 19:20:06 -0700520 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700521 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700522 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700523 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700524
Romain Guyeb993562010-10-05 18:14:38 -0700525 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700526 if (bounds.intersect(*snapshot->clipRect)) {
527 // We cannot work with sub-pixels in this case
528 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700529
Romain Guyad37cd32011-03-15 11:12:25 -0700530 // When the layer is not an FBO, we may use glCopyTexImage so we
531 // need to make sure the layer does not extend outside the bounds
532 // of the framebuffer
533 if (!bounds.intersect(snapshot->previous->viewport)) {
534 bounds.setEmpty();
535 }
536 } else {
537 bounds.setEmpty();
538 }
Romain Guyeb993562010-10-05 18:14:38 -0700539 }
Romain Guybf434112010-09-16 14:40:17 -0700540
Romain Guy746b7402010-10-26 16:27:31 -0700541 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
542 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800543 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700544 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700545 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700546 }
547
548 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800549 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700550 return false;
551 }
Romain Guyf18fd992010-07-08 11:45:51 -0700552
Romain Guya1d3c912011-12-13 14:55:06 -0800553 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700554 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700555 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700556 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700557 }
558
Romain Guy9ace8f52011-07-07 20:50:11 -0700559 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700560 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700561 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
562 bounds.getWidth() / float(layer->getWidth()), 0.0f);
563 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700564 layer->setBlend(true);
Romain Guydda570202010-07-06 11:39:32 -0700565
Romain Guy8fb95422010-08-17 18:38:51 -0700566 // Save the layer in the snapshot
567 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700568 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700569
Romain Guyeb993562010-10-05 18:14:38 -0700570 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700571 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700572 } else {
573 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700574 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800575 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700576 if (layer->isEmpty()) {
577 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
578 bounds.left, snapshot->height - bounds.bottom,
579 layer->getWidth(), layer->getHeight(), 0);
580 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800581 } else {
582 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
583 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
584 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700585
Romain Guy54be1cd2011-06-13 19:04:27 -0700586 // Enqueue the buffer coordinates to clear the corresponding region later
587 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700588 }
Romain Guyeb993562010-10-05 18:14:38 -0700589 }
Romain Guyf86ef572010-07-01 11:05:42 -0700590
Romain Guyd55a8612010-06-28 17:42:46 -0700591 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700592}
593
Romain Guy5b3b3522010-10-27 18:57:51 -0700594bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
595 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700596 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700597
598#if RENDER_LAYERS_AS_REGIONS
599 snapshot->region = &snapshot->layer->region;
600 snapshot->flags |= Snapshot::kFlagFboTarget;
601#endif
602
603 Rect clip(bounds);
604 snapshot->transform->mapRect(clip);
605 clip.intersect(*snapshot->clipRect);
606 clip.snapToPixelBoundaries();
607 clip.intersect(snapshot->previous->viewport);
608
609 mat4 inverse;
610 inverse.loadInverse(*mSnapshot->transform);
611
612 inverse.mapRect(clip);
613 clip.snapToPixelBoundaries();
614 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700615 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700616
617 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700618 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700619 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700620 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
621 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
622 snapshot->height = bounds.getHeight();
623 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
624 snapshot->orthoMatrix.load(mOrthoMatrix);
625
626 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700627 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
628 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700629
630 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700631 if (layer->isEmpty()) {
632 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
633 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700634 }
635
636 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700637 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700638
639#if DEBUG_LAYERS_AS_REGIONS
640 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
641 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000642 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700643
644 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700645 layer->deleteTexture();
646 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700647
648 delete layer;
649
650 return false;
651 }
652#endif
653
654 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800655 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700656 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700657 glClear(GL_COLOR_BUFFER_BIT);
658
659 dirtyClip();
660
661 // Change the ortho projection
662 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
663 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
664
665 return true;
666}
667
Romain Guy1c740bc2010-09-13 18:00:09 -0700668/**
669 * Read the documentation of createLayer() before doing anything in this method.
670 */
Romain Guy1d83e192010-08-17 11:37:00 -0700671void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
672 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000673 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700674 return;
675 }
676
Romain Guy5b3b3522010-10-27 18:57:51 -0700677 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700678
679 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700680 // Detach the texture from the FBO
681 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
682
Romain Guyeb993562010-10-05 18:14:38 -0700683 // Unbind current FBO and restore previous one
684 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
685 }
686
Romain Guy1d83e192010-08-17 11:37:00 -0700687 Layer* layer = current->layer;
688 const Rect& rect = layer->layer;
689
Romain Guy9ace8f52011-07-07 20:50:11 -0700690 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700691 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700692 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700693 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700694 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700695 }
696
Romain Guy03750a02010-10-18 14:06:08 -0700697 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700698
Romain Guya1d3c912011-12-13 14:55:06 -0800699 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700700
Romain Guy5b3b3522010-10-27 18:57:51 -0700701 // When the layer is stored in an FBO, we can save a bit of fillrate by
702 // drawing only the dirty region
703 if (fboLayer) {
704 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700705 if (layer->getColorFilter()) {
706 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800707 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700708 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700709 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800710 resetColorFilter();
711 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700712 } else if (!rect.isEmpty()) {
713 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
714 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700715 }
Romain Guy8b55f372010-08-18 17:10:07 -0700716
Romain Guyeb993562010-10-05 18:14:38 -0700717 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800718 // Note: No need to use glDiscardFramebufferEXT() since we never
719 // create/compose layers that are not on screen with this
720 // code path
721 // See LayerRenderer::destroyLayer(Layer*)
722
Romain Guyeb993562010-10-05 18:14:38 -0700723 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
724 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700725 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700726 }
727
Romain Guy746b7402010-10-26 16:27:31 -0700728 dirtyClip();
729
Romain Guyeb993562010-10-05 18:14:38 -0700730 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700731 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700732 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700733 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700734 delete layer;
735 }
736}
737
Romain Guyaa6c24c2011-04-28 18:40:04 -0700738void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700739 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700740
Romain Guy302a9df2011-08-16 13:55:02 -0700741 mat4& transform = layer->getTransform();
742 if (!transform.isIdentity()) {
743 save(0);
744 mSnapshot->transform->multiply(transform);
745 }
746
Romain Guyaa6c24c2011-04-28 18:40:04 -0700747 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700748 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700749 setupDrawWithTexture();
750 } else {
751 setupDrawWithExternalTexture();
752 }
753 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700754 setupDrawColor(alpha, alpha, alpha, alpha);
755 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700756 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700757 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700758 setupDrawPureColorUniforms();
759 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700760 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
761 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700762 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700763 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700764 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700765 if (mSnapshot->transform->isPureTranslate() &&
766 layer->getWidth() == (uint32_t) rect.getWidth() &&
767 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700768 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
769 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
770
Romain Guyd21b6e12011-11-30 20:21:23 -0800771 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700772 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
773 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800774 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700775 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
776 }
777 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700778 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
779
780 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
781
782 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700783
784 if (!transform.isIdentity()) {
785 restore();
786 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700787}
788
Romain Guy5b3b3522010-10-27 18:57:51 -0700789void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700790 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700791 const Rect& texCoords = layer->texCoords;
792 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
793 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700794
Romain Guy9ace8f52011-07-07 20:50:11 -0700795 float x = rect.left;
796 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700797 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700798 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700799 layer->getHeight() == (uint32_t) rect.getHeight();
800
801 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700802 // When we're swapping, the layer is already in screen coordinates
803 if (!swap) {
804 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
805 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
806 }
807
Romain Guyd21b6e12011-11-30 20:21:23 -0800808 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700809 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800810 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700811 }
812
813 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
814 layer->getTexture(), layer->getAlpha() / 255.0f,
815 layer->getMode(), layer->isBlend(),
816 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
817 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700818
Romain Guyaa6c24c2011-04-28 18:40:04 -0700819 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
820 } else {
821 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
822 drawTextureLayer(layer, rect);
823 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
824 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700825}
826
827void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
828#if RENDER_LAYERS_AS_REGIONS
829 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700830 layer->setRegionAsRect();
831
Romain Guy40667672011-03-18 14:34:03 -0700832 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700833
Romain Guy5b3b3522010-10-27 18:57:51 -0700834 layer->region.clear();
835 return;
836 }
837
Romain Guy8a3957d2011-09-07 17:55:15 -0700838 // TODO: See LayerRenderer.cpp::generateMesh() for important
839 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800840 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700841 size_t count;
842 const android::Rect* rects = layer->region.getArray(&count);
843
Romain Guy9ace8f52011-07-07 20:50:11 -0700844 const float alpha = layer->getAlpha() / 255.0f;
845 const float texX = 1.0f / float(layer->getWidth());
846 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800847 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700848
849 TextureVertex* mesh = mCaches.getRegionMesh();
850 GLsizei numQuads = 0;
851
Romain Guy7230a742011-01-10 22:26:16 -0800852 setupDraw();
853 setupDrawWithTexture();
854 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800855 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700856 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800857 setupDrawProgram();
858 setupDrawDirtyRegionsDisabled();
859 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800860 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700861 setupDrawTexture(layer->getTexture());
862 if (mSnapshot->transform->isPureTranslate()) {
863 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
864 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
865
Romain Guyd21b6e12011-11-30 20:21:23 -0800866 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700867 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
868 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800869 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700870 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
871 }
Romain Guy15bc6432011-12-13 13:11:32 -0800872 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700873
874 for (size_t i = 0; i < count; i++) {
875 const android::Rect* r = &rects[i];
876
877 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800878 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700879 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800880 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700881
882 // TODO: Reject quads outside of the clip
883 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
884 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
885 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
886 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
887
888 numQuads++;
889
890 if (numQuads >= REGION_MESH_QUAD_COUNT) {
891 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
892 numQuads = 0;
893 mesh = mCaches.getRegionMesh();
894 }
895 }
896
897 if (numQuads > 0) {
898 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
899 }
900
Romain Guy7230a742011-01-10 22:26:16 -0800901 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700902
903#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800904 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700905#endif
906
907 layer->region.clear();
908 }
909#else
910 composeLayerRect(layer, rect);
911#endif
912}
913
Romain Guy3a3133d2011-02-01 22:59:58 -0800914void OpenGLRenderer::drawRegionRects(const Region& region) {
915#if DEBUG_LAYERS_AS_REGIONS
916 size_t count;
917 const android::Rect* rects = region.getArray(&count);
918
919 uint32_t colors[] = {
920 0x7fff0000, 0x7f00ff00,
921 0x7f0000ff, 0x7fff00ff,
922 };
923
924 int offset = 0;
925 int32_t top = rects[0].top;
926
927 for (size_t i = 0; i < count; i++) {
928 if (top != rects[i].top) {
929 offset ^= 0x2;
930 top = rects[i].top;
931 }
932
933 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
934 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
935 SkXfermode::kSrcOver_Mode);
936 }
937#endif
938}
939
Romain Guy5b3b3522010-10-27 18:57:51 -0700940void OpenGLRenderer::dirtyLayer(const float left, const float top,
941 const float right, const float bottom, const mat4 transform) {
942#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800943 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700944 Rect bounds(left, top, right, bottom);
945 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800946 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700947 }
948#endif
949}
950
951void OpenGLRenderer::dirtyLayer(const float left, const float top,
952 const float right, const float bottom) {
953#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800954 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700955 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800956 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800957 }
958#endif
959}
960
961void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
962#if RENDER_LAYERS_AS_REGIONS
963 if (bounds.intersect(*mSnapshot->clipRect)) {
964 bounds.snapToPixelBoundaries();
965 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
966 if (!dirty.isEmpty()) {
967 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700968 }
969 }
970#endif
971}
972
Romain Guy54be1cd2011-06-13 19:04:27 -0700973void OpenGLRenderer::clearLayerRegions() {
974 const size_t count = mLayers.size();
975 if (count == 0) return;
976
977 if (!mSnapshot->isIgnored()) {
978 // Doing several glScissor/glClear here can negatively impact
979 // GPUs with a tiler architecture, instead we draw quads with
980 // the Clear blending mode
981
982 // The list contains bounds that have already been clipped
983 // against their initial clip rect, and the current clip
984 // is likely different so we need to disable clipping here
985 glDisable(GL_SCISSOR_TEST);
986
987 Vertex mesh[count * 6];
988 Vertex* vertex = mesh;
989
990 for (uint32_t i = 0; i < count; i++) {
991 Rect* bounds = mLayers.itemAt(i);
992
993 Vertex::set(vertex++, bounds->left, bounds->bottom);
994 Vertex::set(vertex++, bounds->left, bounds->top);
995 Vertex::set(vertex++, bounds->right, bounds->top);
996 Vertex::set(vertex++, bounds->left, bounds->bottom);
997 Vertex::set(vertex++, bounds->right, bounds->top);
998 Vertex::set(vertex++, bounds->right, bounds->bottom);
999
1000 delete bounds;
1001 }
1002
1003 setupDraw(false);
1004 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1005 setupDrawBlending(true, SkXfermode::kClear_Mode);
1006 setupDrawProgram();
1007 setupDrawPureColorUniforms();
1008 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001009 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001010
Romain Guy54be1cd2011-06-13 19:04:27 -07001011 glDrawArrays(GL_TRIANGLES, 0, count * 6);
1012
1013 glEnable(GL_SCISSOR_TEST);
1014 } else {
1015 for (uint32_t i = 0; i < count; i++) {
1016 delete mLayers.itemAt(i);
1017 }
1018 }
1019
1020 mLayers.clear();
1021}
1022
Romain Guybd6b79b2010-06-26 00:13:53 -07001023///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001024// Transforms
1025///////////////////////////////////////////////////////////////////////////////
1026
1027void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001028 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001029}
1030
1031void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001032 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001033}
1034
1035void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001036 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001037}
1038
Romain Guy807daf72011-01-18 11:19:19 -08001039void OpenGLRenderer::skew(float sx, float sy) {
1040 mSnapshot->transform->skew(sx, sy);
1041}
1042
Romain Guyf6a11b82010-06-23 17:47:49 -07001043void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001044 if (matrix) {
1045 mSnapshot->transform->load(*matrix);
1046 } else {
1047 mSnapshot->transform->loadIdentity();
1048 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001049}
1050
1051void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001052 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001053}
1054
1055void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001056 SkMatrix transform;
1057 mSnapshot->transform->copyTo(transform);
1058 transform.preConcat(*matrix);
1059 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001060}
1061
1062///////////////////////////////////////////////////////////////////////////////
1063// Clipping
1064///////////////////////////////////////////////////////////////////////////////
1065
Romain Guybb9524b2010-06-22 18:56:38 -07001066void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001067 Rect clip(*mSnapshot->clipRect);
1068 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001069
1070 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1071 clip.getWidth(), clip.getHeight());
1072
Romain Guy746b7402010-10-26 16:27:31 -07001073 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -07001074}
1075
1076const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001077 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001078}
1079
Romain Guyc7d53492010-06-25 13:41:57 -07001080bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001081 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001082 return true;
1083 }
1084
Romain Guy1d83e192010-08-17 11:37:00 -07001085 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001086 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001087 r.snapToPixelBoundaries();
1088
1089 Rect clipRect(*mSnapshot->clipRect);
1090 clipRect.snapToPixelBoundaries();
1091
1092 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001093}
1094
Romain Guy079ba2c2010-07-16 14:12:24 -07001095bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1096 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001097 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001098 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001099 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001100 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001101}
1102
Chet Haasea23eed82012-04-12 15:19:04 -07001103Rect* OpenGLRenderer::getClipRect() {
1104 return mSnapshot->clipRect;
1105}
1106
Romain Guyf6a11b82010-06-23 17:47:49 -07001107///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001108// Drawing commands
1109///////////////////////////////////////////////////////////////////////////////
1110
Romain Guy54be1cd2011-06-13 19:04:27 -07001111void OpenGLRenderer::setupDraw(bool clear) {
1112 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001113 if (mDirtyClip) {
1114 setScissorFromClip();
1115 }
1116 mDescription.reset();
1117 mSetShaderColor = false;
1118 mColorSet = false;
1119 mColorA = mColorR = mColorG = mColorB = 0.0f;
1120 mTextureUnit = 0;
1121 mTrackDirtyRegions = true;
1122}
1123
1124void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1125 mDescription.hasTexture = true;
1126 mDescription.hasAlpha8Texture = isAlpha8;
1127}
1128
Romain Guyaa6c24c2011-04-28 18:40:04 -07001129void OpenGLRenderer::setupDrawWithExternalTexture() {
1130 mDescription.hasExternalTexture = true;
1131}
1132
Romain Guy15bc6432011-12-13 13:11:32 -08001133void OpenGLRenderer::setupDrawNoTexture() {
1134 mCaches.disbaleTexCoordsVertexArray();
1135}
1136
Chet Haase5b0200b2011-04-13 17:58:08 -07001137void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001138 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001139}
1140
Romain Guyed6fcb02011-03-21 13:11:28 -07001141void OpenGLRenderer::setupDrawPoint(float pointSize) {
1142 mDescription.isPoint = true;
1143 mDescription.pointSize = pointSize;
1144}
1145
Romain Guy70ca14e2010-12-13 18:24:33 -08001146void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001147 setupDrawColor(color, (color >> 24) & 0xFF);
1148}
1149
1150void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1151 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001152 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1153 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001154 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001155 mColorR = a * ((color >> 16) & 0xFF);
1156 mColorG = a * ((color >> 8) & 0xFF);
1157 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001158 mColorSet = true;
1159 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1160}
1161
Romain Guy86568192010-12-14 15:55:39 -08001162void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1163 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001164 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1165 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001166 const float a = mColorA / 255.0f;
1167 mColorR = a * ((color >> 16) & 0xFF);
1168 mColorG = a * ((color >> 8) & 0xFF);
1169 mColorB = a * ((color ) & 0xFF);
1170 mColorSet = true;
1171 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1172}
1173
Romain Guy70ca14e2010-12-13 18:24:33 -08001174void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1175 mColorA = a;
1176 mColorR = r;
1177 mColorG = g;
1178 mColorB = b;
1179 mColorSet = true;
1180 mSetShaderColor = mDescription.setColor(r, g, b, a);
1181}
1182
1183void OpenGLRenderer::setupDrawShader() {
1184 if (mShader) {
1185 mShader->describe(mDescription, mCaches.extensions);
1186 }
1187}
1188
1189void OpenGLRenderer::setupDrawColorFilter() {
1190 if (mColorFilter) {
1191 mColorFilter->describe(mDescription, mCaches.extensions);
1192 }
1193}
1194
Romain Guyf09ef512011-05-27 11:43:46 -07001195void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1196 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1197 mColorA = 1.0f;
1198 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001199 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001200 }
1201}
1202
Romain Guy70ca14e2010-12-13 18:24:33 -08001203void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001204 // When the blending mode is kClear_Mode, we need to use a modulate color
1205 // argb=1,0,0,0
1206 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001207 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1208 mDescription, swapSrcDst);
1209}
1210
1211void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001212 // When the blending mode is kClear_Mode, we need to use a modulate color
1213 // argb=1,0,0,0
1214 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001215 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1216 mDescription, swapSrcDst);
1217}
1218
1219void OpenGLRenderer::setupDrawProgram() {
1220 useProgram(mCaches.programCache.get(mDescription));
1221}
1222
1223void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1224 mTrackDirtyRegions = false;
1225}
1226
1227void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1228 bool ignoreTransform) {
1229 mModelView.loadTranslate(left, top, 0.0f);
1230 if (!ignoreTransform) {
1231 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1232 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1233 } else {
1234 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1235 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1236 }
1237}
1238
Chet Haase8a5cc922011-04-26 07:28:09 -07001239void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1240 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001241}
1242
Romain Guy70ca14e2010-12-13 18:24:33 -08001243void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1244 bool ignoreTransform, bool ignoreModelView) {
1245 if (!ignoreModelView) {
1246 mModelView.loadTranslate(left, top, 0.0f);
1247 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001248 } else {
1249 mModelView.loadIdentity();
1250 }
Romain Guy86568192010-12-14 15:55:39 -08001251 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1252 if (!ignoreTransform) {
1253 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1254 if (mTrackDirtyRegions && dirty) {
1255 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1256 }
1257 } else {
1258 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1259 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1260 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001261}
1262
Romain Guyed6fcb02011-03-21 13:11:28 -07001263void OpenGLRenderer::setupDrawPointUniforms() {
1264 int slot = mCaches.currentProgram->getUniform("pointSize");
1265 glUniform1f(slot, mDescription.pointSize);
1266}
1267
Romain Guy70ca14e2010-12-13 18:24:33 -08001268void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001269 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001270 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1271 }
1272}
1273
Romain Guy86568192010-12-14 15:55:39 -08001274void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001275 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001276 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001277 }
1278}
1279
Romain Guy70ca14e2010-12-13 18:24:33 -08001280void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1281 if (mShader) {
1282 if (ignoreTransform) {
1283 mModelView.loadInverse(*mSnapshot->transform);
1284 }
1285 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1286 }
1287}
1288
Romain Guy8d0d4782010-12-14 20:13:35 -08001289void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1290 if (mShader) {
1291 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1292 }
1293}
1294
Romain Guy70ca14e2010-12-13 18:24:33 -08001295void OpenGLRenderer::setupDrawColorFilterUniforms() {
1296 if (mColorFilter) {
1297 mColorFilter->setupProgram(mCaches.currentProgram);
1298 }
1299}
1300
1301void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001302 bool force = mCaches.bindMeshBuffer();
1303 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001304 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001305}
1306
1307void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1308 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001309 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001310 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001311}
1312
Romain Guyaa6c24c2011-04-28 18:40:04 -07001313void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1314 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001315 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001316 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001317}
1318
Romain Guy8f0095c2011-05-02 17:24:22 -07001319void OpenGLRenderer::setupDrawTextureTransform() {
1320 mDescription.hasTextureTransform = true;
1321}
1322
1323void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001324 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1325 GL_FALSE, &transform.data[0]);
1326}
1327
Romain Guy70ca14e2010-12-13 18:24:33 -08001328void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001329 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001330 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001331 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001332 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001333 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001334 }
Romain Guyd71dd362011-12-12 19:03:35 -08001335
Romain Guyf3a910b42011-12-12 20:35:21 -08001336 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001337 if (mCaches.currentProgram->texCoords >= 0) {
1338 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1339 }
1340
1341 mCaches.unbindIndicesBuffer();
1342}
1343
1344void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1345 bool force = mCaches.unbindMeshBuffer();
1346 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1347 if (mCaches.currentProgram->texCoords >= 0) {
1348 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001349 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001350}
1351
Chet Haase5b0200b2011-04-13 17:58:08 -07001352void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001353 bool force = mCaches.unbindMeshBuffer();
1354 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1355 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001356 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001357}
1358
1359/**
1360 * 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 -07001361 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1362 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1363 * attributes (one per vertex) are values from zero to one that tells the fragment
1364 * shader where the fragment is in relation to the line width/length overall; these values are
1365 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1366 * region of the line.
1367 * Note that we only pass down the width values in this setup function. The length coordinates
1368 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001369 */
Chet Haase99585ad2011-05-02 15:00:16 -07001370void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001371 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001372 bool force = mCaches.unbindMeshBuffer();
1373 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1374 vertices, gAAVertexStride);
1375 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001376 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001377
Romain Guy7b631422012-04-04 11:38:54 -07001378 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001379 glEnableVertexAttribArray(widthSlot);
1380 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001381
Romain Guy7b631422012-04-04 11:38:54 -07001382 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001383 glEnableVertexAttribArray(lengthSlot);
1384 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001385
Chet Haase5b0200b2011-04-13 17:58:08 -07001386 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001387 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001388
Chet Haase99585ad2011-05-02 15:00:16 -07001389 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001390 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Romain Guy7b631422012-04-04 11:38:54 -07001391 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion);
1392}
1393
1394void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1395 glDisableVertexAttribArray(widthSlot);
1396 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001397}
1398
Romain Guy70ca14e2010-12-13 18:24:33 -08001399void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001400}
1401
1402///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001403// Drawing
1404///////////////////////////////////////////////////////////////////////////////
1405
Chet Haase1271e2c2012-04-20 09:54:27 -07001406status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001407 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001408
Romain Guy0fe478e2010-11-08 12:08:41 -08001409 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1410 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001411 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001412 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001413 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001414
Romain Guy65549432012-03-26 16:45:05 -07001415 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001416}
1417
Chet Haaseed30fd82011-04-22 16:18:45 -07001418void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1419 if (displayList) {
1420 displayList->output(*this, level);
1421 }
1422}
1423
Romain Guya168d732011-03-18 16:50:13 -07001424void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1425 int alpha;
1426 SkXfermode::Mode mode;
1427 getAlphaAndMode(paint, &alpha, &mode);
1428
Romain Guya168d732011-03-18 16:50:13 -07001429 float x = left;
1430 float y = top;
1431
Romain Guye3c26852011-07-25 16:36:01 -07001432 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001433 bool ignoreTransform = false;
1434 if (mSnapshot->transform->isPureTranslate()) {
1435 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1436 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1437 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001438 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001439 } else {
1440 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001441 }
1442
1443 setupDraw();
1444 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001445 if (paint) {
1446 setupDrawAlpha8Color(paint->getColor(), alpha);
1447 }
Romain Guya168d732011-03-18 16:50:13 -07001448 setupDrawColorFilter();
1449 setupDrawShader();
1450 setupDrawBlending(true, mode);
1451 setupDrawProgram();
1452 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001453
Romain Guya168d732011-03-18 16:50:13 -07001454 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001455 texture->setWrap(GL_CLAMP_TO_EDGE);
1456 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001457
Romain Guya168d732011-03-18 16:50:13 -07001458 setupDrawPureColorUniforms();
1459 setupDrawColorFilterUniforms();
1460 setupDrawShaderUniforms();
1461 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1462
1463 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1464
1465 finishDrawTexture();
1466}
1467
Chet Haase48659092012-05-31 15:21:51 -07001468status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001469 const float right = left + bitmap->width();
1470 const float bottom = top + bitmap->height();
1471
1472 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001473 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001474 }
1475
Romain Guya1d3c912011-12-13 14:55:06 -08001476 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001477 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001478 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001479 const AutoTexture autoCleanup(texture);
1480
Romain Guy211370f2012-02-01 16:10:55 -08001481 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001482 drawAlphaBitmap(texture, left, top, paint);
1483 } else {
1484 drawTextureRect(left, top, right, bottom, texture, paint);
1485 }
Chet Haase48659092012-05-31 15:21:51 -07001486
1487 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001488}
1489
Chet Haase48659092012-05-31 15:21:51 -07001490status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001491 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1492 const mat4 transform(*matrix);
1493 transform.mapRect(r);
1494
Romain Guy6926c722010-07-12 20:20:03 -07001495 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001496 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001497 }
1498
Romain Guya1d3c912011-12-13 14:55:06 -08001499 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001500 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001501 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001502 const AutoTexture autoCleanup(texture);
1503
Romain Guy5b3b3522010-10-27 18:57:51 -07001504 // This could be done in a cheaper way, all we need is pass the matrix
1505 // to the vertex shader. The save/restore is a bit overkill.
1506 save(SkCanvas::kMatrix_SaveFlag);
1507 concatMatrix(matrix);
1508 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1509 restore();
Chet Haase48659092012-05-31 15:21:51 -07001510
1511 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001512}
1513
Chet Haase48659092012-05-31 15:21:51 -07001514status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001515 const float right = left + bitmap->width();
1516 const float bottom = top + bitmap->height();
1517
1518 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001519 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001520 }
1521
1522 mCaches.activeTexture(0);
1523 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1524 const AutoTexture autoCleanup(texture);
1525
1526 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001527
1528 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001529}
1530
Chet Haase48659092012-05-31 15:21:51 -07001531status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001532 float* vertices, int* colors, SkPaint* paint) {
1533 // TODO: Do a quickReject
1534 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001535 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001536 }
1537
Romain Guya1d3c912011-12-13 14:55:06 -08001538 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001539 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001540 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001541 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001542
Romain Guyd21b6e12011-11-30 20:21:23 -08001543 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1544 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001545
1546 int alpha;
1547 SkXfermode::Mode mode;
1548 getAlphaAndMode(paint, &alpha, &mode);
1549
Romain Guy5a7b4662011-01-20 19:09:30 -08001550 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001551
Romain Guyb18d2d02011-02-10 15:52:54 -08001552 float left = FLT_MAX;
1553 float top = FLT_MAX;
1554 float right = FLT_MIN;
1555 float bottom = FLT_MIN;
1556
1557#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001558 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001559#else
Romain Guy211370f2012-02-01 16:10:55 -08001560 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001561#endif
1562
Romain Guya566b7c2011-01-23 16:36:11 -08001563 // TODO: Support the colors array
1564 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001565 TextureVertex* vertex = mesh;
1566 for (int32_t y = 0; y < meshHeight; y++) {
1567 for (int32_t x = 0; x < meshWidth; x++) {
1568 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1569
1570 float u1 = float(x) / meshWidth;
1571 float u2 = float(x + 1) / meshWidth;
1572 float v1 = float(y) / meshHeight;
1573 float v2 = float(y + 1) / meshHeight;
1574
1575 int ax = i + (meshWidth + 1) * 2;
1576 int ay = ax + 1;
1577 int bx = i;
1578 int by = bx + 1;
1579 int cx = i + 2;
1580 int cy = cx + 1;
1581 int dx = i + (meshWidth + 1) * 2 + 2;
1582 int dy = dx + 1;
1583
1584 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1585 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1586 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1587
1588 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1589 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1590 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001591
1592#if RENDER_LAYERS_AS_REGIONS
1593 if (hasActiveLayer) {
1594 // TODO: This could be optimized to avoid unnecessary ops
1595 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1596 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1597 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1598 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1599 }
1600#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001601 }
1602 }
1603
Romain Guyb18d2d02011-02-10 15:52:54 -08001604#if RENDER_LAYERS_AS_REGIONS
1605 if (hasActiveLayer) {
1606 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1607 }
1608#endif
1609
Romain Guy5a7b4662011-01-20 19:09:30 -08001610 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1611 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001612 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001613
1614 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001615}
1616
Chet Haase48659092012-05-31 15:21:51 -07001617status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001618 float srcLeft, float srcTop, float srcRight, float srcBottom,
1619 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001620 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001621 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001622 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001623 }
1624
Romain Guya1d3c912011-12-13 14:55:06 -08001625 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001626 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001627 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001628 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001629
Romain Guy8ba548f2010-06-30 19:21:21 -07001630 const float width = texture->width;
1631 const float height = texture->height;
1632
Romain Guy68169722011-08-22 17:33:33 -07001633 const float u1 = fmax(0.0f, srcLeft / width);
1634 const float v1 = fmax(0.0f, srcTop / height);
1635 const float u2 = fmin(1.0f, srcRight / width);
1636 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001637
Romain Guy03750a02010-10-18 14:06:08 -07001638 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001639 resetDrawTextureTexCoords(u1, v1, u2, v2);
1640
Romain Guy03750a02010-10-18 14:06:08 -07001641 int alpha;
1642 SkXfermode::Mode mode;
1643 getAlphaAndMode(paint, &alpha, &mode);
1644
Romain Guyd21b6e12011-11-30 20:21:23 -08001645 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1646
Romain Guy211370f2012-02-01 16:10:55 -08001647 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001648 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1649 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1650
Romain Guyb5014982011-07-28 15:39:12 -07001651 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001652 // Enable linear filtering if the source rectangle is scaled
1653 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001654 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001655 }
Romain Guyb5014982011-07-28 15:39:12 -07001656
Romain Guyd21b6e12011-11-30 20:21:23 -08001657 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001658 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1659 texture->id, alpha / 255.0f, mode, texture->blend,
1660 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1661 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1662 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001663 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001664 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1665 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1666 GL_TRIANGLE_STRIP, gMeshCount);
1667 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001668
1669 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001670
1671 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001672}
1673
Chet Haase48659092012-05-31 15:21:51 -07001674status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001675 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001676 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001677 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001678 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001679 }
1680
Romain Guya1d3c912011-12-13 14:55:06 -08001681 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001682 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001683 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001684 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001685 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1686 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001687
1688 int alpha;
1689 SkXfermode::Mode mode;
1690 getAlphaAndMode(paint, &alpha, &mode);
1691
Romain Guy2728f962010-10-08 18:36:15 -07001692 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001693 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001694
Romain Guy211370f2012-02-01 16:10:55 -08001695 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001696 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001697#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001698 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001699 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001700 const float offsetX = left + mSnapshot->transform->getTranslateX();
1701 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001702 const size_t count = mesh->quads.size();
1703 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001704 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001705 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001706 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1707 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1708 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001709 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001710 dirtyLayer(left + bounds.left, top + bounds.top,
1711 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001712 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001713 }
1714 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001715#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001716
Romain Guy211370f2012-02-01 16:10:55 -08001717 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001718 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1719 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1720
1721 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1722 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1723 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1724 true, !mesh->hasEmptyQuads);
1725 } else {
1726 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1727 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1728 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1729 true, !mesh->hasEmptyQuads);
1730 }
Romain Guy054dc182010-10-15 17:55:25 -07001731 }
Chet Haase48659092012-05-31 15:21:51 -07001732
1733 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001734}
1735
Chet Haase99ecdc42011-05-06 12:06:34 -07001736/**
Chet Haase858aa932011-05-12 09:06:00 -07001737 * This function uses a similar approach to that of AA lines in the drawLines() function.
1738 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1739 * shader to compute the translucency of the color, determined by whether a given pixel is
1740 * within that boundary region and how far into the region it is.
1741 */
1742void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001743 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001744 float inverseScaleX = 1.0f;
1745 float inverseScaleY = 1.0f;
1746 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001747 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001748 Matrix4 *mat = mSnapshot->transform;
1749 float m00 = mat->data[Matrix4::kScaleX];
1750 float m01 = mat->data[Matrix4::kSkewY];
1751 float m02 = mat->data[2];
1752 float m10 = mat->data[Matrix4::kSkewX];
1753 float m11 = mat->data[Matrix4::kScaleX];
1754 float m12 = mat->data[6];
1755 float scaleX = sqrt(m00 * m00 + m01 * m01);
1756 float scaleY = sqrt(m10 * m10 + m11 * m11);
1757 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1758 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1759 }
1760
1761 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001762 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001763 setupDrawAALine();
Romain Guy0a386ff2012-07-13 12:13:07 -07001764 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Chet Haase858aa932011-05-12 09:06:00 -07001765 setupDrawColorFilter();
1766 setupDrawShader();
1767 setupDrawBlending(true, mode);
1768 setupDrawProgram();
1769 setupDrawModelViewIdentity(true);
1770 setupDrawColorUniforms();
1771 setupDrawColorFilterUniforms();
1772 setupDrawShaderIdentityUniforms();
1773
1774 AAVertex rects[4];
1775 AAVertex* aaVertices = &rects[0];
1776 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1777 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1778
1779 float boundarySizeX = .5 * inverseScaleX;
1780 float boundarySizeY = .5 * inverseScaleY;
1781
1782 // Adjust the rect by the AA boundary padding
1783 left -= boundarySizeX;
1784 right += boundarySizeX;
1785 top -= boundarySizeY;
1786 bottom += boundarySizeY;
1787
1788 float width = right - left;
1789 float height = bottom - top;
1790
Romain Guy7b631422012-04-04 11:38:54 -07001791 int widthSlot;
1792 int lengthSlot;
1793
Chet Haase858aa932011-05-12 09:06:00 -07001794 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1795 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001796 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1797 boundaryWidthProportion, widthSlot, lengthSlot);
1798
Chet Haase858aa932011-05-12 09:06:00 -07001799 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1800 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1801 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001802 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion));
Chet Haase858aa932011-05-12 09:06:00 -07001803
1804 if (!quickReject(left, top, right, bottom)) {
1805 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1806 AAVertex::set(aaVertices++, left, top, 1, 0);
1807 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1808 AAVertex::set(aaVertices++, right, top, 0, 0);
1809 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1810 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1811 }
Romain Guy7b631422012-04-04 11:38:54 -07001812
1813 finishDrawAALine(widthSlot, lengthSlot);
Chet Haase858aa932011-05-12 09:06:00 -07001814}
1815
1816/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001817 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1818 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1819 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1820 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1821 * of the line. Hairlines are more involved because we need to account for transform scaling
1822 * to end up with a one-pixel-wide line in screen space..
1823 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1824 * in combination with values that we calculate and pass down in this method. The basic approach
1825 * is that the quad we create contains both the core line area plus a bounding area in which
1826 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1827 * proportion of the width and the length of a given segment is represented by the boundary
1828 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1829 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1830 * on the inside). This ends up giving the result we want, with pixels that are completely
1831 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1832 * how far into the boundary region they are, which is determined by shader interpolation.
1833 */
Chet Haase48659092012-05-31 15:21:51 -07001834status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1835 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001836
1837 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001838 // We use half the stroke width here because we're going to position the quad
1839 // corner vertices half of the width away from the line endpoints
1840 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001841 // A stroke width of 0 has a special meaning in Skia:
1842 // it draws a line 1 px wide regardless of current transform
1843 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001844
Chet Haase99ecdc42011-05-06 12:06:34 -07001845 float inverseScaleX = 1.0f;
1846 float inverseScaleY = 1.0f;
1847 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001848
Chet Haase8a5cc922011-04-26 07:28:09 -07001849 int alpha;
1850 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001851
Chet Haase8a5cc922011-04-26 07:28:09 -07001852 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001853 int verticesCount = count;
1854 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001855 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001856 verticesCount += (count - 4);
1857 }
1858
1859 if (isHairLine || isAA) {
1860 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1861 // the line on the screen should always be one pixel wide regardless of scale. For
1862 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001863 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001864 Matrix4 *mat = mSnapshot->transform;
1865 float m00 = mat->data[Matrix4::kScaleX];
1866 float m01 = mat->data[Matrix4::kSkewY];
1867 float m02 = mat->data[2];
1868 float m10 = mat->data[Matrix4::kSkewX];
1869 float m11 = mat->data[Matrix4::kScaleX];
1870 float m12 = mat->data[6];
Romain Guy7b631422012-04-04 11:38:54 -07001871
Romain Guy211370f2012-02-01 16:10:55 -08001872 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1873 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001874
Chet Haase99ecdc42011-05-06 12:06:34 -07001875 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1876 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001877
Chet Haase99ecdc42011-05-06 12:06:34 -07001878 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1879 scaled = true;
1880 }
1881 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001882 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001883
1884 getAlphaAndMode(paint, &alpha, &mode);
1885 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001886 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001887 if (isAA) {
1888 setupDrawAALine();
1889 }
1890 setupDrawColor(paint->getColor(), alpha);
1891 setupDrawColorFilter();
1892 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001893 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001894 setupDrawProgram();
1895 setupDrawModelViewIdentity(true);
1896 setupDrawColorUniforms();
1897 setupDrawColorFilterUniforms();
1898 setupDrawShaderIdentityUniforms();
1899
1900 if (isHairLine) {
1901 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001902 halfStrokeWidth = isAA? 1 : .5;
1903 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001904 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001905 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001906 }
Romain Guy7b631422012-04-04 11:38:54 -07001907
1908 int widthSlot;
1909 int lengthSlot;
1910
Chet Haase5b0200b2011-04-13 17:58:08 -07001911 Vertex lines[verticesCount];
1912 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001913
Chet Haase99585ad2011-05-02 15:00:16 -07001914 AAVertex wLines[verticesCount];
1915 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001916
Romain Guy211370f2012-02-01 16:10:55 -08001917 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001918 setupDrawVertices(vertices);
1919 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001920 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1921 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001922 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001923 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1924 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001925 // We will need to calculate the actual width proportion on each segment for
1926 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1927 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001928 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1929 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001930 }
1931
Chet Haase99ecdc42011-05-06 12:06:34 -07001932 AAVertex* prevAAVertex = NULL;
1933 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001934
Chet Haase99585ad2011-05-02 15:00:16 -07001935 int boundaryLengthSlot = -1;
1936 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001937 int boundaryWidthSlot = -1;
1938 int inverseBoundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001939
Chet Haase5b0200b2011-04-13 17:58:08 -07001940 for (int i = 0; i < count; i += 4) {
1941 // a = start point, b = end point
1942 vec2 a(points[i], points[i + 1]);
1943 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001944
Chet Haase99585ad2011-05-02 15:00:16 -07001945 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001946 float boundaryLengthProportion = 0;
1947 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001948
Chet Haase5b0200b2011-04-13 17:58:08 -07001949 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001950 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001951 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001952 if (isAA) {
1953 float wideningFactor;
1954 if (fabs(n.x) >= fabs(n.y)) {
1955 wideningFactor = fabs(1.0f / n.x);
1956 } else {
1957 wideningFactor = fabs(1.0f / n.y);
1958 }
1959 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001960 }
Romain Guy7b631422012-04-04 11:38:54 -07001961
Chet Haase99ecdc42011-05-06 12:06:34 -07001962 if (scaled) {
1963 n.x *= inverseScaleX;
1964 n.y *= inverseScaleY;
1965 }
1966 } else if (scaled) {
1967 // Extend n by .5 pixel on each side, post-transform
1968 vec2 extendedN = n.copyNormalized();
1969 extendedN /= 2;
1970 extendedN.x *= inverseScaleX;
1971 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07001972
Chet Haase99ecdc42011-05-06 12:06:34 -07001973 float extendedNLength = extendedN.length();
1974 // We need to set this value on the shader prior to drawing
1975 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1976 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001977 }
Romain Guy7b631422012-04-04 11:38:54 -07001978
Chet Haase5b0200b2011-04-13 17:58:08 -07001979 float x = n.x;
1980 n.x = -n.y;
1981 n.y = x;
1982
Chet Haase99585ad2011-05-02 15:00:16 -07001983 // aa lines expand the endpoint vertices to encompass the AA boundary
1984 if (isAA) {
1985 vec2 abVector = (b - a);
1986 length = abVector.length();
1987 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07001988
Chet Haase99ecdc42011-05-06 12:06:34 -07001989 if (scaled) {
1990 abVector.x *= inverseScaleX;
1991 abVector.y *= inverseScaleY;
1992 float abLength = abVector.length();
1993 boundaryLengthProportion = abLength / (length + abLength);
1994 } else {
1995 boundaryLengthProportion = .5 / (length + 1);
1996 }
Romain Guy7b631422012-04-04 11:38:54 -07001997
Chet Haase99ecdc42011-05-06 12:06:34 -07001998 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001999 a -= abVector;
2000 b += abVector;
2001 }
2002
Chet Haase5b0200b2011-04-13 17:58:08 -07002003 // Four corners of the rectangle defining a thick line
2004 vec2 p1 = a - n;
2005 vec2 p2 = a + n;
2006 vec2 p3 = b + n;
2007 vec2 p4 = b - n;
2008
Chet Haase99585ad2011-05-02 15:00:16 -07002009
Chet Haase5b0200b2011-04-13 17:58:08 -07002010 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2011 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2012 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2013 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2014
2015 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002016 if (!isAA) {
2017 if (prevVertex != NULL) {
2018 // Issue two repeat vertices to create degenerate triangles to bridge
2019 // between the previous line and the new one. This is necessary because
2020 // we are creating a single triangle_strip which will contain
2021 // potentially discontinuous line segments.
2022 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2023 Vertex::set(vertices++, p1.x, p1.y);
2024 generatedVerticesCount += 2;
2025 }
Romain Guy7b631422012-04-04 11:38:54 -07002026
Chet Haase5b0200b2011-04-13 17:58:08 -07002027 Vertex::set(vertices++, p1.x, p1.y);
2028 Vertex::set(vertices++, p2.x, p2.y);
2029 Vertex::set(vertices++, p4.x, p4.y);
2030 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002031
Chet Haase5b0200b2011-04-13 17:58:08 -07002032 prevVertex = vertices - 1;
2033 generatedVerticesCount += 4;
2034 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002035 if (!isHairLine && scaled) {
2036 // Must set width proportions per-segment for scaled non-hairlines to use the
2037 // correct AA boundary dimensions
2038 if (boundaryWidthSlot < 0) {
2039 boundaryWidthSlot =
2040 mCaches.currentProgram->getUniform("boundaryWidth");
2041 inverseBoundaryWidthSlot =
2042 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
2043 }
Romain Guy7b631422012-04-04 11:38:54 -07002044
Chet Haase99ecdc42011-05-06 12:06:34 -07002045 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
2046 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
2047 }
Romain Guy7b631422012-04-04 11:38:54 -07002048
Chet Haase99585ad2011-05-02 15:00:16 -07002049 if (boundaryLengthSlot < 0) {
2050 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
2051 inverseBoundaryLengthSlot =
2052 mCaches.currentProgram->getUniform("inverseBoundaryLength");
2053 }
Romain Guy7b631422012-04-04 11:38:54 -07002054
Chet Haase99ecdc42011-05-06 12:06:34 -07002055 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
2056 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07002057
Chet Haase5b0200b2011-04-13 17:58:08 -07002058 if (prevAAVertex != NULL) {
2059 // Issue two repeat vertices to create degenerate triangles to bridge
2060 // between the previous line and the new one. This is necessary because
2061 // we are creating a single triangle_strip which will contain
2062 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002063 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2064 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2065 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002066 generatedVerticesCount += 2;
2067 }
Romain Guy7b631422012-04-04 11:38:54 -07002068
Chet Haase99585ad2011-05-02 15:00:16 -07002069 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2070 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2071 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2072 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002073
Chet Haase5b0200b2011-04-13 17:58:08 -07002074 prevAAVertex = aaVertices - 1;
2075 generatedVerticesCount += 4;
2076 }
Romain Guy7b631422012-04-04 11:38:54 -07002077
Chet Haase99585ad2011-05-02 15:00:16 -07002078 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2079 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2080 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002081 }
2082 }
Romain Guy7b631422012-04-04 11:38:54 -07002083
Chet Haase5b0200b2011-04-13 17:58:08 -07002084 if (generatedVerticesCount > 0) {
2085 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2086 }
Romain Guy7b631422012-04-04 11:38:54 -07002087
2088 if (isAA) {
2089 finishDrawAALine(widthSlot, lengthSlot);
2090 }
Chet Haase48659092012-05-31 15:21:51 -07002091
2092 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002093}
2094
Chet Haase48659092012-05-31 15:21:51 -07002095status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2096 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002097
2098 // TODO: The paint's cap style defines whether the points are square or circular
2099 // TODO: Handle AA for round points
2100
Chet Haase5b0200b2011-04-13 17:58:08 -07002101 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002102 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002103 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002104 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002105 if (isHairLine) {
2106 // Now that we know it's hairline, we can set the effective width, to be used later
2107 strokeWidth = 1.0f;
2108 }
2109 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002110 int alpha;
2111 SkXfermode::Mode mode;
2112 getAlphaAndMode(paint, &alpha, &mode);
2113
2114 int verticesCount = count >> 1;
2115 int generatedVerticesCount = 0;
2116
2117 TextureVertex pointsData[verticesCount];
2118 TextureVertex* vertex = &pointsData[0];
2119
2120 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002121 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002122 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002123 setupDrawColor(paint->getColor(), alpha);
2124 setupDrawColorFilter();
2125 setupDrawShader();
2126 setupDrawBlending(mode);
2127 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002128 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002129 setupDrawColorUniforms();
2130 setupDrawColorFilterUniforms();
2131 setupDrawPointUniforms();
2132 setupDrawShaderIdentityUniforms();
2133 setupDrawMesh(vertex);
2134
2135 for (int i = 0; i < count; i += 2) {
2136 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2137 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002138
Chet Haase8a5cc922011-04-26 07:28:09 -07002139 float left = points[i] - halfWidth;
2140 float right = points[i] + halfWidth;
2141 float top = points[i + 1] - halfWidth;
2142 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002143
Chet Haase8a5cc922011-04-26 07:28:09 -07002144 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002145 }
2146
2147 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002148
2149 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002150}
2151
Chet Haase48659092012-05-31 15:21:51 -07002152status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002153 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002154 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002155
Romain Guyae88e5e2010-10-22 17:49:18 -07002156 Rect& clip(*mSnapshot->clipRect);
2157 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002158
Romain Guy3d58c032010-07-14 16:34:53 -07002159 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002160
2161 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002162}
Romain Guy9d5316e2010-06-24 19:30:36 -07002163
Chet Haase48659092012-05-31 15:21:51 -07002164status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2165 SkPaint* paint) {
2166 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002167 const AutoTexture autoCleanup(texture);
2168
2169 const float x = left + texture->left - texture->offset;
2170 const float y = top + texture->top - texture->offset;
2171
2172 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002173
2174 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002175}
2176
Chet Haase48659092012-05-31 15:21:51 -07002177status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002178 float rx, float ry, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002179 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002180
Romain Guya1d3c912011-12-13 14:55:06 -08002181 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002182 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2183 right - left, bottom - top, rx, ry, paint);
Chet Haase48659092012-05-31 15:21:51 -07002184 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002185}
2186
Chet Haase48659092012-05-31 15:21:51 -07002187status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2188 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002189
Romain Guya1d3c912011-12-13 14:55:06 -08002190 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002191 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Chet Haase48659092012-05-31 15:21:51 -07002192 return drawShape(x - radius, y - radius, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002193}
Romain Guy01d58e42011-01-19 21:54:02 -08002194
Chet Haase48659092012-05-31 15:21:51 -07002195status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
2196 SkPaint* paint) {
2197 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002198
Romain Guya1d3c912011-12-13 14:55:06 -08002199 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002200 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002201 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002202}
2203
Chet Haase48659092012-05-31 15:21:51 -07002204status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002205 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002206 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002207
2208 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002209 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002210 }
2211
Romain Guya1d3c912011-12-13 14:55:06 -08002212 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002213 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2214 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002215 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002216}
2217
Chet Haase48659092012-05-31 15:21:51 -07002218status_t OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002219 SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002220 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002221
Romain Guya1d3c912011-12-13 14:55:06 -08002222 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002223 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002224 return drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002225}
2226
Chet Haase48659092012-05-31 15:21:51 -07002227status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002228 if (p->getStyle() != SkPaint::kFill_Style) {
Chet Haase48659092012-05-31 15:21:51 -07002229 return drawRectAsShape(left, top, right, bottom, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002230 }
2231
Romain Guy6926c722010-07-12 20:20:03 -07002232 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002233 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002234 }
2235
Romain Guy026c5e162010-06-28 17:12:22 -07002236 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002237 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002238 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2239 if (!isMode) {
2240 // Assume SRC_OVER
2241 mode = SkXfermode::kSrcOver_Mode;
2242 }
2243 } else {
2244 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002245 }
2246
Romain Guy026c5e162010-06-28 17:12:22 -07002247 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002248 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002249 drawAARect(left, top, right, bottom, color, mode);
2250 } else {
2251 drawColorRect(left, top, right, bottom, color, mode);
2252 }
Chet Haase48659092012-05-31 15:21:51 -07002253
2254 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002255}
Romain Guy9d5316e2010-06-24 19:30:36 -07002256
Chet Haase48659092012-05-31 15:21:51 -07002257status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002258 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002259 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy0a386ff2012-07-13 12:13:07 -07002260 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002261 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002262 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002263
Romain Guy671d6cf2012-01-18 12:39:17 -08002264 // NOTE: Skia does not support perspective transform on drawPosText yet
2265 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002266 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002267 }
2268
2269 float x = 0.0f;
2270 float y = 0.0f;
2271 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2272 if (pureTranslate) {
2273 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2274 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2275 }
2276
2277 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2278 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2279 paint->getTextSize());
2280
2281 int alpha;
2282 SkXfermode::Mode mode;
2283 getAlphaAndMode(paint, &alpha, &mode);
2284
2285 // Pick the appropriate texture filtering
2286 bool linearFilter = mSnapshot->transform->changesBounds();
2287 if (pureTranslate && !linearFilter) {
2288 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2289 }
2290
2291 mCaches.activeTexture(0);
2292 setupDraw();
2293 setupDrawDirtyRegionsDisabled();
2294 setupDrawWithTexture(true);
2295 setupDrawAlpha8Color(paint->getColor(), alpha);
2296 setupDrawColorFilter();
2297 setupDrawShader();
2298 setupDrawBlending(true, mode);
2299 setupDrawProgram();
2300 setupDrawModelView(x, y, x, y, pureTranslate, true);
2301 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2302 setupDrawPureColorUniforms();
2303 setupDrawColorFilterUniforms();
2304 setupDrawShaderUniforms(pureTranslate);
2305
2306 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2307 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2308
2309#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002310 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002311#else
Romain Guy211370f2012-02-01 16:10:55 -08002312 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002313#endif
2314
2315 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2316 positions, hasActiveLayer ? &bounds : NULL)) {
2317#if RENDER_LAYERS_AS_REGIONS
2318 if (hasActiveLayer) {
2319 if (!pureTranslate) {
2320 mSnapshot->transform->mapRect(bounds);
2321 }
2322 dirtyLayerUnchecked(bounds, getRegion());
2323 }
2324#endif
2325 }
Chet Haase48659092012-05-31 15:21:51 -07002326
2327 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002328}
2329
Chet Haase48659092012-05-31 15:21:51 -07002330status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002331 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002332 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy0a386ff2012-07-13 12:13:07 -07002333 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002334 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002335 }
2336
Chet Haasea1cff502012-02-21 13:43:44 -08002337 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002338 switch (paint->getTextAlign()) {
2339 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002340 x -= length / 2.0f;
2341 break;
2342 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002343 x -= length;
2344 break;
2345 default:
2346 break;
2347 }
2348
Romain Guycac5fd32011-12-01 20:08:50 -08002349 SkPaint::FontMetrics metrics;
2350 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002351 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002352 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002353 }
2354
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002355 const float oldX = x;
2356 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002357 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002358 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002359 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2360 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2361 }
2362
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002363#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002364 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002365#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002366
2367 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002368 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002369 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002370
Romain Guy86568192010-12-14 15:55:39 -08002371 int alpha;
2372 SkXfermode::Mode mode;
2373 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002374
Romain Guy211370f2012-02-01 16:10:55 -08002375 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002376 mCaches.activeTexture(0);
2377
Romain Guyb45c0c92010-08-26 20:35:23 -07002378 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002379 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2380 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002381 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002382
Romain Guy740bf2b2011-04-26 15:33:10 -07002383 const float sx = oldX - shadow->left + mShadowDx;
2384 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002385
Romain Guy0a386ff2012-07-13 12:13:07 -07002386 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
Romain Guy740bf2b2011-04-26 15:33:10 -07002387 int shadowColor = mShadowColor;
2388 if (mShader) {
2389 shadowColor = 0xffffffff;
2390 }
Romain Guy86568192010-12-14 15:55:39 -08002391
Romain Guy86568192010-12-14 15:55:39 -08002392 setupDraw();
2393 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002394 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2395 setupDrawColorFilter();
2396 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002397 setupDrawBlending(true, mode);
2398 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002399 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002400 setupDrawTexture(shadow->id);
2401 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002402 setupDrawColorFilterUniforms();
2403 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002404 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2405
Romain Guy0a417492010-08-16 20:26:20 -07002406 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002407 }
2408
Romain Guy6620c6d2010-12-06 18:07:02 -08002409 // Pick the appropriate texture filtering
2410 bool linearFilter = mSnapshot->transform->changesBounds();
2411 if (pureTranslate && !linearFilter) {
2412 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2413 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002414
Romain Guy16c88082012-06-11 16:03:47 -07002415 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002416 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002417 setupDraw();
2418 setupDrawDirtyRegionsDisabled();
2419 setupDrawWithTexture(true);
2420 setupDrawAlpha8Color(paint->getColor(), alpha);
2421 setupDrawColorFilter();
2422 setupDrawShader();
2423 setupDrawBlending(true, mode);
2424 setupDrawProgram();
2425 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002426 // See comment above; the font renderer must use texture unit 0
2427 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002428 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2429 setupDrawPureColorUniforms();
2430 setupDrawColorFilterUniforms();
2431 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002432
Romain Guy6620c6d2010-12-06 18:07:02 -08002433 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002434 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2435
2436#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002437 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002438#else
Romain Guy211370f2012-02-01 16:10:55 -08002439 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002440#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002441
Romain Guy6620c6d2010-12-06 18:07:02 -08002442 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002443 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002444#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002445 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002446 if (!pureTranslate) {
2447 mSnapshot->transform->mapRect(bounds);
2448 }
Romain Guyf219da52011-01-16 12:54:25 -08002449 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002450 }
2451#endif
2452 }
Romain Guy694b5192010-07-21 21:33:20 -07002453
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002454 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002455
2456 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002457}
2458
Chet Haase48659092012-05-31 15:21:51 -07002459status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002460 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002461 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2462 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002463 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002464 }
2465
Romain Guy03d58522012-02-24 17:54:07 -08002466 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2467 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2468 paint->getTextSize());
2469
2470 int alpha;
2471 SkXfermode::Mode mode;
2472 getAlphaAndMode(paint, &alpha, &mode);
2473
2474 mCaches.activeTexture(0);
2475 setupDraw();
2476 setupDrawDirtyRegionsDisabled();
2477 setupDrawWithTexture(true);
2478 setupDrawAlpha8Color(paint->getColor(), alpha);
2479 setupDrawColorFilter();
2480 setupDrawShader();
2481 setupDrawBlending(true, mode);
2482 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002483 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002484 setupDrawTexture(fontRenderer.getTexture(true));
2485 setupDrawPureColorUniforms();
2486 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002487 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002488
Romain Guy97771732012-02-28 18:17:02 -08002489 const Rect* clip = &mSnapshot->getLocalClip();
2490 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 -08002491
Romain Guy97771732012-02-28 18:17:02 -08002492#if RENDER_LAYERS_AS_REGIONS
2493 const bool hasActiveLayer = hasLayer();
2494#else
2495 const bool hasActiveLayer = false;
2496#endif
2497
2498 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2499 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2500#if RENDER_LAYERS_AS_REGIONS
2501 if (hasActiveLayer) {
2502 mSnapshot->transform->mapRect(bounds);
2503 dirtyLayerUnchecked(bounds, getRegion());
2504 }
2505#endif
2506 }
Chet Haase48659092012-05-31 15:21:51 -07002507
2508 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002509}
2510
Chet Haase48659092012-05-31 15:21:51 -07002511status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2512 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002513
Romain Guya1d3c912011-12-13 14:55:06 -08002514 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002515
Romain Guy33f6beb2012-02-16 19:24:51 -08002516 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002517 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002518 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002519 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002520
Romain Guy8b55f372010-08-18 17:10:07 -07002521 const float x = texture->left - texture->offset;
2522 const float y = texture->top - texture->offset;
2523
Romain Guy01d58e42011-01-19 21:54:02 -08002524 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002525
2526 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002527}
2528
Chet Haase48659092012-05-31 15:21:51 -07002529status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guyada830f2011-01-13 12:13:20 -08002530 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Chet Haase48659092012-05-31 15:21:51 -07002531 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002532 }
2533
Romain Guy2bf68f02012-03-02 13:37:47 -08002534 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2535 OpenGLRenderer* renderer = layer->renderer;
2536 Rect& dirty = layer->dirtyRect;
2537
2538 interrupt();
2539 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2540 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002541 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002542 renderer->finish();
2543 resume();
2544
2545 dirty.setEmpty();
2546 layer->deferredUpdateScheduled = false;
2547 layer->renderer = NULL;
2548 layer->displayList = NULL;
2549 }
2550
Romain Guya1d3c912011-12-13 14:55:06 -08002551 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002552
2553 int alpha;
2554 SkXfermode::Mode mode;
2555 getAlphaAndMode(paint, &alpha, &mode);
2556
Romain Guy9ace8f52011-07-07 20:50:11 -07002557 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002558
Romain Guyf219da52011-01-16 12:54:25 -08002559#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002560 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002561 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002562 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002563 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002564 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002565 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002566
Romain Guyc88e3572011-01-22 00:32:12 -08002567 setupDraw();
2568 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002569 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002570 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002571 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002572 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002573 setupDrawPureColorUniforms();
2574 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002575 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002576 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002577 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2578 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2579
Romain Guyd21b6e12011-11-30 20:21:23 -08002580 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002581 setupDrawModelViewTranslate(x, y,
2582 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2583 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002584 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002585 setupDrawModelViewTranslate(x, y,
2586 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2587 }
Romain Guyc88e3572011-01-22 00:32:12 -08002588 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002589
Romain Guyc88e3572011-01-22 00:32:12 -08002590 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2591 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002592
Romain Guyc88e3572011-01-22 00:32:12 -08002593 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002594
2595#if DEBUG_LAYERS_AS_REGIONS
2596 drawRegionRects(layer->region);
2597#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002598 }
Romain Guyf219da52011-01-16 12:54:25 -08002599 }
2600#else
Romain Guyada830f2011-01-13 12:13:20 -08002601 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2602 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002603#endif
Chet Haase48659092012-05-31 15:21:51 -07002604
2605 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002606}
2607
Romain Guy6926c722010-07-12 20:20:03 -07002608///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002609// Shaders
2610///////////////////////////////////////////////////////////////////////////////
2611
2612void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002613 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002614}
2615
Romain Guy06f96e22010-07-30 19:18:16 -07002616void OpenGLRenderer::setupShader(SkiaShader* shader) {
2617 mShader = shader;
2618 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002619 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002620 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002621}
2622
Romain Guyd27977d2010-07-14 19:18:51 -07002623///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002624// Color filters
2625///////////////////////////////////////////////////////////////////////////////
2626
2627void OpenGLRenderer::resetColorFilter() {
2628 mColorFilter = NULL;
2629}
2630
2631void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2632 mColorFilter = filter;
2633}
2634
2635///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002636// Drop shadow
2637///////////////////////////////////////////////////////////////////////////////
2638
2639void OpenGLRenderer::resetShadow() {
2640 mHasShadow = false;
2641}
2642
2643void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2644 mHasShadow = true;
2645 mShadowRadius = radius;
2646 mShadowDx = dx;
2647 mShadowDy = dy;
2648 mShadowColor = color;
2649}
2650
2651///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002652// Draw filters
2653///////////////////////////////////////////////////////////////////////////////
2654
2655void OpenGLRenderer::resetPaintFilter() {
2656 mHasDrawFilter = false;
2657}
2658
2659void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2660 mHasDrawFilter = true;
2661 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2662 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2663}
2664
2665SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002666 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002667
2668 uint32_t flags = paint->getFlags();
2669
2670 mFilteredPaint = *paint;
2671 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2672
2673 return &mFilteredPaint;
2674}
2675
2676///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002677// Drawing implementation
2678///////////////////////////////////////////////////////////////////////////////
2679
Romain Guy01d58e42011-01-19 21:54:02 -08002680void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2681 float x, float y, SkPaint* paint) {
2682 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2683 return;
2684 }
2685
2686 int alpha;
2687 SkXfermode::Mode mode;
2688 getAlphaAndMode(paint, &alpha, &mode);
2689
2690 setupDraw();
2691 setupDrawWithTexture(true);
2692 setupDrawAlpha8Color(paint->getColor(), alpha);
2693 setupDrawColorFilter();
2694 setupDrawShader();
2695 setupDrawBlending(true, mode);
2696 setupDrawProgram();
2697 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2698 setupDrawTexture(texture->id);
2699 setupDrawPureColorUniforms();
2700 setupDrawColorFilterUniforms();
2701 setupDrawShaderUniforms();
2702 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2703
2704 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2705
2706 finishDrawTexture();
2707}
2708
Romain Guyf607bdc2010-09-10 19:20:06 -07002709// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002710#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2711#define kStdUnderline_Offset (1.0f / 9.0f)
2712#define kStdUnderline_Thickness (1.0f / 18.0f)
2713
2714void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2715 float x, float y, SkPaint* paint) {
2716 // Handle underline and strike-through
2717 uint32_t flags = paint->getFlags();
2718 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002719 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002720 float underlineWidth = length;
2721 // If length is > 0.0f, we already measured the text for the text alignment
2722 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002723 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002724 }
2725
2726 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002727 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002728 case SkPaint::kCenter_Align:
2729 offsetX = underlineWidth * 0.5f;
2730 break;
2731 case SkPaint::kRight_Align:
2732 offsetX = underlineWidth;
2733 break;
2734 default:
2735 break;
2736 }
2737
Romain Guy211370f2012-02-01 16:10:55 -08002738 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002739 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002740 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002741
Romain Guye20ecbd2010-09-22 19:49:04 -07002742 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002743 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002744
Romain Guyf6834472011-01-23 13:32:12 -08002745 int linesCount = 0;
2746 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2747 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2748
2749 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002750 float points[pointsCount];
2751 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002752
2753 if (flags & SkPaint::kUnderlineText_Flag) {
2754 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002755 points[currentPoint++] = left;
2756 points[currentPoint++] = top;
2757 points[currentPoint++] = left + underlineWidth;
2758 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002759 }
2760
2761 if (flags & SkPaint::kStrikeThruText_Flag) {
2762 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002763 points[currentPoint++] = left;
2764 points[currentPoint++] = top;
2765 points[currentPoint++] = left + underlineWidth;
2766 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002767 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002768
Romain Guy726aeba2011-06-01 14:52:00 -07002769 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002770
Romain Guy726aeba2011-06-01 14:52:00 -07002771 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002772 }
2773 }
2774}
2775
Romain Guy026c5e162010-06-28 17:12:22 -07002776void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002777 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002778 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002779 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002780 color |= 0x00ffffff;
2781 }
2782
Romain Guy70ca14e2010-12-13 18:24:33 -08002783 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002784 setupDrawNoTexture();
Romain Guy0a386ff2012-07-13 12:13:07 -07002785 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002786 setupDrawShader();
2787 setupDrawColorFilter();
2788 setupDrawBlending(mode);
2789 setupDrawProgram();
2790 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2791 setupDrawColorUniforms();
2792 setupDrawShaderUniforms(ignoreTransform);
2793 setupDrawColorFilterUniforms();
2794 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002795
Romain Guyc95c8d62010-09-17 15:31:32 -07002796 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2797}
2798
Romain Guy82ba8142010-07-09 13:25:56 -07002799void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002800 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002801 int alpha;
2802 SkXfermode::Mode mode;
2803 getAlphaAndMode(paint, &alpha, &mode);
2804
Romain Guyd21b6e12011-11-30 20:21:23 -08002805 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002806
Romain Guy211370f2012-02-01 16:10:55 -08002807 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002808 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2809 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2810
Romain Guyd21b6e12011-11-30 20:21:23 -08002811 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002812 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2813 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2814 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2815 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002816 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002817 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2818 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2819 GL_TRIANGLE_STRIP, gMeshCount);
2820 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002821}
2822
Romain Guybd6b79b2010-06-26 00:13:53 -07002823void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002824 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2825 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002826 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002827}
2828
2829void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002830 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002831 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002832 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002833
Romain Guy746b7402010-10-26 16:27:31 -07002834 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002835 setupDrawWithTexture();
2836 setupDrawColor(alpha, alpha, alpha, alpha);
2837 setupDrawColorFilter();
2838 setupDrawBlending(blend, mode, swapSrcDst);
2839 setupDrawProgram();
2840 if (!dirty) {
2841 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002842 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002843 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002844 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002845 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002846 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002847 }
Romain Guy86568192010-12-14 15:55:39 -08002848 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002849 setupDrawColorFilterUniforms();
2850 setupDrawTexture(texture);
2851 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002852
Romain Guy6820ac82010-09-15 18:11:50 -07002853 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002854
2855 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002856}
2857
Romain Guya5aed0d2010-09-09 14:42:43 -07002858void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002859 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002860 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002861
Romain Guy82ba8142010-07-09 13:25:56 -07002862 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002863 // These blend modes are not supported by OpenGL directly and have
2864 // to be implemented using shaders. Since the shader will perform
2865 // the blending, turn blending off here
2866 // If the blend mode cannot be implemented using shaders, fall
2867 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002868 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2869 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002870 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002871 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002872
Romain Guy82bc7a72012-01-03 14:13:39 -08002873 if (mCaches.blend) {
2874 glDisable(GL_BLEND);
2875 mCaches.blend = false;
2876 }
2877
2878 return;
2879 } else {
2880 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002881 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002882 }
2883
2884 if (!mCaches.blend) {
2885 glEnable(GL_BLEND);
2886 }
2887
2888 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2889 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2890
2891 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2892 glBlendFunc(sourceMode, destMode);
2893 mCaches.lastSrcMode = sourceMode;
2894 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002895 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002896 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002897 glDisable(GL_BLEND);
2898 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002899 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002900}
2901
Romain Guy889f8d12010-07-29 14:37:42 -07002902bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002903 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002904 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002905 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002906 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002907 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002908 }
Romain Guy6926c722010-07-12 20:20:03 -07002909 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002910}
2911
Romain Guy026c5e162010-06-28 17:12:22 -07002912void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002913 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002914 TextureVertex::setUV(v++, u1, v1);
2915 TextureVertex::setUV(v++, u2, v1);
2916 TextureVertex::setUV(v++, u1, v2);
2917 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002918}
2919
Chet Haase5c13d892010-10-08 08:37:55 -07002920void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002921 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002922 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002923
2924 // Skia draws using the color's alpha channel if < 255
2925 // Otherwise, it uses the paint's alpha
2926 int color = paint->getColor();
2927 *alpha = (color >> 24) & 0xFF;
2928 if (*alpha == 255) {
2929 *alpha = paint->getAlpha();
2930 }
2931 } else {
2932 *mode = SkXfermode::kSrcOver_Mode;
2933 *alpha = 255;
2934 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07002935 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002936}
2937
Romain Guya5aed0d2010-09-09 14:42:43 -07002938SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002939 SkXfermode::Mode resultMode;
2940 if (!SkXfermode::AsMode(mode, &resultMode)) {
2941 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002942 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002943 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002944}
2945
Romain Guy9d5316e2010-06-24 19:30:36 -07002946}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002947}; // namespace android