blob: fa3780d3cb1ff72ced32d12eb87ce7b912a0ef89 [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 Guy49c5fc02012-05-15 11:10:01 -0700147bool OpenGLRenderer::isDeferred() {
148 return false;
149}
150
Romain Guy85bf02f2010-06-22 13:11:24 -0700151void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700152 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700153
154 mWidth = width;
155 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700156
157 mFirstSnapshot->height = height;
158 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700159
Romain Guy3e263fa2011-12-12 16:47:48 -0800160 glDisable(GL_DITHER);
Romain Guy39d252a2011-12-12 18:14:06 -0800161 glEnable(GL_SCISSOR_TEST);
Romain Guy3e263fa2011-12-12 16:47:48 -0800162 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800163
Romain Guy3e263fa2011-12-12 16:47:48 -0800164 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700165}
166
Romain Guy6b7bd242010-10-06 19:49:23 -0700167void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800168 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
169}
170
171void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800172 mCaches.clearGarbage();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700173 mFunctors.clear();
Romain Guyfe48f652010-11-11 15:36:56 -0800174
Romain Guy8aef54f2010-09-01 15:13:49 -0700175 mSnapshot = new Snapshot(mFirstSnapshot,
176 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800177 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700178 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700179
Romain Guy7d7b5492011-01-24 16:33:45 -0800180 mSnapshot->setClip(left, top, right, bottom);
Romain Guyddf74372012-05-22 14:07:07 -0700181 mDirtyClip = opaque;
182
183 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800184
Romain Guy6b7bd242010-10-06 19:49:23 -0700185 if (!opaque) {
Romain Guyddf74372012-05-22 14:07:07 -0700186 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700187 glClear(GL_COLOR_BUFFER_BIT);
Romain Guyddf74372012-05-22 14:07:07 -0700188 } else {
189 mCaches.resetScissor();
190 }
191}
192
193void OpenGLRenderer::syncState() {
194 glViewport(0, 0, mWidth, mHeight);
195
196 if (mCaches.blend) {
197 glEnable(GL_BLEND);
198 } else {
199 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700200 }
Romain Guybb9524b2010-06-22 18:56:38 -0700201}
202
Romain Guyb025b9c2010-09-16 14:16:48 -0700203void OpenGLRenderer::finish() {
204#if DEBUG_OPENGL
205 GLenum status = GL_NO_ERROR;
206 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000207 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800208 switch (status) {
Romain Guya44a63a2012-04-26 14:05:02 -0700209 case GL_INVALID_ENUM:
210 ALOGE(" GL_INVALID_ENUM");
211 break;
212 case GL_INVALID_VALUE:
213 ALOGE(" GL_INVALID_VALUE");
214 break;
215 case GL_INVALID_OPERATION:
216 ALOGE(" GL_INVALID_OPERATION");
217 break;
Romain Guya07105b2011-01-10 21:14:18 -0800218 case GL_OUT_OF_MEMORY:
Romain Guya44a63a2012-04-26 14:05:02 -0700219 ALOGE(" Out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800220 break;
221 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700222 }
223#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800224#if DEBUG_MEMORY_USAGE
225 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800226#else
227 if (mCaches.getDebugLevel() & kDebugMemory) {
228 mCaches.dumpMemoryUsage();
229 }
Romain Guyc15008e2010-11-10 11:59:15 -0800230#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700231}
232
Romain Guy6c319ca2011-01-11 14:29:25 -0800233void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700234 if (mCaches.currentProgram) {
235 if (mCaches.currentProgram->isInUse()) {
236 mCaches.currentProgram->remove();
237 mCaches.currentProgram = NULL;
238 }
239 }
Romain Guy50c0f092010-10-19 11:42:22 -0700240 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800241 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800242 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800243 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700244}
245
Romain Guy6c319ca2011-01-11 14:29:25 -0800246void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800247 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
248
249 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800250 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
251
Romain Guyda8532c2010-08-31 11:50:35 -0700252 glEnable(GL_SCISSOR_TEST);
Romain Guy82bc7a72012-01-03 14:13:39 -0800253 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700254 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700255
Romain Guya1d3c912011-12-13 14:55:06 -0800256 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800257 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700258
Romain Guy50c0f092010-10-19 11:42:22 -0700259 mCaches.blend = true;
260 glEnable(GL_BLEND);
261 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
262 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700263}
264
Romain Guyba6be8a2012-04-23 18:22:09 -0700265void OpenGLRenderer::detachFunctor(Functor* functor) {
266 mFunctors.remove(functor);
267}
268
269void OpenGLRenderer::attachFunctor(Functor* functor) {
270 mFunctors.add(functor);
271}
272
Romain Guy8f3b8e32012-03-27 16:33:45 -0700273status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
274 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700275 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700276
Romain Guyba6be8a2012-04-23 18:22:09 -0700277 if (count > 0) {
278 SortedVector<Functor*> functors(mFunctors);
279 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700280
Romain Guyba6be8a2012-04-23 18:22:09 -0700281 DrawGlInfo info;
282 info.clipLeft = 0;
283 info.clipTop = 0;
284 info.clipRight = 0;
285 info.clipBottom = 0;
286 info.isLayer = false;
287 info.width = 0;
288 info.height = 0;
289 memset(info.transform, 0, sizeof(float) * 16);
290
291 for (size_t i = 0; i < count; i++) {
292 Functor* f = functors.itemAt(i);
293 result |= (*f)(DrawGlInfo::kModeProcess, &info);
294
Chris Craikc2c95432012-04-25 15:13:52 -0700295 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700296 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
297 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700298 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700299
Chris Craikc2c95432012-04-25 15:13:52 -0700300 if (result & DrawGlInfo::kStatusInvoke) {
301 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700302 }
303 }
304 }
305
Romain Guyc189ef52012-04-25 20:02:53 -0700306 mCaches.activeTexture(0);
307
Romain Guy8f3b8e32012-03-27 16:33:45 -0700308 return result;
309}
310
311status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800312 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800313 if (mDirtyClip) {
314 setScissorFromClip();
315 }
Romain Guyd643bb52011-03-01 14:55:21 -0800316
Romain Guy80911b82011-03-16 15:30:12 -0700317 Rect clip(*mSnapshot->clipRect);
318 clip.snapToPixelBoundaries();
319
Romain Guyd643bb52011-03-01 14:55:21 -0800320#if RENDER_LAYERS_AS_REGIONS
321 // Since we don't know what the functor will draw, let's dirty
322 // tne entire clip region
323 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800324 dirtyLayerUnchecked(clip, getRegion());
325 }
326#endif
327
Romain Guy08aa2cb2011-03-17 11:06:57 -0700328 DrawGlInfo info;
329 info.clipLeft = clip.left;
330 info.clipTop = clip.top;
331 info.clipRight = clip.right;
332 info.clipBottom = clip.bottom;
333 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700334 info.width = getSnapshot()->viewport.getWidth();
335 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700336 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700337
Romain Guy8f3b8e32012-03-27 16:33:45 -0700338 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800339
Romain Guy8f3b8e32012-03-27 16:33:45 -0700340 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700341 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800342 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700343
Chris Craik65924a32012-04-05 17:52:11 -0700344 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700345 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700346 }
Romain Guycabfcc12011-03-07 18:06:46 -0800347 }
348
Chet Haasedaf98e92011-01-10 14:10:36 -0800349 resume();
Romain Guy65549432012-03-26 16:45:05 -0700350 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800351}
352
Romain Guyf6a11b82010-06-23 17:47:49 -0700353///////////////////////////////////////////////////////////////////////////////
354// State management
355///////////////////////////////////////////////////////////////////////////////
356
Romain Guybb9524b2010-06-22 18:56:38 -0700357int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700358 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700359}
360
361int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700362 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700363}
364
365void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700366 if (mSaveCount > 1) {
367 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700368 }
Romain Guybb9524b2010-06-22 18:56:38 -0700369}
370
371void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700372 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700373
Romain Guy8fb95422010-08-17 18:38:51 -0700374 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700375 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700376 }
Romain Guybb9524b2010-06-22 18:56:38 -0700377}
378
Romain Guy8aef54f2010-09-01 15:13:49 -0700379int OpenGLRenderer::saveSnapshot(int flags) {
380 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700381 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700382}
383
384bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700385 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700386 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700387 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700388
Romain Guybd6b79b2010-06-26 00:13:53 -0700389 sp<Snapshot> current = mSnapshot;
390 sp<Snapshot> previous = mSnapshot->previous;
391
Romain Guyeb993562010-10-05 18:14:38 -0700392 if (restoreOrtho) {
393 Rect& r = previous->viewport;
394 glViewport(r.left, r.top, r.right, r.bottom);
395 mOrthoMatrix.load(current->orthoMatrix);
396 }
397
Romain Guy8b55f372010-08-18 17:10:07 -0700398 mSaveCount--;
399 mSnapshot = previous;
400
Romain Guy2542d192010-08-18 11:47:12 -0700401 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700402 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700403 }
Romain Guy2542d192010-08-18 11:47:12 -0700404
Romain Guy5ec99242010-11-03 16:19:08 -0700405 if (restoreLayer) {
406 composeLayer(current, previous);
407 }
408
Romain Guy2542d192010-08-18 11:47:12 -0700409 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700410}
411
Romain Guyf6a11b82010-06-23 17:47:49 -0700412///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700413// Layers
414///////////////////////////////////////////////////////////////////////////////
415
416int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700417 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700418 const GLuint previousFbo = mSnapshot->fbo;
419 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700420
Romain Guyaf636eb2010-12-09 17:47:21 -0800421 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700422 int alpha = 255;
423 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700424
Romain Guye45362c2010-11-03 19:58:32 -0700425 if (p) {
426 alpha = p->getAlpha();
427 if (!mCaches.extensions.hasFramebufferFetch()) {
428 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
429 if (!isMode) {
430 // Assume SRC_OVER
431 mode = SkXfermode::kSrcOver_Mode;
432 }
433 } else {
434 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700435 }
436 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700437 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700438 }
Romain Guyd55a8612010-06-28 17:42:46 -0700439
Romain Guydbc26d22010-10-11 17:58:29 -0700440 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
441 }
Romain Guyd55a8612010-06-28 17:42:46 -0700442
443 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700444}
445
446int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
447 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700448 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700449 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700450 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700451 SkPaint paint;
452 paint.setAlpha(alpha);
453 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700454 }
Romain Guyd55a8612010-06-28 17:42:46 -0700455}
Romain Guybd6b79b2010-06-26 00:13:53 -0700456
Romain Guy1c740bc2010-09-13 18:00:09 -0700457/**
458 * Layers are viewed by Skia are slightly different than layers in image editing
459 * programs (for instance.) When a layer is created, previously created layers
460 * and the frame buffer still receive every drawing command. For instance, if a
461 * layer is created and a shape intersecting the bounds of the layers and the
462 * framebuffer is draw, the shape will be drawn on both (unless the layer was
463 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
464 *
465 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
466 * texture. Unfortunately, this is inefficient as it requires every primitive to
467 * be drawn n + 1 times, where n is the number of active layers. In practice this
468 * means, for every primitive:
469 * - Switch active frame buffer
470 * - Change viewport, clip and projection matrix
471 * - Issue the drawing
472 *
473 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700474 * To avoid this, layers are implemented in a different way here, at least in the
475 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
476 * is set. When this flag is set we can redirect all drawing operations into a
477 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700478 *
479 * This implementation relies on the frame buffer being at least RGBA 8888. When
480 * a layer is created, only a texture is created, not an FBO. The content of the
481 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700482 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700483 * buffer and drawing continues as normal. This technique therefore treats the
484 * frame buffer as a scratch buffer for the layers.
485 *
486 * To compose the layers back onto the frame buffer, each layer texture
487 * (containing the original frame buffer data) is drawn as a simple quad over
488 * the frame buffer. The trick is that the quad is set as the composition
489 * destination in the blending equation, and the frame buffer becomes the source
490 * of the composition.
491 *
492 * Drawing layers with an alpha value requires an extra step before composition.
493 * An empty quad is drawn over the layer's region in the frame buffer. This quad
494 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
495 * quad is used to multiply the colors in the frame buffer. This is achieved by
496 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
497 * GL_ZERO, GL_SRC_ALPHA.
498 *
499 * Because glCopyTexImage2D() can be slow, an alternative implementation might
500 * be use to draw a single clipped layer. The implementation described above
501 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700502 *
503 * (1) The frame buffer is actually not cleared right away. To allow the GPU
504 * to potentially optimize series of calls to glCopyTexImage2D, the frame
505 * buffer is left untouched until the first drawing operation. Only when
506 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700507 */
Romain Guyd55a8612010-06-28 17:42:46 -0700508bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700509 float right, float bottom, int alpha, SkXfermode::Mode mode,
510 int flags, GLuint previousFbo) {
511 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700512 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700513
Romain Guyeb993562010-10-05 18:14:38 -0700514 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
515
Romain Guyf607bdc2010-09-10 19:20:06 -0700516 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700517 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700518 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700519 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700520
Romain Guyeb993562010-10-05 18:14:38 -0700521 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700522 if (bounds.intersect(*snapshot->clipRect)) {
523 // We cannot work with sub-pixels in this case
524 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700525
Romain Guyad37cd32011-03-15 11:12:25 -0700526 // When the layer is not an FBO, we may use glCopyTexImage so we
527 // need to make sure the layer does not extend outside the bounds
528 // of the framebuffer
529 if (!bounds.intersect(snapshot->previous->viewport)) {
530 bounds.setEmpty();
531 }
532 } else {
533 bounds.setEmpty();
534 }
Romain Guyeb993562010-10-05 18:14:38 -0700535 }
Romain Guybf434112010-09-16 14:40:17 -0700536
Romain Guy746b7402010-10-26 16:27:31 -0700537 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
538 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800539 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700540 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700541 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700542 }
543
544 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800545 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700546 return false;
547 }
Romain Guyf18fd992010-07-08 11:45:51 -0700548
Romain Guya1d3c912011-12-13 14:55:06 -0800549 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700550 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700551 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700552 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700553 }
554
Romain Guy9ace8f52011-07-07 20:50:11 -0700555 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700556 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700557 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
558 bounds.getWidth() / float(layer->getWidth()), 0.0f);
559 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700560 layer->setBlend(true);
Romain Guydda57022010-07-06 11:39:32 -0700561
Romain Guy8fb95422010-08-17 18:38:51 -0700562 // Save the layer in the snapshot
563 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700564 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700565
Romain Guyeb993562010-10-05 18:14:38 -0700566 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700567 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700568 } else {
569 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700570 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800571 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700572 if (layer->isEmpty()) {
573 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
574 bounds.left, snapshot->height - bounds.bottom,
575 layer->getWidth(), layer->getHeight(), 0);
576 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800577 } else {
578 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
579 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
580 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700581
Romain Guy54be1cd2011-06-13 19:04:27 -0700582 // Enqueue the buffer coordinates to clear the corresponding region later
583 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700584 }
Romain Guyeb993562010-10-05 18:14:38 -0700585 }
Romain Guyf86ef572010-07-01 11:05:42 -0700586
Romain Guyd55a8612010-06-28 17:42:46 -0700587 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700588}
589
Romain Guy5b3b3522010-10-27 18:57:51 -0700590bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
591 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700592 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700593
594#if RENDER_LAYERS_AS_REGIONS
595 snapshot->region = &snapshot->layer->region;
596 snapshot->flags |= Snapshot::kFlagFboTarget;
597#endif
598
599 Rect clip(bounds);
600 snapshot->transform->mapRect(clip);
601 clip.intersect(*snapshot->clipRect);
602 clip.snapToPixelBoundaries();
603 clip.intersect(snapshot->previous->viewport);
604
605 mat4 inverse;
606 inverse.loadInverse(*mSnapshot->transform);
607
608 inverse.mapRect(clip);
609 clip.snapToPixelBoundaries();
610 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700611 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700612
613 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700614 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700615 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700616 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
617 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
618 snapshot->height = bounds.getHeight();
619 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
620 snapshot->orthoMatrix.load(mOrthoMatrix);
621
622 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700623 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
624 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700625
626 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700627 if (layer->isEmpty()) {
628 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
629 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700630 }
631
632 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700633 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700634
635#if DEBUG_LAYERS_AS_REGIONS
636 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
637 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000638 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700639
640 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700641 layer->deleteTexture();
642 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700643
644 delete layer;
645
646 return false;
647 }
648#endif
649
650 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800651 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700652 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700653 glClear(GL_COLOR_BUFFER_BIT);
654
655 dirtyClip();
656
657 // Change the ortho projection
658 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
659 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
660
661 return true;
662}
663
Romain Guy1c740bc2010-09-13 18:00:09 -0700664/**
665 * Read the documentation of createLayer() before doing anything in this method.
666 */
Romain Guy1d83e192010-08-17 11:37:00 -0700667void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
668 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000669 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700670 return;
671 }
672
Romain Guy5b3b3522010-10-27 18:57:51 -0700673 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700674
675 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700676 // Detach the texture from the FBO
677 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
678
Romain Guyeb993562010-10-05 18:14:38 -0700679 // Unbind current FBO and restore previous one
680 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
681 }
682
Romain Guy1d83e192010-08-17 11:37:00 -0700683 Layer* layer = current->layer;
684 const Rect& rect = layer->layer;
685
Romain Guy9ace8f52011-07-07 20:50:11 -0700686 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700687 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700688 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700689 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700690 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700691 }
692
Romain Guy03750a02010-10-18 14:06:08 -0700693 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700694
Romain Guya1d3c912011-12-13 14:55:06 -0800695 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700696
Romain Guy5b3b3522010-10-27 18:57:51 -0700697 // When the layer is stored in an FBO, we can save a bit of fillrate by
698 // drawing only the dirty region
699 if (fboLayer) {
700 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700701 if (layer->getColorFilter()) {
702 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800703 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700704 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700705 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800706 resetColorFilter();
707 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700708 } else if (!rect.isEmpty()) {
709 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
710 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700711 }
Romain Guy8b55f372010-08-18 17:10:07 -0700712
Romain Guyeb993562010-10-05 18:14:38 -0700713 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800714 // Note: No need to use glDiscardFramebufferEXT() since we never
715 // create/compose layers that are not on screen with this
716 // code path
717 // See LayerRenderer::destroyLayer(Layer*)
718
Romain Guyeb993562010-10-05 18:14:38 -0700719 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
720 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700721 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700722 }
723
Romain Guy746b7402010-10-26 16:27:31 -0700724 dirtyClip();
725
Romain Guyeb993562010-10-05 18:14:38 -0700726 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700727 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700728 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700729 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700730 delete layer;
731 }
732}
733
Romain Guyaa6c24c2011-04-28 18:40:04 -0700734void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700735 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700736
Romain Guy302a9df2011-08-16 13:55:02 -0700737 mat4& transform = layer->getTransform();
738 if (!transform.isIdentity()) {
739 save(0);
740 mSnapshot->transform->multiply(transform);
741 }
742
Romain Guyaa6c24c2011-04-28 18:40:04 -0700743 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700744 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700745 setupDrawWithTexture();
746 } else {
747 setupDrawWithExternalTexture();
748 }
749 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700750 setupDrawColor(alpha, alpha, alpha, alpha);
751 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700752 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700753 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700754 setupDrawPureColorUniforms();
755 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700756 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
757 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700758 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700759 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700760 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700761 if (mSnapshot->transform->isPureTranslate() &&
762 layer->getWidth() == (uint32_t) rect.getWidth() &&
763 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700764 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
765 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
766
Romain Guyd21b6e12011-11-30 20:21:23 -0800767 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700768 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
769 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800770 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700771 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
772 }
773 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700774 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
775
776 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
777
778 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700779
780 if (!transform.isIdentity()) {
781 restore();
782 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700783}
784
Romain Guy5b3b3522010-10-27 18:57:51 -0700785void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700786 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700787 const Rect& texCoords = layer->texCoords;
788 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
789 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700790
Romain Guy9ace8f52011-07-07 20:50:11 -0700791 float x = rect.left;
792 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700793 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700794 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700795 layer->getHeight() == (uint32_t) rect.getHeight();
796
797 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700798 // When we're swapping, the layer is already in screen coordinates
799 if (!swap) {
800 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
801 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
802 }
803
Romain Guyd21b6e12011-11-30 20:21:23 -0800804 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700805 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800806 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700807 }
808
809 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
810 layer->getTexture(), layer->getAlpha() / 255.0f,
811 layer->getMode(), layer->isBlend(),
812 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
813 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700814
Romain Guyaa6c24c2011-04-28 18:40:04 -0700815 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
816 } else {
817 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
818 drawTextureLayer(layer, rect);
819 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
820 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700821}
822
823void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
824#if RENDER_LAYERS_AS_REGIONS
825 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700826 layer->setRegionAsRect();
827
Romain Guy40667672011-03-18 14:34:03 -0700828 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700829
Romain Guy5b3b3522010-10-27 18:57:51 -0700830 layer->region.clear();
831 return;
832 }
833
Romain Guy8a3957d2011-09-07 17:55:15 -0700834 // TODO: See LayerRenderer.cpp::generateMesh() for important
835 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800836 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700837 size_t count;
838 const android::Rect* rects = layer->region.getArray(&count);
839
Romain Guy9ace8f52011-07-07 20:50:11 -0700840 const float alpha = layer->getAlpha() / 255.0f;
841 const float texX = 1.0f / float(layer->getWidth());
842 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800843 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700844
845 TextureVertex* mesh = mCaches.getRegionMesh();
846 GLsizei numQuads = 0;
847
Romain Guy7230a742011-01-10 22:26:16 -0800848 setupDraw();
849 setupDrawWithTexture();
850 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800851 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700852 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800853 setupDrawProgram();
854 setupDrawDirtyRegionsDisabled();
855 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800856 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700857 setupDrawTexture(layer->getTexture());
858 if (mSnapshot->transform->isPureTranslate()) {
859 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
860 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
861
Romain Guyd21b6e12011-11-30 20:21:23 -0800862 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700863 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
864 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800865 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700866 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
867 }
Romain Guy15bc6432011-12-13 13:11:32 -0800868 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700869
870 for (size_t i = 0; i < count; i++) {
871 const android::Rect* r = &rects[i];
872
873 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800874 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700875 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800876 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700877
878 // TODO: Reject quads outside of the clip
879 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
880 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
881 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
882 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
883
884 numQuads++;
885
886 if (numQuads >= REGION_MESH_QUAD_COUNT) {
887 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
888 numQuads = 0;
889 mesh = mCaches.getRegionMesh();
890 }
891 }
892
893 if (numQuads > 0) {
894 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
895 }
896
Romain Guy7230a742011-01-10 22:26:16 -0800897 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700898
899#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800900 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700901#endif
902
903 layer->region.clear();
904 }
905#else
906 composeLayerRect(layer, rect);
907#endif
908}
909
Romain Guy3a3133d2011-02-01 22:59:58 -0800910void OpenGLRenderer::drawRegionRects(const Region& region) {
911#if DEBUG_LAYERS_AS_REGIONS
912 size_t count;
913 const android::Rect* rects = region.getArray(&count);
914
915 uint32_t colors[] = {
916 0x7fff0000, 0x7f00ff00,
917 0x7f0000ff, 0x7fff00ff,
918 };
919
920 int offset = 0;
921 int32_t top = rects[0].top;
922
923 for (size_t i = 0; i < count; i++) {
924 if (top != rects[i].top) {
925 offset ^= 0x2;
926 top = rects[i].top;
927 }
928
929 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
930 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
931 SkXfermode::kSrcOver_Mode);
932 }
933#endif
934}
935
Romain Guy5b3b3522010-10-27 18:57:51 -0700936void OpenGLRenderer::dirtyLayer(const float left, const float top,
937 const float right, const float bottom, const mat4 transform) {
938#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800939 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700940 Rect bounds(left, top, right, bottom);
941 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800942 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700943 }
944#endif
945}
946
947void OpenGLRenderer::dirtyLayer(const float left, const float top,
948 const float right, const float bottom) {
949#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800950 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700951 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800952 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800953 }
954#endif
955}
956
957void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
958#if RENDER_LAYERS_AS_REGIONS
959 if (bounds.intersect(*mSnapshot->clipRect)) {
960 bounds.snapToPixelBoundaries();
961 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
962 if (!dirty.isEmpty()) {
963 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700964 }
965 }
966#endif
967}
968
Romain Guy54be1cd2011-06-13 19:04:27 -0700969void OpenGLRenderer::clearLayerRegions() {
970 const size_t count = mLayers.size();
971 if (count == 0) return;
972
973 if (!mSnapshot->isIgnored()) {
974 // Doing several glScissor/glClear here can negatively impact
975 // GPUs with a tiler architecture, instead we draw quads with
976 // the Clear blending mode
977
978 // The list contains bounds that have already been clipped
979 // against their initial clip rect, and the current clip
980 // is likely different so we need to disable clipping here
981 glDisable(GL_SCISSOR_TEST);
982
983 Vertex mesh[count * 6];
984 Vertex* vertex = mesh;
985
986 for (uint32_t i = 0; i < count; i++) {
987 Rect* bounds = mLayers.itemAt(i);
988
989 Vertex::set(vertex++, bounds->left, bounds->bottom);
990 Vertex::set(vertex++, bounds->left, bounds->top);
991 Vertex::set(vertex++, bounds->right, bounds->top);
992 Vertex::set(vertex++, bounds->left, bounds->bottom);
993 Vertex::set(vertex++, bounds->right, bounds->top);
994 Vertex::set(vertex++, bounds->right, bounds->bottom);
995
996 delete bounds;
997 }
998
999 setupDraw(false);
1000 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1001 setupDrawBlending(true, SkXfermode::kClear_Mode);
1002 setupDrawProgram();
1003 setupDrawPureColorUniforms();
1004 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001005 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001006
Romain Guy54be1cd2011-06-13 19:04:27 -07001007 glDrawArrays(GL_TRIANGLES, 0, count * 6);
1008
1009 glEnable(GL_SCISSOR_TEST);
1010 } else {
1011 for (uint32_t i = 0; i < count; i++) {
1012 delete mLayers.itemAt(i);
1013 }
1014 }
1015
1016 mLayers.clear();
1017}
1018
Romain Guybd6b79b2010-06-26 00:13:53 -07001019///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001020// Transforms
1021///////////////////////////////////////////////////////////////////////////////
1022
1023void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001024 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001025}
1026
1027void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001028 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001029}
1030
1031void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001032 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001033}
1034
Romain Guy807daf72011-01-18 11:19:19 -08001035void OpenGLRenderer::skew(float sx, float sy) {
1036 mSnapshot->transform->skew(sx, sy);
1037}
1038
Romain Guyf6a11b82010-06-23 17:47:49 -07001039void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001040 if (matrix) {
1041 mSnapshot->transform->load(*matrix);
1042 } else {
1043 mSnapshot->transform->loadIdentity();
1044 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001045}
1046
1047void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001048 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001049}
1050
1051void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001052 SkMatrix transform;
1053 mSnapshot->transform->copyTo(transform);
1054 transform.preConcat(*matrix);
1055 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001056}
1057
1058///////////////////////////////////////////////////////////////////////////////
1059// Clipping
1060///////////////////////////////////////////////////////////////////////////////
1061
Romain Guybb9524b2010-06-22 18:56:38 -07001062void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001063 Rect clip(*mSnapshot->clipRect);
1064 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001065
1066 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1067 clip.getWidth(), clip.getHeight());
1068
Romain Guy746b7402010-10-26 16:27:31 -07001069 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -07001070}
1071
1072const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001073 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001074}
1075
Romain Guyc7d53492010-06-25 13:41:57 -07001076bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001077 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001078 return true;
1079 }
1080
Romain Guy1d83e192010-08-17 11:37:00 -07001081 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001082 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001083 r.snapToPixelBoundaries();
1084
1085 Rect clipRect(*mSnapshot->clipRect);
1086 clipRect.snapToPixelBoundaries();
1087
1088 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001089}
1090
Romain Guy079ba2c2010-07-16 14:12:24 -07001091bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1092 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001093 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001094 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001095 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001096 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001097}
1098
Chet Haasea23eed82012-04-12 15:19:04 -07001099Rect* OpenGLRenderer::getClipRect() {
1100 return mSnapshot->clipRect;
1101}
1102
Romain Guyf6a11b82010-06-23 17:47:49 -07001103///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001104// Drawing commands
1105///////////////////////////////////////////////////////////////////////////////
1106
Romain Guy54be1cd2011-06-13 19:04:27 -07001107void OpenGLRenderer::setupDraw(bool clear) {
1108 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001109 if (mDirtyClip) {
1110 setScissorFromClip();
1111 }
1112 mDescription.reset();
1113 mSetShaderColor = false;
1114 mColorSet = false;
1115 mColorA = mColorR = mColorG = mColorB = 0.0f;
1116 mTextureUnit = 0;
1117 mTrackDirtyRegions = true;
1118}
1119
1120void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1121 mDescription.hasTexture = true;
1122 mDescription.hasAlpha8Texture = isAlpha8;
1123}
1124
Romain Guyaa6c24c2011-04-28 18:40:04 -07001125void OpenGLRenderer::setupDrawWithExternalTexture() {
1126 mDescription.hasExternalTexture = true;
1127}
1128
Romain Guy15bc6432011-12-13 13:11:32 -08001129void OpenGLRenderer::setupDrawNoTexture() {
1130 mCaches.disbaleTexCoordsVertexArray();
1131}
1132
Chet Haase5b0200b2011-04-13 17:58:08 -07001133void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001134 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001135}
1136
Romain Guyed6fcb02011-03-21 13:11:28 -07001137void OpenGLRenderer::setupDrawPoint(float pointSize) {
1138 mDescription.isPoint = true;
1139 mDescription.pointSize = pointSize;
1140}
1141
Romain Guy70ca14e2010-12-13 18:24:33 -08001142void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001143 setupDrawColor(color, (color >> 24) & 0xFF);
1144}
1145
1146void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1147 mColorA = alpha / 255.0f;
Chet Haasedb8c9a62012-03-21 18:54:18 -07001148 mColorA *= mSnapshot->alpha;
Chet Haase6cfdf452011-04-22 16:42:10 -07001149 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1150 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001151 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001152 mColorR = a * ((color >> 16) & 0xFF);
1153 mColorG = a * ((color >> 8) & 0xFF);
1154 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001155 mColorSet = true;
1156 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1157}
1158
Romain Guy86568192010-12-14 15:55:39 -08001159void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1160 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001161 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1162 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001163 const float a = mColorA / 255.0f;
1164 mColorR = a * ((color >> 16) & 0xFF);
1165 mColorG = a * ((color >> 8) & 0xFF);
1166 mColorB = a * ((color ) & 0xFF);
1167 mColorSet = true;
1168 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1169}
1170
Romain Guy70ca14e2010-12-13 18:24:33 -08001171void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1172 mColorA = a;
1173 mColorR = r;
1174 mColorG = g;
1175 mColorB = b;
1176 mColorSet = true;
1177 mSetShaderColor = mDescription.setColor(r, g, b, a);
1178}
1179
Romain Guy86568192010-12-14 15:55:39 -08001180void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1181 mColorA = a;
1182 mColorR = r;
1183 mColorG = g;
1184 mColorB = b;
1185 mColorSet = true;
1186 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1187}
1188
Romain Guy70ca14e2010-12-13 18:24:33 -08001189void OpenGLRenderer::setupDrawShader() {
1190 if (mShader) {
1191 mShader->describe(mDescription, mCaches.extensions);
1192 }
1193}
1194
1195void OpenGLRenderer::setupDrawColorFilter() {
1196 if (mColorFilter) {
1197 mColorFilter->describe(mDescription, mCaches.extensions);
1198 }
1199}
1200
Romain Guyf09ef512011-05-27 11:43:46 -07001201void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1202 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1203 mColorA = 1.0f;
1204 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001205 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001206 }
1207}
1208
Romain Guy70ca14e2010-12-13 18:24:33 -08001209void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001210 // When the blending mode is kClear_Mode, we need to use a modulate color
1211 // argb=1,0,0,0
1212 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001213 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1214 mDescription, swapSrcDst);
1215}
1216
1217void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001218 // When the blending mode is kClear_Mode, we need to use a modulate color
1219 // argb=1,0,0,0
1220 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001221 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1222 mDescription, swapSrcDst);
1223}
1224
1225void OpenGLRenderer::setupDrawProgram() {
1226 useProgram(mCaches.programCache.get(mDescription));
1227}
1228
1229void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1230 mTrackDirtyRegions = false;
1231}
1232
1233void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1234 bool ignoreTransform) {
1235 mModelView.loadTranslate(left, top, 0.0f);
1236 if (!ignoreTransform) {
1237 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1238 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1239 } else {
1240 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1241 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1242 }
1243}
1244
Chet Haase8a5cc922011-04-26 07:28:09 -07001245void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1246 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001247}
1248
Romain Guy70ca14e2010-12-13 18:24:33 -08001249void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1250 bool ignoreTransform, bool ignoreModelView) {
1251 if (!ignoreModelView) {
1252 mModelView.loadTranslate(left, top, 0.0f);
1253 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001254 } else {
1255 mModelView.loadIdentity();
1256 }
Romain Guy86568192010-12-14 15:55:39 -08001257 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1258 if (!ignoreTransform) {
1259 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1260 if (mTrackDirtyRegions && dirty) {
1261 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1262 }
1263 } else {
1264 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1265 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1266 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001267}
1268
Romain Guyed6fcb02011-03-21 13:11:28 -07001269void OpenGLRenderer::setupDrawPointUniforms() {
1270 int slot = mCaches.currentProgram->getUniform("pointSize");
1271 glUniform1f(slot, mDescription.pointSize);
1272}
1273
Romain Guy70ca14e2010-12-13 18:24:33 -08001274void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001275 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001276 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1277 }
1278}
1279
Romain Guy86568192010-12-14 15:55:39 -08001280void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001281 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001282 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001283 }
1284}
1285
Romain Guy70ca14e2010-12-13 18:24:33 -08001286void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1287 if (mShader) {
1288 if (ignoreTransform) {
1289 mModelView.loadInverse(*mSnapshot->transform);
1290 }
1291 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1292 }
1293}
1294
Romain Guy8d0d4782010-12-14 20:13:35 -08001295void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1296 if (mShader) {
1297 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1298 }
1299}
1300
Romain Guy70ca14e2010-12-13 18:24:33 -08001301void OpenGLRenderer::setupDrawColorFilterUniforms() {
1302 if (mColorFilter) {
1303 mColorFilter->setupProgram(mCaches.currentProgram);
1304 }
1305}
1306
1307void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001308 bool force = mCaches.bindMeshBuffer();
1309 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001310 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001311}
1312
1313void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1314 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001315 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001316 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001317}
1318
Romain Guyaa6c24c2011-04-28 18:40:04 -07001319void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1320 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001321 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001322 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001323}
1324
Romain Guy8f0095c2011-05-02 17:24:22 -07001325void OpenGLRenderer::setupDrawTextureTransform() {
1326 mDescription.hasTextureTransform = true;
1327}
1328
1329void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001330 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1331 GL_FALSE, &transform.data[0]);
1332}
1333
Romain Guy70ca14e2010-12-13 18:24:33 -08001334void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001335 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001336 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001337 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001338 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001339 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001340 }
Romain Guyd71dd362011-12-12 19:03:35 -08001341
Romain Guyf3a910b42011-12-12 20:35:21 -08001342 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001343 if (mCaches.currentProgram->texCoords >= 0) {
1344 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1345 }
1346
1347 mCaches.unbindIndicesBuffer();
1348}
1349
1350void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1351 bool force = mCaches.unbindMeshBuffer();
1352 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1353 if (mCaches.currentProgram->texCoords >= 0) {
1354 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001355 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001356}
1357
Chet Haase5b0200b2011-04-13 17:58:08 -07001358void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001359 bool force = mCaches.unbindMeshBuffer();
1360 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1361 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001362 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001363}
1364
1365/**
1366 * 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 -07001367 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1368 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1369 * attributes (one per vertex) are values from zero to one that tells the fragment
1370 * shader where the fragment is in relation to the line width/length overall; these values are
1371 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1372 * region of the line.
1373 * Note that we only pass down the width values in this setup function. The length coordinates
1374 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001375 */
Chet Haase99585ad2011-05-02 15:00:16 -07001376void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001377 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001378 bool force = mCaches.unbindMeshBuffer();
1379 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1380 vertices, gAAVertexStride);
1381 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001382 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001383
Romain Guy7b631422012-04-04 11:38:54 -07001384 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001385 glEnableVertexAttribArray(widthSlot);
1386 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001387
Romain Guy7b631422012-04-04 11:38:54 -07001388 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001389 glEnableVertexAttribArray(lengthSlot);
1390 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001391
Chet Haase5b0200b2011-04-13 17:58:08 -07001392 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001393 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001394
Chet Haase99585ad2011-05-02 15:00:16 -07001395 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001396 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Romain Guy7b631422012-04-04 11:38:54 -07001397 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion);
1398}
1399
1400void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1401 glDisableVertexAttribArray(widthSlot);
1402 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001403}
1404
Romain Guy70ca14e2010-12-13 18:24:33 -08001405void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001406}
1407
1408///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001409// Drawing
1410///////////////////////////////////////////////////////////////////////////////
1411
Chet Haase1271e2c2012-04-20 09:54:27 -07001412status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001413 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001414
Romain Guy0fe478e2010-11-08 12:08:41 -08001415 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1416 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001417 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001418 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001419 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001420
Romain Guy65549432012-03-26 16:45:05 -07001421 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001422}
1423
Chet Haaseed30fd82011-04-22 16:18:45 -07001424void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1425 if (displayList) {
1426 displayList->output(*this, level);
1427 }
1428}
1429
Romain Guya168d732011-03-18 16:50:13 -07001430void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1431 int alpha;
1432 SkXfermode::Mode mode;
1433 getAlphaAndMode(paint, &alpha, &mode);
1434
Romain Guya168d732011-03-18 16:50:13 -07001435 float x = left;
1436 float y = top;
1437
Romain Guye3c26852011-07-25 16:36:01 -07001438 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001439 bool ignoreTransform = false;
1440 if (mSnapshot->transform->isPureTranslate()) {
1441 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1442 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1443 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001444 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001445 } else {
1446 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001447 }
1448
1449 setupDraw();
1450 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001451 if (paint) {
1452 setupDrawAlpha8Color(paint->getColor(), alpha);
1453 }
Romain Guya168d732011-03-18 16:50:13 -07001454 setupDrawColorFilter();
1455 setupDrawShader();
1456 setupDrawBlending(true, mode);
1457 setupDrawProgram();
1458 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001459
Romain Guya168d732011-03-18 16:50:13 -07001460 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001461 texture->setWrap(GL_CLAMP_TO_EDGE);
1462 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001463
Romain Guya168d732011-03-18 16:50:13 -07001464 setupDrawPureColorUniforms();
1465 setupDrawColorFilterUniforms();
1466 setupDrawShaderUniforms();
1467 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1468
1469 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1470
1471 finishDrawTexture();
1472}
1473
Chet Haase5c13d892010-10-08 08:37:55 -07001474void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001475 const float right = left + bitmap->width();
1476 const float bottom = top + bitmap->height();
1477
1478 if (quickReject(left, top, right, bottom)) {
1479 return;
1480 }
1481
Romain Guya1d3c912011-12-13 14:55:06 -08001482 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001483 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001484 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001485 const AutoTexture autoCleanup(texture);
1486
Romain Guy211370f2012-02-01 16:10:55 -08001487 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001488 drawAlphaBitmap(texture, left, top, paint);
1489 } else {
1490 drawTextureRect(left, top, right, bottom, texture, paint);
1491 }
Romain Guyce0537b2010-06-29 21:05:21 -07001492}
1493
Chet Haase5c13d892010-10-08 08:37:55 -07001494void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001495 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1496 const mat4 transform(*matrix);
1497 transform.mapRect(r);
1498
Romain Guy6926c722010-07-12 20:20:03 -07001499 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1500 return;
1501 }
1502
Romain Guya1d3c912011-12-13 14:55:06 -08001503 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001504 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001505 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001506 const AutoTexture autoCleanup(texture);
1507
Romain Guy5b3b3522010-10-27 18:57:51 -07001508 // This could be done in a cheaper way, all we need is pass the matrix
1509 // to the vertex shader. The save/restore is a bit overkill.
1510 save(SkCanvas::kMatrix_SaveFlag);
1511 concatMatrix(matrix);
1512 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1513 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001514}
1515
Romain Guye651cc62012-05-14 19:44:40 -07001516void OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
1517 const float right = left + bitmap->width();
1518 const float bottom = top + bitmap->height();
1519
1520 if (quickReject(left, top, right, bottom)) {
1521 return;
1522 }
1523
1524 mCaches.activeTexture(0);
1525 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1526 const AutoTexture autoCleanup(texture);
1527
1528 drawTextureRect(left, top, right, bottom, texture, paint);
1529}
1530
Romain Guy5a7b4662011-01-20 19:09:30 -08001531void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1532 float* vertices, int* colors, SkPaint* paint) {
1533 // TODO: Do a quickReject
1534 if (!vertices || mSnapshot->isIgnored()) {
1535 return;
1536 }
1537
Romain Guya1d3c912011-12-13 14:55:06 -08001538 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001539 Texture* texture = mCaches.textureCache.get(bitmap);
1540 if (!texture) return;
1541 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001542
Romain Guyd21b6e12011-11-30 20:21:23 -08001543 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1544 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001545
1546 int alpha;
1547 SkXfermode::Mode mode;
1548 getAlphaAndMode(paint, &alpha, &mode);
1549
Romain Guy5a7b4662011-01-20 19:09:30 -08001550 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001551
Romain Guyb18d2d02011-02-10 15:52:54 -08001552 float left = FLT_MAX;
1553 float top = FLT_MAX;
1554 float right = FLT_MIN;
1555 float bottom = FLT_MIN;
1556
1557#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001558 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001559#else
Romain Guy211370f2012-02-01 16:10:55 -08001560 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001561#endif
1562
Romain Guya566b7c2011-01-23 16:36:11 -08001563 // TODO: Support the colors array
1564 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001565 TextureVertex* vertex = mesh;
1566 for (int32_t y = 0; y < meshHeight; y++) {
1567 for (int32_t x = 0; x < meshWidth; x++) {
1568 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1569
1570 float u1 = float(x) / meshWidth;
1571 float u2 = float(x + 1) / meshWidth;
1572 float v1 = float(y) / meshHeight;
1573 float v2 = float(y + 1) / meshHeight;
1574
1575 int ax = i + (meshWidth + 1) * 2;
1576 int ay = ax + 1;
1577 int bx = i;
1578 int by = bx + 1;
1579 int cx = i + 2;
1580 int cy = cx + 1;
1581 int dx = i + (meshWidth + 1) * 2 + 2;
1582 int dy = dx + 1;
1583
1584 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1585 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1586 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1587
1588 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1589 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1590 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001591
1592#if RENDER_LAYERS_AS_REGIONS
1593 if (hasActiveLayer) {
1594 // TODO: This could be optimized to avoid unnecessary ops
1595 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1596 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1597 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1598 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1599 }
1600#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001601 }
1602 }
1603
Romain Guyb18d2d02011-02-10 15:52:54 -08001604#if RENDER_LAYERS_AS_REGIONS
1605 if (hasActiveLayer) {
1606 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1607 }
1608#endif
1609
Romain Guy5a7b4662011-01-20 19:09:30 -08001610 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1611 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001612 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001613}
1614
Romain Guy8ba548f2010-06-30 19:21:21 -07001615void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1616 float srcLeft, float srcTop, float srcRight, float srcBottom,
1617 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001618 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001619 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1620 return;
1621 }
1622
Romain Guya1d3c912011-12-13 14:55:06 -08001623 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001624 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001625 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001626 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001627
Romain Guy8ba548f2010-06-30 19:21:21 -07001628 const float width = texture->width;
1629 const float height = texture->height;
1630
Romain Guy68169722011-08-22 17:33:33 -07001631 const float u1 = fmax(0.0f, srcLeft / width);
1632 const float v1 = fmax(0.0f, srcTop / height);
1633 const float u2 = fmin(1.0f, srcRight / width);
1634 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001635
Romain Guy03750a02010-10-18 14:06:08 -07001636 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001637 resetDrawTextureTexCoords(u1, v1, u2, v2);
1638
Romain Guy03750a02010-10-18 14:06:08 -07001639 int alpha;
1640 SkXfermode::Mode mode;
1641 getAlphaAndMode(paint, &alpha, &mode);
1642
Romain Guyd21b6e12011-11-30 20:21:23 -08001643 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1644
Romain Guy211370f2012-02-01 16:10:55 -08001645 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001646 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1647 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1648
Romain Guyb5014982011-07-28 15:39:12 -07001649 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001650 // Enable linear filtering if the source rectangle is scaled
1651 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001652 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001653 }
Romain Guyb5014982011-07-28 15:39:12 -07001654
Romain Guyd21b6e12011-11-30 20:21:23 -08001655 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001656 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1657 texture->id, alpha / 255.0f, mode, texture->blend,
1658 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1659 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1660 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001661 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001662 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1663 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1664 GL_TRIANGLE_STRIP, gMeshCount);
1665 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001666
1667 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1668}
1669
Romain Guy4aa90572010-09-26 18:40:37 -07001670void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001671 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001672 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001673 if (quickReject(left, top, right, bottom)) {
1674 return;
1675 }
1676
Romain Guya1d3c912011-12-13 14:55:06 -08001677 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001678 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001679 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001680 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001681 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1682 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001683
1684 int alpha;
1685 SkXfermode::Mode mode;
1686 getAlphaAndMode(paint, &alpha, &mode);
1687
Romain Guy2728f962010-10-08 18:36:15 -07001688 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001689 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001690
Romain Guy211370f2012-02-01 16:10:55 -08001691 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001692 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001693#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001694 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001695 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001696 const float offsetX = left + mSnapshot->transform->getTranslateX();
1697 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001698 const size_t count = mesh->quads.size();
1699 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001700 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001701 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001702 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1703 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1704 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001705 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001706 dirtyLayer(left + bounds.left, top + bounds.top,
1707 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001708 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001709 }
1710 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001711#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001712
Romain Guy211370f2012-02-01 16:10:55 -08001713 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001714 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1715 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1716
1717 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1718 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1719 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1720 true, !mesh->hasEmptyQuads);
1721 } else {
1722 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1723 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1724 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1725 true, !mesh->hasEmptyQuads);
1726 }
Romain Guy054dc182010-10-15 17:55:25 -07001727 }
Romain Guyf7f93552010-07-08 19:17:03 -07001728}
1729
Chet Haase99ecdc42011-05-06 12:06:34 -07001730/**
Chet Haase858aa932011-05-12 09:06:00 -07001731 * This function uses a similar approach to that of AA lines in the drawLines() function.
1732 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1733 * shader to compute the translucency of the color, determined by whether a given pixel is
1734 * within that boundary region and how far into the region it is.
1735 */
1736void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001737 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001738 float inverseScaleX = 1.0f;
1739 float inverseScaleY = 1.0f;
1740 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001741 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001742 Matrix4 *mat = mSnapshot->transform;
1743 float m00 = mat->data[Matrix4::kScaleX];
1744 float m01 = mat->data[Matrix4::kSkewY];
1745 float m02 = mat->data[2];
1746 float m10 = mat->data[Matrix4::kSkewX];
1747 float m11 = mat->data[Matrix4::kScaleX];
1748 float m12 = mat->data[6];
1749 float scaleX = sqrt(m00 * m00 + m01 * m01);
1750 float scaleY = sqrt(m10 * m10 + m11 * m11);
1751 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1752 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1753 }
1754
1755 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001756 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001757 setupDrawAALine();
1758 setupDrawColor(color);
1759 setupDrawColorFilter();
1760 setupDrawShader();
1761 setupDrawBlending(true, mode);
1762 setupDrawProgram();
1763 setupDrawModelViewIdentity(true);
1764 setupDrawColorUniforms();
1765 setupDrawColorFilterUniforms();
1766 setupDrawShaderIdentityUniforms();
1767
1768 AAVertex rects[4];
1769 AAVertex* aaVertices = &rects[0];
1770 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1771 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1772
1773 float boundarySizeX = .5 * inverseScaleX;
1774 float boundarySizeY = .5 * inverseScaleY;
1775
1776 // Adjust the rect by the AA boundary padding
1777 left -= boundarySizeX;
1778 right += boundarySizeX;
1779 top -= boundarySizeY;
1780 bottom += boundarySizeY;
1781
1782 float width = right - left;
1783 float height = bottom - top;
1784
Romain Guy7b631422012-04-04 11:38:54 -07001785 int widthSlot;
1786 int lengthSlot;
1787
Chet Haase858aa932011-05-12 09:06:00 -07001788 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1789 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001790 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1791 boundaryWidthProportion, widthSlot, lengthSlot);
1792
Chet Haase858aa932011-05-12 09:06:00 -07001793 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1794 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1795 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001796 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion));
Chet Haase858aa932011-05-12 09:06:00 -07001797
1798 if (!quickReject(left, top, right, bottom)) {
1799 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1800 AAVertex::set(aaVertices++, left, top, 1, 0);
1801 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1802 AAVertex::set(aaVertices++, right, top, 0, 0);
1803 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1804 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1805 }
Romain Guy7b631422012-04-04 11:38:54 -07001806
1807 finishDrawAALine(widthSlot, lengthSlot);
Chet Haase858aa932011-05-12 09:06:00 -07001808}
1809
1810/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001811 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1812 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1813 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1814 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1815 * of the line. Hairlines are more involved because we need to account for transform scaling
1816 * to end up with a one-pixel-wide line in screen space..
1817 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1818 * in combination with values that we calculate and pass down in this method. The basic approach
1819 * is that the quad we create contains both the core line area plus a bounding area in which
1820 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1821 * proportion of the width and the length of a given segment is represented by the boundary
1822 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1823 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1824 * on the inside). This ends up giving the result we want, with pixels that are completely
1825 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1826 * how far into the boundary region they are, which is determined by shader interpolation.
1827 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001828void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1829 if (mSnapshot->isIgnored()) return;
1830
1831 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001832 // We use half the stroke width here because we're going to position the quad
1833 // corner vertices half of the width away from the line endpoints
1834 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001835 // A stroke width of 0 has a special meaning in Skia:
1836 // it draws a line 1 px wide regardless of current transform
1837 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001838
Chet Haase99ecdc42011-05-06 12:06:34 -07001839 float inverseScaleX = 1.0f;
1840 float inverseScaleY = 1.0f;
1841 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001842
Chet Haase8a5cc922011-04-26 07:28:09 -07001843 int alpha;
1844 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001845
Chet Haase8a5cc922011-04-26 07:28:09 -07001846 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001847 int verticesCount = count;
1848 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001849 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001850 verticesCount += (count - 4);
1851 }
1852
1853 if (isHairLine || isAA) {
1854 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1855 // the line on the screen should always be one pixel wide regardless of scale. For
1856 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001857 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001858 Matrix4 *mat = mSnapshot->transform;
1859 float m00 = mat->data[Matrix4::kScaleX];
1860 float m01 = mat->data[Matrix4::kSkewY];
1861 float m02 = mat->data[2];
1862 float m10 = mat->data[Matrix4::kSkewX];
1863 float m11 = mat->data[Matrix4::kScaleX];
1864 float m12 = mat->data[6];
Romain Guy7b631422012-04-04 11:38:54 -07001865
Romain Guy211370f2012-02-01 16:10:55 -08001866 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1867 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001868
Chet Haase99ecdc42011-05-06 12:06:34 -07001869 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1870 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001871
Chet Haase99ecdc42011-05-06 12:06:34 -07001872 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1873 scaled = true;
1874 }
1875 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001876 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001877
1878 getAlphaAndMode(paint, &alpha, &mode);
1879 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001880 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001881 if (isAA) {
1882 setupDrawAALine();
1883 }
1884 setupDrawColor(paint->getColor(), alpha);
1885 setupDrawColorFilter();
1886 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001887 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001888 setupDrawProgram();
1889 setupDrawModelViewIdentity(true);
1890 setupDrawColorUniforms();
1891 setupDrawColorFilterUniforms();
1892 setupDrawShaderIdentityUniforms();
1893
1894 if (isHairLine) {
1895 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001896 halfStrokeWidth = isAA? 1 : .5;
1897 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001898 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001899 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001900 }
Romain Guy7b631422012-04-04 11:38:54 -07001901
1902 int widthSlot;
1903 int lengthSlot;
1904
Chet Haase5b0200b2011-04-13 17:58:08 -07001905 Vertex lines[verticesCount];
1906 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001907
Chet Haase99585ad2011-05-02 15:00:16 -07001908 AAVertex wLines[verticesCount];
1909 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001910
Romain Guy211370f2012-02-01 16:10:55 -08001911 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001912 setupDrawVertices(vertices);
1913 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001914 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1915 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001916 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001917 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1918 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001919 // We will need to calculate the actual width proportion on each segment for
1920 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1921 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001922 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1923 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001924 }
1925
Chet Haase99ecdc42011-05-06 12:06:34 -07001926 AAVertex* prevAAVertex = NULL;
1927 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001928
Chet Haase99585ad2011-05-02 15:00:16 -07001929 int boundaryLengthSlot = -1;
1930 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001931 int boundaryWidthSlot = -1;
1932 int inverseBoundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001933
Chet Haase5b0200b2011-04-13 17:58:08 -07001934 for (int i = 0; i < count; i += 4) {
1935 // a = start point, b = end point
1936 vec2 a(points[i], points[i + 1]);
1937 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001938
Chet Haase99585ad2011-05-02 15:00:16 -07001939 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001940 float boundaryLengthProportion = 0;
1941 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001942
Chet Haase5b0200b2011-04-13 17:58:08 -07001943 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001944 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001945 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001946 if (isAA) {
1947 float wideningFactor;
1948 if (fabs(n.x) >= fabs(n.y)) {
1949 wideningFactor = fabs(1.0f / n.x);
1950 } else {
1951 wideningFactor = fabs(1.0f / n.y);
1952 }
1953 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001954 }
Romain Guy7b631422012-04-04 11:38:54 -07001955
Chet Haase99ecdc42011-05-06 12:06:34 -07001956 if (scaled) {
1957 n.x *= inverseScaleX;
1958 n.y *= inverseScaleY;
1959 }
1960 } else if (scaled) {
1961 // Extend n by .5 pixel on each side, post-transform
1962 vec2 extendedN = n.copyNormalized();
1963 extendedN /= 2;
1964 extendedN.x *= inverseScaleX;
1965 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07001966
Chet Haase99ecdc42011-05-06 12:06:34 -07001967 float extendedNLength = extendedN.length();
1968 // We need to set this value on the shader prior to drawing
1969 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1970 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001971 }
Romain Guy7b631422012-04-04 11:38:54 -07001972
Chet Haase5b0200b2011-04-13 17:58:08 -07001973 float x = n.x;
1974 n.x = -n.y;
1975 n.y = x;
1976
Chet Haase99585ad2011-05-02 15:00:16 -07001977 // aa lines expand the endpoint vertices to encompass the AA boundary
1978 if (isAA) {
1979 vec2 abVector = (b - a);
1980 length = abVector.length();
1981 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07001982
Chet Haase99ecdc42011-05-06 12:06:34 -07001983 if (scaled) {
1984 abVector.x *= inverseScaleX;
1985 abVector.y *= inverseScaleY;
1986 float abLength = abVector.length();
1987 boundaryLengthProportion = abLength / (length + abLength);
1988 } else {
1989 boundaryLengthProportion = .5 / (length + 1);
1990 }
Romain Guy7b631422012-04-04 11:38:54 -07001991
Chet Haase99ecdc42011-05-06 12:06:34 -07001992 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001993 a -= abVector;
1994 b += abVector;
1995 }
1996
Chet Haase5b0200b2011-04-13 17:58:08 -07001997 // Four corners of the rectangle defining a thick line
1998 vec2 p1 = a - n;
1999 vec2 p2 = a + n;
2000 vec2 p3 = b + n;
2001 vec2 p4 = b - n;
2002
Chet Haase99585ad2011-05-02 15:00:16 -07002003
Chet Haase5b0200b2011-04-13 17:58:08 -07002004 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2005 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2006 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2007 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2008
2009 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002010 if (!isAA) {
2011 if (prevVertex != NULL) {
2012 // Issue two repeat vertices to create degenerate triangles to bridge
2013 // between the previous line and the new one. This is necessary because
2014 // we are creating a single triangle_strip which will contain
2015 // potentially discontinuous line segments.
2016 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2017 Vertex::set(vertices++, p1.x, p1.y);
2018 generatedVerticesCount += 2;
2019 }
Romain Guy7b631422012-04-04 11:38:54 -07002020
Chet Haase5b0200b2011-04-13 17:58:08 -07002021 Vertex::set(vertices++, p1.x, p1.y);
2022 Vertex::set(vertices++, p2.x, p2.y);
2023 Vertex::set(vertices++, p4.x, p4.y);
2024 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002025
Chet Haase5b0200b2011-04-13 17:58:08 -07002026 prevVertex = vertices - 1;
2027 generatedVerticesCount += 4;
2028 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002029 if (!isHairLine && scaled) {
2030 // Must set width proportions per-segment for scaled non-hairlines to use the
2031 // correct AA boundary dimensions
2032 if (boundaryWidthSlot < 0) {
2033 boundaryWidthSlot =
2034 mCaches.currentProgram->getUniform("boundaryWidth");
2035 inverseBoundaryWidthSlot =
2036 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
2037 }
Romain Guy7b631422012-04-04 11:38:54 -07002038
Chet Haase99ecdc42011-05-06 12:06:34 -07002039 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
2040 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
2041 }
Romain Guy7b631422012-04-04 11:38:54 -07002042
Chet Haase99585ad2011-05-02 15:00:16 -07002043 if (boundaryLengthSlot < 0) {
2044 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
2045 inverseBoundaryLengthSlot =
2046 mCaches.currentProgram->getUniform("inverseBoundaryLength");
2047 }
Romain Guy7b631422012-04-04 11:38:54 -07002048
Chet Haase99ecdc42011-05-06 12:06:34 -07002049 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
2050 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07002051
Chet Haase5b0200b2011-04-13 17:58:08 -07002052 if (prevAAVertex != NULL) {
2053 // Issue two repeat vertices to create degenerate triangles to bridge
2054 // between the previous line and the new one. This is necessary because
2055 // we are creating a single triangle_strip which will contain
2056 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002057 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2058 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2059 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002060 generatedVerticesCount += 2;
2061 }
Romain Guy7b631422012-04-04 11:38:54 -07002062
Chet Haase99585ad2011-05-02 15:00:16 -07002063 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2064 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2065 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2066 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002067
Chet Haase5b0200b2011-04-13 17:58:08 -07002068 prevAAVertex = aaVertices - 1;
2069 generatedVerticesCount += 4;
2070 }
Romain Guy7b631422012-04-04 11:38:54 -07002071
Chet Haase99585ad2011-05-02 15:00:16 -07002072 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2073 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2074 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002075 }
2076 }
Romain Guy7b631422012-04-04 11:38:54 -07002077
Chet Haase5b0200b2011-04-13 17:58:08 -07002078 if (generatedVerticesCount > 0) {
2079 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2080 }
Romain Guy7b631422012-04-04 11:38:54 -07002081
2082 if (isAA) {
2083 finishDrawAALine(widthSlot, lengthSlot);
2084 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002085}
2086
Romain Guyed6fcb02011-03-21 13:11:28 -07002087void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2088 if (mSnapshot->isIgnored()) return;
2089
2090 // TODO: The paint's cap style defines whether the points are square or circular
2091 // TODO: Handle AA for round points
2092
Chet Haase5b0200b2011-04-13 17:58:08 -07002093 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002094 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002095 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002096 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002097 if (isHairLine) {
2098 // Now that we know it's hairline, we can set the effective width, to be used later
2099 strokeWidth = 1.0f;
2100 }
2101 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002102 int alpha;
2103 SkXfermode::Mode mode;
2104 getAlphaAndMode(paint, &alpha, &mode);
2105
2106 int verticesCount = count >> 1;
2107 int generatedVerticesCount = 0;
2108
2109 TextureVertex pointsData[verticesCount];
2110 TextureVertex* vertex = &pointsData[0];
2111
2112 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002113 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002114 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002115 setupDrawColor(paint->getColor(), alpha);
2116 setupDrawColorFilter();
2117 setupDrawShader();
2118 setupDrawBlending(mode);
2119 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002120 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002121 setupDrawColorUniforms();
2122 setupDrawColorFilterUniforms();
2123 setupDrawPointUniforms();
2124 setupDrawShaderIdentityUniforms();
2125 setupDrawMesh(vertex);
2126
2127 for (int i = 0; i < count; i += 2) {
2128 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2129 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002130
Chet Haase8a5cc922011-04-26 07:28:09 -07002131 float left = points[i] - halfWidth;
2132 float right = points[i] + halfWidth;
2133 float top = points[i + 1] - halfWidth;
2134 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002135
Chet Haase8a5cc922011-04-26 07:28:09 -07002136 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002137 }
2138
2139 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
2140}
2141
Romain Guy85bf02f2010-06-22 13:11:24 -07002142void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002143 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08002144 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002145
Romain Guyae88e5e2010-10-22 17:49:18 -07002146 Rect& clip(*mSnapshot->clipRect);
2147 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002148
Romain Guy3d58c032010-07-14 16:34:53 -07002149 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07002150}
Romain Guy9d5316e2010-06-24 19:30:36 -07002151
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002152void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08002153 if (!texture) return;
2154 const AutoTexture autoCleanup(texture);
2155
2156 const float x = left + texture->left - texture->offset;
2157 const float y = top + texture->top - texture->offset;
2158
2159 drawPathTexture(texture, x, y, paint);
2160}
2161
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002162void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2163 float rx, float ry, SkPaint* paint) {
2164 if (mSnapshot->isIgnored()) return;
2165
Romain Guya1d3c912011-12-13 14:55:06 -08002166 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002167 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2168 right - left, bottom - top, rx, ry, paint);
2169 drawShape(left, top, texture, paint);
2170}
2171
Romain Guy01d58e42011-01-19 21:54:02 -08002172void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2173 if (mSnapshot->isIgnored()) return;
2174
Romain Guya1d3c912011-12-13 14:55:06 -08002175 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002176 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002177 drawShape(x - radius, y - radius, texture, paint);
2178}
Romain Guy01d58e42011-01-19 21:54:02 -08002179
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002180void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2181 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002182
Romain Guya1d3c912011-12-13 14:55:06 -08002183 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002184 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2185 drawShape(left, top, texture, paint);
2186}
2187
Romain Guy8b2f5262011-01-23 16:15:02 -08002188void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2189 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2190 if (mSnapshot->isIgnored()) return;
2191
2192 if (fabs(sweepAngle) >= 360.0f) {
2193 drawOval(left, top, right, bottom, paint);
2194 return;
2195 }
2196
Romain Guya1d3c912011-12-13 14:55:06 -08002197 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002198 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2199 startAngle, sweepAngle, useCenter, paint);
2200 drawShape(left, top, texture, paint);
2201}
2202
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002203void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2204 SkPaint* paint) {
2205 if (mSnapshot->isIgnored()) return;
2206
Romain Guya1d3c912011-12-13 14:55:06 -08002207 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002208 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2209 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002210}
2211
Chet Haase5c13d892010-10-08 08:37:55 -07002212void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002213 if (p->getStyle() != SkPaint::kFill_Style) {
2214 drawRectAsShape(left, top, right, bottom, p);
2215 return;
2216 }
2217
Romain Guy6926c722010-07-12 20:20:03 -07002218 if (quickReject(left, top, right, bottom)) {
2219 return;
2220 }
2221
Romain Guy026c5e162010-06-28 17:12:22 -07002222 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002223 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002224 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2225 if (!isMode) {
2226 // Assume SRC_OVER
2227 mode = SkXfermode::kSrcOver_Mode;
2228 }
2229 } else {
2230 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002231 }
2232
Romain Guy026c5e162010-06-28 17:12:22 -07002233 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002234 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002235 drawAARect(left, top, right, bottom, color, mode);
2236 } else {
2237 drawColorRect(left, top, right, bottom, color, mode);
2238 }
Romain Guyc7d53492010-06-25 13:41:57 -07002239}
Romain Guy9d5316e2010-06-24 19:30:36 -07002240
Romain Guyeb9a5362012-01-17 17:39:26 -08002241void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
2242 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002243 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2244 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guyeb9a5362012-01-17 17:39:26 -08002245 return;
2246 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002247
Romain Guy671d6cf2012-01-18 12:39:17 -08002248 // NOTE: Skia does not support perspective transform on drawPosText yet
2249 if (!mSnapshot->transform->isSimple()) {
2250 return;
2251 }
2252
2253 float x = 0.0f;
2254 float y = 0.0f;
2255 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2256 if (pureTranslate) {
2257 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2258 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2259 }
2260
2261 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2262 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2263 paint->getTextSize());
2264
2265 int alpha;
2266 SkXfermode::Mode mode;
2267 getAlphaAndMode(paint, &alpha, &mode);
2268
2269 // Pick the appropriate texture filtering
2270 bool linearFilter = mSnapshot->transform->changesBounds();
2271 if (pureTranslate && !linearFilter) {
2272 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2273 }
2274
2275 mCaches.activeTexture(0);
2276 setupDraw();
2277 setupDrawDirtyRegionsDisabled();
2278 setupDrawWithTexture(true);
2279 setupDrawAlpha8Color(paint->getColor(), alpha);
2280 setupDrawColorFilter();
2281 setupDrawShader();
2282 setupDrawBlending(true, mode);
2283 setupDrawProgram();
2284 setupDrawModelView(x, y, x, y, pureTranslate, true);
2285 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2286 setupDrawPureColorUniforms();
2287 setupDrawColorFilterUniforms();
2288 setupDrawShaderUniforms(pureTranslate);
2289
2290 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2291 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2292
2293#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002294 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002295#else
Romain Guy211370f2012-02-01 16:10:55 -08002296 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002297#endif
2298
2299 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2300 positions, hasActiveLayer ? &bounds : NULL)) {
2301#if RENDER_LAYERS_AS_REGIONS
2302 if (hasActiveLayer) {
2303 if (!pureTranslate) {
2304 mSnapshot->transform->mapRect(bounds);
2305 }
2306 dirtyLayerUnchecked(bounds, getRegion());
2307 }
2308#endif
2309 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002310}
2311
Romain Guye8e62a42010-07-23 18:55:21 -07002312void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002313 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002314 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2315 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07002316 return;
2317 }
2318
Chet Haasea1cff502012-02-21 13:43:44 -08002319 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002320 switch (paint->getTextAlign()) {
2321 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002322 x -= length / 2.0f;
2323 break;
2324 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002325 x -= length;
2326 break;
2327 default:
2328 break;
2329 }
2330
Romain Guycac5fd32011-12-01 20:08:50 -08002331 SkPaint::FontMetrics metrics;
2332 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002333 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Romain Guycac5fd32011-12-01 20:08:50 -08002334 return;
2335 }
2336
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002337 const float oldX = x;
2338 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002339 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002340 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002341 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2342 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2343 }
2344
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002345#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002346 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002347#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002348
2349 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002350 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002351 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002352
Romain Guy86568192010-12-14 15:55:39 -08002353 int alpha;
2354 SkXfermode::Mode mode;
2355 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002356
Romain Guy211370f2012-02-01 16:10:55 -08002357 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002358 mCaches.activeTexture(0);
2359
Romain Guyb45c0c92010-08-26 20:35:23 -07002360 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002361 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2362 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002363 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002364
Romain Guy740bf2b2011-04-26 15:33:10 -07002365 const float sx = oldX - shadow->left + mShadowDx;
2366 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002367
Romain Guy86568192010-12-14 15:55:39 -08002368 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002369 int shadowColor = mShadowColor;
2370 if (mShader) {
2371 shadowColor = 0xffffffff;
2372 }
Romain Guy86568192010-12-14 15:55:39 -08002373
Romain Guy86568192010-12-14 15:55:39 -08002374 setupDraw();
2375 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002376 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2377 setupDrawColorFilter();
2378 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002379 setupDrawBlending(true, mode);
2380 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002381 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002382 setupDrawTexture(shadow->id);
2383 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002384 setupDrawColorFilterUniforms();
2385 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002386 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2387
Romain Guy0a417492010-08-16 20:26:20 -07002388 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002389 }
2390
Romain Guy6620c6d2010-12-06 18:07:02 -08002391 // Pick the appropriate texture filtering
2392 bool linearFilter = mSnapshot->transform->changesBounds();
2393 if (pureTranslate && !linearFilter) {
2394 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2395 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002396
Romain Guya1d3c912011-12-13 14:55:06 -08002397 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002398 setupDraw();
2399 setupDrawDirtyRegionsDisabled();
2400 setupDrawWithTexture(true);
2401 setupDrawAlpha8Color(paint->getColor(), alpha);
2402 setupDrawColorFilter();
2403 setupDrawShader();
2404 setupDrawBlending(true, mode);
2405 setupDrawProgram();
2406 setupDrawModelView(x, y, x, y, pureTranslate, true);
2407 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2408 setupDrawPureColorUniforms();
2409 setupDrawColorFilterUniforms();
2410 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002411
Romain Guy6620c6d2010-12-06 18:07:02 -08002412 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002413 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2414
2415#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002416 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002417#else
Romain Guy211370f2012-02-01 16:10:55 -08002418 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002419#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002420
Romain Guy6620c6d2010-12-06 18:07:02 -08002421 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002422 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002423#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002424 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002425 if (!pureTranslate) {
2426 mSnapshot->transform->mapRect(bounds);
2427 }
Romain Guyf219da52011-01-16 12:54:25 -08002428 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002429 }
2430#endif
2431 }
Romain Guy694b5192010-07-21 21:33:20 -07002432
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002433 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002434}
2435
Romain Guy325740f2012-02-24 16:48:34 -08002436void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
2437 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002438 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2439 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
2440 return;
2441 }
2442
Romain Guy03d58522012-02-24 17:54:07 -08002443 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2444 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2445 paint->getTextSize());
2446
2447 int alpha;
2448 SkXfermode::Mode mode;
2449 getAlphaAndMode(paint, &alpha, &mode);
2450
2451 mCaches.activeTexture(0);
2452 setupDraw();
2453 setupDrawDirtyRegionsDisabled();
2454 setupDrawWithTexture(true);
2455 setupDrawAlpha8Color(paint->getColor(), alpha);
2456 setupDrawColorFilter();
2457 setupDrawShader();
2458 setupDrawBlending(true, mode);
2459 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002460 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002461 setupDrawTexture(fontRenderer.getTexture(true));
2462 setupDrawPureColorUniforms();
2463 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002464 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002465
Romain Guy97771732012-02-28 18:17:02 -08002466 const Rect* clip = &mSnapshot->getLocalClip();
2467 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 -08002468
Romain Guy97771732012-02-28 18:17:02 -08002469#if RENDER_LAYERS_AS_REGIONS
2470 const bool hasActiveLayer = hasLayer();
2471#else
2472 const bool hasActiveLayer = false;
2473#endif
2474
2475 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2476 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2477#if RENDER_LAYERS_AS_REGIONS
2478 if (hasActiveLayer) {
2479 mSnapshot->transform->mapRect(bounds);
2480 dirtyLayerUnchecked(bounds, getRegion());
2481 }
2482#endif
2483 }
Romain Guy325740f2012-02-24 16:48:34 -08002484}
2485
Romain Guy7fbcc042010-08-04 15:40:07 -07002486void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002487 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002488
Romain Guya1d3c912011-12-13 14:55:06 -08002489 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002490
Romain Guy33f6beb2012-02-16 19:24:51 -08002491 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002492 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07002493 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002494 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002495
Romain Guy8b55f372010-08-18 17:10:07 -07002496 const float x = texture->left - texture->offset;
2497 const float y = texture->top - texture->offset;
2498
Romain Guy01d58e42011-01-19 21:54:02 -08002499 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002500}
2501
Romain Guyada830f2011-01-13 12:13:20 -08002502void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2503 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002504 return;
2505 }
2506
Romain Guy2bf68f02012-03-02 13:37:47 -08002507 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2508 OpenGLRenderer* renderer = layer->renderer;
2509 Rect& dirty = layer->dirtyRect;
2510
2511 interrupt();
2512 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2513 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002514 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002515 renderer->finish();
2516 resume();
2517
2518 dirty.setEmpty();
2519 layer->deferredUpdateScheduled = false;
2520 layer->renderer = NULL;
2521 layer->displayList = NULL;
2522 }
2523
Romain Guya1d3c912011-12-13 14:55:06 -08002524 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002525
2526 int alpha;
2527 SkXfermode::Mode mode;
2528 getAlphaAndMode(paint, &alpha, &mode);
2529
Romain Guy9ace8f52011-07-07 20:50:11 -07002530 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002531
Romain Guyf219da52011-01-16 12:54:25 -08002532#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002533 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002534 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002535 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002536 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002537 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002538 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002539
Romain Guyc88e3572011-01-22 00:32:12 -08002540 setupDraw();
2541 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002542 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002543 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002544 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002545 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002546 setupDrawPureColorUniforms();
2547 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002548 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002549 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002550 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2551 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2552
Romain Guyd21b6e12011-11-30 20:21:23 -08002553 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002554 setupDrawModelViewTranslate(x, y,
2555 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2556 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002557 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002558 setupDrawModelViewTranslate(x, y,
2559 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2560 }
Romain Guyc88e3572011-01-22 00:32:12 -08002561 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002562
Romain Guyc88e3572011-01-22 00:32:12 -08002563 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2564 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002565
Romain Guyc88e3572011-01-22 00:32:12 -08002566 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002567
2568#if DEBUG_LAYERS_AS_REGIONS
2569 drawRegionRects(layer->region);
2570#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002571 }
Romain Guyf219da52011-01-16 12:54:25 -08002572 }
2573#else
Romain Guyada830f2011-01-13 12:13:20 -08002574 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2575 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002576#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002577}
2578
Romain Guy6926c722010-07-12 20:20:03 -07002579///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002580// Shaders
2581///////////////////////////////////////////////////////////////////////////////
2582
2583void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002584 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002585}
2586
Romain Guy06f96e22010-07-30 19:18:16 -07002587void OpenGLRenderer::setupShader(SkiaShader* shader) {
2588 mShader = shader;
2589 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002590 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002591 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002592}
2593
Romain Guyd27977d2010-07-14 19:18:51 -07002594///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002595// Color filters
2596///////////////////////////////////////////////////////////////////////////////
2597
2598void OpenGLRenderer::resetColorFilter() {
2599 mColorFilter = NULL;
2600}
2601
2602void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2603 mColorFilter = filter;
2604}
2605
2606///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002607// Drop shadow
2608///////////////////////////////////////////////////////////////////////////////
2609
2610void OpenGLRenderer::resetShadow() {
2611 mHasShadow = false;
2612}
2613
2614void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2615 mHasShadow = true;
2616 mShadowRadius = radius;
2617 mShadowDx = dx;
2618 mShadowDy = dy;
2619 mShadowColor = color;
2620}
2621
2622///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002623// Draw filters
2624///////////////////////////////////////////////////////////////////////////////
2625
2626void OpenGLRenderer::resetPaintFilter() {
2627 mHasDrawFilter = false;
2628}
2629
2630void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2631 mHasDrawFilter = true;
2632 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2633 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2634}
2635
2636SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002637 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002638
2639 uint32_t flags = paint->getFlags();
2640
2641 mFilteredPaint = *paint;
2642 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2643
2644 return &mFilteredPaint;
2645}
2646
2647///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002648// Drawing implementation
2649///////////////////////////////////////////////////////////////////////////////
2650
Romain Guy01d58e42011-01-19 21:54:02 -08002651void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2652 float x, float y, SkPaint* paint) {
2653 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2654 return;
2655 }
2656
2657 int alpha;
2658 SkXfermode::Mode mode;
2659 getAlphaAndMode(paint, &alpha, &mode);
2660
2661 setupDraw();
2662 setupDrawWithTexture(true);
2663 setupDrawAlpha8Color(paint->getColor(), alpha);
2664 setupDrawColorFilter();
2665 setupDrawShader();
2666 setupDrawBlending(true, mode);
2667 setupDrawProgram();
2668 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2669 setupDrawTexture(texture->id);
2670 setupDrawPureColorUniforms();
2671 setupDrawColorFilterUniforms();
2672 setupDrawShaderUniforms();
2673 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2674
2675 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2676
2677 finishDrawTexture();
2678}
2679
Romain Guyf607bdc2010-09-10 19:20:06 -07002680// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002681#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2682#define kStdUnderline_Offset (1.0f / 9.0f)
2683#define kStdUnderline_Thickness (1.0f / 18.0f)
2684
2685void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2686 float x, float y, SkPaint* paint) {
2687 // Handle underline and strike-through
2688 uint32_t flags = paint->getFlags();
2689 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002690 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002691 float underlineWidth = length;
2692 // If length is > 0.0f, we already measured the text for the text alignment
2693 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002694 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002695 }
2696
2697 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002698 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002699 case SkPaint::kCenter_Align:
2700 offsetX = underlineWidth * 0.5f;
2701 break;
2702 case SkPaint::kRight_Align:
2703 offsetX = underlineWidth;
2704 break;
2705 default:
2706 break;
2707 }
2708
Romain Guy211370f2012-02-01 16:10:55 -08002709 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002710 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002711 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002712
Romain Guye20ecbd2010-09-22 19:49:04 -07002713 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002714 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002715
Romain Guyf6834472011-01-23 13:32:12 -08002716 int linesCount = 0;
2717 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2718 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2719
2720 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002721 float points[pointsCount];
2722 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002723
2724 if (flags & SkPaint::kUnderlineText_Flag) {
2725 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002726 points[currentPoint++] = left;
2727 points[currentPoint++] = top;
2728 points[currentPoint++] = left + underlineWidth;
2729 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002730 }
2731
2732 if (flags & SkPaint::kStrikeThruText_Flag) {
2733 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002734 points[currentPoint++] = left;
2735 points[currentPoint++] = top;
2736 points[currentPoint++] = left + underlineWidth;
2737 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002738 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002739
Romain Guy726aeba2011-06-01 14:52:00 -07002740 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002741
Romain Guy726aeba2011-06-01 14:52:00 -07002742 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002743 }
2744 }
2745}
2746
Romain Guy026c5e162010-06-28 17:12:22 -07002747void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002748 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002749 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002750 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002751 color |= 0x00ffffff;
2752 }
2753
Romain Guy70ca14e2010-12-13 18:24:33 -08002754 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002755 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002756 setupDrawColor(color);
2757 setupDrawShader();
2758 setupDrawColorFilter();
2759 setupDrawBlending(mode);
2760 setupDrawProgram();
2761 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2762 setupDrawColorUniforms();
2763 setupDrawShaderUniforms(ignoreTransform);
2764 setupDrawColorFilterUniforms();
2765 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002766
Romain Guyc95c8d62010-09-17 15:31:32 -07002767 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2768}
2769
Romain Guy82ba8142010-07-09 13:25:56 -07002770void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002771 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002772 int alpha;
2773 SkXfermode::Mode mode;
2774 getAlphaAndMode(paint, &alpha, &mode);
2775
Romain Guyd21b6e12011-11-30 20:21:23 -08002776 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002777
Romain Guy211370f2012-02-01 16:10:55 -08002778 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002779 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2780 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2781
Romain Guyd21b6e12011-11-30 20:21:23 -08002782 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002783 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2784 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2785 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2786 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002787 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002788 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2789 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2790 GL_TRIANGLE_STRIP, gMeshCount);
2791 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002792}
2793
Romain Guybd6b79b2010-06-26 00:13:53 -07002794void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002795 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2796 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002797 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002798}
2799
2800void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002801 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002802 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002803 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002804
Romain Guy746b7402010-10-26 16:27:31 -07002805 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002806 setupDrawWithTexture();
2807 setupDrawColor(alpha, alpha, alpha, alpha);
2808 setupDrawColorFilter();
2809 setupDrawBlending(blend, mode, swapSrcDst);
2810 setupDrawProgram();
2811 if (!dirty) {
2812 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002813 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002814 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002815 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002816 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002817 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002818 }
Romain Guy86568192010-12-14 15:55:39 -08002819 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002820 setupDrawColorFilterUniforms();
2821 setupDrawTexture(texture);
2822 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002823
Romain Guy6820ac82010-09-15 18:11:50 -07002824 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002825
2826 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002827}
2828
Romain Guya5aed0d2010-09-09 14:42:43 -07002829void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002830 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002831 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002832
Romain Guy82ba8142010-07-09 13:25:56 -07002833 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002834 // These blend modes are not supported by OpenGL directly and have
2835 // to be implemented using shaders. Since the shader will perform
2836 // the blending, turn blending off here
2837 // If the blend mode cannot be implemented using shaders, fall
2838 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002839 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2840 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002841 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002842 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002843
Romain Guy82bc7a72012-01-03 14:13:39 -08002844 if (mCaches.blend) {
2845 glDisable(GL_BLEND);
2846 mCaches.blend = false;
2847 }
2848
2849 return;
2850 } else {
2851 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002852 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002853 }
2854
2855 if (!mCaches.blend) {
2856 glEnable(GL_BLEND);
2857 }
2858
2859 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2860 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2861
2862 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2863 glBlendFunc(sourceMode, destMode);
2864 mCaches.lastSrcMode = sourceMode;
2865 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002866 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002867 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002868 glDisable(GL_BLEND);
2869 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002870 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002871}
2872
Romain Guy889f8d12010-07-29 14:37:42 -07002873bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002874 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002875 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002876 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002877 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002878 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002879 }
Romain Guy6926c722010-07-12 20:20:03 -07002880 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002881}
2882
Romain Guy026c5e162010-06-28 17:12:22 -07002883void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002884 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002885 TextureVertex::setUV(v++, u1, v1);
2886 TextureVertex::setUV(v++, u2, v1);
2887 TextureVertex::setUV(v++, u1, v2);
2888 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002889}
2890
Chet Haase5c13d892010-10-08 08:37:55 -07002891void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002892 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002893 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002894
2895 // Skia draws using the color's alpha channel if < 255
2896 // Otherwise, it uses the paint's alpha
2897 int color = paint->getColor();
2898 *alpha = (color >> 24) & 0xFF;
2899 if (*alpha == 255) {
2900 *alpha = paint->getAlpha();
2901 }
2902 } else {
2903 *mode = SkXfermode::kSrcOver_Mode;
2904 *alpha = 255;
2905 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07002906 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002907}
2908
Romain Guya5aed0d2010-09-09 14:42:43 -07002909SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002910 SkXfermode::Mode resultMode;
2911 if (!SkXfermode::AsMode(mode, &resultMode)) {
2912 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002913 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002914 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002915}
2916
Romain Guy9d5316e2010-06-24 19:30:36 -07002917}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002918}; // namespace android