blob: 766a1991d1f5d18d0f3df672b7fb4c4297065a19 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy03d58522012-02-24 17:54:07 -080024#include <SkPathMeasure.h>
Romain Guy694b5192010-07-21 21:33:20 -070025#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070028#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Romain Guy08aa2cb2011-03-17 11:06:57 -070030#include <private/hwui/DrawGlInfo.h>
31
Romain Guy5b3b3522010-10-27 18:57:51 -070032#include <ui/Rect.h>
33
Romain Guy85bf02f2010-06-22 13:11:24 -070034#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Romain Guya957eea2010-12-08 18:34:42 -080036#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070037
38namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070039namespace uirenderer {
40
41///////////////////////////////////////////////////////////////////////////////
42// Defines
43///////////////////////////////////////////////////////////////////////////////
44
Romain Guy759ea802010-09-16 20:49:46 -070045#define RAD_TO_DEG (180.0f / 3.14159265f)
46#define MIN_ANGLE 0.001f
47
Romain Guydbc26d22010-10-11 17:58:29 -070048// TODO: This should be set in properties
49#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
50
Romain Guyd21b6e12011-11-30 20:21:23 -080051#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
52
Romain Guy9d5316e2010-06-24 19:30:36 -070053///////////////////////////////////////////////////////////////////////////////
54// Globals
55///////////////////////////////////////////////////////////////////////////////
56
Romain Guy889f8d12010-07-29 14:37:42 -070057/**
58 * Structure mapping Skia xfermodes to OpenGL blending factors.
59 */
60struct Blender {
61 SkXfermode::Mode mode;
62 GLenum src;
63 GLenum dst;
64}; // struct Blender
65
Romain Guy026c5e162010-06-28 17:12:22 -070066// In this array, the index of each Blender equals the value of the first
67// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
68static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070069 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
70 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
71 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
72 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
73 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
74 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
75 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
76 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
80 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
82 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
83 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070084};
Romain Guye4d01122010-06-16 18:44:05 -070085
Romain Guy87a76572010-09-13 18:11:21 -070086// This array contains the swapped version of each SkXfermode. For instance
87// this array's SrcOver blending mode is actually DstOver. You can refer to
88// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070089static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070090 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
92 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
93 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
94 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
96 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
97 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
98 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
100 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
101 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
103 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
104 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700105};
106
Romain Guyf6a11b82010-06-23 17:47:49 -0700107///////////////////////////////////////////////////////////////////////////////
108// Constructors/destructor
109///////////////////////////////////////////////////////////////////////////////
110
Romain Guyfb8b7632010-08-23 21:05:08 -0700111OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700112 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700113 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700114 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800115 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700116
Romain Guyac670c02010-07-27 17:39:27 -0700117 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
118
Romain Guyae5575b2010-07-29 18:48:04 -0700119 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700120}
121
Romain Guy85bf02f2010-06-22 13:11:24 -0700122OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700123 // The context has already been destroyed at this point, do not call
124 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700125}
126
Romain Guyf6a11b82010-06-23 17:47:49 -0700127///////////////////////////////////////////////////////////////////////////////
Romain Guy13631f32012-01-30 17:41:55 -0800128// Debug
129///////////////////////////////////////////////////////////////////////////////
130
131void OpenGLRenderer::startMark(const char* name) const {
132 mCaches.startMark(0, name);
133}
134
135void OpenGLRenderer::endMark() const {
136 mCaches.endMark();
137}
138
139///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700140// Setup
141///////////////////////////////////////////////////////////////////////////////
142
Romain Guy530041d2012-01-25 18:56:29 -0800143uint32_t OpenGLRenderer::getStencilSize() {
144 return STENCIL_BUFFER_SIZE;
145}
146
Romain Guy49c5fc02012-05-15 11:10:01 -0700147bool OpenGLRenderer::isDeferred() {
148 return false;
149}
150
Romain Guy85bf02f2010-06-22 13:11:24 -0700151void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700152 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700153
154 mWidth = width;
155 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700156
157 mFirstSnapshot->height = height;
158 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700159
Romain Guy3e263fa2011-12-12 16:47:48 -0800160 glDisable(GL_DITHER);
Romain Guy39d252a2011-12-12 18:14:06 -0800161 glEnable(GL_SCISSOR_TEST);
Romain Guy3e263fa2011-12-12 16:47:48 -0800162 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800163
Romain Guy3e263fa2011-12-12 16:47:48 -0800164 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700165}
166
Romain Guy6b7bd242010-10-06 19:49:23 -0700167void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800168 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
169}
170
171void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800172 mCaches.clearGarbage();
173
Romain Guy8aef54f2010-09-01 15:13:49 -0700174 mSnapshot = new Snapshot(mFirstSnapshot,
175 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800176 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700177 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700178
Romain Guy39d252a2011-12-12 18:14:06 -0800179 glViewport(0, 0, mWidth, mHeight);
Romain Guy8f85e802011-12-14 19:23:32 -0800180 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy39d252a2011-12-12 18:14:06 -0800181
Romain Guy7d7b5492011-01-24 16:33:45 -0800182 mSnapshot->setClip(left, top, right, bottom);
Romain Guy39d252a2011-12-12 18:14:06 -0800183 mDirtyClip = false;
Romain Guy7d7b5492011-01-24 16:33:45 -0800184
Romain Guy6b7bd242010-10-06 19:49:23 -0700185 if (!opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700186 glClear(GL_COLOR_BUFFER_BIT);
187 }
Romain Guybb9524b2010-06-22 18:56:38 -0700188}
189
Romain Guyb025b9c2010-09-16 14:16:48 -0700190void OpenGLRenderer::finish() {
191#if DEBUG_OPENGL
192 GLenum status = GL_NO_ERROR;
193 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000194 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800195 switch (status) {
Romain Guya44a63a2012-04-26 14:05:02 -0700196 case GL_INVALID_ENUM:
197 ALOGE(" GL_INVALID_ENUM");
198 break;
199 case GL_INVALID_VALUE:
200 ALOGE(" GL_INVALID_VALUE");
201 break;
202 case GL_INVALID_OPERATION:
203 ALOGE(" GL_INVALID_OPERATION");
204 break;
Romain Guya07105b2011-01-10 21:14:18 -0800205 case GL_OUT_OF_MEMORY:
Romain Guya44a63a2012-04-26 14:05:02 -0700206 ALOGE(" Out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800207 break;
208 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700209 }
210#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800211#if DEBUG_MEMORY_USAGE
212 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800213#else
214 if (mCaches.getDebugLevel() & kDebugMemory) {
215 mCaches.dumpMemoryUsage();
216 }
Romain Guyc15008e2010-11-10 11:59:15 -0800217#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700218}
219
Romain Guy6c319ca2011-01-11 14:29:25 -0800220void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700221 if (mCaches.currentProgram) {
222 if (mCaches.currentProgram->isInUse()) {
223 mCaches.currentProgram->remove();
224 mCaches.currentProgram = NULL;
225 }
226 }
Romain Guy50c0f092010-10-19 11:42:22 -0700227 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800228 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800229 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800230 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700231}
232
Romain Guy6c319ca2011-01-11 14:29:25 -0800233void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800234 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
235
236 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800237 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
238
Romain Guyda8532c2010-08-31 11:50:35 -0700239 glEnable(GL_SCISSOR_TEST);
Romain Guy82bc7a72012-01-03 14:13:39 -0800240 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700241 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700242
Romain Guya1d3c912011-12-13 14:55:06 -0800243 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800244 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700245
Romain Guy50c0f092010-10-19 11:42:22 -0700246 mCaches.blend = true;
247 glEnable(GL_BLEND);
248 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
249 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700250}
251
Romain Guyba6be8a2012-04-23 18:22:09 -0700252void OpenGLRenderer::detachFunctor(Functor* functor) {
253 mFunctors.remove(functor);
254}
255
256void OpenGLRenderer::attachFunctor(Functor* functor) {
257 mFunctors.add(functor);
258}
259
Romain Guy8f3b8e32012-03-27 16:33:45 -0700260status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
261 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700262 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700263
Romain Guyba6be8a2012-04-23 18:22:09 -0700264 if (count > 0) {
265 SortedVector<Functor*> functors(mFunctors);
266 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700267
Romain Guyba6be8a2012-04-23 18:22:09 -0700268 DrawGlInfo info;
269 info.clipLeft = 0;
270 info.clipTop = 0;
271 info.clipRight = 0;
272 info.clipBottom = 0;
273 info.isLayer = false;
274 info.width = 0;
275 info.height = 0;
276 memset(info.transform, 0, sizeof(float) * 16);
277
278 for (size_t i = 0; i < count; i++) {
279 Functor* f = functors.itemAt(i);
280 result |= (*f)(DrawGlInfo::kModeProcess, &info);
281
Chris Craikc2c95432012-04-25 15:13:52 -0700282 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700283 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
284 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700285 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700286
Chris Craikc2c95432012-04-25 15:13:52 -0700287 if (result & DrawGlInfo::kStatusInvoke) {
288 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700289 }
290 }
291 }
292
Romain Guyc189ef52012-04-25 20:02:53 -0700293 // Restore state possibly changed by the functors in process mode
294 GLboolean value;
295 glGetBooleanv(GL_BLEND, &value);
296 mCaches.blend = value;
297
298 mCaches.activeTexture(0);
299
Romain Guy8f3b8e32012-03-27 16:33:45 -0700300 return result;
301}
302
303status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800304 interrupt();
Chris Craikc8538ade2012-05-22 11:54:06 -0700305 detachFunctor(functor);
306
Romain Guyf90f8172011-01-25 22:53:24 -0800307 if (mDirtyClip) {
308 setScissorFromClip();
309 }
Romain Guyd643bb52011-03-01 14:55:21 -0800310
Romain Guy80911b82011-03-16 15:30:12 -0700311 Rect clip(*mSnapshot->clipRect);
312 clip.snapToPixelBoundaries();
313
Romain Guyd643bb52011-03-01 14:55:21 -0800314#if RENDER_LAYERS_AS_REGIONS
315 // Since we don't know what the functor will draw, let's dirty
316 // tne entire clip region
317 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800318 dirtyLayerUnchecked(clip, getRegion());
319 }
320#endif
321
Romain Guy08aa2cb2011-03-17 11:06:57 -0700322 DrawGlInfo info;
323 info.clipLeft = clip.left;
324 info.clipTop = clip.top;
325 info.clipRight = clip.right;
326 info.clipBottom = clip.bottom;
327 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700328 info.width = getSnapshot()->viewport.getWidth();
329 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700330 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700331
Romain Guy8f3b8e32012-03-27 16:33:45 -0700332 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800333
Romain Guy8f3b8e32012-03-27 16:33:45 -0700334 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700335 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800336 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700337
Chris Craik65924a32012-04-05 17:52:11 -0700338 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700339 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700340 }
Romain Guycabfcc12011-03-07 18:06:46 -0800341 }
342
Chet Haasedaf98e92011-01-10 14:10:36 -0800343 resume();
Romain Guy65549432012-03-26 16:45:05 -0700344 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800345}
346
Romain Guyf6a11b82010-06-23 17:47:49 -0700347///////////////////////////////////////////////////////////////////////////////
348// State management
349///////////////////////////////////////////////////////////////////////////////
350
Romain Guybb9524b2010-06-22 18:56:38 -0700351int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700352 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700353}
354
355int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700356 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700357}
358
359void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700360 if (mSaveCount > 1) {
361 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700362 }
Romain Guybb9524b2010-06-22 18:56:38 -0700363}
364
365void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700366 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700367
Romain Guy8fb95422010-08-17 18:38:51 -0700368 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700369 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700370 }
Romain Guybb9524b2010-06-22 18:56:38 -0700371}
372
Romain Guy8aef54f2010-09-01 15:13:49 -0700373int OpenGLRenderer::saveSnapshot(int flags) {
374 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700375 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700376}
377
378bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700379 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700380 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700381 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700382
Romain Guybd6b79b2010-06-26 00:13:53 -0700383 sp<Snapshot> current = mSnapshot;
384 sp<Snapshot> previous = mSnapshot->previous;
385
Romain Guyeb993562010-10-05 18:14:38 -0700386 if (restoreOrtho) {
387 Rect& r = previous->viewport;
388 glViewport(r.left, r.top, r.right, r.bottom);
389 mOrthoMatrix.load(current->orthoMatrix);
390 }
391
Romain Guy8b55f372010-08-18 17:10:07 -0700392 mSaveCount--;
393 mSnapshot = previous;
394
Romain Guy2542d192010-08-18 11:47:12 -0700395 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700396 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700397 }
Romain Guy2542d192010-08-18 11:47:12 -0700398
Romain Guy5ec99242010-11-03 16:19:08 -0700399 if (restoreLayer) {
400 composeLayer(current, previous);
401 }
402
Romain Guy2542d192010-08-18 11:47:12 -0700403 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700404}
405
Romain Guyf6a11b82010-06-23 17:47:49 -0700406///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700407// Layers
408///////////////////////////////////////////////////////////////////////////////
409
410int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700411 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700412 const GLuint previousFbo = mSnapshot->fbo;
413 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700414
Romain Guyaf636eb2010-12-09 17:47:21 -0800415 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700416 int alpha = 255;
417 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700418
Romain Guye45362c2010-11-03 19:58:32 -0700419 if (p) {
420 alpha = p->getAlpha();
421 if (!mCaches.extensions.hasFramebufferFetch()) {
422 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
423 if (!isMode) {
424 // Assume SRC_OVER
425 mode = SkXfermode::kSrcOver_Mode;
426 }
427 } else {
428 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700429 }
430 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700431 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700432 }
Romain Guyd55a8612010-06-28 17:42:46 -0700433
Romain Guydbc26d22010-10-11 17:58:29 -0700434 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
435 }
Romain Guyd55a8612010-06-28 17:42:46 -0700436
437 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700438}
439
440int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
441 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700442 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700443 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700444 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700445 SkPaint paint;
446 paint.setAlpha(alpha);
447 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700448 }
Romain Guyd55a8612010-06-28 17:42:46 -0700449}
Romain Guybd6b79b2010-06-26 00:13:53 -0700450
Romain Guy1c740bc2010-09-13 18:00:09 -0700451/**
452 * Layers are viewed by Skia are slightly different than layers in image editing
453 * programs (for instance.) When a layer is created, previously created layers
454 * and the frame buffer still receive every drawing command. For instance, if a
455 * layer is created and a shape intersecting the bounds of the layers and the
456 * framebuffer is draw, the shape will be drawn on both (unless the layer was
457 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
458 *
459 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
460 * texture. Unfortunately, this is inefficient as it requires every primitive to
461 * be drawn n + 1 times, where n is the number of active layers. In practice this
462 * means, for every primitive:
463 * - Switch active frame buffer
464 * - Change viewport, clip and projection matrix
465 * - Issue the drawing
466 *
467 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700468 * To avoid this, layers are implemented in a different way here, at least in the
469 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
470 * is set. When this flag is set we can redirect all drawing operations into a
471 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700472 *
473 * This implementation relies on the frame buffer being at least RGBA 8888. When
474 * a layer is created, only a texture is created, not an FBO. The content of the
475 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700476 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700477 * buffer and drawing continues as normal. This technique therefore treats the
478 * frame buffer as a scratch buffer for the layers.
479 *
480 * To compose the layers back onto the frame buffer, each layer texture
481 * (containing the original frame buffer data) is drawn as a simple quad over
482 * the frame buffer. The trick is that the quad is set as the composition
483 * destination in the blending equation, and the frame buffer becomes the source
484 * of the composition.
485 *
486 * Drawing layers with an alpha value requires an extra step before composition.
487 * An empty quad is drawn over the layer's region in the frame buffer. This quad
488 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
489 * quad is used to multiply the colors in the frame buffer. This is achieved by
490 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
491 * GL_ZERO, GL_SRC_ALPHA.
492 *
493 * Because glCopyTexImage2D() can be slow, an alternative implementation might
494 * be use to draw a single clipped layer. The implementation described above
495 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700496 *
497 * (1) The frame buffer is actually not cleared right away. To allow the GPU
498 * to potentially optimize series of calls to glCopyTexImage2D, the frame
499 * buffer is left untouched until the first drawing operation. Only when
500 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700501 */
Romain Guyd55a8612010-06-28 17:42:46 -0700502bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700503 float right, float bottom, int alpha, SkXfermode::Mode mode,
504 int flags, GLuint previousFbo) {
505 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700506 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700507
Romain Guyeb993562010-10-05 18:14:38 -0700508 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
509
Romain Guyf607bdc2010-09-10 19:20:06 -0700510 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700511 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700512 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700513 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700514
Romain Guyeb993562010-10-05 18:14:38 -0700515 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700516 if (bounds.intersect(*snapshot->clipRect)) {
517 // We cannot work with sub-pixels in this case
518 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700519
Romain Guyad37cd32011-03-15 11:12:25 -0700520 // When the layer is not an FBO, we may use glCopyTexImage so we
521 // need to make sure the layer does not extend outside the bounds
522 // of the framebuffer
523 if (!bounds.intersect(snapshot->previous->viewport)) {
524 bounds.setEmpty();
525 }
526 } else {
527 bounds.setEmpty();
528 }
Romain Guyeb993562010-10-05 18:14:38 -0700529 }
Romain Guybf434112010-09-16 14:40:17 -0700530
Romain Guy746b7402010-10-26 16:27:31 -0700531 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
532 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800533 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700534 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700535 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700536 }
537
538 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800539 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700540 return false;
541 }
Romain Guyf18fd992010-07-08 11:45:51 -0700542
Romain Guya1d3c912011-12-13 14:55:06 -0800543 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700544 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700545 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700546 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700547 }
548
Romain Guy9ace8f52011-07-07 20:50:11 -0700549 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700550 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700551 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
552 bounds.getWidth() / float(layer->getWidth()), 0.0f);
553 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700554 layer->setBlend(true);
Romain Guydda570202010-07-06 11:39:32 -0700555
Romain Guy8fb95422010-08-17 18:38:51 -0700556 // Save the layer in the snapshot
557 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700558 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700559
Romain Guyeb993562010-10-05 18:14:38 -0700560 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700561 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700562 } else {
563 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700564 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800565 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700566 if (layer->isEmpty()) {
567 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
568 bounds.left, snapshot->height - bounds.bottom,
569 layer->getWidth(), layer->getHeight(), 0);
570 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800571 } else {
572 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
573 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
574 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700575
Romain Guy54be1cd2011-06-13 19:04:27 -0700576 // Enqueue the buffer coordinates to clear the corresponding region later
577 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700578 }
Romain Guyeb993562010-10-05 18:14:38 -0700579 }
Romain Guyf86ef572010-07-01 11:05:42 -0700580
Romain Guyd55a8612010-06-28 17:42:46 -0700581 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700582}
583
Romain Guy5b3b3522010-10-27 18:57:51 -0700584bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
585 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700586 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700587
588#if RENDER_LAYERS_AS_REGIONS
589 snapshot->region = &snapshot->layer->region;
590 snapshot->flags |= Snapshot::kFlagFboTarget;
591#endif
592
593 Rect clip(bounds);
594 snapshot->transform->mapRect(clip);
595 clip.intersect(*snapshot->clipRect);
596 clip.snapToPixelBoundaries();
597 clip.intersect(snapshot->previous->viewport);
598
599 mat4 inverse;
600 inverse.loadInverse(*mSnapshot->transform);
601
602 inverse.mapRect(clip);
603 clip.snapToPixelBoundaries();
604 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700605 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700606
607 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700608 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700609 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700610 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
611 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
612 snapshot->height = bounds.getHeight();
613 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
614 snapshot->orthoMatrix.load(mOrthoMatrix);
615
616 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700617 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
618 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700619
620 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700621 if (layer->isEmpty()) {
622 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
623 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700624 }
625
626 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700627 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700628
629#if DEBUG_LAYERS_AS_REGIONS
630 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
631 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000632 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700633
634 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700635 layer->deleteTexture();
636 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700637
638 delete layer;
639
640 return false;
641 }
642#endif
643
644 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800645 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700646 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700647 glClear(GL_COLOR_BUFFER_BIT);
648
649 dirtyClip();
650
651 // Change the ortho projection
652 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
653 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
654
655 return true;
656}
657
Romain Guy1c740bc2010-09-13 18:00:09 -0700658/**
659 * Read the documentation of createLayer() before doing anything in this method.
660 */
Romain Guy1d83e192010-08-17 11:37:00 -0700661void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
662 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000663 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700664 return;
665 }
666
Romain Guy5b3b3522010-10-27 18:57:51 -0700667 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700668
669 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700670 // Detach the texture from the FBO
671 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
672
Romain Guyeb993562010-10-05 18:14:38 -0700673 // Unbind current FBO and restore previous one
674 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
675 }
676
Romain Guy1d83e192010-08-17 11:37:00 -0700677 Layer* layer = current->layer;
678 const Rect& rect = layer->layer;
679
Romain Guy9ace8f52011-07-07 20:50:11 -0700680 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700681 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700682 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700683 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700684 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700685 }
686
Romain Guy03750a02010-10-18 14:06:08 -0700687 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700688
Romain Guya1d3c912011-12-13 14:55:06 -0800689 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700690
Romain Guy5b3b3522010-10-27 18:57:51 -0700691 // When the layer is stored in an FBO, we can save a bit of fillrate by
692 // drawing only the dirty region
693 if (fboLayer) {
694 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700695 if (layer->getColorFilter()) {
696 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800697 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700698 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700699 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800700 resetColorFilter();
701 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700702 } else if (!rect.isEmpty()) {
703 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
704 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700705 }
Romain Guy8b55f372010-08-18 17:10:07 -0700706
Romain Guyeb993562010-10-05 18:14:38 -0700707 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800708 // Note: No need to use glDiscardFramebufferEXT() since we never
709 // create/compose layers that are not on screen with this
710 // code path
711 // See LayerRenderer::destroyLayer(Layer*)
712
Romain Guyeb993562010-10-05 18:14:38 -0700713 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
714 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700715 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700716 }
717
Romain Guy746b7402010-10-26 16:27:31 -0700718 dirtyClip();
719
Romain Guyeb993562010-10-05 18:14:38 -0700720 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700721 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700722 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700723 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700724 delete layer;
725 }
726}
727
Romain Guyaa6c24c2011-04-28 18:40:04 -0700728void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700729 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700730
Romain Guy302a9df2011-08-16 13:55:02 -0700731 mat4& transform = layer->getTransform();
732 if (!transform.isIdentity()) {
733 save(0);
734 mSnapshot->transform->multiply(transform);
735 }
736
Romain Guyaa6c24c2011-04-28 18:40:04 -0700737 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700738 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700739 setupDrawWithTexture();
740 } else {
741 setupDrawWithExternalTexture();
742 }
743 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700744 setupDrawColor(alpha, alpha, alpha, alpha);
745 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700746 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700747 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700748 setupDrawPureColorUniforms();
749 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700750 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
751 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700752 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700753 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700754 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700755 if (mSnapshot->transform->isPureTranslate() &&
756 layer->getWidth() == (uint32_t) rect.getWidth() &&
757 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700758 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
759 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
760
Romain Guyd21b6e12011-11-30 20:21:23 -0800761 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700762 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
763 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800764 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700765 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
766 }
767 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700768 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
769
770 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
771
772 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700773
774 if (!transform.isIdentity()) {
775 restore();
776 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700777}
778
Romain Guy5b3b3522010-10-27 18:57:51 -0700779void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700780 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700781 const Rect& texCoords = layer->texCoords;
782 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
783 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700784
Romain Guy9ace8f52011-07-07 20:50:11 -0700785 float x = rect.left;
786 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700787 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700788 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700789 layer->getHeight() == (uint32_t) rect.getHeight();
790
791 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700792 // When we're swapping, the layer is already in screen coordinates
793 if (!swap) {
794 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
795 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
796 }
797
Romain Guyd21b6e12011-11-30 20:21:23 -0800798 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700799 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800800 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700801 }
802
803 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
804 layer->getTexture(), layer->getAlpha() / 255.0f,
805 layer->getMode(), layer->isBlend(),
806 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
807 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700808
Romain Guyaa6c24c2011-04-28 18:40:04 -0700809 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
810 } else {
811 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
812 drawTextureLayer(layer, rect);
813 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
814 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700815}
816
817void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
818#if RENDER_LAYERS_AS_REGIONS
819 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700820 layer->setRegionAsRect();
821
Romain Guy40667672011-03-18 14:34:03 -0700822 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700823
Romain Guy5b3b3522010-10-27 18:57:51 -0700824 layer->region.clear();
825 return;
826 }
827
Romain Guy8a3957d2011-09-07 17:55:15 -0700828 // TODO: See LayerRenderer.cpp::generateMesh() for important
829 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800830 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700831 size_t count;
832 const android::Rect* rects = layer->region.getArray(&count);
833
Romain Guy9ace8f52011-07-07 20:50:11 -0700834 const float alpha = layer->getAlpha() / 255.0f;
835 const float texX = 1.0f / float(layer->getWidth());
836 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800837 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700838
839 TextureVertex* mesh = mCaches.getRegionMesh();
840 GLsizei numQuads = 0;
841
Romain Guy7230a742011-01-10 22:26:16 -0800842 setupDraw();
843 setupDrawWithTexture();
844 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800845 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700846 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800847 setupDrawProgram();
848 setupDrawDirtyRegionsDisabled();
849 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800850 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700851 setupDrawTexture(layer->getTexture());
852 if (mSnapshot->transform->isPureTranslate()) {
853 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
854 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
855
Romain Guyd21b6e12011-11-30 20:21:23 -0800856 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700857 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
858 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800859 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700860 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
861 }
Romain Guy15bc6432011-12-13 13:11:32 -0800862 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700863
864 for (size_t i = 0; i < count; i++) {
865 const android::Rect* r = &rects[i];
866
867 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800868 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700869 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800870 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700871
872 // TODO: Reject quads outside of the clip
873 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
874 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
875 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
876 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
877
878 numQuads++;
879
880 if (numQuads >= REGION_MESH_QUAD_COUNT) {
881 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
882 numQuads = 0;
883 mesh = mCaches.getRegionMesh();
884 }
885 }
886
887 if (numQuads > 0) {
888 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
889 }
890
Romain Guy7230a742011-01-10 22:26:16 -0800891 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700892
893#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800894 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700895#endif
896
897 layer->region.clear();
898 }
899#else
900 composeLayerRect(layer, rect);
901#endif
902}
903
Romain Guy3a3133d2011-02-01 22:59:58 -0800904void OpenGLRenderer::drawRegionRects(const Region& region) {
905#if DEBUG_LAYERS_AS_REGIONS
906 size_t count;
907 const android::Rect* rects = region.getArray(&count);
908
909 uint32_t colors[] = {
910 0x7fff0000, 0x7f00ff00,
911 0x7f0000ff, 0x7fff00ff,
912 };
913
914 int offset = 0;
915 int32_t top = rects[0].top;
916
917 for (size_t i = 0; i < count; i++) {
918 if (top != rects[i].top) {
919 offset ^= 0x2;
920 top = rects[i].top;
921 }
922
923 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
924 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
925 SkXfermode::kSrcOver_Mode);
926 }
927#endif
928}
929
Romain Guy5b3b3522010-10-27 18:57:51 -0700930void OpenGLRenderer::dirtyLayer(const float left, const float top,
931 const float right, const float bottom, const mat4 transform) {
932#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800933 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700934 Rect bounds(left, top, right, bottom);
935 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800936 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700937 }
938#endif
939}
940
941void OpenGLRenderer::dirtyLayer(const float left, const float top,
942 const float right, const float bottom) {
943#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800944 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700945 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800946 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800947 }
948#endif
949}
950
951void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
952#if RENDER_LAYERS_AS_REGIONS
953 if (bounds.intersect(*mSnapshot->clipRect)) {
954 bounds.snapToPixelBoundaries();
955 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
956 if (!dirty.isEmpty()) {
957 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700958 }
959 }
960#endif
961}
962
Romain Guy54be1cd2011-06-13 19:04:27 -0700963void OpenGLRenderer::clearLayerRegions() {
964 const size_t count = mLayers.size();
965 if (count == 0) return;
966
967 if (!mSnapshot->isIgnored()) {
968 // Doing several glScissor/glClear here can negatively impact
969 // GPUs with a tiler architecture, instead we draw quads with
970 // the Clear blending mode
971
972 // The list contains bounds that have already been clipped
973 // against their initial clip rect, and the current clip
974 // is likely different so we need to disable clipping here
975 glDisable(GL_SCISSOR_TEST);
976
977 Vertex mesh[count * 6];
978 Vertex* vertex = mesh;
979
980 for (uint32_t i = 0; i < count; i++) {
981 Rect* bounds = mLayers.itemAt(i);
982
983 Vertex::set(vertex++, bounds->left, bounds->bottom);
984 Vertex::set(vertex++, bounds->left, bounds->top);
985 Vertex::set(vertex++, bounds->right, bounds->top);
986 Vertex::set(vertex++, bounds->left, bounds->bottom);
987 Vertex::set(vertex++, bounds->right, bounds->top);
988 Vertex::set(vertex++, bounds->right, bounds->bottom);
989
990 delete bounds;
991 }
992
993 setupDraw(false);
994 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
995 setupDrawBlending(true, SkXfermode::kClear_Mode);
996 setupDrawProgram();
997 setupDrawPureColorUniforms();
998 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800999 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001000
Romain Guy54be1cd2011-06-13 19:04:27 -07001001 glDrawArrays(GL_TRIANGLES, 0, count * 6);
1002
1003 glEnable(GL_SCISSOR_TEST);
1004 } else {
1005 for (uint32_t i = 0; i < count; i++) {
1006 delete mLayers.itemAt(i);
1007 }
1008 }
1009
1010 mLayers.clear();
1011}
1012
Romain Guybd6b79b2010-06-26 00:13:53 -07001013///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001014// Transforms
1015///////////////////////////////////////////////////////////////////////////////
1016
1017void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001018 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001019}
1020
1021void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001022 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001023}
1024
1025void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001026 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001027}
1028
Romain Guy807daf72011-01-18 11:19:19 -08001029void OpenGLRenderer::skew(float sx, float sy) {
1030 mSnapshot->transform->skew(sx, sy);
1031}
1032
Romain Guyf6a11b82010-06-23 17:47:49 -07001033void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001034 if (matrix) {
1035 mSnapshot->transform->load(*matrix);
1036 } else {
1037 mSnapshot->transform->loadIdentity();
1038 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001039}
1040
1041void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001042 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001043}
1044
1045void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001046 SkMatrix transform;
1047 mSnapshot->transform->copyTo(transform);
1048 transform.preConcat(*matrix);
1049 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001050}
1051
1052///////////////////////////////////////////////////////////////////////////////
1053// Clipping
1054///////////////////////////////////////////////////////////////////////////////
1055
Romain Guybb9524b2010-06-22 18:56:38 -07001056void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001057 Rect clip(*mSnapshot->clipRect);
1058 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001059
1060 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1061 clip.getWidth(), clip.getHeight());
1062
Romain Guy746b7402010-10-26 16:27:31 -07001063 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -07001064}
1065
1066const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001067 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001068}
1069
Romain Guyc7d53492010-06-25 13:41:57 -07001070bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001071 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001072 return true;
1073 }
1074
Romain Guy1d83e192010-08-17 11:37:00 -07001075 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001076 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001077 r.snapToPixelBoundaries();
1078
1079 Rect clipRect(*mSnapshot->clipRect);
1080 clipRect.snapToPixelBoundaries();
1081
1082 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001083}
1084
Romain Guy079ba2c2010-07-16 14:12:24 -07001085bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1086 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001087 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001088 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001089 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001090 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001091}
1092
Chet Haasea23eed82012-04-12 15:19:04 -07001093Rect* OpenGLRenderer::getClipRect() {
1094 return mSnapshot->clipRect;
1095}
1096
Romain Guyf6a11b82010-06-23 17:47:49 -07001097///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001098// Drawing commands
1099///////////////////////////////////////////////////////////////////////////////
1100
Romain Guy54be1cd2011-06-13 19:04:27 -07001101void OpenGLRenderer::setupDraw(bool clear) {
1102 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001103 if (mDirtyClip) {
1104 setScissorFromClip();
1105 }
1106 mDescription.reset();
1107 mSetShaderColor = false;
1108 mColorSet = false;
1109 mColorA = mColorR = mColorG = mColorB = 0.0f;
1110 mTextureUnit = 0;
1111 mTrackDirtyRegions = true;
1112}
1113
1114void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1115 mDescription.hasTexture = true;
1116 mDescription.hasAlpha8Texture = isAlpha8;
1117}
1118
Romain Guyaa6c24c2011-04-28 18:40:04 -07001119void OpenGLRenderer::setupDrawWithExternalTexture() {
1120 mDescription.hasExternalTexture = true;
1121}
1122
Romain Guy15bc6432011-12-13 13:11:32 -08001123void OpenGLRenderer::setupDrawNoTexture() {
1124 mCaches.disbaleTexCoordsVertexArray();
1125}
1126
Chet Haase5b0200b2011-04-13 17:58:08 -07001127void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001128 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001129}
1130
Romain Guyed6fcb02011-03-21 13:11:28 -07001131void OpenGLRenderer::setupDrawPoint(float pointSize) {
1132 mDescription.isPoint = true;
1133 mDescription.pointSize = pointSize;
1134}
1135
Romain Guy70ca14e2010-12-13 18:24:33 -08001136void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001137 setupDrawColor(color, (color >> 24) & 0xFF);
1138}
1139
1140void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1141 mColorA = alpha / 255.0f;
Chet Haasedb8c9a62012-03-21 18:54:18 -07001142 mColorA *= mSnapshot->alpha;
Chet Haase6cfdf452011-04-22 16:42:10 -07001143 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1144 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001145 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001146 mColorR = a * ((color >> 16) & 0xFF);
1147 mColorG = a * ((color >> 8) & 0xFF);
1148 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001149 mColorSet = true;
1150 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1151}
1152
Romain Guy86568192010-12-14 15:55:39 -08001153void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1154 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001155 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1156 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001157 const float a = mColorA / 255.0f;
1158 mColorR = a * ((color >> 16) & 0xFF);
1159 mColorG = a * ((color >> 8) & 0xFF);
1160 mColorB = a * ((color ) & 0xFF);
1161 mColorSet = true;
1162 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1163}
1164
Romain Guy70ca14e2010-12-13 18:24:33 -08001165void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1166 mColorA = a;
1167 mColorR = r;
1168 mColorG = g;
1169 mColorB = b;
1170 mColorSet = true;
1171 mSetShaderColor = mDescription.setColor(r, g, b, a);
1172}
1173
Romain Guy86568192010-12-14 15:55:39 -08001174void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1175 mColorA = a;
1176 mColorR = r;
1177 mColorG = g;
1178 mColorB = b;
1179 mColorSet = true;
1180 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1181}
1182
Romain Guy70ca14e2010-12-13 18:24:33 -08001183void OpenGLRenderer::setupDrawShader() {
1184 if (mShader) {
1185 mShader->describe(mDescription, mCaches.extensions);
1186 }
1187}
1188
1189void OpenGLRenderer::setupDrawColorFilter() {
1190 if (mColorFilter) {
1191 mColorFilter->describe(mDescription, mCaches.extensions);
1192 }
1193}
1194
Romain Guyf09ef512011-05-27 11:43:46 -07001195void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1196 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1197 mColorA = 1.0f;
1198 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001199 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001200 }
1201}
1202
Romain Guy70ca14e2010-12-13 18:24:33 -08001203void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001204 // When the blending mode is kClear_Mode, we need to use a modulate color
1205 // argb=1,0,0,0
1206 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001207 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1208 mDescription, swapSrcDst);
1209}
1210
1211void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001212 // When the blending mode is kClear_Mode, we need to use a modulate color
1213 // argb=1,0,0,0
1214 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001215 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1216 mDescription, swapSrcDst);
1217}
1218
1219void OpenGLRenderer::setupDrawProgram() {
1220 useProgram(mCaches.programCache.get(mDescription));
1221}
1222
1223void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1224 mTrackDirtyRegions = false;
1225}
1226
1227void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1228 bool ignoreTransform) {
1229 mModelView.loadTranslate(left, top, 0.0f);
1230 if (!ignoreTransform) {
1231 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1232 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1233 } else {
1234 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1235 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1236 }
1237}
1238
Chet Haase8a5cc922011-04-26 07:28:09 -07001239void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1240 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001241}
1242
Romain Guy70ca14e2010-12-13 18:24:33 -08001243void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1244 bool ignoreTransform, bool ignoreModelView) {
1245 if (!ignoreModelView) {
1246 mModelView.loadTranslate(left, top, 0.0f);
1247 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001248 } else {
1249 mModelView.loadIdentity();
1250 }
Romain Guy86568192010-12-14 15:55:39 -08001251 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1252 if (!ignoreTransform) {
1253 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1254 if (mTrackDirtyRegions && dirty) {
1255 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1256 }
1257 } else {
1258 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1259 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1260 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001261}
1262
Romain Guyed6fcb02011-03-21 13:11:28 -07001263void OpenGLRenderer::setupDrawPointUniforms() {
1264 int slot = mCaches.currentProgram->getUniform("pointSize");
1265 glUniform1f(slot, mDescription.pointSize);
1266}
1267
Romain Guy70ca14e2010-12-13 18:24:33 -08001268void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001269 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001270 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1271 }
1272}
1273
Romain Guy86568192010-12-14 15:55:39 -08001274void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001275 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001276 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001277 }
1278}
1279
Romain Guy70ca14e2010-12-13 18:24:33 -08001280void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1281 if (mShader) {
1282 if (ignoreTransform) {
1283 mModelView.loadInverse(*mSnapshot->transform);
1284 }
1285 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1286 }
1287}
1288
Romain Guy8d0d4782010-12-14 20:13:35 -08001289void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1290 if (mShader) {
1291 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1292 }
1293}
1294
Romain Guy70ca14e2010-12-13 18:24:33 -08001295void OpenGLRenderer::setupDrawColorFilterUniforms() {
1296 if (mColorFilter) {
1297 mColorFilter->setupProgram(mCaches.currentProgram);
1298 }
1299}
1300
1301void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001302 bool force = mCaches.bindMeshBuffer();
1303 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001304 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001305}
1306
1307void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1308 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001309 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001310 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001311}
1312
Romain Guyaa6c24c2011-04-28 18:40:04 -07001313void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1314 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001315 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001316 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001317}
1318
Romain Guy8f0095c2011-05-02 17:24:22 -07001319void OpenGLRenderer::setupDrawTextureTransform() {
1320 mDescription.hasTextureTransform = true;
1321}
1322
1323void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001324 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1325 GL_FALSE, &transform.data[0]);
1326}
1327
Romain Guy70ca14e2010-12-13 18:24:33 -08001328void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001329 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001330 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001331 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001332 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001333 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001334 }
Romain Guyd71dd362011-12-12 19:03:35 -08001335
Romain Guyf3a910b42011-12-12 20:35:21 -08001336 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001337 if (mCaches.currentProgram->texCoords >= 0) {
1338 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1339 }
1340
1341 mCaches.unbindIndicesBuffer();
1342}
1343
1344void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1345 bool force = mCaches.unbindMeshBuffer();
1346 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1347 if (mCaches.currentProgram->texCoords >= 0) {
1348 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001349 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001350}
1351
Chet Haase5b0200b2011-04-13 17:58:08 -07001352void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001353 bool force = mCaches.unbindMeshBuffer();
1354 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1355 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001356 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001357}
1358
1359/**
1360 * Sets up the shader to draw an AA line. We draw AA lines with quads, where there is an
Chet Haase99585ad2011-05-02 15:00:16 -07001361 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1362 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1363 * attributes (one per vertex) are values from zero to one that tells the fragment
1364 * shader where the fragment is in relation to the line width/length overall; these values are
1365 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1366 * region of the line.
1367 * Note that we only pass down the width values in this setup function. The length coordinates
1368 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001369 */
Chet Haase99585ad2011-05-02 15:00:16 -07001370void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001371 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001372 bool force = mCaches.unbindMeshBuffer();
1373 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1374 vertices, gAAVertexStride);
1375 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001376 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001377
Romain Guy7b631422012-04-04 11:38:54 -07001378 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001379 glEnableVertexAttribArray(widthSlot);
1380 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001381
Romain Guy7b631422012-04-04 11:38:54 -07001382 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001383 glEnableVertexAttribArray(lengthSlot);
1384 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001385
Chet Haase5b0200b2011-04-13 17:58:08 -07001386 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001387 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001388
Chet Haase99585ad2011-05-02 15:00:16 -07001389 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001390 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Romain Guy7b631422012-04-04 11:38:54 -07001391 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion);
1392}
1393
1394void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1395 glDisableVertexAttribArray(widthSlot);
1396 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001397}
1398
Romain Guy70ca14e2010-12-13 18:24:33 -08001399void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001400}
1401
1402///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001403// Drawing
1404///////////////////////////////////////////////////////////////////////////////
1405
Chet Haase1271e2c2012-04-20 09:54:27 -07001406status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001407 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001408
Romain Guy0fe478e2010-11-08 12:08:41 -08001409 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1410 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001411 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001412 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001413 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001414
Romain Guy65549432012-03-26 16:45:05 -07001415 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001416}
1417
Chet Haaseed30fd82011-04-22 16:18:45 -07001418void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1419 if (displayList) {
1420 displayList->output(*this, level);
1421 }
1422}
1423
Romain Guya168d732011-03-18 16:50:13 -07001424void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1425 int alpha;
1426 SkXfermode::Mode mode;
1427 getAlphaAndMode(paint, &alpha, &mode);
1428
Romain Guya168d732011-03-18 16:50:13 -07001429 float x = left;
1430 float y = top;
1431
Romain Guye3c26852011-07-25 16:36:01 -07001432 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001433 bool ignoreTransform = false;
1434 if (mSnapshot->transform->isPureTranslate()) {
1435 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1436 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1437 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001438 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001439 } else {
1440 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001441 }
1442
1443 setupDraw();
1444 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001445 if (paint) {
1446 setupDrawAlpha8Color(paint->getColor(), alpha);
1447 }
Romain Guya168d732011-03-18 16:50:13 -07001448 setupDrawColorFilter();
1449 setupDrawShader();
1450 setupDrawBlending(true, mode);
1451 setupDrawProgram();
1452 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001453
Romain Guya168d732011-03-18 16:50:13 -07001454 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001455 texture->setWrap(GL_CLAMP_TO_EDGE);
1456 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001457
Romain Guya168d732011-03-18 16:50:13 -07001458 setupDrawPureColorUniforms();
1459 setupDrawColorFilterUniforms();
1460 setupDrawShaderUniforms();
1461 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1462
1463 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1464
1465 finishDrawTexture();
1466}
1467
Chet Haase5c13d892010-10-08 08:37:55 -07001468void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001469 const float right = left + bitmap->width();
1470 const float bottom = top + bitmap->height();
1471
1472 if (quickReject(left, top, right, bottom)) {
1473 return;
1474 }
1475
Romain Guya1d3c912011-12-13 14:55:06 -08001476 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001477 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001478 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001479 const AutoTexture autoCleanup(texture);
1480
Romain Guy211370f2012-02-01 16:10:55 -08001481 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001482 drawAlphaBitmap(texture, left, top, paint);
1483 } else {
1484 drawTextureRect(left, top, right, bottom, texture, paint);
1485 }
Romain Guyce0537b2010-06-29 21:05:21 -07001486}
1487
Chet Haase5c13d892010-10-08 08:37:55 -07001488void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001489 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1490 const mat4 transform(*matrix);
1491 transform.mapRect(r);
1492
Romain Guy6926c722010-07-12 20:20:03 -07001493 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1494 return;
1495 }
1496
Romain Guya1d3c912011-12-13 14:55:06 -08001497 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001498 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001499 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001500 const AutoTexture autoCleanup(texture);
1501
Romain Guy5b3b3522010-10-27 18:57:51 -07001502 // This could be done in a cheaper way, all we need is pass the matrix
1503 // to the vertex shader. The save/restore is a bit overkill.
1504 save(SkCanvas::kMatrix_SaveFlag);
1505 concatMatrix(matrix);
1506 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1507 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001508}
1509
Romain Guye651cc62012-05-14 19:44:40 -07001510void OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
1511 const float right = left + bitmap->width();
1512 const float bottom = top + bitmap->height();
1513
1514 if (quickReject(left, top, right, bottom)) {
1515 return;
1516 }
1517
1518 mCaches.activeTexture(0);
1519 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1520 const AutoTexture autoCleanup(texture);
1521
1522 drawTextureRect(left, top, right, bottom, texture, paint);
1523}
1524
Romain Guy5a7b4662011-01-20 19:09:30 -08001525void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1526 float* vertices, int* colors, SkPaint* paint) {
1527 // TODO: Do a quickReject
1528 if (!vertices || mSnapshot->isIgnored()) {
1529 return;
1530 }
1531
Romain Guya1d3c912011-12-13 14:55:06 -08001532 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001533 Texture* texture = mCaches.textureCache.get(bitmap);
1534 if (!texture) return;
1535 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001536
Romain Guyd21b6e12011-11-30 20:21:23 -08001537 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1538 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001539
1540 int alpha;
1541 SkXfermode::Mode mode;
1542 getAlphaAndMode(paint, &alpha, &mode);
1543
Romain Guy5a7b4662011-01-20 19:09:30 -08001544 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001545
Romain Guyb18d2d02011-02-10 15:52:54 -08001546 float left = FLT_MAX;
1547 float top = FLT_MAX;
1548 float right = FLT_MIN;
1549 float bottom = FLT_MIN;
1550
1551#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001552 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001553#else
Romain Guy211370f2012-02-01 16:10:55 -08001554 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001555#endif
1556
Romain Guya566b7c2011-01-23 16:36:11 -08001557 // TODO: Support the colors array
1558 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001559 TextureVertex* vertex = mesh;
1560 for (int32_t y = 0; y < meshHeight; y++) {
1561 for (int32_t x = 0; x < meshWidth; x++) {
1562 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1563
1564 float u1 = float(x) / meshWidth;
1565 float u2 = float(x + 1) / meshWidth;
1566 float v1 = float(y) / meshHeight;
1567 float v2 = float(y + 1) / meshHeight;
1568
1569 int ax = i + (meshWidth + 1) * 2;
1570 int ay = ax + 1;
1571 int bx = i;
1572 int by = bx + 1;
1573 int cx = i + 2;
1574 int cy = cx + 1;
1575 int dx = i + (meshWidth + 1) * 2 + 2;
1576 int dy = dx + 1;
1577
1578 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1579 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1580 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1581
1582 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1583 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1584 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001585
1586#if RENDER_LAYERS_AS_REGIONS
1587 if (hasActiveLayer) {
1588 // TODO: This could be optimized to avoid unnecessary ops
1589 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1590 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1591 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1592 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1593 }
1594#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001595 }
1596 }
1597
Romain Guyb18d2d02011-02-10 15:52:54 -08001598#if RENDER_LAYERS_AS_REGIONS
1599 if (hasActiveLayer) {
1600 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1601 }
1602#endif
1603
Romain Guy5a7b4662011-01-20 19:09:30 -08001604 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1605 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001606 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001607}
1608
Romain Guy8ba548f2010-06-30 19:21:21 -07001609void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1610 float srcLeft, float srcTop, float srcRight, float srcBottom,
1611 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001612 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001613 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1614 return;
1615 }
1616
Romain Guya1d3c912011-12-13 14:55:06 -08001617 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001618 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001619 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001620 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001621
Romain Guy8ba548f2010-06-30 19:21:21 -07001622 const float width = texture->width;
1623 const float height = texture->height;
1624
Romain Guy68169722011-08-22 17:33:33 -07001625 const float u1 = fmax(0.0f, srcLeft / width);
1626 const float v1 = fmax(0.0f, srcTop / height);
1627 const float u2 = fmin(1.0f, srcRight / width);
1628 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001629
Romain Guy03750a02010-10-18 14:06:08 -07001630 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001631 resetDrawTextureTexCoords(u1, v1, u2, v2);
1632
Romain Guy03750a02010-10-18 14:06:08 -07001633 int alpha;
1634 SkXfermode::Mode mode;
1635 getAlphaAndMode(paint, &alpha, &mode);
1636
Romain Guyd21b6e12011-11-30 20:21:23 -08001637 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1638
Romain Guy211370f2012-02-01 16:10:55 -08001639 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001640 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1641 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1642
Romain Guyb5014982011-07-28 15:39:12 -07001643 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001644 // Enable linear filtering if the source rectangle is scaled
1645 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001646 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001647 }
Romain Guyb5014982011-07-28 15:39:12 -07001648
Romain Guyd21b6e12011-11-30 20:21:23 -08001649 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001650 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1651 texture->id, alpha / 255.0f, mode, texture->blend,
1652 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1653 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1654 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001655 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001656 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1657 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1658 GL_TRIANGLE_STRIP, gMeshCount);
1659 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001660
1661 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1662}
1663
Romain Guy4aa90572010-09-26 18:40:37 -07001664void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001665 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001666 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001667 if (quickReject(left, top, right, bottom)) {
1668 return;
1669 }
1670
Romain Guya1d3c912011-12-13 14:55:06 -08001671 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001672 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001673 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001674 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001675 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1676 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001677
1678 int alpha;
1679 SkXfermode::Mode mode;
1680 getAlphaAndMode(paint, &alpha, &mode);
1681
Romain Guy2728f962010-10-08 18:36:15 -07001682 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001683 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001684
Romain Guy211370f2012-02-01 16:10:55 -08001685 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001686 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001687#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001688 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001689 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001690 const float offsetX = left + mSnapshot->transform->getTranslateX();
1691 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001692 const size_t count = mesh->quads.size();
1693 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001694 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001695 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001696 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1697 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1698 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001699 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001700 dirtyLayer(left + bounds.left, top + bounds.top,
1701 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001702 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001703 }
1704 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001705#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001706
Romain Guy211370f2012-02-01 16:10:55 -08001707 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001708 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1709 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1710
1711 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1712 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1713 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1714 true, !mesh->hasEmptyQuads);
1715 } else {
1716 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1717 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1718 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1719 true, !mesh->hasEmptyQuads);
1720 }
Romain Guy054dc182010-10-15 17:55:25 -07001721 }
Romain Guyf7f93552010-07-08 19:17:03 -07001722}
1723
Chet Haase99ecdc42011-05-06 12:06:34 -07001724/**
Chet Haase858aa932011-05-12 09:06:00 -07001725 * This function uses a similar approach to that of AA lines in the drawLines() function.
1726 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1727 * shader to compute the translucency of the color, determined by whether a given pixel is
1728 * within that boundary region and how far into the region it is.
1729 */
1730void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001731 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001732 float inverseScaleX = 1.0f;
1733 float inverseScaleY = 1.0f;
1734 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001735 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001736 Matrix4 *mat = mSnapshot->transform;
1737 float m00 = mat->data[Matrix4::kScaleX];
1738 float m01 = mat->data[Matrix4::kSkewY];
1739 float m02 = mat->data[2];
1740 float m10 = mat->data[Matrix4::kSkewX];
1741 float m11 = mat->data[Matrix4::kScaleX];
1742 float m12 = mat->data[6];
1743 float scaleX = sqrt(m00 * m00 + m01 * m01);
1744 float scaleY = sqrt(m10 * m10 + m11 * m11);
1745 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1746 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1747 }
1748
1749 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001750 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001751 setupDrawAALine();
1752 setupDrawColor(color);
1753 setupDrawColorFilter();
1754 setupDrawShader();
1755 setupDrawBlending(true, mode);
1756 setupDrawProgram();
1757 setupDrawModelViewIdentity(true);
1758 setupDrawColorUniforms();
1759 setupDrawColorFilterUniforms();
1760 setupDrawShaderIdentityUniforms();
1761
1762 AAVertex rects[4];
1763 AAVertex* aaVertices = &rects[0];
1764 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1765 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1766
1767 float boundarySizeX = .5 * inverseScaleX;
1768 float boundarySizeY = .5 * inverseScaleY;
1769
1770 // Adjust the rect by the AA boundary padding
1771 left -= boundarySizeX;
1772 right += boundarySizeX;
1773 top -= boundarySizeY;
1774 bottom += boundarySizeY;
1775
1776 float width = right - left;
1777 float height = bottom - top;
1778
Romain Guy7b631422012-04-04 11:38:54 -07001779 int widthSlot;
1780 int lengthSlot;
1781
Chet Haase858aa932011-05-12 09:06:00 -07001782 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1783 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001784 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1785 boundaryWidthProportion, widthSlot, lengthSlot);
1786
Chet Haase858aa932011-05-12 09:06:00 -07001787 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1788 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1789 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001790 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion));
Chet Haase858aa932011-05-12 09:06:00 -07001791
1792 if (!quickReject(left, top, right, bottom)) {
1793 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1794 AAVertex::set(aaVertices++, left, top, 1, 0);
1795 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1796 AAVertex::set(aaVertices++, right, top, 0, 0);
1797 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1798 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1799 }
Romain Guy7b631422012-04-04 11:38:54 -07001800
1801 finishDrawAALine(widthSlot, lengthSlot);
Chet Haase858aa932011-05-12 09:06:00 -07001802}
1803
1804/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001805 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1806 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1807 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1808 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1809 * of the line. Hairlines are more involved because we need to account for transform scaling
1810 * to end up with a one-pixel-wide line in screen space..
1811 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1812 * in combination with values that we calculate and pass down in this method. The basic approach
1813 * is that the quad we create contains both the core line area plus a bounding area in which
1814 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1815 * proportion of the width and the length of a given segment is represented by the boundary
1816 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1817 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1818 * on the inside). This ends up giving the result we want, with pixels that are completely
1819 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1820 * how far into the boundary region they are, which is determined by shader interpolation.
1821 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001822void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1823 if (mSnapshot->isIgnored()) return;
1824
1825 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001826 // We use half the stroke width here because we're going to position the quad
1827 // corner vertices half of the width away from the line endpoints
1828 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001829 // A stroke width of 0 has a special meaning in Skia:
1830 // it draws a line 1 px wide regardless of current transform
1831 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001832
Chet Haase99ecdc42011-05-06 12:06:34 -07001833 float inverseScaleX = 1.0f;
1834 float inverseScaleY = 1.0f;
1835 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001836
Chet Haase8a5cc922011-04-26 07:28:09 -07001837 int alpha;
1838 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001839
Chet Haase8a5cc922011-04-26 07:28:09 -07001840 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001841 int verticesCount = count;
1842 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001843 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001844 verticesCount += (count - 4);
1845 }
1846
1847 if (isHairLine || isAA) {
1848 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1849 // the line on the screen should always be one pixel wide regardless of scale. For
1850 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001851 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001852 Matrix4 *mat = mSnapshot->transform;
1853 float m00 = mat->data[Matrix4::kScaleX];
1854 float m01 = mat->data[Matrix4::kSkewY];
1855 float m02 = mat->data[2];
1856 float m10 = mat->data[Matrix4::kSkewX];
1857 float m11 = mat->data[Matrix4::kScaleX];
1858 float m12 = mat->data[6];
Romain Guy7b631422012-04-04 11:38:54 -07001859
Romain Guy211370f2012-02-01 16:10:55 -08001860 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1861 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001862
Chet Haase99ecdc42011-05-06 12:06:34 -07001863 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1864 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001865
Chet Haase99ecdc42011-05-06 12:06:34 -07001866 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1867 scaled = true;
1868 }
1869 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001870 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001871
1872 getAlphaAndMode(paint, &alpha, &mode);
1873 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001874 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001875 if (isAA) {
1876 setupDrawAALine();
1877 }
1878 setupDrawColor(paint->getColor(), alpha);
1879 setupDrawColorFilter();
1880 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001881 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001882 setupDrawProgram();
1883 setupDrawModelViewIdentity(true);
1884 setupDrawColorUniforms();
1885 setupDrawColorFilterUniforms();
1886 setupDrawShaderIdentityUniforms();
1887
1888 if (isHairLine) {
1889 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001890 halfStrokeWidth = isAA? 1 : .5;
1891 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001892 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001893 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001894 }
Romain Guy7b631422012-04-04 11:38:54 -07001895
1896 int widthSlot;
1897 int lengthSlot;
1898
Chet Haase5b0200b2011-04-13 17:58:08 -07001899 Vertex lines[verticesCount];
1900 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001901
Chet Haase99585ad2011-05-02 15:00:16 -07001902 AAVertex wLines[verticesCount];
1903 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001904
Romain Guy211370f2012-02-01 16:10:55 -08001905 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001906 setupDrawVertices(vertices);
1907 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001908 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1909 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001910 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001911 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1912 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001913 // We will need to calculate the actual width proportion on each segment for
1914 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1915 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001916 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1917 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001918 }
1919
Chet Haase99ecdc42011-05-06 12:06:34 -07001920 AAVertex* prevAAVertex = NULL;
1921 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001922
Chet Haase99585ad2011-05-02 15:00:16 -07001923 int boundaryLengthSlot = -1;
1924 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001925 int boundaryWidthSlot = -1;
1926 int inverseBoundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001927
Chet Haase5b0200b2011-04-13 17:58:08 -07001928 for (int i = 0; i < count; i += 4) {
1929 // a = start point, b = end point
1930 vec2 a(points[i], points[i + 1]);
1931 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001932
Chet Haase99585ad2011-05-02 15:00:16 -07001933 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001934 float boundaryLengthProportion = 0;
1935 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001936
Chet Haase5b0200b2011-04-13 17:58:08 -07001937 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001938 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001939 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001940 if (isAA) {
1941 float wideningFactor;
1942 if (fabs(n.x) >= fabs(n.y)) {
1943 wideningFactor = fabs(1.0f / n.x);
1944 } else {
1945 wideningFactor = fabs(1.0f / n.y);
1946 }
1947 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001948 }
Romain Guy7b631422012-04-04 11:38:54 -07001949
Chet Haase99ecdc42011-05-06 12:06:34 -07001950 if (scaled) {
1951 n.x *= inverseScaleX;
1952 n.y *= inverseScaleY;
1953 }
1954 } else if (scaled) {
1955 // Extend n by .5 pixel on each side, post-transform
1956 vec2 extendedN = n.copyNormalized();
1957 extendedN /= 2;
1958 extendedN.x *= inverseScaleX;
1959 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07001960
Chet Haase99ecdc42011-05-06 12:06:34 -07001961 float extendedNLength = extendedN.length();
1962 // We need to set this value on the shader prior to drawing
1963 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1964 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001965 }
Romain Guy7b631422012-04-04 11:38:54 -07001966
Chet Haase5b0200b2011-04-13 17:58:08 -07001967 float x = n.x;
1968 n.x = -n.y;
1969 n.y = x;
1970
Chet Haase99585ad2011-05-02 15:00:16 -07001971 // aa lines expand the endpoint vertices to encompass the AA boundary
1972 if (isAA) {
1973 vec2 abVector = (b - a);
1974 length = abVector.length();
1975 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07001976
Chet Haase99ecdc42011-05-06 12:06:34 -07001977 if (scaled) {
1978 abVector.x *= inverseScaleX;
1979 abVector.y *= inverseScaleY;
1980 float abLength = abVector.length();
1981 boundaryLengthProportion = abLength / (length + abLength);
1982 } else {
1983 boundaryLengthProportion = .5 / (length + 1);
1984 }
Romain Guy7b631422012-04-04 11:38:54 -07001985
Chet Haase99ecdc42011-05-06 12:06:34 -07001986 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001987 a -= abVector;
1988 b += abVector;
1989 }
1990
Chet Haase5b0200b2011-04-13 17:58:08 -07001991 // Four corners of the rectangle defining a thick line
1992 vec2 p1 = a - n;
1993 vec2 p2 = a + n;
1994 vec2 p3 = b + n;
1995 vec2 p4 = b - n;
1996
Chet Haase99585ad2011-05-02 15:00:16 -07001997
Chet Haase5b0200b2011-04-13 17:58:08 -07001998 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1999 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2000 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2001 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2002
2003 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002004 if (!isAA) {
2005 if (prevVertex != NULL) {
2006 // Issue two repeat vertices to create degenerate triangles to bridge
2007 // between the previous line and the new one. This is necessary because
2008 // we are creating a single triangle_strip which will contain
2009 // potentially discontinuous line segments.
2010 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2011 Vertex::set(vertices++, p1.x, p1.y);
2012 generatedVerticesCount += 2;
2013 }
Romain Guy7b631422012-04-04 11:38:54 -07002014
Chet Haase5b0200b2011-04-13 17:58:08 -07002015 Vertex::set(vertices++, p1.x, p1.y);
2016 Vertex::set(vertices++, p2.x, p2.y);
2017 Vertex::set(vertices++, p4.x, p4.y);
2018 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002019
Chet Haase5b0200b2011-04-13 17:58:08 -07002020 prevVertex = vertices - 1;
2021 generatedVerticesCount += 4;
2022 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002023 if (!isHairLine && scaled) {
2024 // Must set width proportions per-segment for scaled non-hairlines to use the
2025 // correct AA boundary dimensions
2026 if (boundaryWidthSlot < 0) {
2027 boundaryWidthSlot =
2028 mCaches.currentProgram->getUniform("boundaryWidth");
2029 inverseBoundaryWidthSlot =
2030 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
2031 }
Romain Guy7b631422012-04-04 11:38:54 -07002032
Chet Haase99ecdc42011-05-06 12:06:34 -07002033 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
2034 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
2035 }
Romain Guy7b631422012-04-04 11:38:54 -07002036
Chet Haase99585ad2011-05-02 15:00:16 -07002037 if (boundaryLengthSlot < 0) {
2038 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
2039 inverseBoundaryLengthSlot =
2040 mCaches.currentProgram->getUniform("inverseBoundaryLength");
2041 }
Romain Guy7b631422012-04-04 11:38:54 -07002042
Chet Haase99ecdc42011-05-06 12:06:34 -07002043 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
2044 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07002045
Chet Haase5b0200b2011-04-13 17:58:08 -07002046 if (prevAAVertex != NULL) {
2047 // Issue two repeat vertices to create degenerate triangles to bridge
2048 // between the previous line and the new one. This is necessary because
2049 // we are creating a single triangle_strip which will contain
2050 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002051 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2052 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2053 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002054 generatedVerticesCount += 2;
2055 }
Romain Guy7b631422012-04-04 11:38:54 -07002056
Chet Haase99585ad2011-05-02 15:00:16 -07002057 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2058 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2059 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2060 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002061
Chet Haase5b0200b2011-04-13 17:58:08 -07002062 prevAAVertex = aaVertices - 1;
2063 generatedVerticesCount += 4;
2064 }
Romain Guy7b631422012-04-04 11:38:54 -07002065
Chet Haase99585ad2011-05-02 15:00:16 -07002066 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2067 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2068 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002069 }
2070 }
Romain Guy7b631422012-04-04 11:38:54 -07002071
Chet Haase5b0200b2011-04-13 17:58:08 -07002072 if (generatedVerticesCount > 0) {
2073 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2074 }
Romain Guy7b631422012-04-04 11:38:54 -07002075
2076 if (isAA) {
2077 finishDrawAALine(widthSlot, lengthSlot);
2078 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002079}
2080
Romain Guyed6fcb02011-03-21 13:11:28 -07002081void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2082 if (mSnapshot->isIgnored()) return;
2083
2084 // TODO: The paint's cap style defines whether the points are square or circular
2085 // TODO: Handle AA for round points
2086
Chet Haase5b0200b2011-04-13 17:58:08 -07002087 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002088 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002089 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002090 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002091 if (isHairLine) {
2092 // Now that we know it's hairline, we can set the effective width, to be used later
2093 strokeWidth = 1.0f;
2094 }
2095 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002096 int alpha;
2097 SkXfermode::Mode mode;
2098 getAlphaAndMode(paint, &alpha, &mode);
2099
2100 int verticesCount = count >> 1;
2101 int generatedVerticesCount = 0;
2102
2103 TextureVertex pointsData[verticesCount];
2104 TextureVertex* vertex = &pointsData[0];
2105
2106 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002107 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002108 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002109 setupDrawColor(paint->getColor(), alpha);
2110 setupDrawColorFilter();
2111 setupDrawShader();
2112 setupDrawBlending(mode);
2113 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002114 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002115 setupDrawColorUniforms();
2116 setupDrawColorFilterUniforms();
2117 setupDrawPointUniforms();
2118 setupDrawShaderIdentityUniforms();
2119 setupDrawMesh(vertex);
2120
2121 for (int i = 0; i < count; i += 2) {
2122 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2123 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002124
Chet Haase8a5cc922011-04-26 07:28:09 -07002125 float left = points[i] - halfWidth;
2126 float right = points[i] + halfWidth;
2127 float top = points[i + 1] - halfWidth;
2128 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002129
Chet Haase8a5cc922011-04-26 07:28:09 -07002130 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002131 }
2132
2133 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
2134}
2135
Romain Guy85bf02f2010-06-22 13:11:24 -07002136void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002137 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08002138 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002139
Romain Guyae88e5e2010-10-22 17:49:18 -07002140 Rect& clip(*mSnapshot->clipRect);
2141 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002142
Romain Guy3d58c032010-07-14 16:34:53 -07002143 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07002144}
Romain Guy9d5316e2010-06-24 19:30:36 -07002145
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002146void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08002147 if (!texture) return;
2148 const AutoTexture autoCleanup(texture);
2149
2150 const float x = left + texture->left - texture->offset;
2151 const float y = top + texture->top - texture->offset;
2152
2153 drawPathTexture(texture, x, y, paint);
2154}
2155
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002156void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2157 float rx, float ry, SkPaint* paint) {
2158 if (mSnapshot->isIgnored()) return;
2159
Romain Guya1d3c912011-12-13 14:55:06 -08002160 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002161 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2162 right - left, bottom - top, rx, ry, paint);
2163 drawShape(left, top, texture, paint);
2164}
2165
Romain Guy01d58e42011-01-19 21:54:02 -08002166void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2167 if (mSnapshot->isIgnored()) return;
2168
Romain Guya1d3c912011-12-13 14:55:06 -08002169 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002170 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002171 drawShape(x - radius, y - radius, texture, paint);
2172}
Romain Guy01d58e42011-01-19 21:54:02 -08002173
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002174void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2175 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002176
Romain Guya1d3c912011-12-13 14:55:06 -08002177 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002178 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2179 drawShape(left, top, texture, paint);
2180}
2181
Romain Guy8b2f5262011-01-23 16:15:02 -08002182void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2183 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2184 if (mSnapshot->isIgnored()) return;
2185
2186 if (fabs(sweepAngle) >= 360.0f) {
2187 drawOval(left, top, right, bottom, paint);
2188 return;
2189 }
2190
Romain Guya1d3c912011-12-13 14:55:06 -08002191 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002192 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2193 startAngle, sweepAngle, useCenter, paint);
2194 drawShape(left, top, texture, paint);
2195}
2196
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002197void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2198 SkPaint* paint) {
2199 if (mSnapshot->isIgnored()) return;
2200
Romain Guya1d3c912011-12-13 14:55:06 -08002201 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002202 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2203 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002204}
2205
Chet Haase5c13d892010-10-08 08:37:55 -07002206void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002207 if (p->getStyle() != SkPaint::kFill_Style) {
2208 drawRectAsShape(left, top, right, bottom, p);
2209 return;
2210 }
2211
Romain Guy6926c722010-07-12 20:20:03 -07002212 if (quickReject(left, top, right, bottom)) {
2213 return;
2214 }
2215
Romain Guy026c5e162010-06-28 17:12:22 -07002216 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002217 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002218 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2219 if (!isMode) {
2220 // Assume SRC_OVER
2221 mode = SkXfermode::kSrcOver_Mode;
2222 }
2223 } else {
2224 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002225 }
2226
Romain Guy026c5e162010-06-28 17:12:22 -07002227 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002228 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002229 drawAARect(left, top, right, bottom, color, mode);
2230 } else {
2231 drawColorRect(left, top, right, bottom, color, mode);
2232 }
Romain Guyc7d53492010-06-25 13:41:57 -07002233}
Romain Guy9d5316e2010-06-24 19:30:36 -07002234
Romain Guyeb9a5362012-01-17 17:39:26 -08002235void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
2236 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002237 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2238 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guyeb9a5362012-01-17 17:39:26 -08002239 return;
2240 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002241
Romain Guy671d6cf2012-01-18 12:39:17 -08002242 // NOTE: Skia does not support perspective transform on drawPosText yet
2243 if (!mSnapshot->transform->isSimple()) {
2244 return;
2245 }
2246
2247 float x = 0.0f;
2248 float y = 0.0f;
2249 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2250 if (pureTranslate) {
2251 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2252 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2253 }
2254
2255 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2256 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2257 paint->getTextSize());
2258
2259 int alpha;
2260 SkXfermode::Mode mode;
2261 getAlphaAndMode(paint, &alpha, &mode);
2262
2263 // Pick the appropriate texture filtering
2264 bool linearFilter = mSnapshot->transform->changesBounds();
2265 if (pureTranslate && !linearFilter) {
2266 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2267 }
2268
2269 mCaches.activeTexture(0);
2270 setupDraw();
2271 setupDrawDirtyRegionsDisabled();
2272 setupDrawWithTexture(true);
2273 setupDrawAlpha8Color(paint->getColor(), alpha);
2274 setupDrawColorFilter();
2275 setupDrawShader();
2276 setupDrawBlending(true, mode);
2277 setupDrawProgram();
2278 setupDrawModelView(x, y, x, y, pureTranslate, true);
2279 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2280 setupDrawPureColorUniforms();
2281 setupDrawColorFilterUniforms();
2282 setupDrawShaderUniforms(pureTranslate);
2283
2284 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2285 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2286
2287#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002288 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002289#else
Romain Guy211370f2012-02-01 16:10:55 -08002290 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002291#endif
2292
2293 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2294 positions, hasActiveLayer ? &bounds : NULL)) {
2295#if RENDER_LAYERS_AS_REGIONS
2296 if (hasActiveLayer) {
2297 if (!pureTranslate) {
2298 mSnapshot->transform->mapRect(bounds);
2299 }
2300 dirtyLayerUnchecked(bounds, getRegion());
2301 }
2302#endif
2303 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002304}
2305
Romain Guye8e62a42010-07-23 18:55:21 -07002306void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002307 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002308 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2309 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07002310 return;
2311 }
2312
Chet Haasea1cff502012-02-21 13:43:44 -08002313 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002314 switch (paint->getTextAlign()) {
2315 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002316 x -= length / 2.0f;
2317 break;
2318 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002319 x -= length;
2320 break;
2321 default:
2322 break;
2323 }
2324
Romain Guycac5fd32011-12-01 20:08:50 -08002325 SkPaint::FontMetrics metrics;
2326 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002327 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Romain Guycac5fd32011-12-01 20:08:50 -08002328 return;
2329 }
2330
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002331 const float oldX = x;
2332 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002333 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002334 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002335 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2336 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2337 }
2338
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002339#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002340 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002341#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002342
2343 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002344 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002345 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002346
Romain Guy86568192010-12-14 15:55:39 -08002347 int alpha;
2348 SkXfermode::Mode mode;
2349 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002350
Romain Guy211370f2012-02-01 16:10:55 -08002351 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002352 mCaches.activeTexture(0);
2353
Romain Guyb45c0c92010-08-26 20:35:23 -07002354 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002355 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2356 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002357 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002358
Romain Guy740bf2b2011-04-26 15:33:10 -07002359 const float sx = oldX - shadow->left + mShadowDx;
2360 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002361
Romain Guy86568192010-12-14 15:55:39 -08002362 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002363 int shadowColor = mShadowColor;
2364 if (mShader) {
2365 shadowColor = 0xffffffff;
2366 }
Romain Guy86568192010-12-14 15:55:39 -08002367
Romain Guy86568192010-12-14 15:55:39 -08002368 setupDraw();
2369 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002370 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2371 setupDrawColorFilter();
2372 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002373 setupDrawBlending(true, mode);
2374 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002375 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002376 setupDrawTexture(shadow->id);
2377 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002378 setupDrawColorFilterUniforms();
2379 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002380 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2381
Romain Guy0a417492010-08-16 20:26:20 -07002382 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002383 }
2384
Romain Guy6620c6d2010-12-06 18:07:02 -08002385 // Pick the appropriate texture filtering
2386 bool linearFilter = mSnapshot->transform->changesBounds();
2387 if (pureTranslate && !linearFilter) {
2388 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2389 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002390
Romain Guya1d3c912011-12-13 14:55:06 -08002391 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002392 setupDraw();
2393 setupDrawDirtyRegionsDisabled();
2394 setupDrawWithTexture(true);
2395 setupDrawAlpha8Color(paint->getColor(), alpha);
2396 setupDrawColorFilter();
2397 setupDrawShader();
2398 setupDrawBlending(true, mode);
2399 setupDrawProgram();
2400 setupDrawModelView(x, y, x, y, pureTranslate, true);
2401 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2402 setupDrawPureColorUniforms();
2403 setupDrawColorFilterUniforms();
2404 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002405
Romain Guy6620c6d2010-12-06 18:07:02 -08002406 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002407 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2408
2409#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002410 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002411#else
Romain Guy211370f2012-02-01 16:10:55 -08002412 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002413#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002414
Romain Guy6620c6d2010-12-06 18:07:02 -08002415 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002416 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002417#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002418 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002419 if (!pureTranslate) {
2420 mSnapshot->transform->mapRect(bounds);
2421 }
Romain Guyf219da52011-01-16 12:54:25 -08002422 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002423 }
2424#endif
2425 }
Romain Guy694b5192010-07-21 21:33:20 -07002426
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002427 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002428}
2429
Romain Guy325740f2012-02-24 16:48:34 -08002430void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
2431 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002432 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2433 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
2434 return;
2435 }
2436
Romain Guy03d58522012-02-24 17:54:07 -08002437 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2438 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2439 paint->getTextSize());
2440
2441 int alpha;
2442 SkXfermode::Mode mode;
2443 getAlphaAndMode(paint, &alpha, &mode);
2444
2445 mCaches.activeTexture(0);
2446 setupDraw();
2447 setupDrawDirtyRegionsDisabled();
2448 setupDrawWithTexture(true);
2449 setupDrawAlpha8Color(paint->getColor(), alpha);
2450 setupDrawColorFilter();
2451 setupDrawShader();
2452 setupDrawBlending(true, mode);
2453 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002454 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002455 setupDrawTexture(fontRenderer.getTexture(true));
2456 setupDrawPureColorUniforms();
2457 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002458 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002459
Romain Guy97771732012-02-28 18:17:02 -08002460 const Rect* clip = &mSnapshot->getLocalClip();
2461 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 -08002462
Romain Guy97771732012-02-28 18:17:02 -08002463#if RENDER_LAYERS_AS_REGIONS
2464 const bool hasActiveLayer = hasLayer();
2465#else
2466 const bool hasActiveLayer = false;
2467#endif
2468
2469 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2470 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2471#if RENDER_LAYERS_AS_REGIONS
2472 if (hasActiveLayer) {
2473 mSnapshot->transform->mapRect(bounds);
2474 dirtyLayerUnchecked(bounds, getRegion());
2475 }
2476#endif
2477 }
Romain Guy325740f2012-02-24 16:48:34 -08002478}
2479
Romain Guy7fbcc042010-08-04 15:40:07 -07002480void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002481 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002482
Romain Guya1d3c912011-12-13 14:55:06 -08002483 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002484
Romain Guy33f6beb2012-02-16 19:24:51 -08002485 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002486 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002487 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002488 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002489
Romain Guy8b55f372010-08-18 17:10:07 -07002490 const float x = texture->left - texture->offset;
2491 const float y = texture->top - texture->offset;
2492
Romain Guy01d58e42011-01-19 21:54:02 -08002493 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002494}
2495
Romain Guyada830f2011-01-13 12:13:20 -08002496void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2497 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002498 return;
2499 }
2500
Romain Guy2bf68f02012-03-02 13:37:47 -08002501 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2502 OpenGLRenderer* renderer = layer->renderer;
2503 Rect& dirty = layer->dirtyRect;
2504
2505 interrupt();
2506 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2507 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002508 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002509 renderer->finish();
2510 resume();
2511
2512 dirty.setEmpty();
2513 layer->deferredUpdateScheduled = false;
2514 layer->renderer = NULL;
2515 layer->displayList = NULL;
2516 }
2517
Romain Guya1d3c912011-12-13 14:55:06 -08002518 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002519
2520 int alpha;
2521 SkXfermode::Mode mode;
2522 getAlphaAndMode(paint, &alpha, &mode);
2523
Romain Guy9ace8f52011-07-07 20:50:11 -07002524 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002525
Romain Guyf219da52011-01-16 12:54:25 -08002526#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002527 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002528 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002529 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002530 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002531 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002532 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002533
Romain Guyc88e3572011-01-22 00:32:12 -08002534 setupDraw();
2535 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002536 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002537 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002538 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002539 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002540 setupDrawPureColorUniforms();
2541 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002542 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002543 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002544 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2545 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2546
Romain Guyd21b6e12011-11-30 20:21:23 -08002547 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002548 setupDrawModelViewTranslate(x, y,
2549 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2550 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002551 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002552 setupDrawModelViewTranslate(x, y,
2553 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2554 }
Romain Guyc88e3572011-01-22 00:32:12 -08002555 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002556
Romain Guyc88e3572011-01-22 00:32:12 -08002557 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2558 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002559
Romain Guyc88e3572011-01-22 00:32:12 -08002560 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002561
2562#if DEBUG_LAYERS_AS_REGIONS
2563 drawRegionRects(layer->region);
2564#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002565 }
Romain Guyf219da52011-01-16 12:54:25 -08002566 }
2567#else
Romain Guyada830f2011-01-13 12:13:20 -08002568 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2569 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002570#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002571}
2572
Romain Guy6926c722010-07-12 20:20:03 -07002573///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002574// Shaders
2575///////////////////////////////////////////////////////////////////////////////
2576
2577void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002578 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002579}
2580
Romain Guy06f96e22010-07-30 19:18:16 -07002581void OpenGLRenderer::setupShader(SkiaShader* shader) {
2582 mShader = shader;
2583 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002584 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002585 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002586}
2587
Romain Guyd27977d2010-07-14 19:18:51 -07002588///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002589// Color filters
2590///////////////////////////////////////////////////////////////////////////////
2591
2592void OpenGLRenderer::resetColorFilter() {
2593 mColorFilter = NULL;
2594}
2595
2596void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2597 mColorFilter = filter;
2598}
2599
2600///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002601// Drop shadow
2602///////////////////////////////////////////////////////////////////////////////
2603
2604void OpenGLRenderer::resetShadow() {
2605 mHasShadow = false;
2606}
2607
2608void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2609 mHasShadow = true;
2610 mShadowRadius = radius;
2611 mShadowDx = dx;
2612 mShadowDy = dy;
2613 mShadowColor = color;
2614}
2615
2616///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002617// Draw filters
2618///////////////////////////////////////////////////////////////////////////////
2619
2620void OpenGLRenderer::resetPaintFilter() {
2621 mHasDrawFilter = false;
2622}
2623
2624void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2625 mHasDrawFilter = true;
2626 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2627 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2628}
2629
2630SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002631 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002632
2633 uint32_t flags = paint->getFlags();
2634
2635 mFilteredPaint = *paint;
2636 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2637
2638 return &mFilteredPaint;
2639}
2640
2641///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002642// Drawing implementation
2643///////////////////////////////////////////////////////////////////////////////
2644
Romain Guy01d58e42011-01-19 21:54:02 -08002645void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2646 float x, float y, SkPaint* paint) {
2647 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2648 return;
2649 }
2650
2651 int alpha;
2652 SkXfermode::Mode mode;
2653 getAlphaAndMode(paint, &alpha, &mode);
2654
2655 setupDraw();
2656 setupDrawWithTexture(true);
2657 setupDrawAlpha8Color(paint->getColor(), alpha);
2658 setupDrawColorFilter();
2659 setupDrawShader();
2660 setupDrawBlending(true, mode);
2661 setupDrawProgram();
2662 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2663 setupDrawTexture(texture->id);
2664 setupDrawPureColorUniforms();
2665 setupDrawColorFilterUniforms();
2666 setupDrawShaderUniforms();
2667 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2668
2669 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2670
2671 finishDrawTexture();
2672}
2673
Romain Guyf607bdc2010-09-10 19:20:06 -07002674// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002675#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2676#define kStdUnderline_Offset (1.0f / 9.0f)
2677#define kStdUnderline_Thickness (1.0f / 18.0f)
2678
2679void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2680 float x, float y, SkPaint* paint) {
2681 // Handle underline and strike-through
2682 uint32_t flags = paint->getFlags();
2683 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002684 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002685 float underlineWidth = length;
2686 // If length is > 0.0f, we already measured the text for the text alignment
2687 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002688 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002689 }
2690
2691 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002692 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002693 case SkPaint::kCenter_Align:
2694 offsetX = underlineWidth * 0.5f;
2695 break;
2696 case SkPaint::kRight_Align:
2697 offsetX = underlineWidth;
2698 break;
2699 default:
2700 break;
2701 }
2702
Romain Guy211370f2012-02-01 16:10:55 -08002703 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002704 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002705 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002706
Romain Guye20ecbd2010-09-22 19:49:04 -07002707 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002708 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002709
Romain Guyf6834472011-01-23 13:32:12 -08002710 int linesCount = 0;
2711 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2712 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2713
2714 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002715 float points[pointsCount];
2716 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002717
2718 if (flags & SkPaint::kUnderlineText_Flag) {
2719 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002720 points[currentPoint++] = left;
2721 points[currentPoint++] = top;
2722 points[currentPoint++] = left + underlineWidth;
2723 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002724 }
2725
2726 if (flags & SkPaint::kStrikeThruText_Flag) {
2727 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002728 points[currentPoint++] = left;
2729 points[currentPoint++] = top;
2730 points[currentPoint++] = left + underlineWidth;
2731 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002732 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002733
Romain Guy726aeba2011-06-01 14:52:00 -07002734 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002735
Romain Guy726aeba2011-06-01 14:52:00 -07002736 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002737 }
2738 }
2739}
2740
Romain Guy026c5e162010-06-28 17:12:22 -07002741void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002742 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002743 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002744 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002745 color |= 0x00ffffff;
2746 }
2747
Romain Guy70ca14e2010-12-13 18:24:33 -08002748 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002749 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002750 setupDrawColor(color);
2751 setupDrawShader();
2752 setupDrawColorFilter();
2753 setupDrawBlending(mode);
2754 setupDrawProgram();
2755 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2756 setupDrawColorUniforms();
2757 setupDrawShaderUniforms(ignoreTransform);
2758 setupDrawColorFilterUniforms();
2759 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002760
Romain Guyc95c8d62010-09-17 15:31:32 -07002761 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2762}
2763
Romain Guy82ba8142010-07-09 13:25:56 -07002764void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002765 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002766 int alpha;
2767 SkXfermode::Mode mode;
2768 getAlphaAndMode(paint, &alpha, &mode);
2769
Romain Guyd21b6e12011-11-30 20:21:23 -08002770 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002771
Romain Guy211370f2012-02-01 16:10:55 -08002772 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002773 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2774 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2775
Romain Guyd21b6e12011-11-30 20:21:23 -08002776 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002777 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2778 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2779 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2780 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002781 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002782 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2783 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2784 GL_TRIANGLE_STRIP, gMeshCount);
2785 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002786}
2787
Romain Guybd6b79b2010-06-26 00:13:53 -07002788void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002789 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2790 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002791 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002792}
2793
2794void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002795 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002796 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002797 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002798
Romain Guy746b7402010-10-26 16:27:31 -07002799 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002800 setupDrawWithTexture();
2801 setupDrawColor(alpha, alpha, alpha, alpha);
2802 setupDrawColorFilter();
2803 setupDrawBlending(blend, mode, swapSrcDst);
2804 setupDrawProgram();
2805 if (!dirty) {
2806 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002807 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002808 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002809 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002810 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002811 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002812 }
Romain Guy86568192010-12-14 15:55:39 -08002813 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002814 setupDrawColorFilterUniforms();
2815 setupDrawTexture(texture);
2816 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002817
Romain Guy6820ac82010-09-15 18:11:50 -07002818 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002819
2820 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002821}
2822
Romain Guya5aed0d2010-09-09 14:42:43 -07002823void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002824 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002825 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002826
Romain Guy82ba8142010-07-09 13:25:56 -07002827 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002828 // These blend modes are not supported by OpenGL directly and have
2829 // to be implemented using shaders. Since the shader will perform
2830 // the blending, turn blending off here
2831 // If the blend mode cannot be implemented using shaders, fall
2832 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002833 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2834 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002835 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002836 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002837
Romain Guy82bc7a72012-01-03 14:13:39 -08002838 if (mCaches.blend) {
2839 glDisable(GL_BLEND);
2840 mCaches.blend = false;
2841 }
2842
2843 return;
2844 } else {
2845 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002846 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002847 }
2848
2849 if (!mCaches.blend) {
2850 glEnable(GL_BLEND);
2851 }
2852
2853 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2854 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2855
2856 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2857 glBlendFunc(sourceMode, destMode);
2858 mCaches.lastSrcMode = sourceMode;
2859 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002860 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002861 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002862 glDisable(GL_BLEND);
2863 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002864 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002865}
2866
Romain Guy889f8d12010-07-29 14:37:42 -07002867bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002868 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002869 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002870 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002871 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002872 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002873 }
Romain Guy6926c722010-07-12 20:20:03 -07002874 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002875}
2876
Romain Guy026c5e162010-06-28 17:12:22 -07002877void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002878 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002879 TextureVertex::setUV(v++, u1, v1);
2880 TextureVertex::setUV(v++, u2, v1);
2881 TextureVertex::setUV(v++, u1, v2);
2882 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002883}
2884
Chet Haase5c13d892010-10-08 08:37:55 -07002885void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002886 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002887 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002888
2889 // Skia draws using the color's alpha channel if < 255
2890 // Otherwise, it uses the paint's alpha
2891 int color = paint->getColor();
2892 *alpha = (color >> 24) & 0xFF;
2893 if (*alpha == 255) {
2894 *alpha = paint->getAlpha();
2895 }
2896 } else {
2897 *mode = SkXfermode::kSrcOver_Mode;
2898 *alpha = 255;
2899 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07002900 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002901}
2902
Romain Guya5aed0d2010-09-09 14:42:43 -07002903SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002904 SkXfermode::Mode resultMode;
2905 if (!SkXfermode::AsMode(mode, &resultMode)) {
2906 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002907 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002908 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002909}
2910
Romain Guy9d5316e2010-06-24 19:30:36 -07002911}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002912}; // namespace android