blob: c92bead45586d88c83b2499c5eef408fc1e63116 [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 Haasedb8c9a62012-03-21 18:54:18 -07001152 mColorA *= mSnapshot->alpha;
Chet Haase6cfdf452011-04-22 16:42:10 -07001153 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1154 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001155 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001156 mColorR = a * ((color >> 16) & 0xFF);
1157 mColorG = a * ((color >> 8) & 0xFF);
1158 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001159 mColorSet = true;
1160 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1161}
1162
Romain Guy86568192010-12-14 15:55:39 -08001163void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1164 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001165 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1166 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001167 const float a = mColorA / 255.0f;
1168 mColorR = a * ((color >> 16) & 0xFF);
1169 mColorG = a * ((color >> 8) & 0xFF);
1170 mColorB = a * ((color ) & 0xFF);
1171 mColorSet = true;
1172 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1173}
1174
Romain Guy70ca14e2010-12-13 18:24:33 -08001175void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1176 mColorA = a;
1177 mColorR = r;
1178 mColorG = g;
1179 mColorB = b;
1180 mColorSet = true;
1181 mSetShaderColor = mDescription.setColor(r, g, b, a);
1182}
1183
Romain Guy86568192010-12-14 15:55:39 -08001184void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1185 mColorA = a;
1186 mColorR = r;
1187 mColorG = g;
1188 mColorB = b;
1189 mColorSet = true;
1190 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1191}
1192
Romain Guy70ca14e2010-12-13 18:24:33 -08001193void OpenGLRenderer::setupDrawShader() {
1194 if (mShader) {
1195 mShader->describe(mDescription, mCaches.extensions);
1196 }
1197}
1198
1199void OpenGLRenderer::setupDrawColorFilter() {
1200 if (mColorFilter) {
1201 mColorFilter->describe(mDescription, mCaches.extensions);
1202 }
1203}
1204
Romain Guyf09ef512011-05-27 11:43:46 -07001205void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1206 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1207 mColorA = 1.0f;
1208 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001209 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001210 }
1211}
1212
Romain Guy70ca14e2010-12-13 18:24:33 -08001213void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001214 // When the blending mode is kClear_Mode, we need to use a modulate color
1215 // argb=1,0,0,0
1216 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001217 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1218 mDescription, swapSrcDst);
1219}
1220
1221void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001222 // When the blending mode is kClear_Mode, we need to use a modulate color
1223 // argb=1,0,0,0
1224 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001225 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1226 mDescription, swapSrcDst);
1227}
1228
1229void OpenGLRenderer::setupDrawProgram() {
1230 useProgram(mCaches.programCache.get(mDescription));
1231}
1232
1233void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1234 mTrackDirtyRegions = false;
1235}
1236
1237void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1238 bool ignoreTransform) {
1239 mModelView.loadTranslate(left, top, 0.0f);
1240 if (!ignoreTransform) {
1241 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1242 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1243 } else {
1244 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1245 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1246 }
1247}
1248
Chet Haase8a5cc922011-04-26 07:28:09 -07001249void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1250 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001251}
1252
Romain Guy70ca14e2010-12-13 18:24:33 -08001253void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1254 bool ignoreTransform, bool ignoreModelView) {
1255 if (!ignoreModelView) {
1256 mModelView.loadTranslate(left, top, 0.0f);
1257 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001258 } else {
1259 mModelView.loadIdentity();
1260 }
Romain Guy86568192010-12-14 15:55:39 -08001261 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1262 if (!ignoreTransform) {
1263 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1264 if (mTrackDirtyRegions && dirty) {
1265 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1266 }
1267 } else {
1268 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1269 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1270 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001271}
1272
Romain Guyed6fcb02011-03-21 13:11:28 -07001273void OpenGLRenderer::setupDrawPointUniforms() {
1274 int slot = mCaches.currentProgram->getUniform("pointSize");
1275 glUniform1f(slot, mDescription.pointSize);
1276}
1277
Romain Guy70ca14e2010-12-13 18:24:33 -08001278void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001279 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001280 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1281 }
1282}
1283
Romain Guy86568192010-12-14 15:55:39 -08001284void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001285 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001286 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001287 }
1288}
1289
Romain Guy70ca14e2010-12-13 18:24:33 -08001290void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1291 if (mShader) {
1292 if (ignoreTransform) {
1293 mModelView.loadInverse(*mSnapshot->transform);
1294 }
1295 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1296 }
1297}
1298
Romain Guy8d0d4782010-12-14 20:13:35 -08001299void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1300 if (mShader) {
1301 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1302 }
1303}
1304
Romain Guy70ca14e2010-12-13 18:24:33 -08001305void OpenGLRenderer::setupDrawColorFilterUniforms() {
1306 if (mColorFilter) {
1307 mColorFilter->setupProgram(mCaches.currentProgram);
1308 }
1309}
1310
1311void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001312 bool force = mCaches.bindMeshBuffer();
1313 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001314 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001315}
1316
1317void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1318 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001319 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001320 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001321}
1322
Romain Guyaa6c24c2011-04-28 18:40:04 -07001323void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1324 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001325 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001326 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001327}
1328
Romain Guy8f0095c2011-05-02 17:24:22 -07001329void OpenGLRenderer::setupDrawTextureTransform() {
1330 mDescription.hasTextureTransform = true;
1331}
1332
1333void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001334 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1335 GL_FALSE, &transform.data[0]);
1336}
1337
Romain Guy70ca14e2010-12-13 18:24:33 -08001338void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001339 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001340 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001341 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001342 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001343 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001344 }
Romain Guyd71dd362011-12-12 19:03:35 -08001345
Romain Guyf3a910b42011-12-12 20:35:21 -08001346 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001347 if (mCaches.currentProgram->texCoords >= 0) {
1348 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1349 }
1350
1351 mCaches.unbindIndicesBuffer();
1352}
1353
1354void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1355 bool force = mCaches.unbindMeshBuffer();
1356 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1357 if (mCaches.currentProgram->texCoords >= 0) {
1358 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001359 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001360}
1361
Chet Haase5b0200b2011-04-13 17:58:08 -07001362void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001363 bool force = mCaches.unbindMeshBuffer();
1364 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1365 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001366 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001367}
1368
1369/**
1370 * 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 -07001371 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1372 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1373 * attributes (one per vertex) are values from zero to one that tells the fragment
1374 * shader where the fragment is in relation to the line width/length overall; these values are
1375 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1376 * region of the line.
1377 * Note that we only pass down the width values in this setup function. The length coordinates
1378 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001379 */
Chet Haase99585ad2011-05-02 15:00:16 -07001380void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001381 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001382 bool force = mCaches.unbindMeshBuffer();
1383 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1384 vertices, gAAVertexStride);
1385 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001386 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001387
Romain Guy7b631422012-04-04 11:38:54 -07001388 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001389 glEnableVertexAttribArray(widthSlot);
1390 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001391
Romain Guy7b631422012-04-04 11:38:54 -07001392 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001393 glEnableVertexAttribArray(lengthSlot);
1394 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001395
Chet Haase5b0200b2011-04-13 17:58:08 -07001396 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001397 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001398
Chet Haase99585ad2011-05-02 15:00:16 -07001399 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001400 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Romain Guy7b631422012-04-04 11:38:54 -07001401 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion);
1402}
1403
1404void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1405 glDisableVertexAttribArray(widthSlot);
1406 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001407}
1408
Romain Guy70ca14e2010-12-13 18:24:33 -08001409void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001410}
1411
1412///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001413// Drawing
1414///////////////////////////////////////////////////////////////////////////////
1415
Chet Haase1271e2c2012-04-20 09:54:27 -07001416status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001417 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001418
Romain Guy0fe478e2010-11-08 12:08:41 -08001419 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1420 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001421 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001422 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001423 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001424
Romain Guy65549432012-03-26 16:45:05 -07001425 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001426}
1427
Chet Haaseed30fd82011-04-22 16:18:45 -07001428void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1429 if (displayList) {
1430 displayList->output(*this, level);
1431 }
1432}
1433
Romain Guya168d732011-03-18 16:50:13 -07001434void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1435 int alpha;
1436 SkXfermode::Mode mode;
1437 getAlphaAndMode(paint, &alpha, &mode);
1438
Romain Guya168d732011-03-18 16:50:13 -07001439 float x = left;
1440 float y = top;
1441
Romain Guye3c26852011-07-25 16:36:01 -07001442 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001443 bool ignoreTransform = false;
1444 if (mSnapshot->transform->isPureTranslate()) {
1445 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1446 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1447 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001448 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001449 } else {
1450 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001451 }
1452
1453 setupDraw();
1454 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001455 if (paint) {
1456 setupDrawAlpha8Color(paint->getColor(), alpha);
1457 }
Romain Guya168d732011-03-18 16:50:13 -07001458 setupDrawColorFilter();
1459 setupDrawShader();
1460 setupDrawBlending(true, mode);
1461 setupDrawProgram();
1462 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001463
Romain Guya168d732011-03-18 16:50:13 -07001464 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001465 texture->setWrap(GL_CLAMP_TO_EDGE);
1466 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001467
Romain Guya168d732011-03-18 16:50:13 -07001468 setupDrawPureColorUniforms();
1469 setupDrawColorFilterUniforms();
1470 setupDrawShaderUniforms();
1471 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1472
1473 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1474
1475 finishDrawTexture();
1476}
1477
Chet Haase48659092012-05-31 15:21:51 -07001478status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001479 const float right = left + bitmap->width();
1480 const float bottom = top + bitmap->height();
1481
1482 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001483 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001484 }
1485
Romain Guya1d3c912011-12-13 14:55:06 -08001486 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001487 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001488 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001489 const AutoTexture autoCleanup(texture);
1490
Romain Guy211370f2012-02-01 16:10:55 -08001491 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001492 drawAlphaBitmap(texture, left, top, paint);
1493 } else {
1494 drawTextureRect(left, top, right, bottom, texture, paint);
1495 }
Chet Haase48659092012-05-31 15:21:51 -07001496
1497 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001498}
1499
Chet Haase48659092012-05-31 15:21:51 -07001500status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001501 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1502 const mat4 transform(*matrix);
1503 transform.mapRect(r);
1504
Romain Guy6926c722010-07-12 20:20:03 -07001505 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001506 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001507 }
1508
Romain Guya1d3c912011-12-13 14:55:06 -08001509 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001510 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001511 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001512 const AutoTexture autoCleanup(texture);
1513
Romain Guy5b3b3522010-10-27 18:57:51 -07001514 // This could be done in a cheaper way, all we need is pass the matrix
1515 // to the vertex shader. The save/restore is a bit overkill.
1516 save(SkCanvas::kMatrix_SaveFlag);
1517 concatMatrix(matrix);
1518 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1519 restore();
Chet Haase48659092012-05-31 15:21:51 -07001520
1521 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001522}
1523
Chet Haase48659092012-05-31 15:21:51 -07001524status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001525 const float right = left + bitmap->width();
1526 const float bottom = top + bitmap->height();
1527
1528 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001529 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001530 }
1531
1532 mCaches.activeTexture(0);
1533 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1534 const AutoTexture autoCleanup(texture);
1535
1536 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001537
1538 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001539}
1540
Chet Haase48659092012-05-31 15:21:51 -07001541status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001542 float* vertices, int* colors, SkPaint* paint) {
1543 // TODO: Do a quickReject
1544 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001545 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001546 }
1547
Romain Guya1d3c912011-12-13 14:55:06 -08001548 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001549 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001550 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001551 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001552
Romain Guyd21b6e12011-11-30 20:21:23 -08001553 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1554 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001555
1556 int alpha;
1557 SkXfermode::Mode mode;
1558 getAlphaAndMode(paint, &alpha, &mode);
1559
Romain Guy5a7b4662011-01-20 19:09:30 -08001560 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001561
Romain Guyb18d2d02011-02-10 15:52:54 -08001562 float left = FLT_MAX;
1563 float top = FLT_MAX;
1564 float right = FLT_MIN;
1565 float bottom = FLT_MIN;
1566
1567#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001568 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001569#else
Romain Guy211370f2012-02-01 16:10:55 -08001570 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001571#endif
1572
Romain Guya566b7c2011-01-23 16:36:11 -08001573 // TODO: Support the colors array
1574 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001575 TextureVertex* vertex = mesh;
1576 for (int32_t y = 0; y < meshHeight; y++) {
1577 for (int32_t x = 0; x < meshWidth; x++) {
1578 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1579
1580 float u1 = float(x) / meshWidth;
1581 float u2 = float(x + 1) / meshWidth;
1582 float v1 = float(y) / meshHeight;
1583 float v2 = float(y + 1) / meshHeight;
1584
1585 int ax = i + (meshWidth + 1) * 2;
1586 int ay = ax + 1;
1587 int bx = i;
1588 int by = bx + 1;
1589 int cx = i + 2;
1590 int cy = cx + 1;
1591 int dx = i + (meshWidth + 1) * 2 + 2;
1592 int dy = dx + 1;
1593
1594 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1595 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1596 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1597
1598 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1599 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1600 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001601
1602#if RENDER_LAYERS_AS_REGIONS
1603 if (hasActiveLayer) {
1604 // TODO: This could be optimized to avoid unnecessary ops
1605 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1606 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1607 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1608 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1609 }
1610#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001611 }
1612 }
1613
Romain Guyb18d2d02011-02-10 15:52:54 -08001614#if RENDER_LAYERS_AS_REGIONS
1615 if (hasActiveLayer) {
1616 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1617 }
1618#endif
1619
Romain Guy5a7b4662011-01-20 19:09:30 -08001620 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1621 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001622 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001623
1624 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001625}
1626
Chet Haase48659092012-05-31 15:21:51 -07001627status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001628 float srcLeft, float srcTop, float srcRight, float srcBottom,
1629 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001630 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001631 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001632 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001633 }
1634
Romain Guya1d3c912011-12-13 14:55:06 -08001635 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001636 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001637 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001638 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001639
Romain Guy8ba548f2010-06-30 19:21:21 -07001640 const float width = texture->width;
1641 const float height = texture->height;
1642
Romain Guy68169722011-08-22 17:33:33 -07001643 const float u1 = fmax(0.0f, srcLeft / width);
1644 const float v1 = fmax(0.0f, srcTop / height);
1645 const float u2 = fmin(1.0f, srcRight / width);
1646 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001647
Romain Guy03750a02010-10-18 14:06:08 -07001648 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001649 resetDrawTextureTexCoords(u1, v1, u2, v2);
1650
Romain Guy03750a02010-10-18 14:06:08 -07001651 int alpha;
1652 SkXfermode::Mode mode;
1653 getAlphaAndMode(paint, &alpha, &mode);
1654
Romain Guyd21b6e12011-11-30 20:21:23 -08001655 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1656
Romain Guy211370f2012-02-01 16:10:55 -08001657 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001658 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1659 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1660
Romain Guyb5014982011-07-28 15:39:12 -07001661 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001662 // Enable linear filtering if the source rectangle is scaled
1663 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001664 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001665 }
Romain Guyb5014982011-07-28 15:39:12 -07001666
Romain Guyd21b6e12011-11-30 20:21:23 -08001667 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001668 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1669 texture->id, alpha / 255.0f, mode, texture->blend,
1670 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1671 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1672 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001673 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001674 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1675 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1676 GL_TRIANGLE_STRIP, gMeshCount);
1677 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001678
1679 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001680
1681 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001682}
1683
Chet Haase48659092012-05-31 15:21:51 -07001684status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001685 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001686 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001687 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001688 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001689 }
1690
Romain Guya1d3c912011-12-13 14:55:06 -08001691 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001692 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001693 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001694 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001695 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1696 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001697
1698 int alpha;
1699 SkXfermode::Mode mode;
1700 getAlphaAndMode(paint, &alpha, &mode);
1701
Romain Guy2728f962010-10-08 18:36:15 -07001702 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001703 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001704
Romain Guy211370f2012-02-01 16:10:55 -08001705 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001706 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001707#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001708 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001709 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001710 const float offsetX = left + mSnapshot->transform->getTranslateX();
1711 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001712 const size_t count = mesh->quads.size();
1713 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001714 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001715 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001716 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1717 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1718 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001719 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001720 dirtyLayer(left + bounds.left, top + bounds.top,
1721 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001722 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001723 }
1724 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001725#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001726
Romain Guy211370f2012-02-01 16:10:55 -08001727 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001728 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1729 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1730
1731 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1732 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1733 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1734 true, !mesh->hasEmptyQuads);
1735 } else {
1736 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1737 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1738 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1739 true, !mesh->hasEmptyQuads);
1740 }
Romain Guy054dc182010-10-15 17:55:25 -07001741 }
Chet Haase48659092012-05-31 15:21:51 -07001742
1743 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001744}
1745
Chet Haase99ecdc42011-05-06 12:06:34 -07001746/**
Chet Haase858aa932011-05-12 09:06:00 -07001747 * This function uses a similar approach to that of AA lines in the drawLines() function.
1748 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1749 * shader to compute the translucency of the color, determined by whether a given pixel is
1750 * within that boundary region and how far into the region it is.
1751 */
1752void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001753 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001754 float inverseScaleX = 1.0f;
1755 float inverseScaleY = 1.0f;
1756 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001757 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001758 Matrix4 *mat = mSnapshot->transform;
1759 float m00 = mat->data[Matrix4::kScaleX];
1760 float m01 = mat->data[Matrix4::kSkewY];
1761 float m02 = mat->data[2];
1762 float m10 = mat->data[Matrix4::kSkewX];
1763 float m11 = mat->data[Matrix4::kScaleX];
1764 float m12 = mat->data[6];
1765 float scaleX = sqrt(m00 * m00 + m01 * m01);
1766 float scaleY = sqrt(m10 * m10 + m11 * m11);
1767 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1768 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1769 }
1770
1771 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001772 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001773 setupDrawAALine();
1774 setupDrawColor(color);
1775 setupDrawColorFilter();
1776 setupDrawShader();
1777 setupDrawBlending(true, mode);
1778 setupDrawProgram();
1779 setupDrawModelViewIdentity(true);
1780 setupDrawColorUniforms();
1781 setupDrawColorFilterUniforms();
1782 setupDrawShaderIdentityUniforms();
1783
1784 AAVertex rects[4];
1785 AAVertex* aaVertices = &rects[0];
1786 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1787 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1788
1789 float boundarySizeX = .5 * inverseScaleX;
1790 float boundarySizeY = .5 * inverseScaleY;
1791
1792 // Adjust the rect by the AA boundary padding
1793 left -= boundarySizeX;
1794 right += boundarySizeX;
1795 top -= boundarySizeY;
1796 bottom += boundarySizeY;
1797
1798 float width = right - left;
1799 float height = bottom - top;
1800
Romain Guy7b631422012-04-04 11:38:54 -07001801 int widthSlot;
1802 int lengthSlot;
1803
Chet Haase858aa932011-05-12 09:06:00 -07001804 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1805 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001806 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1807 boundaryWidthProportion, widthSlot, lengthSlot);
1808
Chet Haase858aa932011-05-12 09:06:00 -07001809 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1810 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1811 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001812 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion));
Chet Haase858aa932011-05-12 09:06:00 -07001813
1814 if (!quickReject(left, top, right, bottom)) {
1815 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1816 AAVertex::set(aaVertices++, left, top, 1, 0);
1817 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1818 AAVertex::set(aaVertices++, right, top, 0, 0);
1819 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1820 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1821 }
Romain Guy7b631422012-04-04 11:38:54 -07001822
1823 finishDrawAALine(widthSlot, lengthSlot);
Chet Haase858aa932011-05-12 09:06:00 -07001824}
1825
1826/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001827 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1828 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1829 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1830 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1831 * of the line. Hairlines are more involved because we need to account for transform scaling
1832 * to end up with a one-pixel-wide line in screen space..
1833 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1834 * in combination with values that we calculate and pass down in this method. The basic approach
1835 * is that the quad we create contains both the core line area plus a bounding area in which
1836 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1837 * proportion of the width and the length of a given segment is represented by the boundary
1838 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1839 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1840 * on the inside). This ends up giving the result we want, with pixels that are completely
1841 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1842 * how far into the boundary region they are, which is determined by shader interpolation.
1843 */
Chet Haase48659092012-05-31 15:21:51 -07001844status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1845 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001846
1847 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001848 // We use half the stroke width here because we're going to position the quad
1849 // corner vertices half of the width away from the line endpoints
1850 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001851 // A stroke width of 0 has a special meaning in Skia:
1852 // it draws a line 1 px wide regardless of current transform
1853 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001854
Chet Haase99ecdc42011-05-06 12:06:34 -07001855 float inverseScaleX = 1.0f;
1856 float inverseScaleY = 1.0f;
1857 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001858
Chet Haase8a5cc922011-04-26 07:28:09 -07001859 int alpha;
1860 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001861
Chet Haase8a5cc922011-04-26 07:28:09 -07001862 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001863 int verticesCount = count;
1864 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001865 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001866 verticesCount += (count - 4);
1867 }
1868
1869 if (isHairLine || isAA) {
1870 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1871 // the line on the screen should always be one pixel wide regardless of scale. For
1872 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001873 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001874 Matrix4 *mat = mSnapshot->transform;
1875 float m00 = mat->data[Matrix4::kScaleX];
1876 float m01 = mat->data[Matrix4::kSkewY];
1877 float m02 = mat->data[2];
1878 float m10 = mat->data[Matrix4::kSkewX];
1879 float m11 = mat->data[Matrix4::kScaleX];
1880 float m12 = mat->data[6];
Romain Guy7b631422012-04-04 11:38:54 -07001881
Romain Guy211370f2012-02-01 16:10:55 -08001882 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1883 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001884
Chet Haase99ecdc42011-05-06 12:06:34 -07001885 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1886 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001887
Chet Haase99ecdc42011-05-06 12:06:34 -07001888 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1889 scaled = true;
1890 }
1891 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001892 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001893
1894 getAlphaAndMode(paint, &alpha, &mode);
1895 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001896 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001897 if (isAA) {
1898 setupDrawAALine();
1899 }
1900 setupDrawColor(paint->getColor(), alpha);
1901 setupDrawColorFilter();
1902 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001903 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001904 setupDrawProgram();
1905 setupDrawModelViewIdentity(true);
1906 setupDrawColorUniforms();
1907 setupDrawColorFilterUniforms();
1908 setupDrawShaderIdentityUniforms();
1909
1910 if (isHairLine) {
1911 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001912 halfStrokeWidth = isAA? 1 : .5;
1913 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001914 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001915 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001916 }
Romain Guy7b631422012-04-04 11:38:54 -07001917
1918 int widthSlot;
1919 int lengthSlot;
1920
Chet Haase5b0200b2011-04-13 17:58:08 -07001921 Vertex lines[verticesCount];
1922 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001923
Chet Haase99585ad2011-05-02 15:00:16 -07001924 AAVertex wLines[verticesCount];
1925 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001926
Romain Guy211370f2012-02-01 16:10:55 -08001927 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001928 setupDrawVertices(vertices);
1929 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001930 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1931 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001932 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001933 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1934 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001935 // We will need to calculate the actual width proportion on each segment for
1936 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1937 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001938 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1939 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001940 }
1941
Chet Haase99ecdc42011-05-06 12:06:34 -07001942 AAVertex* prevAAVertex = NULL;
1943 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001944
Chet Haase99585ad2011-05-02 15:00:16 -07001945 int boundaryLengthSlot = -1;
1946 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001947 int boundaryWidthSlot = -1;
1948 int inverseBoundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001949
Chet Haase5b0200b2011-04-13 17:58:08 -07001950 for (int i = 0; i < count; i += 4) {
1951 // a = start point, b = end point
1952 vec2 a(points[i], points[i + 1]);
1953 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001954
Chet Haase99585ad2011-05-02 15:00:16 -07001955 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001956 float boundaryLengthProportion = 0;
1957 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001958
Chet Haase5b0200b2011-04-13 17:58:08 -07001959 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001960 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001961 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001962 if (isAA) {
1963 float wideningFactor;
1964 if (fabs(n.x) >= fabs(n.y)) {
1965 wideningFactor = fabs(1.0f / n.x);
1966 } else {
1967 wideningFactor = fabs(1.0f / n.y);
1968 }
1969 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001970 }
Romain Guy7b631422012-04-04 11:38:54 -07001971
Chet Haase99ecdc42011-05-06 12:06:34 -07001972 if (scaled) {
1973 n.x *= inverseScaleX;
1974 n.y *= inverseScaleY;
1975 }
1976 } else if (scaled) {
1977 // Extend n by .5 pixel on each side, post-transform
1978 vec2 extendedN = n.copyNormalized();
1979 extendedN /= 2;
1980 extendedN.x *= inverseScaleX;
1981 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07001982
Chet Haase99ecdc42011-05-06 12:06:34 -07001983 float extendedNLength = extendedN.length();
1984 // We need to set this value on the shader prior to drawing
1985 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1986 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001987 }
Romain Guy7b631422012-04-04 11:38:54 -07001988
Chet Haase5b0200b2011-04-13 17:58:08 -07001989 float x = n.x;
1990 n.x = -n.y;
1991 n.y = x;
1992
Chet Haase99585ad2011-05-02 15:00:16 -07001993 // aa lines expand the endpoint vertices to encompass the AA boundary
1994 if (isAA) {
1995 vec2 abVector = (b - a);
1996 length = abVector.length();
1997 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07001998
Chet Haase99ecdc42011-05-06 12:06:34 -07001999 if (scaled) {
2000 abVector.x *= inverseScaleX;
2001 abVector.y *= inverseScaleY;
2002 float abLength = abVector.length();
2003 boundaryLengthProportion = abLength / (length + abLength);
2004 } else {
2005 boundaryLengthProportion = .5 / (length + 1);
2006 }
Romain Guy7b631422012-04-04 11:38:54 -07002007
Chet Haase99ecdc42011-05-06 12:06:34 -07002008 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002009 a -= abVector;
2010 b += abVector;
2011 }
2012
Chet Haase5b0200b2011-04-13 17:58:08 -07002013 // Four corners of the rectangle defining a thick line
2014 vec2 p1 = a - n;
2015 vec2 p2 = a + n;
2016 vec2 p3 = b + n;
2017 vec2 p4 = b - n;
2018
Chet Haase99585ad2011-05-02 15:00:16 -07002019
Chet Haase5b0200b2011-04-13 17:58:08 -07002020 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2021 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2022 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2023 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2024
2025 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002026 if (!isAA) {
2027 if (prevVertex != NULL) {
2028 // Issue two repeat vertices to create degenerate triangles to bridge
2029 // between the previous line and the new one. This is necessary because
2030 // we are creating a single triangle_strip which will contain
2031 // potentially discontinuous line segments.
2032 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2033 Vertex::set(vertices++, p1.x, p1.y);
2034 generatedVerticesCount += 2;
2035 }
Romain Guy7b631422012-04-04 11:38:54 -07002036
Chet Haase5b0200b2011-04-13 17:58:08 -07002037 Vertex::set(vertices++, p1.x, p1.y);
2038 Vertex::set(vertices++, p2.x, p2.y);
2039 Vertex::set(vertices++, p4.x, p4.y);
2040 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002041
Chet Haase5b0200b2011-04-13 17:58:08 -07002042 prevVertex = vertices - 1;
2043 generatedVerticesCount += 4;
2044 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002045 if (!isHairLine && scaled) {
2046 // Must set width proportions per-segment for scaled non-hairlines to use the
2047 // correct AA boundary dimensions
2048 if (boundaryWidthSlot < 0) {
2049 boundaryWidthSlot =
2050 mCaches.currentProgram->getUniform("boundaryWidth");
2051 inverseBoundaryWidthSlot =
2052 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
2053 }
Romain Guy7b631422012-04-04 11:38:54 -07002054
Chet Haase99ecdc42011-05-06 12:06:34 -07002055 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
2056 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
2057 }
Romain Guy7b631422012-04-04 11:38:54 -07002058
Chet Haase99585ad2011-05-02 15:00:16 -07002059 if (boundaryLengthSlot < 0) {
2060 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
2061 inverseBoundaryLengthSlot =
2062 mCaches.currentProgram->getUniform("inverseBoundaryLength");
2063 }
Romain Guy7b631422012-04-04 11:38:54 -07002064
Chet Haase99ecdc42011-05-06 12:06:34 -07002065 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
2066 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07002067
Chet Haase5b0200b2011-04-13 17:58:08 -07002068 if (prevAAVertex != NULL) {
2069 // Issue two repeat vertices to create degenerate triangles to bridge
2070 // between the previous line and the new one. This is necessary because
2071 // we are creating a single triangle_strip which will contain
2072 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002073 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2074 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2075 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002076 generatedVerticesCount += 2;
2077 }
Romain Guy7b631422012-04-04 11:38:54 -07002078
Chet Haase99585ad2011-05-02 15:00:16 -07002079 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2080 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2081 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2082 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002083
Chet Haase5b0200b2011-04-13 17:58:08 -07002084 prevAAVertex = aaVertices - 1;
2085 generatedVerticesCount += 4;
2086 }
Romain Guy7b631422012-04-04 11:38:54 -07002087
Chet Haase99585ad2011-05-02 15:00:16 -07002088 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2089 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2090 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002091 }
2092 }
Romain Guy7b631422012-04-04 11:38:54 -07002093
Chet Haase5b0200b2011-04-13 17:58:08 -07002094 if (generatedVerticesCount > 0) {
2095 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2096 }
Romain Guy7b631422012-04-04 11:38:54 -07002097
2098 if (isAA) {
2099 finishDrawAALine(widthSlot, lengthSlot);
2100 }
Chet Haase48659092012-05-31 15:21:51 -07002101
2102 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002103}
2104
Chet Haase48659092012-05-31 15:21:51 -07002105status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2106 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002107
2108 // TODO: The paint's cap style defines whether the points are square or circular
2109 // TODO: Handle AA for round points
2110
Chet Haase5b0200b2011-04-13 17:58:08 -07002111 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002112 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002113 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002114 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002115 if (isHairLine) {
2116 // Now that we know it's hairline, we can set the effective width, to be used later
2117 strokeWidth = 1.0f;
2118 }
2119 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002120 int alpha;
2121 SkXfermode::Mode mode;
2122 getAlphaAndMode(paint, &alpha, &mode);
2123
2124 int verticesCount = count >> 1;
2125 int generatedVerticesCount = 0;
2126
2127 TextureVertex pointsData[verticesCount];
2128 TextureVertex* vertex = &pointsData[0];
2129
2130 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002131 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002132 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002133 setupDrawColor(paint->getColor(), alpha);
2134 setupDrawColorFilter();
2135 setupDrawShader();
2136 setupDrawBlending(mode);
2137 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002138 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002139 setupDrawColorUniforms();
2140 setupDrawColorFilterUniforms();
2141 setupDrawPointUniforms();
2142 setupDrawShaderIdentityUniforms();
2143 setupDrawMesh(vertex);
2144
2145 for (int i = 0; i < count; i += 2) {
2146 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2147 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002148
Chet Haase8a5cc922011-04-26 07:28:09 -07002149 float left = points[i] - halfWidth;
2150 float right = points[i] + halfWidth;
2151 float top = points[i + 1] - halfWidth;
2152 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002153
Chet Haase8a5cc922011-04-26 07:28:09 -07002154 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002155 }
2156
2157 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002158
2159 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002160}
2161
Chet Haase48659092012-05-31 15:21:51 -07002162status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002163 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002164 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002165
Romain Guyae88e5e2010-10-22 17:49:18 -07002166 Rect& clip(*mSnapshot->clipRect);
2167 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002168
Romain Guy3d58c032010-07-14 16:34:53 -07002169 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002170
2171 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002172}
Romain Guy9d5316e2010-06-24 19:30:36 -07002173
Chet Haase48659092012-05-31 15:21:51 -07002174status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2175 SkPaint* paint) {
2176 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002177 const AutoTexture autoCleanup(texture);
2178
2179 const float x = left + texture->left - texture->offset;
2180 const float y = top + texture->top - texture->offset;
2181
2182 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002183
2184 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002185}
2186
Chet Haase48659092012-05-31 15:21:51 -07002187status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002188 float rx, float ry, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002189 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002190
Romain Guya1d3c912011-12-13 14:55:06 -08002191 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002192 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2193 right - left, bottom - top, rx, ry, paint);
Chet Haase48659092012-05-31 15:21:51 -07002194 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002195}
2196
Chet Haase48659092012-05-31 15:21:51 -07002197status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2198 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002199
Romain Guya1d3c912011-12-13 14:55:06 -08002200 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002201 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Chet Haase48659092012-05-31 15:21:51 -07002202 return drawShape(x - radius, y - radius, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002203}
Romain Guy01d58e42011-01-19 21:54:02 -08002204
Chet Haase48659092012-05-31 15:21:51 -07002205status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
2206 SkPaint* paint) {
2207 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002208
Romain Guya1d3c912011-12-13 14:55:06 -08002209 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002210 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002211 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002212}
2213
Chet Haase48659092012-05-31 15:21:51 -07002214status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002215 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002216 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002217
2218 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002219 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002220 }
2221
Romain Guya1d3c912011-12-13 14:55:06 -08002222 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002223 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2224 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002225 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002226}
2227
Chet Haase48659092012-05-31 15:21:51 -07002228status_t OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002229 SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002230 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002231
Romain Guya1d3c912011-12-13 14:55:06 -08002232 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002233 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002234 return drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002235}
2236
Chet Haase48659092012-05-31 15:21:51 -07002237status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002238 if (p->getStyle() != SkPaint::kFill_Style) {
Chet Haase48659092012-05-31 15:21:51 -07002239 return drawRectAsShape(left, top, right, bottom, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002240 }
2241
Romain Guy6926c722010-07-12 20:20:03 -07002242 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002243 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002244 }
2245
Romain Guy026c5e162010-06-28 17:12:22 -07002246 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002247 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002248 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2249 if (!isMode) {
2250 // Assume SRC_OVER
2251 mode = SkXfermode::kSrcOver_Mode;
2252 }
2253 } else {
2254 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002255 }
2256
Romain Guy026c5e162010-06-28 17:12:22 -07002257 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002258 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002259 drawAARect(left, top, right, bottom, color, mode);
2260 } else {
2261 drawColorRect(left, top, right, bottom, color, mode);
2262 }
Chet Haase48659092012-05-31 15:21:51 -07002263
2264 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002265}
Romain Guy9d5316e2010-06-24 19:30:36 -07002266
Chet Haase48659092012-05-31 15:21:51 -07002267status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002268 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002269 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2270 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002271 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002272 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002273
Romain Guy671d6cf2012-01-18 12:39:17 -08002274 // NOTE: Skia does not support perspective transform on drawPosText yet
2275 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002276 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002277 }
2278
2279 float x = 0.0f;
2280 float y = 0.0f;
2281 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2282 if (pureTranslate) {
2283 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2284 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2285 }
2286
2287 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2288 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2289 paint->getTextSize());
2290
2291 int alpha;
2292 SkXfermode::Mode mode;
2293 getAlphaAndMode(paint, &alpha, &mode);
2294
2295 // Pick the appropriate texture filtering
2296 bool linearFilter = mSnapshot->transform->changesBounds();
2297 if (pureTranslate && !linearFilter) {
2298 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2299 }
2300
2301 mCaches.activeTexture(0);
2302 setupDraw();
2303 setupDrawDirtyRegionsDisabled();
2304 setupDrawWithTexture(true);
2305 setupDrawAlpha8Color(paint->getColor(), alpha);
2306 setupDrawColorFilter();
2307 setupDrawShader();
2308 setupDrawBlending(true, mode);
2309 setupDrawProgram();
2310 setupDrawModelView(x, y, x, y, pureTranslate, true);
2311 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2312 setupDrawPureColorUniforms();
2313 setupDrawColorFilterUniforms();
2314 setupDrawShaderUniforms(pureTranslate);
2315
2316 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2317 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2318
2319#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002320 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002321#else
Romain Guy211370f2012-02-01 16:10:55 -08002322 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002323#endif
2324
2325 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2326 positions, hasActiveLayer ? &bounds : NULL)) {
2327#if RENDER_LAYERS_AS_REGIONS
2328 if (hasActiveLayer) {
2329 if (!pureTranslate) {
2330 mSnapshot->transform->mapRect(bounds);
2331 }
2332 dirtyLayerUnchecked(bounds, getRegion());
2333 }
2334#endif
2335 }
Chet Haase48659092012-05-31 15:21:51 -07002336
2337 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002338}
2339
Chet Haase48659092012-05-31 15:21:51 -07002340status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002341 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002342 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2343 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002344 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002345 }
2346
Chet Haasea1cff502012-02-21 13:43:44 -08002347 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002348 switch (paint->getTextAlign()) {
2349 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002350 x -= length / 2.0f;
2351 break;
2352 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002353 x -= length;
2354 break;
2355 default:
2356 break;
2357 }
2358
Romain Guycac5fd32011-12-01 20:08:50 -08002359 SkPaint::FontMetrics metrics;
2360 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002361 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002362 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002363 }
2364
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002365 const float oldX = x;
2366 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002367 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002368 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002369 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2370 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2371 }
2372
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002373#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002374 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002375#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002376
2377 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002378 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002379 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002380
Romain Guy86568192010-12-14 15:55:39 -08002381 int alpha;
2382 SkXfermode::Mode mode;
2383 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002384
Romain Guy211370f2012-02-01 16:10:55 -08002385 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002386 mCaches.activeTexture(0);
2387
Romain Guyb45c0c92010-08-26 20:35:23 -07002388 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002389 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2390 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002391 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002392
Romain Guy740bf2b2011-04-26 15:33:10 -07002393 const float sx = oldX - shadow->left + mShadowDx;
2394 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002395
Romain Guy86568192010-12-14 15:55:39 -08002396 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002397 int shadowColor = mShadowColor;
2398 if (mShader) {
2399 shadowColor = 0xffffffff;
2400 }
Romain Guy86568192010-12-14 15:55:39 -08002401
Romain Guy86568192010-12-14 15:55:39 -08002402 setupDraw();
2403 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002404 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2405 setupDrawColorFilter();
2406 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002407 setupDrawBlending(true, mode);
2408 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002409 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002410 setupDrawTexture(shadow->id);
2411 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002412 setupDrawColorFilterUniforms();
2413 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002414 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2415
Romain Guy0a417492010-08-16 20:26:20 -07002416 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002417 }
2418
Romain Guy6620c6d2010-12-06 18:07:02 -08002419 // Pick the appropriate texture filtering
2420 bool linearFilter = mSnapshot->transform->changesBounds();
2421 if (pureTranslate && !linearFilter) {
2422 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2423 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002424
Romain Guy16c88082012-06-11 16:03:47 -07002425 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002426 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002427 setupDraw();
2428 setupDrawDirtyRegionsDisabled();
2429 setupDrawWithTexture(true);
2430 setupDrawAlpha8Color(paint->getColor(), alpha);
2431 setupDrawColorFilter();
2432 setupDrawShader();
2433 setupDrawBlending(true, mode);
2434 setupDrawProgram();
2435 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002436 // See comment above; the font renderer must use texture unit 0
2437 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002438 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2439 setupDrawPureColorUniforms();
2440 setupDrawColorFilterUniforms();
2441 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002442
Romain Guy6620c6d2010-12-06 18:07:02 -08002443 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002444 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2445
2446#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002447 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002448#else
Romain Guy211370f2012-02-01 16:10:55 -08002449 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002450#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002451
Romain Guy6620c6d2010-12-06 18:07:02 -08002452 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002453 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002454#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002455 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002456 if (!pureTranslate) {
2457 mSnapshot->transform->mapRect(bounds);
2458 }
Romain Guyf219da52011-01-16 12:54:25 -08002459 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002460 }
2461#endif
2462 }
Romain Guy694b5192010-07-21 21:33:20 -07002463
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002464 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002465
2466 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002467}
2468
Chet Haase48659092012-05-31 15:21:51 -07002469status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002470 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002471 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2472 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002473 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002474 }
2475
Romain Guy03d58522012-02-24 17:54:07 -08002476 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2477 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2478 paint->getTextSize());
2479
2480 int alpha;
2481 SkXfermode::Mode mode;
2482 getAlphaAndMode(paint, &alpha, &mode);
2483
2484 mCaches.activeTexture(0);
2485 setupDraw();
2486 setupDrawDirtyRegionsDisabled();
2487 setupDrawWithTexture(true);
2488 setupDrawAlpha8Color(paint->getColor(), alpha);
2489 setupDrawColorFilter();
2490 setupDrawShader();
2491 setupDrawBlending(true, mode);
2492 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002493 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002494 setupDrawTexture(fontRenderer.getTexture(true));
2495 setupDrawPureColorUniforms();
2496 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002497 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002498
Romain Guy97771732012-02-28 18:17:02 -08002499 const Rect* clip = &mSnapshot->getLocalClip();
2500 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 -08002501
Romain Guy97771732012-02-28 18:17:02 -08002502#if RENDER_LAYERS_AS_REGIONS
2503 const bool hasActiveLayer = hasLayer();
2504#else
2505 const bool hasActiveLayer = false;
2506#endif
2507
2508 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2509 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2510#if RENDER_LAYERS_AS_REGIONS
2511 if (hasActiveLayer) {
2512 mSnapshot->transform->mapRect(bounds);
2513 dirtyLayerUnchecked(bounds, getRegion());
2514 }
2515#endif
2516 }
Chet Haase48659092012-05-31 15:21:51 -07002517
2518 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002519}
2520
Chet Haase48659092012-05-31 15:21:51 -07002521status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2522 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002523
Romain Guya1d3c912011-12-13 14:55:06 -08002524 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002525
Romain Guy33f6beb2012-02-16 19:24:51 -08002526 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002527 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002528 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002529 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002530
Romain Guy8b55f372010-08-18 17:10:07 -07002531 const float x = texture->left - texture->offset;
2532 const float y = texture->top - texture->offset;
2533
Romain Guy01d58e42011-01-19 21:54:02 -08002534 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002535
2536 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002537}
2538
Chet Haase48659092012-05-31 15:21:51 -07002539status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guyada830f2011-01-13 12:13:20 -08002540 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Chet Haase48659092012-05-31 15:21:51 -07002541 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002542 }
2543
Romain Guy2bf68f02012-03-02 13:37:47 -08002544 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2545 OpenGLRenderer* renderer = layer->renderer;
2546 Rect& dirty = layer->dirtyRect;
2547
2548 interrupt();
2549 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2550 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002551 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002552 renderer->finish();
2553 resume();
2554
2555 dirty.setEmpty();
2556 layer->deferredUpdateScheduled = false;
2557 layer->renderer = NULL;
2558 layer->displayList = NULL;
2559 }
2560
Romain Guya1d3c912011-12-13 14:55:06 -08002561 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002562
2563 int alpha;
2564 SkXfermode::Mode mode;
2565 getAlphaAndMode(paint, &alpha, &mode);
2566
Romain Guy9ace8f52011-07-07 20:50:11 -07002567 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002568
Romain Guyf219da52011-01-16 12:54:25 -08002569#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002570 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002571 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002572 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002573 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002574 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002575 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002576
Romain Guyc88e3572011-01-22 00:32:12 -08002577 setupDraw();
2578 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002579 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002580 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002581 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002582 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002583 setupDrawPureColorUniforms();
2584 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002585 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002586 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002587 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2588 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2589
Romain Guyd21b6e12011-11-30 20:21:23 -08002590 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002591 setupDrawModelViewTranslate(x, y,
2592 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2593 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002594 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002595 setupDrawModelViewTranslate(x, y,
2596 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2597 }
Romain Guyc88e3572011-01-22 00:32:12 -08002598 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002599
Romain Guyc88e3572011-01-22 00:32:12 -08002600 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2601 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002602
Romain Guyc88e3572011-01-22 00:32:12 -08002603 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002604
2605#if DEBUG_LAYERS_AS_REGIONS
2606 drawRegionRects(layer->region);
2607#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002608 }
Romain Guyf219da52011-01-16 12:54:25 -08002609 }
2610#else
Romain Guyada830f2011-01-13 12:13:20 -08002611 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2612 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002613#endif
Chet Haase48659092012-05-31 15:21:51 -07002614
2615 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002616}
2617
Romain Guy6926c722010-07-12 20:20:03 -07002618///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002619// Shaders
2620///////////////////////////////////////////////////////////////////////////////
2621
2622void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002623 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002624}
2625
Romain Guy06f96e22010-07-30 19:18:16 -07002626void OpenGLRenderer::setupShader(SkiaShader* shader) {
2627 mShader = shader;
2628 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002629 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002630 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002631}
2632
Romain Guyd27977d2010-07-14 19:18:51 -07002633///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002634// Color filters
2635///////////////////////////////////////////////////////////////////////////////
2636
2637void OpenGLRenderer::resetColorFilter() {
2638 mColorFilter = NULL;
2639}
2640
2641void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2642 mColorFilter = filter;
2643}
2644
2645///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002646// Drop shadow
2647///////////////////////////////////////////////////////////////////////////////
2648
2649void OpenGLRenderer::resetShadow() {
2650 mHasShadow = false;
2651}
2652
2653void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2654 mHasShadow = true;
2655 mShadowRadius = radius;
2656 mShadowDx = dx;
2657 mShadowDy = dy;
2658 mShadowColor = color;
2659}
2660
2661///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002662// Draw filters
2663///////////////////////////////////////////////////////////////////////////////
2664
2665void OpenGLRenderer::resetPaintFilter() {
2666 mHasDrawFilter = false;
2667}
2668
2669void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2670 mHasDrawFilter = true;
2671 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2672 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2673}
2674
2675SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002676 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002677
2678 uint32_t flags = paint->getFlags();
2679
2680 mFilteredPaint = *paint;
2681 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2682
2683 return &mFilteredPaint;
2684}
2685
2686///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002687// Drawing implementation
2688///////////////////////////////////////////////////////////////////////////////
2689
Romain Guy01d58e42011-01-19 21:54:02 -08002690void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2691 float x, float y, SkPaint* paint) {
2692 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2693 return;
2694 }
2695
2696 int alpha;
2697 SkXfermode::Mode mode;
2698 getAlphaAndMode(paint, &alpha, &mode);
2699
2700 setupDraw();
2701 setupDrawWithTexture(true);
2702 setupDrawAlpha8Color(paint->getColor(), alpha);
2703 setupDrawColorFilter();
2704 setupDrawShader();
2705 setupDrawBlending(true, mode);
2706 setupDrawProgram();
2707 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2708 setupDrawTexture(texture->id);
2709 setupDrawPureColorUniforms();
2710 setupDrawColorFilterUniforms();
2711 setupDrawShaderUniforms();
2712 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2713
2714 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2715
2716 finishDrawTexture();
2717}
2718
Romain Guyf607bdc2010-09-10 19:20:06 -07002719// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002720#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2721#define kStdUnderline_Offset (1.0f / 9.0f)
2722#define kStdUnderline_Thickness (1.0f / 18.0f)
2723
2724void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2725 float x, float y, SkPaint* paint) {
2726 // Handle underline and strike-through
2727 uint32_t flags = paint->getFlags();
2728 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002729 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002730 float underlineWidth = length;
2731 // If length is > 0.0f, we already measured the text for the text alignment
2732 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002733 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002734 }
2735
2736 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002737 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002738 case SkPaint::kCenter_Align:
2739 offsetX = underlineWidth * 0.5f;
2740 break;
2741 case SkPaint::kRight_Align:
2742 offsetX = underlineWidth;
2743 break;
2744 default:
2745 break;
2746 }
2747
Romain Guy211370f2012-02-01 16:10:55 -08002748 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002749 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002750 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002751
Romain Guye20ecbd2010-09-22 19:49:04 -07002752 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002753 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002754
Romain Guyf6834472011-01-23 13:32:12 -08002755 int linesCount = 0;
2756 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2757 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2758
2759 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002760 float points[pointsCount];
2761 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002762
2763 if (flags & SkPaint::kUnderlineText_Flag) {
2764 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002765 points[currentPoint++] = left;
2766 points[currentPoint++] = top;
2767 points[currentPoint++] = left + underlineWidth;
2768 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002769 }
2770
2771 if (flags & SkPaint::kStrikeThruText_Flag) {
2772 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002773 points[currentPoint++] = left;
2774 points[currentPoint++] = top;
2775 points[currentPoint++] = left + underlineWidth;
2776 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002777 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002778
Romain Guy726aeba2011-06-01 14:52:00 -07002779 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002780
Romain Guy726aeba2011-06-01 14:52:00 -07002781 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002782 }
2783 }
2784}
2785
Romain Guy026c5e162010-06-28 17:12:22 -07002786void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002787 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002788 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002789 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002790 color |= 0x00ffffff;
2791 }
2792
Romain Guy70ca14e2010-12-13 18:24:33 -08002793 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002794 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002795 setupDrawColor(color);
2796 setupDrawShader();
2797 setupDrawColorFilter();
2798 setupDrawBlending(mode);
2799 setupDrawProgram();
2800 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2801 setupDrawColorUniforms();
2802 setupDrawShaderUniforms(ignoreTransform);
2803 setupDrawColorFilterUniforms();
2804 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002805
Romain Guyc95c8d62010-09-17 15:31:32 -07002806 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2807}
2808
Romain Guy82ba8142010-07-09 13:25:56 -07002809void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002810 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002811 int alpha;
2812 SkXfermode::Mode mode;
2813 getAlphaAndMode(paint, &alpha, &mode);
2814
Romain Guyd21b6e12011-11-30 20:21:23 -08002815 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002816
Romain Guy211370f2012-02-01 16:10:55 -08002817 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002818 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2819 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2820
Romain Guyd21b6e12011-11-30 20:21:23 -08002821 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002822 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2823 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2824 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2825 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002826 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002827 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2828 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2829 GL_TRIANGLE_STRIP, gMeshCount);
2830 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002831}
2832
Romain Guybd6b79b2010-06-26 00:13:53 -07002833void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002834 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2835 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002836 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002837}
2838
2839void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002840 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002841 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002842 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002843
Romain Guy746b7402010-10-26 16:27:31 -07002844 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002845 setupDrawWithTexture();
2846 setupDrawColor(alpha, alpha, alpha, alpha);
2847 setupDrawColorFilter();
2848 setupDrawBlending(blend, mode, swapSrcDst);
2849 setupDrawProgram();
2850 if (!dirty) {
2851 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002852 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002853 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002854 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002855 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002856 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002857 }
Romain Guy86568192010-12-14 15:55:39 -08002858 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002859 setupDrawColorFilterUniforms();
2860 setupDrawTexture(texture);
2861 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002862
Romain Guy6820ac82010-09-15 18:11:50 -07002863 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002864
2865 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002866}
2867
Romain Guya5aed0d2010-09-09 14:42:43 -07002868void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002869 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002870 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002871
Romain Guy82ba8142010-07-09 13:25:56 -07002872 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002873 // These blend modes are not supported by OpenGL directly and have
2874 // to be implemented using shaders. Since the shader will perform
2875 // the blending, turn blending off here
2876 // If the blend mode cannot be implemented using shaders, fall
2877 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002878 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2879 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002880 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002881 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002882
Romain Guy82bc7a72012-01-03 14:13:39 -08002883 if (mCaches.blend) {
2884 glDisable(GL_BLEND);
2885 mCaches.blend = false;
2886 }
2887
2888 return;
2889 } else {
2890 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002891 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002892 }
2893
2894 if (!mCaches.blend) {
2895 glEnable(GL_BLEND);
2896 }
2897
2898 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2899 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2900
2901 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2902 glBlendFunc(sourceMode, destMode);
2903 mCaches.lastSrcMode = sourceMode;
2904 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002905 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002906 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002907 glDisable(GL_BLEND);
2908 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002909 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002910}
2911
Romain Guy889f8d12010-07-29 14:37:42 -07002912bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002913 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002914 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002915 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002916 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002917 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002918 }
Romain Guy6926c722010-07-12 20:20:03 -07002919 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002920}
2921
Romain Guy026c5e162010-06-28 17:12:22 -07002922void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002923 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002924 TextureVertex::setUV(v++, u1, v1);
2925 TextureVertex::setUV(v++, u2, v1);
2926 TextureVertex::setUV(v++, u1, v2);
2927 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002928}
2929
Chet Haase5c13d892010-10-08 08:37:55 -07002930void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002931 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002932 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002933
2934 // Skia draws using the color's alpha channel if < 255
2935 // Otherwise, it uses the paint's alpha
2936 int color = paint->getColor();
2937 *alpha = (color >> 24) & 0xFF;
2938 if (*alpha == 255) {
2939 *alpha = paint->getAlpha();
2940 }
2941 } else {
2942 *mode = SkXfermode::kSrcOver_Mode;
2943 *alpha = 255;
2944 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07002945 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002946}
2947
Romain Guya5aed0d2010-09-09 14:42:43 -07002948SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002949 SkXfermode::Mode resultMode;
2950 if (!SkXfermode::AsMode(mode, &resultMode)) {
2951 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002952 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002953 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002954}
2955
Romain Guy9d5316e2010-06-24 19:30:36 -07002956}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002957}; // namespace android