blob: f81e1977f8c6e1a427630a298bdd3b1612684b0d [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
Chris Craikc2c95432012-04-25 15:13:52 -0700270 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700271 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
272 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700273 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700274
Chris Craikc2c95432012-04-25 15:13:52 -0700275 if (result & DrawGlInfo::kStatusInvoke) {
276 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700277 }
278 }
279 }
280
281 return result;
282}
283
284status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800285 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800286 if (mDirtyClip) {
287 setScissorFromClip();
288 }
Romain Guyd643bb52011-03-01 14:55:21 -0800289
Romain Guy80911b82011-03-16 15:30:12 -0700290 Rect clip(*mSnapshot->clipRect);
291 clip.snapToPixelBoundaries();
292
Romain Guyd643bb52011-03-01 14:55:21 -0800293#if RENDER_LAYERS_AS_REGIONS
294 // Since we don't know what the functor will draw, let's dirty
295 // tne entire clip region
296 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800297 dirtyLayerUnchecked(clip, getRegion());
298 }
299#endif
300
Romain Guy08aa2cb2011-03-17 11:06:57 -0700301 DrawGlInfo info;
302 info.clipLeft = clip.left;
303 info.clipTop = clip.top;
304 info.clipRight = clip.right;
305 info.clipBottom = clip.bottom;
306 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700307 info.width = getSnapshot()->viewport.getWidth();
308 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700309 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700310
Romain Guy8f3b8e32012-03-27 16:33:45 -0700311 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800312
Romain Guy8f3b8e32012-03-27 16:33:45 -0700313 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700314 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800315 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700316
Chris Craik65924a32012-04-05 17:52:11 -0700317 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700318 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700319 }
Romain Guycabfcc12011-03-07 18:06:46 -0800320 }
321
Chet Haasedaf98e92011-01-10 14:10:36 -0800322 resume();
Romain Guy65549432012-03-26 16:45:05 -0700323 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800324}
325
Romain Guyf6a11b82010-06-23 17:47:49 -0700326///////////////////////////////////////////////////////////////////////////////
327// State management
328///////////////////////////////////////////////////////////////////////////////
329
Romain Guybb9524b2010-06-22 18:56:38 -0700330int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700331 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700332}
333
334int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700335 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700336}
337
338void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700339 if (mSaveCount > 1) {
340 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700341 }
Romain Guybb9524b2010-06-22 18:56:38 -0700342}
343
344void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700345 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700346
Romain Guy8fb95422010-08-17 18:38:51 -0700347 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700348 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700349 }
Romain Guybb9524b2010-06-22 18:56:38 -0700350}
351
Romain Guy8aef54f2010-09-01 15:13:49 -0700352int OpenGLRenderer::saveSnapshot(int flags) {
353 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700354 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700355}
356
357bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700358 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700359 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700360 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700361
Romain Guybd6b79b2010-06-26 00:13:53 -0700362 sp<Snapshot> current = mSnapshot;
363 sp<Snapshot> previous = mSnapshot->previous;
364
Romain Guyeb993562010-10-05 18:14:38 -0700365 if (restoreOrtho) {
366 Rect& r = previous->viewport;
367 glViewport(r.left, r.top, r.right, r.bottom);
368 mOrthoMatrix.load(current->orthoMatrix);
369 }
370
Romain Guy8b55f372010-08-18 17:10:07 -0700371 mSaveCount--;
372 mSnapshot = previous;
373
Romain Guy2542d192010-08-18 11:47:12 -0700374 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700375 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700376 }
Romain Guy2542d192010-08-18 11:47:12 -0700377
Romain Guy5ec99242010-11-03 16:19:08 -0700378 if (restoreLayer) {
379 composeLayer(current, previous);
380 }
381
Romain Guy2542d192010-08-18 11:47:12 -0700382 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700383}
384
Romain Guyf6a11b82010-06-23 17:47:49 -0700385///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700386// Layers
387///////////////////////////////////////////////////////////////////////////////
388
389int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700390 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700391 const GLuint previousFbo = mSnapshot->fbo;
392 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700393
Romain Guyaf636eb2010-12-09 17:47:21 -0800394 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700395 int alpha = 255;
396 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700397
Romain Guye45362c2010-11-03 19:58:32 -0700398 if (p) {
399 alpha = p->getAlpha();
400 if (!mCaches.extensions.hasFramebufferFetch()) {
401 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
402 if (!isMode) {
403 // Assume SRC_OVER
404 mode = SkXfermode::kSrcOver_Mode;
405 }
406 } else {
407 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700408 }
409 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700410 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700411 }
Romain Guyd55a8612010-06-28 17:42:46 -0700412
Romain Guydbc26d22010-10-11 17:58:29 -0700413 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
414 }
Romain Guyd55a8612010-06-28 17:42:46 -0700415
416 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700417}
418
419int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
420 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700421 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700422 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700423 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700424 SkPaint paint;
425 paint.setAlpha(alpha);
426 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700427 }
Romain Guyd55a8612010-06-28 17:42:46 -0700428}
Romain Guybd6b79b2010-06-26 00:13:53 -0700429
Romain Guy1c740bc2010-09-13 18:00:09 -0700430/**
431 * Layers are viewed by Skia are slightly different than layers in image editing
432 * programs (for instance.) When a layer is created, previously created layers
433 * and the frame buffer still receive every drawing command. For instance, if a
434 * layer is created and a shape intersecting the bounds of the layers and the
435 * framebuffer is draw, the shape will be drawn on both (unless the layer was
436 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
437 *
438 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
439 * texture. Unfortunately, this is inefficient as it requires every primitive to
440 * be drawn n + 1 times, where n is the number of active layers. In practice this
441 * means, for every primitive:
442 * - Switch active frame buffer
443 * - Change viewport, clip and projection matrix
444 * - Issue the drawing
445 *
446 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700447 * To avoid this, layers are implemented in a different way here, at least in the
448 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
449 * is set. When this flag is set we can redirect all drawing operations into a
450 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700451 *
452 * This implementation relies on the frame buffer being at least RGBA 8888. When
453 * a layer is created, only a texture is created, not an FBO. The content of the
454 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700455 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700456 * buffer and drawing continues as normal. This technique therefore treats the
457 * frame buffer as a scratch buffer for the layers.
458 *
459 * To compose the layers back onto the frame buffer, each layer texture
460 * (containing the original frame buffer data) is drawn as a simple quad over
461 * the frame buffer. The trick is that the quad is set as the composition
462 * destination in the blending equation, and the frame buffer becomes the source
463 * of the composition.
464 *
465 * Drawing layers with an alpha value requires an extra step before composition.
466 * An empty quad is drawn over the layer's region in the frame buffer. This quad
467 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
468 * quad is used to multiply the colors in the frame buffer. This is achieved by
469 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
470 * GL_ZERO, GL_SRC_ALPHA.
471 *
472 * Because glCopyTexImage2D() can be slow, an alternative implementation might
473 * be use to draw a single clipped layer. The implementation described above
474 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700475 *
476 * (1) The frame buffer is actually not cleared right away. To allow the GPU
477 * to potentially optimize series of calls to glCopyTexImage2D, the frame
478 * buffer is left untouched until the first drawing operation. Only when
479 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700480 */
Romain Guyd55a8612010-06-28 17:42:46 -0700481bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700482 float right, float bottom, int alpha, SkXfermode::Mode mode,
483 int flags, GLuint previousFbo) {
484 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700485 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700486
Romain Guyeb993562010-10-05 18:14:38 -0700487 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
488
Romain Guyf607bdc2010-09-10 19:20:06 -0700489 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700490 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700491 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700492 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700493
Romain Guyeb993562010-10-05 18:14:38 -0700494 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700495 if (bounds.intersect(*snapshot->clipRect)) {
496 // We cannot work with sub-pixels in this case
497 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700498
Romain Guyad37cd32011-03-15 11:12:25 -0700499 // When the layer is not an FBO, we may use glCopyTexImage so we
500 // need to make sure the layer does not extend outside the bounds
501 // of the framebuffer
502 if (!bounds.intersect(snapshot->previous->viewport)) {
503 bounds.setEmpty();
504 }
505 } else {
506 bounds.setEmpty();
507 }
Romain Guyeb993562010-10-05 18:14:38 -0700508 }
Romain Guybf434112010-09-16 14:40:17 -0700509
Romain Guy746b7402010-10-26 16:27:31 -0700510 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
511 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800512 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700513 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700514 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700515 }
516
517 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800518 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700519 return false;
520 }
Romain Guyf18fd992010-07-08 11:45:51 -0700521
Romain Guya1d3c912011-12-13 14:55:06 -0800522 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700523 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700524 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700525 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700526 }
527
Romain Guy9ace8f52011-07-07 20:50:11 -0700528 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700529 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700530 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
531 bounds.getWidth() / float(layer->getWidth()), 0.0f);
532 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700533 layer->setBlend(true);
Romain Guydda570202010-07-06 11:39:32 -0700534
Romain Guy8fb95422010-08-17 18:38:51 -0700535 // Save the layer in the snapshot
536 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700537 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700538
Romain Guyeb993562010-10-05 18:14:38 -0700539 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700540 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700541 } else {
542 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700543 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800544 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700545 if (layer->isEmpty()) {
546 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
547 bounds.left, snapshot->height - bounds.bottom,
548 layer->getWidth(), layer->getHeight(), 0);
549 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800550 } else {
551 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
552 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
553 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700554
Romain Guy54be1cd2011-06-13 19:04:27 -0700555 // Enqueue the buffer coordinates to clear the corresponding region later
556 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700557 }
Romain Guyeb993562010-10-05 18:14:38 -0700558 }
Romain Guyf86ef572010-07-01 11:05:42 -0700559
Romain Guyd55a8612010-06-28 17:42:46 -0700560 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700561}
562
Romain Guy5b3b3522010-10-27 18:57:51 -0700563bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
564 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700565 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700566
567#if RENDER_LAYERS_AS_REGIONS
568 snapshot->region = &snapshot->layer->region;
569 snapshot->flags |= Snapshot::kFlagFboTarget;
570#endif
571
572 Rect clip(bounds);
573 snapshot->transform->mapRect(clip);
574 clip.intersect(*snapshot->clipRect);
575 clip.snapToPixelBoundaries();
576 clip.intersect(snapshot->previous->viewport);
577
578 mat4 inverse;
579 inverse.loadInverse(*mSnapshot->transform);
580
581 inverse.mapRect(clip);
582 clip.snapToPixelBoundaries();
583 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700584 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700585
586 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700587 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700588 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700589 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
590 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
591 snapshot->height = bounds.getHeight();
592 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
593 snapshot->orthoMatrix.load(mOrthoMatrix);
594
595 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700596 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
597 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700598
599 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700600 if (layer->isEmpty()) {
601 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
602 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700603 }
604
605 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700606 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700607
608#if DEBUG_LAYERS_AS_REGIONS
609 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
610 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000611 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700612
613 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700614 layer->deleteTexture();
615 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700616
617 delete layer;
618
619 return false;
620 }
621#endif
622
623 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800624 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700625 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700626 glClear(GL_COLOR_BUFFER_BIT);
627
628 dirtyClip();
629
630 // Change the ortho projection
631 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
632 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
633
634 return true;
635}
636
Romain Guy1c740bc2010-09-13 18:00:09 -0700637/**
638 * Read the documentation of createLayer() before doing anything in this method.
639 */
Romain Guy1d83e192010-08-17 11:37:00 -0700640void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
641 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000642 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700643 return;
644 }
645
Romain Guy5b3b3522010-10-27 18:57:51 -0700646 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700647
648 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700649 // Detach the texture from the FBO
650 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
651
Romain Guyeb993562010-10-05 18:14:38 -0700652 // Unbind current FBO and restore previous one
653 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
654 }
655
Romain Guy1d83e192010-08-17 11:37:00 -0700656 Layer* layer = current->layer;
657 const Rect& rect = layer->layer;
658
Romain Guy9ace8f52011-07-07 20:50:11 -0700659 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700660 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700661 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700662 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700663 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700664 }
665
Romain Guy03750a02010-10-18 14:06:08 -0700666 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700667
Romain Guya1d3c912011-12-13 14:55:06 -0800668 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700669
Romain Guy5b3b3522010-10-27 18:57:51 -0700670 // When the layer is stored in an FBO, we can save a bit of fillrate by
671 // drawing only the dirty region
672 if (fboLayer) {
673 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700674 if (layer->getColorFilter()) {
675 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800676 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700677 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700678 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800679 resetColorFilter();
680 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700681 } else if (!rect.isEmpty()) {
682 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
683 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700684 }
Romain Guy8b55f372010-08-18 17:10:07 -0700685
Romain Guyeb993562010-10-05 18:14:38 -0700686 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800687 // Note: No need to use glDiscardFramebufferEXT() since we never
688 // create/compose layers that are not on screen with this
689 // code path
690 // See LayerRenderer::destroyLayer(Layer*)
691
Romain Guyeb993562010-10-05 18:14:38 -0700692 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
693 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700694 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700695 }
696
Romain Guy746b7402010-10-26 16:27:31 -0700697 dirtyClip();
698
Romain Guyeb993562010-10-05 18:14:38 -0700699 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700700 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700701 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700702 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700703 delete layer;
704 }
705}
706
Romain Guyaa6c24c2011-04-28 18:40:04 -0700707void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700708 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700709
Romain Guy302a9df2011-08-16 13:55:02 -0700710 mat4& transform = layer->getTransform();
711 if (!transform.isIdentity()) {
712 save(0);
713 mSnapshot->transform->multiply(transform);
714 }
715
Romain Guyaa6c24c2011-04-28 18:40:04 -0700716 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700717 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700718 setupDrawWithTexture();
719 } else {
720 setupDrawWithExternalTexture();
721 }
722 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700723 setupDrawColor(alpha, alpha, alpha, alpha);
724 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700725 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700726 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700727 setupDrawPureColorUniforms();
728 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700729 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
730 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700731 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700732 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700733 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700734 if (mSnapshot->transform->isPureTranslate() &&
735 layer->getWidth() == (uint32_t) rect.getWidth() &&
736 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700737 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
738 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
739
Romain Guyd21b6e12011-11-30 20:21:23 -0800740 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700741 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
742 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800743 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700744 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
745 }
746 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700747 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
748
749 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
750
751 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700752
753 if (!transform.isIdentity()) {
754 restore();
755 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700756}
757
Romain Guy5b3b3522010-10-27 18:57:51 -0700758void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700759 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700760 const Rect& texCoords = layer->texCoords;
761 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
762 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700763
Romain Guy9ace8f52011-07-07 20:50:11 -0700764 float x = rect.left;
765 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700766 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700767 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700768 layer->getHeight() == (uint32_t) rect.getHeight();
769
770 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700771 // When we're swapping, the layer is already in screen coordinates
772 if (!swap) {
773 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
774 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
775 }
776
Romain Guyd21b6e12011-11-30 20:21:23 -0800777 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700778 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800779 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700780 }
781
782 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
783 layer->getTexture(), layer->getAlpha() / 255.0f,
784 layer->getMode(), layer->isBlend(),
785 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
786 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700787
Romain Guyaa6c24c2011-04-28 18:40:04 -0700788 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
789 } else {
790 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
791 drawTextureLayer(layer, rect);
792 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
793 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700794}
795
796void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
797#if RENDER_LAYERS_AS_REGIONS
798 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700799 layer->setRegionAsRect();
800
Romain Guy40667672011-03-18 14:34:03 -0700801 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700802
Romain Guy5b3b3522010-10-27 18:57:51 -0700803 layer->region.clear();
804 return;
805 }
806
Romain Guy8a3957d2011-09-07 17:55:15 -0700807 // TODO: See LayerRenderer.cpp::generateMesh() for important
808 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800809 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700810 size_t count;
811 const android::Rect* rects = layer->region.getArray(&count);
812
Romain Guy9ace8f52011-07-07 20:50:11 -0700813 const float alpha = layer->getAlpha() / 255.0f;
814 const float texX = 1.0f / float(layer->getWidth());
815 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800816 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700817
818 TextureVertex* mesh = mCaches.getRegionMesh();
819 GLsizei numQuads = 0;
820
Romain Guy7230a742011-01-10 22:26:16 -0800821 setupDraw();
822 setupDrawWithTexture();
823 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800824 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700825 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800826 setupDrawProgram();
827 setupDrawDirtyRegionsDisabled();
828 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800829 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700830 setupDrawTexture(layer->getTexture());
831 if (mSnapshot->transform->isPureTranslate()) {
832 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
833 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
834
Romain Guyd21b6e12011-11-30 20:21:23 -0800835 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700836 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
837 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800838 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700839 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
840 }
Romain Guy15bc6432011-12-13 13:11:32 -0800841 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700842
843 for (size_t i = 0; i < count; i++) {
844 const android::Rect* r = &rects[i];
845
846 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800847 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700848 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800849 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700850
851 // TODO: Reject quads outside of the clip
852 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
853 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
854 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
855 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
856
857 numQuads++;
858
859 if (numQuads >= REGION_MESH_QUAD_COUNT) {
860 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
861 numQuads = 0;
862 mesh = mCaches.getRegionMesh();
863 }
864 }
865
866 if (numQuads > 0) {
867 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
868 }
869
Romain Guy7230a742011-01-10 22:26:16 -0800870 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700871
872#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800873 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700874#endif
875
876 layer->region.clear();
877 }
878#else
879 composeLayerRect(layer, rect);
880#endif
881}
882
Romain Guy3a3133d2011-02-01 22:59:58 -0800883void OpenGLRenderer::drawRegionRects(const Region& region) {
884#if DEBUG_LAYERS_AS_REGIONS
885 size_t count;
886 const android::Rect* rects = region.getArray(&count);
887
888 uint32_t colors[] = {
889 0x7fff0000, 0x7f00ff00,
890 0x7f0000ff, 0x7fff00ff,
891 };
892
893 int offset = 0;
894 int32_t top = rects[0].top;
895
896 for (size_t i = 0; i < count; i++) {
897 if (top != rects[i].top) {
898 offset ^= 0x2;
899 top = rects[i].top;
900 }
901
902 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
903 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
904 SkXfermode::kSrcOver_Mode);
905 }
906#endif
907}
908
Romain Guy5b3b3522010-10-27 18:57:51 -0700909void OpenGLRenderer::dirtyLayer(const float left, const float top,
910 const float right, const float bottom, const mat4 transform) {
911#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800912 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700913 Rect bounds(left, top, right, bottom);
914 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800915 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700916 }
917#endif
918}
919
920void OpenGLRenderer::dirtyLayer(const float left, const float top,
921 const float right, const float bottom) {
922#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800923 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700924 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800925 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800926 }
927#endif
928}
929
930void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
931#if RENDER_LAYERS_AS_REGIONS
932 if (bounds.intersect(*mSnapshot->clipRect)) {
933 bounds.snapToPixelBoundaries();
934 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
935 if (!dirty.isEmpty()) {
936 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700937 }
938 }
939#endif
940}
941
Romain Guy54be1cd2011-06-13 19:04:27 -0700942void OpenGLRenderer::clearLayerRegions() {
943 const size_t count = mLayers.size();
944 if (count == 0) return;
945
946 if (!mSnapshot->isIgnored()) {
947 // Doing several glScissor/glClear here can negatively impact
948 // GPUs with a tiler architecture, instead we draw quads with
949 // the Clear blending mode
950
951 // The list contains bounds that have already been clipped
952 // against their initial clip rect, and the current clip
953 // is likely different so we need to disable clipping here
954 glDisable(GL_SCISSOR_TEST);
955
956 Vertex mesh[count * 6];
957 Vertex* vertex = mesh;
958
959 for (uint32_t i = 0; i < count; i++) {
960 Rect* bounds = mLayers.itemAt(i);
961
962 Vertex::set(vertex++, bounds->left, bounds->bottom);
963 Vertex::set(vertex++, bounds->left, bounds->top);
964 Vertex::set(vertex++, bounds->right, bounds->top);
965 Vertex::set(vertex++, bounds->left, bounds->bottom);
966 Vertex::set(vertex++, bounds->right, bounds->top);
967 Vertex::set(vertex++, bounds->right, bounds->bottom);
968
969 delete bounds;
970 }
971
972 setupDraw(false);
973 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
974 setupDrawBlending(true, SkXfermode::kClear_Mode);
975 setupDrawProgram();
976 setupDrawPureColorUniforms();
977 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800978 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700979
Romain Guy54be1cd2011-06-13 19:04:27 -0700980 glDrawArrays(GL_TRIANGLES, 0, count * 6);
981
982 glEnable(GL_SCISSOR_TEST);
983 } else {
984 for (uint32_t i = 0; i < count; i++) {
985 delete mLayers.itemAt(i);
986 }
987 }
988
989 mLayers.clear();
990}
991
Romain Guybd6b79b2010-06-26 00:13:53 -0700992///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700993// Transforms
994///////////////////////////////////////////////////////////////////////////////
995
996void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700997 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700998}
999
1000void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001001 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001002}
1003
1004void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001005 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001006}
1007
Romain Guy807daf72011-01-18 11:19:19 -08001008void OpenGLRenderer::skew(float sx, float sy) {
1009 mSnapshot->transform->skew(sx, sy);
1010}
1011
Romain Guyf6a11b82010-06-23 17:47:49 -07001012void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001013 if (matrix) {
1014 mSnapshot->transform->load(*matrix);
1015 } else {
1016 mSnapshot->transform->loadIdentity();
1017 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001018}
1019
1020void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001021 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001022}
1023
1024void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001025 SkMatrix transform;
1026 mSnapshot->transform->copyTo(transform);
1027 transform.preConcat(*matrix);
1028 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001029}
1030
1031///////////////////////////////////////////////////////////////////////////////
1032// Clipping
1033///////////////////////////////////////////////////////////////////////////////
1034
Romain Guybb9524b2010-06-22 18:56:38 -07001035void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001036 Rect clip(*mSnapshot->clipRect);
1037 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001038
1039 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1040 clip.getWidth(), clip.getHeight());
1041
Romain Guy746b7402010-10-26 16:27:31 -07001042 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -07001043}
1044
1045const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001046 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001047}
1048
Romain Guyc7d53492010-06-25 13:41:57 -07001049bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001050 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001051 return true;
1052 }
1053
Romain Guy1d83e192010-08-17 11:37:00 -07001054 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001055 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001056 r.snapToPixelBoundaries();
1057
1058 Rect clipRect(*mSnapshot->clipRect);
1059 clipRect.snapToPixelBoundaries();
1060
1061 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001062}
1063
Romain Guy079ba2c2010-07-16 14:12:24 -07001064bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1065 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001066 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001067 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001068 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001069 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001070}
1071
Chet Haasea23eed82012-04-12 15:19:04 -07001072Rect* OpenGLRenderer::getClipRect() {
1073 return mSnapshot->clipRect;
1074}
1075
Romain Guyf6a11b82010-06-23 17:47:49 -07001076///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001077// Drawing commands
1078///////////////////////////////////////////////////////////////////////////////
1079
Romain Guy54be1cd2011-06-13 19:04:27 -07001080void OpenGLRenderer::setupDraw(bool clear) {
1081 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001082 if (mDirtyClip) {
1083 setScissorFromClip();
1084 }
1085 mDescription.reset();
1086 mSetShaderColor = false;
1087 mColorSet = false;
1088 mColorA = mColorR = mColorG = mColorB = 0.0f;
1089 mTextureUnit = 0;
1090 mTrackDirtyRegions = true;
1091}
1092
1093void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1094 mDescription.hasTexture = true;
1095 mDescription.hasAlpha8Texture = isAlpha8;
1096}
1097
Romain Guyaa6c24c2011-04-28 18:40:04 -07001098void OpenGLRenderer::setupDrawWithExternalTexture() {
1099 mDescription.hasExternalTexture = true;
1100}
1101
Romain Guy15bc6432011-12-13 13:11:32 -08001102void OpenGLRenderer::setupDrawNoTexture() {
1103 mCaches.disbaleTexCoordsVertexArray();
1104}
1105
Chet Haase5b0200b2011-04-13 17:58:08 -07001106void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001107 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001108}
1109
Romain Guyed6fcb02011-03-21 13:11:28 -07001110void OpenGLRenderer::setupDrawPoint(float pointSize) {
1111 mDescription.isPoint = true;
1112 mDescription.pointSize = pointSize;
1113}
1114
Romain Guy70ca14e2010-12-13 18:24:33 -08001115void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001116 setupDrawColor(color, (color >> 24) & 0xFF);
1117}
1118
1119void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1120 mColorA = alpha / 255.0f;
Chet Haasedb8c9a62012-03-21 18:54:18 -07001121 mColorA *= mSnapshot->alpha;
Chet Haase6cfdf452011-04-22 16:42:10 -07001122 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1123 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001124 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001125 mColorR = a * ((color >> 16) & 0xFF);
1126 mColorG = a * ((color >> 8) & 0xFF);
1127 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001128 mColorSet = true;
1129 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1130}
1131
Romain Guy86568192010-12-14 15:55:39 -08001132void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1133 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001134 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1135 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001136 const float a = mColorA / 255.0f;
1137 mColorR = a * ((color >> 16) & 0xFF);
1138 mColorG = a * ((color >> 8) & 0xFF);
1139 mColorB = a * ((color ) & 0xFF);
1140 mColorSet = true;
1141 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1142}
1143
Romain Guy70ca14e2010-12-13 18:24:33 -08001144void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1145 mColorA = a;
1146 mColorR = r;
1147 mColorG = g;
1148 mColorB = b;
1149 mColorSet = true;
1150 mSetShaderColor = mDescription.setColor(r, g, b, a);
1151}
1152
Romain Guy86568192010-12-14 15:55:39 -08001153void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1154 mColorA = a;
1155 mColorR = r;
1156 mColorG = g;
1157 mColorB = b;
1158 mColorSet = true;
1159 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1160}
1161
Romain Guy70ca14e2010-12-13 18:24:33 -08001162void OpenGLRenderer::setupDrawShader() {
1163 if (mShader) {
1164 mShader->describe(mDescription, mCaches.extensions);
1165 }
1166}
1167
1168void OpenGLRenderer::setupDrawColorFilter() {
1169 if (mColorFilter) {
1170 mColorFilter->describe(mDescription, mCaches.extensions);
1171 }
1172}
1173
Romain Guyf09ef512011-05-27 11:43:46 -07001174void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1175 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1176 mColorA = 1.0f;
1177 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001178 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001179 }
1180}
1181
Romain Guy70ca14e2010-12-13 18:24:33 -08001182void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001183 // When the blending mode is kClear_Mode, we need to use a modulate color
1184 // argb=1,0,0,0
1185 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001186 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1187 mDescription, swapSrcDst);
1188}
1189
1190void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001191 // When the blending mode is kClear_Mode, we need to use a modulate color
1192 // argb=1,0,0,0
1193 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001194 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1195 mDescription, swapSrcDst);
1196}
1197
1198void OpenGLRenderer::setupDrawProgram() {
1199 useProgram(mCaches.programCache.get(mDescription));
1200}
1201
1202void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1203 mTrackDirtyRegions = false;
1204}
1205
1206void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1207 bool ignoreTransform) {
1208 mModelView.loadTranslate(left, top, 0.0f);
1209 if (!ignoreTransform) {
1210 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1211 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1212 } else {
1213 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1214 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1215 }
1216}
1217
Chet Haase8a5cc922011-04-26 07:28:09 -07001218void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1219 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001220}
1221
Romain Guy70ca14e2010-12-13 18:24:33 -08001222void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1223 bool ignoreTransform, bool ignoreModelView) {
1224 if (!ignoreModelView) {
1225 mModelView.loadTranslate(left, top, 0.0f);
1226 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001227 } else {
1228 mModelView.loadIdentity();
1229 }
Romain Guy86568192010-12-14 15:55:39 -08001230 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1231 if (!ignoreTransform) {
1232 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1233 if (mTrackDirtyRegions && dirty) {
1234 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1235 }
1236 } else {
1237 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1238 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1239 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001240}
1241
Romain Guyed6fcb02011-03-21 13:11:28 -07001242void OpenGLRenderer::setupDrawPointUniforms() {
1243 int slot = mCaches.currentProgram->getUniform("pointSize");
1244 glUniform1f(slot, mDescription.pointSize);
1245}
1246
Romain Guy70ca14e2010-12-13 18:24:33 -08001247void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001248 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001249 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1250 }
1251}
1252
Romain Guy86568192010-12-14 15:55:39 -08001253void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001254 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001255 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001256 }
1257}
1258
Romain Guy70ca14e2010-12-13 18:24:33 -08001259void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1260 if (mShader) {
1261 if (ignoreTransform) {
1262 mModelView.loadInverse(*mSnapshot->transform);
1263 }
1264 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1265 }
1266}
1267
Romain Guy8d0d4782010-12-14 20:13:35 -08001268void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1269 if (mShader) {
1270 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1271 }
1272}
1273
Romain Guy70ca14e2010-12-13 18:24:33 -08001274void OpenGLRenderer::setupDrawColorFilterUniforms() {
1275 if (mColorFilter) {
1276 mColorFilter->setupProgram(mCaches.currentProgram);
1277 }
1278}
1279
1280void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001281 bool force = mCaches.bindMeshBuffer();
1282 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001283 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001284}
1285
1286void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1287 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001288 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001289 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001290}
1291
Romain Guyaa6c24c2011-04-28 18:40:04 -07001292void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1293 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001294 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001295 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001296}
1297
Romain Guy8f0095c2011-05-02 17:24:22 -07001298void OpenGLRenderer::setupDrawTextureTransform() {
1299 mDescription.hasTextureTransform = true;
1300}
1301
1302void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001303 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1304 GL_FALSE, &transform.data[0]);
1305}
1306
Romain Guy70ca14e2010-12-13 18:24:33 -08001307void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001308 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001309 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001310 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001311 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001312 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001313 }
Romain Guyd71dd362011-12-12 19:03:35 -08001314
Romain Guyf3a910b42011-12-12 20:35:21 -08001315 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001316 if (mCaches.currentProgram->texCoords >= 0) {
1317 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1318 }
1319
1320 mCaches.unbindIndicesBuffer();
1321}
1322
1323void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1324 bool force = mCaches.unbindMeshBuffer();
1325 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1326 if (mCaches.currentProgram->texCoords >= 0) {
1327 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001328 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001329}
1330
Chet Haase5b0200b2011-04-13 17:58:08 -07001331void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001332 bool force = mCaches.unbindMeshBuffer();
1333 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1334 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001335 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001336}
1337
1338/**
1339 * 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 -07001340 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1341 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1342 * attributes (one per vertex) are values from zero to one that tells the fragment
1343 * shader where the fragment is in relation to the line width/length overall; these values are
1344 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1345 * region of the line.
1346 * Note that we only pass down the width values in this setup function. The length coordinates
1347 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001348 */
Chet Haase99585ad2011-05-02 15:00:16 -07001349void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001350 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001351 bool force = mCaches.unbindMeshBuffer();
1352 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1353 vertices, gAAVertexStride);
1354 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001355 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001356
Romain Guy7b631422012-04-04 11:38:54 -07001357 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001358 glEnableVertexAttribArray(widthSlot);
1359 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001360
Romain Guy7b631422012-04-04 11:38:54 -07001361 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001362 glEnableVertexAttribArray(lengthSlot);
1363 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001364
Chet Haase5b0200b2011-04-13 17:58:08 -07001365 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001366 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001367
Chet Haase99585ad2011-05-02 15:00:16 -07001368 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001369 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Romain Guy7b631422012-04-04 11:38:54 -07001370 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion);
1371}
1372
1373void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1374 glDisableVertexAttribArray(widthSlot);
1375 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001376}
1377
Romain Guy70ca14e2010-12-13 18:24:33 -08001378void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001379}
1380
1381///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001382// Drawing
1383///////////////////////////////////////////////////////////////////////////////
1384
Chet Haase1271e2c2012-04-20 09:54:27 -07001385status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001386 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001387
Romain Guy0fe478e2010-11-08 12:08:41 -08001388 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1389 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001390 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001391 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001392 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001393
Romain Guy65549432012-03-26 16:45:05 -07001394 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001395}
1396
Chet Haaseed30fd82011-04-22 16:18:45 -07001397void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1398 if (displayList) {
1399 displayList->output(*this, level);
1400 }
1401}
1402
Romain Guya168d732011-03-18 16:50:13 -07001403void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1404 int alpha;
1405 SkXfermode::Mode mode;
1406 getAlphaAndMode(paint, &alpha, &mode);
1407
Romain Guya168d732011-03-18 16:50:13 -07001408 float x = left;
1409 float y = top;
1410
Romain Guye3c26852011-07-25 16:36:01 -07001411 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001412 bool ignoreTransform = false;
1413 if (mSnapshot->transform->isPureTranslate()) {
1414 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1415 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1416 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001417 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001418 } else {
1419 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001420 }
1421
1422 setupDraw();
1423 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001424 if (paint) {
1425 setupDrawAlpha8Color(paint->getColor(), alpha);
1426 }
Romain Guya168d732011-03-18 16:50:13 -07001427 setupDrawColorFilter();
1428 setupDrawShader();
1429 setupDrawBlending(true, mode);
1430 setupDrawProgram();
1431 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001432
Romain Guya168d732011-03-18 16:50:13 -07001433 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001434 texture->setWrap(GL_CLAMP_TO_EDGE);
1435 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001436
Romain Guya168d732011-03-18 16:50:13 -07001437 setupDrawPureColorUniforms();
1438 setupDrawColorFilterUniforms();
1439 setupDrawShaderUniforms();
1440 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1441
1442 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1443
1444 finishDrawTexture();
1445}
1446
Chet Haase5c13d892010-10-08 08:37:55 -07001447void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001448 const float right = left + bitmap->width();
1449 const float bottom = top + bitmap->height();
1450
1451 if (quickReject(left, top, right, bottom)) {
1452 return;
1453 }
1454
Romain Guya1d3c912011-12-13 14:55:06 -08001455 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001456 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001457 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001458 const AutoTexture autoCleanup(texture);
1459
Romain Guy211370f2012-02-01 16:10:55 -08001460 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001461 drawAlphaBitmap(texture, left, top, paint);
1462 } else {
1463 drawTextureRect(left, top, right, bottom, texture, paint);
1464 }
Romain Guyce0537b2010-06-29 21:05:21 -07001465}
1466
Chet Haase5c13d892010-10-08 08:37:55 -07001467void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001468 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1469 const mat4 transform(*matrix);
1470 transform.mapRect(r);
1471
Romain Guy6926c722010-07-12 20:20:03 -07001472 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1473 return;
1474 }
1475
Romain Guya1d3c912011-12-13 14:55:06 -08001476 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001477 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001478 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001479 const AutoTexture autoCleanup(texture);
1480
Romain Guy5b3b3522010-10-27 18:57:51 -07001481 // This could be done in a cheaper way, all we need is pass the matrix
1482 // to the vertex shader. The save/restore is a bit overkill.
1483 save(SkCanvas::kMatrix_SaveFlag);
1484 concatMatrix(matrix);
1485 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1486 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001487}
1488
Romain Guy5a7b4662011-01-20 19:09:30 -08001489void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1490 float* vertices, int* colors, SkPaint* paint) {
1491 // TODO: Do a quickReject
1492 if (!vertices || mSnapshot->isIgnored()) {
1493 return;
1494 }
1495
Romain Guya1d3c912011-12-13 14:55:06 -08001496 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001497 Texture* texture = mCaches.textureCache.get(bitmap);
1498 if (!texture) return;
1499 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001500
Romain Guyd21b6e12011-11-30 20:21:23 -08001501 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1502 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001503
1504 int alpha;
1505 SkXfermode::Mode mode;
1506 getAlphaAndMode(paint, &alpha, &mode);
1507
Romain Guy5a7b4662011-01-20 19:09:30 -08001508 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001509
Romain Guyb18d2d02011-02-10 15:52:54 -08001510 float left = FLT_MAX;
1511 float top = FLT_MAX;
1512 float right = FLT_MIN;
1513 float bottom = FLT_MIN;
1514
1515#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001516 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001517#else
Romain Guy211370f2012-02-01 16:10:55 -08001518 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001519#endif
1520
Romain Guya566b7c2011-01-23 16:36:11 -08001521 // TODO: Support the colors array
1522 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001523 TextureVertex* vertex = mesh;
1524 for (int32_t y = 0; y < meshHeight; y++) {
1525 for (int32_t x = 0; x < meshWidth; x++) {
1526 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1527
1528 float u1 = float(x) / meshWidth;
1529 float u2 = float(x + 1) / meshWidth;
1530 float v1 = float(y) / meshHeight;
1531 float v2 = float(y + 1) / meshHeight;
1532
1533 int ax = i + (meshWidth + 1) * 2;
1534 int ay = ax + 1;
1535 int bx = i;
1536 int by = bx + 1;
1537 int cx = i + 2;
1538 int cy = cx + 1;
1539 int dx = i + (meshWidth + 1) * 2 + 2;
1540 int dy = dx + 1;
1541
1542 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1543 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1544 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1545
1546 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1547 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1548 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001549
1550#if RENDER_LAYERS_AS_REGIONS
1551 if (hasActiveLayer) {
1552 // TODO: This could be optimized to avoid unnecessary ops
1553 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1554 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1555 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1556 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1557 }
1558#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001559 }
1560 }
1561
Romain Guyb18d2d02011-02-10 15:52:54 -08001562#if RENDER_LAYERS_AS_REGIONS
1563 if (hasActiveLayer) {
1564 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1565 }
1566#endif
1567
Romain Guy5a7b4662011-01-20 19:09:30 -08001568 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1569 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001570 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001571}
1572
Romain Guy8ba548f2010-06-30 19:21:21 -07001573void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1574 float srcLeft, float srcTop, float srcRight, float srcBottom,
1575 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001576 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001577 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1578 return;
1579 }
1580
Romain Guya1d3c912011-12-13 14:55:06 -08001581 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001582 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001583 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001584 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001585
Romain Guy8ba548f2010-06-30 19:21:21 -07001586 const float width = texture->width;
1587 const float height = texture->height;
1588
Romain Guy68169722011-08-22 17:33:33 -07001589 const float u1 = fmax(0.0f, srcLeft / width);
1590 const float v1 = fmax(0.0f, srcTop / height);
1591 const float u2 = fmin(1.0f, srcRight / width);
1592 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001593
Romain Guy03750a02010-10-18 14:06:08 -07001594 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001595 resetDrawTextureTexCoords(u1, v1, u2, v2);
1596
Romain Guy03750a02010-10-18 14:06:08 -07001597 int alpha;
1598 SkXfermode::Mode mode;
1599 getAlphaAndMode(paint, &alpha, &mode);
1600
Romain Guyd21b6e12011-11-30 20:21:23 -08001601 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1602
Romain Guy211370f2012-02-01 16:10:55 -08001603 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001604 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1605 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1606
Romain Guyb5014982011-07-28 15:39:12 -07001607 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001608 // Enable linear filtering if the source rectangle is scaled
1609 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001610 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001611 }
Romain Guyb5014982011-07-28 15:39:12 -07001612
Romain Guyd21b6e12011-11-30 20:21:23 -08001613 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001614 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1615 texture->id, alpha / 255.0f, mode, texture->blend,
1616 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1617 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1618 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001619 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001620 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1621 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1622 GL_TRIANGLE_STRIP, gMeshCount);
1623 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001624
1625 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1626}
1627
Romain Guy4aa90572010-09-26 18:40:37 -07001628void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001629 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001630 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001631 if (quickReject(left, top, right, bottom)) {
1632 return;
1633 }
1634
Romain Guya1d3c912011-12-13 14:55:06 -08001635 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001636 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001637 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001638 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001639 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1640 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001641
1642 int alpha;
1643 SkXfermode::Mode mode;
1644 getAlphaAndMode(paint, &alpha, &mode);
1645
Romain Guy2728f962010-10-08 18:36:15 -07001646 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001647 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001648
Romain Guy211370f2012-02-01 16:10:55 -08001649 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001650 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001651#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001652 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001653 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001654 const float offsetX = left + mSnapshot->transform->getTranslateX();
1655 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001656 const size_t count = mesh->quads.size();
1657 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001658 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001659 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001660 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1661 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1662 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001663 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001664 dirtyLayer(left + bounds.left, top + bounds.top,
1665 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001666 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001667 }
1668 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001669#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001670
Romain Guy211370f2012-02-01 16:10:55 -08001671 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001672 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1673 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1674
1675 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1676 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1677 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1678 true, !mesh->hasEmptyQuads);
1679 } else {
1680 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1681 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1682 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1683 true, !mesh->hasEmptyQuads);
1684 }
Romain Guy054dc182010-10-15 17:55:25 -07001685 }
Romain Guyf7f93552010-07-08 19:17:03 -07001686}
1687
Chet Haase99ecdc42011-05-06 12:06:34 -07001688/**
Chet Haase858aa932011-05-12 09:06:00 -07001689 * This function uses a similar approach to that of AA lines in the drawLines() function.
1690 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1691 * shader to compute the translucency of the color, determined by whether a given pixel is
1692 * within that boundary region and how far into the region it is.
1693 */
1694void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001695 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001696 float inverseScaleX = 1.0f;
1697 float inverseScaleY = 1.0f;
1698 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001699 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001700 Matrix4 *mat = mSnapshot->transform;
1701 float m00 = mat->data[Matrix4::kScaleX];
1702 float m01 = mat->data[Matrix4::kSkewY];
1703 float m02 = mat->data[2];
1704 float m10 = mat->data[Matrix4::kSkewX];
1705 float m11 = mat->data[Matrix4::kScaleX];
1706 float m12 = mat->data[6];
1707 float scaleX = sqrt(m00 * m00 + m01 * m01);
1708 float scaleY = sqrt(m10 * m10 + m11 * m11);
1709 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1710 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1711 }
1712
1713 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001714 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001715 setupDrawAALine();
1716 setupDrawColor(color);
1717 setupDrawColorFilter();
1718 setupDrawShader();
1719 setupDrawBlending(true, mode);
1720 setupDrawProgram();
1721 setupDrawModelViewIdentity(true);
1722 setupDrawColorUniforms();
1723 setupDrawColorFilterUniforms();
1724 setupDrawShaderIdentityUniforms();
1725
1726 AAVertex rects[4];
1727 AAVertex* aaVertices = &rects[0];
1728 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1729 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1730
1731 float boundarySizeX = .5 * inverseScaleX;
1732 float boundarySizeY = .5 * inverseScaleY;
1733
1734 // Adjust the rect by the AA boundary padding
1735 left -= boundarySizeX;
1736 right += boundarySizeX;
1737 top -= boundarySizeY;
1738 bottom += boundarySizeY;
1739
1740 float width = right - left;
1741 float height = bottom - top;
1742
Romain Guy7b631422012-04-04 11:38:54 -07001743 int widthSlot;
1744 int lengthSlot;
1745
Chet Haase858aa932011-05-12 09:06:00 -07001746 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1747 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001748 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1749 boundaryWidthProportion, widthSlot, lengthSlot);
1750
Chet Haase858aa932011-05-12 09:06:00 -07001751 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1752 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1753 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001754 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion));
Chet Haase858aa932011-05-12 09:06:00 -07001755
1756 if (!quickReject(left, top, right, bottom)) {
1757 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1758 AAVertex::set(aaVertices++, left, top, 1, 0);
1759 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1760 AAVertex::set(aaVertices++, right, top, 0, 0);
1761 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1762 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1763 }
Romain Guy7b631422012-04-04 11:38:54 -07001764
1765 finishDrawAALine(widthSlot, lengthSlot);
Chet Haase858aa932011-05-12 09:06:00 -07001766}
1767
1768/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001769 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1770 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1771 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1772 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1773 * of the line. Hairlines are more involved because we need to account for transform scaling
1774 * to end up with a one-pixel-wide line in screen space..
1775 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1776 * in combination with values that we calculate and pass down in this method. The basic approach
1777 * is that the quad we create contains both the core line area plus a bounding area in which
1778 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1779 * proportion of the width and the length of a given segment is represented by the boundary
1780 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1781 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1782 * on the inside). This ends up giving the result we want, with pixels that are completely
1783 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1784 * how far into the boundary region they are, which is determined by shader interpolation.
1785 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001786void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1787 if (mSnapshot->isIgnored()) return;
1788
1789 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001790 // We use half the stroke width here because we're going to position the quad
1791 // corner vertices half of the width away from the line endpoints
1792 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001793 // A stroke width of 0 has a special meaning in Skia:
1794 // it draws a line 1 px wide regardless of current transform
1795 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001796
Chet Haase99ecdc42011-05-06 12:06:34 -07001797 float inverseScaleX = 1.0f;
1798 float inverseScaleY = 1.0f;
1799 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001800
Chet Haase8a5cc922011-04-26 07:28:09 -07001801 int alpha;
1802 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001803
Chet Haase8a5cc922011-04-26 07:28:09 -07001804 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001805 int verticesCount = count;
1806 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001807 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001808 verticesCount += (count - 4);
1809 }
1810
1811 if (isHairLine || isAA) {
1812 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1813 // the line on the screen should always be one pixel wide regardless of scale. For
1814 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001815 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001816 Matrix4 *mat = mSnapshot->transform;
1817 float m00 = mat->data[Matrix4::kScaleX];
1818 float m01 = mat->data[Matrix4::kSkewY];
1819 float m02 = mat->data[2];
1820 float m10 = mat->data[Matrix4::kSkewX];
1821 float m11 = mat->data[Matrix4::kScaleX];
1822 float m12 = mat->data[6];
Romain Guy7b631422012-04-04 11:38:54 -07001823
Romain Guy211370f2012-02-01 16:10:55 -08001824 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1825 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001826
Chet Haase99ecdc42011-05-06 12:06:34 -07001827 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1828 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001829
Chet Haase99ecdc42011-05-06 12:06:34 -07001830 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1831 scaled = true;
1832 }
1833 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001834 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001835
1836 getAlphaAndMode(paint, &alpha, &mode);
1837 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001838 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001839 if (isAA) {
1840 setupDrawAALine();
1841 }
1842 setupDrawColor(paint->getColor(), alpha);
1843 setupDrawColorFilter();
1844 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001845 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001846 setupDrawProgram();
1847 setupDrawModelViewIdentity(true);
1848 setupDrawColorUniforms();
1849 setupDrawColorFilterUniforms();
1850 setupDrawShaderIdentityUniforms();
1851
1852 if (isHairLine) {
1853 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001854 halfStrokeWidth = isAA? 1 : .5;
1855 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001856 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001857 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001858 }
Romain Guy7b631422012-04-04 11:38:54 -07001859
1860 int widthSlot;
1861 int lengthSlot;
1862
Chet Haase5b0200b2011-04-13 17:58:08 -07001863 Vertex lines[verticesCount];
1864 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001865
Chet Haase99585ad2011-05-02 15:00:16 -07001866 AAVertex wLines[verticesCount];
1867 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001868
Romain Guy211370f2012-02-01 16:10:55 -08001869 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001870 setupDrawVertices(vertices);
1871 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001872 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1873 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001874 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001875 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1876 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001877 // We will need to calculate the actual width proportion on each segment for
1878 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1879 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001880 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1881 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001882 }
1883
Chet Haase99ecdc42011-05-06 12:06:34 -07001884 AAVertex* prevAAVertex = NULL;
1885 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001886
Chet Haase99585ad2011-05-02 15:00:16 -07001887 int boundaryLengthSlot = -1;
1888 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001889 int boundaryWidthSlot = -1;
1890 int inverseBoundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001891
Chet Haase5b0200b2011-04-13 17:58:08 -07001892 for (int i = 0; i < count; i += 4) {
1893 // a = start point, b = end point
1894 vec2 a(points[i], points[i + 1]);
1895 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001896
Chet Haase99585ad2011-05-02 15:00:16 -07001897 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001898 float boundaryLengthProportion = 0;
1899 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001900
Chet Haase5b0200b2011-04-13 17:58:08 -07001901 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001902 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001903 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001904 if (isAA) {
1905 float wideningFactor;
1906 if (fabs(n.x) >= fabs(n.y)) {
1907 wideningFactor = fabs(1.0f / n.x);
1908 } else {
1909 wideningFactor = fabs(1.0f / n.y);
1910 }
1911 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001912 }
Romain Guy7b631422012-04-04 11:38:54 -07001913
Chet Haase99ecdc42011-05-06 12:06:34 -07001914 if (scaled) {
1915 n.x *= inverseScaleX;
1916 n.y *= inverseScaleY;
1917 }
1918 } else if (scaled) {
1919 // Extend n by .5 pixel on each side, post-transform
1920 vec2 extendedN = n.copyNormalized();
1921 extendedN /= 2;
1922 extendedN.x *= inverseScaleX;
1923 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07001924
Chet Haase99ecdc42011-05-06 12:06:34 -07001925 float extendedNLength = extendedN.length();
1926 // We need to set this value on the shader prior to drawing
1927 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1928 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001929 }
Romain Guy7b631422012-04-04 11:38:54 -07001930
Chet Haase5b0200b2011-04-13 17:58:08 -07001931 float x = n.x;
1932 n.x = -n.y;
1933 n.y = x;
1934
Chet Haase99585ad2011-05-02 15:00:16 -07001935 // aa lines expand the endpoint vertices to encompass the AA boundary
1936 if (isAA) {
1937 vec2 abVector = (b - a);
1938 length = abVector.length();
1939 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07001940
Chet Haase99ecdc42011-05-06 12:06:34 -07001941 if (scaled) {
1942 abVector.x *= inverseScaleX;
1943 abVector.y *= inverseScaleY;
1944 float abLength = abVector.length();
1945 boundaryLengthProportion = abLength / (length + abLength);
1946 } else {
1947 boundaryLengthProportion = .5 / (length + 1);
1948 }
Romain Guy7b631422012-04-04 11:38:54 -07001949
Chet Haase99ecdc42011-05-06 12:06:34 -07001950 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001951 a -= abVector;
1952 b += abVector;
1953 }
1954
Chet Haase5b0200b2011-04-13 17:58:08 -07001955 // Four corners of the rectangle defining a thick line
1956 vec2 p1 = a - n;
1957 vec2 p2 = a + n;
1958 vec2 p3 = b + n;
1959 vec2 p4 = b - n;
1960
Chet Haase99585ad2011-05-02 15:00:16 -07001961
Chet Haase5b0200b2011-04-13 17:58:08 -07001962 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1963 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1964 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1965 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1966
1967 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001968 if (!isAA) {
1969 if (prevVertex != NULL) {
1970 // Issue two repeat vertices to create degenerate triangles to bridge
1971 // between the previous line and the new one. This is necessary because
1972 // we are creating a single triangle_strip which will contain
1973 // potentially discontinuous line segments.
1974 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1975 Vertex::set(vertices++, p1.x, p1.y);
1976 generatedVerticesCount += 2;
1977 }
Romain Guy7b631422012-04-04 11:38:54 -07001978
Chet Haase5b0200b2011-04-13 17:58:08 -07001979 Vertex::set(vertices++, p1.x, p1.y);
1980 Vertex::set(vertices++, p2.x, p2.y);
1981 Vertex::set(vertices++, p4.x, p4.y);
1982 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07001983
Chet Haase5b0200b2011-04-13 17:58:08 -07001984 prevVertex = vertices - 1;
1985 generatedVerticesCount += 4;
1986 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001987 if (!isHairLine && scaled) {
1988 // Must set width proportions per-segment for scaled non-hairlines to use the
1989 // correct AA boundary dimensions
1990 if (boundaryWidthSlot < 0) {
1991 boundaryWidthSlot =
1992 mCaches.currentProgram->getUniform("boundaryWidth");
1993 inverseBoundaryWidthSlot =
1994 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1995 }
Romain Guy7b631422012-04-04 11:38:54 -07001996
Chet Haase99ecdc42011-05-06 12:06:34 -07001997 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1998 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1999 }
Romain Guy7b631422012-04-04 11:38:54 -07002000
Chet Haase99585ad2011-05-02 15:00:16 -07002001 if (boundaryLengthSlot < 0) {
2002 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
2003 inverseBoundaryLengthSlot =
2004 mCaches.currentProgram->getUniform("inverseBoundaryLength");
2005 }
Romain Guy7b631422012-04-04 11:38:54 -07002006
Chet Haase99ecdc42011-05-06 12:06:34 -07002007 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
2008 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07002009
Chet Haase5b0200b2011-04-13 17:58:08 -07002010 if (prevAAVertex != NULL) {
2011 // Issue two repeat vertices to create degenerate triangles to bridge
2012 // between the previous line and the new one. This is necessary because
2013 // we are creating a single triangle_strip which will contain
2014 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002015 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2016 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2017 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002018 generatedVerticesCount += 2;
2019 }
Romain Guy7b631422012-04-04 11:38:54 -07002020
Chet Haase99585ad2011-05-02 15:00:16 -07002021 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2022 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2023 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2024 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002025
Chet Haase5b0200b2011-04-13 17:58:08 -07002026 prevAAVertex = aaVertices - 1;
2027 generatedVerticesCount += 4;
2028 }
Romain Guy7b631422012-04-04 11:38:54 -07002029
Chet Haase99585ad2011-05-02 15:00:16 -07002030 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2031 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2032 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002033 }
2034 }
Romain Guy7b631422012-04-04 11:38:54 -07002035
Chet Haase5b0200b2011-04-13 17:58:08 -07002036 if (generatedVerticesCount > 0) {
2037 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2038 }
Romain Guy7b631422012-04-04 11:38:54 -07002039
2040 if (isAA) {
2041 finishDrawAALine(widthSlot, lengthSlot);
2042 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002043}
2044
Romain Guyed6fcb02011-03-21 13:11:28 -07002045void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2046 if (mSnapshot->isIgnored()) return;
2047
2048 // TODO: The paint's cap style defines whether the points are square or circular
2049 // TODO: Handle AA for round points
2050
Chet Haase5b0200b2011-04-13 17:58:08 -07002051 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002052 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002053 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002054 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002055 if (isHairLine) {
2056 // Now that we know it's hairline, we can set the effective width, to be used later
2057 strokeWidth = 1.0f;
2058 }
2059 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002060 int alpha;
2061 SkXfermode::Mode mode;
2062 getAlphaAndMode(paint, &alpha, &mode);
2063
2064 int verticesCount = count >> 1;
2065 int generatedVerticesCount = 0;
2066
2067 TextureVertex pointsData[verticesCount];
2068 TextureVertex* vertex = &pointsData[0];
2069
2070 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002071 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002072 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002073 setupDrawColor(paint->getColor(), alpha);
2074 setupDrawColorFilter();
2075 setupDrawShader();
2076 setupDrawBlending(mode);
2077 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002078 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002079 setupDrawColorUniforms();
2080 setupDrawColorFilterUniforms();
2081 setupDrawPointUniforms();
2082 setupDrawShaderIdentityUniforms();
2083 setupDrawMesh(vertex);
2084
2085 for (int i = 0; i < count; i += 2) {
2086 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2087 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002088
Chet Haase8a5cc922011-04-26 07:28:09 -07002089 float left = points[i] - halfWidth;
2090 float right = points[i] + halfWidth;
2091 float top = points[i + 1] - halfWidth;
2092 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002093
Chet Haase8a5cc922011-04-26 07:28:09 -07002094 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002095 }
2096
2097 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
2098}
2099
Romain Guy85bf02f2010-06-22 13:11:24 -07002100void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002101 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08002102 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002103
Romain Guyae88e5e2010-10-22 17:49:18 -07002104 Rect& clip(*mSnapshot->clipRect);
2105 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002106
Romain Guy3d58c032010-07-14 16:34:53 -07002107 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07002108}
Romain Guy9d5316e2010-06-24 19:30:36 -07002109
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002110void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08002111 if (!texture) return;
2112 const AutoTexture autoCleanup(texture);
2113
2114 const float x = left + texture->left - texture->offset;
2115 const float y = top + texture->top - texture->offset;
2116
2117 drawPathTexture(texture, x, y, paint);
2118}
2119
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002120void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2121 float rx, float ry, SkPaint* paint) {
2122 if (mSnapshot->isIgnored()) return;
2123
Romain Guya1d3c912011-12-13 14:55:06 -08002124 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002125 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2126 right - left, bottom - top, rx, ry, paint);
2127 drawShape(left, top, texture, paint);
2128}
2129
Romain Guy01d58e42011-01-19 21:54:02 -08002130void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2131 if (mSnapshot->isIgnored()) return;
2132
Romain Guya1d3c912011-12-13 14:55:06 -08002133 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002134 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002135 drawShape(x - radius, y - radius, texture, paint);
2136}
Romain Guy01d58e42011-01-19 21:54:02 -08002137
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002138void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2139 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002140
Romain Guya1d3c912011-12-13 14:55:06 -08002141 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002142 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2143 drawShape(left, top, texture, paint);
2144}
2145
Romain Guy8b2f5262011-01-23 16:15:02 -08002146void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2147 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2148 if (mSnapshot->isIgnored()) return;
2149
2150 if (fabs(sweepAngle) >= 360.0f) {
2151 drawOval(left, top, right, bottom, paint);
2152 return;
2153 }
2154
Romain Guya1d3c912011-12-13 14:55:06 -08002155 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002156 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2157 startAngle, sweepAngle, useCenter, paint);
2158 drawShape(left, top, texture, paint);
2159}
2160
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002161void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2162 SkPaint* paint) {
2163 if (mSnapshot->isIgnored()) return;
2164
Romain Guya1d3c912011-12-13 14:55:06 -08002165 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002166 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2167 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002168}
2169
Chet Haase5c13d892010-10-08 08:37:55 -07002170void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002171 if (p->getStyle() != SkPaint::kFill_Style) {
2172 drawRectAsShape(left, top, right, bottom, p);
2173 return;
2174 }
2175
Romain Guy6926c722010-07-12 20:20:03 -07002176 if (quickReject(left, top, right, bottom)) {
2177 return;
2178 }
2179
Romain Guy026c5e162010-06-28 17:12:22 -07002180 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002181 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002182 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2183 if (!isMode) {
2184 // Assume SRC_OVER
2185 mode = SkXfermode::kSrcOver_Mode;
2186 }
2187 } else {
2188 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002189 }
2190
Romain Guy026c5e162010-06-28 17:12:22 -07002191 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002192 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002193 drawAARect(left, top, right, bottom, color, mode);
2194 } else {
2195 drawColorRect(left, top, right, bottom, color, mode);
2196 }
Romain Guyc7d53492010-06-25 13:41:57 -07002197}
Romain Guy9d5316e2010-06-24 19:30:36 -07002198
Romain Guyeb9a5362012-01-17 17:39:26 -08002199void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
2200 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002201 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2202 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guyeb9a5362012-01-17 17:39:26 -08002203 return;
2204 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002205
Romain Guy671d6cf2012-01-18 12:39:17 -08002206 // NOTE: Skia does not support perspective transform on drawPosText yet
2207 if (!mSnapshot->transform->isSimple()) {
2208 return;
2209 }
2210
2211 float x = 0.0f;
2212 float y = 0.0f;
2213 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2214 if (pureTranslate) {
2215 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2216 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2217 }
2218
2219 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2220 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2221 paint->getTextSize());
2222
2223 int alpha;
2224 SkXfermode::Mode mode;
2225 getAlphaAndMode(paint, &alpha, &mode);
2226
2227 // Pick the appropriate texture filtering
2228 bool linearFilter = mSnapshot->transform->changesBounds();
2229 if (pureTranslate && !linearFilter) {
2230 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2231 }
2232
2233 mCaches.activeTexture(0);
2234 setupDraw();
2235 setupDrawDirtyRegionsDisabled();
2236 setupDrawWithTexture(true);
2237 setupDrawAlpha8Color(paint->getColor(), alpha);
2238 setupDrawColorFilter();
2239 setupDrawShader();
2240 setupDrawBlending(true, mode);
2241 setupDrawProgram();
2242 setupDrawModelView(x, y, x, y, pureTranslate, true);
2243 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2244 setupDrawPureColorUniforms();
2245 setupDrawColorFilterUniforms();
2246 setupDrawShaderUniforms(pureTranslate);
2247
2248 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2249 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2250
2251#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002252 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002253#else
Romain Guy211370f2012-02-01 16:10:55 -08002254 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002255#endif
2256
2257 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2258 positions, hasActiveLayer ? &bounds : NULL)) {
2259#if RENDER_LAYERS_AS_REGIONS
2260 if (hasActiveLayer) {
2261 if (!pureTranslate) {
2262 mSnapshot->transform->mapRect(bounds);
2263 }
2264 dirtyLayerUnchecked(bounds, getRegion());
2265 }
2266#endif
2267 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002268}
2269
Romain Guye8e62a42010-07-23 18:55:21 -07002270void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002271 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002272 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2273 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07002274 return;
2275 }
2276
Chet Haasea1cff502012-02-21 13:43:44 -08002277 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002278 switch (paint->getTextAlign()) {
2279 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002280 x -= length / 2.0f;
2281 break;
2282 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002283 x -= length;
2284 break;
2285 default:
2286 break;
2287 }
2288
Romain Guycac5fd32011-12-01 20:08:50 -08002289 SkPaint::FontMetrics metrics;
2290 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002291 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Romain Guycac5fd32011-12-01 20:08:50 -08002292 return;
2293 }
2294
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002295 const float oldX = x;
2296 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002297 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002298 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002299 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2300 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2301 }
2302
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002303#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002304 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002305#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002306
2307 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002308 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002309 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002310
Romain Guy86568192010-12-14 15:55:39 -08002311 int alpha;
2312 SkXfermode::Mode mode;
2313 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002314
Romain Guy211370f2012-02-01 16:10:55 -08002315 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002316 mCaches.activeTexture(0);
2317
Romain Guyb45c0c92010-08-26 20:35:23 -07002318 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002319 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2320 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002321 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002322
Romain Guy740bf2b2011-04-26 15:33:10 -07002323 const float sx = oldX - shadow->left + mShadowDx;
2324 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002325
Romain Guy86568192010-12-14 15:55:39 -08002326 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002327 int shadowColor = mShadowColor;
2328 if (mShader) {
2329 shadowColor = 0xffffffff;
2330 }
Romain Guy86568192010-12-14 15:55:39 -08002331
Romain Guy86568192010-12-14 15:55:39 -08002332 setupDraw();
2333 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002334 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2335 setupDrawColorFilter();
2336 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002337 setupDrawBlending(true, mode);
2338 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002339 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002340 setupDrawTexture(shadow->id);
2341 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002342 setupDrawColorFilterUniforms();
2343 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002344 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2345
Romain Guy0a417492010-08-16 20:26:20 -07002346 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002347 }
2348
Romain Guy6620c6d2010-12-06 18:07:02 -08002349 // Pick the appropriate texture filtering
2350 bool linearFilter = mSnapshot->transform->changesBounds();
2351 if (pureTranslate && !linearFilter) {
2352 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2353 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002354
Romain Guya1d3c912011-12-13 14:55:06 -08002355 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002356 setupDraw();
2357 setupDrawDirtyRegionsDisabled();
2358 setupDrawWithTexture(true);
2359 setupDrawAlpha8Color(paint->getColor(), alpha);
2360 setupDrawColorFilter();
2361 setupDrawShader();
2362 setupDrawBlending(true, mode);
2363 setupDrawProgram();
2364 setupDrawModelView(x, y, x, y, pureTranslate, true);
2365 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2366 setupDrawPureColorUniforms();
2367 setupDrawColorFilterUniforms();
2368 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002369
Romain Guy6620c6d2010-12-06 18:07:02 -08002370 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002371 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2372
2373#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002374 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002375#else
Romain Guy211370f2012-02-01 16:10:55 -08002376 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002377#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002378
Romain Guy6620c6d2010-12-06 18:07:02 -08002379 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002380 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002381#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002382 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002383 if (!pureTranslate) {
2384 mSnapshot->transform->mapRect(bounds);
2385 }
Romain Guyf219da52011-01-16 12:54:25 -08002386 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002387 }
2388#endif
2389 }
Romain Guy694b5192010-07-21 21:33:20 -07002390
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002391 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002392}
2393
Romain Guy325740f2012-02-24 16:48:34 -08002394void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
2395 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002396 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2397 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
2398 return;
2399 }
2400
Romain Guy03d58522012-02-24 17:54:07 -08002401 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2402 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2403 paint->getTextSize());
2404
2405 int alpha;
2406 SkXfermode::Mode mode;
2407 getAlphaAndMode(paint, &alpha, &mode);
2408
2409 mCaches.activeTexture(0);
2410 setupDraw();
2411 setupDrawDirtyRegionsDisabled();
2412 setupDrawWithTexture(true);
2413 setupDrawAlpha8Color(paint->getColor(), alpha);
2414 setupDrawColorFilter();
2415 setupDrawShader();
2416 setupDrawBlending(true, mode);
2417 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002418 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002419 setupDrawTexture(fontRenderer.getTexture(true));
2420 setupDrawPureColorUniforms();
2421 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002422 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002423
Romain Guy97771732012-02-28 18:17:02 -08002424 const Rect* clip = &mSnapshot->getLocalClip();
2425 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 -08002426
Romain Guy97771732012-02-28 18:17:02 -08002427#if RENDER_LAYERS_AS_REGIONS
2428 const bool hasActiveLayer = hasLayer();
2429#else
2430 const bool hasActiveLayer = false;
2431#endif
2432
2433 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2434 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2435#if RENDER_LAYERS_AS_REGIONS
2436 if (hasActiveLayer) {
2437 mSnapshot->transform->mapRect(bounds);
2438 dirtyLayerUnchecked(bounds, getRegion());
2439 }
2440#endif
2441 }
Romain Guy325740f2012-02-24 16:48:34 -08002442}
2443
Romain Guy7fbcc042010-08-04 15:40:07 -07002444void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002445 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002446
Romain Guya1d3c912011-12-13 14:55:06 -08002447 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002448
Romain Guy33f6beb2012-02-16 19:24:51 -08002449 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002450 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002451 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002452 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002453
Romain Guy8b55f372010-08-18 17:10:07 -07002454 const float x = texture->left - texture->offset;
2455 const float y = texture->top - texture->offset;
2456
Romain Guy01d58e42011-01-19 21:54:02 -08002457 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002458}
2459
Romain Guyada830f2011-01-13 12:13:20 -08002460void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2461 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002462 return;
2463 }
2464
Romain Guy2bf68f02012-03-02 13:37:47 -08002465 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2466 OpenGLRenderer* renderer = layer->renderer;
2467 Rect& dirty = layer->dirtyRect;
2468
2469 interrupt();
2470 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2471 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002472 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002473 renderer->finish();
2474 resume();
2475
2476 dirty.setEmpty();
2477 layer->deferredUpdateScheduled = false;
2478 layer->renderer = NULL;
2479 layer->displayList = NULL;
2480 }
2481
Romain Guya1d3c912011-12-13 14:55:06 -08002482 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002483
2484 int alpha;
2485 SkXfermode::Mode mode;
2486 getAlphaAndMode(paint, &alpha, &mode);
2487
Romain Guy9ace8f52011-07-07 20:50:11 -07002488 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002489
Romain Guyf219da52011-01-16 12:54:25 -08002490#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002491 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002492 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002493 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002494 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002495 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002496 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002497
Romain Guyc88e3572011-01-22 00:32:12 -08002498 setupDraw();
2499 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002500 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002501 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002502 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002503 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002504 setupDrawPureColorUniforms();
2505 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002506 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002507 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002508 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2509 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2510
Romain Guyd21b6e12011-11-30 20:21:23 -08002511 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002512 setupDrawModelViewTranslate(x, y,
2513 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2514 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002515 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002516 setupDrawModelViewTranslate(x, y,
2517 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2518 }
Romain Guyc88e3572011-01-22 00:32:12 -08002519 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002520
Romain Guyc88e3572011-01-22 00:32:12 -08002521 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2522 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002523
Romain Guyc88e3572011-01-22 00:32:12 -08002524 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002525
2526#if DEBUG_LAYERS_AS_REGIONS
2527 drawRegionRects(layer->region);
2528#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002529 }
Romain Guyf219da52011-01-16 12:54:25 -08002530 }
2531#else
Romain Guyada830f2011-01-13 12:13:20 -08002532 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2533 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002534#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002535}
2536
Romain Guy6926c722010-07-12 20:20:03 -07002537///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002538// Shaders
2539///////////////////////////////////////////////////////////////////////////////
2540
2541void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002542 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002543}
2544
Romain Guy06f96e22010-07-30 19:18:16 -07002545void OpenGLRenderer::setupShader(SkiaShader* shader) {
2546 mShader = shader;
2547 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002548 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002549 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002550}
2551
Romain Guyd27977d2010-07-14 19:18:51 -07002552///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002553// Color filters
2554///////////////////////////////////////////////////////////////////////////////
2555
2556void OpenGLRenderer::resetColorFilter() {
2557 mColorFilter = NULL;
2558}
2559
2560void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2561 mColorFilter = filter;
2562}
2563
2564///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002565// Drop shadow
2566///////////////////////////////////////////////////////////////////////////////
2567
2568void OpenGLRenderer::resetShadow() {
2569 mHasShadow = false;
2570}
2571
2572void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2573 mHasShadow = true;
2574 mShadowRadius = radius;
2575 mShadowDx = dx;
2576 mShadowDy = dy;
2577 mShadowColor = color;
2578}
2579
2580///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002581// Draw filters
2582///////////////////////////////////////////////////////////////////////////////
2583
2584void OpenGLRenderer::resetPaintFilter() {
2585 mHasDrawFilter = false;
2586}
2587
2588void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2589 mHasDrawFilter = true;
2590 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2591 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2592}
2593
2594SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002595 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002596
2597 uint32_t flags = paint->getFlags();
2598
2599 mFilteredPaint = *paint;
2600 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2601
2602 return &mFilteredPaint;
2603}
2604
2605///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002606// Drawing implementation
2607///////////////////////////////////////////////////////////////////////////////
2608
Romain Guy01d58e42011-01-19 21:54:02 -08002609void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2610 float x, float y, SkPaint* paint) {
2611 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2612 return;
2613 }
2614
2615 int alpha;
2616 SkXfermode::Mode mode;
2617 getAlphaAndMode(paint, &alpha, &mode);
2618
2619 setupDraw();
2620 setupDrawWithTexture(true);
2621 setupDrawAlpha8Color(paint->getColor(), alpha);
2622 setupDrawColorFilter();
2623 setupDrawShader();
2624 setupDrawBlending(true, mode);
2625 setupDrawProgram();
2626 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2627 setupDrawTexture(texture->id);
2628 setupDrawPureColorUniforms();
2629 setupDrawColorFilterUniforms();
2630 setupDrawShaderUniforms();
2631 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2632
2633 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2634
2635 finishDrawTexture();
2636}
2637
Romain Guyf607bdc2010-09-10 19:20:06 -07002638// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002639#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2640#define kStdUnderline_Offset (1.0f / 9.0f)
2641#define kStdUnderline_Thickness (1.0f / 18.0f)
2642
2643void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2644 float x, float y, SkPaint* paint) {
2645 // Handle underline and strike-through
2646 uint32_t flags = paint->getFlags();
2647 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002648 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002649 float underlineWidth = length;
2650 // If length is > 0.0f, we already measured the text for the text alignment
2651 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002652 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002653 }
2654
2655 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002656 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002657 case SkPaint::kCenter_Align:
2658 offsetX = underlineWidth * 0.5f;
2659 break;
2660 case SkPaint::kRight_Align:
2661 offsetX = underlineWidth;
2662 break;
2663 default:
2664 break;
2665 }
2666
Romain Guy211370f2012-02-01 16:10:55 -08002667 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002668 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002669 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002670
Romain Guye20ecbd2010-09-22 19:49:04 -07002671 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002672 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002673
Romain Guyf6834472011-01-23 13:32:12 -08002674 int linesCount = 0;
2675 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2676 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2677
2678 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002679 float points[pointsCount];
2680 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002681
2682 if (flags & SkPaint::kUnderlineText_Flag) {
2683 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002684 points[currentPoint++] = left;
2685 points[currentPoint++] = top;
2686 points[currentPoint++] = left + underlineWidth;
2687 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002688 }
2689
2690 if (flags & SkPaint::kStrikeThruText_Flag) {
2691 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002692 points[currentPoint++] = left;
2693 points[currentPoint++] = top;
2694 points[currentPoint++] = left + underlineWidth;
2695 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002696 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002697
Romain Guy726aeba2011-06-01 14:52:00 -07002698 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002699
Romain Guy726aeba2011-06-01 14:52:00 -07002700 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002701 }
2702 }
2703}
2704
Romain Guy026c5e162010-06-28 17:12:22 -07002705void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002706 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002707 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002708 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002709 color |= 0x00ffffff;
2710 }
2711
Romain Guy70ca14e2010-12-13 18:24:33 -08002712 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002713 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002714 setupDrawColor(color);
2715 setupDrawShader();
2716 setupDrawColorFilter();
2717 setupDrawBlending(mode);
2718 setupDrawProgram();
2719 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2720 setupDrawColorUniforms();
2721 setupDrawShaderUniforms(ignoreTransform);
2722 setupDrawColorFilterUniforms();
2723 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002724
Romain Guyc95c8d62010-09-17 15:31:32 -07002725 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2726}
2727
Romain Guy82ba8142010-07-09 13:25:56 -07002728void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002729 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002730 int alpha;
2731 SkXfermode::Mode mode;
2732 getAlphaAndMode(paint, &alpha, &mode);
2733
Romain Guyd21b6e12011-11-30 20:21:23 -08002734 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002735
Romain Guy211370f2012-02-01 16:10:55 -08002736 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002737 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2738 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2739
Romain Guyd21b6e12011-11-30 20:21:23 -08002740 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002741 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2742 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2743 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2744 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002745 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002746 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2747 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2748 GL_TRIANGLE_STRIP, gMeshCount);
2749 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002750}
2751
Romain Guybd6b79b2010-06-26 00:13:53 -07002752void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002753 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2754 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002755 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002756}
2757
2758void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002759 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002760 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002761 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002762
Romain Guy746b7402010-10-26 16:27:31 -07002763 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002764 setupDrawWithTexture();
2765 setupDrawColor(alpha, alpha, alpha, alpha);
2766 setupDrawColorFilter();
2767 setupDrawBlending(blend, mode, swapSrcDst);
2768 setupDrawProgram();
2769 if (!dirty) {
2770 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002771 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002772 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002773 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002774 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002775 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002776 }
Romain Guy86568192010-12-14 15:55:39 -08002777 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002778 setupDrawColorFilterUniforms();
2779 setupDrawTexture(texture);
2780 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002781
Romain Guy6820ac82010-09-15 18:11:50 -07002782 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002783
2784 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002785}
2786
Romain Guya5aed0d2010-09-09 14:42:43 -07002787void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002788 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002789 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2790 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002791 // These blend modes are not supported by OpenGL directly and have
2792 // to be implemented using shaders. Since the shader will perform
2793 // the blending, turn blending off here
2794 // If the blend mode cannot be implemented using shaders, fall
2795 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002796 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2797 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002798 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002799 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002800
Romain Guy82bc7a72012-01-03 14:13:39 -08002801 if (mCaches.blend) {
2802 glDisable(GL_BLEND);
2803 mCaches.blend = false;
2804 }
2805
2806 return;
2807 } else {
2808 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002809 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002810 }
2811
2812 if (!mCaches.blend) {
2813 glEnable(GL_BLEND);
2814 }
2815
2816 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2817 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2818
2819 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2820 glBlendFunc(sourceMode, destMode);
2821 mCaches.lastSrcMode = sourceMode;
2822 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002823 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002824 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002825 glDisable(GL_BLEND);
2826 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002827 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002828}
2829
Romain Guy889f8d12010-07-29 14:37:42 -07002830bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002831 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002832 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002833 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002834 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002835 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002836 }
Romain Guy6926c722010-07-12 20:20:03 -07002837 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002838}
2839
Romain Guy026c5e162010-06-28 17:12:22 -07002840void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002841 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002842 TextureVertex::setUV(v++, u1, v1);
2843 TextureVertex::setUV(v++, u2, v1);
2844 TextureVertex::setUV(v++, u1, v2);
2845 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002846}
2847
Chet Haase5c13d892010-10-08 08:37:55 -07002848void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002849 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002850 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002851
2852 // Skia draws using the color's alpha channel if < 255
2853 // Otherwise, it uses the paint's alpha
2854 int color = paint->getColor();
2855 *alpha = (color >> 24) & 0xFF;
2856 if (*alpha == 255) {
2857 *alpha = paint->getAlpha();
2858 }
2859 } else {
2860 *mode = SkXfermode::kSrcOver_Mode;
2861 *alpha = 255;
2862 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07002863 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002864}
2865
Romain Guya5aed0d2010-09-09 14:42:43 -07002866SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002867 SkXfermode::Mode resultMode;
2868 if (!SkXfermode::AsMode(mode, &resultMode)) {
2869 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002870 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002871 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002872}
2873
Romain Guy9d5316e2010-06-24 19:30:36 -07002874}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002875}; // namespace android