blob: 2a908abba338398cfc960cad230a949544a933b7 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy03d58522012-02-24 17:54:07 -080024#include <SkPathMeasure.h>
Romain Guy694b5192010-07-21 21:33:20 -070025#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070028#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Romain Guy08aa2cb2011-03-17 11:06:57 -070030#include <private/hwui/DrawGlInfo.h>
31
Romain Guy5b3b3522010-10-27 18:57:51 -070032#include <ui/Rect.h>
33
Romain Guy85bf02f2010-06-22 13:11:24 -070034#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Romain Guya957eea2010-12-08 18:34:42 -080036#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070037
38namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070039namespace uirenderer {
40
41///////////////////////////////////////////////////////////////////////////////
42// Defines
43///////////////////////////////////////////////////////////////////////////////
44
Romain Guy759ea802010-09-16 20:49:46 -070045#define RAD_TO_DEG (180.0f / 3.14159265f)
46#define MIN_ANGLE 0.001f
47
Romain Guydbc26d22010-10-11 17:58:29 -070048// TODO: This should be set in properties
49#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
50
Romain Guyd21b6e12011-11-30 20:21:23 -080051#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
52
Romain Guy9d5316e2010-06-24 19:30:36 -070053///////////////////////////////////////////////////////////////////////////////
54// Globals
55///////////////////////////////////////////////////////////////////////////////
56
Romain Guy889f8d12010-07-29 14:37:42 -070057/**
58 * Structure mapping Skia xfermodes to OpenGL blending factors.
59 */
60struct Blender {
61 SkXfermode::Mode mode;
62 GLenum src;
63 GLenum dst;
64}; // struct Blender
65
Romain Guy026c5e162010-06-28 17:12:22 -070066// In this array, the index of each Blender equals the value of the first
67// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
68static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070069 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
70 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
71 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
72 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
73 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
74 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
75 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
76 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
80 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
82 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
83 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070084};
Romain Guye4d01122010-06-16 18:44:05 -070085
Romain Guy87a76572010-09-13 18:11:21 -070086// This array contains the swapped version of each SkXfermode. For instance
87// this array's SrcOver blending mode is actually DstOver. You can refer to
88// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070089static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070090 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
92 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
93 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
94 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
96 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
97 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
98 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
100 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
101 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
103 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
104 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700105};
106
Romain Guyf6a11b82010-06-23 17:47:49 -0700107///////////////////////////////////////////////////////////////////////////////
108// Constructors/destructor
109///////////////////////////////////////////////////////////////////////////////
110
Romain Guyfb8b7632010-08-23 21:05:08 -0700111OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700112 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700113 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700114 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800115 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700116
Romain Guyac670c02010-07-27 17:39:27 -0700117 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
118
Romain Guyae5575b2010-07-29 18:48:04 -0700119 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700120}
121
Romain Guy85bf02f2010-06-22 13:11:24 -0700122OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700123 // The context has already been destroyed at this point, do not call
124 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700125}
126
Romain Guyf6a11b82010-06-23 17:47:49 -0700127///////////////////////////////////////////////////////////////////////////////
Romain Guy13631f32012-01-30 17:41:55 -0800128// Debug
129///////////////////////////////////////////////////////////////////////////////
130
131void OpenGLRenderer::startMark(const char* name) const {
132 mCaches.startMark(0, name);
133}
134
135void OpenGLRenderer::endMark() const {
136 mCaches.endMark();
137}
138
139///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700140// Setup
141///////////////////////////////////////////////////////////////////////////////
142
Romain Guy530041d2012-01-25 18:56:29 -0800143uint32_t OpenGLRenderer::getStencilSize() {
144 return STENCIL_BUFFER_SIZE;
145}
146
Romain Guy85bf02f2010-06-22 13:11:24 -0700147void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700148 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700149
150 mWidth = width;
151 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700152
153 mFirstSnapshot->height = height;
154 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700155
Romain Guy3e263fa2011-12-12 16:47:48 -0800156 glDisable(GL_DITHER);
Romain Guy39d252a2011-12-12 18:14:06 -0800157 glEnable(GL_SCISSOR_TEST);
Romain Guy3e263fa2011-12-12 16:47:48 -0800158 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800159
Romain Guy3e263fa2011-12-12 16:47:48 -0800160 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700161}
162
Romain Guy6b7bd242010-10-06 19:49:23 -0700163void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800164 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
165}
166
167void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800168 mCaches.clearGarbage();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700169 mFunctors.clear();
Romain Guyfe48f652010-11-11 15:36:56 -0800170
Romain Guy8aef54f2010-09-01 15:13:49 -0700171 mSnapshot = new Snapshot(mFirstSnapshot,
172 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800173 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700174 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700175
Romain Guy39d252a2011-12-12 18:14:06 -0800176 glViewport(0, 0, mWidth, mHeight);
Romain Guy8f85e802011-12-14 19:23:32 -0800177 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy39d252a2011-12-12 18:14:06 -0800178
Romain Guy7d7b5492011-01-24 16:33:45 -0800179 mSnapshot->setClip(left, top, right, bottom);
Romain Guy39d252a2011-12-12 18:14:06 -0800180 mDirtyClip = false;
Romain Guy7d7b5492011-01-24 16:33:45 -0800181
Romain Guy6b7bd242010-10-06 19:49:23 -0700182 if (!opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700183 glClear(GL_COLOR_BUFFER_BIT);
184 }
Romain Guybb9524b2010-06-22 18:56:38 -0700185}
186
Romain Guyb025b9c2010-09-16 14:16:48 -0700187void OpenGLRenderer::finish() {
188#if DEBUG_OPENGL
189 GLenum status = GL_NO_ERROR;
190 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000191 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800192 switch (status) {
193 case GL_OUT_OF_MEMORY:
Steve Block3762c312012-01-06 19:20:56 +0000194 ALOGE(" OpenGLRenderer is out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800195 break;
196 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700197 }
198#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800199#if DEBUG_MEMORY_USAGE
200 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800201#else
202 if (mCaches.getDebugLevel() & kDebugMemory) {
203 mCaches.dumpMemoryUsage();
204 }
Romain Guyc15008e2010-11-10 11:59:15 -0800205#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700206}
207
Romain Guy6c319ca2011-01-11 14:29:25 -0800208void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700209 if (mCaches.currentProgram) {
210 if (mCaches.currentProgram->isInUse()) {
211 mCaches.currentProgram->remove();
212 mCaches.currentProgram = NULL;
213 }
214 }
Romain Guy50c0f092010-10-19 11:42:22 -0700215 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800216 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800217 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800218 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700219}
220
Romain Guy6c319ca2011-01-11 14:29:25 -0800221void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800222 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
223
224 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800225 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
226
Romain Guyda8532c2010-08-31 11:50:35 -0700227 glEnable(GL_SCISSOR_TEST);
Romain Guy82bc7a72012-01-03 14:13:39 -0800228 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700229 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700230
Romain Guya1d3c912011-12-13 14:55:06 -0800231 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800232 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700233
Romain Guy50c0f092010-10-19 11:42:22 -0700234 mCaches.blend = true;
235 glEnable(GL_BLEND);
236 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
237 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700238}
239
Romain Guy8f3b8e32012-03-27 16:33:45 -0700240status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
241 status_t result = DrawGlInfo::kStatusDone;
242
243 Vector<Functor*> functors(mFunctors);
244 mFunctors.clear();
245
246 DrawGlInfo info;
247 info.clipLeft = 0;
248 info.clipTop = 0;
249 info.clipRight = 0;
250 info.clipBottom = 0;
251 info.isLayer = false;
252 memset(info.transform, 0, sizeof(float) * 16);
253
254 size_t count = functors.size();
255 for (size_t i = 0; i < count; i++) {
256 Functor* f = functors.itemAt(i);
257 result |= (*f)(DrawGlInfo::kModeProcess, &info);
258
259 if (result != DrawGlInfo::kStatusDone) {
260 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
261 dirty.unionWith(localDirty);
262
Chris Craik65924a32012-04-05 17:52:11 -0700263 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guy8f3b8e32012-03-27 16:33:45 -0700264 mFunctors.push(f);
265 }
266 }
267 }
268
269 return result;
270}
271
272status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800273 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800274 if (mDirtyClip) {
275 setScissorFromClip();
276 }
Romain Guyd643bb52011-03-01 14:55:21 -0800277
Romain Guy80911b82011-03-16 15:30:12 -0700278 Rect clip(*mSnapshot->clipRect);
279 clip.snapToPixelBoundaries();
280
Romain Guyd643bb52011-03-01 14:55:21 -0800281#if RENDER_LAYERS_AS_REGIONS
282 // Since we don't know what the functor will draw, let's dirty
283 // tne entire clip region
284 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800285 dirtyLayerUnchecked(clip, getRegion());
286 }
287#endif
288
Romain Guy08aa2cb2011-03-17 11:06:57 -0700289 DrawGlInfo info;
290 info.clipLeft = clip.left;
291 info.clipTop = clip.top;
292 info.clipRight = clip.right;
293 info.clipBottom = clip.bottom;
294 info.isLayer = hasLayer();
295 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700296
Romain Guy8f3b8e32012-03-27 16:33:45 -0700297 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800298
Romain Guy8f3b8e32012-03-27 16:33:45 -0700299 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700300 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800301 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700302
Chris Craik65924a32012-04-05 17:52:11 -0700303 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guy8f3b8e32012-03-27 16:33:45 -0700304 mFunctors.push(functor);
305 }
Romain Guycabfcc12011-03-07 18:06:46 -0800306 }
307
Chet Haasedaf98e92011-01-10 14:10:36 -0800308 resume();
Romain Guy65549432012-03-26 16:45:05 -0700309 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800310}
311
Romain Guyf6a11b82010-06-23 17:47:49 -0700312///////////////////////////////////////////////////////////////////////////////
313// State management
314///////////////////////////////////////////////////////////////////////////////
315
Romain Guybb9524b2010-06-22 18:56:38 -0700316int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700317 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700318}
319
320int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700321 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700322}
323
324void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700325 if (mSaveCount > 1) {
326 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700327 }
Romain Guybb9524b2010-06-22 18:56:38 -0700328}
329
330void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700331 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700332
Romain Guy8fb95422010-08-17 18:38:51 -0700333 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700334 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700335 }
Romain Guybb9524b2010-06-22 18:56:38 -0700336}
337
Romain Guy8aef54f2010-09-01 15:13:49 -0700338int OpenGLRenderer::saveSnapshot(int flags) {
339 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700340 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700341}
342
343bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700344 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700345 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700346 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700347
Romain Guybd6b79b2010-06-26 00:13:53 -0700348 sp<Snapshot> current = mSnapshot;
349 sp<Snapshot> previous = mSnapshot->previous;
350
Romain Guyeb993562010-10-05 18:14:38 -0700351 if (restoreOrtho) {
352 Rect& r = previous->viewport;
353 glViewport(r.left, r.top, r.right, r.bottom);
354 mOrthoMatrix.load(current->orthoMatrix);
355 }
356
Romain Guy8b55f372010-08-18 17:10:07 -0700357 mSaveCount--;
358 mSnapshot = previous;
359
Romain Guy2542d192010-08-18 11:47:12 -0700360 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700361 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700362 }
Romain Guy2542d192010-08-18 11:47:12 -0700363
Romain Guy5ec99242010-11-03 16:19:08 -0700364 if (restoreLayer) {
365 composeLayer(current, previous);
366 }
367
Romain Guy2542d192010-08-18 11:47:12 -0700368 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700369}
370
Romain Guyf6a11b82010-06-23 17:47:49 -0700371///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700372// Layers
373///////////////////////////////////////////////////////////////////////////////
374
375int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700376 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700377 const GLuint previousFbo = mSnapshot->fbo;
378 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700379
Romain Guyaf636eb2010-12-09 17:47:21 -0800380 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700381 int alpha = 255;
382 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700383
Romain Guye45362c2010-11-03 19:58:32 -0700384 if (p) {
385 alpha = p->getAlpha();
386 if (!mCaches.extensions.hasFramebufferFetch()) {
387 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
388 if (!isMode) {
389 // Assume SRC_OVER
390 mode = SkXfermode::kSrcOver_Mode;
391 }
392 } else {
393 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700394 }
395 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700396 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700397 }
Romain Guyd55a8612010-06-28 17:42:46 -0700398
Romain Guydbc26d22010-10-11 17:58:29 -0700399 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
400 }
Romain Guyd55a8612010-06-28 17:42:46 -0700401
402 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700403}
404
405int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
406 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700407 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700408 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700409 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700410 SkPaint paint;
411 paint.setAlpha(alpha);
412 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700413 }
Romain Guyd55a8612010-06-28 17:42:46 -0700414}
Romain Guybd6b79b2010-06-26 00:13:53 -0700415
Romain Guy1c740bc2010-09-13 18:00:09 -0700416/**
417 * Layers are viewed by Skia are slightly different than layers in image editing
418 * programs (for instance.) When a layer is created, previously created layers
419 * and the frame buffer still receive every drawing command. For instance, if a
420 * layer is created and a shape intersecting the bounds of the layers and the
421 * framebuffer is draw, the shape will be drawn on both (unless the layer was
422 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
423 *
424 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
425 * texture. Unfortunately, this is inefficient as it requires every primitive to
426 * be drawn n + 1 times, where n is the number of active layers. In practice this
427 * means, for every primitive:
428 * - Switch active frame buffer
429 * - Change viewport, clip and projection matrix
430 * - Issue the drawing
431 *
432 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700433 * To avoid this, layers are implemented in a different way here, at least in the
434 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
435 * is set. When this flag is set we can redirect all drawing operations into a
436 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700437 *
438 * This implementation relies on the frame buffer being at least RGBA 8888. When
439 * a layer is created, only a texture is created, not an FBO. The content of the
440 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700441 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700442 * buffer and drawing continues as normal. This technique therefore treats the
443 * frame buffer as a scratch buffer for the layers.
444 *
445 * To compose the layers back onto the frame buffer, each layer texture
446 * (containing the original frame buffer data) is drawn as a simple quad over
447 * the frame buffer. The trick is that the quad is set as the composition
448 * destination in the blending equation, and the frame buffer becomes the source
449 * of the composition.
450 *
451 * Drawing layers with an alpha value requires an extra step before composition.
452 * An empty quad is drawn over the layer's region in the frame buffer. This quad
453 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
454 * quad is used to multiply the colors in the frame buffer. This is achieved by
455 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
456 * GL_ZERO, GL_SRC_ALPHA.
457 *
458 * Because glCopyTexImage2D() can be slow, an alternative implementation might
459 * be use to draw a single clipped layer. The implementation described above
460 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700461 *
462 * (1) The frame buffer is actually not cleared right away. To allow the GPU
463 * to potentially optimize series of calls to glCopyTexImage2D, the frame
464 * buffer is left untouched until the first drawing operation. Only when
465 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700466 */
Romain Guyd55a8612010-06-28 17:42:46 -0700467bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700468 float right, float bottom, int alpha, SkXfermode::Mode mode,
469 int flags, GLuint previousFbo) {
470 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700471 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700472
Romain Guyeb993562010-10-05 18:14:38 -0700473 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
474
Romain Guyf607bdc2010-09-10 19:20:06 -0700475 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700476 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700477 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700478 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700479
Romain Guyeb993562010-10-05 18:14:38 -0700480 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700481 if (bounds.intersect(*snapshot->clipRect)) {
482 // We cannot work with sub-pixels in this case
483 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700484
Romain Guyad37cd32011-03-15 11:12:25 -0700485 // When the layer is not an FBO, we may use glCopyTexImage so we
486 // need to make sure the layer does not extend outside the bounds
487 // of the framebuffer
488 if (!bounds.intersect(snapshot->previous->viewport)) {
489 bounds.setEmpty();
490 }
491 } else {
492 bounds.setEmpty();
493 }
Romain Guyeb993562010-10-05 18:14:38 -0700494 }
Romain Guybf434112010-09-16 14:40:17 -0700495
Romain Guy746b7402010-10-26 16:27:31 -0700496 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
497 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800498 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700499 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700500 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700501 }
502
503 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800504 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700505 return false;
506 }
Romain Guyf18fd992010-07-08 11:45:51 -0700507
Romain Guya1d3c912011-12-13 14:55:06 -0800508 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700509 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700510 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700511 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700512 }
513
Romain Guy9ace8f52011-07-07 20:50:11 -0700514 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700515 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700516 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
517 bounds.getWidth() / float(layer->getWidth()), 0.0f);
518 layer->setColorFilter(mColorFilter);
Romain Guydda570202010-07-06 11:39:32 -0700519
Romain Guy8fb95422010-08-17 18:38:51 -0700520 // Save the layer in the snapshot
521 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700522 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700523
Romain Guyeb993562010-10-05 18:14:38 -0700524 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700525 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700526 } else {
527 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700528 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800529 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700530 if (layer->isEmpty()) {
531 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
532 bounds.left, snapshot->height - bounds.bottom,
533 layer->getWidth(), layer->getHeight(), 0);
534 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800535 } else {
536 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
537 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
538 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700539
Romain Guy54be1cd2011-06-13 19:04:27 -0700540 // Enqueue the buffer coordinates to clear the corresponding region later
541 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700542 }
Romain Guyeb993562010-10-05 18:14:38 -0700543 }
Romain Guyf86ef572010-07-01 11:05:42 -0700544
Romain Guyd55a8612010-06-28 17:42:46 -0700545 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700546}
547
Romain Guy5b3b3522010-10-27 18:57:51 -0700548bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
549 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700550 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700551
552#if RENDER_LAYERS_AS_REGIONS
553 snapshot->region = &snapshot->layer->region;
554 snapshot->flags |= Snapshot::kFlagFboTarget;
555#endif
556
557 Rect clip(bounds);
558 snapshot->transform->mapRect(clip);
559 clip.intersect(*snapshot->clipRect);
560 clip.snapToPixelBoundaries();
561 clip.intersect(snapshot->previous->viewport);
562
563 mat4 inverse;
564 inverse.loadInverse(*mSnapshot->transform);
565
566 inverse.mapRect(clip);
567 clip.snapToPixelBoundaries();
568 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700569 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700570
571 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700572 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700573 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700574 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
575 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
576 snapshot->height = bounds.getHeight();
577 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
578 snapshot->orthoMatrix.load(mOrthoMatrix);
579
580 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700581 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
582 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700583
584 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700585 if (layer->isEmpty()) {
586 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
587 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700588 }
589
590 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700591 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700592
593#if DEBUG_LAYERS_AS_REGIONS
594 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
595 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000596 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700597
598 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700599 layer->deleteTexture();
600 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700601
602 delete layer;
603
604 return false;
605 }
606#endif
607
608 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800609 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700610 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700611 glClear(GL_COLOR_BUFFER_BIT);
612
613 dirtyClip();
614
615 // Change the ortho projection
616 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
617 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
618
619 return true;
620}
621
Romain Guy1c740bc2010-09-13 18:00:09 -0700622/**
623 * Read the documentation of createLayer() before doing anything in this method.
624 */
Romain Guy1d83e192010-08-17 11:37:00 -0700625void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
626 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000627 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700628 return;
629 }
630
Romain Guy5b3b3522010-10-27 18:57:51 -0700631 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700632
633 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700634 // Detach the texture from the FBO
635 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
636
Romain Guyeb993562010-10-05 18:14:38 -0700637 // Unbind current FBO and restore previous one
638 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
639 }
640
Romain Guy1d83e192010-08-17 11:37:00 -0700641 Layer* layer = current->layer;
642 const Rect& rect = layer->layer;
643
Romain Guy9ace8f52011-07-07 20:50:11 -0700644 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700645 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700646 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700647 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700648 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700649 }
650
Romain Guy03750a02010-10-18 14:06:08 -0700651 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700652
Romain Guya1d3c912011-12-13 14:55:06 -0800653 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700654
Romain Guy5b3b3522010-10-27 18:57:51 -0700655 // When the layer is stored in an FBO, we can save a bit of fillrate by
656 // drawing only the dirty region
657 if (fboLayer) {
658 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700659 if (layer->getColorFilter()) {
660 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800661 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700662 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700663 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800664 resetColorFilter();
665 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700666 } else if (!rect.isEmpty()) {
667 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
668 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700669 }
Romain Guy8b55f372010-08-18 17:10:07 -0700670
Romain Guyeb993562010-10-05 18:14:38 -0700671 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800672 // Note: No need to use glDiscardFramebufferEXT() since we never
673 // create/compose layers that are not on screen with this
674 // code path
675 // See LayerRenderer::destroyLayer(Layer*)
676
Romain Guyeb993562010-10-05 18:14:38 -0700677 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
678 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700679 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700680 }
681
Romain Guy746b7402010-10-26 16:27:31 -0700682 dirtyClip();
683
Romain Guyeb993562010-10-05 18:14:38 -0700684 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700685 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700686 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700687 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700688 delete layer;
689 }
690}
691
Romain Guyaa6c24c2011-04-28 18:40:04 -0700692void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700693 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700694
Romain Guy302a9df2011-08-16 13:55:02 -0700695 mat4& transform = layer->getTransform();
696 if (!transform.isIdentity()) {
697 save(0);
698 mSnapshot->transform->multiply(transform);
699 }
700
Romain Guyaa6c24c2011-04-28 18:40:04 -0700701 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700702 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700703 setupDrawWithTexture();
704 } else {
705 setupDrawWithExternalTexture();
706 }
707 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700708 setupDrawColor(alpha, alpha, alpha, alpha);
709 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700710 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700711 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700712 setupDrawPureColorUniforms();
713 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700714 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
715 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700716 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700717 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700718 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700719 if (mSnapshot->transform->isPureTranslate() &&
720 layer->getWidth() == (uint32_t) rect.getWidth() &&
721 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700722 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
723 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
724
Romain Guyd21b6e12011-11-30 20:21:23 -0800725 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700726 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
727 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800728 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700729 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
730 }
731 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700732 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
733
734 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
735
736 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700737
738 if (!transform.isIdentity()) {
739 restore();
740 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700741}
742
Romain Guy5b3b3522010-10-27 18:57:51 -0700743void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700744 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700745 const Rect& texCoords = layer->texCoords;
746 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
747 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700748
Romain Guy9ace8f52011-07-07 20:50:11 -0700749 float x = rect.left;
750 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700751 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700752 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700753 layer->getHeight() == (uint32_t) rect.getHeight();
754
755 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700756 // When we're swapping, the layer is already in screen coordinates
757 if (!swap) {
758 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
759 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
760 }
761
Romain Guyd21b6e12011-11-30 20:21:23 -0800762 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700763 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800764 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700765 }
766
767 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
768 layer->getTexture(), layer->getAlpha() / 255.0f,
769 layer->getMode(), layer->isBlend(),
770 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
771 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700772
Romain Guyaa6c24c2011-04-28 18:40:04 -0700773 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
774 } else {
775 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
776 drawTextureLayer(layer, rect);
777 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
778 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700779}
780
781void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
782#if RENDER_LAYERS_AS_REGIONS
783 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700784 layer->setRegionAsRect();
785
Romain Guy40667672011-03-18 14:34:03 -0700786 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700787
Romain Guy5b3b3522010-10-27 18:57:51 -0700788 layer->region.clear();
789 return;
790 }
791
Romain Guy8a3957d2011-09-07 17:55:15 -0700792 // TODO: See LayerRenderer.cpp::generateMesh() for important
793 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800794 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700795 size_t count;
796 const android::Rect* rects = layer->region.getArray(&count);
797
Romain Guy9ace8f52011-07-07 20:50:11 -0700798 const float alpha = layer->getAlpha() / 255.0f;
799 const float texX = 1.0f / float(layer->getWidth());
800 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800801 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700802
803 TextureVertex* mesh = mCaches.getRegionMesh();
804 GLsizei numQuads = 0;
805
Romain Guy7230a742011-01-10 22:26:16 -0800806 setupDraw();
807 setupDrawWithTexture();
808 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800809 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700810 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800811 setupDrawProgram();
812 setupDrawDirtyRegionsDisabled();
813 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800814 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700815 setupDrawTexture(layer->getTexture());
816 if (mSnapshot->transform->isPureTranslate()) {
817 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
818 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
819
Romain Guyd21b6e12011-11-30 20:21:23 -0800820 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700821 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
822 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800823 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700824 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
825 }
Romain Guy15bc6432011-12-13 13:11:32 -0800826 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700827
828 for (size_t i = 0; i < count; i++) {
829 const android::Rect* r = &rects[i];
830
831 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800832 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700833 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800834 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700835
836 // TODO: Reject quads outside of the clip
837 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
838 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
839 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
840 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
841
842 numQuads++;
843
844 if (numQuads >= REGION_MESH_QUAD_COUNT) {
845 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
846 numQuads = 0;
847 mesh = mCaches.getRegionMesh();
848 }
849 }
850
851 if (numQuads > 0) {
852 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
853 }
854
Romain Guy7230a742011-01-10 22:26:16 -0800855 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700856
857#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800858 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700859#endif
860
861 layer->region.clear();
862 }
863#else
864 composeLayerRect(layer, rect);
865#endif
866}
867
Romain Guy3a3133d2011-02-01 22:59:58 -0800868void OpenGLRenderer::drawRegionRects(const Region& region) {
869#if DEBUG_LAYERS_AS_REGIONS
870 size_t count;
871 const android::Rect* rects = region.getArray(&count);
872
873 uint32_t colors[] = {
874 0x7fff0000, 0x7f00ff00,
875 0x7f0000ff, 0x7fff00ff,
876 };
877
878 int offset = 0;
879 int32_t top = rects[0].top;
880
881 for (size_t i = 0; i < count; i++) {
882 if (top != rects[i].top) {
883 offset ^= 0x2;
884 top = rects[i].top;
885 }
886
887 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
888 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
889 SkXfermode::kSrcOver_Mode);
890 }
891#endif
892}
893
Romain Guy5b3b3522010-10-27 18:57:51 -0700894void OpenGLRenderer::dirtyLayer(const float left, const float top,
895 const float right, const float bottom, const mat4 transform) {
896#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800897 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700898 Rect bounds(left, top, right, bottom);
899 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800900 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700901 }
902#endif
903}
904
905void OpenGLRenderer::dirtyLayer(const float left, const float top,
906 const float right, const float bottom) {
907#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800908 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700909 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800910 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800911 }
912#endif
913}
914
915void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
916#if RENDER_LAYERS_AS_REGIONS
917 if (bounds.intersect(*mSnapshot->clipRect)) {
918 bounds.snapToPixelBoundaries();
919 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
920 if (!dirty.isEmpty()) {
921 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700922 }
923 }
924#endif
925}
926
Romain Guy54be1cd2011-06-13 19:04:27 -0700927void OpenGLRenderer::clearLayerRegions() {
928 const size_t count = mLayers.size();
929 if (count == 0) return;
930
931 if (!mSnapshot->isIgnored()) {
932 // Doing several glScissor/glClear here can negatively impact
933 // GPUs with a tiler architecture, instead we draw quads with
934 // the Clear blending mode
935
936 // The list contains bounds that have already been clipped
937 // against their initial clip rect, and the current clip
938 // is likely different so we need to disable clipping here
939 glDisable(GL_SCISSOR_TEST);
940
941 Vertex mesh[count * 6];
942 Vertex* vertex = mesh;
943
944 for (uint32_t i = 0; i < count; i++) {
945 Rect* bounds = mLayers.itemAt(i);
946
947 Vertex::set(vertex++, bounds->left, bounds->bottom);
948 Vertex::set(vertex++, bounds->left, bounds->top);
949 Vertex::set(vertex++, bounds->right, bounds->top);
950 Vertex::set(vertex++, bounds->left, bounds->bottom);
951 Vertex::set(vertex++, bounds->right, bounds->top);
952 Vertex::set(vertex++, bounds->right, bounds->bottom);
953
954 delete bounds;
955 }
956
957 setupDraw(false);
958 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
959 setupDrawBlending(true, SkXfermode::kClear_Mode);
960 setupDrawProgram();
961 setupDrawPureColorUniforms();
962 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800963 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700964
Romain Guy54be1cd2011-06-13 19:04:27 -0700965 glDrawArrays(GL_TRIANGLES, 0, count * 6);
966
967 glEnable(GL_SCISSOR_TEST);
968 } else {
969 for (uint32_t i = 0; i < count; i++) {
970 delete mLayers.itemAt(i);
971 }
972 }
973
974 mLayers.clear();
975}
976
Romain Guybd6b79b2010-06-26 00:13:53 -0700977///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700978// Transforms
979///////////////////////////////////////////////////////////////////////////////
980
981void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700982 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700983}
984
985void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700986 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700987}
988
989void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700990 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700991}
992
Romain Guy807daf72011-01-18 11:19:19 -0800993void OpenGLRenderer::skew(float sx, float sy) {
994 mSnapshot->transform->skew(sx, sy);
995}
996
Romain Guyf6a11b82010-06-23 17:47:49 -0700997void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -0700998 if (matrix) {
999 mSnapshot->transform->load(*matrix);
1000 } else {
1001 mSnapshot->transform->loadIdentity();
1002 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001003}
1004
1005void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001006 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001007}
1008
1009void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001010 SkMatrix transform;
1011 mSnapshot->transform->copyTo(transform);
1012 transform.preConcat(*matrix);
1013 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001014}
1015
1016///////////////////////////////////////////////////////////////////////////////
1017// Clipping
1018///////////////////////////////////////////////////////////////////////////////
1019
Romain Guybb9524b2010-06-22 18:56:38 -07001020void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001021 Rect clip(*mSnapshot->clipRect);
1022 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001023
1024 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1025 clip.getWidth(), clip.getHeight());
1026
Romain Guy746b7402010-10-26 16:27:31 -07001027 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -07001028}
1029
1030const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001031 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001032}
1033
Romain Guyc7d53492010-06-25 13:41:57 -07001034bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001035 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001036 return true;
1037 }
1038
Romain Guy1d83e192010-08-17 11:37:00 -07001039 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001040 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001041 r.snapToPixelBoundaries();
1042
1043 Rect clipRect(*mSnapshot->clipRect);
1044 clipRect.snapToPixelBoundaries();
1045
1046 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001047}
1048
Romain Guy079ba2c2010-07-16 14:12:24 -07001049bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1050 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001051 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001052 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001053 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001054 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001055}
1056
Romain Guyf6a11b82010-06-23 17:47:49 -07001057///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001058// Drawing commands
1059///////////////////////////////////////////////////////////////////////////////
1060
Romain Guy54be1cd2011-06-13 19:04:27 -07001061void OpenGLRenderer::setupDraw(bool clear) {
1062 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001063 if (mDirtyClip) {
1064 setScissorFromClip();
1065 }
1066 mDescription.reset();
1067 mSetShaderColor = false;
1068 mColorSet = false;
1069 mColorA = mColorR = mColorG = mColorB = 0.0f;
1070 mTextureUnit = 0;
1071 mTrackDirtyRegions = true;
1072}
1073
1074void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1075 mDescription.hasTexture = true;
1076 mDescription.hasAlpha8Texture = isAlpha8;
1077}
1078
Romain Guyaa6c24c2011-04-28 18:40:04 -07001079void OpenGLRenderer::setupDrawWithExternalTexture() {
1080 mDescription.hasExternalTexture = true;
1081}
1082
Romain Guy15bc6432011-12-13 13:11:32 -08001083void OpenGLRenderer::setupDrawNoTexture() {
1084 mCaches.disbaleTexCoordsVertexArray();
1085}
1086
Chet Haase5b0200b2011-04-13 17:58:08 -07001087void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001088 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001089}
1090
Romain Guyed6fcb02011-03-21 13:11:28 -07001091void OpenGLRenderer::setupDrawPoint(float pointSize) {
1092 mDescription.isPoint = true;
1093 mDescription.pointSize = pointSize;
1094}
1095
Romain Guy70ca14e2010-12-13 18:24:33 -08001096void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001097 setupDrawColor(color, (color >> 24) & 0xFF);
1098}
1099
1100void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1101 mColorA = alpha / 255.0f;
Chet Haasedb8c9a62012-03-21 18:54:18 -07001102 mColorA *= mSnapshot->alpha;
Chet Haase6cfdf452011-04-22 16:42:10 -07001103 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1104 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001105 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001106 mColorR = a * ((color >> 16) & 0xFF);
1107 mColorG = a * ((color >> 8) & 0xFF);
1108 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001109 mColorSet = true;
1110 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1111}
1112
Romain Guy86568192010-12-14 15:55:39 -08001113void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1114 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001115 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1116 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001117 const float a = mColorA / 255.0f;
1118 mColorR = a * ((color >> 16) & 0xFF);
1119 mColorG = a * ((color >> 8) & 0xFF);
1120 mColorB = a * ((color ) & 0xFF);
1121 mColorSet = true;
1122 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1123}
1124
Romain Guy70ca14e2010-12-13 18:24:33 -08001125void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1126 mColorA = a;
1127 mColorR = r;
1128 mColorG = g;
1129 mColorB = b;
1130 mColorSet = true;
1131 mSetShaderColor = mDescription.setColor(r, g, b, a);
1132}
1133
Romain Guy86568192010-12-14 15:55:39 -08001134void OpenGLRenderer::setupDrawAlpha8Color(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.setAlpha8Color(r, g, b, a);
1141}
1142
Romain Guy70ca14e2010-12-13 18:24:33 -08001143void OpenGLRenderer::setupDrawShader() {
1144 if (mShader) {
1145 mShader->describe(mDescription, mCaches.extensions);
1146 }
1147}
1148
1149void OpenGLRenderer::setupDrawColorFilter() {
1150 if (mColorFilter) {
1151 mColorFilter->describe(mDescription, mCaches.extensions);
1152 }
1153}
1154
Romain Guyf09ef512011-05-27 11:43:46 -07001155void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1156 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1157 mColorA = 1.0f;
1158 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001159 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001160 }
1161}
1162
Romain Guy70ca14e2010-12-13 18:24:33 -08001163void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001164 // When the blending mode is kClear_Mode, we need to use a modulate color
1165 // argb=1,0,0,0
1166 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001167 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1168 mDescription, swapSrcDst);
1169}
1170
1171void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001172 // When the blending mode is kClear_Mode, we need to use a modulate color
1173 // argb=1,0,0,0
1174 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001175 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1176 mDescription, swapSrcDst);
1177}
1178
1179void OpenGLRenderer::setupDrawProgram() {
1180 useProgram(mCaches.programCache.get(mDescription));
1181}
1182
1183void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1184 mTrackDirtyRegions = false;
1185}
1186
1187void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1188 bool ignoreTransform) {
1189 mModelView.loadTranslate(left, top, 0.0f);
1190 if (!ignoreTransform) {
1191 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1192 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1193 } else {
1194 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1195 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1196 }
1197}
1198
Chet Haase8a5cc922011-04-26 07:28:09 -07001199void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1200 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001201}
1202
Romain Guy70ca14e2010-12-13 18:24:33 -08001203void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1204 bool ignoreTransform, bool ignoreModelView) {
1205 if (!ignoreModelView) {
1206 mModelView.loadTranslate(left, top, 0.0f);
1207 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001208 } else {
1209 mModelView.loadIdentity();
1210 }
Romain Guy86568192010-12-14 15:55:39 -08001211 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1212 if (!ignoreTransform) {
1213 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1214 if (mTrackDirtyRegions && dirty) {
1215 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1216 }
1217 } else {
1218 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1219 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1220 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001221}
1222
Romain Guyed6fcb02011-03-21 13:11:28 -07001223void OpenGLRenderer::setupDrawPointUniforms() {
1224 int slot = mCaches.currentProgram->getUniform("pointSize");
1225 glUniform1f(slot, mDescription.pointSize);
1226}
1227
Romain Guy70ca14e2010-12-13 18:24:33 -08001228void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001229 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001230 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1231 }
1232}
1233
Romain Guy86568192010-12-14 15:55:39 -08001234void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001235 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001236 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001237 }
1238}
1239
Romain Guy70ca14e2010-12-13 18:24:33 -08001240void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1241 if (mShader) {
1242 if (ignoreTransform) {
1243 mModelView.loadInverse(*mSnapshot->transform);
1244 }
1245 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1246 }
1247}
1248
Romain Guy8d0d4782010-12-14 20:13:35 -08001249void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1250 if (mShader) {
1251 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1252 }
1253}
1254
Romain Guy70ca14e2010-12-13 18:24:33 -08001255void OpenGLRenderer::setupDrawColorFilterUniforms() {
1256 if (mColorFilter) {
1257 mColorFilter->setupProgram(mCaches.currentProgram);
1258 }
1259}
1260
1261void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001262 bool force = mCaches.bindMeshBuffer();
1263 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001264 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001265}
1266
1267void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1268 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001269 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001270 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001271}
1272
Romain Guyaa6c24c2011-04-28 18:40:04 -07001273void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1274 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001275 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001276 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001277}
1278
Romain Guy8f0095c2011-05-02 17:24:22 -07001279void OpenGLRenderer::setupDrawTextureTransform() {
1280 mDescription.hasTextureTransform = true;
1281}
1282
1283void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001284 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1285 GL_FALSE, &transform.data[0]);
1286}
1287
Romain Guy70ca14e2010-12-13 18:24:33 -08001288void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001289 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001290 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001291 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001292 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001293 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001294 }
Romain Guyd71dd362011-12-12 19:03:35 -08001295
Romain Guyf3a910b42011-12-12 20:35:21 -08001296 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001297 if (mCaches.currentProgram->texCoords >= 0) {
1298 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1299 }
1300
1301 mCaches.unbindIndicesBuffer();
1302}
1303
1304void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1305 bool force = mCaches.unbindMeshBuffer();
1306 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1307 if (mCaches.currentProgram->texCoords >= 0) {
1308 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001309 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001310}
1311
Chet Haase5b0200b2011-04-13 17:58:08 -07001312void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001313 bool force = mCaches.unbindMeshBuffer();
1314 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1315 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001316 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001317}
1318
1319/**
1320 * 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 -07001321 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1322 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1323 * attributes (one per vertex) are values from zero to one that tells the fragment
1324 * shader where the fragment is in relation to the line width/length overall; these values are
1325 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1326 * region of the line.
1327 * Note that we only pass down the width values in this setup function. The length coordinates
1328 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001329 */
Chet Haase99585ad2011-05-02 15:00:16 -07001330void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001331 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001332 bool force = mCaches.unbindMeshBuffer();
1333 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1334 vertices, gAAVertexStride);
1335 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001336 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001337
Romain Guy7b631422012-04-04 11:38:54 -07001338 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001339 glEnableVertexAttribArray(widthSlot);
1340 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001341
Romain Guy7b631422012-04-04 11:38:54 -07001342 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001343 glEnableVertexAttribArray(lengthSlot);
1344 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001345
Chet Haase5b0200b2011-04-13 17:58:08 -07001346 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001347 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001348
Chet Haase99585ad2011-05-02 15:00:16 -07001349 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001350 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Romain Guy7b631422012-04-04 11:38:54 -07001351 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion);
1352}
1353
1354void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1355 glDisableVertexAttribArray(widthSlot);
1356 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001357}
1358
Romain Guy70ca14e2010-12-13 18:24:33 -08001359void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001360}
1361
1362///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001363// Drawing
1364///////////////////////////////////////////////////////////////////////////////
1365
Romain Guy65549432012-03-26 16:45:05 -07001366status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
Romain Guy33f6beb2012-02-16 19:24:51 -08001367 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001368
1369 if (!USE_DISPLAY_LIST_PROPERTIES && quickReject(0, 0, width, height)) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001370 return false;
1371 }
1372
Romain Guy0fe478e2010-11-08 12:08:41 -08001373 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1374 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001375 if (displayList && displayList->isRenderable()) {
Chet Haasea1cff502012-02-21 13:43:44 -08001376 return displayList->replay(*this, width, height, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001377 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001378
Romain Guy65549432012-03-26 16:45:05 -07001379 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001380}
1381
Chet Haaseed30fd82011-04-22 16:18:45 -07001382void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1383 if (displayList) {
1384 displayList->output(*this, level);
1385 }
1386}
1387
Romain Guya168d732011-03-18 16:50:13 -07001388void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1389 int alpha;
1390 SkXfermode::Mode mode;
1391 getAlphaAndMode(paint, &alpha, &mode);
1392
Romain Guya168d732011-03-18 16:50:13 -07001393 float x = left;
1394 float y = top;
1395
Romain Guye3c26852011-07-25 16:36:01 -07001396 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001397 bool ignoreTransform = false;
1398 if (mSnapshot->transform->isPureTranslate()) {
1399 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1400 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1401 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001402 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001403 } else {
1404 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001405 }
1406
1407 setupDraw();
1408 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001409 if (paint) {
1410 setupDrawAlpha8Color(paint->getColor(), alpha);
1411 }
Romain Guya168d732011-03-18 16:50:13 -07001412 setupDrawColorFilter();
1413 setupDrawShader();
1414 setupDrawBlending(true, mode);
1415 setupDrawProgram();
1416 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001417
Romain Guya168d732011-03-18 16:50:13 -07001418 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001419 texture->setWrap(GL_CLAMP_TO_EDGE);
1420 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001421
Romain Guya168d732011-03-18 16:50:13 -07001422 setupDrawPureColorUniforms();
1423 setupDrawColorFilterUniforms();
1424 setupDrawShaderUniforms();
1425 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1426
1427 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1428
1429 finishDrawTexture();
1430}
1431
Chet Haase5c13d892010-10-08 08:37:55 -07001432void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001433 const float right = left + bitmap->width();
1434 const float bottom = top + bitmap->height();
1435
1436 if (quickReject(left, top, right, bottom)) {
1437 return;
1438 }
1439
Romain Guya1d3c912011-12-13 14:55:06 -08001440 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001441 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001442 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001443 const AutoTexture autoCleanup(texture);
1444
Romain Guy211370f2012-02-01 16:10:55 -08001445 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001446 drawAlphaBitmap(texture, left, top, paint);
1447 } else {
1448 drawTextureRect(left, top, right, bottom, texture, paint);
1449 }
Romain Guyce0537b2010-06-29 21:05:21 -07001450}
1451
Chet Haase5c13d892010-10-08 08:37:55 -07001452void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001453 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1454 const mat4 transform(*matrix);
1455 transform.mapRect(r);
1456
Romain Guy6926c722010-07-12 20:20:03 -07001457 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1458 return;
1459 }
1460
Romain Guya1d3c912011-12-13 14:55:06 -08001461 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001462 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001463 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001464 const AutoTexture autoCleanup(texture);
1465
Romain Guy5b3b3522010-10-27 18:57:51 -07001466 // This could be done in a cheaper way, all we need is pass the matrix
1467 // to the vertex shader. The save/restore is a bit overkill.
1468 save(SkCanvas::kMatrix_SaveFlag);
1469 concatMatrix(matrix);
1470 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1471 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001472}
1473
Romain Guy5a7b4662011-01-20 19:09:30 -08001474void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1475 float* vertices, int* colors, SkPaint* paint) {
1476 // TODO: Do a quickReject
1477 if (!vertices || mSnapshot->isIgnored()) {
1478 return;
1479 }
1480
Romain Guya1d3c912011-12-13 14:55:06 -08001481 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001482 Texture* texture = mCaches.textureCache.get(bitmap);
1483 if (!texture) return;
1484 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001485
Romain Guyd21b6e12011-11-30 20:21:23 -08001486 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1487 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001488
1489 int alpha;
1490 SkXfermode::Mode mode;
1491 getAlphaAndMode(paint, &alpha, &mode);
1492
Romain Guy5a7b4662011-01-20 19:09:30 -08001493 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001494
Romain Guyb18d2d02011-02-10 15:52:54 -08001495 float left = FLT_MAX;
1496 float top = FLT_MAX;
1497 float right = FLT_MIN;
1498 float bottom = FLT_MIN;
1499
1500#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001501 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001502#else
Romain Guy211370f2012-02-01 16:10:55 -08001503 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001504#endif
1505
Romain Guya566b7c2011-01-23 16:36:11 -08001506 // TODO: Support the colors array
1507 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001508 TextureVertex* vertex = mesh;
1509 for (int32_t y = 0; y < meshHeight; y++) {
1510 for (int32_t x = 0; x < meshWidth; x++) {
1511 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1512
1513 float u1 = float(x) / meshWidth;
1514 float u2 = float(x + 1) / meshWidth;
1515 float v1 = float(y) / meshHeight;
1516 float v2 = float(y + 1) / meshHeight;
1517
1518 int ax = i + (meshWidth + 1) * 2;
1519 int ay = ax + 1;
1520 int bx = i;
1521 int by = bx + 1;
1522 int cx = i + 2;
1523 int cy = cx + 1;
1524 int dx = i + (meshWidth + 1) * 2 + 2;
1525 int dy = dx + 1;
1526
1527 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1528 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1529 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1530
1531 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1532 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1533 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001534
1535#if RENDER_LAYERS_AS_REGIONS
1536 if (hasActiveLayer) {
1537 // TODO: This could be optimized to avoid unnecessary ops
1538 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1539 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1540 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1541 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1542 }
1543#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001544 }
1545 }
1546
Romain Guyb18d2d02011-02-10 15:52:54 -08001547#if RENDER_LAYERS_AS_REGIONS
1548 if (hasActiveLayer) {
1549 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1550 }
1551#endif
1552
Romain Guy5a7b4662011-01-20 19:09:30 -08001553 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1554 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001555 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001556}
1557
Romain Guy8ba548f2010-06-30 19:21:21 -07001558void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1559 float srcLeft, float srcTop, float srcRight, float srcBottom,
1560 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001561 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001562 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1563 return;
1564 }
1565
Romain Guya1d3c912011-12-13 14:55:06 -08001566 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001567 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001568 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001569 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001570
Romain Guy8ba548f2010-06-30 19:21:21 -07001571 const float width = texture->width;
1572 const float height = texture->height;
1573
Romain Guy68169722011-08-22 17:33:33 -07001574 const float u1 = fmax(0.0f, srcLeft / width);
1575 const float v1 = fmax(0.0f, srcTop / height);
1576 const float u2 = fmin(1.0f, srcRight / width);
1577 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001578
Romain Guy03750a02010-10-18 14:06:08 -07001579 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001580 resetDrawTextureTexCoords(u1, v1, u2, v2);
1581
Romain Guy03750a02010-10-18 14:06:08 -07001582 int alpha;
1583 SkXfermode::Mode mode;
1584 getAlphaAndMode(paint, &alpha, &mode);
1585
Romain Guyd21b6e12011-11-30 20:21:23 -08001586 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1587
Romain Guy211370f2012-02-01 16:10:55 -08001588 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001589 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1590 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1591
Romain Guyb5014982011-07-28 15:39:12 -07001592 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001593 // Enable linear filtering if the source rectangle is scaled
1594 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001595 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001596 }
Romain Guyb5014982011-07-28 15:39:12 -07001597
Romain Guyd21b6e12011-11-30 20:21:23 -08001598 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001599 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1600 texture->id, alpha / 255.0f, mode, texture->blend,
1601 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1602 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1603 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001604 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001605 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1606 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1607 GL_TRIANGLE_STRIP, gMeshCount);
1608 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001609
1610 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1611}
1612
Romain Guy4aa90572010-09-26 18:40:37 -07001613void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001614 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001615 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001616 if (quickReject(left, top, right, bottom)) {
1617 return;
1618 }
1619
Romain Guya1d3c912011-12-13 14:55:06 -08001620 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001621 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001622 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001623 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001624 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1625 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001626
1627 int alpha;
1628 SkXfermode::Mode mode;
1629 getAlphaAndMode(paint, &alpha, &mode);
1630
Romain Guy2728f962010-10-08 18:36:15 -07001631 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001632 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001633
Romain Guy211370f2012-02-01 16:10:55 -08001634 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001635 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001636#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001637 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001638 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001639 const float offsetX = left + mSnapshot->transform->getTranslateX();
1640 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001641 const size_t count = mesh->quads.size();
1642 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001643 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001644 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001645 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1646 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1647 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001648 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001649 dirtyLayer(left + bounds.left, top + bounds.top,
1650 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001651 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001652 }
1653 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001654#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001655
Romain Guy211370f2012-02-01 16:10:55 -08001656 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001657 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1658 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1659
1660 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1661 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1662 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1663 true, !mesh->hasEmptyQuads);
1664 } else {
1665 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1666 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1667 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1668 true, !mesh->hasEmptyQuads);
1669 }
Romain Guy054dc182010-10-15 17:55:25 -07001670 }
Romain Guyf7f93552010-07-08 19:17:03 -07001671}
1672
Chet Haase99ecdc42011-05-06 12:06:34 -07001673/**
Chet Haase858aa932011-05-12 09:06:00 -07001674 * This function uses a similar approach to that of AA lines in the drawLines() function.
1675 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1676 * shader to compute the translucency of the color, determined by whether a given pixel is
1677 * within that boundary region and how far into the region it is.
1678 */
1679void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001680 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001681 float inverseScaleX = 1.0f;
1682 float inverseScaleY = 1.0f;
1683 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001684 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001685 Matrix4 *mat = mSnapshot->transform;
1686 float m00 = mat->data[Matrix4::kScaleX];
1687 float m01 = mat->data[Matrix4::kSkewY];
1688 float m02 = mat->data[2];
1689 float m10 = mat->data[Matrix4::kSkewX];
1690 float m11 = mat->data[Matrix4::kScaleX];
1691 float m12 = mat->data[6];
1692 float scaleX = sqrt(m00 * m00 + m01 * m01);
1693 float scaleY = sqrt(m10 * m10 + m11 * m11);
1694 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1695 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1696 }
1697
1698 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001699 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001700 setupDrawAALine();
1701 setupDrawColor(color);
1702 setupDrawColorFilter();
1703 setupDrawShader();
1704 setupDrawBlending(true, mode);
1705 setupDrawProgram();
1706 setupDrawModelViewIdentity(true);
1707 setupDrawColorUniforms();
1708 setupDrawColorFilterUniforms();
1709 setupDrawShaderIdentityUniforms();
1710
1711 AAVertex rects[4];
1712 AAVertex* aaVertices = &rects[0];
1713 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1714 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1715
1716 float boundarySizeX = .5 * inverseScaleX;
1717 float boundarySizeY = .5 * inverseScaleY;
1718
1719 // Adjust the rect by the AA boundary padding
1720 left -= boundarySizeX;
1721 right += boundarySizeX;
1722 top -= boundarySizeY;
1723 bottom += boundarySizeY;
1724
1725 float width = right - left;
1726 float height = bottom - top;
1727
Romain Guy7b631422012-04-04 11:38:54 -07001728 int widthSlot;
1729 int lengthSlot;
1730
Chet Haase858aa932011-05-12 09:06:00 -07001731 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1732 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001733 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1734 boundaryWidthProportion, widthSlot, lengthSlot);
1735
Chet Haase858aa932011-05-12 09:06:00 -07001736 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1737 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1738 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001739 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion));
Chet Haase858aa932011-05-12 09:06:00 -07001740
1741 if (!quickReject(left, top, right, bottom)) {
1742 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1743 AAVertex::set(aaVertices++, left, top, 1, 0);
1744 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1745 AAVertex::set(aaVertices++, right, top, 0, 0);
1746 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1747 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1748 }
Romain Guy7b631422012-04-04 11:38:54 -07001749
1750 finishDrawAALine(widthSlot, lengthSlot);
Chet Haase858aa932011-05-12 09:06:00 -07001751}
1752
1753/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001754 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1755 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1756 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1757 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1758 * of the line. Hairlines are more involved because we need to account for transform scaling
1759 * to end up with a one-pixel-wide line in screen space..
1760 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1761 * in combination with values that we calculate and pass down in this method. The basic approach
1762 * is that the quad we create contains both the core line area plus a bounding area in which
1763 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1764 * proportion of the width and the length of a given segment is represented by the boundary
1765 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1766 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1767 * on the inside). This ends up giving the result we want, with pixels that are completely
1768 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1769 * how far into the boundary region they are, which is determined by shader interpolation.
1770 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001771void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1772 if (mSnapshot->isIgnored()) return;
1773
1774 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001775 // We use half the stroke width here because we're going to position the quad
1776 // corner vertices half of the width away from the line endpoints
1777 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001778 // A stroke width of 0 has a special meaning in Skia:
1779 // it draws a line 1 px wide regardless of current transform
1780 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001781
Chet Haase99ecdc42011-05-06 12:06:34 -07001782 float inverseScaleX = 1.0f;
1783 float inverseScaleY = 1.0f;
1784 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001785
Chet Haase8a5cc922011-04-26 07:28:09 -07001786 int alpha;
1787 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001788
Chet Haase8a5cc922011-04-26 07:28:09 -07001789 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001790 int verticesCount = count;
1791 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001792 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001793 verticesCount += (count - 4);
1794 }
1795
1796 if (isHairLine || isAA) {
1797 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1798 // the line on the screen should always be one pixel wide regardless of scale. For
1799 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001800 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001801 Matrix4 *mat = mSnapshot->transform;
1802 float m00 = mat->data[Matrix4::kScaleX];
1803 float m01 = mat->data[Matrix4::kSkewY];
1804 float m02 = mat->data[2];
1805 float m10 = mat->data[Matrix4::kSkewX];
1806 float m11 = mat->data[Matrix4::kScaleX];
1807 float m12 = mat->data[6];
Romain Guy7b631422012-04-04 11:38:54 -07001808
Romain Guy211370f2012-02-01 16:10:55 -08001809 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1810 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001811
Chet Haase99ecdc42011-05-06 12:06:34 -07001812 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1813 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001814
Chet Haase99ecdc42011-05-06 12:06:34 -07001815 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1816 scaled = true;
1817 }
1818 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001819 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001820
1821 getAlphaAndMode(paint, &alpha, &mode);
1822 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001823 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001824 if (isAA) {
1825 setupDrawAALine();
1826 }
1827 setupDrawColor(paint->getColor(), alpha);
1828 setupDrawColorFilter();
1829 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001830 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001831 setupDrawProgram();
1832 setupDrawModelViewIdentity(true);
1833 setupDrawColorUniforms();
1834 setupDrawColorFilterUniforms();
1835 setupDrawShaderIdentityUniforms();
1836
1837 if (isHairLine) {
1838 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001839 halfStrokeWidth = isAA? 1 : .5;
1840 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001841 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001842 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001843 }
Romain Guy7b631422012-04-04 11:38:54 -07001844
1845 int widthSlot;
1846 int lengthSlot;
1847
Chet Haase5b0200b2011-04-13 17:58:08 -07001848 Vertex lines[verticesCount];
1849 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001850
Chet Haase99585ad2011-05-02 15:00:16 -07001851 AAVertex wLines[verticesCount];
1852 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001853
Romain Guy211370f2012-02-01 16:10:55 -08001854 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001855 setupDrawVertices(vertices);
1856 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001857 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1858 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001859 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001860 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1861 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001862 // We will need to calculate the actual width proportion on each segment for
1863 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1864 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001865 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1866 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001867 }
1868
Chet Haase99ecdc42011-05-06 12:06:34 -07001869 AAVertex* prevAAVertex = NULL;
1870 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001871
Chet Haase99585ad2011-05-02 15:00:16 -07001872 int boundaryLengthSlot = -1;
1873 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001874 int boundaryWidthSlot = -1;
1875 int inverseBoundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001876
Chet Haase5b0200b2011-04-13 17:58:08 -07001877 for (int i = 0; i < count; i += 4) {
1878 // a = start point, b = end point
1879 vec2 a(points[i], points[i + 1]);
1880 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001881
Chet Haase99585ad2011-05-02 15:00:16 -07001882 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001883 float boundaryLengthProportion = 0;
1884 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001885
Chet Haase5b0200b2011-04-13 17:58:08 -07001886 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001887 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001888 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001889 if (isAA) {
1890 float wideningFactor;
1891 if (fabs(n.x) >= fabs(n.y)) {
1892 wideningFactor = fabs(1.0f / n.x);
1893 } else {
1894 wideningFactor = fabs(1.0f / n.y);
1895 }
1896 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001897 }
Romain Guy7b631422012-04-04 11:38:54 -07001898
Chet Haase99ecdc42011-05-06 12:06:34 -07001899 if (scaled) {
1900 n.x *= inverseScaleX;
1901 n.y *= inverseScaleY;
1902 }
1903 } else if (scaled) {
1904 // Extend n by .5 pixel on each side, post-transform
1905 vec2 extendedN = n.copyNormalized();
1906 extendedN /= 2;
1907 extendedN.x *= inverseScaleX;
1908 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07001909
Chet Haase99ecdc42011-05-06 12:06:34 -07001910 float extendedNLength = extendedN.length();
1911 // We need to set this value on the shader prior to drawing
1912 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1913 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001914 }
Romain Guy7b631422012-04-04 11:38:54 -07001915
Chet Haase5b0200b2011-04-13 17:58:08 -07001916 float x = n.x;
1917 n.x = -n.y;
1918 n.y = x;
1919
Chet Haase99585ad2011-05-02 15:00:16 -07001920 // aa lines expand the endpoint vertices to encompass the AA boundary
1921 if (isAA) {
1922 vec2 abVector = (b - a);
1923 length = abVector.length();
1924 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07001925
Chet Haase99ecdc42011-05-06 12:06:34 -07001926 if (scaled) {
1927 abVector.x *= inverseScaleX;
1928 abVector.y *= inverseScaleY;
1929 float abLength = abVector.length();
1930 boundaryLengthProportion = abLength / (length + abLength);
1931 } else {
1932 boundaryLengthProportion = .5 / (length + 1);
1933 }
Romain Guy7b631422012-04-04 11:38:54 -07001934
Chet Haase99ecdc42011-05-06 12:06:34 -07001935 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001936 a -= abVector;
1937 b += abVector;
1938 }
1939
Chet Haase5b0200b2011-04-13 17:58:08 -07001940 // Four corners of the rectangle defining a thick line
1941 vec2 p1 = a - n;
1942 vec2 p2 = a + n;
1943 vec2 p3 = b + n;
1944 vec2 p4 = b - n;
1945
Chet Haase99585ad2011-05-02 15:00:16 -07001946
Chet Haase5b0200b2011-04-13 17:58:08 -07001947 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1948 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1949 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1950 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1951
1952 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001953 if (!isAA) {
1954 if (prevVertex != NULL) {
1955 // Issue two repeat vertices to create degenerate triangles to bridge
1956 // between the previous line and the new one. This is necessary because
1957 // we are creating a single triangle_strip which will contain
1958 // potentially discontinuous line segments.
1959 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1960 Vertex::set(vertices++, p1.x, p1.y);
1961 generatedVerticesCount += 2;
1962 }
Romain Guy7b631422012-04-04 11:38:54 -07001963
Chet Haase5b0200b2011-04-13 17:58:08 -07001964 Vertex::set(vertices++, p1.x, p1.y);
1965 Vertex::set(vertices++, p2.x, p2.y);
1966 Vertex::set(vertices++, p4.x, p4.y);
1967 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07001968
Chet Haase5b0200b2011-04-13 17:58:08 -07001969 prevVertex = vertices - 1;
1970 generatedVerticesCount += 4;
1971 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001972 if (!isHairLine && scaled) {
1973 // Must set width proportions per-segment for scaled non-hairlines to use the
1974 // correct AA boundary dimensions
1975 if (boundaryWidthSlot < 0) {
1976 boundaryWidthSlot =
1977 mCaches.currentProgram->getUniform("boundaryWidth");
1978 inverseBoundaryWidthSlot =
1979 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1980 }
Romain Guy7b631422012-04-04 11:38:54 -07001981
Chet Haase99ecdc42011-05-06 12:06:34 -07001982 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1983 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1984 }
Romain Guy7b631422012-04-04 11:38:54 -07001985
Chet Haase99585ad2011-05-02 15:00:16 -07001986 if (boundaryLengthSlot < 0) {
1987 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1988 inverseBoundaryLengthSlot =
1989 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1990 }
Romain Guy7b631422012-04-04 11:38:54 -07001991
Chet Haase99ecdc42011-05-06 12:06:34 -07001992 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1993 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001994
Chet Haase5b0200b2011-04-13 17:58:08 -07001995 if (prevAAVertex != NULL) {
1996 // Issue two repeat vertices to create degenerate triangles to bridge
1997 // between the previous line and the new one. This is necessary because
1998 // we are creating a single triangle_strip which will contain
1999 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002000 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2001 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2002 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002003 generatedVerticesCount += 2;
2004 }
Romain Guy7b631422012-04-04 11:38:54 -07002005
Chet Haase99585ad2011-05-02 15:00:16 -07002006 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2007 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2008 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2009 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002010
Chet Haase5b0200b2011-04-13 17:58:08 -07002011 prevAAVertex = aaVertices - 1;
2012 generatedVerticesCount += 4;
2013 }
Romain Guy7b631422012-04-04 11:38:54 -07002014
Chet Haase99585ad2011-05-02 15:00:16 -07002015 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2016 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2017 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002018 }
2019 }
Romain Guy7b631422012-04-04 11:38:54 -07002020
Chet Haase5b0200b2011-04-13 17:58:08 -07002021 if (generatedVerticesCount > 0) {
2022 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2023 }
Romain Guy7b631422012-04-04 11:38:54 -07002024
2025 if (isAA) {
2026 finishDrawAALine(widthSlot, lengthSlot);
2027 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002028}
2029
Romain Guyed6fcb02011-03-21 13:11:28 -07002030void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2031 if (mSnapshot->isIgnored()) return;
2032
2033 // TODO: The paint's cap style defines whether the points are square or circular
2034 // TODO: Handle AA for round points
2035
Chet Haase5b0200b2011-04-13 17:58:08 -07002036 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002037 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002038 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002039 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002040 if (isHairLine) {
2041 // Now that we know it's hairline, we can set the effective width, to be used later
2042 strokeWidth = 1.0f;
2043 }
2044 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002045 int alpha;
2046 SkXfermode::Mode mode;
2047 getAlphaAndMode(paint, &alpha, &mode);
2048
2049 int verticesCount = count >> 1;
2050 int generatedVerticesCount = 0;
2051
2052 TextureVertex pointsData[verticesCount];
2053 TextureVertex* vertex = &pointsData[0];
2054
2055 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002056 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002057 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002058 setupDrawColor(paint->getColor(), alpha);
2059 setupDrawColorFilter();
2060 setupDrawShader();
2061 setupDrawBlending(mode);
2062 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002063 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002064 setupDrawColorUniforms();
2065 setupDrawColorFilterUniforms();
2066 setupDrawPointUniforms();
2067 setupDrawShaderIdentityUniforms();
2068 setupDrawMesh(vertex);
2069
2070 for (int i = 0; i < count; i += 2) {
2071 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2072 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002073
Chet Haase8a5cc922011-04-26 07:28:09 -07002074 float left = points[i] - halfWidth;
2075 float right = points[i] + halfWidth;
2076 float top = points[i + 1] - halfWidth;
2077 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002078
Chet Haase8a5cc922011-04-26 07:28:09 -07002079 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002080 }
2081
2082 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
2083}
2084
Romain Guy85bf02f2010-06-22 13:11:24 -07002085void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002086 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08002087 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002088
Romain Guyae88e5e2010-10-22 17:49:18 -07002089 Rect& clip(*mSnapshot->clipRect);
2090 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002091
Romain Guy3d58c032010-07-14 16:34:53 -07002092 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07002093}
Romain Guy9d5316e2010-06-24 19:30:36 -07002094
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002095void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08002096 if (!texture) return;
2097 const AutoTexture autoCleanup(texture);
2098
2099 const float x = left + texture->left - texture->offset;
2100 const float y = top + texture->top - texture->offset;
2101
2102 drawPathTexture(texture, x, y, paint);
2103}
2104
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002105void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2106 float rx, float ry, SkPaint* paint) {
2107 if (mSnapshot->isIgnored()) return;
2108
Romain Guya1d3c912011-12-13 14:55:06 -08002109 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002110 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2111 right - left, bottom - top, rx, ry, paint);
2112 drawShape(left, top, texture, paint);
2113}
2114
Romain Guy01d58e42011-01-19 21:54:02 -08002115void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2116 if (mSnapshot->isIgnored()) return;
2117
Romain Guya1d3c912011-12-13 14:55:06 -08002118 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002119 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002120 drawShape(x - radius, y - radius, texture, paint);
2121}
Romain Guy01d58e42011-01-19 21:54:02 -08002122
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002123void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2124 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002125
Romain Guya1d3c912011-12-13 14:55:06 -08002126 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002127 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2128 drawShape(left, top, texture, paint);
2129}
2130
Romain Guy8b2f5262011-01-23 16:15:02 -08002131void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2132 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2133 if (mSnapshot->isIgnored()) return;
2134
2135 if (fabs(sweepAngle) >= 360.0f) {
2136 drawOval(left, top, right, bottom, paint);
2137 return;
2138 }
2139
Romain Guya1d3c912011-12-13 14:55:06 -08002140 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002141 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2142 startAngle, sweepAngle, useCenter, paint);
2143 drawShape(left, top, texture, paint);
2144}
2145
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002146void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2147 SkPaint* paint) {
2148 if (mSnapshot->isIgnored()) return;
2149
Romain Guya1d3c912011-12-13 14:55:06 -08002150 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002151 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2152 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002153}
2154
Chet Haase5c13d892010-10-08 08:37:55 -07002155void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002156 if (p->getStyle() != SkPaint::kFill_Style) {
2157 drawRectAsShape(left, top, right, bottom, p);
2158 return;
2159 }
2160
Romain Guy6926c722010-07-12 20:20:03 -07002161 if (quickReject(left, top, right, bottom)) {
2162 return;
2163 }
2164
Romain Guy026c5e162010-06-28 17:12:22 -07002165 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002166 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002167 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2168 if (!isMode) {
2169 // Assume SRC_OVER
2170 mode = SkXfermode::kSrcOver_Mode;
2171 }
2172 } else {
2173 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002174 }
2175
Romain Guy026c5e162010-06-28 17:12:22 -07002176 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002177 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002178 drawAARect(left, top, right, bottom, color, mode);
2179 } else {
2180 drawColorRect(left, top, right, bottom, color, mode);
2181 }
Romain Guyc7d53492010-06-25 13:41:57 -07002182}
Romain Guy9d5316e2010-06-24 19:30:36 -07002183
Romain Guyeb9a5362012-01-17 17:39:26 -08002184void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
2185 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002186 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2187 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guyeb9a5362012-01-17 17:39:26 -08002188 return;
2189 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002190
Romain Guy671d6cf2012-01-18 12:39:17 -08002191 // NOTE: Skia does not support perspective transform on drawPosText yet
2192 if (!mSnapshot->transform->isSimple()) {
2193 return;
2194 }
2195
2196 float x = 0.0f;
2197 float y = 0.0f;
2198 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2199 if (pureTranslate) {
2200 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2201 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2202 }
2203
2204 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2205 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2206 paint->getTextSize());
2207
2208 int alpha;
2209 SkXfermode::Mode mode;
2210 getAlphaAndMode(paint, &alpha, &mode);
2211
2212 // Pick the appropriate texture filtering
2213 bool linearFilter = mSnapshot->transform->changesBounds();
2214 if (pureTranslate && !linearFilter) {
2215 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2216 }
2217
2218 mCaches.activeTexture(0);
2219 setupDraw();
2220 setupDrawDirtyRegionsDisabled();
2221 setupDrawWithTexture(true);
2222 setupDrawAlpha8Color(paint->getColor(), alpha);
2223 setupDrawColorFilter();
2224 setupDrawShader();
2225 setupDrawBlending(true, mode);
2226 setupDrawProgram();
2227 setupDrawModelView(x, y, x, y, pureTranslate, true);
2228 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2229 setupDrawPureColorUniforms();
2230 setupDrawColorFilterUniforms();
2231 setupDrawShaderUniforms(pureTranslate);
2232
2233 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2234 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2235
2236#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002237 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002238#else
Romain Guy211370f2012-02-01 16:10:55 -08002239 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002240#endif
2241
2242 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2243 positions, hasActiveLayer ? &bounds : NULL)) {
2244#if RENDER_LAYERS_AS_REGIONS
2245 if (hasActiveLayer) {
2246 if (!pureTranslate) {
2247 mSnapshot->transform->mapRect(bounds);
2248 }
2249 dirtyLayerUnchecked(bounds, getRegion());
2250 }
2251#endif
2252 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002253}
2254
Romain Guye8e62a42010-07-23 18:55:21 -07002255void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002256 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002257 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2258 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07002259 return;
2260 }
2261
Chet Haasea1cff502012-02-21 13:43:44 -08002262 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002263 switch (paint->getTextAlign()) {
2264 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002265 x -= length / 2.0f;
2266 break;
2267 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002268 x -= length;
2269 break;
2270 default:
2271 break;
2272 }
2273
Romain Guycac5fd32011-12-01 20:08:50 -08002274 SkPaint::FontMetrics metrics;
2275 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002276 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Romain Guycac5fd32011-12-01 20:08:50 -08002277 return;
2278 }
2279
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002280 const float oldX = x;
2281 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002282 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002283 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002284 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2285 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2286 }
2287
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002288#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002289 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002290#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002291
2292 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002293 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002294 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002295
Romain Guy86568192010-12-14 15:55:39 -08002296 int alpha;
2297 SkXfermode::Mode mode;
2298 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002299
Romain Guy211370f2012-02-01 16:10:55 -08002300 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002301 mCaches.activeTexture(0);
2302
Romain Guyb45c0c92010-08-26 20:35:23 -07002303 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002304 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2305 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002306 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002307
Romain Guy740bf2b2011-04-26 15:33:10 -07002308 const float sx = oldX - shadow->left + mShadowDx;
2309 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002310
Romain Guy86568192010-12-14 15:55:39 -08002311 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002312 int shadowColor = mShadowColor;
2313 if (mShader) {
2314 shadowColor = 0xffffffff;
2315 }
Romain Guy86568192010-12-14 15:55:39 -08002316
Romain Guy86568192010-12-14 15:55:39 -08002317 setupDraw();
2318 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002319 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2320 setupDrawColorFilter();
2321 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002322 setupDrawBlending(true, mode);
2323 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002324 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002325 setupDrawTexture(shadow->id);
2326 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002327 setupDrawColorFilterUniforms();
2328 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002329 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2330
Romain Guy0a417492010-08-16 20:26:20 -07002331 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002332 }
2333
Romain Guy6620c6d2010-12-06 18:07:02 -08002334 // Pick the appropriate texture filtering
2335 bool linearFilter = mSnapshot->transform->changesBounds();
2336 if (pureTranslate && !linearFilter) {
2337 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2338 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002339
Romain Guya1d3c912011-12-13 14:55:06 -08002340 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002341 setupDraw();
2342 setupDrawDirtyRegionsDisabled();
2343 setupDrawWithTexture(true);
2344 setupDrawAlpha8Color(paint->getColor(), alpha);
2345 setupDrawColorFilter();
2346 setupDrawShader();
2347 setupDrawBlending(true, mode);
2348 setupDrawProgram();
2349 setupDrawModelView(x, y, x, y, pureTranslate, true);
2350 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2351 setupDrawPureColorUniforms();
2352 setupDrawColorFilterUniforms();
2353 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002354
Romain Guy6620c6d2010-12-06 18:07:02 -08002355 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002356 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2357
2358#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002359 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002360#else
Romain Guy211370f2012-02-01 16:10:55 -08002361 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002362#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002363
Romain Guy6620c6d2010-12-06 18:07:02 -08002364 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002365 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002366#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002367 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002368 if (!pureTranslate) {
2369 mSnapshot->transform->mapRect(bounds);
2370 }
Romain Guyf219da52011-01-16 12:54:25 -08002371 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002372 }
2373#endif
2374 }
Romain Guy694b5192010-07-21 21:33:20 -07002375
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002376 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002377}
2378
Romain Guy325740f2012-02-24 16:48:34 -08002379void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
2380 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002381 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2382 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
2383 return;
2384 }
2385
Romain Guy03d58522012-02-24 17:54:07 -08002386 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2387 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2388 paint->getTextSize());
2389
2390 int alpha;
2391 SkXfermode::Mode mode;
2392 getAlphaAndMode(paint, &alpha, &mode);
2393
2394 mCaches.activeTexture(0);
2395 setupDraw();
2396 setupDrawDirtyRegionsDisabled();
2397 setupDrawWithTexture(true);
2398 setupDrawAlpha8Color(paint->getColor(), alpha);
2399 setupDrawColorFilter();
2400 setupDrawShader();
2401 setupDrawBlending(true, mode);
2402 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002403 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002404 setupDrawTexture(fontRenderer.getTexture(true));
2405 setupDrawPureColorUniforms();
2406 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002407 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002408
Romain Guy97771732012-02-28 18:17:02 -08002409 const Rect* clip = &mSnapshot->getLocalClip();
2410 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 -08002411
Romain Guy97771732012-02-28 18:17:02 -08002412#if RENDER_LAYERS_AS_REGIONS
2413 const bool hasActiveLayer = hasLayer();
2414#else
2415 const bool hasActiveLayer = false;
2416#endif
2417
2418 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2419 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2420#if RENDER_LAYERS_AS_REGIONS
2421 if (hasActiveLayer) {
2422 mSnapshot->transform->mapRect(bounds);
2423 dirtyLayerUnchecked(bounds, getRegion());
2424 }
2425#endif
2426 }
Romain Guy325740f2012-02-24 16:48:34 -08002427}
2428
Romain Guy7fbcc042010-08-04 15:40:07 -07002429void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002430 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002431
Romain Guya1d3c912011-12-13 14:55:06 -08002432 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002433
Romain Guy33f6beb2012-02-16 19:24:51 -08002434 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002435 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002436 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002437 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002438
Romain Guy8b55f372010-08-18 17:10:07 -07002439 const float x = texture->left - texture->offset;
2440 const float y = texture->top - texture->offset;
2441
Romain Guy01d58e42011-01-19 21:54:02 -08002442 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002443}
2444
Romain Guyada830f2011-01-13 12:13:20 -08002445void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2446 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002447 return;
2448 }
2449
Romain Guy2bf68f02012-03-02 13:37:47 -08002450 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2451 OpenGLRenderer* renderer = layer->renderer;
2452 Rect& dirty = layer->dirtyRect;
2453
2454 interrupt();
2455 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2456 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
2457 renderer->drawDisplayList(layer->displayList, layer->getWidth(), layer->getHeight(),
2458 dirty, DisplayList::kReplayFlag_ClipChildren);
2459 renderer->finish();
2460 resume();
2461
2462 dirty.setEmpty();
2463 layer->deferredUpdateScheduled = false;
2464 layer->renderer = NULL;
2465 layer->displayList = NULL;
2466 }
2467
Romain Guya1d3c912011-12-13 14:55:06 -08002468 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002469
2470 int alpha;
2471 SkXfermode::Mode mode;
2472 getAlphaAndMode(paint, &alpha, &mode);
2473
Romain Guy9ace8f52011-07-07 20:50:11 -07002474 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002475
Romain Guyf219da52011-01-16 12:54:25 -08002476#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002477 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002478 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002479 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002480 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002481 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002482 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002483
Romain Guyc88e3572011-01-22 00:32:12 -08002484 setupDraw();
2485 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002486 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002487 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002488 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002489 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002490 setupDrawPureColorUniforms();
2491 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002492 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002493 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002494 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2495 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2496
Romain Guyd21b6e12011-11-30 20:21:23 -08002497 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002498 setupDrawModelViewTranslate(x, y,
2499 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2500 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002501 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002502 setupDrawModelViewTranslate(x, y,
2503 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2504 }
Romain Guyc88e3572011-01-22 00:32:12 -08002505 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002506
Romain Guyc88e3572011-01-22 00:32:12 -08002507 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2508 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002509
Romain Guyc88e3572011-01-22 00:32:12 -08002510 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002511
2512#if DEBUG_LAYERS_AS_REGIONS
2513 drawRegionRects(layer->region);
2514#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002515 }
Romain Guyf219da52011-01-16 12:54:25 -08002516 }
2517#else
Romain Guyada830f2011-01-13 12:13:20 -08002518 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2519 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002520#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002521}
2522
Romain Guy6926c722010-07-12 20:20:03 -07002523///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002524// Shaders
2525///////////////////////////////////////////////////////////////////////////////
2526
2527void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002528 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002529}
2530
Romain Guy06f96e22010-07-30 19:18:16 -07002531void OpenGLRenderer::setupShader(SkiaShader* shader) {
2532 mShader = shader;
2533 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002534 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002535 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002536}
2537
Romain Guyd27977d2010-07-14 19:18:51 -07002538///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002539// Color filters
2540///////////////////////////////////////////////////////////////////////////////
2541
2542void OpenGLRenderer::resetColorFilter() {
2543 mColorFilter = NULL;
2544}
2545
2546void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2547 mColorFilter = filter;
2548}
2549
2550///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002551// Drop shadow
2552///////////////////////////////////////////////////////////////////////////////
2553
2554void OpenGLRenderer::resetShadow() {
2555 mHasShadow = false;
2556}
2557
2558void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2559 mHasShadow = true;
2560 mShadowRadius = radius;
2561 mShadowDx = dx;
2562 mShadowDy = dy;
2563 mShadowColor = color;
2564}
2565
2566///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002567// Draw filters
2568///////////////////////////////////////////////////////////////////////////////
2569
2570void OpenGLRenderer::resetPaintFilter() {
2571 mHasDrawFilter = false;
2572}
2573
2574void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2575 mHasDrawFilter = true;
2576 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2577 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2578}
2579
2580SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002581 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002582
2583 uint32_t flags = paint->getFlags();
2584
2585 mFilteredPaint = *paint;
2586 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2587
2588 return &mFilteredPaint;
2589}
2590
2591///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002592// Drawing implementation
2593///////////////////////////////////////////////////////////////////////////////
2594
Romain Guy01d58e42011-01-19 21:54:02 -08002595void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2596 float x, float y, SkPaint* paint) {
2597 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2598 return;
2599 }
2600
2601 int alpha;
2602 SkXfermode::Mode mode;
2603 getAlphaAndMode(paint, &alpha, &mode);
2604
2605 setupDraw();
2606 setupDrawWithTexture(true);
2607 setupDrawAlpha8Color(paint->getColor(), alpha);
2608 setupDrawColorFilter();
2609 setupDrawShader();
2610 setupDrawBlending(true, mode);
2611 setupDrawProgram();
2612 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2613 setupDrawTexture(texture->id);
2614 setupDrawPureColorUniforms();
2615 setupDrawColorFilterUniforms();
2616 setupDrawShaderUniforms();
2617 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2618
2619 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2620
2621 finishDrawTexture();
2622}
2623
Romain Guyf607bdc2010-09-10 19:20:06 -07002624// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002625#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2626#define kStdUnderline_Offset (1.0f / 9.0f)
2627#define kStdUnderline_Thickness (1.0f / 18.0f)
2628
2629void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2630 float x, float y, SkPaint* paint) {
2631 // Handle underline and strike-through
2632 uint32_t flags = paint->getFlags();
2633 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002634 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002635 float underlineWidth = length;
2636 // If length is > 0.0f, we already measured the text for the text alignment
2637 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002638 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002639 }
2640
2641 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002642 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002643 case SkPaint::kCenter_Align:
2644 offsetX = underlineWidth * 0.5f;
2645 break;
2646 case SkPaint::kRight_Align:
2647 offsetX = underlineWidth;
2648 break;
2649 default:
2650 break;
2651 }
2652
Romain Guy211370f2012-02-01 16:10:55 -08002653 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002654 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002655 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002656
Romain Guye20ecbd2010-09-22 19:49:04 -07002657 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002658 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002659
Romain Guyf6834472011-01-23 13:32:12 -08002660 int linesCount = 0;
2661 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2662 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2663
2664 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002665 float points[pointsCount];
2666 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002667
2668 if (flags & SkPaint::kUnderlineText_Flag) {
2669 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002670 points[currentPoint++] = left;
2671 points[currentPoint++] = top;
2672 points[currentPoint++] = left + underlineWidth;
2673 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002674 }
2675
2676 if (flags & SkPaint::kStrikeThruText_Flag) {
2677 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002678 points[currentPoint++] = left;
2679 points[currentPoint++] = top;
2680 points[currentPoint++] = left + underlineWidth;
2681 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002682 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002683
Romain Guy726aeba2011-06-01 14:52:00 -07002684 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002685
Romain Guy726aeba2011-06-01 14:52:00 -07002686 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002687 }
2688 }
2689}
2690
Romain Guy026c5e162010-06-28 17:12:22 -07002691void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002692 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002693 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002694 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002695 color |= 0x00ffffff;
2696 }
2697
Romain Guy70ca14e2010-12-13 18:24:33 -08002698 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002699 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002700 setupDrawColor(color);
2701 setupDrawShader();
2702 setupDrawColorFilter();
2703 setupDrawBlending(mode);
2704 setupDrawProgram();
2705 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2706 setupDrawColorUniforms();
2707 setupDrawShaderUniforms(ignoreTransform);
2708 setupDrawColorFilterUniforms();
2709 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002710
Romain Guyc95c8d62010-09-17 15:31:32 -07002711 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2712}
2713
Romain Guy82ba8142010-07-09 13:25:56 -07002714void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002715 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002716 int alpha;
2717 SkXfermode::Mode mode;
2718 getAlphaAndMode(paint, &alpha, &mode);
2719
Romain Guyd21b6e12011-11-30 20:21:23 -08002720 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002721
Romain Guy211370f2012-02-01 16:10:55 -08002722 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002723 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2724 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2725
Romain Guyd21b6e12011-11-30 20:21:23 -08002726 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002727 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2728 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2729 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2730 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002731 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002732 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2733 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2734 GL_TRIANGLE_STRIP, gMeshCount);
2735 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002736}
2737
Romain Guybd6b79b2010-06-26 00:13:53 -07002738void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002739 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2740 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002741 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002742}
2743
2744void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002745 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002746 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002747 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002748
Romain Guy746b7402010-10-26 16:27:31 -07002749 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002750 setupDrawWithTexture();
2751 setupDrawColor(alpha, alpha, alpha, alpha);
2752 setupDrawColorFilter();
2753 setupDrawBlending(blend, mode, swapSrcDst);
2754 setupDrawProgram();
2755 if (!dirty) {
2756 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002757 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002758 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002759 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002760 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002761 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002762 }
Romain Guy86568192010-12-14 15:55:39 -08002763 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002764 setupDrawColorFilterUniforms();
2765 setupDrawTexture(texture);
2766 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002767
Romain Guy6820ac82010-09-15 18:11:50 -07002768 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002769
2770 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002771}
2772
Romain Guya5aed0d2010-09-09 14:42:43 -07002773void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002774 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002775 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2776 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002777 // These blend modes are not supported by OpenGL directly and have
2778 // to be implemented using shaders. Since the shader will perform
2779 // the blending, turn blending off here
2780 // If the blend mode cannot be implemented using shaders, fall
2781 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002782 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2783 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002784 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002785 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002786
Romain Guy82bc7a72012-01-03 14:13:39 -08002787 if (mCaches.blend) {
2788 glDisable(GL_BLEND);
2789 mCaches.blend = false;
2790 }
2791
2792 return;
2793 } else {
2794 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002795 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002796 }
2797
2798 if (!mCaches.blend) {
2799 glEnable(GL_BLEND);
2800 }
2801
2802 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2803 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2804
2805 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2806 glBlendFunc(sourceMode, destMode);
2807 mCaches.lastSrcMode = sourceMode;
2808 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002809 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002810 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002811 glDisable(GL_BLEND);
2812 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002813 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002814}
2815
Romain Guy889f8d12010-07-29 14:37:42 -07002816bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002817 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002818 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002819 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002820 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002821 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002822 }
Romain Guy6926c722010-07-12 20:20:03 -07002823 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002824}
2825
Romain Guy026c5e162010-06-28 17:12:22 -07002826void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002827 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002828 TextureVertex::setUV(v++, u1, v1);
2829 TextureVertex::setUV(v++, u2, v1);
2830 TextureVertex::setUV(v++, u1, v2);
2831 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002832}
2833
Chet Haase5c13d892010-10-08 08:37:55 -07002834void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002835 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002836 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002837
2838 // Skia draws using the color's alpha channel if < 255
2839 // Otherwise, it uses the paint's alpha
2840 int color = paint->getColor();
2841 *alpha = (color >> 24) & 0xFF;
2842 if (*alpha == 255) {
2843 *alpha = paint->getAlpha();
2844 }
2845 } else {
2846 *mode = SkXfermode::kSrcOver_Mode;
2847 *alpha = 255;
2848 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07002849 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002850}
2851
Romain Guya5aed0d2010-09-09 14:42:43 -07002852SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002853 SkXfermode::Mode resultMode;
2854 if (!SkXfermode::AsMode(mode, &resultMode)) {
2855 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002856 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002857 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002858}
2859
Romain Guy9d5316e2010-06-24 19:30:36 -07002860}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002861}; // namespace android