blob: 55e962adde5cc05e553eb42776e00ef6064a14bc [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 Guy85bf02f2010-06-22 13:11:24 -0700147void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700148 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700149
150 mWidth = width;
151 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700152
153 mFirstSnapshot->height = height;
154 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700155
Romain Guy3e263fa2011-12-12 16:47:48 -0800156 glDisable(GL_DITHER);
Romain Guy39d252a2011-12-12 18:14:06 -0800157 glEnable(GL_SCISSOR_TEST);
Romain Guy3e263fa2011-12-12 16:47:48 -0800158 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800159
Romain Guy3e263fa2011-12-12 16:47:48 -0800160 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700161}
162
Romain Guy6b7bd242010-10-06 19:49:23 -0700163void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800164 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
165}
166
167void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800168 mCaches.clearGarbage();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700169 mFunctors.clear();
Romain Guyfe48f652010-11-11 15:36:56 -0800170
Romain Guy8aef54f2010-09-01 15:13:49 -0700171 mSnapshot = new Snapshot(mFirstSnapshot,
172 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800173 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700174 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700175
Romain Guy39d252a2011-12-12 18:14:06 -0800176 glViewport(0, 0, mWidth, mHeight);
Romain Guy8f85e802011-12-14 19:23:32 -0800177 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy39d252a2011-12-12 18:14:06 -0800178
Romain Guy7d7b5492011-01-24 16:33:45 -0800179 mSnapshot->setClip(left, top, right, bottom);
Romain Guy39d252a2011-12-12 18:14:06 -0800180 mDirtyClip = false;
Romain Guy7d7b5492011-01-24 16:33:45 -0800181
Romain Guy6b7bd242010-10-06 19:49:23 -0700182 if (!opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700183 glClear(GL_COLOR_BUFFER_BIT);
184 }
Romain Guybb9524b2010-06-22 18:56:38 -0700185}
186
Romain Guyb025b9c2010-09-16 14:16:48 -0700187void OpenGLRenderer::finish() {
188#if DEBUG_OPENGL
189 GLenum status = GL_NO_ERROR;
190 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000191 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800192 switch (status) {
193 case GL_OUT_OF_MEMORY:
Steve Block3762c312012-01-06 19:20:56 +0000194 ALOGE(" OpenGLRenderer is out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800195 break;
196 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700197 }
198#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800199#if DEBUG_MEMORY_USAGE
200 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800201#else
202 if (mCaches.getDebugLevel() & kDebugMemory) {
203 mCaches.dumpMemoryUsage();
204 }
Romain Guyc15008e2010-11-10 11:59:15 -0800205#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700206}
207
Romain Guy6c319ca2011-01-11 14:29:25 -0800208void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700209 if (mCaches.currentProgram) {
210 if (mCaches.currentProgram->isInUse()) {
211 mCaches.currentProgram->remove();
212 mCaches.currentProgram = NULL;
213 }
214 }
Romain Guy50c0f092010-10-19 11:42:22 -0700215 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800216 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800217 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800218 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700219}
220
Romain Guy6c319ca2011-01-11 14:29:25 -0800221void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800222 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
223
224 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800225 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
226
Romain Guyda8532c2010-08-31 11:50:35 -0700227 glEnable(GL_SCISSOR_TEST);
Romain Guy82bc7a72012-01-03 14:13:39 -0800228 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700229 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700230
Romain Guya1d3c912011-12-13 14:55:06 -0800231 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800232 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700233
Romain Guy50c0f092010-10-19 11:42:22 -0700234 mCaches.blend = true;
235 glEnable(GL_BLEND);
236 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
237 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700238}
239
Romain Guyba6be8a2012-04-23 18:22:09 -0700240void OpenGLRenderer::detachFunctor(Functor* functor) {
241 mFunctors.remove(functor);
242}
243
244void OpenGLRenderer::attachFunctor(Functor* functor) {
245 mFunctors.add(functor);
246}
247
Romain Guy8f3b8e32012-03-27 16:33:45 -0700248status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
249 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700250 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700251
Romain Guyba6be8a2012-04-23 18:22:09 -0700252 if (count > 0) {
253 SortedVector<Functor*> functors(mFunctors);
254 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700255
Romain Guyba6be8a2012-04-23 18:22:09 -0700256 DrawGlInfo info;
257 info.clipLeft = 0;
258 info.clipTop = 0;
259 info.clipRight = 0;
260 info.clipBottom = 0;
261 info.isLayer = false;
262 info.width = 0;
263 info.height = 0;
264 memset(info.transform, 0, sizeof(float) * 16);
265
266 for (size_t i = 0; i < count; i++) {
267 Functor* f = functors.itemAt(i);
268 result |= (*f)(DrawGlInfo::kModeProcess, &info);
269
270 if (result != DrawGlInfo::kStatusDone) {
271 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
272 dirty.unionWith(localDirty);
273
274 if (result & DrawGlInfo::kStatusInvoke) {
275 mFunctors.add(f);
276 }
Romain Guy8f3b8e32012-03-27 16:33:45 -0700277 }
278 }
279 }
280
Romain Guyc189ef52012-04-25 20:02:53 -0700281 // Restore state possibly changed by the functors in process mode
282 GLboolean value;
283 glGetBooleanv(GL_BLEND, &value);
284 mCaches.blend = value;
285
286 mCaches.activeTexture(0);
287
Romain Guy8f3b8e32012-03-27 16:33:45 -0700288 return result;
289}
290
291status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800292 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800293 if (mDirtyClip) {
294 setScissorFromClip();
295 }
Romain Guyd643bb52011-03-01 14:55:21 -0800296
Romain Guy80911b82011-03-16 15:30:12 -0700297 Rect clip(*mSnapshot->clipRect);
298 clip.snapToPixelBoundaries();
299
Romain Guyd643bb52011-03-01 14:55:21 -0800300#if RENDER_LAYERS_AS_REGIONS
301 // Since we don't know what the functor will draw, let's dirty
302 // tne entire clip region
303 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800304 dirtyLayerUnchecked(clip, getRegion());
305 }
306#endif
307
Romain Guy08aa2cb2011-03-17 11:06:57 -0700308 DrawGlInfo info;
309 info.clipLeft = clip.left;
310 info.clipTop = clip.top;
311 info.clipRight = clip.right;
312 info.clipBottom = clip.bottom;
313 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700314 info.width = getSnapshot()->viewport.getWidth();
315 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700316 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700317
Romain Guy8f3b8e32012-03-27 16:33:45 -0700318 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800319
Romain Guy8f3b8e32012-03-27 16:33:45 -0700320 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700321 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800322 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700323
Chris Craik65924a32012-04-05 17:52:11 -0700324 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700325 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700326 }
Romain Guycabfcc12011-03-07 18:06:46 -0800327 }
328
Chet Haasedaf98e92011-01-10 14:10:36 -0800329 resume();
Romain Guy65549432012-03-26 16:45:05 -0700330 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800331}
332
Romain Guyf6a11b82010-06-23 17:47:49 -0700333///////////////////////////////////////////////////////////////////////////////
334// State management
335///////////////////////////////////////////////////////////////////////////////
336
Romain Guybb9524b2010-06-22 18:56:38 -0700337int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700338 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700339}
340
341int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700342 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700343}
344
345void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700346 if (mSaveCount > 1) {
347 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700348 }
Romain Guybb9524b2010-06-22 18:56:38 -0700349}
350
351void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700352 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700353
Romain Guy8fb95422010-08-17 18:38:51 -0700354 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700355 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700356 }
Romain Guybb9524b2010-06-22 18:56:38 -0700357}
358
Romain Guy8aef54f2010-09-01 15:13:49 -0700359int OpenGLRenderer::saveSnapshot(int flags) {
360 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700361 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700362}
363
364bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700365 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700366 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700367 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700368
Romain Guybd6b79b2010-06-26 00:13:53 -0700369 sp<Snapshot> current = mSnapshot;
370 sp<Snapshot> previous = mSnapshot->previous;
371
Romain Guyeb993562010-10-05 18:14:38 -0700372 if (restoreOrtho) {
373 Rect& r = previous->viewport;
374 glViewport(r.left, r.top, r.right, r.bottom);
375 mOrthoMatrix.load(current->orthoMatrix);
376 }
377
Romain Guy8b55f372010-08-18 17:10:07 -0700378 mSaveCount--;
379 mSnapshot = previous;
380
Romain Guy2542d192010-08-18 11:47:12 -0700381 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700382 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700383 }
Romain Guy2542d192010-08-18 11:47:12 -0700384
Romain Guy5ec99242010-11-03 16:19:08 -0700385 if (restoreLayer) {
386 composeLayer(current, previous);
387 }
388
Romain Guy2542d192010-08-18 11:47:12 -0700389 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700390}
391
Romain Guyf6a11b82010-06-23 17:47:49 -0700392///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700393// Layers
394///////////////////////////////////////////////////////////////////////////////
395
396int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700397 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700398 const GLuint previousFbo = mSnapshot->fbo;
399 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700400
Romain Guyaf636eb2010-12-09 17:47:21 -0800401 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700402 int alpha = 255;
403 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700404
Romain Guye45362c2010-11-03 19:58:32 -0700405 if (p) {
406 alpha = p->getAlpha();
407 if (!mCaches.extensions.hasFramebufferFetch()) {
408 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
409 if (!isMode) {
410 // Assume SRC_OVER
411 mode = SkXfermode::kSrcOver_Mode;
412 }
413 } else {
414 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700415 }
416 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700417 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700418 }
Romain Guyd55a8612010-06-28 17:42:46 -0700419
Romain Guydbc26d22010-10-11 17:58:29 -0700420 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
421 }
Romain Guyd55a8612010-06-28 17:42:46 -0700422
423 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700424}
425
426int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
427 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700428 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700429 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700430 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700431 SkPaint paint;
432 paint.setAlpha(alpha);
433 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700434 }
Romain Guyd55a8612010-06-28 17:42:46 -0700435}
Romain Guybd6b79b2010-06-26 00:13:53 -0700436
Romain Guy1c740bc2010-09-13 18:00:09 -0700437/**
438 * Layers are viewed by Skia are slightly different than layers in image editing
439 * programs (for instance.) When a layer is created, previously created layers
440 * and the frame buffer still receive every drawing command. For instance, if a
441 * layer is created and a shape intersecting the bounds of the layers and the
442 * framebuffer is draw, the shape will be drawn on both (unless the layer was
443 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
444 *
445 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
446 * texture. Unfortunately, this is inefficient as it requires every primitive to
447 * be drawn n + 1 times, where n is the number of active layers. In practice this
448 * means, for every primitive:
449 * - Switch active frame buffer
450 * - Change viewport, clip and projection matrix
451 * - Issue the drawing
452 *
453 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700454 * To avoid this, layers are implemented in a different way here, at least in the
455 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
456 * is set. When this flag is set we can redirect all drawing operations into a
457 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700458 *
459 * This implementation relies on the frame buffer being at least RGBA 8888. When
460 * a layer is created, only a texture is created, not an FBO. The content of the
461 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700462 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700463 * buffer and drawing continues as normal. This technique therefore treats the
464 * frame buffer as a scratch buffer for the layers.
465 *
466 * To compose the layers back onto the frame buffer, each layer texture
467 * (containing the original frame buffer data) is drawn as a simple quad over
468 * the frame buffer. The trick is that the quad is set as the composition
469 * destination in the blending equation, and the frame buffer becomes the source
470 * of the composition.
471 *
472 * Drawing layers with an alpha value requires an extra step before composition.
473 * An empty quad is drawn over the layer's region in the frame buffer. This quad
474 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
475 * quad is used to multiply the colors in the frame buffer. This is achieved by
476 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
477 * GL_ZERO, GL_SRC_ALPHA.
478 *
479 * Because glCopyTexImage2D() can be slow, an alternative implementation might
480 * be use to draw a single clipped layer. The implementation described above
481 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700482 *
483 * (1) The frame buffer is actually not cleared right away. To allow the GPU
484 * to potentially optimize series of calls to glCopyTexImage2D, the frame
485 * buffer is left untouched until the first drawing operation. Only when
486 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700487 */
Romain Guyd55a8612010-06-28 17:42:46 -0700488bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700489 float right, float bottom, int alpha, SkXfermode::Mode mode,
490 int flags, GLuint previousFbo) {
491 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700492 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700493
Romain Guyeb993562010-10-05 18:14:38 -0700494 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
495
Romain Guyf607bdc2010-09-10 19:20:06 -0700496 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700497 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700498 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700499 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700500
Romain Guyeb993562010-10-05 18:14:38 -0700501 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700502 if (bounds.intersect(*snapshot->clipRect)) {
503 // We cannot work with sub-pixels in this case
504 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700505
Romain Guyad37cd32011-03-15 11:12:25 -0700506 // When the layer is not an FBO, we may use glCopyTexImage so we
507 // need to make sure the layer does not extend outside the bounds
508 // of the framebuffer
509 if (!bounds.intersect(snapshot->previous->viewport)) {
510 bounds.setEmpty();
511 }
512 } else {
513 bounds.setEmpty();
514 }
Romain Guyeb993562010-10-05 18:14:38 -0700515 }
Romain Guybf434112010-09-16 14:40:17 -0700516
Romain Guy746b7402010-10-26 16:27:31 -0700517 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
518 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800519 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700520 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700521 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700522 }
523
524 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800525 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700526 return false;
527 }
Romain Guyf18fd992010-07-08 11:45:51 -0700528
Romain Guya1d3c912011-12-13 14:55:06 -0800529 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700530 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700531 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700532 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700533 }
534
Romain Guy9ace8f52011-07-07 20:50:11 -0700535 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700536 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700537 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
538 bounds.getWidth() / float(layer->getWidth()), 0.0f);
539 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700540 layer->setBlend(true);
Romain Guydda570202010-07-06 11:39:32 -0700541
Romain Guy8fb95422010-08-17 18:38:51 -0700542 // Save the layer in the snapshot
543 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700544 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700545
Romain Guyeb993562010-10-05 18:14:38 -0700546 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700547 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700548 } else {
549 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700550 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800551 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700552 if (layer->isEmpty()) {
553 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
554 bounds.left, snapshot->height - bounds.bottom,
555 layer->getWidth(), layer->getHeight(), 0);
556 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800557 } else {
558 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
559 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
560 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700561
Romain Guy54be1cd2011-06-13 19:04:27 -0700562 // Enqueue the buffer coordinates to clear the corresponding region later
563 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700564 }
Romain Guyeb993562010-10-05 18:14:38 -0700565 }
Romain Guyf86ef572010-07-01 11:05:42 -0700566
Romain Guyd55a8612010-06-28 17:42:46 -0700567 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700568}
569
Romain Guy5b3b3522010-10-27 18:57:51 -0700570bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
571 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700572 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700573
574#if RENDER_LAYERS_AS_REGIONS
575 snapshot->region = &snapshot->layer->region;
576 snapshot->flags |= Snapshot::kFlagFboTarget;
577#endif
578
579 Rect clip(bounds);
580 snapshot->transform->mapRect(clip);
581 clip.intersect(*snapshot->clipRect);
582 clip.snapToPixelBoundaries();
583 clip.intersect(snapshot->previous->viewport);
584
585 mat4 inverse;
586 inverse.loadInverse(*mSnapshot->transform);
587
588 inverse.mapRect(clip);
589 clip.snapToPixelBoundaries();
590 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700591 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700592
593 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700594 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700595 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700596 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
597 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
598 snapshot->height = bounds.getHeight();
599 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
600 snapshot->orthoMatrix.load(mOrthoMatrix);
601
602 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700603 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
604 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700605
606 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700607 if (layer->isEmpty()) {
608 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
609 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700610 }
611
612 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700613 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700614
615#if DEBUG_LAYERS_AS_REGIONS
616 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
617 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000618 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700619
620 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700621 layer->deleteTexture();
622 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700623
624 delete layer;
625
626 return false;
627 }
628#endif
629
630 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800631 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700632 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700633 glClear(GL_COLOR_BUFFER_BIT);
634
635 dirtyClip();
636
637 // Change the ortho projection
638 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
639 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
640
641 return true;
642}
643
Romain Guy1c740bc2010-09-13 18:00:09 -0700644/**
645 * Read the documentation of createLayer() before doing anything in this method.
646 */
Romain Guy1d83e192010-08-17 11:37:00 -0700647void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
648 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000649 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700650 return;
651 }
652
Romain Guy5b3b3522010-10-27 18:57:51 -0700653 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700654
655 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700656 // Detach the texture from the FBO
657 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
658
Romain Guyeb993562010-10-05 18:14:38 -0700659 // Unbind current FBO and restore previous one
660 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
661 }
662
Romain Guy1d83e192010-08-17 11:37:00 -0700663 Layer* layer = current->layer;
664 const Rect& rect = layer->layer;
665
Romain Guy9ace8f52011-07-07 20:50:11 -0700666 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700667 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700668 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700669 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700670 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700671 }
672
Romain Guy03750a02010-10-18 14:06:08 -0700673 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700674
Romain Guya1d3c912011-12-13 14:55:06 -0800675 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700676
Romain Guy5b3b3522010-10-27 18:57:51 -0700677 // When the layer is stored in an FBO, we can save a bit of fillrate by
678 // drawing only the dirty region
679 if (fboLayer) {
680 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700681 if (layer->getColorFilter()) {
682 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800683 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700684 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700685 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800686 resetColorFilter();
687 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700688 } else if (!rect.isEmpty()) {
689 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
690 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700691 }
Romain Guy8b55f372010-08-18 17:10:07 -0700692
Romain Guyeb993562010-10-05 18:14:38 -0700693 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800694 // Note: No need to use glDiscardFramebufferEXT() since we never
695 // create/compose layers that are not on screen with this
696 // code path
697 // See LayerRenderer::destroyLayer(Layer*)
698
Romain Guyeb993562010-10-05 18:14:38 -0700699 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
700 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700701 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700702 }
703
Romain Guy746b7402010-10-26 16:27:31 -0700704 dirtyClip();
705
Romain Guyeb993562010-10-05 18:14:38 -0700706 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700707 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700708 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700709 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700710 delete layer;
711 }
712}
713
Romain Guyaa6c24c2011-04-28 18:40:04 -0700714void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700715 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700716
Romain Guy302a9df2011-08-16 13:55:02 -0700717 mat4& transform = layer->getTransform();
718 if (!transform.isIdentity()) {
719 save(0);
720 mSnapshot->transform->multiply(transform);
721 }
722
Romain Guyaa6c24c2011-04-28 18:40:04 -0700723 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700724 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700725 setupDrawWithTexture();
726 } else {
727 setupDrawWithExternalTexture();
728 }
729 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700730 setupDrawColor(alpha, alpha, alpha, alpha);
731 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700732 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700733 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700734 setupDrawPureColorUniforms();
735 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700736 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
737 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700738 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700739 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700740 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700741 if (mSnapshot->transform->isPureTranslate() &&
742 layer->getWidth() == (uint32_t) rect.getWidth() &&
743 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700744 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
745 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
746
Romain Guyd21b6e12011-11-30 20:21:23 -0800747 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700748 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
749 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800750 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700751 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
752 }
753 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700754 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
755
756 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
757
758 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700759
760 if (!transform.isIdentity()) {
761 restore();
762 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700763}
764
Romain Guy5b3b3522010-10-27 18:57:51 -0700765void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700766 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700767 const Rect& texCoords = layer->texCoords;
768 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
769 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700770
Romain Guy9ace8f52011-07-07 20:50:11 -0700771 float x = rect.left;
772 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700773 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700774 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700775 layer->getHeight() == (uint32_t) rect.getHeight();
776
777 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700778 // When we're swapping, the layer is already in screen coordinates
779 if (!swap) {
780 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
781 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
782 }
783
Romain Guyd21b6e12011-11-30 20:21:23 -0800784 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700785 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800786 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700787 }
788
789 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
790 layer->getTexture(), layer->getAlpha() / 255.0f,
791 layer->getMode(), layer->isBlend(),
792 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
793 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700794
Romain Guyaa6c24c2011-04-28 18:40:04 -0700795 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
796 } else {
797 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
798 drawTextureLayer(layer, rect);
799 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
800 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700801}
802
803void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
804#if RENDER_LAYERS_AS_REGIONS
805 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700806 layer->setRegionAsRect();
807
Romain Guy40667672011-03-18 14:34:03 -0700808 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700809
Romain Guy5b3b3522010-10-27 18:57:51 -0700810 layer->region.clear();
811 return;
812 }
813
Romain Guy8a3957d2011-09-07 17:55:15 -0700814 // TODO: See LayerRenderer.cpp::generateMesh() for important
815 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800816 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700817 size_t count;
818 const android::Rect* rects = layer->region.getArray(&count);
819
Romain Guy9ace8f52011-07-07 20:50:11 -0700820 const float alpha = layer->getAlpha() / 255.0f;
821 const float texX = 1.0f / float(layer->getWidth());
822 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800823 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700824
825 TextureVertex* mesh = mCaches.getRegionMesh();
826 GLsizei numQuads = 0;
827
Romain Guy7230a742011-01-10 22:26:16 -0800828 setupDraw();
829 setupDrawWithTexture();
830 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800831 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700832 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800833 setupDrawProgram();
834 setupDrawDirtyRegionsDisabled();
835 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800836 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700837 setupDrawTexture(layer->getTexture());
838 if (mSnapshot->transform->isPureTranslate()) {
839 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
840 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
841
Romain Guyd21b6e12011-11-30 20:21:23 -0800842 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700843 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
844 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800845 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700846 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
847 }
Romain Guy15bc6432011-12-13 13:11:32 -0800848 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700849
850 for (size_t i = 0; i < count; i++) {
851 const android::Rect* r = &rects[i];
852
853 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800854 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700855 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800856 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700857
858 // TODO: Reject quads outside of the clip
859 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
860 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
861 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
862 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
863
864 numQuads++;
865
866 if (numQuads >= REGION_MESH_QUAD_COUNT) {
867 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
868 numQuads = 0;
869 mesh = mCaches.getRegionMesh();
870 }
871 }
872
873 if (numQuads > 0) {
874 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
875 }
876
Romain Guy7230a742011-01-10 22:26:16 -0800877 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700878
879#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800880 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700881#endif
882
883 layer->region.clear();
884 }
885#else
886 composeLayerRect(layer, rect);
887#endif
888}
889
Romain Guy3a3133d2011-02-01 22:59:58 -0800890void OpenGLRenderer::drawRegionRects(const Region& region) {
891#if DEBUG_LAYERS_AS_REGIONS
892 size_t count;
893 const android::Rect* rects = region.getArray(&count);
894
895 uint32_t colors[] = {
896 0x7fff0000, 0x7f00ff00,
897 0x7f0000ff, 0x7fff00ff,
898 };
899
900 int offset = 0;
901 int32_t top = rects[0].top;
902
903 for (size_t i = 0; i < count; i++) {
904 if (top != rects[i].top) {
905 offset ^= 0x2;
906 top = rects[i].top;
907 }
908
909 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
910 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
911 SkXfermode::kSrcOver_Mode);
912 }
913#endif
914}
915
Romain Guy5b3b3522010-10-27 18:57:51 -0700916void OpenGLRenderer::dirtyLayer(const float left, const float top,
917 const float right, const float bottom, const mat4 transform) {
918#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800919 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700920 Rect bounds(left, top, right, bottom);
921 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800922 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700923 }
924#endif
925}
926
927void OpenGLRenderer::dirtyLayer(const float left, const float top,
928 const float right, const float bottom) {
929#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800930 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700931 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800932 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800933 }
934#endif
935}
936
937void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
938#if RENDER_LAYERS_AS_REGIONS
939 if (bounds.intersect(*mSnapshot->clipRect)) {
940 bounds.snapToPixelBoundaries();
941 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
942 if (!dirty.isEmpty()) {
943 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700944 }
945 }
946#endif
947}
948
Romain Guy54be1cd2011-06-13 19:04:27 -0700949void OpenGLRenderer::clearLayerRegions() {
950 const size_t count = mLayers.size();
951 if (count == 0) return;
952
953 if (!mSnapshot->isIgnored()) {
954 // Doing several glScissor/glClear here can negatively impact
955 // GPUs with a tiler architecture, instead we draw quads with
956 // the Clear blending mode
957
958 // The list contains bounds that have already been clipped
959 // against their initial clip rect, and the current clip
960 // is likely different so we need to disable clipping here
961 glDisable(GL_SCISSOR_TEST);
962
963 Vertex mesh[count * 6];
964 Vertex* vertex = mesh;
965
966 for (uint32_t i = 0; i < count; i++) {
967 Rect* bounds = mLayers.itemAt(i);
968
969 Vertex::set(vertex++, bounds->left, bounds->bottom);
970 Vertex::set(vertex++, bounds->left, bounds->top);
971 Vertex::set(vertex++, bounds->right, bounds->top);
972 Vertex::set(vertex++, bounds->left, bounds->bottom);
973 Vertex::set(vertex++, bounds->right, bounds->top);
974 Vertex::set(vertex++, bounds->right, bounds->bottom);
975
976 delete bounds;
977 }
978
979 setupDraw(false);
980 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
981 setupDrawBlending(true, SkXfermode::kClear_Mode);
982 setupDrawProgram();
983 setupDrawPureColorUniforms();
984 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800985 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700986
Romain Guy54be1cd2011-06-13 19:04:27 -0700987 glDrawArrays(GL_TRIANGLES, 0, count * 6);
988
989 glEnable(GL_SCISSOR_TEST);
990 } else {
991 for (uint32_t i = 0; i < count; i++) {
992 delete mLayers.itemAt(i);
993 }
994 }
995
996 mLayers.clear();
997}
998
Romain Guybd6b79b2010-06-26 00:13:53 -0700999///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001000// Transforms
1001///////////////////////////////////////////////////////////////////////////////
1002
1003void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001004 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001005}
1006
1007void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001008 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001009}
1010
1011void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001012 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001013}
1014
Romain Guy807daf72011-01-18 11:19:19 -08001015void OpenGLRenderer::skew(float sx, float sy) {
1016 mSnapshot->transform->skew(sx, sy);
1017}
1018
Romain Guyf6a11b82010-06-23 17:47:49 -07001019void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001020 if (matrix) {
1021 mSnapshot->transform->load(*matrix);
1022 } else {
1023 mSnapshot->transform->loadIdentity();
1024 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001025}
1026
1027void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001028 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001029}
1030
1031void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001032 SkMatrix transform;
1033 mSnapshot->transform->copyTo(transform);
1034 transform.preConcat(*matrix);
1035 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001036}
1037
1038///////////////////////////////////////////////////////////////////////////////
1039// Clipping
1040///////////////////////////////////////////////////////////////////////////////
1041
Romain Guybb9524b2010-06-22 18:56:38 -07001042void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001043 Rect clip(*mSnapshot->clipRect);
1044 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001045
1046 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1047 clip.getWidth(), clip.getHeight());
1048
Romain Guy746b7402010-10-26 16:27:31 -07001049 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -07001050}
1051
1052const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001053 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001054}
1055
Romain Guyc7d53492010-06-25 13:41:57 -07001056bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001057 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001058 return true;
1059 }
1060
Romain Guy1d83e192010-08-17 11:37:00 -07001061 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001062 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001063 r.snapToPixelBoundaries();
1064
1065 Rect clipRect(*mSnapshot->clipRect);
1066 clipRect.snapToPixelBoundaries();
1067
1068 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001069}
1070
Romain Guy079ba2c2010-07-16 14:12:24 -07001071bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1072 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001073 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001074 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001075 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001076 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001077}
1078
Chet Haasea23eed82012-04-12 15:19:04 -07001079Rect* OpenGLRenderer::getClipRect() {
1080 return mSnapshot->clipRect;
1081}
1082
Romain Guyf6a11b82010-06-23 17:47:49 -07001083///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001084// Drawing commands
1085///////////////////////////////////////////////////////////////////////////////
1086
Romain Guy54be1cd2011-06-13 19:04:27 -07001087void OpenGLRenderer::setupDraw(bool clear) {
1088 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001089 if (mDirtyClip) {
1090 setScissorFromClip();
1091 }
1092 mDescription.reset();
1093 mSetShaderColor = false;
1094 mColorSet = false;
1095 mColorA = mColorR = mColorG = mColorB = 0.0f;
1096 mTextureUnit = 0;
1097 mTrackDirtyRegions = true;
1098}
1099
1100void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1101 mDescription.hasTexture = true;
1102 mDescription.hasAlpha8Texture = isAlpha8;
1103}
1104
Romain Guyaa6c24c2011-04-28 18:40:04 -07001105void OpenGLRenderer::setupDrawWithExternalTexture() {
1106 mDescription.hasExternalTexture = true;
1107}
1108
Romain Guy15bc6432011-12-13 13:11:32 -08001109void OpenGLRenderer::setupDrawNoTexture() {
1110 mCaches.disbaleTexCoordsVertexArray();
1111}
1112
Chet Haase5b0200b2011-04-13 17:58:08 -07001113void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001114 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001115}
1116
Romain Guyed6fcb02011-03-21 13:11:28 -07001117void OpenGLRenderer::setupDrawPoint(float pointSize) {
1118 mDescription.isPoint = true;
1119 mDescription.pointSize = pointSize;
1120}
1121
Romain Guy70ca14e2010-12-13 18:24:33 -08001122void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001123 setupDrawColor(color, (color >> 24) & 0xFF);
1124}
1125
1126void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1127 mColorA = alpha / 255.0f;
Chet Haasedb8c9a62012-03-21 18:54:18 -07001128 mColorA *= mSnapshot->alpha;
Chet Haase6cfdf452011-04-22 16:42:10 -07001129 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1130 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001131 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001132 mColorR = a * ((color >> 16) & 0xFF);
1133 mColorG = a * ((color >> 8) & 0xFF);
1134 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001135 mColorSet = true;
1136 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1137}
1138
Romain Guy86568192010-12-14 15:55:39 -08001139void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1140 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001141 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1142 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001143 const float a = mColorA / 255.0f;
1144 mColorR = a * ((color >> 16) & 0xFF);
1145 mColorG = a * ((color >> 8) & 0xFF);
1146 mColorB = a * ((color ) & 0xFF);
1147 mColorSet = true;
1148 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1149}
1150
Romain Guy70ca14e2010-12-13 18:24:33 -08001151void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1152 mColorA = a;
1153 mColorR = r;
1154 mColorG = g;
1155 mColorB = b;
1156 mColorSet = true;
1157 mSetShaderColor = mDescription.setColor(r, g, b, a);
1158}
1159
Romain Guy86568192010-12-14 15:55:39 -08001160void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1161 mColorA = a;
1162 mColorR = r;
1163 mColorG = g;
1164 mColorB = b;
1165 mColorSet = true;
1166 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1167}
1168
Romain Guy70ca14e2010-12-13 18:24:33 -08001169void OpenGLRenderer::setupDrawShader() {
1170 if (mShader) {
1171 mShader->describe(mDescription, mCaches.extensions);
1172 }
1173}
1174
1175void OpenGLRenderer::setupDrawColorFilter() {
1176 if (mColorFilter) {
1177 mColorFilter->describe(mDescription, mCaches.extensions);
1178 }
1179}
1180
Romain Guyf09ef512011-05-27 11:43:46 -07001181void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1182 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1183 mColorA = 1.0f;
1184 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001185 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001186 }
1187}
1188
Romain Guy70ca14e2010-12-13 18:24:33 -08001189void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001190 // When the blending mode is kClear_Mode, we need to use a modulate color
1191 // argb=1,0,0,0
1192 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001193 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1194 mDescription, swapSrcDst);
1195}
1196
1197void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001198 // When the blending mode is kClear_Mode, we need to use a modulate color
1199 // argb=1,0,0,0
1200 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001201 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1202 mDescription, swapSrcDst);
1203}
1204
1205void OpenGLRenderer::setupDrawProgram() {
1206 useProgram(mCaches.programCache.get(mDescription));
1207}
1208
1209void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1210 mTrackDirtyRegions = false;
1211}
1212
1213void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1214 bool ignoreTransform) {
1215 mModelView.loadTranslate(left, top, 0.0f);
1216 if (!ignoreTransform) {
1217 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1218 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1219 } else {
1220 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1221 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1222 }
1223}
1224
Chet Haase8a5cc922011-04-26 07:28:09 -07001225void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1226 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001227}
1228
Romain Guy70ca14e2010-12-13 18:24:33 -08001229void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1230 bool ignoreTransform, bool ignoreModelView) {
1231 if (!ignoreModelView) {
1232 mModelView.loadTranslate(left, top, 0.0f);
1233 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001234 } else {
1235 mModelView.loadIdentity();
1236 }
Romain Guy86568192010-12-14 15:55:39 -08001237 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1238 if (!ignoreTransform) {
1239 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1240 if (mTrackDirtyRegions && dirty) {
1241 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1242 }
1243 } else {
1244 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1245 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1246 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001247}
1248
Romain Guyed6fcb02011-03-21 13:11:28 -07001249void OpenGLRenderer::setupDrawPointUniforms() {
1250 int slot = mCaches.currentProgram->getUniform("pointSize");
1251 glUniform1f(slot, mDescription.pointSize);
1252}
1253
Romain Guy70ca14e2010-12-13 18:24:33 -08001254void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001255 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001256 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1257 }
1258}
1259
Romain Guy86568192010-12-14 15:55:39 -08001260void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001261 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001262 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001263 }
1264}
1265
Romain Guy70ca14e2010-12-13 18:24:33 -08001266void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1267 if (mShader) {
1268 if (ignoreTransform) {
1269 mModelView.loadInverse(*mSnapshot->transform);
1270 }
1271 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1272 }
1273}
1274
Romain Guy8d0d4782010-12-14 20:13:35 -08001275void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1276 if (mShader) {
1277 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1278 }
1279}
1280
Romain Guy70ca14e2010-12-13 18:24:33 -08001281void OpenGLRenderer::setupDrawColorFilterUniforms() {
1282 if (mColorFilter) {
1283 mColorFilter->setupProgram(mCaches.currentProgram);
1284 }
1285}
1286
1287void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001288 bool force = mCaches.bindMeshBuffer();
1289 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001290 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001291}
1292
1293void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1294 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001295 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001296 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001297}
1298
Romain Guyaa6c24c2011-04-28 18:40:04 -07001299void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1300 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001301 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001302 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001303}
1304
Romain Guy8f0095c2011-05-02 17:24:22 -07001305void OpenGLRenderer::setupDrawTextureTransform() {
1306 mDescription.hasTextureTransform = true;
1307}
1308
1309void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001310 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1311 GL_FALSE, &transform.data[0]);
1312}
1313
Romain Guy70ca14e2010-12-13 18:24:33 -08001314void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001315 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001316 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001317 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001318 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001319 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001320 }
Romain Guyd71dd362011-12-12 19:03:35 -08001321
Romain Guyf3a910b42011-12-12 20:35:21 -08001322 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001323 if (mCaches.currentProgram->texCoords >= 0) {
1324 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1325 }
1326
1327 mCaches.unbindIndicesBuffer();
1328}
1329
1330void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1331 bool force = mCaches.unbindMeshBuffer();
1332 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1333 if (mCaches.currentProgram->texCoords >= 0) {
1334 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001335 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001336}
1337
Chet Haase5b0200b2011-04-13 17:58:08 -07001338void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001339 bool force = mCaches.unbindMeshBuffer();
1340 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1341 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001342 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001343}
1344
1345/**
1346 * 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 -07001347 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1348 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1349 * attributes (one per vertex) are values from zero to one that tells the fragment
1350 * shader where the fragment is in relation to the line width/length overall; these values are
1351 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1352 * region of the line.
1353 * Note that we only pass down the width values in this setup function. The length coordinates
1354 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001355 */
Chet Haase99585ad2011-05-02 15:00:16 -07001356void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001357 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001358 bool force = mCaches.unbindMeshBuffer();
1359 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1360 vertices, gAAVertexStride);
1361 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001362 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001363
Romain Guy7b631422012-04-04 11:38:54 -07001364 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001365 glEnableVertexAttribArray(widthSlot);
1366 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001367
Romain Guy7b631422012-04-04 11:38:54 -07001368 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001369 glEnableVertexAttribArray(lengthSlot);
1370 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001371
Chet Haase5b0200b2011-04-13 17:58:08 -07001372 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001373 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001374
Chet Haase99585ad2011-05-02 15:00:16 -07001375 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001376 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Romain Guy7b631422012-04-04 11:38:54 -07001377 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion);
1378}
1379
1380void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1381 glDisableVertexAttribArray(widthSlot);
1382 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001383}
1384
Romain Guy70ca14e2010-12-13 18:24:33 -08001385void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001386}
1387
1388///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001389// Drawing
1390///////////////////////////////////////////////////////////////////////////////
1391
Chet Haase1271e2c2012-04-20 09:54:27 -07001392status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001393 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001394
Romain Guy0fe478e2010-11-08 12:08:41 -08001395 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1396 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001397 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001398 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001399 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001400
Romain Guy65549432012-03-26 16:45:05 -07001401 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001402}
1403
Chet Haaseed30fd82011-04-22 16:18:45 -07001404void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1405 if (displayList) {
1406 displayList->output(*this, level);
1407 }
1408}
1409
Romain Guya168d732011-03-18 16:50:13 -07001410void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1411 int alpha;
1412 SkXfermode::Mode mode;
1413 getAlphaAndMode(paint, &alpha, &mode);
1414
Romain Guya168d732011-03-18 16:50:13 -07001415 float x = left;
1416 float y = top;
1417
Romain Guye3c26852011-07-25 16:36:01 -07001418 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001419 bool ignoreTransform = false;
1420 if (mSnapshot->transform->isPureTranslate()) {
1421 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1422 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1423 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001424 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001425 } else {
1426 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001427 }
1428
1429 setupDraw();
1430 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001431 if (paint) {
1432 setupDrawAlpha8Color(paint->getColor(), alpha);
1433 }
Romain Guya168d732011-03-18 16:50:13 -07001434 setupDrawColorFilter();
1435 setupDrawShader();
1436 setupDrawBlending(true, mode);
1437 setupDrawProgram();
1438 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001439
Romain Guya168d732011-03-18 16:50:13 -07001440 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001441 texture->setWrap(GL_CLAMP_TO_EDGE);
1442 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001443
Romain Guya168d732011-03-18 16:50:13 -07001444 setupDrawPureColorUniforms();
1445 setupDrawColorFilterUniforms();
1446 setupDrawShaderUniforms();
1447 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1448
1449 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1450
1451 finishDrawTexture();
1452}
1453
Chet Haase5c13d892010-10-08 08:37:55 -07001454void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001455 const float right = left + bitmap->width();
1456 const float bottom = top + bitmap->height();
1457
1458 if (quickReject(left, top, right, bottom)) {
1459 return;
1460 }
1461
Romain Guya1d3c912011-12-13 14:55:06 -08001462 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001463 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001464 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001465 const AutoTexture autoCleanup(texture);
1466
Romain Guy211370f2012-02-01 16:10:55 -08001467 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001468 drawAlphaBitmap(texture, left, top, paint);
1469 } else {
1470 drawTextureRect(left, top, right, bottom, texture, paint);
1471 }
Romain Guyce0537b2010-06-29 21:05:21 -07001472}
1473
Chet Haase5c13d892010-10-08 08:37:55 -07001474void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001475 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1476 const mat4 transform(*matrix);
1477 transform.mapRect(r);
1478
Romain Guy6926c722010-07-12 20:20:03 -07001479 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1480 return;
1481 }
1482
Romain Guya1d3c912011-12-13 14:55:06 -08001483 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001484 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001485 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001486 const AutoTexture autoCleanup(texture);
1487
Romain Guy5b3b3522010-10-27 18:57:51 -07001488 // This could be done in a cheaper way, all we need is pass the matrix
1489 // to the vertex shader. The save/restore is a bit overkill.
1490 save(SkCanvas::kMatrix_SaveFlag);
1491 concatMatrix(matrix);
1492 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1493 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001494}
1495
Romain Guy5a7b4662011-01-20 19:09:30 -08001496void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1497 float* vertices, int* colors, SkPaint* paint) {
1498 // TODO: Do a quickReject
1499 if (!vertices || mSnapshot->isIgnored()) {
1500 return;
1501 }
1502
Romain Guya1d3c912011-12-13 14:55:06 -08001503 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001504 Texture* texture = mCaches.textureCache.get(bitmap);
1505 if (!texture) return;
1506 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001507
Romain Guyd21b6e12011-11-30 20:21:23 -08001508 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1509 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001510
1511 int alpha;
1512 SkXfermode::Mode mode;
1513 getAlphaAndMode(paint, &alpha, &mode);
1514
Romain Guy5a7b4662011-01-20 19:09:30 -08001515 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001516
Romain Guyb18d2d02011-02-10 15:52:54 -08001517 float left = FLT_MAX;
1518 float top = FLT_MAX;
1519 float right = FLT_MIN;
1520 float bottom = FLT_MIN;
1521
1522#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001523 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001524#else
Romain Guy211370f2012-02-01 16:10:55 -08001525 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001526#endif
1527
Romain Guya566b7c2011-01-23 16:36:11 -08001528 // TODO: Support the colors array
1529 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001530 TextureVertex* vertex = mesh;
1531 for (int32_t y = 0; y < meshHeight; y++) {
1532 for (int32_t x = 0; x < meshWidth; x++) {
1533 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1534
1535 float u1 = float(x) / meshWidth;
1536 float u2 = float(x + 1) / meshWidth;
1537 float v1 = float(y) / meshHeight;
1538 float v2 = float(y + 1) / meshHeight;
1539
1540 int ax = i + (meshWidth + 1) * 2;
1541 int ay = ax + 1;
1542 int bx = i;
1543 int by = bx + 1;
1544 int cx = i + 2;
1545 int cy = cx + 1;
1546 int dx = i + (meshWidth + 1) * 2 + 2;
1547 int dy = dx + 1;
1548
1549 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1550 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1551 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1552
1553 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1554 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1555 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001556
1557#if RENDER_LAYERS_AS_REGIONS
1558 if (hasActiveLayer) {
1559 // TODO: This could be optimized to avoid unnecessary ops
1560 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1561 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1562 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1563 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1564 }
1565#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001566 }
1567 }
1568
Romain Guyb18d2d02011-02-10 15:52:54 -08001569#if RENDER_LAYERS_AS_REGIONS
1570 if (hasActiveLayer) {
1571 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1572 }
1573#endif
1574
Romain Guy5a7b4662011-01-20 19:09:30 -08001575 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1576 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001577 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001578}
1579
Romain Guy8ba548f2010-06-30 19:21:21 -07001580void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1581 float srcLeft, float srcTop, float srcRight, float srcBottom,
1582 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001583 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001584 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1585 return;
1586 }
1587
Romain Guya1d3c912011-12-13 14:55:06 -08001588 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001589 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001590 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001591 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001592
Romain Guy8ba548f2010-06-30 19:21:21 -07001593 const float width = texture->width;
1594 const float height = texture->height;
1595
Romain Guy68169722011-08-22 17:33:33 -07001596 const float u1 = fmax(0.0f, srcLeft / width);
1597 const float v1 = fmax(0.0f, srcTop / height);
1598 const float u2 = fmin(1.0f, srcRight / width);
1599 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001600
Romain Guy03750a02010-10-18 14:06:08 -07001601 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001602 resetDrawTextureTexCoords(u1, v1, u2, v2);
1603
Romain Guy03750a02010-10-18 14:06:08 -07001604 int alpha;
1605 SkXfermode::Mode mode;
1606 getAlphaAndMode(paint, &alpha, &mode);
1607
Romain Guyd21b6e12011-11-30 20:21:23 -08001608 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1609
Romain Guy211370f2012-02-01 16:10:55 -08001610 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001611 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1612 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1613
Romain Guyb5014982011-07-28 15:39:12 -07001614 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001615 // Enable linear filtering if the source rectangle is scaled
1616 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001617 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001618 }
Romain Guyb5014982011-07-28 15:39:12 -07001619
Romain Guyd21b6e12011-11-30 20:21:23 -08001620 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001621 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1622 texture->id, alpha / 255.0f, mode, texture->blend,
1623 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1624 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1625 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001626 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001627 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1628 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1629 GL_TRIANGLE_STRIP, gMeshCount);
1630 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001631
1632 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1633}
1634
Romain Guy4aa90572010-09-26 18:40:37 -07001635void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001636 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001637 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001638 if (quickReject(left, top, right, bottom)) {
1639 return;
1640 }
1641
Romain Guya1d3c912011-12-13 14:55:06 -08001642 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001643 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001644 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001645 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001646 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1647 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001648
1649 int alpha;
1650 SkXfermode::Mode mode;
1651 getAlphaAndMode(paint, &alpha, &mode);
1652
Romain Guy2728f962010-10-08 18:36:15 -07001653 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001654 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001655
Romain Guy211370f2012-02-01 16:10:55 -08001656 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001657 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001658#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001659 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001660 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001661 const float offsetX = left + mSnapshot->transform->getTranslateX();
1662 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001663 const size_t count = mesh->quads.size();
1664 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001665 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001666 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001667 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1668 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1669 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001670 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001671 dirtyLayer(left + bounds.left, top + bounds.top,
1672 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001673 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001674 }
1675 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001676#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001677
Romain Guy211370f2012-02-01 16:10:55 -08001678 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001679 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1680 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1681
1682 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1683 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1684 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1685 true, !mesh->hasEmptyQuads);
1686 } else {
1687 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1688 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1689 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1690 true, !mesh->hasEmptyQuads);
1691 }
Romain Guy054dc182010-10-15 17:55:25 -07001692 }
Romain Guyf7f93552010-07-08 19:17:03 -07001693}
1694
Chet Haase99ecdc42011-05-06 12:06:34 -07001695/**
Chet Haase858aa932011-05-12 09:06:00 -07001696 * This function uses a similar approach to that of AA lines in the drawLines() function.
1697 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1698 * shader to compute the translucency of the color, determined by whether a given pixel is
1699 * within that boundary region and how far into the region it is.
1700 */
1701void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001702 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001703 float inverseScaleX = 1.0f;
1704 float inverseScaleY = 1.0f;
1705 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001706 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001707 Matrix4 *mat = mSnapshot->transform;
1708 float m00 = mat->data[Matrix4::kScaleX];
1709 float m01 = mat->data[Matrix4::kSkewY];
1710 float m02 = mat->data[2];
1711 float m10 = mat->data[Matrix4::kSkewX];
1712 float m11 = mat->data[Matrix4::kScaleX];
1713 float m12 = mat->data[6];
1714 float scaleX = sqrt(m00 * m00 + m01 * m01);
1715 float scaleY = sqrt(m10 * m10 + m11 * m11);
1716 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1717 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1718 }
1719
1720 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001721 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001722 setupDrawAALine();
1723 setupDrawColor(color);
1724 setupDrawColorFilter();
1725 setupDrawShader();
1726 setupDrawBlending(true, mode);
1727 setupDrawProgram();
1728 setupDrawModelViewIdentity(true);
1729 setupDrawColorUniforms();
1730 setupDrawColorFilterUniforms();
1731 setupDrawShaderIdentityUniforms();
1732
1733 AAVertex rects[4];
1734 AAVertex* aaVertices = &rects[0];
1735 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1736 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1737
1738 float boundarySizeX = .5 * inverseScaleX;
1739 float boundarySizeY = .5 * inverseScaleY;
1740
1741 // Adjust the rect by the AA boundary padding
1742 left -= boundarySizeX;
1743 right += boundarySizeX;
1744 top -= boundarySizeY;
1745 bottom += boundarySizeY;
1746
1747 float width = right - left;
1748 float height = bottom - top;
1749
Romain Guy7b631422012-04-04 11:38:54 -07001750 int widthSlot;
1751 int lengthSlot;
1752
Chet Haase858aa932011-05-12 09:06:00 -07001753 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1754 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001755 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1756 boundaryWidthProportion, widthSlot, lengthSlot);
1757
Chet Haase858aa932011-05-12 09:06:00 -07001758 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1759 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1760 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001761 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion));
Chet Haase858aa932011-05-12 09:06:00 -07001762
1763 if (!quickReject(left, top, right, bottom)) {
1764 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1765 AAVertex::set(aaVertices++, left, top, 1, 0);
1766 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1767 AAVertex::set(aaVertices++, right, top, 0, 0);
1768 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1769 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1770 }
Romain Guy7b631422012-04-04 11:38:54 -07001771
1772 finishDrawAALine(widthSlot, lengthSlot);
Chet Haase858aa932011-05-12 09:06:00 -07001773}
1774
1775/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001776 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1777 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1778 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1779 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1780 * of the line. Hairlines are more involved because we need to account for transform scaling
1781 * to end up with a one-pixel-wide line in screen space..
1782 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1783 * in combination with values that we calculate and pass down in this method. The basic approach
1784 * is that the quad we create contains both the core line area plus a bounding area in which
1785 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1786 * proportion of the width and the length of a given segment is represented by the boundary
1787 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1788 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1789 * on the inside). This ends up giving the result we want, with pixels that are completely
1790 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1791 * how far into the boundary region they are, which is determined by shader interpolation.
1792 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001793void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1794 if (mSnapshot->isIgnored()) return;
1795
1796 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001797 // We use half the stroke width here because we're going to position the quad
1798 // corner vertices half of the width away from the line endpoints
1799 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001800 // A stroke width of 0 has a special meaning in Skia:
1801 // it draws a line 1 px wide regardless of current transform
1802 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001803
Chet Haase99ecdc42011-05-06 12:06:34 -07001804 float inverseScaleX = 1.0f;
1805 float inverseScaleY = 1.0f;
1806 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001807
Chet Haase8a5cc922011-04-26 07:28:09 -07001808 int alpha;
1809 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001810
Chet Haase8a5cc922011-04-26 07:28:09 -07001811 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001812 int verticesCount = count;
1813 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001814 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001815 verticesCount += (count - 4);
1816 }
1817
1818 if (isHairLine || isAA) {
1819 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1820 // the line on the screen should always be one pixel wide regardless of scale. For
1821 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001822 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001823 Matrix4 *mat = mSnapshot->transform;
1824 float m00 = mat->data[Matrix4::kScaleX];
1825 float m01 = mat->data[Matrix4::kSkewY];
1826 float m02 = mat->data[2];
1827 float m10 = mat->data[Matrix4::kSkewX];
1828 float m11 = mat->data[Matrix4::kScaleX];
1829 float m12 = mat->data[6];
Romain Guy7b631422012-04-04 11:38:54 -07001830
Romain Guy211370f2012-02-01 16:10:55 -08001831 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1832 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001833
Chet Haase99ecdc42011-05-06 12:06:34 -07001834 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1835 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001836
Chet Haase99ecdc42011-05-06 12:06:34 -07001837 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1838 scaled = true;
1839 }
1840 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001841 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001842
1843 getAlphaAndMode(paint, &alpha, &mode);
1844 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001845 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001846 if (isAA) {
1847 setupDrawAALine();
1848 }
1849 setupDrawColor(paint->getColor(), alpha);
1850 setupDrawColorFilter();
1851 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001852 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001853 setupDrawProgram();
1854 setupDrawModelViewIdentity(true);
1855 setupDrawColorUniforms();
1856 setupDrawColorFilterUniforms();
1857 setupDrawShaderIdentityUniforms();
1858
1859 if (isHairLine) {
1860 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001861 halfStrokeWidth = isAA? 1 : .5;
1862 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001863 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001864 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001865 }
Romain Guy7b631422012-04-04 11:38:54 -07001866
1867 int widthSlot;
1868 int lengthSlot;
1869
Chet Haase5b0200b2011-04-13 17:58:08 -07001870 Vertex lines[verticesCount];
1871 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001872
Chet Haase99585ad2011-05-02 15:00:16 -07001873 AAVertex wLines[verticesCount];
1874 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001875
Romain Guy211370f2012-02-01 16:10:55 -08001876 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001877 setupDrawVertices(vertices);
1878 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001879 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1880 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001881 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001882 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1883 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001884 // We will need to calculate the actual width proportion on each segment for
1885 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1886 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001887 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1888 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001889 }
1890
Chet Haase99ecdc42011-05-06 12:06:34 -07001891 AAVertex* prevAAVertex = NULL;
1892 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001893
Chet Haase99585ad2011-05-02 15:00:16 -07001894 int boundaryLengthSlot = -1;
1895 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001896 int boundaryWidthSlot = -1;
1897 int inverseBoundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001898
Chet Haase5b0200b2011-04-13 17:58:08 -07001899 for (int i = 0; i < count; i += 4) {
1900 // a = start point, b = end point
1901 vec2 a(points[i], points[i + 1]);
1902 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001903
Chet Haase99585ad2011-05-02 15:00:16 -07001904 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001905 float boundaryLengthProportion = 0;
1906 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001907
Chet Haase5b0200b2011-04-13 17:58:08 -07001908 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001909 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001910 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001911 if (isAA) {
1912 float wideningFactor;
1913 if (fabs(n.x) >= fabs(n.y)) {
1914 wideningFactor = fabs(1.0f / n.x);
1915 } else {
1916 wideningFactor = fabs(1.0f / n.y);
1917 }
1918 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001919 }
Romain Guy7b631422012-04-04 11:38:54 -07001920
Chet Haase99ecdc42011-05-06 12:06:34 -07001921 if (scaled) {
1922 n.x *= inverseScaleX;
1923 n.y *= inverseScaleY;
1924 }
1925 } else if (scaled) {
1926 // Extend n by .5 pixel on each side, post-transform
1927 vec2 extendedN = n.copyNormalized();
1928 extendedN /= 2;
1929 extendedN.x *= inverseScaleX;
1930 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07001931
Chet Haase99ecdc42011-05-06 12:06:34 -07001932 float extendedNLength = extendedN.length();
1933 // We need to set this value on the shader prior to drawing
1934 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1935 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001936 }
Romain Guy7b631422012-04-04 11:38:54 -07001937
Chet Haase5b0200b2011-04-13 17:58:08 -07001938 float x = n.x;
1939 n.x = -n.y;
1940 n.y = x;
1941
Chet Haase99585ad2011-05-02 15:00:16 -07001942 // aa lines expand the endpoint vertices to encompass the AA boundary
1943 if (isAA) {
1944 vec2 abVector = (b - a);
1945 length = abVector.length();
1946 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07001947
Chet Haase99ecdc42011-05-06 12:06:34 -07001948 if (scaled) {
1949 abVector.x *= inverseScaleX;
1950 abVector.y *= inverseScaleY;
1951 float abLength = abVector.length();
1952 boundaryLengthProportion = abLength / (length + abLength);
1953 } else {
1954 boundaryLengthProportion = .5 / (length + 1);
1955 }
Romain Guy7b631422012-04-04 11:38:54 -07001956
Chet Haase99ecdc42011-05-06 12:06:34 -07001957 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001958 a -= abVector;
1959 b += abVector;
1960 }
1961
Chet Haase5b0200b2011-04-13 17:58:08 -07001962 // Four corners of the rectangle defining a thick line
1963 vec2 p1 = a - n;
1964 vec2 p2 = a + n;
1965 vec2 p3 = b + n;
1966 vec2 p4 = b - n;
1967
Chet Haase99585ad2011-05-02 15:00:16 -07001968
Chet Haase5b0200b2011-04-13 17:58:08 -07001969 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1970 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1971 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1972 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1973
1974 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001975 if (!isAA) {
1976 if (prevVertex != NULL) {
1977 // Issue two repeat vertices to create degenerate triangles to bridge
1978 // between the previous line and the new one. This is necessary because
1979 // we are creating a single triangle_strip which will contain
1980 // potentially discontinuous line segments.
1981 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1982 Vertex::set(vertices++, p1.x, p1.y);
1983 generatedVerticesCount += 2;
1984 }
Romain Guy7b631422012-04-04 11:38:54 -07001985
Chet Haase5b0200b2011-04-13 17:58:08 -07001986 Vertex::set(vertices++, p1.x, p1.y);
1987 Vertex::set(vertices++, p2.x, p2.y);
1988 Vertex::set(vertices++, p4.x, p4.y);
1989 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07001990
Chet Haase5b0200b2011-04-13 17:58:08 -07001991 prevVertex = vertices - 1;
1992 generatedVerticesCount += 4;
1993 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001994 if (!isHairLine && scaled) {
1995 // Must set width proportions per-segment for scaled non-hairlines to use the
1996 // correct AA boundary dimensions
1997 if (boundaryWidthSlot < 0) {
1998 boundaryWidthSlot =
1999 mCaches.currentProgram->getUniform("boundaryWidth");
2000 inverseBoundaryWidthSlot =
2001 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
2002 }
Romain Guy7b631422012-04-04 11:38:54 -07002003
Chet Haase99ecdc42011-05-06 12:06:34 -07002004 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
2005 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
2006 }
Romain Guy7b631422012-04-04 11:38:54 -07002007
Chet Haase99585ad2011-05-02 15:00:16 -07002008 if (boundaryLengthSlot < 0) {
2009 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
2010 inverseBoundaryLengthSlot =
2011 mCaches.currentProgram->getUniform("inverseBoundaryLength");
2012 }
Romain Guy7b631422012-04-04 11:38:54 -07002013
Chet Haase99ecdc42011-05-06 12:06:34 -07002014 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
2015 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07002016
Chet Haase5b0200b2011-04-13 17:58:08 -07002017 if (prevAAVertex != NULL) {
2018 // Issue two repeat vertices to create degenerate triangles to bridge
2019 // between the previous line and the new one. This is necessary because
2020 // we are creating a single triangle_strip which will contain
2021 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002022 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2023 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2024 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002025 generatedVerticesCount += 2;
2026 }
Romain Guy7b631422012-04-04 11:38:54 -07002027
Chet Haase99585ad2011-05-02 15:00:16 -07002028 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2029 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2030 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2031 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002032
Chet Haase5b0200b2011-04-13 17:58:08 -07002033 prevAAVertex = aaVertices - 1;
2034 generatedVerticesCount += 4;
2035 }
Romain Guy7b631422012-04-04 11:38:54 -07002036
Chet Haase99585ad2011-05-02 15:00:16 -07002037 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2038 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2039 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002040 }
2041 }
Romain Guy7b631422012-04-04 11:38:54 -07002042
Chet Haase5b0200b2011-04-13 17:58:08 -07002043 if (generatedVerticesCount > 0) {
2044 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2045 }
Romain Guy7b631422012-04-04 11:38:54 -07002046
2047 if (isAA) {
2048 finishDrawAALine(widthSlot, lengthSlot);
2049 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002050}
2051
Romain Guyed6fcb02011-03-21 13:11:28 -07002052void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2053 if (mSnapshot->isIgnored()) return;
2054
2055 // TODO: The paint's cap style defines whether the points are square or circular
2056 // TODO: Handle AA for round points
2057
Chet Haase5b0200b2011-04-13 17:58:08 -07002058 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002059 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002060 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002061 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002062 if (isHairLine) {
2063 // Now that we know it's hairline, we can set the effective width, to be used later
2064 strokeWidth = 1.0f;
2065 }
2066 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002067 int alpha;
2068 SkXfermode::Mode mode;
2069 getAlphaAndMode(paint, &alpha, &mode);
2070
2071 int verticesCount = count >> 1;
2072 int generatedVerticesCount = 0;
2073
2074 TextureVertex pointsData[verticesCount];
2075 TextureVertex* vertex = &pointsData[0];
2076
2077 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002078 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002079 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002080 setupDrawColor(paint->getColor(), alpha);
2081 setupDrawColorFilter();
2082 setupDrawShader();
2083 setupDrawBlending(mode);
2084 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002085 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002086 setupDrawColorUniforms();
2087 setupDrawColorFilterUniforms();
2088 setupDrawPointUniforms();
2089 setupDrawShaderIdentityUniforms();
2090 setupDrawMesh(vertex);
2091
2092 for (int i = 0; i < count; i += 2) {
2093 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2094 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002095
Chet Haase8a5cc922011-04-26 07:28:09 -07002096 float left = points[i] - halfWidth;
2097 float right = points[i] + halfWidth;
2098 float top = points[i + 1] - halfWidth;
2099 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002100
Chet Haase8a5cc922011-04-26 07:28:09 -07002101 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002102 }
2103
2104 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
2105}
2106
Romain Guy85bf02f2010-06-22 13:11:24 -07002107void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002108 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08002109 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002110
Romain Guyae88e5e2010-10-22 17:49:18 -07002111 Rect& clip(*mSnapshot->clipRect);
2112 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002113
Romain Guy3d58c032010-07-14 16:34:53 -07002114 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07002115}
Romain Guy9d5316e2010-06-24 19:30:36 -07002116
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002117void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08002118 if (!texture) return;
2119 const AutoTexture autoCleanup(texture);
2120
2121 const float x = left + texture->left - texture->offset;
2122 const float y = top + texture->top - texture->offset;
2123
2124 drawPathTexture(texture, x, y, paint);
2125}
2126
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002127void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2128 float rx, float ry, SkPaint* paint) {
2129 if (mSnapshot->isIgnored()) return;
2130
Romain Guya1d3c912011-12-13 14:55:06 -08002131 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002132 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2133 right - left, bottom - top, rx, ry, paint);
2134 drawShape(left, top, texture, paint);
2135}
2136
Romain Guy01d58e42011-01-19 21:54:02 -08002137void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2138 if (mSnapshot->isIgnored()) return;
2139
Romain Guya1d3c912011-12-13 14:55:06 -08002140 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002141 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002142 drawShape(x - radius, y - radius, texture, paint);
2143}
Romain Guy01d58e42011-01-19 21:54:02 -08002144
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002145void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2146 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002147
Romain Guya1d3c912011-12-13 14:55:06 -08002148 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002149 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2150 drawShape(left, top, texture, paint);
2151}
2152
Romain Guy8b2f5262011-01-23 16:15:02 -08002153void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2154 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2155 if (mSnapshot->isIgnored()) return;
2156
2157 if (fabs(sweepAngle) >= 360.0f) {
2158 drawOval(left, top, right, bottom, paint);
2159 return;
2160 }
2161
Romain Guya1d3c912011-12-13 14:55:06 -08002162 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002163 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2164 startAngle, sweepAngle, useCenter, paint);
2165 drawShape(left, top, texture, paint);
2166}
2167
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002168void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2169 SkPaint* paint) {
2170 if (mSnapshot->isIgnored()) return;
2171
Romain Guya1d3c912011-12-13 14:55:06 -08002172 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002173 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2174 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002175}
2176
Chet Haase5c13d892010-10-08 08:37:55 -07002177void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002178 if (p->getStyle() != SkPaint::kFill_Style) {
2179 drawRectAsShape(left, top, right, bottom, p);
2180 return;
2181 }
2182
Romain Guy6926c722010-07-12 20:20:03 -07002183 if (quickReject(left, top, right, bottom)) {
2184 return;
2185 }
2186
Romain Guy026c5e162010-06-28 17:12:22 -07002187 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002188 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002189 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2190 if (!isMode) {
2191 // Assume SRC_OVER
2192 mode = SkXfermode::kSrcOver_Mode;
2193 }
2194 } else {
2195 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002196 }
2197
Romain Guy026c5e162010-06-28 17:12:22 -07002198 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002199 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002200 drawAARect(left, top, right, bottom, color, mode);
2201 } else {
2202 drawColorRect(left, top, right, bottom, color, mode);
2203 }
Romain Guyc7d53492010-06-25 13:41:57 -07002204}
Romain Guy9d5316e2010-06-24 19:30:36 -07002205
Romain Guyeb9a5362012-01-17 17:39:26 -08002206void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
2207 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002208 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2209 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guyeb9a5362012-01-17 17:39:26 -08002210 return;
2211 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002212
Romain Guy671d6cf2012-01-18 12:39:17 -08002213 // NOTE: Skia does not support perspective transform on drawPosText yet
2214 if (!mSnapshot->transform->isSimple()) {
2215 return;
2216 }
2217
2218 float x = 0.0f;
2219 float y = 0.0f;
2220 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2221 if (pureTranslate) {
2222 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2223 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2224 }
2225
2226 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2227 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2228 paint->getTextSize());
2229
2230 int alpha;
2231 SkXfermode::Mode mode;
2232 getAlphaAndMode(paint, &alpha, &mode);
2233
2234 // Pick the appropriate texture filtering
2235 bool linearFilter = mSnapshot->transform->changesBounds();
2236 if (pureTranslate && !linearFilter) {
2237 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2238 }
2239
2240 mCaches.activeTexture(0);
2241 setupDraw();
2242 setupDrawDirtyRegionsDisabled();
2243 setupDrawWithTexture(true);
2244 setupDrawAlpha8Color(paint->getColor(), alpha);
2245 setupDrawColorFilter();
2246 setupDrawShader();
2247 setupDrawBlending(true, mode);
2248 setupDrawProgram();
2249 setupDrawModelView(x, y, x, y, pureTranslate, true);
2250 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2251 setupDrawPureColorUniforms();
2252 setupDrawColorFilterUniforms();
2253 setupDrawShaderUniforms(pureTranslate);
2254
2255 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2256 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2257
2258#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002259 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002260#else
Romain Guy211370f2012-02-01 16:10:55 -08002261 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002262#endif
2263
2264 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2265 positions, hasActiveLayer ? &bounds : NULL)) {
2266#if RENDER_LAYERS_AS_REGIONS
2267 if (hasActiveLayer) {
2268 if (!pureTranslate) {
2269 mSnapshot->transform->mapRect(bounds);
2270 }
2271 dirtyLayerUnchecked(bounds, getRegion());
2272 }
2273#endif
2274 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002275}
2276
Romain Guye8e62a42010-07-23 18:55:21 -07002277void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002278 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002279 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2280 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07002281 return;
2282 }
2283
Chet Haasea1cff502012-02-21 13:43:44 -08002284 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002285 switch (paint->getTextAlign()) {
2286 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002287 x -= length / 2.0f;
2288 break;
2289 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002290 x -= length;
2291 break;
2292 default:
2293 break;
2294 }
2295
Romain Guycac5fd32011-12-01 20:08:50 -08002296 SkPaint::FontMetrics metrics;
2297 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002298 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Romain Guycac5fd32011-12-01 20:08:50 -08002299 return;
2300 }
2301
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002302 const float oldX = x;
2303 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002304 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002305 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002306 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2307 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2308 }
2309
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002310#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002311 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002312#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002313
2314 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002315 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002316 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002317
Romain Guy86568192010-12-14 15:55:39 -08002318 int alpha;
2319 SkXfermode::Mode mode;
2320 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002321
Romain Guy211370f2012-02-01 16:10:55 -08002322 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002323 mCaches.activeTexture(0);
2324
Romain Guyb45c0c92010-08-26 20:35:23 -07002325 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002326 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2327 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002328 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002329
Romain Guy740bf2b2011-04-26 15:33:10 -07002330 const float sx = oldX - shadow->left + mShadowDx;
2331 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002332
Romain Guy86568192010-12-14 15:55:39 -08002333 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002334 int shadowColor = mShadowColor;
2335 if (mShader) {
2336 shadowColor = 0xffffffff;
2337 }
Romain Guy86568192010-12-14 15:55:39 -08002338
Romain Guy86568192010-12-14 15:55:39 -08002339 setupDraw();
2340 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002341 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2342 setupDrawColorFilter();
2343 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002344 setupDrawBlending(true, mode);
2345 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002346 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002347 setupDrawTexture(shadow->id);
2348 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002349 setupDrawColorFilterUniforms();
2350 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002351 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2352
Romain Guy0a417492010-08-16 20:26:20 -07002353 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002354 }
2355
Romain Guy6620c6d2010-12-06 18:07:02 -08002356 // Pick the appropriate texture filtering
2357 bool linearFilter = mSnapshot->transform->changesBounds();
2358 if (pureTranslate && !linearFilter) {
2359 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2360 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002361
Romain Guya1d3c912011-12-13 14:55:06 -08002362 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002363 setupDraw();
2364 setupDrawDirtyRegionsDisabled();
2365 setupDrawWithTexture(true);
2366 setupDrawAlpha8Color(paint->getColor(), alpha);
2367 setupDrawColorFilter();
2368 setupDrawShader();
2369 setupDrawBlending(true, mode);
2370 setupDrawProgram();
2371 setupDrawModelView(x, y, x, y, pureTranslate, true);
2372 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2373 setupDrawPureColorUniforms();
2374 setupDrawColorFilterUniforms();
2375 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002376
Romain Guy6620c6d2010-12-06 18:07:02 -08002377 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002378 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2379
2380#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002381 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002382#else
Romain Guy211370f2012-02-01 16:10:55 -08002383 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002384#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002385
Romain Guy6620c6d2010-12-06 18:07:02 -08002386 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002387 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002388#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002389 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002390 if (!pureTranslate) {
2391 mSnapshot->transform->mapRect(bounds);
2392 }
Romain Guyf219da52011-01-16 12:54:25 -08002393 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002394 }
2395#endif
2396 }
Romain Guy694b5192010-07-21 21:33:20 -07002397
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002398 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002399}
2400
Romain Guy325740f2012-02-24 16:48:34 -08002401void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
2402 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002403 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2404 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
2405 return;
2406 }
2407
Romain Guy03d58522012-02-24 17:54:07 -08002408 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2409 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2410 paint->getTextSize());
2411
2412 int alpha;
2413 SkXfermode::Mode mode;
2414 getAlphaAndMode(paint, &alpha, &mode);
2415
2416 mCaches.activeTexture(0);
2417 setupDraw();
2418 setupDrawDirtyRegionsDisabled();
2419 setupDrawWithTexture(true);
2420 setupDrawAlpha8Color(paint->getColor(), alpha);
2421 setupDrawColorFilter();
2422 setupDrawShader();
2423 setupDrawBlending(true, mode);
2424 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002425 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002426 setupDrawTexture(fontRenderer.getTexture(true));
2427 setupDrawPureColorUniforms();
2428 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002429 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002430
Romain Guy97771732012-02-28 18:17:02 -08002431 const Rect* clip = &mSnapshot->getLocalClip();
2432 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 -08002433
Romain Guy97771732012-02-28 18:17:02 -08002434#if RENDER_LAYERS_AS_REGIONS
2435 const bool hasActiveLayer = hasLayer();
2436#else
2437 const bool hasActiveLayer = false;
2438#endif
2439
2440 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2441 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2442#if RENDER_LAYERS_AS_REGIONS
2443 if (hasActiveLayer) {
2444 mSnapshot->transform->mapRect(bounds);
2445 dirtyLayerUnchecked(bounds, getRegion());
2446 }
2447#endif
2448 }
Romain Guy325740f2012-02-24 16:48:34 -08002449}
2450
Romain Guy7fbcc042010-08-04 15:40:07 -07002451void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002452 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002453
Romain Guya1d3c912011-12-13 14:55:06 -08002454 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002455
Romain Guy33f6beb2012-02-16 19:24:51 -08002456 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002457 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002458 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002459 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002460
Romain Guy8b55f372010-08-18 17:10:07 -07002461 const float x = texture->left - texture->offset;
2462 const float y = texture->top - texture->offset;
2463
Romain Guy01d58e42011-01-19 21:54:02 -08002464 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002465}
2466
Romain Guyada830f2011-01-13 12:13:20 -08002467void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2468 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002469 return;
2470 }
2471
Romain Guy2bf68f02012-03-02 13:37:47 -08002472 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2473 OpenGLRenderer* renderer = layer->renderer;
2474 Rect& dirty = layer->dirtyRect;
2475
2476 interrupt();
2477 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2478 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002479 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002480 renderer->finish();
2481 resume();
2482
2483 dirty.setEmpty();
2484 layer->deferredUpdateScheduled = false;
2485 layer->renderer = NULL;
2486 layer->displayList = NULL;
2487 }
2488
Romain Guya1d3c912011-12-13 14:55:06 -08002489 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002490
2491 int alpha;
2492 SkXfermode::Mode mode;
2493 getAlphaAndMode(paint, &alpha, &mode);
2494
Romain Guy9ace8f52011-07-07 20:50:11 -07002495 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002496
Romain Guyf219da52011-01-16 12:54:25 -08002497#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002498 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002499 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002500 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002501 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002502 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002503 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002504
Romain Guyc88e3572011-01-22 00:32:12 -08002505 setupDraw();
2506 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002507 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002508 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002509 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002510 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002511 setupDrawPureColorUniforms();
2512 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002513 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002514 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002515 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2516 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2517
Romain Guyd21b6e12011-11-30 20:21:23 -08002518 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002519 setupDrawModelViewTranslate(x, y,
2520 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2521 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002522 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002523 setupDrawModelViewTranslate(x, y,
2524 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2525 }
Romain Guyc88e3572011-01-22 00:32:12 -08002526 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002527
Romain Guyc88e3572011-01-22 00:32:12 -08002528 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2529 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002530
Romain Guyc88e3572011-01-22 00:32:12 -08002531 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002532
2533#if DEBUG_LAYERS_AS_REGIONS
2534 drawRegionRects(layer->region);
2535#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002536 }
Romain Guyf219da52011-01-16 12:54:25 -08002537 }
2538#else
Romain Guyada830f2011-01-13 12:13:20 -08002539 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2540 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002541#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002542}
2543
Romain Guy6926c722010-07-12 20:20:03 -07002544///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002545// Shaders
2546///////////////////////////////////////////////////////////////////////////////
2547
2548void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002549 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002550}
2551
Romain Guy06f96e22010-07-30 19:18:16 -07002552void OpenGLRenderer::setupShader(SkiaShader* shader) {
2553 mShader = shader;
2554 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002555 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002556 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002557}
2558
Romain Guyd27977d2010-07-14 19:18:51 -07002559///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002560// Color filters
2561///////////////////////////////////////////////////////////////////////////////
2562
2563void OpenGLRenderer::resetColorFilter() {
2564 mColorFilter = NULL;
2565}
2566
2567void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2568 mColorFilter = filter;
2569}
2570
2571///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002572// Drop shadow
2573///////////////////////////////////////////////////////////////////////////////
2574
2575void OpenGLRenderer::resetShadow() {
2576 mHasShadow = false;
2577}
2578
2579void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2580 mHasShadow = true;
2581 mShadowRadius = radius;
2582 mShadowDx = dx;
2583 mShadowDy = dy;
2584 mShadowColor = color;
2585}
2586
2587///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002588// Draw filters
2589///////////////////////////////////////////////////////////////////////////////
2590
2591void OpenGLRenderer::resetPaintFilter() {
2592 mHasDrawFilter = false;
2593}
2594
2595void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2596 mHasDrawFilter = true;
2597 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2598 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2599}
2600
2601SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002602 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002603
2604 uint32_t flags = paint->getFlags();
2605
2606 mFilteredPaint = *paint;
2607 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2608
2609 return &mFilteredPaint;
2610}
2611
2612///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002613// Drawing implementation
2614///////////////////////////////////////////////////////////////////////////////
2615
Romain Guy01d58e42011-01-19 21:54:02 -08002616void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2617 float x, float y, SkPaint* paint) {
2618 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2619 return;
2620 }
2621
2622 int alpha;
2623 SkXfermode::Mode mode;
2624 getAlphaAndMode(paint, &alpha, &mode);
2625
2626 setupDraw();
2627 setupDrawWithTexture(true);
2628 setupDrawAlpha8Color(paint->getColor(), alpha);
2629 setupDrawColorFilter();
2630 setupDrawShader();
2631 setupDrawBlending(true, mode);
2632 setupDrawProgram();
2633 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2634 setupDrawTexture(texture->id);
2635 setupDrawPureColorUniforms();
2636 setupDrawColorFilterUniforms();
2637 setupDrawShaderUniforms();
2638 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2639
2640 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2641
2642 finishDrawTexture();
2643}
2644
Romain Guyf607bdc2010-09-10 19:20:06 -07002645// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002646#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2647#define kStdUnderline_Offset (1.0f / 9.0f)
2648#define kStdUnderline_Thickness (1.0f / 18.0f)
2649
2650void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2651 float x, float y, SkPaint* paint) {
2652 // Handle underline and strike-through
2653 uint32_t flags = paint->getFlags();
2654 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002655 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002656 float underlineWidth = length;
2657 // If length is > 0.0f, we already measured the text for the text alignment
2658 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002659 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002660 }
2661
2662 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002663 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002664 case SkPaint::kCenter_Align:
2665 offsetX = underlineWidth * 0.5f;
2666 break;
2667 case SkPaint::kRight_Align:
2668 offsetX = underlineWidth;
2669 break;
2670 default:
2671 break;
2672 }
2673
Romain Guy211370f2012-02-01 16:10:55 -08002674 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002675 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002676 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002677
Romain Guye20ecbd2010-09-22 19:49:04 -07002678 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002679 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002680
Romain Guyf6834472011-01-23 13:32:12 -08002681 int linesCount = 0;
2682 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2683 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2684
2685 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002686 float points[pointsCount];
2687 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002688
2689 if (flags & SkPaint::kUnderlineText_Flag) {
2690 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002691 points[currentPoint++] = left;
2692 points[currentPoint++] = top;
2693 points[currentPoint++] = left + underlineWidth;
2694 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002695 }
2696
2697 if (flags & SkPaint::kStrikeThruText_Flag) {
2698 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002699 points[currentPoint++] = left;
2700 points[currentPoint++] = top;
2701 points[currentPoint++] = left + underlineWidth;
2702 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002703 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002704
Romain Guy726aeba2011-06-01 14:52:00 -07002705 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002706
Romain Guy726aeba2011-06-01 14:52:00 -07002707 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002708 }
2709 }
2710}
2711
Romain Guy026c5e162010-06-28 17:12:22 -07002712void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002713 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002714 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002715 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002716 color |= 0x00ffffff;
2717 }
2718
Romain Guy70ca14e2010-12-13 18:24:33 -08002719 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002720 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002721 setupDrawColor(color);
2722 setupDrawShader();
2723 setupDrawColorFilter();
2724 setupDrawBlending(mode);
2725 setupDrawProgram();
2726 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2727 setupDrawColorUniforms();
2728 setupDrawShaderUniforms(ignoreTransform);
2729 setupDrawColorFilterUniforms();
2730 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002731
Romain Guyc95c8d62010-09-17 15:31:32 -07002732 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2733}
2734
Romain Guy82ba8142010-07-09 13:25:56 -07002735void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002736 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002737 int alpha;
2738 SkXfermode::Mode mode;
2739 getAlphaAndMode(paint, &alpha, &mode);
2740
Romain Guyd21b6e12011-11-30 20:21:23 -08002741 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002742
Romain Guy211370f2012-02-01 16:10:55 -08002743 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002744 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2745 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2746
Romain Guyd21b6e12011-11-30 20:21:23 -08002747 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002748 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2749 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2750 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2751 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002752 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002753 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2754 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2755 GL_TRIANGLE_STRIP, gMeshCount);
2756 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002757}
2758
Romain Guybd6b79b2010-06-26 00:13:53 -07002759void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002760 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2761 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002762 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002763}
2764
2765void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002766 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002767 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002768 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002769
Romain Guy746b7402010-10-26 16:27:31 -07002770 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002771 setupDrawWithTexture();
2772 setupDrawColor(alpha, alpha, alpha, alpha);
2773 setupDrawColorFilter();
2774 setupDrawBlending(blend, mode, swapSrcDst);
2775 setupDrawProgram();
2776 if (!dirty) {
2777 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002778 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002779 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002780 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002781 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002782 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002783 }
Romain Guy86568192010-12-14 15:55:39 -08002784 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002785 setupDrawColorFilterUniforms();
2786 setupDrawTexture(texture);
2787 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002788
Romain Guy6820ac82010-09-15 18:11:50 -07002789 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002790
2791 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002792}
2793
Romain Guya5aed0d2010-09-09 14:42:43 -07002794void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002795 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002796 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002797
Romain Guy82ba8142010-07-09 13:25:56 -07002798 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002799 // These blend modes are not supported by OpenGL directly and have
2800 // to be implemented using shaders. Since the shader will perform
2801 // the blending, turn blending off here
2802 // If the blend mode cannot be implemented using shaders, fall
2803 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002804 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2805 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002806 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002807 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002808
Romain Guy82bc7a72012-01-03 14:13:39 -08002809 if (mCaches.blend) {
2810 glDisable(GL_BLEND);
2811 mCaches.blend = false;
2812 }
2813
2814 return;
2815 } else {
2816 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002817 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002818 }
2819
2820 if (!mCaches.blend) {
2821 glEnable(GL_BLEND);
2822 }
2823
2824 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2825 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2826
2827 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2828 glBlendFunc(sourceMode, destMode);
2829 mCaches.lastSrcMode = sourceMode;
2830 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002831 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002832 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002833 glDisable(GL_BLEND);
2834 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002835 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002836}
2837
Romain Guy889f8d12010-07-29 14:37:42 -07002838bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002839 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002840 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002841 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002842 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002843 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002844 }
Romain Guy6926c722010-07-12 20:20:03 -07002845 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002846}
2847
Romain Guy026c5e162010-06-28 17:12:22 -07002848void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002849 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002850 TextureVertex::setUV(v++, u1, v1);
2851 TextureVertex::setUV(v++, u2, v1);
2852 TextureVertex::setUV(v++, u1, v2);
2853 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002854}
2855
Chet Haase5c13d892010-10-08 08:37:55 -07002856void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002857 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002858 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002859
2860 // Skia draws using the color's alpha channel if < 255
2861 // Otherwise, it uses the paint's alpha
2862 int color = paint->getColor();
2863 *alpha = (color >> 24) & 0xFF;
2864 if (*alpha == 255) {
2865 *alpha = paint->getAlpha();
2866 }
2867 } else {
2868 *mode = SkXfermode::kSrcOver_Mode;
2869 *alpha = 255;
2870 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07002871 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002872}
2873
Romain Guya5aed0d2010-09-09 14:42:43 -07002874SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002875 SkXfermode::Mode resultMode;
2876 if (!SkXfermode::AsMode(mode, &resultMode)) {
2877 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002878 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002879 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002880}
2881
Romain Guy9d5316e2010-06-24 19:30:36 -07002882}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002883}; // namespace android