blob: ebb8eb7e161e31c0dd394c1447f2f136dd3bac70 [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 Guy026c5e12010-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 Guy026c5e12010-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 Guy026c5e12010-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 Guydda57022010-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);
Chet Haasea23eed82012-04-12 15:19:04 -0700523 layer->setBlend(true);
Romain Guydda57022010-07-06 11:39:32 -0700524
Romain Guy8fb95422010-08-17 18:38:51 -0700525 // Save the layer in the snapshot
526 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700527 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700528
Romain Guyeb993562010-10-05 18:14:38 -0700529 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700530 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700531 } else {
532 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700533 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800534 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700535 if (layer->isEmpty()) {
536 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
537 bounds.left, snapshot->height - bounds.bottom,
538 layer->getWidth(), layer->getHeight(), 0);
539 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800540 } else {
541 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
542 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
543 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700544
Romain Guy54be1cd2011-06-13 19:04:27 -0700545 // Enqueue the buffer coordinates to clear the corresponding region later
546 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700547 }
Romain Guyeb993562010-10-05 18:14:38 -0700548 }
Romain Guyf86ef572010-07-01 11:05:42 -0700549
Romain Guyd55a8612010-06-28 17:42:46 -0700550 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700551}
552
Romain Guy5b3b3522010-10-27 18:57:51 -0700553bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
554 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700555 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700556
557#if RENDER_LAYERS_AS_REGIONS
558 snapshot->region = &snapshot->layer->region;
559 snapshot->flags |= Snapshot::kFlagFboTarget;
560#endif
561
562 Rect clip(bounds);
563 snapshot->transform->mapRect(clip);
564 clip.intersect(*snapshot->clipRect);
565 clip.snapToPixelBoundaries();
566 clip.intersect(snapshot->previous->viewport);
567
568 mat4 inverse;
569 inverse.loadInverse(*mSnapshot->transform);
570
571 inverse.mapRect(clip);
572 clip.snapToPixelBoundaries();
573 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700574 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700575
576 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700577 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700578 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700579 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
580 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
581 snapshot->height = bounds.getHeight();
582 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
583 snapshot->orthoMatrix.load(mOrthoMatrix);
584
585 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700586 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
587 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700588
589 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700590 if (layer->isEmpty()) {
591 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
592 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700593 }
594
595 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700596 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700597
598#if DEBUG_LAYERS_AS_REGIONS
599 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
600 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000601 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700602
603 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700604 layer->deleteTexture();
605 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700606
607 delete layer;
608
609 return false;
610 }
611#endif
612
613 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800614 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700615 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700616 glClear(GL_COLOR_BUFFER_BIT);
617
618 dirtyClip();
619
620 // Change the ortho projection
621 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
622 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
623
624 return true;
625}
626
Romain Guy1c740bc2010-09-13 18:00:09 -0700627/**
628 * Read the documentation of createLayer() before doing anything in this method.
629 */
Romain Guy1d83e192010-08-17 11:37:00 -0700630void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
631 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000632 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700633 return;
634 }
635
Romain Guy5b3b3522010-10-27 18:57:51 -0700636 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700637
638 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700639 // Detach the texture from the FBO
640 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
641
Romain Guyeb993562010-10-05 18:14:38 -0700642 // Unbind current FBO and restore previous one
643 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
644 }
645
Romain Guy1d83e192010-08-17 11:37:00 -0700646 Layer* layer = current->layer;
647 const Rect& rect = layer->layer;
648
Romain Guy9ace8f52011-07-07 20:50:11 -0700649 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700650 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700651 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700652 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700653 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700654 }
655
Romain Guy03750a02010-10-18 14:06:08 -0700656 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700657
Romain Guya1d3c912011-12-13 14:55:06 -0800658 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700659
Romain Guy5b3b3522010-10-27 18:57:51 -0700660 // When the layer is stored in an FBO, we can save a bit of fillrate by
661 // drawing only the dirty region
662 if (fboLayer) {
663 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700664 if (layer->getColorFilter()) {
665 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800666 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700667 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700668 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800669 resetColorFilter();
670 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700671 } else if (!rect.isEmpty()) {
672 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
673 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700674 }
Romain Guy8b55f372010-08-18 17:10:07 -0700675
Romain Guyeb993562010-10-05 18:14:38 -0700676 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800677 // Note: No need to use glDiscardFramebufferEXT() since we never
678 // create/compose layers that are not on screen with this
679 // code path
680 // See LayerRenderer::destroyLayer(Layer*)
681
Romain Guyeb993562010-10-05 18:14:38 -0700682 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
683 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700684 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700685 }
686
Romain Guy746b7402010-10-26 16:27:31 -0700687 dirtyClip();
688
Romain Guyeb993562010-10-05 18:14:38 -0700689 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700690 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700691 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700692 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700693 delete layer;
694 }
695}
696
Romain Guyaa6c24c2011-04-28 18:40:04 -0700697void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700698 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700699
Romain Guy302a9df2011-08-16 13:55:02 -0700700 mat4& transform = layer->getTransform();
701 if (!transform.isIdentity()) {
702 save(0);
703 mSnapshot->transform->multiply(transform);
704 }
705
Romain Guyaa6c24c2011-04-28 18:40:04 -0700706 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700707 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700708 setupDrawWithTexture();
709 } else {
710 setupDrawWithExternalTexture();
711 }
712 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700713 setupDrawColor(alpha, alpha, alpha, alpha);
714 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700715 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700716 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700717 setupDrawPureColorUniforms();
718 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700719 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
720 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700721 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700722 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700723 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700724 if (mSnapshot->transform->isPureTranslate() &&
725 layer->getWidth() == (uint32_t) rect.getWidth() &&
726 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700727 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
728 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
729
Romain Guyd21b6e12011-11-30 20:21:23 -0800730 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700731 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
732 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800733 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700734 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
735 }
736 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700737 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
738
739 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
740
741 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700742
743 if (!transform.isIdentity()) {
744 restore();
745 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700746}
747
Romain Guy5b3b3522010-10-27 18:57:51 -0700748void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700749 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700750 const Rect& texCoords = layer->texCoords;
751 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
752 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700753
Romain Guy9ace8f52011-07-07 20:50:11 -0700754 float x = rect.left;
755 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700756 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700757 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700758 layer->getHeight() == (uint32_t) rect.getHeight();
759
760 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700761 // When we're swapping, the layer is already in screen coordinates
762 if (!swap) {
763 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
764 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
765 }
766
Romain Guyd21b6e12011-11-30 20:21:23 -0800767 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700768 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800769 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700770 }
771
772 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
773 layer->getTexture(), layer->getAlpha() / 255.0f,
774 layer->getMode(), layer->isBlend(),
775 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
776 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700777
Romain Guyaa6c24c2011-04-28 18:40:04 -0700778 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
779 } else {
780 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
781 drawTextureLayer(layer, rect);
782 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
783 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700784}
785
786void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
787#if RENDER_LAYERS_AS_REGIONS
788 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700789 layer->setRegionAsRect();
790
Romain Guy40667672011-03-18 14:34:03 -0700791 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700792
Romain Guy5b3b3522010-10-27 18:57:51 -0700793 layer->region.clear();
794 return;
795 }
796
Romain Guy8a3957d2011-09-07 17:55:15 -0700797 // TODO: See LayerRenderer.cpp::generateMesh() for important
798 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800799 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700800 size_t count;
801 const android::Rect* rects = layer->region.getArray(&count);
802
Romain Guy9ace8f52011-07-07 20:50:11 -0700803 const float alpha = layer->getAlpha() / 255.0f;
804 const float texX = 1.0f / float(layer->getWidth());
805 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800806 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700807
808 TextureVertex* mesh = mCaches.getRegionMesh();
809 GLsizei numQuads = 0;
810
Romain Guy7230a742011-01-10 22:26:16 -0800811 setupDraw();
812 setupDrawWithTexture();
813 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800814 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700815 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800816 setupDrawProgram();
817 setupDrawDirtyRegionsDisabled();
818 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800819 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700820 setupDrawTexture(layer->getTexture());
821 if (mSnapshot->transform->isPureTranslate()) {
822 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
823 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
824
Romain Guyd21b6e12011-11-30 20:21:23 -0800825 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700826 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
827 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800828 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700829 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
830 }
Romain Guy15bc6432011-12-13 13:11:32 -0800831 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700832
833 for (size_t i = 0; i < count; i++) {
834 const android::Rect* r = &rects[i];
835
836 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800837 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700838 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800839 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700840
841 // TODO: Reject quads outside of the clip
842 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
843 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
844 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
845 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
846
847 numQuads++;
848
849 if (numQuads >= REGION_MESH_QUAD_COUNT) {
850 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
851 numQuads = 0;
852 mesh = mCaches.getRegionMesh();
853 }
854 }
855
856 if (numQuads > 0) {
857 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
858 }
859
Romain Guy7230a742011-01-10 22:26:16 -0800860 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700861
862#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800863 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700864#endif
865
866 layer->region.clear();
867 }
868#else
869 composeLayerRect(layer, rect);
870#endif
871}
872
Romain Guy3a3133d2011-02-01 22:59:58 -0800873void OpenGLRenderer::drawRegionRects(const Region& region) {
874#if DEBUG_LAYERS_AS_REGIONS
875 size_t count;
876 const android::Rect* rects = region.getArray(&count);
877
878 uint32_t colors[] = {
879 0x7fff0000, 0x7f00ff00,
880 0x7f0000ff, 0x7fff00ff,
881 };
882
883 int offset = 0;
884 int32_t top = rects[0].top;
885
886 for (size_t i = 0; i < count; i++) {
887 if (top != rects[i].top) {
888 offset ^= 0x2;
889 top = rects[i].top;
890 }
891
892 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
893 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
894 SkXfermode::kSrcOver_Mode);
895 }
896#endif
897}
898
Romain Guy5b3b3522010-10-27 18:57:51 -0700899void OpenGLRenderer::dirtyLayer(const float left, const float top,
900 const float right, const float bottom, const mat4 transform) {
901#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800902 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700903 Rect bounds(left, top, right, bottom);
904 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800905 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700906 }
907#endif
908}
909
910void OpenGLRenderer::dirtyLayer(const float left, const float top,
911 const float right, const float bottom) {
912#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800913 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700914 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800915 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800916 }
917#endif
918}
919
920void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
921#if RENDER_LAYERS_AS_REGIONS
922 if (bounds.intersect(*mSnapshot->clipRect)) {
923 bounds.snapToPixelBoundaries();
924 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
925 if (!dirty.isEmpty()) {
926 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700927 }
928 }
929#endif
930}
931
Romain Guy54be1cd2011-06-13 19:04:27 -0700932void OpenGLRenderer::clearLayerRegions() {
933 const size_t count = mLayers.size();
934 if (count == 0) return;
935
936 if (!mSnapshot->isIgnored()) {
937 // Doing several glScissor/glClear here can negatively impact
938 // GPUs with a tiler architecture, instead we draw quads with
939 // the Clear blending mode
940
941 // The list contains bounds that have already been clipped
942 // against their initial clip rect, and the current clip
943 // is likely different so we need to disable clipping here
944 glDisable(GL_SCISSOR_TEST);
945
946 Vertex mesh[count * 6];
947 Vertex* vertex = mesh;
948
949 for (uint32_t i = 0; i < count; i++) {
950 Rect* bounds = mLayers.itemAt(i);
951
952 Vertex::set(vertex++, bounds->left, bounds->bottom);
953 Vertex::set(vertex++, bounds->left, bounds->top);
954 Vertex::set(vertex++, bounds->right, bounds->top);
955 Vertex::set(vertex++, bounds->left, bounds->bottom);
956 Vertex::set(vertex++, bounds->right, bounds->top);
957 Vertex::set(vertex++, bounds->right, bounds->bottom);
958
959 delete bounds;
960 }
961
962 setupDraw(false);
963 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
964 setupDrawBlending(true, SkXfermode::kClear_Mode);
965 setupDrawProgram();
966 setupDrawPureColorUniforms();
967 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800968 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700969
Romain Guy54be1cd2011-06-13 19:04:27 -0700970 glDrawArrays(GL_TRIANGLES, 0, count * 6);
971
972 glEnable(GL_SCISSOR_TEST);
973 } else {
974 for (uint32_t i = 0; i < count; i++) {
975 delete mLayers.itemAt(i);
976 }
977 }
978
979 mLayers.clear();
980}
981
Romain Guybd6b79b2010-06-26 00:13:53 -0700982///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700983// Transforms
984///////////////////////////////////////////////////////////////////////////////
985
986void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700987 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700988}
989
990void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700991 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700992}
993
994void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700995 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700996}
997
Romain Guy807daf72011-01-18 11:19:19 -0800998void OpenGLRenderer::skew(float sx, float sy) {
999 mSnapshot->transform->skew(sx, sy);
1000}
1001
Romain Guyf6a11b82010-06-23 17:47:49 -07001002void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001003 if (matrix) {
1004 mSnapshot->transform->load(*matrix);
1005 } else {
1006 mSnapshot->transform->loadIdentity();
1007 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001008}
1009
1010void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001011 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001012}
1013
1014void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001015 SkMatrix transform;
1016 mSnapshot->transform->copyTo(transform);
1017 transform.preConcat(*matrix);
1018 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001019}
1020
1021///////////////////////////////////////////////////////////////////////////////
1022// Clipping
1023///////////////////////////////////////////////////////////////////////////////
1024
Romain Guybb9524b2010-06-22 18:56:38 -07001025void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001026 Rect clip(*mSnapshot->clipRect);
1027 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001028
1029 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1030 clip.getWidth(), clip.getHeight());
1031
Romain Guy746b7402010-10-26 16:27:31 -07001032 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -07001033}
1034
1035const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001036 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001037}
1038
Romain Guyc7d53492010-06-25 13:41:57 -07001039bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001040 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001041 return true;
1042 }
1043
Romain Guy1d83e192010-08-17 11:37:00 -07001044 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001045 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001046 r.snapToPixelBoundaries();
1047
1048 Rect clipRect(*mSnapshot->clipRect);
1049 clipRect.snapToPixelBoundaries();
1050
1051 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001052}
1053
Romain Guy079ba2c2010-07-16 14:12:24 -07001054bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1055 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001056 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001057 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001058 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001059 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001060}
1061
Chet Haasea23eed82012-04-12 15:19:04 -07001062Rect* OpenGLRenderer::getClipRect() {
1063 return mSnapshot->clipRect;
1064}
1065
Romain Guyf6a11b82010-06-23 17:47:49 -07001066///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001067// Drawing commands
1068///////////////////////////////////////////////////////////////////////////////
1069
Romain Guy54be1cd2011-06-13 19:04:27 -07001070void OpenGLRenderer::setupDraw(bool clear) {
1071 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001072 if (mDirtyClip) {
1073 setScissorFromClip();
1074 }
1075 mDescription.reset();
1076 mSetShaderColor = false;
1077 mColorSet = false;
1078 mColorA = mColorR = mColorG = mColorB = 0.0f;
1079 mTextureUnit = 0;
1080 mTrackDirtyRegions = true;
1081}
1082
1083void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1084 mDescription.hasTexture = true;
1085 mDescription.hasAlpha8Texture = isAlpha8;
1086}
1087
Romain Guyaa6c24c2011-04-28 18:40:04 -07001088void OpenGLRenderer::setupDrawWithExternalTexture() {
1089 mDescription.hasExternalTexture = true;
1090}
1091
Romain Guy15bc6432011-12-13 13:11:32 -08001092void OpenGLRenderer::setupDrawNoTexture() {
1093 mCaches.disbaleTexCoordsVertexArray();
1094}
1095
Chet Haase5b0200b2011-04-13 17:58:08 -07001096void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001097 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001098}
1099
Romain Guyed6fcb02011-03-21 13:11:28 -07001100void OpenGLRenderer::setupDrawPoint(float pointSize) {
1101 mDescription.isPoint = true;
1102 mDescription.pointSize = pointSize;
1103}
1104
Romain Guy70ca14e2010-12-13 18:24:33 -08001105void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001106 setupDrawColor(color, (color >> 24) & 0xFF);
1107}
1108
1109void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1110 mColorA = alpha / 255.0f;
Chet Haasedb8c9a62012-03-21 18:54:18 -07001111 mColorA *= mSnapshot->alpha;
Chet Haase6cfdf452011-04-22 16:42:10 -07001112 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1113 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001114 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001115 mColorR = a * ((color >> 16) & 0xFF);
1116 mColorG = a * ((color >> 8) & 0xFF);
1117 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001118 mColorSet = true;
1119 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1120}
1121
Romain Guy86568192010-12-14 15:55:39 -08001122void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1123 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001124 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1125 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001126 const float a = mColorA / 255.0f;
1127 mColorR = a * ((color >> 16) & 0xFF);
1128 mColorG = a * ((color >> 8) & 0xFF);
1129 mColorB = a * ((color ) & 0xFF);
1130 mColorSet = true;
1131 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1132}
1133
Romain Guy70ca14e2010-12-13 18:24:33 -08001134void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1135 mColorA = a;
1136 mColorR = r;
1137 mColorG = g;
1138 mColorB = b;
1139 mColorSet = true;
1140 mSetShaderColor = mDescription.setColor(r, g, b, a);
1141}
1142
Romain Guy86568192010-12-14 15:55:39 -08001143void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1144 mColorA = a;
1145 mColorR = r;
1146 mColorG = g;
1147 mColorB = b;
1148 mColorSet = true;
1149 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1150}
1151
Romain Guy70ca14e2010-12-13 18:24:33 -08001152void OpenGLRenderer::setupDrawShader() {
1153 if (mShader) {
1154 mShader->describe(mDescription, mCaches.extensions);
1155 }
1156}
1157
1158void OpenGLRenderer::setupDrawColorFilter() {
1159 if (mColorFilter) {
1160 mColorFilter->describe(mDescription, mCaches.extensions);
1161 }
1162}
1163
Romain Guyf09ef512011-05-27 11:43:46 -07001164void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1165 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1166 mColorA = 1.0f;
1167 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001168 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001169 }
1170}
1171
Romain Guy70ca14e2010-12-13 18:24:33 -08001172void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001173 // When the blending mode is kClear_Mode, we need to use a modulate color
1174 // argb=1,0,0,0
1175 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001176 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1177 mDescription, swapSrcDst);
1178}
1179
1180void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001181 // When the blending mode is kClear_Mode, we need to use a modulate color
1182 // argb=1,0,0,0
1183 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001184 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1185 mDescription, swapSrcDst);
1186}
1187
1188void OpenGLRenderer::setupDrawProgram() {
1189 useProgram(mCaches.programCache.get(mDescription));
1190}
1191
1192void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1193 mTrackDirtyRegions = false;
1194}
1195
1196void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1197 bool ignoreTransform) {
1198 mModelView.loadTranslate(left, top, 0.0f);
1199 if (!ignoreTransform) {
1200 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1201 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1202 } else {
1203 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1204 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1205 }
1206}
1207
Chet Haase8a5cc922011-04-26 07:28:09 -07001208void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1209 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001210}
1211
Romain Guy70ca14e2010-12-13 18:24:33 -08001212void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1213 bool ignoreTransform, bool ignoreModelView) {
1214 if (!ignoreModelView) {
1215 mModelView.loadTranslate(left, top, 0.0f);
1216 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001217 } else {
1218 mModelView.loadIdentity();
1219 }
Romain Guy86568192010-12-14 15:55:39 -08001220 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1221 if (!ignoreTransform) {
1222 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1223 if (mTrackDirtyRegions && dirty) {
1224 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1225 }
1226 } else {
1227 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1228 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1229 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001230}
1231
Romain Guyed6fcb02011-03-21 13:11:28 -07001232void OpenGLRenderer::setupDrawPointUniforms() {
1233 int slot = mCaches.currentProgram->getUniform("pointSize");
1234 glUniform1f(slot, mDescription.pointSize);
1235}
1236
Romain Guy70ca14e2010-12-13 18:24:33 -08001237void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001238 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001239 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1240 }
1241}
1242
Romain Guy86568192010-12-14 15:55:39 -08001243void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001244 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001245 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001246 }
1247}
1248
Romain Guy70ca14e2010-12-13 18:24:33 -08001249void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1250 if (mShader) {
1251 if (ignoreTransform) {
1252 mModelView.loadInverse(*mSnapshot->transform);
1253 }
1254 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1255 }
1256}
1257
Romain Guy8d0d4782010-12-14 20:13:35 -08001258void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1259 if (mShader) {
1260 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1261 }
1262}
1263
Romain Guy70ca14e2010-12-13 18:24:33 -08001264void OpenGLRenderer::setupDrawColorFilterUniforms() {
1265 if (mColorFilter) {
1266 mColorFilter->setupProgram(mCaches.currentProgram);
1267 }
1268}
1269
1270void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001271 bool force = mCaches.bindMeshBuffer();
1272 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001273 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001274}
1275
1276void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1277 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001278 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001279 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001280}
1281
Romain Guyaa6c24c2011-04-28 18:40:04 -07001282void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1283 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001284 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001285 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001286}
1287
Romain Guy8f0095c2011-05-02 17:24:22 -07001288void OpenGLRenderer::setupDrawTextureTransform() {
1289 mDescription.hasTextureTransform = true;
1290}
1291
1292void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001293 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1294 GL_FALSE, &transform.data[0]);
1295}
1296
Romain Guy70ca14e2010-12-13 18:24:33 -08001297void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001298 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001299 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001300 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001301 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001302 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001303 }
Romain Guyd71dd362011-12-12 19:03:35 -08001304
Romain Guyf3a910b42011-12-12 20:35:21 -08001305 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001306 if (mCaches.currentProgram->texCoords >= 0) {
1307 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1308 }
1309
1310 mCaches.unbindIndicesBuffer();
1311}
1312
1313void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1314 bool force = mCaches.unbindMeshBuffer();
1315 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1316 if (mCaches.currentProgram->texCoords >= 0) {
1317 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001318 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001319}
1320
Chet Haase5b0200b2011-04-13 17:58:08 -07001321void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001322 bool force = mCaches.unbindMeshBuffer();
1323 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1324 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001325 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001326}
1327
1328/**
1329 * 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 -07001330 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1331 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1332 * attributes (one per vertex) are values from zero to one that tells the fragment
1333 * shader where the fragment is in relation to the line width/length overall; these values are
1334 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1335 * region of the line.
1336 * Note that we only pass down the width values in this setup function. The length coordinates
1337 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001338 */
Chet Haase99585ad2011-05-02 15:00:16 -07001339void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001340 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001341 bool force = mCaches.unbindMeshBuffer();
1342 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1343 vertices, gAAVertexStride);
1344 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001345 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001346
Romain Guy7b631422012-04-04 11:38:54 -07001347 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001348 glEnableVertexAttribArray(widthSlot);
1349 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001350
Romain Guy7b631422012-04-04 11:38:54 -07001351 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001352 glEnableVertexAttribArray(lengthSlot);
1353 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001354
Chet Haase5b0200b2011-04-13 17:58:08 -07001355 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001356 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001357
Chet Haase99585ad2011-05-02 15:00:16 -07001358 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001359 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Romain Guy7b631422012-04-04 11:38:54 -07001360 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion);
1361}
1362
1363void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1364 glDisableVertexAttribArray(widthSlot);
1365 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001366}
1367
Romain Guy70ca14e2010-12-13 18:24:33 -08001368void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001369}
1370
1371///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001372// Drawing
1373///////////////////////////////////////////////////////////////////////////////
1374
Romain Guy65549432012-03-26 16:45:05 -07001375status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
Romain Guy33f6beb2012-02-16 19:24:51 -08001376 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001377
1378 if (!USE_DISPLAY_LIST_PROPERTIES && quickReject(0, 0, width, height)) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001379 return false;
1380 }
1381
Romain Guy0fe478e2010-11-08 12:08:41 -08001382 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1383 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001384 if (displayList && displayList->isRenderable()) {
Chet Haasea1cff502012-02-21 13:43:44 -08001385 return displayList->replay(*this, width, height, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001386 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001387
Romain Guy65549432012-03-26 16:45:05 -07001388 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001389}
1390
Chet Haaseed30fd82011-04-22 16:18:45 -07001391void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1392 if (displayList) {
1393 displayList->output(*this, level);
1394 }
1395}
1396
Romain Guya168d732011-03-18 16:50:13 -07001397void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1398 int alpha;
1399 SkXfermode::Mode mode;
1400 getAlphaAndMode(paint, &alpha, &mode);
1401
Romain Guya168d732011-03-18 16:50:13 -07001402 float x = left;
1403 float y = top;
1404
Romain Guye3c26852011-07-25 16:36:01 -07001405 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001406 bool ignoreTransform = false;
1407 if (mSnapshot->transform->isPureTranslate()) {
1408 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1409 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1410 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001411 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001412 } else {
1413 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001414 }
1415
1416 setupDraw();
1417 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001418 if (paint) {
1419 setupDrawAlpha8Color(paint->getColor(), alpha);
1420 }
Romain Guya168d732011-03-18 16:50:13 -07001421 setupDrawColorFilter();
1422 setupDrawShader();
1423 setupDrawBlending(true, mode);
1424 setupDrawProgram();
1425 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001426
Romain Guya168d732011-03-18 16:50:13 -07001427 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001428 texture->setWrap(GL_CLAMP_TO_EDGE);
1429 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001430
Romain Guya168d732011-03-18 16:50:13 -07001431 setupDrawPureColorUniforms();
1432 setupDrawColorFilterUniforms();
1433 setupDrawShaderUniforms();
1434 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1435
1436 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1437
1438 finishDrawTexture();
1439}
1440
Chet Haase5c13d892010-10-08 08:37:55 -07001441void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001442 const float right = left + bitmap->width();
1443 const float bottom = top + bitmap->height();
1444
1445 if (quickReject(left, top, right, bottom)) {
1446 return;
1447 }
1448
Romain Guya1d3c912011-12-13 14:55:06 -08001449 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001450 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001451 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001452 const AutoTexture autoCleanup(texture);
1453
Romain Guy211370f2012-02-01 16:10:55 -08001454 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001455 drawAlphaBitmap(texture, left, top, paint);
1456 } else {
1457 drawTextureRect(left, top, right, bottom, texture, paint);
1458 }
Romain Guyce0537b2010-06-29 21:05:21 -07001459}
1460
Chet Haase5c13d892010-10-08 08:37:55 -07001461void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001462 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1463 const mat4 transform(*matrix);
1464 transform.mapRect(r);
1465
Romain Guy6926c722010-07-12 20:20:03 -07001466 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1467 return;
1468 }
1469
Romain Guya1d3c912011-12-13 14:55:06 -08001470 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001471 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001472 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001473 const AutoTexture autoCleanup(texture);
1474
Romain Guy5b3b3522010-10-27 18:57:51 -07001475 // This could be done in a cheaper way, all we need is pass the matrix
1476 // to the vertex shader. The save/restore is a bit overkill.
1477 save(SkCanvas::kMatrix_SaveFlag);
1478 concatMatrix(matrix);
1479 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1480 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001481}
1482
Romain Guy5a7b4662011-01-20 19:09:30 -08001483void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1484 float* vertices, int* colors, SkPaint* paint) {
1485 // TODO: Do a quickReject
1486 if (!vertices || mSnapshot->isIgnored()) {
1487 return;
1488 }
1489
Romain Guya1d3c912011-12-13 14:55:06 -08001490 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001491 Texture* texture = mCaches.textureCache.get(bitmap);
1492 if (!texture) return;
1493 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001494
Romain Guyd21b6e12011-11-30 20:21:23 -08001495 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1496 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001497
1498 int alpha;
1499 SkXfermode::Mode mode;
1500 getAlphaAndMode(paint, &alpha, &mode);
1501
Romain Guy5a7b4662011-01-20 19:09:30 -08001502 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001503
Romain Guyb18d2d02011-02-10 15:52:54 -08001504 float left = FLT_MAX;
1505 float top = FLT_MAX;
1506 float right = FLT_MIN;
1507 float bottom = FLT_MIN;
1508
1509#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001510 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001511#else
Romain Guy211370f2012-02-01 16:10:55 -08001512 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001513#endif
1514
Romain Guya566b7c2011-01-23 16:36:11 -08001515 // TODO: Support the colors array
1516 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001517 TextureVertex* vertex = mesh;
1518 for (int32_t y = 0; y < meshHeight; y++) {
1519 for (int32_t x = 0; x < meshWidth; x++) {
1520 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1521
1522 float u1 = float(x) / meshWidth;
1523 float u2 = float(x + 1) / meshWidth;
1524 float v1 = float(y) / meshHeight;
1525 float v2 = float(y + 1) / meshHeight;
1526
1527 int ax = i + (meshWidth + 1) * 2;
1528 int ay = ax + 1;
1529 int bx = i;
1530 int by = bx + 1;
1531 int cx = i + 2;
1532 int cy = cx + 1;
1533 int dx = i + (meshWidth + 1) * 2 + 2;
1534 int dy = dx + 1;
1535
1536 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1537 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1538 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1539
1540 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1541 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1542 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001543
1544#if RENDER_LAYERS_AS_REGIONS
1545 if (hasActiveLayer) {
1546 // TODO: This could be optimized to avoid unnecessary ops
1547 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1548 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1549 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1550 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1551 }
1552#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001553 }
1554 }
1555
Romain Guyb18d2d02011-02-10 15:52:54 -08001556#if RENDER_LAYERS_AS_REGIONS
1557 if (hasActiveLayer) {
1558 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1559 }
1560#endif
1561
Romain Guy5a7b4662011-01-20 19:09:30 -08001562 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1563 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001564 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001565}
1566
Romain Guy8ba548f2010-06-30 19:21:21 -07001567void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1568 float srcLeft, float srcTop, float srcRight, float srcBottom,
1569 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001570 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001571 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1572 return;
1573 }
1574
Romain Guya1d3c912011-12-13 14:55:06 -08001575 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001576 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001577 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001578 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001579
Romain Guy8ba548f2010-06-30 19:21:21 -07001580 const float width = texture->width;
1581 const float height = texture->height;
1582
Romain Guy68169722011-08-22 17:33:33 -07001583 const float u1 = fmax(0.0f, srcLeft / width);
1584 const float v1 = fmax(0.0f, srcTop / height);
1585 const float u2 = fmin(1.0f, srcRight / width);
1586 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001587
Romain Guy03750a02010-10-18 14:06:08 -07001588 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001589 resetDrawTextureTexCoords(u1, v1, u2, v2);
1590
Romain Guy03750a02010-10-18 14:06:08 -07001591 int alpha;
1592 SkXfermode::Mode mode;
1593 getAlphaAndMode(paint, &alpha, &mode);
1594
Romain Guyd21b6e12011-11-30 20:21:23 -08001595 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1596
Romain Guy211370f2012-02-01 16:10:55 -08001597 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001598 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1599 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1600
Romain Guyb5014982011-07-28 15:39:12 -07001601 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001602 // Enable linear filtering if the source rectangle is scaled
1603 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001604 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001605 }
Romain Guyb5014982011-07-28 15:39:12 -07001606
Romain Guyd21b6e12011-11-30 20:21:23 -08001607 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001608 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1609 texture->id, alpha / 255.0f, mode, texture->blend,
1610 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1611 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1612 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001613 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001614 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1615 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1616 GL_TRIANGLE_STRIP, gMeshCount);
1617 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001618
1619 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1620}
1621
Romain Guy4aa90572010-09-26 18:40:37 -07001622void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001623 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001624 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001625 if (quickReject(left, top, right, bottom)) {
1626 return;
1627 }
1628
Romain Guya1d3c912011-12-13 14:55:06 -08001629 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001630 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001631 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001632 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001633 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1634 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001635
1636 int alpha;
1637 SkXfermode::Mode mode;
1638 getAlphaAndMode(paint, &alpha, &mode);
1639
Romain Guy2728f962010-10-08 18:36:15 -07001640 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001641 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001642
Romain Guy211370f2012-02-01 16:10:55 -08001643 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001644 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001645#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001646 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001647 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001648 const float offsetX = left + mSnapshot->transform->getTranslateX();
1649 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001650 const size_t count = mesh->quads.size();
1651 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001652 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001653 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001654 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1655 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1656 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001657 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001658 dirtyLayer(left + bounds.left, top + bounds.top,
1659 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001660 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001661 }
1662 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001663#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001664
Romain Guy211370f2012-02-01 16:10:55 -08001665 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001666 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1667 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1668
1669 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1670 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1671 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1672 true, !mesh->hasEmptyQuads);
1673 } else {
1674 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1675 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1676 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1677 true, !mesh->hasEmptyQuads);
1678 }
Romain Guy054dc182010-10-15 17:55:25 -07001679 }
Romain Guyf7f93552010-07-08 19:17:03 -07001680}
1681
Chet Haase99ecdc42011-05-06 12:06:34 -07001682/**
Chet Haase858aa932011-05-12 09:06:00 -07001683 * This function uses a similar approach to that of AA lines in the drawLines() function.
1684 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1685 * shader to compute the translucency of the color, determined by whether a given pixel is
1686 * within that boundary region and how far into the region it is.
1687 */
1688void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001689 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001690 float inverseScaleX = 1.0f;
1691 float inverseScaleY = 1.0f;
1692 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001693 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001694 Matrix4 *mat = mSnapshot->transform;
1695 float m00 = mat->data[Matrix4::kScaleX];
1696 float m01 = mat->data[Matrix4::kSkewY];
1697 float m02 = mat->data[2];
1698 float m10 = mat->data[Matrix4::kSkewX];
1699 float m11 = mat->data[Matrix4::kScaleX];
1700 float m12 = mat->data[6];
1701 float scaleX = sqrt(m00 * m00 + m01 * m01);
1702 float scaleY = sqrt(m10 * m10 + m11 * m11);
1703 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1704 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1705 }
1706
1707 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001708 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001709 setupDrawAALine();
1710 setupDrawColor(color);
1711 setupDrawColorFilter();
1712 setupDrawShader();
1713 setupDrawBlending(true, mode);
1714 setupDrawProgram();
1715 setupDrawModelViewIdentity(true);
1716 setupDrawColorUniforms();
1717 setupDrawColorFilterUniforms();
1718 setupDrawShaderIdentityUniforms();
1719
1720 AAVertex rects[4];
1721 AAVertex* aaVertices = &rects[0];
1722 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1723 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1724
1725 float boundarySizeX = .5 * inverseScaleX;
1726 float boundarySizeY = .5 * inverseScaleY;
1727
1728 // Adjust the rect by the AA boundary padding
1729 left -= boundarySizeX;
1730 right += boundarySizeX;
1731 top -= boundarySizeY;
1732 bottom += boundarySizeY;
1733
1734 float width = right - left;
1735 float height = bottom - top;
1736
Romain Guy7b631422012-04-04 11:38:54 -07001737 int widthSlot;
1738 int lengthSlot;
1739
Chet Haase858aa932011-05-12 09:06:00 -07001740 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1741 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001742 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1743 boundaryWidthProportion, widthSlot, lengthSlot);
1744
Chet Haase858aa932011-05-12 09:06:00 -07001745 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1746 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1747 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001748 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion));
Chet Haase858aa932011-05-12 09:06:00 -07001749
1750 if (!quickReject(left, top, right, bottom)) {
1751 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1752 AAVertex::set(aaVertices++, left, top, 1, 0);
1753 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1754 AAVertex::set(aaVertices++, right, top, 0, 0);
1755 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1756 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1757 }
Romain Guy7b631422012-04-04 11:38:54 -07001758
1759 finishDrawAALine(widthSlot, lengthSlot);
Chet Haase858aa932011-05-12 09:06:00 -07001760}
1761
1762/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001763 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1764 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1765 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1766 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1767 * of the line. Hairlines are more involved because we need to account for transform scaling
1768 * to end up with a one-pixel-wide line in screen space..
1769 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1770 * in combination with values that we calculate and pass down in this method. The basic approach
1771 * is that the quad we create contains both the core line area plus a bounding area in which
1772 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1773 * proportion of the width and the length of a given segment is represented by the boundary
1774 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1775 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1776 * on the inside). This ends up giving the result we want, with pixels that are completely
1777 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1778 * how far into the boundary region they are, which is determined by shader interpolation.
1779 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001780void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1781 if (mSnapshot->isIgnored()) return;
1782
1783 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001784 // We use half the stroke width here because we're going to position the quad
1785 // corner vertices half of the width away from the line endpoints
1786 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001787 // A stroke width of 0 has a special meaning in Skia:
1788 // it draws a line 1 px wide regardless of current transform
1789 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001790
Chet Haase99ecdc42011-05-06 12:06:34 -07001791 float inverseScaleX = 1.0f;
1792 float inverseScaleY = 1.0f;
1793 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001794
Chet Haase8a5cc922011-04-26 07:28:09 -07001795 int alpha;
1796 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001797
Chet Haase8a5cc922011-04-26 07:28:09 -07001798 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001799 int verticesCount = count;
1800 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001801 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001802 verticesCount += (count - 4);
1803 }
1804
1805 if (isHairLine || isAA) {
1806 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1807 // the line on the screen should always be one pixel wide regardless of scale. For
1808 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001809 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001810 Matrix4 *mat = mSnapshot->transform;
1811 float m00 = mat->data[Matrix4::kScaleX];
1812 float m01 = mat->data[Matrix4::kSkewY];
1813 float m02 = mat->data[2];
1814 float m10 = mat->data[Matrix4::kSkewX];
1815 float m11 = mat->data[Matrix4::kScaleX];
1816 float m12 = mat->data[6];
Romain Guy7b631422012-04-04 11:38:54 -07001817
Romain Guy211370f2012-02-01 16:10:55 -08001818 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1819 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001820
Chet Haase99ecdc42011-05-06 12:06:34 -07001821 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1822 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001823
Chet Haase99ecdc42011-05-06 12:06:34 -07001824 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1825 scaled = true;
1826 }
1827 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001828 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001829
1830 getAlphaAndMode(paint, &alpha, &mode);
1831 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001832 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001833 if (isAA) {
1834 setupDrawAALine();
1835 }
1836 setupDrawColor(paint->getColor(), alpha);
1837 setupDrawColorFilter();
1838 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001839 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001840 setupDrawProgram();
1841 setupDrawModelViewIdentity(true);
1842 setupDrawColorUniforms();
1843 setupDrawColorFilterUniforms();
1844 setupDrawShaderIdentityUniforms();
1845
1846 if (isHairLine) {
1847 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001848 halfStrokeWidth = isAA? 1 : .5;
1849 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001850 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001851 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001852 }
Romain Guy7b631422012-04-04 11:38:54 -07001853
1854 int widthSlot;
1855 int lengthSlot;
1856
Chet Haase5b0200b2011-04-13 17:58:08 -07001857 Vertex lines[verticesCount];
1858 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001859
Chet Haase99585ad2011-05-02 15:00:16 -07001860 AAVertex wLines[verticesCount];
1861 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001862
Romain Guy211370f2012-02-01 16:10:55 -08001863 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001864 setupDrawVertices(vertices);
1865 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001866 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1867 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001868 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001869 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1870 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001871 // We will need to calculate the actual width proportion on each segment for
1872 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1873 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001874 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1875 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001876 }
1877
Chet Haase99ecdc42011-05-06 12:06:34 -07001878 AAVertex* prevAAVertex = NULL;
1879 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001880
Chet Haase99585ad2011-05-02 15:00:16 -07001881 int boundaryLengthSlot = -1;
1882 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001883 int boundaryWidthSlot = -1;
1884 int inverseBoundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001885
Chet Haase5b0200b2011-04-13 17:58:08 -07001886 for (int i = 0; i < count; i += 4) {
1887 // a = start point, b = end point
1888 vec2 a(points[i], points[i + 1]);
1889 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001890
Chet Haase99585ad2011-05-02 15:00:16 -07001891 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001892 float boundaryLengthProportion = 0;
1893 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001894
Chet Haase5b0200b2011-04-13 17:58:08 -07001895 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001896 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001897 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001898 if (isAA) {
1899 float wideningFactor;
1900 if (fabs(n.x) >= fabs(n.y)) {
1901 wideningFactor = fabs(1.0f / n.x);
1902 } else {
1903 wideningFactor = fabs(1.0f / n.y);
1904 }
1905 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001906 }
Romain Guy7b631422012-04-04 11:38:54 -07001907
Chet Haase99ecdc42011-05-06 12:06:34 -07001908 if (scaled) {
1909 n.x *= inverseScaleX;
1910 n.y *= inverseScaleY;
1911 }
1912 } else if (scaled) {
1913 // Extend n by .5 pixel on each side, post-transform
1914 vec2 extendedN = n.copyNormalized();
1915 extendedN /= 2;
1916 extendedN.x *= inverseScaleX;
1917 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07001918
Chet Haase99ecdc42011-05-06 12:06:34 -07001919 float extendedNLength = extendedN.length();
1920 // We need to set this value on the shader prior to drawing
1921 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1922 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001923 }
Romain Guy7b631422012-04-04 11:38:54 -07001924
Chet Haase5b0200b2011-04-13 17:58:08 -07001925 float x = n.x;
1926 n.x = -n.y;
1927 n.y = x;
1928
Chet Haase99585ad2011-05-02 15:00:16 -07001929 // aa lines expand the endpoint vertices to encompass the AA boundary
1930 if (isAA) {
1931 vec2 abVector = (b - a);
1932 length = abVector.length();
1933 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07001934
Chet Haase99ecdc42011-05-06 12:06:34 -07001935 if (scaled) {
1936 abVector.x *= inverseScaleX;
1937 abVector.y *= inverseScaleY;
1938 float abLength = abVector.length();
1939 boundaryLengthProportion = abLength / (length + abLength);
1940 } else {
1941 boundaryLengthProportion = .5 / (length + 1);
1942 }
Romain Guy7b631422012-04-04 11:38:54 -07001943
Chet Haase99ecdc42011-05-06 12:06:34 -07001944 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001945 a -= abVector;
1946 b += abVector;
1947 }
1948
Chet Haase5b0200b2011-04-13 17:58:08 -07001949 // Four corners of the rectangle defining a thick line
1950 vec2 p1 = a - n;
1951 vec2 p2 = a + n;
1952 vec2 p3 = b + n;
1953 vec2 p4 = b - n;
1954
Chet Haase99585ad2011-05-02 15:00:16 -07001955
Chet Haase5b0200b2011-04-13 17:58:08 -07001956 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1957 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1958 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1959 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1960
1961 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001962 if (!isAA) {
1963 if (prevVertex != NULL) {
1964 // Issue two repeat vertices to create degenerate triangles to bridge
1965 // between the previous line and the new one. This is necessary because
1966 // we are creating a single triangle_strip which will contain
1967 // potentially discontinuous line segments.
1968 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1969 Vertex::set(vertices++, p1.x, p1.y);
1970 generatedVerticesCount += 2;
1971 }
Romain Guy7b631422012-04-04 11:38:54 -07001972
Chet Haase5b0200b2011-04-13 17:58:08 -07001973 Vertex::set(vertices++, p1.x, p1.y);
1974 Vertex::set(vertices++, p2.x, p2.y);
1975 Vertex::set(vertices++, p4.x, p4.y);
1976 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07001977
Chet Haase5b0200b2011-04-13 17:58:08 -07001978 prevVertex = vertices - 1;
1979 generatedVerticesCount += 4;
1980 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001981 if (!isHairLine && scaled) {
1982 // Must set width proportions per-segment for scaled non-hairlines to use the
1983 // correct AA boundary dimensions
1984 if (boundaryWidthSlot < 0) {
1985 boundaryWidthSlot =
1986 mCaches.currentProgram->getUniform("boundaryWidth");
1987 inverseBoundaryWidthSlot =
1988 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1989 }
Romain Guy7b631422012-04-04 11:38:54 -07001990
Chet Haase99ecdc42011-05-06 12:06:34 -07001991 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1992 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1993 }
Romain Guy7b631422012-04-04 11:38:54 -07001994
Chet Haase99585ad2011-05-02 15:00:16 -07001995 if (boundaryLengthSlot < 0) {
1996 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1997 inverseBoundaryLengthSlot =
1998 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1999 }
Romain Guy7b631422012-04-04 11:38:54 -07002000
Chet Haase99ecdc42011-05-06 12:06:34 -07002001 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
2002 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07002003
Chet Haase5b0200b2011-04-13 17:58:08 -07002004 if (prevAAVertex != NULL) {
2005 // Issue two repeat vertices to create degenerate triangles to bridge
2006 // between the previous line and the new one. This is necessary because
2007 // we are creating a single triangle_strip which will contain
2008 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002009 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2010 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2011 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002012 generatedVerticesCount += 2;
2013 }
Romain Guy7b631422012-04-04 11:38:54 -07002014
Chet Haase99585ad2011-05-02 15:00:16 -07002015 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2016 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2017 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2018 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002019
Chet Haase5b0200b2011-04-13 17:58:08 -07002020 prevAAVertex = aaVertices - 1;
2021 generatedVerticesCount += 4;
2022 }
Romain Guy7b631422012-04-04 11:38:54 -07002023
Chet Haase99585ad2011-05-02 15:00:16 -07002024 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2025 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2026 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002027 }
2028 }
Romain Guy7b631422012-04-04 11:38:54 -07002029
Chet Haase5b0200b2011-04-13 17:58:08 -07002030 if (generatedVerticesCount > 0) {
2031 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2032 }
Romain Guy7b631422012-04-04 11:38:54 -07002033
2034 if (isAA) {
2035 finishDrawAALine(widthSlot, lengthSlot);
2036 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002037}
2038
Romain Guyed6fcb02011-03-21 13:11:28 -07002039void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2040 if (mSnapshot->isIgnored()) return;
2041
2042 // TODO: The paint's cap style defines whether the points are square or circular
2043 // TODO: Handle AA for round points
2044
Chet Haase5b0200b2011-04-13 17:58:08 -07002045 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002046 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002047 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002048 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002049 if (isHairLine) {
2050 // Now that we know it's hairline, we can set the effective width, to be used later
2051 strokeWidth = 1.0f;
2052 }
2053 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002054 int alpha;
2055 SkXfermode::Mode mode;
2056 getAlphaAndMode(paint, &alpha, &mode);
2057
2058 int verticesCount = count >> 1;
2059 int generatedVerticesCount = 0;
2060
2061 TextureVertex pointsData[verticesCount];
2062 TextureVertex* vertex = &pointsData[0];
2063
2064 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002065 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002066 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002067 setupDrawColor(paint->getColor(), alpha);
2068 setupDrawColorFilter();
2069 setupDrawShader();
2070 setupDrawBlending(mode);
2071 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002072 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002073 setupDrawColorUniforms();
2074 setupDrawColorFilterUniforms();
2075 setupDrawPointUniforms();
2076 setupDrawShaderIdentityUniforms();
2077 setupDrawMesh(vertex);
2078
2079 for (int i = 0; i < count; i += 2) {
2080 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2081 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002082
Chet Haase8a5cc922011-04-26 07:28:09 -07002083 float left = points[i] - halfWidth;
2084 float right = points[i] + halfWidth;
2085 float top = points[i + 1] - halfWidth;
2086 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002087
Chet Haase8a5cc922011-04-26 07:28:09 -07002088 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002089 }
2090
2091 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
2092}
2093
Romain Guy85bf02f2010-06-22 13:11:24 -07002094void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002095 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08002096 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002097
Romain Guyae88e5e2010-10-22 17:49:18 -07002098 Rect& clip(*mSnapshot->clipRect);
2099 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002100
Romain Guy3d58c032010-07-14 16:34:53 -07002101 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07002102}
Romain Guy9d5316e2010-06-24 19:30:36 -07002103
Romain Guyc1cd9ba2011-01-23 14:18:41 -08002104void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08002105 if (!texture) return;
2106 const AutoTexture autoCleanup(texture);
2107
2108 const float x = left + texture->left - texture->offset;
2109 const float y = top + texture->top - texture->offset;
2110
2111 drawPathTexture(texture, x, y, paint);
2112}
2113
Romain Guyc1cd9ba2011-01-23 14:18:41 -08002114void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2115 float rx, float ry, SkPaint* paint) {
2116 if (mSnapshot->isIgnored()) return;
2117
Romain Guya1d3c912011-12-13 14:55:06 -08002118 mCaches.activeTexture(0);
Romain Guyc1cd9ba2011-01-23 14:18:41 -08002119 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2120 right - left, bottom - top, rx, ry, paint);
2121 drawShape(left, top, texture, paint);
2122}
2123
Romain Guy01d58e42011-01-19 21:54:02 -08002124void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2125 if (mSnapshot->isIgnored()) return;
2126
Romain Guya1d3c912011-12-13 14:55:06 -08002127 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002128 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba2011-01-23 14:18:41 -08002129 drawShape(x - radius, y - radius, texture, paint);
2130}
Romain Guy01d58e42011-01-19 21:54:02 -08002131
Romain Guyc1cd9ba2011-01-23 14:18:41 -08002132void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2133 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002134
Romain Guya1d3c912011-12-13 14:55:06 -08002135 mCaches.activeTexture(0);
Romain Guyc1cd9ba2011-01-23 14:18:41 -08002136 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2137 drawShape(left, top, texture, paint);
2138}
2139
Romain Guy8b2f5262011-01-23 16:15:02 -08002140void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2141 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2142 if (mSnapshot->isIgnored()) return;
2143
2144 if (fabs(sweepAngle) >= 360.0f) {
2145 drawOval(left, top, right, bottom, paint);
2146 return;
2147 }
2148
Romain Guya1d3c912011-12-13 14:55:06 -08002149 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002150 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2151 startAngle, sweepAngle, useCenter, paint);
2152 drawShape(left, top, texture, paint);
2153}
2154
Romain Guyc1cd9ba2011-01-23 14:18:41 -08002155void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2156 SkPaint* paint) {
2157 if (mSnapshot->isIgnored()) return;
2158
Romain Guya1d3c912011-12-13 14:55:06 -08002159 mCaches.activeTexture(0);
Romain Guyc1cd9ba2011-01-23 14:18:41 -08002160 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2161 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002162}
2163
Chet Haase5c13d892010-10-08 08:37:55 -07002164void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba2011-01-23 14:18:41 -08002165 if (p->getStyle() != SkPaint::kFill_Style) {
2166 drawRectAsShape(left, top, right, bottom, p);
2167 return;
2168 }
2169
Romain Guy6926c722010-07-12 20:20:03 -07002170 if (quickReject(left, top, right, bottom)) {
2171 return;
2172 }
2173
Romain Guy026c5e12010-06-28 17:12:22 -07002174 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002175 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002176 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2177 if (!isMode) {
2178 // Assume SRC_OVER
2179 mode = SkXfermode::kSrcOver_Mode;
2180 }
2181 } else {
2182 mode = getXfermode(p->getXfermode());
Romain Guy026c5e12010-06-28 17:12:22 -07002183 }
2184
Romain Guy026c5e12010-06-28 17:12:22 -07002185 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002186 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002187 drawAARect(left, top, right, bottom, color, mode);
2188 } else {
2189 drawColorRect(left, top, right, bottom, color, mode);
2190 }
Romain Guyc7d53492010-06-25 13:41:57 -07002191}
Romain Guy9d5316e2010-06-24 19:30:36 -07002192
Romain Guyeb9a5362012-01-17 17:39:26 -08002193void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
2194 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002195 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2196 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guyeb9a5362012-01-17 17:39:26 -08002197 return;
2198 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002199
Romain Guy671d6cf2012-01-18 12:39:17 -08002200 // NOTE: Skia does not support perspective transform on drawPosText yet
2201 if (!mSnapshot->transform->isSimple()) {
2202 return;
2203 }
2204
2205 float x = 0.0f;
2206 float y = 0.0f;
2207 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2208 if (pureTranslate) {
2209 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2210 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2211 }
2212
2213 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2214 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2215 paint->getTextSize());
2216
2217 int alpha;
2218 SkXfermode::Mode mode;
2219 getAlphaAndMode(paint, &alpha, &mode);
2220
2221 // Pick the appropriate texture filtering
2222 bool linearFilter = mSnapshot->transform->changesBounds();
2223 if (pureTranslate && !linearFilter) {
2224 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2225 }
2226
2227 mCaches.activeTexture(0);
2228 setupDraw();
2229 setupDrawDirtyRegionsDisabled();
2230 setupDrawWithTexture(true);
2231 setupDrawAlpha8Color(paint->getColor(), alpha);
2232 setupDrawColorFilter();
2233 setupDrawShader();
2234 setupDrawBlending(true, mode);
2235 setupDrawProgram();
2236 setupDrawModelView(x, y, x, y, pureTranslate, true);
2237 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2238 setupDrawPureColorUniforms();
2239 setupDrawColorFilterUniforms();
2240 setupDrawShaderUniforms(pureTranslate);
2241
2242 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2243 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2244
2245#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002246 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002247#else
Romain Guy211370f2012-02-01 16:10:55 -08002248 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002249#endif
2250
2251 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2252 positions, hasActiveLayer ? &bounds : NULL)) {
2253#if RENDER_LAYERS_AS_REGIONS
2254 if (hasActiveLayer) {
2255 if (!pureTranslate) {
2256 mSnapshot->transform->mapRect(bounds);
2257 }
2258 dirtyLayerUnchecked(bounds, getRegion());
2259 }
2260#endif
2261 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002262}
2263
Romain Guye8e62a42010-07-23 18:55:21 -07002264void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002265 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002266 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2267 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07002268 return;
2269 }
2270
Chet Haasea1cff502012-02-21 13:43:44 -08002271 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002272 switch (paint->getTextAlign()) {
2273 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002274 x -= length / 2.0f;
2275 break;
2276 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002277 x -= length;
2278 break;
2279 default:
2280 break;
2281 }
2282
Romain Guycac5fd32011-12-01 20:08:50 -08002283 SkPaint::FontMetrics metrics;
2284 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002285 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Romain Guycac5fd32011-12-01 20:08:50 -08002286 return;
2287 }
2288
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002289 const float oldX = x;
2290 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002291 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002292 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002293 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2294 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2295 }
2296
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002297#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002298 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002299#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002300
2301 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002302 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002303 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002304
Romain Guy86568192010-12-14 15:55:39 -08002305 int alpha;
2306 SkXfermode::Mode mode;
2307 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe22010-10-15 16:06:03 -07002308
Romain Guy211370f2012-02-01 16:10:55 -08002309 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002310 mCaches.activeTexture(0);
2311
Romain Guyb45c0c92010-08-26 20:35:23 -07002312 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002313 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2314 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002315 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002316
Romain Guy740bf2b2011-04-26 15:33:10 -07002317 const float sx = oldX - shadow->left + mShadowDx;
2318 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002319
Romain Guy86568192010-12-14 15:55:39 -08002320 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002321 int shadowColor = mShadowColor;
2322 if (mShader) {
2323 shadowColor = 0xffffffff;
2324 }
Romain Guy86568192010-12-14 15:55:39 -08002325
Romain Guy86568192010-12-14 15:55:39 -08002326 setupDraw();
2327 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002328 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2329 setupDrawColorFilter();
2330 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002331 setupDrawBlending(true, mode);
2332 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002333 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002334 setupDrawTexture(shadow->id);
2335 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002336 setupDrawColorFilterUniforms();
2337 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002338 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2339
Romain Guy0a417492010-08-16 20:26:20 -07002340 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002341 }
2342
Romain Guy6620c6d2010-12-06 18:07:02 -08002343 // Pick the appropriate texture filtering
2344 bool linearFilter = mSnapshot->transform->changesBounds();
2345 if (pureTranslate && !linearFilter) {
2346 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2347 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002348
Romain Guya1d3c912011-12-13 14:55:06 -08002349 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002350 setupDraw();
2351 setupDrawDirtyRegionsDisabled();
2352 setupDrawWithTexture(true);
2353 setupDrawAlpha8Color(paint->getColor(), alpha);
2354 setupDrawColorFilter();
2355 setupDrawShader();
2356 setupDrawBlending(true, mode);
2357 setupDrawProgram();
2358 setupDrawModelView(x, y, x, y, pureTranslate, true);
2359 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2360 setupDrawPureColorUniforms();
2361 setupDrawColorFilterUniforms();
2362 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002363
Romain Guy6620c6d2010-12-06 18:07:02 -08002364 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002365 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2366
2367#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002368 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002369#else
Romain Guy211370f2012-02-01 16:10:55 -08002370 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002371#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002372
Romain Guy6620c6d2010-12-06 18:07:02 -08002373 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002374 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002375#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002376 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002377 if (!pureTranslate) {
2378 mSnapshot->transform->mapRect(bounds);
2379 }
Romain Guyf219da52011-01-16 12:54:25 -08002380 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002381 }
2382#endif
2383 }
Romain Guy694b5192010-07-21 21:33:20 -07002384
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002385 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002386}
2387
Romain Guy325740f2012-02-24 16:48:34 -08002388void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
2389 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002390 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2391 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
2392 return;
2393 }
2394
Romain Guy03d58522012-02-24 17:54:07 -08002395 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2396 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2397 paint->getTextSize());
2398
2399 int alpha;
2400 SkXfermode::Mode mode;
2401 getAlphaAndMode(paint, &alpha, &mode);
2402
2403 mCaches.activeTexture(0);
2404 setupDraw();
2405 setupDrawDirtyRegionsDisabled();
2406 setupDrawWithTexture(true);
2407 setupDrawAlpha8Color(paint->getColor(), alpha);
2408 setupDrawColorFilter();
2409 setupDrawShader();
2410 setupDrawBlending(true, mode);
2411 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002412 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002413 setupDrawTexture(fontRenderer.getTexture(true));
2414 setupDrawPureColorUniforms();
2415 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002416 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002417
Romain Guy97771732012-02-28 18:17:02 -08002418 const Rect* clip = &mSnapshot->getLocalClip();
2419 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 -08002420
Romain Guy97771732012-02-28 18:17:02 -08002421#if RENDER_LAYERS_AS_REGIONS
2422 const bool hasActiveLayer = hasLayer();
2423#else
2424 const bool hasActiveLayer = false;
2425#endif
2426
2427 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2428 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2429#if RENDER_LAYERS_AS_REGIONS
2430 if (hasActiveLayer) {
2431 mSnapshot->transform->mapRect(bounds);
2432 dirtyLayerUnchecked(bounds, getRegion());
2433 }
2434#endif
2435 }
Romain Guy325740f2012-02-24 16:48:34 -08002436}
2437
Romain Guy7fbcc042010-08-04 15:40:07 -07002438void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002439 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002440
Romain Guya1d3c912011-12-13 14:55:06 -08002441 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002442
Romain Guy33f6beb2012-02-16 19:24:51 -08002443 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002444 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07002445 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002446 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002447
Romain Guy8b55f372010-08-18 17:10:07 -07002448 const float x = texture->left - texture->offset;
2449 const float y = texture->top - texture->offset;
2450
Romain Guy01d58e42011-01-19 21:54:02 -08002451 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002452}
2453
Romain Guyada830f2011-01-13 12:13:20 -08002454void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2455 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002456 return;
2457 }
2458
Romain Guy2bf68f02012-03-02 13:37:47 -08002459 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2460 OpenGLRenderer* renderer = layer->renderer;
2461 Rect& dirty = layer->dirtyRect;
2462
2463 interrupt();
2464 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2465 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
2466 renderer->drawDisplayList(layer->displayList, layer->getWidth(), layer->getHeight(),
2467 dirty, DisplayList::kReplayFlag_ClipChildren);
2468 renderer->finish();
2469 resume();
2470
2471 dirty.setEmpty();
2472 layer->deferredUpdateScheduled = false;
2473 layer->renderer = NULL;
2474 layer->displayList = NULL;
2475 }
2476
Romain Guya1d3c912011-12-13 14:55:06 -08002477 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002478
2479 int alpha;
2480 SkXfermode::Mode mode;
2481 getAlphaAndMode(paint, &alpha, &mode);
2482
Romain Guy9ace8f52011-07-07 20:50:11 -07002483 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002484
Romain Guyf219da52011-01-16 12:54:25 -08002485#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002486 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002487 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002488 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002489 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002490 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002491 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002492
Romain Guyc88e3572011-01-22 00:32:12 -08002493 setupDraw();
2494 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002495 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002496 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002497 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002498 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002499 setupDrawPureColorUniforms();
2500 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002501 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002502 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002503 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2504 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2505
Romain Guyd21b6e12011-11-30 20:21:23 -08002506 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002507 setupDrawModelViewTranslate(x, y,
2508 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2509 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002510 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002511 setupDrawModelViewTranslate(x, y,
2512 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2513 }
Romain Guyc88e3572011-01-22 00:32:12 -08002514 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002515
Romain Guyc88e3572011-01-22 00:32:12 -08002516 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2517 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002518
Romain Guyc88e3572011-01-22 00:32:12 -08002519 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002520
2521#if DEBUG_LAYERS_AS_REGIONS
2522 drawRegionRects(layer->region);
2523#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002524 }
Romain Guyf219da52011-01-16 12:54:25 -08002525 }
2526#else
Romain Guyada830f2011-01-13 12:13:20 -08002527 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2528 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002529#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002530}
2531
Romain Guy6926c722010-07-12 20:20:03 -07002532///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002533// Shaders
2534///////////////////////////////////////////////////////////////////////////////
2535
2536void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002537 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002538}
2539
Romain Guy06f96e22010-07-30 19:18:16 -07002540void OpenGLRenderer::setupShader(SkiaShader* shader) {
2541 mShader = shader;
2542 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002543 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002544 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002545}
2546
Romain Guyd27977d2010-07-14 19:18:51 -07002547///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002548// Color filters
2549///////////////////////////////////////////////////////////////////////////////
2550
2551void OpenGLRenderer::resetColorFilter() {
2552 mColorFilter = NULL;
2553}
2554
2555void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2556 mColorFilter = filter;
2557}
2558
2559///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002560// Drop shadow
2561///////////////////////////////////////////////////////////////////////////////
2562
2563void OpenGLRenderer::resetShadow() {
2564 mHasShadow = false;
2565}
2566
2567void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2568 mHasShadow = true;
2569 mShadowRadius = radius;
2570 mShadowDx = dx;
2571 mShadowDy = dy;
2572 mShadowColor = color;
2573}
2574
2575///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002576// Draw filters
2577///////////////////////////////////////////////////////////////////////////////
2578
2579void OpenGLRenderer::resetPaintFilter() {
2580 mHasDrawFilter = false;
2581}
2582
2583void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2584 mHasDrawFilter = true;
2585 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2586 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2587}
2588
2589SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002590 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002591
2592 uint32_t flags = paint->getFlags();
2593
2594 mFilteredPaint = *paint;
2595 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2596
2597 return &mFilteredPaint;
2598}
2599
2600///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002601// Drawing implementation
2602///////////////////////////////////////////////////////////////////////////////
2603
Romain Guy01d58e42011-01-19 21:54:02 -08002604void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2605 float x, float y, SkPaint* paint) {
2606 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2607 return;
2608 }
2609
2610 int alpha;
2611 SkXfermode::Mode mode;
2612 getAlphaAndMode(paint, &alpha, &mode);
2613
2614 setupDraw();
2615 setupDrawWithTexture(true);
2616 setupDrawAlpha8Color(paint->getColor(), alpha);
2617 setupDrawColorFilter();
2618 setupDrawShader();
2619 setupDrawBlending(true, mode);
2620 setupDrawProgram();
2621 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2622 setupDrawTexture(texture->id);
2623 setupDrawPureColorUniforms();
2624 setupDrawColorFilterUniforms();
2625 setupDrawShaderUniforms();
2626 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2627
2628 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2629
2630 finishDrawTexture();
2631}
2632
Romain Guyf607bdc2010-09-10 19:20:06 -07002633// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002634#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2635#define kStdUnderline_Offset (1.0f / 9.0f)
2636#define kStdUnderline_Thickness (1.0f / 18.0f)
2637
2638void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2639 float x, float y, SkPaint* paint) {
2640 // Handle underline and strike-through
2641 uint32_t flags = paint->getFlags();
2642 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002643 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002644 float underlineWidth = length;
2645 // If length is > 0.0f, we already measured the text for the text alignment
2646 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002647 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002648 }
2649
2650 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002651 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002652 case SkPaint::kCenter_Align:
2653 offsetX = underlineWidth * 0.5f;
2654 break;
2655 case SkPaint::kRight_Align:
2656 offsetX = underlineWidth;
2657 break;
2658 default:
2659 break;
2660 }
2661
Romain Guy211370f2012-02-01 16:10:55 -08002662 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002663 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002664 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002665
Romain Guye20ecbd2010-09-22 19:49:04 -07002666 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002667 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002668
Romain Guyf6834472011-01-23 13:32:12 -08002669 int linesCount = 0;
2670 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2671 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2672
2673 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002674 float points[pointsCount];
2675 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002676
2677 if (flags & SkPaint::kUnderlineText_Flag) {
2678 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002679 points[currentPoint++] = left;
2680 points[currentPoint++] = top;
2681 points[currentPoint++] = left + underlineWidth;
2682 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002683 }
2684
2685 if (flags & SkPaint::kStrikeThruText_Flag) {
2686 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002687 points[currentPoint++] = left;
2688 points[currentPoint++] = top;
2689 points[currentPoint++] = left + underlineWidth;
2690 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002691 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002692
Romain Guy726aeba2011-06-01 14:52:00 -07002693 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002694
Romain Guy726aeba2011-06-01 14:52:00 -07002695 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002696 }
2697 }
2698}
2699
Romain Guy026c5e12010-06-28 17:12:22 -07002700void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002701 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002702 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002703 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002704 color |= 0x00ffffff;
2705 }
2706
Romain Guy70ca14e2010-12-13 18:24:33 -08002707 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002708 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002709 setupDrawColor(color);
2710 setupDrawShader();
2711 setupDrawColorFilter();
2712 setupDrawBlending(mode);
2713 setupDrawProgram();
2714 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2715 setupDrawColorUniforms();
2716 setupDrawShaderUniforms(ignoreTransform);
2717 setupDrawColorFilterUniforms();
2718 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002719
Romain Guyc95c8d62010-09-17 15:31:32 -07002720 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2721}
2722
Romain Guy82ba8142010-07-09 13:25:56 -07002723void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002724 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002725 int alpha;
2726 SkXfermode::Mode mode;
2727 getAlphaAndMode(paint, &alpha, &mode);
2728
Romain Guyd21b6e12011-11-30 20:21:23 -08002729 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002730
Romain Guy211370f2012-02-01 16:10:55 -08002731 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002732 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2733 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2734
Romain Guyd21b6e12011-11-30 20:21:23 -08002735 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002736 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2737 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2738 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2739 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002740 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002741 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2742 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2743 GL_TRIANGLE_STRIP, gMeshCount);
2744 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002745}
2746
Romain Guybd6b79b2010-06-26 00:13:53 -07002747void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002748 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2749 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002750 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002751}
2752
2753void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002754 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002755 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002756 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002757
Romain Guy746b7402010-10-26 16:27:31 -07002758 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002759 setupDrawWithTexture();
2760 setupDrawColor(alpha, alpha, alpha, alpha);
2761 setupDrawColorFilter();
2762 setupDrawBlending(blend, mode, swapSrcDst);
2763 setupDrawProgram();
2764 if (!dirty) {
2765 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002766 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002767 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002768 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002769 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002770 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002771 }
Romain Guy86568192010-12-14 15:55:39 -08002772 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002773 setupDrawColorFilterUniforms();
2774 setupDrawTexture(texture);
2775 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002776
Romain Guy6820ac82010-09-15 18:11:50 -07002777 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002778
2779 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002780}
2781
Romain Guya5aed0d2010-09-09 14:42:43 -07002782void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002783 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002784 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2785 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002786 // These blend modes are not supported by OpenGL directly and have
2787 // to be implemented using shaders. Since the shader will perform
2788 // the blending, turn blending off here
2789 // If the blend mode cannot be implemented using shaders, fall
2790 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002791 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2792 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002793 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002794 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002795
Romain Guy82bc7a72012-01-03 14:13:39 -08002796 if (mCaches.blend) {
2797 glDisable(GL_BLEND);
2798 mCaches.blend = false;
2799 }
2800
2801 return;
2802 } else {
2803 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002804 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002805 }
2806
2807 if (!mCaches.blend) {
2808 glEnable(GL_BLEND);
2809 }
2810
2811 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2812 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2813
2814 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2815 glBlendFunc(sourceMode, destMode);
2816 mCaches.lastSrcMode = sourceMode;
2817 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002818 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002819 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002820 glDisable(GL_BLEND);
2821 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002822 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002823}
2824
Romain Guy889f8d12010-07-29 14:37:42 -07002825bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002826 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002827 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002828 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002829 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002830 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002831 }
Romain Guy6926c722010-07-12 20:20:03 -07002832 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002833}
2834
Romain Guy026c5e12010-06-28 17:12:22 -07002835void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002836 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002837 TextureVertex::setUV(v++, u1, v1);
2838 TextureVertex::setUV(v++, u2, v1);
2839 TextureVertex::setUV(v++, u1, v2);
2840 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002841}
2842
Chet Haase5c13d892010-10-08 08:37:55 -07002843void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002844 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002845 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002846
2847 // Skia draws using the color's alpha channel if < 255
2848 // Otherwise, it uses the paint's alpha
2849 int color = paint->getColor();
2850 *alpha = (color >> 24) & 0xFF;
2851 if (*alpha == 255) {
2852 *alpha = paint->getAlpha();
2853 }
2854 } else {
2855 *mode = SkXfermode::kSrcOver_Mode;
2856 *alpha = 255;
2857 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07002858 *alpha *= mSnapshot->alpha;
Romain Guy026c5e12010-06-28 17:12:22 -07002859}
2860
Romain Guya5aed0d2010-09-09 14:42:43 -07002861SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002862 SkXfermode::Mode resultMode;
2863 if (!SkXfermode::AsMode(mode, &resultMode)) {
2864 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002865 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002866 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002867}
2868
Romain Guy9d5316e2010-06-24 19:30:36 -07002869}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002870}; // namespace android