blob: 06928df6e39363d8f48e1173a3e49a25d473355f [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 Guy8f3b8e32012-03-27 16:33:45 -0700240status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
241 status_t result = DrawGlInfo::kStatusDone;
242
243 Vector<Functor*> functors(mFunctors);
244 mFunctors.clear();
245
246 DrawGlInfo info;
247 info.clipLeft = 0;
248 info.clipTop = 0;
249 info.clipRight = 0;
250 info.clipBottom = 0;
251 info.isLayer = false;
Chet Haase7b6a7582012-04-11 14:32:02 -0700252 info.width = 0;
253 info.height = 0;
Romain Guy8f3b8e32012-03-27 16:33:45 -0700254 memset(info.transform, 0, sizeof(float) * 16);
255
256 size_t count = functors.size();
257 for (size_t i = 0; i < count; i++) {
258 Functor* f = functors.itemAt(i);
259 result |= (*f)(DrawGlInfo::kModeProcess, &info);
260
261 if (result != DrawGlInfo::kStatusDone) {
262 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
263 dirty.unionWith(localDirty);
264
Chris Craik65924a32012-04-05 17:52:11 -0700265 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guy8f3b8e32012-03-27 16:33:45 -0700266 mFunctors.push(f);
267 }
268 }
269 }
270
271 return result;
272}
273
274status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800275 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800276 if (mDirtyClip) {
277 setScissorFromClip();
278 }
Romain Guyd643bb52011-03-01 14:55:21 -0800279
Romain Guy80911b82011-03-16 15:30:12 -0700280 Rect clip(*mSnapshot->clipRect);
281 clip.snapToPixelBoundaries();
282
Romain Guyd643bb52011-03-01 14:55:21 -0800283#if RENDER_LAYERS_AS_REGIONS
284 // Since we don't know what the functor will draw, let's dirty
285 // tne entire clip region
286 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800287 dirtyLayerUnchecked(clip, getRegion());
288 }
289#endif
290
Romain Guy08aa2cb2011-03-17 11:06:57 -0700291 DrawGlInfo info;
292 info.clipLeft = clip.left;
293 info.clipTop = clip.top;
294 info.clipRight = clip.right;
295 info.clipBottom = clip.bottom;
296 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700297 info.width = getSnapshot()->viewport.getWidth();
298 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700299 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700300
Romain Guy8f3b8e32012-03-27 16:33:45 -0700301 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800302
Romain Guy8f3b8e32012-03-27 16:33:45 -0700303 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700304 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800305 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700306
Chris Craik65924a32012-04-05 17:52:11 -0700307 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guy8f3b8e32012-03-27 16:33:45 -0700308 mFunctors.push(functor);
309 }
Romain Guycabfcc12011-03-07 18:06:46 -0800310 }
311
Chet Haasedaf98e92011-01-10 14:10:36 -0800312 resume();
Romain Guy65549432012-03-26 16:45:05 -0700313 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800314}
315
Romain Guyf6a11b82010-06-23 17:47:49 -0700316///////////////////////////////////////////////////////////////////////////////
317// State management
318///////////////////////////////////////////////////////////////////////////////
319
Romain Guybb9524b2010-06-22 18:56:38 -0700320int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700321 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700322}
323
324int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700325 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700326}
327
328void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700329 if (mSaveCount > 1) {
330 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700331 }
Romain Guybb9524b2010-06-22 18:56:38 -0700332}
333
334void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700335 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700336
Romain Guy8fb95422010-08-17 18:38:51 -0700337 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700338 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700339 }
Romain Guybb9524b2010-06-22 18:56:38 -0700340}
341
Romain Guy8aef54f2010-09-01 15:13:49 -0700342int OpenGLRenderer::saveSnapshot(int flags) {
343 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700344 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700345}
346
347bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700348 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700349 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700350 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700351
Romain Guybd6b79b2010-06-26 00:13:53 -0700352 sp<Snapshot> current = mSnapshot;
353 sp<Snapshot> previous = mSnapshot->previous;
354
Romain Guyeb993562010-10-05 18:14:38 -0700355 if (restoreOrtho) {
356 Rect& r = previous->viewport;
357 glViewport(r.left, r.top, r.right, r.bottom);
358 mOrthoMatrix.load(current->orthoMatrix);
359 }
360
Romain Guy8b55f372010-08-18 17:10:07 -0700361 mSaveCount--;
362 mSnapshot = previous;
363
Romain Guy2542d192010-08-18 11:47:12 -0700364 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700365 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700366 }
Romain Guy2542d192010-08-18 11:47:12 -0700367
Romain Guy5ec99242010-11-03 16:19:08 -0700368 if (restoreLayer) {
369 composeLayer(current, previous);
370 }
371
Romain Guy2542d192010-08-18 11:47:12 -0700372 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700373}
374
Romain Guyf6a11b82010-06-23 17:47:49 -0700375///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700376// Layers
377///////////////////////////////////////////////////////////////////////////////
378
379int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700380 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700381 const GLuint previousFbo = mSnapshot->fbo;
382 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700383
Romain Guyaf636eb2010-12-09 17:47:21 -0800384 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700385 int alpha = 255;
386 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700387
Romain Guye45362c2010-11-03 19:58:32 -0700388 if (p) {
389 alpha = p->getAlpha();
390 if (!mCaches.extensions.hasFramebufferFetch()) {
391 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
392 if (!isMode) {
393 // Assume SRC_OVER
394 mode = SkXfermode::kSrcOver_Mode;
395 }
396 } else {
397 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700398 }
399 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700400 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700401 }
Romain Guyd55a8612010-06-28 17:42:46 -0700402
Romain Guydbc26d22010-10-11 17:58:29 -0700403 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
404 }
Romain Guyd55a8612010-06-28 17:42:46 -0700405
406 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700407}
408
409int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
410 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700411 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700412 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700413 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700414 SkPaint paint;
415 paint.setAlpha(alpha);
416 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700417 }
Romain Guyd55a8612010-06-28 17:42:46 -0700418}
Romain Guybd6b79b2010-06-26 00:13:53 -0700419
Romain Guy1c740bc2010-09-13 18:00:09 -0700420/**
421 * Layers are viewed by Skia are slightly different than layers in image editing
422 * programs (for instance.) When a layer is created, previously created layers
423 * and the frame buffer still receive every drawing command. For instance, if a
424 * layer is created and a shape intersecting the bounds of the layers and the
425 * framebuffer is draw, the shape will be drawn on both (unless the layer was
426 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
427 *
428 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
429 * texture. Unfortunately, this is inefficient as it requires every primitive to
430 * be drawn n + 1 times, where n is the number of active layers. In practice this
431 * means, for every primitive:
432 * - Switch active frame buffer
433 * - Change viewport, clip and projection matrix
434 * - Issue the drawing
435 *
436 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700437 * To avoid this, layers are implemented in a different way here, at least in the
438 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
439 * is set. When this flag is set we can redirect all drawing operations into a
440 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700441 *
442 * This implementation relies on the frame buffer being at least RGBA 8888. When
443 * a layer is created, only a texture is created, not an FBO. The content of the
444 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700445 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700446 * buffer and drawing continues as normal. This technique therefore treats the
447 * frame buffer as a scratch buffer for the layers.
448 *
449 * To compose the layers back onto the frame buffer, each layer texture
450 * (containing the original frame buffer data) is drawn as a simple quad over
451 * the frame buffer. The trick is that the quad is set as the composition
452 * destination in the blending equation, and the frame buffer becomes the source
453 * of the composition.
454 *
455 * Drawing layers with an alpha value requires an extra step before composition.
456 * An empty quad is drawn over the layer's region in the frame buffer. This quad
457 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
458 * quad is used to multiply the colors in the frame buffer. This is achieved by
459 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
460 * GL_ZERO, GL_SRC_ALPHA.
461 *
462 * Because glCopyTexImage2D() can be slow, an alternative implementation might
463 * be use to draw a single clipped layer. The implementation described above
464 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700465 *
466 * (1) The frame buffer is actually not cleared right away. To allow the GPU
467 * to potentially optimize series of calls to glCopyTexImage2D, the frame
468 * buffer is left untouched until the first drawing operation. Only when
469 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700470 */
Romain Guyd55a8612010-06-28 17:42:46 -0700471bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700472 float right, float bottom, int alpha, SkXfermode::Mode mode,
473 int flags, GLuint previousFbo) {
474 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700475 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700476
Romain Guyeb993562010-10-05 18:14:38 -0700477 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
478
Romain Guyf607bdc2010-09-10 19:20:06 -0700479 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700480 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700481 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700482 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700483
Romain Guyeb993562010-10-05 18:14:38 -0700484 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700485 if (bounds.intersect(*snapshot->clipRect)) {
486 // We cannot work with sub-pixels in this case
487 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700488
Romain Guyad37cd32011-03-15 11:12:25 -0700489 // When the layer is not an FBO, we may use glCopyTexImage so we
490 // need to make sure the layer does not extend outside the bounds
491 // of the framebuffer
492 if (!bounds.intersect(snapshot->previous->viewport)) {
493 bounds.setEmpty();
494 }
495 } else {
496 bounds.setEmpty();
497 }
Romain Guyeb993562010-10-05 18:14:38 -0700498 }
Romain Guybf434112010-09-16 14:40:17 -0700499
Romain Guy746b7402010-10-26 16:27:31 -0700500 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
501 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800502 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700503 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700504 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700505 }
506
507 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800508 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700509 return false;
510 }
Romain Guyf18fd992010-07-08 11:45:51 -0700511
Romain Guya1d3c912011-12-13 14:55:06 -0800512 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700513 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700514 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700515 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700516 }
517
Romain Guy9ace8f52011-07-07 20:50:11 -0700518 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700519 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700520 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
521 bounds.getWidth() / float(layer->getWidth()), 0.0f);
522 layer->setColorFilter(mColorFilter);
Romain Guydda570202010-07-06 11:39:32 -0700523
Romain Guy8fb95422010-08-17 18:38:51 -0700524 // Save the layer in the snapshot
525 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700526 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700527
Romain Guyeb993562010-10-05 18:14:38 -0700528 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700529 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700530 } else {
531 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700532 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800533 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700534 if (layer->isEmpty()) {
535 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
536 bounds.left, snapshot->height - bounds.bottom,
537 layer->getWidth(), layer->getHeight(), 0);
538 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800539 } else {
540 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
541 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
542 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700543
Romain Guy54be1cd2011-06-13 19:04:27 -0700544 // Enqueue the buffer coordinates to clear the corresponding region later
545 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700546 }
Romain Guyeb993562010-10-05 18:14:38 -0700547 }
Romain Guyf86ef572010-07-01 11:05:42 -0700548
Romain Guyd55a8612010-06-28 17:42:46 -0700549 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700550}
551
Romain Guy5b3b3522010-10-27 18:57:51 -0700552bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
553 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700554 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700555
556#if RENDER_LAYERS_AS_REGIONS
557 snapshot->region = &snapshot->layer->region;
558 snapshot->flags |= Snapshot::kFlagFboTarget;
559#endif
560
561 Rect clip(bounds);
562 snapshot->transform->mapRect(clip);
563 clip.intersect(*snapshot->clipRect);
564 clip.snapToPixelBoundaries();
565 clip.intersect(snapshot->previous->viewport);
566
567 mat4 inverse;
568 inverse.loadInverse(*mSnapshot->transform);
569
570 inverse.mapRect(clip);
571 clip.snapToPixelBoundaries();
572 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700573 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700574
575 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700576 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700577 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700578 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
579 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
580 snapshot->height = bounds.getHeight();
581 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
582 snapshot->orthoMatrix.load(mOrthoMatrix);
583
584 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700585 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
586 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700587
588 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700589 if (layer->isEmpty()) {
590 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
591 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700592 }
593
594 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700595 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700596
597#if DEBUG_LAYERS_AS_REGIONS
598 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
599 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000600 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700601
602 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700603 layer->deleteTexture();
604 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700605
606 delete layer;
607
608 return false;
609 }
610#endif
611
612 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800613 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700614 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700615 glClear(GL_COLOR_BUFFER_BIT);
616
617 dirtyClip();
618
619 // Change the ortho projection
620 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
621 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
622
623 return true;
624}
625
Romain Guy1c740bc2010-09-13 18:00:09 -0700626/**
627 * Read the documentation of createLayer() before doing anything in this method.
628 */
Romain Guy1d83e192010-08-17 11:37:00 -0700629void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
630 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000631 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700632 return;
633 }
634
Romain Guy5b3b3522010-10-27 18:57:51 -0700635 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700636
637 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700638 // Detach the texture from the FBO
639 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
640
Romain Guyeb993562010-10-05 18:14:38 -0700641 // Unbind current FBO and restore previous one
642 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
643 }
644
Romain Guy1d83e192010-08-17 11:37:00 -0700645 Layer* layer = current->layer;
646 const Rect& rect = layer->layer;
647
Romain Guy9ace8f52011-07-07 20:50:11 -0700648 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700649 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700650 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700651 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700652 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700653 }
654
Romain Guy03750a02010-10-18 14:06:08 -0700655 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700656
Romain Guya1d3c912011-12-13 14:55:06 -0800657 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700658
Romain Guy5b3b3522010-10-27 18:57:51 -0700659 // When the layer is stored in an FBO, we can save a bit of fillrate by
660 // drawing only the dirty region
661 if (fboLayer) {
662 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700663 if (layer->getColorFilter()) {
664 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800665 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700666 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700667 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800668 resetColorFilter();
669 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700670 } else if (!rect.isEmpty()) {
671 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
672 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700673 }
Romain Guy8b55f372010-08-18 17:10:07 -0700674
Romain Guyeb993562010-10-05 18:14:38 -0700675 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800676 // Note: No need to use glDiscardFramebufferEXT() since we never
677 // create/compose layers that are not on screen with this
678 // code path
679 // See LayerRenderer::destroyLayer(Layer*)
680
Romain Guyeb993562010-10-05 18:14:38 -0700681 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
682 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700683 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700684 }
685
Romain Guy746b7402010-10-26 16:27:31 -0700686 dirtyClip();
687
Romain Guyeb993562010-10-05 18:14:38 -0700688 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700689 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700690 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700691 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700692 delete layer;
693 }
694}
695
Romain Guyaa6c24c2011-04-28 18:40:04 -0700696void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700697 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700698
Romain Guy302a9df2011-08-16 13:55:02 -0700699 mat4& transform = layer->getTransform();
700 if (!transform.isIdentity()) {
701 save(0);
702 mSnapshot->transform->multiply(transform);
703 }
704
Romain Guyaa6c24c2011-04-28 18:40:04 -0700705 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700706 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700707 setupDrawWithTexture();
708 } else {
709 setupDrawWithExternalTexture();
710 }
711 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700712 setupDrawColor(alpha, alpha, alpha, alpha);
713 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700714 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700715 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700716 setupDrawPureColorUniforms();
717 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700718 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
719 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700720 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700721 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700722 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700723 if (mSnapshot->transform->isPureTranslate() &&
724 layer->getWidth() == (uint32_t) rect.getWidth() &&
725 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700726 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
727 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
728
Romain Guyd21b6e12011-11-30 20:21:23 -0800729 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700730 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
731 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800732 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700733 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
734 }
735 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700736 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
737
738 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
739
740 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700741
742 if (!transform.isIdentity()) {
743 restore();
744 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700745}
746
Romain Guy5b3b3522010-10-27 18:57:51 -0700747void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700748 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700749 const Rect& texCoords = layer->texCoords;
750 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
751 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700752
Romain Guy9ace8f52011-07-07 20:50:11 -0700753 float x = rect.left;
754 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700755 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700756 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700757 layer->getHeight() == (uint32_t) rect.getHeight();
758
759 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700760 // When we're swapping, the layer is already in screen coordinates
761 if (!swap) {
762 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
763 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
764 }
765
Romain Guyd21b6e12011-11-30 20:21:23 -0800766 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700767 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800768 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700769 }
770
771 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
772 layer->getTexture(), layer->getAlpha() / 255.0f,
773 layer->getMode(), layer->isBlend(),
774 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
775 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700776
Romain Guyaa6c24c2011-04-28 18:40:04 -0700777 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
778 } else {
779 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
780 drawTextureLayer(layer, rect);
781 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
782 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700783}
784
785void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
786#if RENDER_LAYERS_AS_REGIONS
787 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700788 layer->setRegionAsRect();
789
Romain Guy40667672011-03-18 14:34:03 -0700790 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700791
Romain Guy5b3b3522010-10-27 18:57:51 -0700792 layer->region.clear();
793 return;
794 }
795
Romain Guy8a3957d2011-09-07 17:55:15 -0700796 // TODO: See LayerRenderer.cpp::generateMesh() for important
797 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800798 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700799 size_t count;
800 const android::Rect* rects = layer->region.getArray(&count);
801
Romain Guy9ace8f52011-07-07 20:50:11 -0700802 const float alpha = layer->getAlpha() / 255.0f;
803 const float texX = 1.0f / float(layer->getWidth());
804 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800805 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700806
807 TextureVertex* mesh = mCaches.getRegionMesh();
808 GLsizei numQuads = 0;
809
Romain Guy7230a742011-01-10 22:26:16 -0800810 setupDraw();
811 setupDrawWithTexture();
812 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800813 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700814 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800815 setupDrawProgram();
816 setupDrawDirtyRegionsDisabled();
817 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800818 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700819 setupDrawTexture(layer->getTexture());
820 if (mSnapshot->transform->isPureTranslate()) {
821 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
822 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
823
Romain Guyd21b6e12011-11-30 20:21:23 -0800824 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700825 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
826 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800827 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700828 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
829 }
Romain Guy15bc6432011-12-13 13:11:32 -0800830 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700831
832 for (size_t i = 0; i < count; i++) {
833 const android::Rect* r = &rects[i];
834
835 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800836 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700837 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800838 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700839
840 // TODO: Reject quads outside of the clip
841 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
842 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
843 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
844 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
845
846 numQuads++;
847
848 if (numQuads >= REGION_MESH_QUAD_COUNT) {
849 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
850 numQuads = 0;
851 mesh = mCaches.getRegionMesh();
852 }
853 }
854
855 if (numQuads > 0) {
856 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
857 }
858
Romain Guy7230a742011-01-10 22:26:16 -0800859 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700860
861#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800862 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700863#endif
864
865 layer->region.clear();
866 }
867#else
868 composeLayerRect(layer, rect);
869#endif
870}
871
Romain Guy3a3133d2011-02-01 22:59:58 -0800872void OpenGLRenderer::drawRegionRects(const Region& region) {
873#if DEBUG_LAYERS_AS_REGIONS
874 size_t count;
875 const android::Rect* rects = region.getArray(&count);
876
877 uint32_t colors[] = {
878 0x7fff0000, 0x7f00ff00,
879 0x7f0000ff, 0x7fff00ff,
880 };
881
882 int offset = 0;
883 int32_t top = rects[0].top;
884
885 for (size_t i = 0; i < count; i++) {
886 if (top != rects[i].top) {
887 offset ^= 0x2;
888 top = rects[i].top;
889 }
890
891 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
892 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
893 SkXfermode::kSrcOver_Mode);
894 }
895#endif
896}
897
Romain Guy5b3b3522010-10-27 18:57:51 -0700898void OpenGLRenderer::dirtyLayer(const float left, const float top,
899 const float right, const float bottom, const mat4 transform) {
900#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800901 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700902 Rect bounds(left, top, right, bottom);
903 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800904 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700905 }
906#endif
907}
908
909void OpenGLRenderer::dirtyLayer(const float left, const float top,
910 const float right, const float bottom) {
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);
Romain Guyf219da52011-01-16 12:54:25 -0800914 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800915 }
916#endif
917}
918
919void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
920#if RENDER_LAYERS_AS_REGIONS
921 if (bounds.intersect(*mSnapshot->clipRect)) {
922 bounds.snapToPixelBoundaries();
923 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
924 if (!dirty.isEmpty()) {
925 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700926 }
927 }
928#endif
929}
930
Romain Guy54be1cd2011-06-13 19:04:27 -0700931void OpenGLRenderer::clearLayerRegions() {
932 const size_t count = mLayers.size();
933 if (count == 0) return;
934
935 if (!mSnapshot->isIgnored()) {
936 // Doing several glScissor/glClear here can negatively impact
937 // GPUs with a tiler architecture, instead we draw quads with
938 // the Clear blending mode
939
940 // The list contains bounds that have already been clipped
941 // against their initial clip rect, and the current clip
942 // is likely different so we need to disable clipping here
943 glDisable(GL_SCISSOR_TEST);
944
945 Vertex mesh[count * 6];
946 Vertex* vertex = mesh;
947
948 for (uint32_t i = 0; i < count; i++) {
949 Rect* bounds = mLayers.itemAt(i);
950
951 Vertex::set(vertex++, bounds->left, bounds->bottom);
952 Vertex::set(vertex++, bounds->left, bounds->top);
953 Vertex::set(vertex++, bounds->right, bounds->top);
954 Vertex::set(vertex++, bounds->left, bounds->bottom);
955 Vertex::set(vertex++, bounds->right, bounds->top);
956 Vertex::set(vertex++, bounds->right, bounds->bottom);
957
958 delete bounds;
959 }
960
961 setupDraw(false);
962 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
963 setupDrawBlending(true, SkXfermode::kClear_Mode);
964 setupDrawProgram();
965 setupDrawPureColorUniforms();
966 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800967 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700968
Romain Guy54be1cd2011-06-13 19:04:27 -0700969 glDrawArrays(GL_TRIANGLES, 0, count * 6);
970
971 glEnable(GL_SCISSOR_TEST);
972 } else {
973 for (uint32_t i = 0; i < count; i++) {
974 delete mLayers.itemAt(i);
975 }
976 }
977
978 mLayers.clear();
979}
980
Romain Guybd6b79b2010-06-26 00:13:53 -0700981///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700982// Transforms
983///////////////////////////////////////////////////////////////////////////////
984
985void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700986 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700987}
988
989void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700990 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700991}
992
993void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700994 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700995}
996
Romain Guy807daf72011-01-18 11:19:19 -0800997void OpenGLRenderer::skew(float sx, float sy) {
998 mSnapshot->transform->skew(sx, sy);
999}
1000
Romain Guyf6a11b82010-06-23 17:47:49 -07001001void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001002 if (matrix) {
1003 mSnapshot->transform->load(*matrix);
1004 } else {
1005 mSnapshot->transform->loadIdentity();
1006 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001007}
1008
1009void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001010 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001011}
1012
1013void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001014 SkMatrix transform;
1015 mSnapshot->transform->copyTo(transform);
1016 transform.preConcat(*matrix);
1017 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001018}
1019
1020///////////////////////////////////////////////////////////////////////////////
1021// Clipping
1022///////////////////////////////////////////////////////////////////////////////
1023
Romain Guybb9524b2010-06-22 18:56:38 -07001024void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001025 Rect clip(*mSnapshot->clipRect);
1026 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001027
1028 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1029 clip.getWidth(), clip.getHeight());
1030
Romain Guy746b7402010-10-26 16:27:31 -07001031 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -07001032}
1033
1034const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001035 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001036}
1037
Romain Guyc7d53492010-06-25 13:41:57 -07001038bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001039 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001040 return true;
1041 }
1042
Romain Guy1d83e192010-08-17 11:37:00 -07001043 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001044 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001045 r.snapToPixelBoundaries();
1046
1047 Rect clipRect(*mSnapshot->clipRect);
1048 clipRect.snapToPixelBoundaries();
1049
1050 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001051}
1052
Romain Guy079ba2c2010-07-16 14:12:24 -07001053bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1054 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001055 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001056 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001057 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001058 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001059}
1060
Romain Guyf6a11b82010-06-23 17:47:49 -07001061///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001062// Drawing commands
1063///////////////////////////////////////////////////////////////////////////////
1064
Romain Guy54be1cd2011-06-13 19:04:27 -07001065void OpenGLRenderer::setupDraw(bool clear) {
1066 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001067 if (mDirtyClip) {
1068 setScissorFromClip();
1069 }
1070 mDescription.reset();
1071 mSetShaderColor = false;
1072 mColorSet = false;
1073 mColorA = mColorR = mColorG = mColorB = 0.0f;
1074 mTextureUnit = 0;
1075 mTrackDirtyRegions = true;
1076}
1077
1078void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1079 mDescription.hasTexture = true;
1080 mDescription.hasAlpha8Texture = isAlpha8;
1081}
1082
Romain Guyaa6c24c2011-04-28 18:40:04 -07001083void OpenGLRenderer::setupDrawWithExternalTexture() {
1084 mDescription.hasExternalTexture = true;
1085}
1086
Romain Guy15bc6432011-12-13 13:11:32 -08001087void OpenGLRenderer::setupDrawNoTexture() {
1088 mCaches.disbaleTexCoordsVertexArray();
1089}
1090
Chet Haase5b0200b2011-04-13 17:58:08 -07001091void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001092 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001093}
1094
Romain Guyed6fcb02011-03-21 13:11:28 -07001095void OpenGLRenderer::setupDrawPoint(float pointSize) {
1096 mDescription.isPoint = true;
1097 mDescription.pointSize = pointSize;
1098}
1099
Romain Guy70ca14e2010-12-13 18:24:33 -08001100void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001101 setupDrawColor(color, (color >> 24) & 0xFF);
1102}
1103
1104void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1105 mColorA = alpha / 255.0f;
Chet Haasedb8c9a62012-03-21 18:54:18 -07001106 mColorA *= mSnapshot->alpha;
Chet Haase6cfdf452011-04-22 16:42:10 -07001107 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1108 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001109 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001110 mColorR = a * ((color >> 16) & 0xFF);
1111 mColorG = a * ((color >> 8) & 0xFF);
1112 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001113 mColorSet = true;
1114 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1115}
1116
Romain Guy86568192010-12-14 15:55:39 -08001117void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1118 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001119 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1120 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001121 const float a = mColorA / 255.0f;
1122 mColorR = a * ((color >> 16) & 0xFF);
1123 mColorG = a * ((color >> 8) & 0xFF);
1124 mColorB = a * ((color ) & 0xFF);
1125 mColorSet = true;
1126 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1127}
1128
Romain Guy70ca14e2010-12-13 18:24:33 -08001129void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1130 mColorA = a;
1131 mColorR = r;
1132 mColorG = g;
1133 mColorB = b;
1134 mColorSet = true;
1135 mSetShaderColor = mDescription.setColor(r, g, b, a);
1136}
1137
Romain Guy86568192010-12-14 15:55:39 -08001138void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1139 mColorA = a;
1140 mColorR = r;
1141 mColorG = g;
1142 mColorB = b;
1143 mColorSet = true;
1144 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1145}
1146
Romain Guy70ca14e2010-12-13 18:24:33 -08001147void OpenGLRenderer::setupDrawShader() {
1148 if (mShader) {
1149 mShader->describe(mDescription, mCaches.extensions);
1150 }
1151}
1152
1153void OpenGLRenderer::setupDrawColorFilter() {
1154 if (mColorFilter) {
1155 mColorFilter->describe(mDescription, mCaches.extensions);
1156 }
1157}
1158
Romain Guyf09ef512011-05-27 11:43:46 -07001159void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1160 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1161 mColorA = 1.0f;
1162 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001163 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001164 }
1165}
1166
Romain Guy70ca14e2010-12-13 18:24:33 -08001167void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001168 // When the blending mode is kClear_Mode, we need to use a modulate color
1169 // argb=1,0,0,0
1170 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001171 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1172 mDescription, swapSrcDst);
1173}
1174
1175void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001176 // When the blending mode is kClear_Mode, we need to use a modulate color
1177 // argb=1,0,0,0
1178 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001179 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1180 mDescription, swapSrcDst);
1181}
1182
1183void OpenGLRenderer::setupDrawProgram() {
1184 useProgram(mCaches.programCache.get(mDescription));
1185}
1186
1187void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1188 mTrackDirtyRegions = false;
1189}
1190
1191void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1192 bool ignoreTransform) {
1193 mModelView.loadTranslate(left, top, 0.0f);
1194 if (!ignoreTransform) {
1195 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1196 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1197 } else {
1198 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1199 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1200 }
1201}
1202
Chet Haase8a5cc922011-04-26 07:28:09 -07001203void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1204 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001205}
1206
Romain Guy70ca14e2010-12-13 18:24:33 -08001207void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1208 bool ignoreTransform, bool ignoreModelView) {
1209 if (!ignoreModelView) {
1210 mModelView.loadTranslate(left, top, 0.0f);
1211 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001212 } else {
1213 mModelView.loadIdentity();
1214 }
Romain Guy86568192010-12-14 15:55:39 -08001215 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1216 if (!ignoreTransform) {
1217 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1218 if (mTrackDirtyRegions && dirty) {
1219 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1220 }
1221 } else {
1222 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1223 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1224 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001225}
1226
Romain Guyed6fcb02011-03-21 13:11:28 -07001227void OpenGLRenderer::setupDrawPointUniforms() {
1228 int slot = mCaches.currentProgram->getUniform("pointSize");
1229 glUniform1f(slot, mDescription.pointSize);
1230}
1231
Romain Guy70ca14e2010-12-13 18:24:33 -08001232void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001233 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001234 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1235 }
1236}
1237
Romain Guy86568192010-12-14 15:55:39 -08001238void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001239 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001240 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001241 }
1242}
1243
Romain Guy70ca14e2010-12-13 18:24:33 -08001244void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1245 if (mShader) {
1246 if (ignoreTransform) {
1247 mModelView.loadInverse(*mSnapshot->transform);
1248 }
1249 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1250 }
1251}
1252
Romain Guy8d0d4782010-12-14 20:13:35 -08001253void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1254 if (mShader) {
1255 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1256 }
1257}
1258
Romain Guy70ca14e2010-12-13 18:24:33 -08001259void OpenGLRenderer::setupDrawColorFilterUniforms() {
1260 if (mColorFilter) {
1261 mColorFilter->setupProgram(mCaches.currentProgram);
1262 }
1263}
1264
1265void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001266 bool force = mCaches.bindMeshBuffer();
1267 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001268 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001269}
1270
1271void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1272 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001273 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001274 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001275}
1276
Romain Guyaa6c24c2011-04-28 18:40:04 -07001277void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1278 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001279 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001280 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001281}
1282
Romain Guy8f0095c2011-05-02 17:24:22 -07001283void OpenGLRenderer::setupDrawTextureTransform() {
1284 mDescription.hasTextureTransform = true;
1285}
1286
1287void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001288 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1289 GL_FALSE, &transform.data[0]);
1290}
1291
Romain Guy70ca14e2010-12-13 18:24:33 -08001292void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001293 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001294 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001295 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001296 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001297 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001298 }
Romain Guyd71dd362011-12-12 19:03:35 -08001299
Romain Guyf3a910b42011-12-12 20:35:21 -08001300 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001301 if (mCaches.currentProgram->texCoords >= 0) {
1302 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1303 }
1304
1305 mCaches.unbindIndicesBuffer();
1306}
1307
1308void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1309 bool force = mCaches.unbindMeshBuffer();
1310 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1311 if (mCaches.currentProgram->texCoords >= 0) {
1312 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001313 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001314}
1315
Chet Haase5b0200b2011-04-13 17:58:08 -07001316void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001317 bool force = mCaches.unbindMeshBuffer();
1318 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1319 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001320 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001321}
1322
1323/**
1324 * 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 -07001325 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1326 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1327 * attributes (one per vertex) are values from zero to one that tells the fragment
1328 * shader where the fragment is in relation to the line width/length overall; these values are
1329 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1330 * region of the line.
1331 * Note that we only pass down the width values in this setup function. The length coordinates
1332 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001333 */
Chet Haase99585ad2011-05-02 15:00:16 -07001334void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001335 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001336 bool force = mCaches.unbindMeshBuffer();
1337 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1338 vertices, gAAVertexStride);
1339 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001340 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001341
Romain Guy7b631422012-04-04 11:38:54 -07001342 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001343 glEnableVertexAttribArray(widthSlot);
1344 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001345
Romain Guy7b631422012-04-04 11:38:54 -07001346 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001347 glEnableVertexAttribArray(lengthSlot);
1348 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001349
Chet Haase5b0200b2011-04-13 17:58:08 -07001350 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001351 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001352
Chet Haase99585ad2011-05-02 15:00:16 -07001353 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001354 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Romain Guy7b631422012-04-04 11:38:54 -07001355 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion);
1356}
1357
1358void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1359 glDisableVertexAttribArray(widthSlot);
1360 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001361}
1362
Romain Guy70ca14e2010-12-13 18:24:33 -08001363void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001364}
1365
1366///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001367// Drawing
1368///////////////////////////////////////////////////////////////////////////////
1369
Romain Guy65549432012-03-26 16:45:05 -07001370status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
Romain Guy33f6beb2012-02-16 19:24:51 -08001371 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001372
1373 if (!USE_DISPLAY_LIST_PROPERTIES && quickReject(0, 0, width, height)) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001374 return false;
1375 }
1376
Romain Guy0fe478e2010-11-08 12:08:41 -08001377 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1378 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001379 if (displayList && displayList->isRenderable()) {
Chet Haasea1cff502012-02-21 13:43:44 -08001380 return displayList->replay(*this, width, height, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001381 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001382
Romain Guy65549432012-03-26 16:45:05 -07001383 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001384}
1385
Chet Haaseed30fd82011-04-22 16:18:45 -07001386void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1387 if (displayList) {
1388 displayList->output(*this, level);
1389 }
1390}
1391
Romain Guya168d732011-03-18 16:50:13 -07001392void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1393 int alpha;
1394 SkXfermode::Mode mode;
1395 getAlphaAndMode(paint, &alpha, &mode);
1396
Romain Guya168d732011-03-18 16:50:13 -07001397 float x = left;
1398 float y = top;
1399
Romain Guye3c26852011-07-25 16:36:01 -07001400 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001401 bool ignoreTransform = false;
1402 if (mSnapshot->transform->isPureTranslate()) {
1403 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1404 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1405 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001406 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001407 } else {
1408 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001409 }
1410
1411 setupDraw();
1412 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001413 if (paint) {
1414 setupDrawAlpha8Color(paint->getColor(), alpha);
1415 }
Romain Guya168d732011-03-18 16:50:13 -07001416 setupDrawColorFilter();
1417 setupDrawShader();
1418 setupDrawBlending(true, mode);
1419 setupDrawProgram();
1420 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001421
Romain Guya168d732011-03-18 16:50:13 -07001422 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001423 texture->setWrap(GL_CLAMP_TO_EDGE);
1424 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001425
Romain Guya168d732011-03-18 16:50:13 -07001426 setupDrawPureColorUniforms();
1427 setupDrawColorFilterUniforms();
1428 setupDrawShaderUniforms();
1429 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1430
1431 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1432
1433 finishDrawTexture();
1434}
1435
Chet Haase5c13d892010-10-08 08:37:55 -07001436void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c72e2010-07-12 20:20:03 -07001437 const float right = left + bitmap->width();
1438 const float bottom = top + bitmap->height();
1439
1440 if (quickReject(left, top, right, bottom)) {
1441 return;
1442 }
1443
Romain Guya1d3c912011-12-13 14:55:06 -08001444 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001445 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001446 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001447 const AutoTexture autoCleanup(texture);
1448
Romain Guy211370f2012-02-01 16:10:55 -08001449 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001450 drawAlphaBitmap(texture, left, top, paint);
1451 } else {
1452 drawTextureRect(left, top, right, bottom, texture, paint);
1453 }
Romain Guyce0537b2010-06-29 21:05:21 -07001454}
1455
Chet Haase5c13d892010-10-08 08:37:55 -07001456void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001457 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1458 const mat4 transform(*matrix);
1459 transform.mapRect(r);
1460
Romain Guy6926c72e2010-07-12 20:20:03 -07001461 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1462 return;
1463 }
1464
Romain Guya1d3c912011-12-13 14:55:06 -08001465 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001466 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001467 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001468 const AutoTexture autoCleanup(texture);
1469
Romain Guy5b3b3522010-10-27 18:57:51 -07001470 // This could be done in a cheaper way, all we need is pass the matrix
1471 // to the vertex shader. The save/restore is a bit overkill.
1472 save(SkCanvas::kMatrix_SaveFlag);
1473 concatMatrix(matrix);
1474 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1475 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001476}
1477
Romain Guy5a7b4662011-01-20 19:09:30 -08001478void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1479 float* vertices, int* colors, SkPaint* paint) {
1480 // TODO: Do a quickReject
1481 if (!vertices || mSnapshot->isIgnored()) {
1482 return;
1483 }
1484
Romain Guya1d3c912011-12-13 14:55:06 -08001485 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001486 Texture* texture = mCaches.textureCache.get(bitmap);
1487 if (!texture) return;
1488 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001489
Romain Guyd21b6e12011-11-30 20:21:23 -08001490 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1491 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001492
1493 int alpha;
1494 SkXfermode::Mode mode;
1495 getAlphaAndMode(paint, &alpha, &mode);
1496
Romain Guy5a7b4662011-01-20 19:09:30 -08001497 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001498
Romain Guyb18d2d02011-02-10 15:52:54 -08001499 float left = FLT_MAX;
1500 float top = FLT_MAX;
1501 float right = FLT_MIN;
1502 float bottom = FLT_MIN;
1503
1504#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001505 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001506#else
Romain Guy211370f2012-02-01 16:10:55 -08001507 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001508#endif
1509
Romain Guya566b7c2011-01-23 16:36:11 -08001510 // TODO: Support the colors array
1511 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001512 TextureVertex* vertex = mesh;
1513 for (int32_t y = 0; y < meshHeight; y++) {
1514 for (int32_t x = 0; x < meshWidth; x++) {
1515 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1516
1517 float u1 = float(x) / meshWidth;
1518 float u2 = float(x + 1) / meshWidth;
1519 float v1 = float(y) / meshHeight;
1520 float v2 = float(y + 1) / meshHeight;
1521
1522 int ax = i + (meshWidth + 1) * 2;
1523 int ay = ax + 1;
1524 int bx = i;
1525 int by = bx + 1;
1526 int cx = i + 2;
1527 int cy = cx + 1;
1528 int dx = i + (meshWidth + 1) * 2 + 2;
1529 int dy = dx + 1;
1530
1531 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1532 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1533 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1534
1535 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1536 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1537 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001538
1539#if RENDER_LAYERS_AS_REGIONS
1540 if (hasActiveLayer) {
1541 // TODO: This could be optimized to avoid unnecessary ops
1542 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1543 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1544 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1545 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1546 }
1547#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001548 }
1549 }
1550
Romain Guyb18d2d02011-02-10 15:52:54 -08001551#if RENDER_LAYERS_AS_REGIONS
1552 if (hasActiveLayer) {
1553 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1554 }
1555#endif
1556
Romain Guy5a7b4662011-01-20 19:09:30 -08001557 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1558 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001559 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001560}
1561
Romain Guy8ba548f2010-06-30 19:21:21 -07001562void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1563 float srcLeft, float srcTop, float srcRight, float srcBottom,
1564 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001565 SkPaint* paint) {
Romain Guy6926c72e2010-07-12 20:20:03 -07001566 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1567 return;
1568 }
1569
Romain Guya1d3c912011-12-13 14:55:06 -08001570 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001571 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001572 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001573 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001574
Romain Guy8ba548f2010-06-30 19:21:21 -07001575 const float width = texture->width;
1576 const float height = texture->height;
1577
Romain Guy68169722011-08-22 17:33:33 -07001578 const float u1 = fmax(0.0f, srcLeft / width);
1579 const float v1 = fmax(0.0f, srcTop / height);
1580 const float u2 = fmin(1.0f, srcRight / width);
1581 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001582
Romain Guy03750a02010-10-18 14:06:08 -07001583 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001584 resetDrawTextureTexCoords(u1, v1, u2, v2);
1585
Romain Guy03750a02010-10-18 14:06:08 -07001586 int alpha;
1587 SkXfermode::Mode mode;
1588 getAlphaAndMode(paint, &alpha, &mode);
1589
Romain Guyd21b6e12011-11-30 20:21:23 -08001590 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1591
Romain Guy211370f2012-02-01 16:10:55 -08001592 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001593 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1594 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1595
Romain Guyb5014982011-07-28 15:39:12 -07001596 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001597 // Enable linear filtering if the source rectangle is scaled
1598 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001599 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001600 }
Romain Guyb5014982011-07-28 15:39:12 -07001601
Romain Guyd21b6e12011-11-30 20:21:23 -08001602 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001603 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1604 texture->id, alpha / 255.0f, mode, texture->blend,
1605 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1606 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1607 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001608 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001609 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1610 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1611 GL_TRIANGLE_STRIP, gMeshCount);
1612 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001613
1614 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1615}
1616
Romain Guy4aa90572010-09-26 18:40:37 -07001617void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001618 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001619 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c72e2010-07-12 20:20:03 -07001620 if (quickReject(left, top, right, bottom)) {
1621 return;
1622 }
1623
Romain Guya1d3c912011-12-13 14:55:06 -08001624 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001625 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001626 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001627 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001628 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1629 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001630
1631 int alpha;
1632 SkXfermode::Mode mode;
1633 getAlphaAndMode(paint, &alpha, &mode);
1634
Romain Guy2728f962010-10-08 18:36:15 -07001635 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001636 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001637
Romain Guy211370f2012-02-01 16:10:55 -08001638 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001639 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001640#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001641 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001642 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001643 const float offsetX = left + mSnapshot->transform->getTranslateX();
1644 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001645 const size_t count = mesh->quads.size();
1646 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001647 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001648 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001649 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1650 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1651 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001652 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001653 dirtyLayer(left + bounds.left, top + bounds.top,
1654 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001655 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001656 }
1657 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001658#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001659
Romain Guy211370f2012-02-01 16:10:55 -08001660 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001661 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1662 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1663
1664 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1665 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1666 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1667 true, !mesh->hasEmptyQuads);
1668 } else {
1669 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1670 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1671 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1672 true, !mesh->hasEmptyQuads);
1673 }
Romain Guy054dc182010-10-15 17:55:25 -07001674 }
Romain Guyf7f93552010-07-08 19:17:03 -07001675}
1676
Chet Haase99ecdc42011-05-06 12:06:34 -07001677/**
Chet Haase858aa932011-05-12 09:06:00 -07001678 * This function uses a similar approach to that of AA lines in the drawLines() function.
1679 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1680 * shader to compute the translucency of the color, determined by whether a given pixel is
1681 * within that boundary region and how far into the region it is.
1682 */
1683void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001684 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001685 float inverseScaleX = 1.0f;
1686 float inverseScaleY = 1.0f;
1687 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001688 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001689 Matrix4 *mat = mSnapshot->transform;
1690 float m00 = mat->data[Matrix4::kScaleX];
1691 float m01 = mat->data[Matrix4::kSkewY];
1692 float m02 = mat->data[2];
1693 float m10 = mat->data[Matrix4::kSkewX];
1694 float m11 = mat->data[Matrix4::kScaleX];
1695 float m12 = mat->data[6];
1696 float scaleX = sqrt(m00 * m00 + m01 * m01);
1697 float scaleY = sqrt(m10 * m10 + m11 * m11);
1698 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1699 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1700 }
1701
1702 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001703 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001704 setupDrawAALine();
1705 setupDrawColor(color);
1706 setupDrawColorFilter();
1707 setupDrawShader();
1708 setupDrawBlending(true, mode);
1709 setupDrawProgram();
1710 setupDrawModelViewIdentity(true);
1711 setupDrawColorUniforms();
1712 setupDrawColorFilterUniforms();
1713 setupDrawShaderIdentityUniforms();
1714
1715 AAVertex rects[4];
1716 AAVertex* aaVertices = &rects[0];
1717 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1718 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1719
1720 float boundarySizeX = .5 * inverseScaleX;
1721 float boundarySizeY = .5 * inverseScaleY;
1722
1723 // Adjust the rect by the AA boundary padding
1724 left -= boundarySizeX;
1725 right += boundarySizeX;
1726 top -= boundarySizeY;
1727 bottom += boundarySizeY;
1728
1729 float width = right - left;
1730 float height = bottom - top;
1731
Romain Guy7b631422012-04-04 11:38:54 -07001732 int widthSlot;
1733 int lengthSlot;
1734
Chet Haase858aa932011-05-12 09:06:00 -07001735 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1736 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001737 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1738 boundaryWidthProportion, widthSlot, lengthSlot);
1739
Chet Haase858aa932011-05-12 09:06:00 -07001740 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1741 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1742 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001743 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion));
Chet Haase858aa932011-05-12 09:06:00 -07001744
1745 if (!quickReject(left, top, right, bottom)) {
1746 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1747 AAVertex::set(aaVertices++, left, top, 1, 0);
1748 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1749 AAVertex::set(aaVertices++, right, top, 0, 0);
1750 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1751 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1752 }
Romain Guy7b631422012-04-04 11:38:54 -07001753
1754 finishDrawAALine(widthSlot, lengthSlot);
Chet Haase858aa932011-05-12 09:06:00 -07001755}
1756
1757/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001758 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1759 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1760 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1761 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1762 * of the line. Hairlines are more involved because we need to account for transform scaling
1763 * to end up with a one-pixel-wide line in screen space..
1764 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1765 * in combination with values that we calculate and pass down in this method. The basic approach
1766 * is that the quad we create contains both the core line area plus a bounding area in which
1767 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1768 * proportion of the width and the length of a given segment is represented by the boundary
1769 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1770 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1771 * on the inside). This ends up giving the result we want, with pixels that are completely
1772 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1773 * how far into the boundary region they are, which is determined by shader interpolation.
1774 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001775void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1776 if (mSnapshot->isIgnored()) return;
1777
1778 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001779 // We use half the stroke width here because we're going to position the quad
1780 // corner vertices half of the width away from the line endpoints
1781 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001782 // A stroke width of 0 has a special meaning in Skia:
1783 // it draws a line 1 px wide regardless of current transform
1784 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001785
Chet Haase99ecdc42011-05-06 12:06:34 -07001786 float inverseScaleX = 1.0f;
1787 float inverseScaleY = 1.0f;
1788 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001789
Chet Haase8a5cc922011-04-26 07:28:09 -07001790 int alpha;
1791 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001792
Chet Haase8a5cc922011-04-26 07:28:09 -07001793 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001794 int verticesCount = count;
1795 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001796 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001797 verticesCount += (count - 4);
1798 }
1799
1800 if (isHairLine || isAA) {
1801 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1802 // the line on the screen should always be one pixel wide regardless of scale. For
1803 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001804 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001805 Matrix4 *mat = mSnapshot->transform;
1806 float m00 = mat->data[Matrix4::kScaleX];
1807 float m01 = mat->data[Matrix4::kSkewY];
1808 float m02 = mat->data[2];
1809 float m10 = mat->data[Matrix4::kSkewX];
1810 float m11 = mat->data[Matrix4::kScaleX];
1811 float m12 = mat->data[6];
Romain Guy7b631422012-04-04 11:38:54 -07001812
Romain Guy211370f2012-02-01 16:10:55 -08001813 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1814 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001815
Chet Haase99ecdc42011-05-06 12:06:34 -07001816 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1817 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001818
Chet Haase99ecdc42011-05-06 12:06:34 -07001819 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1820 scaled = true;
1821 }
1822 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001823 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001824
1825 getAlphaAndMode(paint, &alpha, &mode);
1826 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001827 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001828 if (isAA) {
1829 setupDrawAALine();
1830 }
1831 setupDrawColor(paint->getColor(), alpha);
1832 setupDrawColorFilter();
1833 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001834 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001835 setupDrawProgram();
1836 setupDrawModelViewIdentity(true);
1837 setupDrawColorUniforms();
1838 setupDrawColorFilterUniforms();
1839 setupDrawShaderIdentityUniforms();
1840
1841 if (isHairLine) {
1842 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001843 halfStrokeWidth = isAA? 1 : .5;
1844 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001845 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001846 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001847 }
Romain Guy7b631422012-04-04 11:38:54 -07001848
1849 int widthSlot;
1850 int lengthSlot;
1851
Chet Haase5b0200b2011-04-13 17:58:08 -07001852 Vertex lines[verticesCount];
1853 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001854
Chet Haase99585ad2011-05-02 15:00:16 -07001855 AAVertex wLines[verticesCount];
1856 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001857
Romain Guy211370f2012-02-01 16:10:55 -08001858 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001859 setupDrawVertices(vertices);
1860 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001861 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1862 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001863 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001864 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1865 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001866 // We will need to calculate the actual width proportion on each segment for
1867 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1868 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001869 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1870 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001871 }
1872
Chet Haase99ecdc42011-05-06 12:06:34 -07001873 AAVertex* prevAAVertex = NULL;
1874 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001875
Chet Haase99585ad2011-05-02 15:00:16 -07001876 int boundaryLengthSlot = -1;
1877 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001878 int boundaryWidthSlot = -1;
1879 int inverseBoundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001880
Chet Haase5b0200b2011-04-13 17:58:08 -07001881 for (int i = 0; i < count; i += 4) {
1882 // a = start point, b = end point
1883 vec2 a(points[i], points[i + 1]);
1884 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001885
Chet Haase99585ad2011-05-02 15:00:16 -07001886 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001887 float boundaryLengthProportion = 0;
1888 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001889
Chet Haase5b0200b2011-04-13 17:58:08 -07001890 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001891 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001892 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001893 if (isAA) {
1894 float wideningFactor;
1895 if (fabs(n.x) >= fabs(n.y)) {
1896 wideningFactor = fabs(1.0f / n.x);
1897 } else {
1898 wideningFactor = fabs(1.0f / n.y);
1899 }
1900 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001901 }
Romain Guy7b631422012-04-04 11:38:54 -07001902
Chet Haase99ecdc42011-05-06 12:06:34 -07001903 if (scaled) {
1904 n.x *= inverseScaleX;
1905 n.y *= inverseScaleY;
1906 }
1907 } else if (scaled) {
1908 // Extend n by .5 pixel on each side, post-transform
1909 vec2 extendedN = n.copyNormalized();
1910 extendedN /= 2;
1911 extendedN.x *= inverseScaleX;
1912 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07001913
Chet Haase99ecdc42011-05-06 12:06:34 -07001914 float extendedNLength = extendedN.length();
1915 // We need to set this value on the shader prior to drawing
1916 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1917 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001918 }
Romain Guy7b631422012-04-04 11:38:54 -07001919
Chet Haase5b0200b2011-04-13 17:58:08 -07001920 float x = n.x;
1921 n.x = -n.y;
1922 n.y = x;
1923
Chet Haase99585ad2011-05-02 15:00:16 -07001924 // aa lines expand the endpoint vertices to encompass the AA boundary
1925 if (isAA) {
1926 vec2 abVector = (b - a);
1927 length = abVector.length();
1928 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07001929
Chet Haase99ecdc42011-05-06 12:06:34 -07001930 if (scaled) {
1931 abVector.x *= inverseScaleX;
1932 abVector.y *= inverseScaleY;
1933 float abLength = abVector.length();
1934 boundaryLengthProportion = abLength / (length + abLength);
1935 } else {
1936 boundaryLengthProportion = .5 / (length + 1);
1937 }
Romain Guy7b631422012-04-04 11:38:54 -07001938
Chet Haase99ecdc42011-05-06 12:06:34 -07001939 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001940 a -= abVector;
1941 b += abVector;
1942 }
1943
Chet Haase5b0200b2011-04-13 17:58:08 -07001944 // Four corners of the rectangle defining a thick line
1945 vec2 p1 = a - n;
1946 vec2 p2 = a + n;
1947 vec2 p3 = b + n;
1948 vec2 p4 = b - n;
1949
Chet Haase99585ad2011-05-02 15:00:16 -07001950
Chet Haase5b0200b2011-04-13 17:58:08 -07001951 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1952 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1953 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1954 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1955
1956 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001957 if (!isAA) {
1958 if (prevVertex != NULL) {
1959 // Issue two repeat vertices to create degenerate triangles to bridge
1960 // between the previous line and the new one. This is necessary because
1961 // we are creating a single triangle_strip which will contain
1962 // potentially discontinuous line segments.
1963 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1964 Vertex::set(vertices++, p1.x, p1.y);
1965 generatedVerticesCount += 2;
1966 }
Romain Guy7b631422012-04-04 11:38:54 -07001967
Chet Haase5b0200b2011-04-13 17:58:08 -07001968 Vertex::set(vertices++, p1.x, p1.y);
1969 Vertex::set(vertices++, p2.x, p2.y);
1970 Vertex::set(vertices++, p4.x, p4.y);
1971 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07001972
Chet Haase5b0200b2011-04-13 17:58:08 -07001973 prevVertex = vertices - 1;
1974 generatedVerticesCount += 4;
1975 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001976 if (!isHairLine && scaled) {
1977 // Must set width proportions per-segment for scaled non-hairlines to use the
1978 // correct AA boundary dimensions
1979 if (boundaryWidthSlot < 0) {
1980 boundaryWidthSlot =
1981 mCaches.currentProgram->getUniform("boundaryWidth");
1982 inverseBoundaryWidthSlot =
1983 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1984 }
Romain Guy7b631422012-04-04 11:38:54 -07001985
Chet Haase99ecdc42011-05-06 12:06:34 -07001986 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1987 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1988 }
Romain Guy7b631422012-04-04 11:38:54 -07001989
Chet Haase99585ad2011-05-02 15:00:16 -07001990 if (boundaryLengthSlot < 0) {
1991 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1992 inverseBoundaryLengthSlot =
1993 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1994 }
Romain Guy7b631422012-04-04 11:38:54 -07001995
Chet Haase99ecdc42011-05-06 12:06:34 -07001996 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1997 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001998
Chet Haase5b0200b2011-04-13 17:58:08 -07001999 if (prevAAVertex != NULL) {
2000 // Issue two repeat vertices to create degenerate triangles to bridge
2001 // between the previous line and the new one. This is necessary because
2002 // we are creating a single triangle_strip which will contain
2003 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002004 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2005 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2006 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002007 generatedVerticesCount += 2;
2008 }
Romain Guy7b631422012-04-04 11:38:54 -07002009
Chet Haase99585ad2011-05-02 15:00:16 -07002010 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2011 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2012 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2013 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002014
Chet Haase5b0200b2011-04-13 17:58:08 -07002015 prevAAVertex = aaVertices - 1;
2016 generatedVerticesCount += 4;
2017 }
Romain Guy7b631422012-04-04 11:38:54 -07002018
Chet Haase99585ad2011-05-02 15:00:16 -07002019 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2020 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2021 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002022 }
2023 }
Romain Guy7b631422012-04-04 11:38:54 -07002024
Chet Haase5b0200b2011-04-13 17:58:08 -07002025 if (generatedVerticesCount > 0) {
2026 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2027 }
Romain Guy7b631422012-04-04 11:38:54 -07002028
2029 if (isAA) {
2030 finishDrawAALine(widthSlot, lengthSlot);
2031 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002032}
2033
Romain Guyed6fcb02011-03-21 13:11:28 -07002034void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2035 if (mSnapshot->isIgnored()) return;
2036
2037 // TODO: The paint's cap style defines whether the points are square or circular
2038 // TODO: Handle AA for round points
2039
Chet Haase5b0200b2011-04-13 17:58:08 -07002040 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002041 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002042 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002043 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002044 if (isHairLine) {
2045 // Now that we know it's hairline, we can set the effective width, to be used later
2046 strokeWidth = 1.0f;
2047 }
2048 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002049 int alpha;
2050 SkXfermode::Mode mode;
2051 getAlphaAndMode(paint, &alpha, &mode);
2052
2053 int verticesCount = count >> 1;
2054 int generatedVerticesCount = 0;
2055
2056 TextureVertex pointsData[verticesCount];
2057 TextureVertex* vertex = &pointsData[0];
2058
2059 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002060 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002061 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002062 setupDrawColor(paint->getColor(), alpha);
2063 setupDrawColorFilter();
2064 setupDrawShader();
2065 setupDrawBlending(mode);
2066 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002067 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002068 setupDrawColorUniforms();
2069 setupDrawColorFilterUniforms();
2070 setupDrawPointUniforms();
2071 setupDrawShaderIdentityUniforms();
2072 setupDrawMesh(vertex);
2073
2074 for (int i = 0; i < count; i += 2) {
2075 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2076 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002077
Chet Haase8a5cc922011-04-26 07:28:09 -07002078 float left = points[i] - halfWidth;
2079 float right = points[i] + halfWidth;
2080 float top = points[i + 1] - halfWidth;
2081 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002082
Chet Haase8a5cc922011-04-26 07:28:09 -07002083 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002084 }
2085
2086 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
2087}
2088
Romain Guy85bf02f2010-06-22 13:11:24 -07002089void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002090 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08002091 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002092
Romain Guyae88e5e2010-10-22 17:49:18 -07002093 Rect& clip(*mSnapshot->clipRect);
2094 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002095
Romain Guy3d58c032010-07-14 16:34:53 -07002096 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07002097}
Romain Guy9d5316e2010-06-24 19:30:36 -07002098
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002099void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08002100 if (!texture) return;
2101 const AutoTexture autoCleanup(texture);
2102
2103 const float x = left + texture->left - texture->offset;
2104 const float y = top + texture->top - texture->offset;
2105
2106 drawPathTexture(texture, x, y, paint);
2107}
2108
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002109void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2110 float rx, float ry, SkPaint* paint) {
2111 if (mSnapshot->isIgnored()) return;
2112
Romain Guya1d3c912011-12-13 14:55:06 -08002113 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002114 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2115 right - left, bottom - top, rx, ry, paint);
2116 drawShape(left, top, texture, paint);
2117}
2118
Romain Guy01d58e42011-01-19 21:54:02 -08002119void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2120 if (mSnapshot->isIgnored()) return;
2121
Romain Guya1d3c912011-12-13 14:55:06 -08002122 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002123 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002124 drawShape(x - radius, y - radius, texture, paint);
2125}
Romain Guy01d58e42011-01-19 21:54:02 -08002126
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002127void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2128 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002129
Romain Guya1d3c912011-12-13 14:55:06 -08002130 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002131 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2132 drawShape(left, top, texture, paint);
2133}
2134
Romain Guy8b2f5262011-01-23 16:15:02 -08002135void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2136 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2137 if (mSnapshot->isIgnored()) return;
2138
2139 if (fabs(sweepAngle) >= 360.0f) {
2140 drawOval(left, top, right, bottom, paint);
2141 return;
2142 }
2143
Romain Guya1d3c912011-12-13 14:55:06 -08002144 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002145 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2146 startAngle, sweepAngle, useCenter, paint);
2147 drawShape(left, top, texture, paint);
2148}
2149
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002150void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2151 SkPaint* paint) {
2152 if (mSnapshot->isIgnored()) return;
2153
Romain Guya1d3c912011-12-13 14:55:06 -08002154 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002155 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2156 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002157}
2158
Chet Haase5c13d892010-10-08 08:37:55 -07002159void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002160 if (p->getStyle() != SkPaint::kFill_Style) {
2161 drawRectAsShape(left, top, right, bottom, p);
2162 return;
2163 }
2164
Romain Guy6926c72e2010-07-12 20:20:03 -07002165 if (quickReject(left, top, right, bottom)) {
2166 return;
2167 }
2168
Romain Guy026c5e162010-06-28 17:12:22 -07002169 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002170 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002171 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2172 if (!isMode) {
2173 // Assume SRC_OVER
2174 mode = SkXfermode::kSrcOver_Mode;
2175 }
2176 } else {
2177 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002178 }
2179
Romain Guy026c5e162010-06-28 17:12:22 -07002180 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002181 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002182 drawAARect(left, top, right, bottom, color, mode);
2183 } else {
2184 drawColorRect(left, top, right, bottom, color, mode);
2185 }
Romain Guyc7d53492010-06-25 13:41:57 -07002186}
Romain Guy9d5316e2010-06-24 19:30:36 -07002187
Romain Guyeb9a5362012-01-17 17:39:26 -08002188void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
2189 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002190 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2191 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guyeb9a5362012-01-17 17:39:26 -08002192 return;
2193 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002194
Romain Guy671d6cf2012-01-18 12:39:17 -08002195 // NOTE: Skia does not support perspective transform on drawPosText yet
2196 if (!mSnapshot->transform->isSimple()) {
2197 return;
2198 }
2199
2200 float x = 0.0f;
2201 float y = 0.0f;
2202 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2203 if (pureTranslate) {
2204 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2205 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2206 }
2207
2208 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2209 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2210 paint->getTextSize());
2211
2212 int alpha;
2213 SkXfermode::Mode mode;
2214 getAlphaAndMode(paint, &alpha, &mode);
2215
2216 // Pick the appropriate texture filtering
2217 bool linearFilter = mSnapshot->transform->changesBounds();
2218 if (pureTranslate && !linearFilter) {
2219 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2220 }
2221
2222 mCaches.activeTexture(0);
2223 setupDraw();
2224 setupDrawDirtyRegionsDisabled();
2225 setupDrawWithTexture(true);
2226 setupDrawAlpha8Color(paint->getColor(), alpha);
2227 setupDrawColorFilter();
2228 setupDrawShader();
2229 setupDrawBlending(true, mode);
2230 setupDrawProgram();
2231 setupDrawModelView(x, y, x, y, pureTranslate, true);
2232 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2233 setupDrawPureColorUniforms();
2234 setupDrawColorFilterUniforms();
2235 setupDrawShaderUniforms(pureTranslate);
2236
2237 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2238 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2239
2240#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002241 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002242#else
Romain Guy211370f2012-02-01 16:10:55 -08002243 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002244#endif
2245
2246 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2247 positions, hasActiveLayer ? &bounds : NULL)) {
2248#if RENDER_LAYERS_AS_REGIONS
2249 if (hasActiveLayer) {
2250 if (!pureTranslate) {
2251 mSnapshot->transform->mapRect(bounds);
2252 }
2253 dirtyLayerUnchecked(bounds, getRegion());
2254 }
2255#endif
2256 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002257}
2258
Romain Guye8e62a42010-07-23 18:55:21 -07002259void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002260 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002261 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2262 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07002263 return;
2264 }
2265
Chet Haasea1cff502012-02-21 13:43:44 -08002266 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002267 switch (paint->getTextAlign()) {
2268 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002269 x -= length / 2.0f;
2270 break;
2271 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002272 x -= length;
2273 break;
2274 default:
2275 break;
2276 }
2277
Romain Guycac5fd32011-12-01 20:08:50 -08002278 SkPaint::FontMetrics metrics;
2279 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002280 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Romain Guycac5fd32011-12-01 20:08:50 -08002281 return;
2282 }
2283
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002284 const float oldX = x;
2285 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002286 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002287 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002288 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2289 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2290 }
2291
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002292#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002293 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002294#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002295
2296 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002297 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002298 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002299
Romain Guy86568192010-12-14 15:55:39 -08002300 int alpha;
2301 SkXfermode::Mode mode;
2302 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002303
Romain Guy211370f2012-02-01 16:10:55 -08002304 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002305 mCaches.activeTexture(0);
2306
Romain Guyb45c0c92010-08-26 20:35:23 -07002307 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002308 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2309 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002310 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002311
Romain Guy740bf2b2011-04-26 15:33:10 -07002312 const float sx = oldX - shadow->left + mShadowDx;
2313 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002314
Romain Guy86568192010-12-14 15:55:39 -08002315 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002316 int shadowColor = mShadowColor;
2317 if (mShader) {
2318 shadowColor = 0xffffffff;
2319 }
Romain Guy86568192010-12-14 15:55:39 -08002320
Romain Guy86568192010-12-14 15:55:39 -08002321 setupDraw();
2322 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002323 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2324 setupDrawColorFilter();
2325 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002326 setupDrawBlending(true, mode);
2327 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002328 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002329 setupDrawTexture(shadow->id);
2330 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002331 setupDrawColorFilterUniforms();
2332 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002333 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2334
Romain Guy0a417492010-08-16 20:26:20 -07002335 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002336 }
2337
Romain Guy6620c6d2010-12-06 18:07:02 -08002338 // Pick the appropriate texture filtering
2339 bool linearFilter = mSnapshot->transform->changesBounds();
2340 if (pureTranslate && !linearFilter) {
2341 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2342 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002343
Romain Guya1d3c912011-12-13 14:55:06 -08002344 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002345 setupDraw();
2346 setupDrawDirtyRegionsDisabled();
2347 setupDrawWithTexture(true);
2348 setupDrawAlpha8Color(paint->getColor(), alpha);
2349 setupDrawColorFilter();
2350 setupDrawShader();
2351 setupDrawBlending(true, mode);
2352 setupDrawProgram();
2353 setupDrawModelView(x, y, x, y, pureTranslate, true);
2354 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2355 setupDrawPureColorUniforms();
2356 setupDrawColorFilterUniforms();
2357 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002358
Romain Guy6620c6d2010-12-06 18:07:02 -08002359 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002360 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2361
2362#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002363 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002364#else
Romain Guy211370f2012-02-01 16:10:55 -08002365 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002366#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002367
Romain Guy6620c6d2010-12-06 18:07:02 -08002368 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002369 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002370#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002371 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002372 if (!pureTranslate) {
2373 mSnapshot->transform->mapRect(bounds);
2374 }
Romain Guyf219da52011-01-16 12:54:25 -08002375 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002376 }
2377#endif
2378 }
Romain Guy694b5192010-07-21 21:33:20 -07002379
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002380 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002381}
2382
Romain Guy325740f2012-02-24 16:48:34 -08002383void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
2384 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002385 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2386 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
2387 return;
2388 }
2389
Romain Guy03d58522012-02-24 17:54:07 -08002390 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2391 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2392 paint->getTextSize());
2393
2394 int alpha;
2395 SkXfermode::Mode mode;
2396 getAlphaAndMode(paint, &alpha, &mode);
2397
2398 mCaches.activeTexture(0);
2399 setupDraw();
2400 setupDrawDirtyRegionsDisabled();
2401 setupDrawWithTexture(true);
2402 setupDrawAlpha8Color(paint->getColor(), alpha);
2403 setupDrawColorFilter();
2404 setupDrawShader();
2405 setupDrawBlending(true, mode);
2406 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002407 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002408 setupDrawTexture(fontRenderer.getTexture(true));
2409 setupDrawPureColorUniforms();
2410 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002411 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002412
Romain Guy97771732012-02-28 18:17:02 -08002413 const Rect* clip = &mSnapshot->getLocalClip();
2414 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 -08002415
Romain Guy97771732012-02-28 18:17:02 -08002416#if RENDER_LAYERS_AS_REGIONS
2417 const bool hasActiveLayer = hasLayer();
2418#else
2419 const bool hasActiveLayer = false;
2420#endif
2421
2422 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2423 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2424#if RENDER_LAYERS_AS_REGIONS
2425 if (hasActiveLayer) {
2426 mSnapshot->transform->mapRect(bounds);
2427 dirtyLayerUnchecked(bounds, getRegion());
2428 }
2429#endif
2430 }
Romain Guy325740f2012-02-24 16:48:34 -08002431}
2432
Romain Guy7fbcc042010-08-04 15:40:07 -07002433void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002434 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002435
Romain Guya1d3c912011-12-13 14:55:06 -08002436 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002437
Romain Guy33f6beb2012-02-16 19:24:51 -08002438 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002439 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002440 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002441 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002442
Romain Guy8b55f372010-08-18 17:10:07 -07002443 const float x = texture->left - texture->offset;
2444 const float y = texture->top - texture->offset;
2445
Romain Guy01d58e42011-01-19 21:54:02 -08002446 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002447}
2448
Romain Guyada830f2011-01-13 12:13:20 -08002449void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2450 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002451 return;
2452 }
2453
Romain Guy2bf68f02012-03-02 13:37:47 -08002454 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2455 OpenGLRenderer* renderer = layer->renderer;
2456 Rect& dirty = layer->dirtyRect;
2457
2458 interrupt();
2459 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2460 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
2461 renderer->drawDisplayList(layer->displayList, layer->getWidth(), layer->getHeight(),
2462 dirty, DisplayList::kReplayFlag_ClipChildren);
2463 renderer->finish();
2464 resume();
2465
2466 dirty.setEmpty();
2467 layer->deferredUpdateScheduled = false;
2468 layer->renderer = NULL;
2469 layer->displayList = NULL;
2470 }
2471
Romain Guya1d3c912011-12-13 14:55:06 -08002472 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002473
2474 int alpha;
2475 SkXfermode::Mode mode;
2476 getAlphaAndMode(paint, &alpha, &mode);
2477
Romain Guy9ace8f52011-07-07 20:50:11 -07002478 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002479
Romain Guyf219da52011-01-16 12:54:25 -08002480#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002481 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002482 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002483 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002484 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002485 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002486 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002487
Romain Guyc88e3572011-01-22 00:32:12 -08002488 setupDraw();
2489 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002490 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002491 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002492 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002493 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002494 setupDrawPureColorUniforms();
2495 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002496 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002497 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002498 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2499 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2500
Romain Guyd21b6e12011-11-30 20:21:23 -08002501 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002502 setupDrawModelViewTranslate(x, y,
2503 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2504 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002505 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002506 setupDrawModelViewTranslate(x, y,
2507 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2508 }
Romain Guyc88e3572011-01-22 00:32:12 -08002509 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002510
Romain Guyc88e3572011-01-22 00:32:12 -08002511 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2512 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002513
Romain Guyc88e3572011-01-22 00:32:12 -08002514 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002515
2516#if DEBUG_LAYERS_AS_REGIONS
2517 drawRegionRects(layer->region);
2518#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002519 }
Romain Guyf219da52011-01-16 12:54:25 -08002520 }
2521#else
Romain Guyada830f2011-01-13 12:13:20 -08002522 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2523 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002524#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002525}
2526
Romain Guy6926c72e2010-07-12 20:20:03 -07002527///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002528// Shaders
2529///////////////////////////////////////////////////////////////////////////////
2530
2531void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002532 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002533}
2534
Romain Guy06f96e22010-07-30 19:18:16 -07002535void OpenGLRenderer::setupShader(SkiaShader* shader) {
2536 mShader = shader;
2537 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002538 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002539 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002540}
2541
Romain Guyd27977d2010-07-14 19:18:51 -07002542///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002543// Color filters
2544///////////////////////////////////////////////////////////////////////////////
2545
2546void OpenGLRenderer::resetColorFilter() {
2547 mColorFilter = NULL;
2548}
2549
2550void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2551 mColorFilter = filter;
2552}
2553
2554///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002555// Drop shadow
2556///////////////////////////////////////////////////////////////////////////////
2557
2558void OpenGLRenderer::resetShadow() {
2559 mHasShadow = false;
2560}
2561
2562void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2563 mHasShadow = true;
2564 mShadowRadius = radius;
2565 mShadowDx = dx;
2566 mShadowDy = dy;
2567 mShadowColor = color;
2568}
2569
2570///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002571// Draw filters
2572///////////////////////////////////////////////////////////////////////////////
2573
2574void OpenGLRenderer::resetPaintFilter() {
2575 mHasDrawFilter = false;
2576}
2577
2578void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2579 mHasDrawFilter = true;
2580 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2581 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2582}
2583
2584SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002585 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002586
2587 uint32_t flags = paint->getFlags();
2588
2589 mFilteredPaint = *paint;
2590 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2591
2592 return &mFilteredPaint;
2593}
2594
2595///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c72e2010-07-12 20:20:03 -07002596// Drawing implementation
2597///////////////////////////////////////////////////////////////////////////////
2598
Romain Guy01d58e42011-01-19 21:54:02 -08002599void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2600 float x, float y, SkPaint* paint) {
2601 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2602 return;
2603 }
2604
2605 int alpha;
2606 SkXfermode::Mode mode;
2607 getAlphaAndMode(paint, &alpha, &mode);
2608
2609 setupDraw();
2610 setupDrawWithTexture(true);
2611 setupDrawAlpha8Color(paint->getColor(), alpha);
2612 setupDrawColorFilter();
2613 setupDrawShader();
2614 setupDrawBlending(true, mode);
2615 setupDrawProgram();
2616 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2617 setupDrawTexture(texture->id);
2618 setupDrawPureColorUniforms();
2619 setupDrawColorFilterUniforms();
2620 setupDrawShaderUniforms();
2621 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2622
2623 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2624
2625 finishDrawTexture();
2626}
2627
Romain Guyf607bdc2010-09-10 19:20:06 -07002628// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002629#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2630#define kStdUnderline_Offset (1.0f / 9.0f)
2631#define kStdUnderline_Thickness (1.0f / 18.0f)
2632
2633void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2634 float x, float y, SkPaint* paint) {
2635 // Handle underline and strike-through
2636 uint32_t flags = paint->getFlags();
2637 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002638 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002639 float underlineWidth = length;
2640 // If length is > 0.0f, we already measured the text for the text alignment
2641 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002642 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002643 }
2644
2645 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002646 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002647 case SkPaint::kCenter_Align:
2648 offsetX = underlineWidth * 0.5f;
2649 break;
2650 case SkPaint::kRight_Align:
2651 offsetX = underlineWidth;
2652 break;
2653 default:
2654 break;
2655 }
2656
Romain Guy211370f2012-02-01 16:10:55 -08002657 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002658 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002659 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002660
Romain Guye20ecbd2010-09-22 19:49:04 -07002661 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002662 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002663
Romain Guyf6834472011-01-23 13:32:12 -08002664 int linesCount = 0;
2665 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2666 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2667
2668 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002669 float points[pointsCount];
2670 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002671
2672 if (flags & SkPaint::kUnderlineText_Flag) {
2673 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002674 points[currentPoint++] = left;
2675 points[currentPoint++] = top;
2676 points[currentPoint++] = left + underlineWidth;
2677 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002678 }
2679
2680 if (flags & SkPaint::kStrikeThruText_Flag) {
2681 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002682 points[currentPoint++] = left;
2683 points[currentPoint++] = top;
2684 points[currentPoint++] = left + underlineWidth;
2685 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002686 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002687
Romain Guy726aeba2011-06-01 14:52:00 -07002688 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002689
Romain Guy726aeba2011-06-01 14:52:00 -07002690 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002691 }
2692 }
2693}
2694
Romain Guy026c5e162010-06-28 17:12:22 -07002695void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002696 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002697 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002698 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002699 color |= 0x00ffffff;
2700 }
2701
Romain Guy70ca14e2010-12-13 18:24:33 -08002702 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002703 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002704 setupDrawColor(color);
2705 setupDrawShader();
2706 setupDrawColorFilter();
2707 setupDrawBlending(mode);
2708 setupDrawProgram();
2709 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2710 setupDrawColorUniforms();
2711 setupDrawShaderUniforms(ignoreTransform);
2712 setupDrawColorFilterUniforms();
2713 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002714
Romain Guyc95c8d62010-09-17 15:31:32 -07002715 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2716}
2717
Romain Guy82ba8142010-07-09 13:25:56 -07002718void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002719 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002720 int alpha;
2721 SkXfermode::Mode mode;
2722 getAlphaAndMode(paint, &alpha, &mode);
2723
Romain Guyd21b6e12011-11-30 20:21:23 -08002724 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002725
Romain Guy211370f2012-02-01 16:10:55 -08002726 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002727 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2728 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2729
Romain Guyd21b6e12011-11-30 20:21:23 -08002730 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002731 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2732 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2733 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2734 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002735 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002736 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2737 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2738 GL_TRIANGLE_STRIP, gMeshCount);
2739 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002740}
2741
Romain Guybd6b79b2010-06-26 00:13:53 -07002742void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002743 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2744 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002745 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002746}
2747
2748void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002749 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002750 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002751 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002752
Romain Guy746b7402010-10-26 16:27:31 -07002753 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002754 setupDrawWithTexture();
2755 setupDrawColor(alpha, alpha, alpha, alpha);
2756 setupDrawColorFilter();
2757 setupDrawBlending(blend, mode, swapSrcDst);
2758 setupDrawProgram();
2759 if (!dirty) {
2760 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002761 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002762 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002763 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002764 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002765 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002766 }
Romain Guy86568192010-12-14 15:55:39 -08002767 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002768 setupDrawColorFilterUniforms();
2769 setupDrawTexture(texture);
2770 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002771
Romain Guy6820ac82010-09-15 18:11:50 -07002772 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002773
2774 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002775}
2776
Romain Guya5aed0d2010-09-09 14:42:43 -07002777void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002778 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002779 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2780 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002781 // These blend modes are not supported by OpenGL directly and have
2782 // to be implemented using shaders. Since the shader will perform
2783 // the blending, turn blending off here
2784 // If the blend mode cannot be implemented using shaders, fall
2785 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002786 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2787 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002788 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002789 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002790
Romain Guy82bc7a72012-01-03 14:13:39 -08002791 if (mCaches.blend) {
2792 glDisable(GL_BLEND);
2793 mCaches.blend = false;
2794 }
2795
2796 return;
2797 } else {
2798 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002799 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002800 }
2801
2802 if (!mCaches.blend) {
2803 glEnable(GL_BLEND);
2804 }
2805
2806 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2807 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2808
2809 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2810 glBlendFunc(sourceMode, destMode);
2811 mCaches.lastSrcMode = sourceMode;
2812 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002813 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002814 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002815 glDisable(GL_BLEND);
2816 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002817 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002818}
2819
Romain Guy889f8d12010-07-29 14:37:42 -07002820bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002821 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002822 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002823 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002824 mCaches.currentProgram = program;
Romain Guy6926c72e2010-07-12 20:20:03 -07002825 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002826 }
Romain Guy6926c72e2010-07-12 20:20:03 -07002827 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002828}
2829
Romain Guy026c5e162010-06-28 17:12:22 -07002830void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002831 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002832 TextureVertex::setUV(v++, u1, v1);
2833 TextureVertex::setUV(v++, u2, v1);
2834 TextureVertex::setUV(v++, u1, v2);
2835 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002836}
2837
Chet Haase5c13d892010-10-08 08:37:55 -07002838void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002839 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002840 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002841
2842 // Skia draws using the color's alpha channel if < 255
2843 // Otherwise, it uses the paint's alpha
2844 int color = paint->getColor();
2845 *alpha = (color >> 24) & 0xFF;
2846 if (*alpha == 255) {
2847 *alpha = paint->getAlpha();
2848 }
2849 } else {
2850 *mode = SkXfermode::kSrcOver_Mode;
2851 *alpha = 255;
2852 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07002853 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002854}
2855
Romain Guya5aed0d2010-09-09 14:42:43 -07002856SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002857 SkXfermode::Mode resultMode;
2858 if (!SkXfermode::AsMode(mode, &resultMode)) {
2859 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002860 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002861 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002862}
2863
Romain Guy9d5316e2010-06-24 19:30:36 -07002864}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002865}; // namespace android